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