1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 place.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
26 static Rect* pick_head(ObClient *c)
28 /* try direct parent first */
29 if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
30 return screen_area_monitor(c->desktop,
31 client_monitor(c->transient_for));
34 /* more than one guy in his group (more than just him) */
35 if (c->group && c->group->members->next) {
38 /* try on the client's desktop */
39 for (it = c->group->members; it; it = g_slist_next(it)) {
40 ObClient *itc = it->data;
42 (itc->desktop == c->desktop ||
43 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
44 return screen_area_monitor(c->desktop,
45 client_monitor(it->data));
48 /* try on all desktops */
49 for (it = c->group->members; it; it = g_slist_next(it)) {
50 ObClient *itc = it->data;
52 return screen_area_monitor(c->desktop,
53 client_monitor(it->data));
60 static gboolean place_random(ObClient *client, gint *x, gint *y)
65 area = pick_head(client);
67 area = screen_area_monitor(client->desktop,
68 g_random_int_range(0, screen_num_monitors));
72 r = area->x + area->width - client->frame->area.width;
73 b = area->y + area->height - client->frame->area.height;
75 if (r > l) *x = g_random_int_range(l, r + 1);
77 if (b > t) *y = g_random_int_range(t, b + 1);
83 static GSList* area_add(GSList *list, Rect *a)
85 Rect *r = g_new(Rect, 1);
87 return g_slist_prepend(list, r);
90 static GSList* area_remove(GSList *list, Rect *a)
93 GSList *result = NULL;
95 for (sit = list; sit; sit = g_slist_next(sit)) {
98 if (!RECT_INTERSECTS_RECT(*r, *a)) {
99 result = g_slist_prepend(result, r);
100 r = NULL; /* dont free it */
104 /* Use an intersection of a and r to determine the space
105 around r that we can use.
107 NOTE: the spaces calculated can overlap.
110 RECT_SET_INTERSECTION(isect, *r, *a);
112 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
113 RECT_SET(extra, r->x, r->y,
114 RECT_LEFT(isect) - r->x, r->height);
115 result = area_add(result, &extra);
118 if (RECT_TOP(isect) > RECT_TOP(*r)) {
119 RECT_SET(extra, r->x, r->y,
120 r->width, RECT_TOP(isect) - r->y + 1);
121 result = area_add(result, &extra);
124 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
125 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
126 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
127 result = area_add(result, &extra);
130 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
131 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
132 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
133 result = area_add(result, &extra);
143 static gint area_cmp(gconstpointer p1, gconstpointer p2, gpointer data)
146 const Rect *a1 = p1, *a2 = p2;
148 return MIN((a1->width - carea->width), (a1->height - carea->height)) -
149 MIN((a2->width - carea->width), (a2->height - carea->height));
152 static gboolean place_smart(ObClient *client, gint *x, gint *y,
153 gboolean only_focused)
156 gboolean ret = FALSE;
157 GSList *spaces = NULL, *sit;
160 list = focus_order[client->desktop == DESKTOP_ALL ?
161 screen_desktop : client->desktop];
163 for (i = 0; i < screen_num_monitors; ++i)
164 spaces = area_add(spaces, screen_area_monitor(client->desktop, i));
166 for (it = list; it; it = g_list_next(it)) {
167 ObClient *c = it->data;
169 if (c != client && !c->shaded && client_normal(c)) {
170 spaces = area_remove(spaces, &c->frame->area);
176 spaces = g_slist_sort_with_data(spaces, area_cmp, &client->frame->area);
178 for (sit = spaces; sit; sit = g_slist_next(sit)) {
182 if (r->width >= client->frame->area.width &&
183 r->height >= client->frame->area.height) {
186 *x = r->x + (r->width - client->frame->area.width) / 2;
187 *y = r->y + (r->height - client->frame->area.height) / 2;
197 g_slist_free(spaces);
202 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
209 screen_pointer_pos(&px, &py);
211 for (i = 0; i < screen_num_monitors; ++i) {
212 area = screen_area_monitor(client->desktop, i);
213 if (RECT_CONTAINS(*area, px, py))
216 if (i == screen_num_monitors)
217 area = screen_area_monitor(client->desktop, 0);
221 r = area->x + area->width - client->frame->area.width;
222 b = area->y + area->height - client->frame->area.height;
224 *x = px - client->area.width / 2 - client->frame->size.left;
225 *x = MIN(MAX(*x, l), r);
226 *y = py - client->area.height / 2 - client->frame->size.top;
227 *y = MIN(MAX(*y, t), b);
232 static gboolean place_transient(ObClient *client, gint *x, gint *y)
234 if (client->transient_for) {
235 if (client->transient_for != OB_TRAN_GROUP) {
236 ObClient *c = client;
237 ObClient *p = client->transient_for;
238 *x = (p->frame->area.width - c->frame->area.width) / 2 +
240 *y = (p->frame->area.height - c->frame->area.height) / 2 +
245 gboolean first = TRUE;
247 for (it = client->group->members; it; it = it->next) {
248 ObClient *m = it->data;
249 if (!(m == client || m->transient_for)) {
251 l = RECT_LEFT(m->frame->area);
252 t = RECT_TOP(m->frame->area);
253 r = RECT_RIGHT(m->frame->area);
254 b = RECT_BOTTOM(m->frame->area);
257 l = MIN(l, RECT_LEFT(m->frame->area));
258 t = MIN(t, RECT_TOP(m->frame->area));
259 r = MAX(r, RECT_RIGHT(m->frame->area));
260 b = MAX(b, RECT_BOTTOM(m->frame->area));
265 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
266 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
274 static gboolean place_dialog(ObClient *client, gint *x, gint *y)
276 /* center parentless dialogs on the screen */
277 if (client->type == OB_CLIENT_TYPE_DIALOG) {
280 area = pick_head(client);
282 area = screen_area_monitor(client->desktop, 0);
284 *x = (area->width - client->frame->area.width) / 2 + area->x;
285 *y = (area->height - client->frame->area.height) / 2 + area->y;
291 void place_client(ObClient *client, gint *x, gint *y)
293 if (client->positioned)
295 if (place_transient(client, x, y) ||
296 place_dialog(client, x, y) ||
297 place_smart(client, x, y, FALSE) ||
298 place_smart(client, x, y, TRUE) ||
299 (config_focus_follow ?
300 place_under_mouse(client, x, y) :
301 place_random(client, x, y)))
303 /* get where the client should be */
304 frame_frame_gravity(client->frame, x, y);
306 g_assert_not_reached(); /* the last one better succeed */