1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 place.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
27 static void add_choice(guint *choice, guint mychoice)
30 for (i = 0; i < screen_num_monitors; ++i) {
31 if (choice[i] == mychoice)
33 else if (choice[i] == screen_num_monitors) {
40 static Rect *pick_pointer_head(ObClient *c)
45 screen_pointer_pos(&px, &py);
47 for (i = 0; i < screen_num_monitors; ++i) {
48 if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
49 return screen_area_monitor(c->desktop, i);
52 g_assert_not_reached();
55 static Rect **pick_head(ObClient *c)
62 area = g_new(Rect*, screen_num_monitors);
63 choice = g_new(guint, screen_num_monitors);
64 for (i = 0; i < screen_num_monitors; ++i)
65 choice[i] = screen_num_monitors; /* make them all invalid to start */
67 /* try direct parent first */
68 if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
69 add_choice(choice, client_monitor(c->transient_for));
72 /* more than one window in its group (more than just this window) */
73 if (client_has_group_siblings(c)) {
76 /* try on the client's desktop */
77 for (it = c->group->members; it; it = g_slist_next(it)) {
78 ObClient *itc = it->data;
80 (itc->desktop == c->desktop ||
81 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
82 add_choice(choice, client_monitor(it->data));
85 /* try on all desktops */
86 for (it = c->group->members; it; it = g_slist_next(it)) {
87 ObClient *itc = it->data;
89 add_choice(choice, client_monitor(it->data));
94 add_choice(choice, client_monitor(focus_client));
96 screen_pointer_pos(&px, &py);
98 for (i = 0; i < screen_num_monitors; i++)
99 if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
100 add_choice(choice, i);
104 /* add any leftover choices */
105 for (i = 0; i < screen_num_monitors; ++i)
106 add_choice(choice, i);
108 for (i = 0; i < screen_num_monitors; ++i)
109 area[i] = screen_area_monitor(c->desktop, choice[i]);
114 static gboolean place_random(ObClient *client, gint *x, gint *y)
120 areas = pick_head(client);
121 i = g_random_int_range(0, screen_num_monitors);
125 r = areas[i]->x + areas[i]->width - client->frame->area.width;
126 b = areas[i]->y + areas[i]->height - client->frame->area.height;
128 if (r > l) *x = g_random_int_range(l, r + 1);
129 else *x = areas[i]->x;
130 if (b > t) *y = g_random_int_range(t, b + 1);
131 else *y = areas[i]->y;
138 static GSList* area_add(GSList *list, Rect *a)
140 Rect *r = g_new(Rect, 1);
142 return g_slist_prepend(list, r);
145 static GSList* area_remove(GSList *list, Rect *a)
148 GSList *result = NULL;
150 for (sit = list; sit; sit = g_slist_next(sit)) {
153 if (!RECT_INTERSECTS_RECT(*r, *a)) {
154 result = g_slist_prepend(result, r);
155 r = NULL; /* dont free it */
159 /* Use an intersection of a and r to determine the space
160 around r that we can use.
162 NOTE: the spaces calculated can overlap.
165 RECT_SET_INTERSECTION(isect, *r, *a);
167 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
168 RECT_SET(extra, r->x, r->y,
169 RECT_LEFT(isect) - r->x, r->height);
170 result = area_add(result, &extra);
173 if (RECT_TOP(isect) > RECT_TOP(*r)) {
174 RECT_SET(extra, r->x, r->y,
175 r->width, RECT_TOP(isect) - r->y + 1);
176 result = area_add(result, &extra);
179 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
180 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
181 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
182 result = area_add(result, &extra);
185 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
186 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
187 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
188 result = area_add(result, &extra);
198 static gint area_cmp(gconstpointer p1, gconstpointer p2, gpointer data)
201 Rect *carea = &c->frame->area;
202 const Rect *a1 = p1, *a2 = p2;
203 gboolean diffhead = FALSE;
207 for (i = 0; i < screen_num_monitors; ++i) {
208 a = screen_physical_area_monitor(i);
209 if (RECT_CONTAINS(*a, a1->x, a1->y) &&
210 !RECT_CONTAINS(*a, a2->x, a2->y))
217 /* has to be more than me in the group */
218 if (diffhead && client_has_group_siblings(c)) {
222 /* find how many clients in the group are on each monitor, use the
223 monitor with the most in it */
224 num = g_new0(guint, screen_num_monitors);
225 for (it = c->group->members; it; it = g_slist_next(it))
227 ++num[client_monitor(it->data)];
229 for (i = 1; i < screen_num_monitors; ++i)
230 if (num[i] > num[most])
235 a = screen_physical_area_monitor(most);
236 if (RECT_CONTAINS(*a, a1->x, a1->y))
238 if (RECT_CONTAINS(*a, a2->x, a2->y))
242 return MIN((a1->width - carea->width), (a1->height - carea->height)) -
243 MIN((a2->width - carea->width), (a2->height - carea->height));
253 #define SMART_IGNORE(placer, c) \
254 (placer == c || c->shaded || !client_normal(c) || \
255 !frame_visible(c->frame) || \
256 (c->desktop != DESKTOP_ALL && \
257 c->desktop != (placer->desktop == DESKTOP_ALL ? \
258 screen_desktop : placer->desktop)))
260 static gboolean place_smart(ObClient *client, gint *x, gint *y,
263 gboolean ret = FALSE;
264 GSList *spaces = NULL, *sit;
269 if (type == SMART_GROUP) {
270 /* has to be more than me in the group */
271 if (!client_has_group_siblings(client))
275 areas = pick_head(client);
277 for (i = 0; i < screen_num_monitors; ++i) {
278 spaces = area_add(spaces, areas[i]);
280 /* stay out from under windows in higher layers */
281 for (it = stacking_list; it; it = g_list_next(it)) {
284 if (WINDOW_IS_CLIENT(it->data)) {
286 if (c->fullscreen || (c->max_vert && c->max_horz))
291 if (c->layer > client->layer) {
292 if (!SMART_IGNORE(client, c))
293 spaces = area_remove(spaces, &c->frame->area);
298 if (type == SMART_FULL || type == SMART_FOCUSED) {
299 gboolean found_foc = FALSE, stop = FALSE;
302 foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
303 screen_desktop : client->desktop);
305 for (; it && !stop; it = g_list_next(it)) {
308 if (WINDOW_IS_CLIENT(it->data)) {
310 if (c->fullscreen || (c->max_vert && c->max_horz))
315 if (!SMART_IGNORE(client, c)) {
316 if (type == SMART_FOCUSED)
320 spaces = area_remove(spaces, &c->frame->area);
326 } else if (type == SMART_GROUP) {
327 for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
328 ObClient *c = sit->data;
329 if (!SMART_IGNORE(client, c))
330 spaces = area_remove(spaces, &c->frame->area);
333 g_assert_not_reached();
335 spaces = g_slist_sort_with_data(spaces, area_cmp, client);
337 for (sit = spaces; sit; sit = g_slist_next(sit)) {
341 if (r->width >= client->frame->area.width &&
342 r->height >= client->frame->area.height) {
344 if (client->type == OB_CLIENT_TYPE_DIALOG ||
347 *x = r->x + (r->width - client->frame->area.width)/2;
348 *y = r->y + (r->height - client->frame->area.height)/2;
358 g_slist_free(spaces);
367 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
373 area = pick_pointer_head(client);
374 screen_pointer_pos(&px, &py);
378 r = area->x + area->width - client->frame->area.width;
379 b = area->y + area->height - client->frame->area.height;
381 *x = px - client->area.width / 2 - client->frame->size.left;
382 *x = MIN(MAX(*x, l), r);
383 *y = py - client->area.height / 2 - client->frame->size.top;
384 *y = MIN(MAX(*y, t), b);
389 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
390 ObAppSettings *settings)
394 if (!settings || (settings && !settings->pos_given))
397 /* Find which head the pointer is on */
398 if (settings->head == -1)
399 screen = pick_pointer_head(client);
401 screen = screen_area_monitor(client->desktop, settings->head);
403 if (settings->center_x)
404 *x = screen->x + screen->width / 2 - client->area.width / 2;
406 *x = screen->x + settings->position.x;
408 if (settings->center_y)
409 *y = screen->y + screen->height / 2 - client->area.height / 2;
411 *y = screen->y + settings->position.y;
416 static gboolean place_transient(ObClient *client, gint *x, gint *y)
418 if (client->transient_for) {
419 if (client->transient_for != OB_TRAN_GROUP) {
420 ObClient *c = client;
421 ObClient *p = client->transient_for;
423 if (client_normal(p)) {
424 *x = (p->frame->area.width - c->frame->area.width) / 2 +
426 *y = (p->frame->area.height - c->frame->area.height) / 2 +
432 gboolean first = TRUE;
434 for (it = client->group->members; it; it = g_slist_next(it)) {
435 ObClient *m = it->data;
436 if (!(m == client || m->transient_for) && client_normal(m)) {
438 l = RECT_LEFT(m->frame->area);
439 t = RECT_TOP(m->frame->area);
440 r = RECT_RIGHT(m->frame->area);
441 b = RECT_BOTTOM(m->frame->area);
444 l = MIN(l, RECT_LEFT(m->frame->area));
445 t = MIN(t, RECT_TOP(m->frame->area));
446 r = MAX(r, RECT_RIGHT(m->frame->area));
447 b = MAX(b, RECT_BOTTOM(m->frame->area));
452 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
453 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
459 if (client->transient) {
462 areas = pick_head(client);
464 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
465 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
474 /* Return TRUE if we want client.c to enforce on-screen-keeping */
475 gboolean place_client(ObClient *client, gint *x, gint *y,
476 ObAppSettings *settings)
478 gboolean ret = FALSE;
479 if (client->positioned)
481 if (place_transient(client, x, y))
484 place_per_app_setting(client, x, y, settings) ||
485 ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
486 place_under_mouse(client, x, y) :
487 place_smart(client, x, y, SMART_FULL) ||
488 place_smart(client, x, y, SMART_GROUP) ||
489 place_smart(client, x, y, SMART_FOCUSED) ||
490 place_random(client, x, y))))
491 g_assert_not_reached(); /* the last one better succeed */
492 /* get where the client should be */
493 frame_frame_gravity(client->frame, x, y,
494 client->area.width, client->area.height);