]> icculus.org git repositories - dana/openbox.git/blob - openbox/place.c
unredirect on a window with bad timing can cause a BadValue error
[dana/openbox.git] / 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         const Rect *monitor = screen_physical_area_monitor(i);
111         gboolean contain = RECT_CONTAINS(*monitor, px, py);
112         if (contain) {
113             add_choice(choice, i);
114             ob_debug("placement adding choice %d for mouse pointer", i);
115             break;
116         }
117     }
118
119     /* add any leftover choices */
120     for (i = 0; i < screen_num_monitors; ++i)
121         add_choice(choice, i);
122
123     for (i = 0; i < screen_num_monitors; ++i)
124         area[i] = screen_area(c->desktop, choice[i], NULL);
125
126     g_free(choice);
127
128     return area;
129 }
130
131 static gboolean place_random(ObClient *client, gint *x, gint *y,
132                              gint w, gint h)
133 {
134     gint l, r, t, b, fw, fh;
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     fw = w + client->frame->size.left + client->frame->size.right;
143     fh = h + client->frame->size.top + client->frame->size.bottom;
144
145     l = areas[i]->x;
146     t = areas[i]->y;
147     r = areas[i]->x + areas[i]->width - fw;
148     b = areas[i]->y + areas[i]->height - fh;
149
150     if (r > l) *x = g_random_int_range(l, r + 1);
151     else       *x = areas[i]->x;
152     if (b > t) *y = g_random_int_range(t, b + 1);
153     else       *y = areas[i]->y;
154
155     for (i = 0; i < screen_num_monitors; ++i)
156         g_slice_free(Rect, areas[i]);
157     g_free(areas);
158
159     return TRUE;
160 }
161
162 static GSList* area_add(GSList *list, Rect *a)
163 {
164     Rect *r = g_slice_new(Rect);
165     *r = *a;
166     return g_slist_prepend(list, r);
167 }
168
169 static GSList* area_remove(GSList *list, Rect *a)
170 {
171     GSList *sit;
172     GSList *result = NULL;
173
174     for (sit = list; sit; sit = g_slist_next(sit)) {
175         Rect *r = sit->data;
176
177         if (!RECT_INTERSECTS_RECT(*r, *a)) {
178             result = g_slist_prepend(result, r);
179             /* dont free r, it's moved to the result list */
180         } else {
181             Rect isect, extra;
182
183             /* Use an intersection of a and r to determine the space
184                around r that we can use.
185
186                NOTE: the spaces calculated can overlap.
187             */
188
189             RECT_SET_INTERSECTION(isect, *r, *a);
190
191             if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
192                 RECT_SET(extra, r->x, r->y,
193                          RECT_LEFT(isect) - r->x, r->height);
194                 result = area_add(result, &extra);
195             }
196
197             if (RECT_TOP(isect) > RECT_TOP(*r)) {
198                 RECT_SET(extra, r->x, r->y,
199                          r->width, RECT_TOP(isect) - r->y + 1);
200                 result = area_add(result, &extra);
201             }
202
203             if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
204                 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
205                          RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
206                 result = area_add(result, &extra);
207             }
208
209             if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
210                 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
211                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
212                 result = area_add(result, &extra);
213             }
214
215             /* 'r' is not being added to the result list, so free it */
216             g_slice_free(Rect, r);
217         }
218     }
219     g_slist_free(list);
220     return result;
221 }
222
223 enum {
224     IGNORE_FULLSCREEN = 1,
225     IGNORE_MAXIMIZED  = 2,
226     IGNORE_MENUTOOL   = 3,
227     /*IGNORE_SHADED     = 3,*/
228     IGNORE_NONGROUP   = 4,
229     IGNORE_BELOW      = 5,
230     /*IGNORE_NONFOCUS   = 1 << 5,*/
231     IGNORE_DOCK       = 6,
232     IGNORE_END        = 7
233 };
234
235 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y, gint w, gint h)
236 {
237     Rect **areas;
238     gint ignore;
239     gboolean ret;
240     gint maxsize;
241     GSList *spaces = NULL, *sit, *maxit;
242     guint i;
243
244     areas = pick_head(c);
245     ret = FALSE;
246     maxsize = 0;
247     maxit = NULL;
248
249     /* try ignoring different things to find empty space */
250     for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
251         /* try all monitors in order of preference, but only the first one
252            if config_place_monitor is MOUSE or ACTIVE */
253         for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
254                           1 : screen_num_monitors) && !ret); ++i)
255         {
256             GList *it;
257
258             /* add the whole monitor */
259             spaces = area_add(spaces, areas[i]);
260
261             /* go thru all the windows */
262             for (it = client_list; it; it = g_list_next(it)) {
263                 ObClient *test = it->data;
264
265                 /* should we ignore this client? */
266                 if (screen_showing_desktop) continue;
267                 if (c == test) continue;
268                 if (test->iconic) continue;
269                 if (c->desktop != DESKTOP_ALL) {
270                     if (test->desktop != c->desktop &&
271                         test->desktop != DESKTOP_ALL) continue;
272                 } else {
273                     if (test->desktop != screen_desktop &&
274                         test->desktop != DESKTOP_ALL) continue;
275                 }
276                 if (test->type == OB_CLIENT_TYPE_SPLASH ||
277                     test->type == OB_CLIENT_TYPE_DESKTOP) continue;
278
279
280                 if ((ignore >= IGNORE_FULLSCREEN) &&
281                     test->fullscreen) continue;
282                 if ((ignore >= IGNORE_MAXIMIZED) &&
283                     test->max_horz && test->max_vert) continue;
284                 if ((ignore >= IGNORE_MENUTOOL) &&
285                     (test->type == OB_CLIENT_TYPE_MENU ||
286                      test->type == OB_CLIENT_TYPE_TOOLBAR) &&
287                     client_has_parent(c)) continue;
288                 /*
289                 if ((ignore >= IGNORE_SHADED) &&
290                     test->shaded) continue;
291                 */
292                 if ((ignore >= IGNORE_NONGROUP) &&
293                     client_has_group_siblings(c) &&
294                     test->group != c->group) continue;
295                 if ((ignore >= IGNORE_BELOW) &&
296                     test->layer < c->layer) continue;
297                 /*
298                 if ((ignore >= IGNORE_NONFOCUS) &&
299                     focus_client != test) continue;
300                 */
301                 /* don't ignore this window, so remove it from the available
302                    area */
303                 spaces = area_remove(spaces, &test->frame->area);
304             }
305
306             if (ignore < IGNORE_DOCK) {
307                 Rect a;
308                 dock_get_area(&a);
309                 spaces = area_remove(spaces, &a);
310             }
311
312             for (sit = spaces; sit; sit = g_slist_next(sit)) {
313                 Rect *r = sit->data;
314
315                 const gint fw = w + c->frame->size.left + c->frame->size.right;
316                 const gint fh = h + c->frame->size.top + c->frame->size.bottom;
317
318                 if (r->width >= fw && r->height >= fh &&
319                     r->width * r->height > maxsize)
320                 {
321                     maxsize = r->width * r->height;
322                     maxit = sit;
323                 }
324             }
325
326             if (maxit) {
327                 Rect *r = maxit->data;
328
329                 const gint fw = w + c->frame->size.left + c->frame->size.right;
330                 const gint fh = h + c->frame->size.top + c->frame->size.bottom;
331
332                 /* center it in the area */
333                 *x = r->x;
334                 *y = r->y;
335                 if (config_place_center) {
336                     *x += (r->width - fw) / 2;
337                     *y += (r->height - fh) / 2;
338                 }
339                 ret = TRUE;
340             }
341
342             while (spaces) {
343                 g_slice_free(Rect, spaces->data);
344                 spaces = g_slist_delete_link(spaces, spaces);
345             }
346         }
347     }
348
349     for (i = 0; i < screen_num_monitors; ++i)
350         g_slice_free(Rect, areas[i]);
351     g_free(areas);
352     return ret;
353 }
354
355 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y,
356                                   gint w, gint h)
357 {
358     gint l, r, t, b;
359     gint px, py;
360     Rect *area;
361
362     const gint fw = w + client->frame->size.left + client->frame->size.right;
363     const gint fh = h + client->frame->size.top + client->frame->size.bottom;
364
365     if (!screen_pointer_pos(&px, &py))
366         return FALSE;
367     area = pick_pointer_head(client);
368
369     l = area->x;
370     t = area->y;
371     r = area->x + area->width - fw;
372     b = area->y + area->height - fh;
373
374     *x = px - client->area.width / 2 - client->frame->size.left;
375     *x = MIN(MAX(*x, l), r);
376     *y = py - client->area.height / 2 - client->frame->size.top;
377     *y = MIN(MAX(*y, t), b);
378
379     g_slice_free(Rect, area);
380
381     return TRUE;
382 }
383
384 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
385                                       gint w, gint h, ObAppSettings *settings)
386 {
387     Rect *screen = NULL;
388
389     const gint fw = w + client->frame->size.left + client->frame->size.right;
390     const gint fh = h + client->frame->size.top + client->frame->size.bottom;
391
392     if (!settings || (settings && !settings->pos_given))
393         return FALSE;
394
395     /* Find which head the pointer is on */
396     if (settings->monitor == 0)
397         /* this can return NULL */
398         screen = pick_pointer_head(client);
399     else if (settings->monitor > 0 &&
400              (guint)settings->monitor <= screen_num_monitors)
401         screen = screen_area(client->desktop, (guint)settings->monitor - 1,
402                              NULL);
403
404     /* if we have't found a screen yet.. */
405     if (!screen) {
406         Rect **areas;
407         guint i;
408
409         areas = pick_head(client);
410         screen = areas[0];
411
412         /* don't free the first one, it's being set as "screen" */
413         for (i = 1; i < screen_num_monitors; ++i)
414             g_slice_free(Rect, areas[i]);
415         g_free(areas);
416     }
417
418     if (settings->position.x.center)
419         *x = screen->x + screen->width / 2 - client->area.width / 2;
420     else if (settings->position.x.opposite)
421         *x = screen->x + screen->width - fw - settings->position.x.pos;
422     else
423         *x = screen->x + settings->position.x.pos;
424     if (settings->position.x.denom)
425         *x = (*x * screen->width) / settings->position.x.denom;
426
427     if (settings->position.y.center)
428         *y = screen->y + screen->height / 2 - client->area.height / 2;
429     else if (settings->position.y.opposite)
430         *y = screen->y + screen->height - fh - settings->position.y.pos;
431     else
432         *y = screen->y + settings->position.y.pos;
433     if (settings->position.y.denom)
434         *y = (*y * screen->height) / settings->position.y.denom;
435
436     g_slice_free(Rect, screen);
437     return TRUE;
438 }
439
440 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y,
441                                        gint w, gint h)
442 {
443     if (client->type == OB_CLIENT_TYPE_DIALOG) {
444         GSList *it;
445         gboolean first = TRUE;
446         gint l, r, t, b;
447         for (it = client->parents; it; it = g_slist_next(it)) {
448             ObClient *m = it->data;
449             if (!m->iconic) {
450                 if (first) {
451                     l = RECT_LEFT(m->frame->area);
452                     t = RECT_TOP(m->frame->area);
453                     r = RECT_RIGHT(m->frame->area);
454                     b = RECT_BOTTOM(m->frame->area);
455                     first = FALSE;
456                 } else {
457                     l = MIN(l, RECT_LEFT(m->frame->area));
458                     t = MIN(t, RECT_TOP(m->frame->area));
459                     r = MAX(r, RECT_RIGHT(m->frame->area));
460                     b = MAX(b, RECT_BOTTOM(m->frame->area));
461                 }
462             }
463             if (!first) {
464                 const gint fw = w + client->frame->size.left +
465                     client->frame->size.right;
466                 const gint fh = h + client->frame->size.top +
467                     client->frame->size.bottom;
468                 *x = ((r + 1 - l) - fw) / 2 + l;
469                 *y = ((b + 1 - t) - fh) / 2 + t;
470                 return TRUE;
471             }
472         }
473     }
474
475     if (client->type == OB_CLIENT_TYPE_DIALOG ||
476         client->type == OB_CLIENT_TYPE_SPLASH)
477     {
478         Rect **areas;
479         guint i;
480
481         const gint fw = w + client->frame->size.left +
482             client->frame->size.right;
483         const gint fh = h + client->frame->size.top +
484             client->frame->size.bottom;
485
486         areas = pick_head(client);
487
488         *x = (areas[0]->width - fw) / 2 + areas[0]->x;
489         *y = (areas[0]->height - fh) / 2 + areas[0]->y;
490
491         for (i = 0; i < screen_num_monitors; ++i)
492             g_slice_free(Rect, areas[i]);
493         g_free(areas);
494         return TRUE;
495     }
496
497     return FALSE;
498 }
499
500 /*! Return TRUE if openbox chose the position for the window, and FALSE if
501   the application chose it */
502 gboolean place_client(ObClient *client, gint *x, gint *y, gint w, gint h,
503                       ObAppSettings *settings)
504 {
505     gboolean ret;
506
507     /* per-app settings override program specified position
508      * but not user specified, unless pos_force is enabled */
509     if (((client->positioned & USPosition) &&
510          !(settings && settings->pos_given && settings->pos_force)) ||
511         ((client->positioned & PPosition) &&
512          !(settings && settings->pos_given)))
513         return FALSE;
514
515     /* try a number of methods */
516     ret = place_per_app_setting(client, x, y, w, h, settings) ||
517         place_transient_splash(client, x, y, w, h) ||
518         (config_place_policy == OB_PLACE_POLICY_MOUSE &&
519          place_under_mouse(client, x, y, w, h)) ||
520         place_nooverlap(client, x, y, w, h) ||
521         place_random(client, x, y, w, h);
522     g_assert(ret);
523
524     /* get where the client should be */
525     frame_frame_gravity(client->frame, x, y);
526     return TRUE;
527 }