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.
23 #include "engine_interface.h"
32 static void add_choice(guint *choice, guint mychoice)
35 for (i = 0; i < screen_num_monitors; ++i) {
36 if (choice[i] == mychoice)
38 else if (choice[i] == screen_num_monitors) {
45 static Rect *pick_pointer_head(ObClient *c)
50 if (screen_pointer_pos(&px, &py)) {
51 for (i = 0; i < screen_num_monitors; ++i) {
52 Rect *monitor = screen_physical_area_monitor(i);
53 gboolean contain = RECT_CONTAINS(*monitor, px, py);
56 return screen_area(c->desktop, i, NULL);
58 g_assert_not_reached();
63 /*! Pick a monitor to place a window on. */
64 static Rect **pick_head(ObClient *c)
72 area = g_new(Rect*, screen_num_monitors);
73 choice = g_new(guint, screen_num_monitors);
74 for (i = 0; i < screen_num_monitors; ++i)
75 choice[i] = screen_num_monitors; /* make them all invalid to start */
77 /* try direct parent first */
78 if ((p = client_direct_parent(c))) {
79 add_choice(choice, client_monitor(p));
80 ob_debug("placement adding choice %d for parent",
84 /* more than one window in its group (more than just this window) */
85 if (client_has_group_siblings(c)) {
88 /* try on the client's desktop */
89 for (it = c->group->members; it; it = g_slist_next(it)) {
90 ObClient *itc = it->data;
92 (itc->desktop == c->desktop ||
93 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
95 add_choice(choice, client_monitor(it->data));
96 ob_debug("placement adding choice %d for group sibling",
97 client_monitor(it->data));
101 /* try on all desktops */
102 for (it = c->group->members; it; it = g_slist_next(it)) {
103 ObClient *itc = it->data;
105 add_choice(choice, client_monitor(it->data));
106 ob_debug("placement adding choice %d for group sibling on "
107 "another desktop", client_monitor(it->data));
112 /* skip this if placing by the mouse position */
113 if (focus_client && client_normal(focus_client) &&
114 config_place_monitor != OB_PLACE_MONITOR_MOUSE)
116 add_choice(choice, client_monitor(focus_client));
117 ob_debug("placement adding choice %d for normal focused window",
118 client_monitor(focus_client));
121 screen_pointer_pos(&px, &py);
123 for (i = 0; i < screen_num_monitors; i++) {
124 Rect *monitor = screen_physical_area_monitor(i);
125 gboolean contain = RECT_CONTAINS(*monitor, px, py);
128 add_choice(choice, i);
129 ob_debug("placement adding choice %d for mouse pointer", i);
134 /* add any leftover choices */
135 for (i = 0; i < screen_num_monitors; ++i)
136 add_choice(choice, i);
138 for (i = 0; i < screen_num_monitors; ++i)
139 area[i] = screen_area(c->desktop, choice[i], NULL);
146 static gboolean place_random(ObClient *client, gint *x, gint *y)
153 frame_engine->frame_get_window_area(client->frame, &area);
155 areas = pick_head(client);
156 i = (config_place_monitor != OB_PLACE_MONITOR_ANY) ?
157 0 : g_random_int_range(0, screen_num_monitors);
161 r = areas[i]->x + areas[i]->width - area.width;
162 b = areas[i]->y + areas[i]->height - area.height;
164 if (r > l) *x = g_random_int_range(l, r + 1);
165 else *x = areas[i]->x;
166 if (b > t) *y = g_random_int_range(t, b + 1);
167 else *y = areas[i]->y;
169 for (i = 0; i < screen_num_monitors; ++i)
176 static GSList* area_add(GSList *list, Rect *a)
178 Rect *r = g_new(Rect, 1);
180 return g_slist_prepend(list, r);
183 static GSList* area_remove(GSList *list, Rect *a)
186 GSList *result = NULL;
188 for (sit = list; sit; sit = g_slist_next(sit)) {
191 if (!RECT_INTERSECTS_RECT(*r, *a)) {
192 result = g_slist_prepend(result, r);
193 /* dont free r, it's moved to the result list */
197 /* Use an intersection of a and r to determine the space
198 around r that we can use.
200 NOTE: the spaces calculated can overlap.
203 RECT_SET_INTERSECTION(isect, *r, *a);
205 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
206 RECT_SET(extra, r->x, r->y,
207 RECT_LEFT(isect) - r->x, r->height);
208 result = area_add(result, &extra);
211 if (RECT_TOP(isect) > RECT_TOP(*r)) {
212 RECT_SET(extra, r->x, r->y,
213 r->width, RECT_TOP(isect) - r->y + 1);
214 result = area_add(result, &extra);
217 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
218 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
219 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
220 result = area_add(result, &extra);
223 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
224 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
225 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
226 result = area_add(result, &extra);
229 /* 'r' is not being added to the result list, so free it */
238 IGNORE_FULLSCREEN = 1,
239 IGNORE_MAXIMIZED = 2,
241 /*IGNORE_SHADED = 3,*/
244 /*IGNORE_NONFOCUS = 1 << 5,*/
249 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
255 GSList *spaces = NULL, *sit, *maxit;
258 areas = pick_head(c);
263 /* try ignoring different things to find empty space */
264 for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
265 /* try all monitors in order of preference, but only the first one
266 if config_place_monitor is MOUSE or ACTIVE */
267 for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
268 1 : screen_num_monitors) && !ret); ++i)
272 /* add the whole monitor */
273 spaces = area_add(spaces, areas[i]);
275 /* go thru all the windows */
276 for (it = client_list; it; it = g_list_next(it)) {
277 ObClient *test = it->data;
279 /* should we ignore this client? */
280 if (screen_showing_desktop) continue;
281 if (c == test) continue;
282 if (test->iconic) continue;
283 if (c->desktop != DESKTOP_ALL) {
284 if (test->desktop != c->desktop &&
285 test->desktop != DESKTOP_ALL) continue;
287 if (test->desktop != screen_desktop &&
288 test->desktop != DESKTOP_ALL) continue;
290 if (test->type == OB_CLIENT_TYPE_SPLASH ||
291 test->type == OB_CLIENT_TYPE_DESKTOP) continue;
294 if ((ignore >= IGNORE_FULLSCREEN) &&
295 test->fullscreen) continue;
296 if ((ignore >= IGNORE_MAXIMIZED) &&
297 test->max_horz && test->max_vert) continue;
298 if ((ignore >= IGNORE_MENUTOOL) &&
299 (test->type == OB_CLIENT_TYPE_MENU ||
300 test->type == OB_CLIENT_TYPE_TOOLBAR) &&
301 client_has_parent(c)) continue;
303 if ((ignore >= IGNORE_SHADED) &&
304 test->shaded) continue;
306 if ((ignore >= IGNORE_NONGROUP) &&
307 client_has_group_siblings(c) &&
308 test->group != c->group) continue;
309 if ((ignore >= IGNORE_BELOW) &&
310 test->layer < c->layer) continue;
312 if ((ignore >= IGNORE_NONFOCUS) &&
313 focus_client != test) continue;
315 /* don't ignore this window, so remove it from the available
318 frame_engine->frame_get_window_area(test->frame, &test_area);
319 spaces = area_remove(spaces, &test_area);
322 if (ignore < IGNORE_DOCK) {
325 spaces = area_remove(spaces, &a);
329 frame_engine->frame_get_window_area(c->frame, &c_area);
330 for (sit = spaces; sit; sit = g_slist_next(sit)) {
333 if (r->width >= c_area.width &&
334 r->height >= c_area.height &&
335 r->width * r->height > maxsize)
337 maxsize = r->width * r->height;
343 Rect *r = maxit->data;
345 /* center it in the area */
348 if (config_place_center) {
349 *x += (r->width - c_area.width) / 2;
350 *y += (r->height - c_area.height) / 2;
356 g_free(spaces->data);
357 spaces = g_slist_delete_link(spaces, spaces);
362 for (i = 0; i < screen_num_monitors; ++i)
368 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
375 frame_engine->frame_get_size(client->frame, &fsize);
377 frame_engine->frame_get_window_area(client->frame, &farea);
379 if (!screen_pointer_pos(&px, &py))
381 area = pick_pointer_head(client);
385 r = area->x + area->width - farea.width;
386 b = area->y + area->height - farea.height;
388 *x = px - client->area.width / 2 - fsize.left;
389 *x = MIN(MAX(*x, l), r);
390 *y = py - client->area.height / 2 - fsize.top;
391 *y = MIN(MAX(*y, t), b);
396 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
397 ObAppSettings *settings)
401 if (!settings || (settings && !settings->pos_given))
404 /* Find which head the pointer is on */
405 if (settings->monitor == 0)
406 /* this can return NULL */
407 screen = pick_pointer_head(client);
408 else if (settings->monitor > 0 &&
409 (guint)settings->monitor <= screen_num_monitors)
410 screen = screen_area(client->desktop, (guint)settings->monitor - 1,
413 /* if we have't found a screen yet.. */
418 areas = pick_head(client);
421 /* don't free the first one, it's being set as "screen" */
422 for (i = 1; i < screen_num_monitors; ++i)
428 frame_engine->frame_get_window_area(client->frame, &farea);
429 if (settings->position.x.center)
430 *x = screen->x + screen->width / 2 - client->area.width / 2;
431 else if (settings->position.x.opposite)
432 *x = screen->x + screen->width - farea.width -
433 settings->position.x.pos;
435 *x = screen->x + settings->position.x.pos;
437 if (settings->position.y.center)
438 *y = screen->y + screen->height / 2 - client->area.height / 2;
439 else if (settings->position.y.opposite)
440 *y = screen->y + screen->height - farea.height -
441 settings->position.y.pos;
443 *y = screen->y + settings->position.y.pos;
449 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
451 if (client->type == OB_CLIENT_TYPE_DIALOG) {
453 gboolean first = TRUE;
455 for (it = client->parents; it; it = g_slist_next(it)) {
456 ObClient *m = it->data;
458 frame_engine->frame_get_window_area(m->frame, &area);
463 r = RECT_RIGHT(area);
464 b = RECT_BOTTOM(area);
467 l = MIN(l, RECT_LEFT(area));
468 t = MIN(t, RECT_TOP(area));
469 r = MAX(r, RECT_RIGHT(area));
470 b = MAX(b, RECT_BOTTOM(area));
474 *x = ((r + 1 - l) - area.width) / 2 + l;
475 *y = ((b + 1 - t) - area.height) / 2 + t;
481 if (client->type == OB_CLIENT_TYPE_DIALOG ||
482 client->type == OB_CLIENT_TYPE_SPLASH)
486 frame_engine->frame_get_window_area(client->frame, &area);
489 areas = pick_head(client);
491 *x = (areas[0]->width - area.width) / 2 + areas[0]->x;
492 *y = (areas[0]->height - area.height) / 2 + areas[0]->y;
494 for (i = 0; i < screen_num_monitors; ++i)
503 /* Return TRUE if we want client.c to enforce on-screen-keeping */
504 gboolean place_client(ObClient *client, gint *x, gint *y,
505 ObAppSettings *settings)
508 gboolean userplaced = FALSE;
510 /* per-app settings override program specified position
511 * but not user specified, unless pos_force is enabled */
512 if (((client->positioned & USPosition) &&
513 !(settings && settings->pos_given && settings->pos_force)) ||
514 ((client->positioned & PPosition) &&
515 !(settings && settings->pos_given)))
518 /* try a number of methods */
519 ret = place_transient_splash(client, x, y) ||
520 (userplaced = place_per_app_setting(client, x, y, settings)) ||
521 (config_place_policy == OB_PLACE_POLICY_MOUSE &&
522 place_under_mouse(client, x, y)) ||
523 place_nooverlap(client, x, y) ||
524 place_random(client, x, y);
527 /* get where the client should be */
528 frame_frame_gravity(client, x, y);