]> Dogcows Code - chaz/openbox/blob - openbox/place.c
Merge branch 'backport' into work
[chaz/openbox] / openbox / place.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 place.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "group.h"
22 #include "screen.h"
23 #include "frame.h"
24 #include "focus.h"
25 #include "config.h"
26 #include "dock.h"
27 #include "debug.h"
28
29 extern ObDock *dock;
30
31 static void add_choice(guint *choice, guint mychoice)
32 {
33 guint i;
34 for (i = 0; i < screen_num_monitors; ++i) {
35 if (choice[i] == mychoice)
36 return;
37 else if (choice[i] == screen_num_monitors) {
38 choice[i] = mychoice;
39 return;
40 }
41 }
42 }
43
44 static Rect *pick_pointer_head(ObClient *c)
45 {
46 guint i;
47 gint px, py;
48
49 if (screen_pointer_pos(&px, &py)) {
50 for (i = 0; i < screen_num_monitors; ++i) {
51 Rect *monitor = screen_physical_area_monitor(i);
52 gboolean contain = RECT_CONTAINS(*monitor, px, py);
53 g_free(monitor);
54 if (contain)
55 return screen_area(c->desktop, i, NULL);
56 }
57 g_assert_not_reached();
58 } else
59 return NULL;
60 }
61
62 /*! Pick a monitor to place a window on. */
63 static Rect **pick_head(ObClient *c)
64 {
65 Rect **area;
66 guint *choice;
67 guint i;
68 gint px, py;
69 ObClient *p;
70
71 area = g_new(Rect*, screen_num_monitors);
72 choice = g_new(guint, screen_num_monitors);
73 for (i = 0; i < screen_num_monitors; ++i)
74 choice[i] = screen_num_monitors; /* make them all invalid to start */
75
76 /* try direct parent first */
77 if ((p = client_direct_parent(c))) {
78 add_choice(choice, client_monitor(p));
79 ob_debug("placement adding choice %d for parent",
80 client_monitor(p));
81 }
82
83 /* more than one window in its group (more than just this window) */
84 if (client_has_group_siblings(c)) {
85 GSList *it;
86
87 /* try on the client's desktop */
88 for (it = c->group->members; it; it = g_slist_next(it)) {
89 ObClient *itc = it->data;
90 if (itc != c &&
91 (itc->desktop == c->desktop ||
92 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
93 {
94 add_choice(choice, client_monitor(it->data));
95 ob_debug("placement adding choice %d for group sibling",
96 client_monitor(it->data));
97 }
98 }
99
100 /* try on all desktops */
101 for (it = c->group->members; it; it = g_slist_next(it)) {
102 ObClient *itc = it->data;
103 if (itc != c) {
104 add_choice(choice, client_monitor(it->data));
105 ob_debug("placement adding choice %d for group sibling on "
106 "another desktop", client_monitor(it->data));
107 }
108 }
109 }
110
111 /* skip this if placing by the mouse position */
112 if (focus_client && client_normal(focus_client) &&
113 config_place_monitor != OB_PLACE_MONITOR_MOUSE)
114 {
115 add_choice(choice, client_monitor(focus_client));
116 ob_debug("placement adding choice %d for normal focused window",
117 client_monitor(focus_client));
118 }
119
120 screen_pointer_pos(&px, &py);
121
122 for (i = 0; i < screen_num_monitors; i++) {
123 Rect *monitor = screen_physical_area_monitor(i);
124 gboolean contain = RECT_CONTAINS(*monitor, px, py);
125 g_free(monitor);
126 if (contain) {
127 add_choice(choice, i);
128 ob_debug("placement adding choice %d for mouse pointer", i);
129 break;
130 }
131 }
132
133 /* add any leftover choices */
134 for (i = 0; i < screen_num_monitors; ++i)
135 add_choice(choice, i);
136
137 for (i = 0; i < screen_num_monitors; ++i)
138 area[i] = screen_area(c->desktop, choice[i], NULL);
139
140 g_free(choice);
141
142 return area;
143 }
144
145 static gboolean place_random(ObClient *client, gint *x, gint *y)
146 {
147 gint l, r, t, b;
148 Rect **areas;
149 guint i;
150
151 areas = pick_head(client);
152 i = (config_place_monitor != OB_PLACE_MONITOR_ANY) ?
153 0 : g_random_int_range(0, screen_num_monitors);
154
155 l = areas[i]->x;
156 t = areas[i]->y;
157 r = areas[i]->x + areas[i]->width - client->frame->area.width;
158 b = areas[i]->y + areas[i]->height - client->frame->area.height;
159
160 if (r > l) *x = g_random_int_range(l, r + 1);
161 else *x = areas[i]->x;
162 if (b > t) *y = g_random_int_range(t, b + 1);
163 else *y = areas[i]->y;
164
165 for (i = 0; i < screen_num_monitors; ++i)
166 g_free(areas[i]);
167 g_free(areas);
168
169 return TRUE;
170 }
171
172 static GSList* area_add(GSList *list, Rect *a)
173 {
174 Rect *r = g_new(Rect, 1);
175 *r = *a;
176 return g_slist_prepend(list, r);
177 }
178
179 static GSList* area_remove(GSList *list, Rect *a)
180 {
181 GSList *sit;
182 GSList *result = NULL;
183
184 for (sit = list; sit; sit = g_slist_next(sit)) {
185 Rect *r = sit->data;
186
187 if (!RECT_INTERSECTS_RECT(*r, *a)) {
188 result = g_slist_prepend(result, r);
189 /* dont free r, it's moved to the result list */
190 } else {
191 Rect isect, extra;
192
193 /* Use an intersection of a and r to determine the space
194 around r that we can use.
195
196 NOTE: the spaces calculated can overlap.
197 */
198
199 RECT_SET_INTERSECTION(isect, *r, *a);
200
201 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
202 RECT_SET(extra, r->x, r->y,
203 RECT_LEFT(isect) - r->x, r->height);
204 result = area_add(result, &extra);
205 }
206
207 if (RECT_TOP(isect) > RECT_TOP(*r)) {
208 RECT_SET(extra, r->x, r->y,
209 r->width, RECT_TOP(isect) - r->y + 1);
210 result = area_add(result, &extra);
211 }
212
213 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
214 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
215 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
216 result = area_add(result, &extra);
217 }
218
219 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
220 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
221 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
222 result = area_add(result, &extra);
223 }
224
225 /* 'r' is not being added to the result list, so free it */
226 g_free(r);
227 }
228 }
229 g_slist_free(list);
230 return result;
231 }
232
233 enum {
234 IGNORE_FULLSCREEN = 1,
235 IGNORE_MAXIMIZED = 2,
236 IGNORE_MENUTOOL = 3,
237 /*IGNORE_SHADED = 3,*/
238 IGNORE_NONGROUP = 4,
239 IGNORE_BELOW = 5,
240 /*IGNORE_NONFOCUS = 1 << 5,*/
241 IGNORE_DOCK = 6,
242 IGNORE_END = 7
243 };
244
245 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
246 {
247 Rect **areas;
248 gint ignore;
249 gboolean ret;
250 gint maxsize;
251 GSList *spaces = NULL, *sit, *maxit;
252 guint i;
253
254 areas = pick_head(c);
255 ret = FALSE;
256 maxsize = 0;
257 maxit = NULL;
258
259 /* try ignoring different things to find empty space */
260 for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
261 /* try all monitors in order of preference, but only the first one
262 if config_place_monitor is MOUSE or ACTIVE */
263 for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
264 1 : screen_num_monitors) && !ret); ++i)
265 {
266 GList *it;
267
268 /* add the whole monitor */
269 spaces = area_add(spaces, areas[i]);
270
271 /* go thru all the windows */
272 for (it = client_list; it; it = g_list_next(it)) {
273 ObClient *test = it->data;
274
275 /* should we ignore this client? */
276 if (screen_showing_desktop) continue;
277 if (c == test) continue;
278 if (test->iconic) continue;
279 if (c->desktop != DESKTOP_ALL) {
280 if (test->desktop != c->desktop &&
281 test->desktop != DESKTOP_ALL) continue;
282 } else {
283 if (test->desktop != screen_desktop &&
284 test->desktop != DESKTOP_ALL) continue;
285 }
286 if (test->type == OB_CLIENT_TYPE_SPLASH ||
287 test->type == OB_CLIENT_TYPE_DESKTOP) continue;
288
289
290 if ((ignore >= IGNORE_FULLSCREEN) &&
291 test->fullscreen) continue;
292 if ((ignore >= IGNORE_MAXIMIZED) &&
293 test->max_horz && test->max_vert) continue;
294 if ((ignore >= IGNORE_MENUTOOL) &&
295 (test->type == OB_CLIENT_TYPE_MENU ||
296 test->type == OB_CLIENT_TYPE_TOOLBAR) &&
297 client_has_parent(c)) continue;
298 /*
299 if ((ignore >= IGNORE_SHADED) &&
300 test->shaded) continue;
301 */
302 if ((ignore >= IGNORE_NONGROUP) &&
303 client_has_group_siblings(c) &&
304 test->group != c->group) continue;
305 if ((ignore >= IGNORE_BELOW) &&
306 test->layer < c->layer) continue;
307 /*
308 if ((ignore >= IGNORE_NONFOCUS) &&
309 focus_client != test) continue;
310 */
311 /* don't ignore this window, so remove it from the available
312 area */
313 spaces = area_remove(spaces, &test->frame->area);
314 }
315
316 if (ignore < IGNORE_DOCK) {
317 Rect a;
318 dock_get_area(&a);
319 spaces = area_remove(spaces, &a);
320 }
321
322 for (sit = spaces; sit; sit = g_slist_next(sit)) {
323 Rect *r = sit->data;
324
325 if (r->width >= c->frame->area.width &&
326 r->height >= c->frame->area.height &&
327 r->width * r->height > maxsize)
328 {
329 maxsize = r->width * r->height;
330 maxit = sit;
331 }
332 }
333
334 if (maxit) {
335 Rect *r = maxit->data;
336
337 /* center it in the area */
338 *x = r->x;
339 *y = r->y;
340 if (config_place_center) {
341 *x += (r->width - c->frame->area.width) / 2;
342 *y += (r->height - c->frame->area.height) / 2;
343 }
344 ret = TRUE;
345 }
346
347 while (spaces) {
348 g_free(spaces->data);
349 spaces = g_slist_delete_link(spaces, spaces);
350 }
351 }
352 }
353
354 for (i = 0; i < screen_num_monitors; ++i)
355 g_free(areas[i]);
356 g_free(areas);
357 return ret;
358 }
359
360 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
361 {
362 gint l, r, t, b;
363 gint px, py;
364 Rect *area;
365
366 if (!screen_pointer_pos(&px, &py))
367 return FALSE;
368 area = pick_pointer_head(client);
369
370 l = area->x;
371 t = area->y;
372 r = area->x + area->width - client->frame->area.width;
373 b = area->y + area->height - client->frame->area.height;
374
375 *x = px - client->area.width / 2 - client->frame->size.left;
376 *x = MIN(MAX(*x, l), r);
377 *y = py - client->area.height / 2 - client->frame->size.top;
378 *y = MIN(MAX(*y, t), b);
379
380 return TRUE;
381 }
382
383 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
384 ObAppSettings *settings)
385 {
386 Rect *screen = NULL;
387
388 if (!settings || (settings && !settings->pos_given))
389 return FALSE;
390
391 /* Find which head the pointer is on */
392 if (settings->monitor == 0)
393 /* this can return NULL */
394 screen = pick_pointer_head(client);
395 else if (settings->monitor > 0 &&
396 (guint)settings->monitor <= screen_num_monitors)
397 screen = screen_area(client->desktop, (guint)settings->monitor - 1,
398 NULL);
399
400 /* if we have't found a screen yet.. */
401 if (!screen) {
402 Rect **areas;
403 guint i;
404
405 areas = pick_head(client);
406 screen = areas[0];
407
408 /* don't free the first one, it's being set as "screen" */
409 for (i = 1; i < screen_num_monitors; ++i)
410 g_free(areas[i]);
411 g_free(areas);
412 }
413
414 if (settings->position.x.center)
415 *x = screen->x + screen->width / 2 - client->area.width / 2;
416 else if (settings->position.x.opposite)
417 *x = screen->x + screen->width - client->frame->area.width -
418 settings->position.x.pos;
419 else
420 *x = screen->x + settings->position.x.pos;
421
422 if (settings->position.y.center)
423 *y = screen->y + screen->height / 2 - client->area.height / 2;
424 else if (settings->position.y.opposite)
425 *y = screen->y + screen->height - client->frame->area.height -
426 settings->position.y.pos;
427 else
428 *y = screen->y + settings->position.y.pos;
429
430 g_free(screen);
431 return TRUE;
432 }
433
434 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
435 {
436 if (client->type == OB_CLIENT_TYPE_DIALOG) {
437 GSList *it;
438 gboolean first = TRUE;
439 gint l, r, t, b;
440 for (it = client->parents; it; it = g_slist_next(it)) {
441 ObClient *m = it->data;
442 if (!m->iconic) {
443 if (first) {
444 l = RECT_LEFT(m->frame->area);
445 t = RECT_TOP(m->frame->area);
446 r = RECT_RIGHT(m->frame->area);
447 b = RECT_BOTTOM(m->frame->area);
448 first = FALSE;
449 } else {
450 l = MIN(l, RECT_LEFT(m->frame->area));
451 t = MIN(t, RECT_TOP(m->frame->area));
452 r = MAX(r, RECT_RIGHT(m->frame->area));
453 b = MAX(b, RECT_BOTTOM(m->frame->area));
454 }
455 }
456 if (!first) {
457 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
458 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
459 return TRUE;
460 }
461 }
462 }
463
464 if (client->type == OB_CLIENT_TYPE_DIALOG ||
465 client->type == OB_CLIENT_TYPE_SPLASH)
466 {
467 Rect **areas;
468 guint i;
469
470 areas = pick_head(client);
471
472 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
473 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
474
475 for (i = 0; i < screen_num_monitors; ++i)
476 g_free(areas[i]);
477 g_free(areas);
478 return TRUE;
479 }
480
481 return FALSE;
482 }
483
484 /* Return TRUE if we want client.c to enforce on-screen-keeping */
485 gboolean place_client(ObClient *client, gint *x, gint *y,
486 ObAppSettings *settings)
487 {
488 gboolean ret;
489 gboolean userplaced = FALSE;
490
491 /* per-app settings override program specified position
492 * but not user specified, unless pos_force is enabled */
493 if (((client->positioned & USPosition) &&
494 !(settings && settings->pos_given && settings->pos_force)) ||
495 ((client->positioned & PPosition) &&
496 !(settings && settings->pos_given)))
497 return FALSE;
498
499 /* try a number of methods */
500 ret = place_transient_splash(client, x, y) ||
501 (userplaced = place_per_app_setting(client, x, y, settings)) ||
502 (config_place_policy == OB_PLACE_POLICY_MOUSE &&
503 place_under_mouse(client, x, y)) ||
504 place_nooverlap(client, x, y) ||
505 place_random(client, x, y);
506 g_assert(ret);
507
508 /* get where the client should be */
509 frame_frame_gravity(client->frame, x, y);
510 return !userplaced;
511 }
This page took 0.060503 seconds and 5 git commands to generate.