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)
152 Rect area = render_plugin->frame_get_window_area(client->frame);
154 areas = pick_head(client);
155 i = (config_place_monitor != OB_PLACE_MONITOR_ANY) ?
156 0 : g_random_int_range(0, screen_num_monitors);
160 r = areas[i]->x + areas[i]->width - area.width;
161 b = areas[i]->y + areas[i]->height - area.height;
163 if (r > l) *x = g_random_int_range(l, r + 1);
164 else *x = areas[i]->x;
165 if (b > t) *y = g_random_int_range(t, b + 1);
166 else *y = areas[i]->y;
168 for (i = 0; i < screen_num_monitors; ++i)
175 static GSList* area_add(GSList *list, Rect *a)
177 Rect *r = g_new(Rect, 1);
179 return g_slist_prepend(list, r);
182 static GSList* area_remove(GSList *list, Rect *a)
185 GSList *result = NULL;
187 for (sit = list; sit; sit = g_slist_next(sit)) {
190 if (!RECT_INTERSECTS_RECT(*r, *a)) {
191 result = g_slist_prepend(result, r);
192 /* dont free r, it's moved to the result list */
196 /* Use an intersection of a and r to determine the space
197 around r that we can use.
199 NOTE: the spaces calculated can overlap.
202 RECT_SET_INTERSECTION(isect, *r, *a);
204 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
205 RECT_SET(extra, r->x, r->y,
206 RECT_LEFT(isect) - r->x, r->height);
207 result = area_add(result, &extra);
210 if (RECT_TOP(isect) > RECT_TOP(*r)) {
211 RECT_SET(extra, r->x, r->y,
212 r->width, RECT_TOP(isect) - r->y + 1);
213 result = area_add(result, &extra);
216 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
217 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
218 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
219 result = area_add(result, &extra);
222 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
223 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
224 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
225 result = area_add(result, &extra);
228 /* 'r' is not being added to the result list, so free it */
237 IGNORE_FULLSCREEN = 1,
238 IGNORE_MAXIMIZED = 2,
240 /*IGNORE_SHADED = 3,*/
243 /*IGNORE_NONFOCUS = 1 << 5,*/
248 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
254 GSList *spaces = NULL, *sit, *maxit;
257 areas = pick_head(c);
262 /* try ignoring different things to find empty space */
263 for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
264 /* try all monitors in order of preference, but only the first one
265 if config_place_monitor is MOUSE or ACTIVE */
266 for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
267 1 : screen_num_monitors) && !ret); ++i)
271 /* add the whole monitor */
272 spaces = area_add(spaces, areas[i]);
274 /* go thru all the windows */
275 for (it = client_list; it; it = g_list_next(it)) {
276 ObClient *test = it->data;
278 /* should we ignore this client? */
279 if (screen_showing_desktop) continue;
280 if (c == test) continue;
281 if (test->iconic) continue;
282 if (c->desktop != DESKTOP_ALL) {
283 if (test->desktop != c->desktop &&
284 test->desktop != DESKTOP_ALL) continue;
286 if (test->desktop != screen_desktop &&
287 test->desktop != DESKTOP_ALL) continue;
289 if (test->type == OB_CLIENT_TYPE_SPLASH ||
290 test->type == OB_CLIENT_TYPE_DESKTOP) continue;
293 if ((ignore >= IGNORE_FULLSCREEN) &&
294 test->fullscreen) continue;
295 if ((ignore >= IGNORE_MAXIMIZED) &&
296 test->max_horz && test->max_vert) continue;
297 if ((ignore >= IGNORE_MENUTOOL) &&
298 (test->type == OB_CLIENT_TYPE_MENU ||
299 test->type == OB_CLIENT_TYPE_TOOLBAR) &&
300 client_has_parent(c)) continue;
302 if ((ignore >= IGNORE_SHADED) &&
303 test->shaded) continue;
305 if ((ignore >= IGNORE_NONGROUP) &&
306 client_has_group_siblings(c) &&
307 test->group != c->group) continue;
308 if ((ignore >= IGNORE_BELOW) &&
309 test->layer < c->layer) continue;
311 if ((ignore >= IGNORE_NONFOCUS) &&
312 focus_client != test) continue;
314 /* don't ignore this window, so remove it from the available
316 Rect test_area = render_plugin->frame_get_window_area(test->frame);
317 spaces = area_remove(spaces, &test_area);
320 if (ignore < IGNORE_DOCK) {
323 spaces = area_remove(spaces, &a);
326 Rect c_area = render_plugin->frame_get_window_area(c->frame);
327 for (sit = spaces; sit; sit = g_slist_next(sit)) {
330 if (r->width >= c_area.width &&
331 r->height >= c_area.height &&
332 r->width * r->height > maxsize)
334 maxsize = r->width * r->height;
340 Rect *r = maxit->data;
342 /* center it in the area */
345 if (config_place_center) {
346 *x += (r->width - c_area.width) / 2;
347 *y += (r->height - c_area.height) / 2;
353 g_free(spaces->data);
354 spaces = g_slist_delete_link(spaces, spaces);
359 for (i = 0; i < screen_num_monitors; ++i)
365 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
371 Strut fsize = render_plugin->frame_get_size(client->frame);
372 Rect farea = render_plugin->frame_get_window_area(client->frame);
374 if (!screen_pointer_pos(&px, &py))
376 area = pick_pointer_head(client);
380 r = area->x + area->width - farea.width;
381 b = area->y + area->height - farea.height;
383 *x = px - client->area.width / 2 - fsize.left;
384 *x = MIN(MAX(*x, l), r);
385 *y = py - client->area.height / 2 - fsize.top;
386 *y = MIN(MAX(*y, t), b);
391 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
392 ObAppSettings *settings)
396 if (!settings || (settings && !settings->pos_given))
399 /* Find which head the pointer is on */
400 if (settings->monitor == 0)
401 /* this can return NULL */
402 screen = pick_pointer_head(client);
403 else if (settings->monitor > 0 &&
404 (guint)settings->monitor <= screen_num_monitors)
405 screen = screen_area(client->desktop, (guint)settings->monitor - 1,
408 /* if we have't found a screen yet.. */
413 areas = pick_head(client);
416 /* don't free the first one, it's being set as "screen" */
417 for (i = 1; i < screen_num_monitors; ++i)
422 Rect farea = render_plugin->frame_get_window_area(client->frame);
423 if (settings->position.x.center)
424 *x = screen->x + screen->width / 2 - client->area.width / 2;
425 else if (settings->position.x.opposite)
426 *x = screen->x + screen->width - farea.width -
427 settings->position.x.pos;
429 *x = screen->x + settings->position.x.pos;
431 if (settings->position.y.center)
432 *y = screen->y + screen->height / 2 - client->area.height / 2;
433 else if (settings->position.y.opposite)
434 *y = screen->y + screen->height - farea.height -
435 settings->position.y.pos;
437 *y = screen->y + settings->position.y.pos;
443 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
445 if (client->type == OB_CLIENT_TYPE_DIALOG) {
447 gboolean first = TRUE;
449 for (it = client->parents; it; it = g_slist_next(it)) {
450 ObClient *m = it->data;
451 Rect area = render_plugin->frame_get_window_area(m->frame);
456 r = RECT_RIGHT(area);
457 b = RECT_BOTTOM(area);
460 l = MIN(l, RECT_LEFT(area));
461 t = MIN(t, RECT_TOP(area));
462 r = MAX(r, RECT_RIGHT(area));
463 b = MAX(b, RECT_BOTTOM(area));
467 *x = ((r + 1 - l) - area.width) / 2 + l;
468 *y = ((b + 1 - t) - area.height) / 2 + t;
474 if (client->type == OB_CLIENT_TYPE_DIALOG ||
475 client->type == OB_CLIENT_TYPE_SPLASH)
478 Rect area = render_plugin->frame_get_window_area(client->frame);
481 areas = pick_head(client);
483 *x = (areas[0]->width - area.width) / 2 + areas[0]->x;
484 *y = (areas[0]->height - area.height) / 2 + areas[0]->y;
486 for (i = 0; i < screen_num_monitors; ++i)
495 /* Return TRUE if we want client.c to enforce on-screen-keeping */
496 gboolean place_client(ObClient *client, gint *x, gint *y,
497 ObAppSettings *settings)
500 gboolean userplaced = FALSE;
502 /* per-app settings override program specified position
503 * but not user specified, unless pos_force is enabled */
504 if (((client->positioned & USPosition) &&
505 !(settings && settings->pos_given && settings->pos_force)) ||
506 ((client->positioned & PPosition) &&
507 !(settings && settings->pos_given)))
510 /* try a number of methods */
511 ret = place_transient_splash(client, x, y) ||
512 (userplaced = place_per_app_setting(client, x, y, settings)) ||
513 (config_place_policy == OB_PLACE_POLICY_MOUSE &&
514 place_under_mouse(client, x, y)) ||
515 place_nooverlap(client, x, y) ||
516 place_random(client, x, y);
519 /* get where the client should be */
520 frame_frame_gravity(client, x, y);