]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/place.c
dont let windows place over menu or toolbars if they don't have a parent
[mikachu/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 "debug.h"
27
28 static void add_choice(guint *choice, guint mychoice)
29 {
30     guint i;
31     for (i = 0; i < screen_num_monitors; ++i) {
32         if (choice[i] == mychoice)
33             return;
34         else if (choice[i] == screen_num_monitors) {
35             choice[i] = mychoice;
36             return;
37         }
38     }
39 }
40
41 static Rect *pick_pointer_head(ObClient *c)
42 {
43     guint i;
44     gint px, py;
45
46     screen_pointer_pos(&px, &py);
47      
48     for (i = 0; i < screen_num_monitors; ++i) {  
49         if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
50             return screen_area_monitor(c->desktop, i);
51         }
52     }
53     g_assert_not_reached();
54 }
55
56 /*! Pick a monitor to place a window on.
57   The returned array value should be freed with g_free. The areas within the
58   array should not be freed. */
59 static Rect **pick_head(ObClient *c)
60 {
61     Rect **area;
62     guint *choice;
63     guint i;
64     gint px, py;
65
66     area = g_new(Rect*, screen_num_monitors);
67     choice = g_new(guint, screen_num_monitors);
68     for (i = 0; i < screen_num_monitors; ++i)
69         choice[i] = screen_num_monitors; /* make them all invalid to start */
70
71     /* try direct parent first */
72     if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
73         add_choice(choice, client_monitor(c->transient_for));
74         ob_debug("placement adding choice %d for parent\n",
75                  client_monitor(c->transient_for));
76     }
77
78     /* more than one window in its group (more than just this window) */
79     if (client_has_group_siblings(c)) {
80         GSList *it;
81
82         /* try on the client's desktop */
83         for (it = c->group->members; it; it = g_slist_next(it)) {
84             ObClient *itc = it->data;            
85             if (itc != c &&
86                 (itc->desktop == c->desktop ||
87                  itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
88             {
89                 add_choice(choice, client_monitor(it->data));
90                 ob_debug("placement adding choice %d for group sibling\n",
91                          client_monitor(it->data));
92             }
93         }
94
95         /* try on all desktops */
96         for (it = c->group->members; it; it = g_slist_next(it)) {
97             ObClient *itc = it->data;            
98             if (itc != c) {
99                 add_choice(choice, client_monitor(it->data));
100                 ob_debug("placement adding choice %d for group sibling on "
101                          "another desktop\n", client_monitor(it->data));
102             }
103         }
104     }
105
106     if (focus_client) {
107         add_choice(choice, client_monitor(focus_client));
108         ob_debug("placement adding choice %d for focused window\n",
109                  client_monitor(focus_client));
110     }
111
112     screen_pointer_pos(&px, &py);
113
114     for (i = 0; i < screen_num_monitors; i++)
115         if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
116             add_choice(choice, i);
117             ob_debug("placement adding choice %d for mouse pointer\n", i);
118             break;
119         }
120
121     /* add any leftover choices */
122     for (i = 0; i < screen_num_monitors; ++i)
123         add_choice(choice, i);
124
125     for (i = 0; i < screen_num_monitors; ++i)
126         area[i] = screen_area_monitor(c->desktop, choice[i]);
127
128     return area;
129 }
130
131 static gboolean place_random(ObClient *client, gint *x, gint *y)
132 {
133     gint l, r, t, b;
134     Rect **areas;
135     guint i;
136
137     areas = pick_head(client);
138     i = g_random_int_range(0, screen_num_monitors);
139
140     l = areas[i]->x;
141     t = areas[i]->y;
142     r = areas[i]->x + areas[i]->width - client->frame->area.width;
143     b = areas[i]->y + areas[i]->height - client->frame->area.height;
144
145     if (r > l) *x = g_random_int_range(l, r + 1);
146     else       *x = areas[i]->x;
147     if (b > t) *y = g_random_int_range(t, b + 1);
148     else       *y = areas[i]->y;
149
150     g_free(areas);
151
152     return TRUE;
153 }
154
155 static GSList* area_add(GSList *list, Rect *a)
156 {
157     Rect *r = g_new(Rect, 1);
158     *r = *a;
159     return g_slist_prepend(list, r);
160 }
161
162 static GSList* area_remove(GSList *list, Rect *a)
163 {
164     GSList *sit;
165     GSList *result = NULL;
166
167     for (sit = list; sit; sit = g_slist_next(sit)) {
168         Rect *r = sit->data;
169
170         if (!RECT_INTERSECTS_RECT(*r, *a)) {
171             result = g_slist_prepend(result, r);
172             r = NULL; /* dont free it */
173         } else {
174             Rect isect, extra;
175
176             /* Use an intersection of a and r to determine the space
177                around r that we can use.
178
179                NOTE: the spaces calculated can overlap.
180             */
181
182             RECT_SET_INTERSECTION(isect, *r, *a);
183
184             if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
185                 RECT_SET(extra, r->x, r->y,
186                          RECT_LEFT(isect) - r->x, r->height);
187                 result = area_add(result, &extra);
188             }
189
190             if (RECT_TOP(isect) > RECT_TOP(*r)) {
191                 RECT_SET(extra, r->x, r->y,
192                          r->width, RECT_TOP(isect) - r->y + 1);
193                 result = area_add(result, &extra);
194             }
195
196             if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
197                 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
198                          RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
199                 result = area_add(result, &extra);
200             }
201
202             if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
203                 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
204                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
205                 result = area_add(result, &extra);
206             }
207         }
208
209         g_free(r);
210     }
211     g_slist_free(list);
212     return result;
213 }
214
215 static gint area_cmp(gconstpointer p1, gconstpointer p2, gpointer data)
216 {
217     ObClient *c = data;
218     Rect *carea = &c->frame->area;
219     const Rect *a1 = p1, *a2 = p2;
220     gboolean diffhead = FALSE;
221     guint i;
222     Rect *a;
223
224     for (i = 0; i < screen_num_monitors; ++i) {
225         a = screen_physical_area_monitor(i);
226         if (RECT_CONTAINS(*a, a1->x, a1->y) &&
227             !RECT_CONTAINS(*a, a2->x, a2->y))
228         {
229             diffhead = TRUE;
230             break;
231         }
232     }
233
234     /* has to be more than me in the group */
235     if (diffhead && client_has_group_siblings(c)) {
236         guint *num, most;
237         GSList *it;
238
239         /* find how many clients in the group are on each monitor, use the
240            monitor with the most in it */
241         num = g_new0(guint, screen_num_monitors);
242         for (it = c->group->members; it; it = g_slist_next(it))
243             if (it->data != c)
244                 ++num[client_monitor(it->data)];
245         most = 0;
246         for (i = 1; i < screen_num_monitors; ++i)
247             if (num[i] > num[most])
248                 most = i;
249
250         g_free(num);
251
252         a = screen_physical_area_monitor(most);
253         if (RECT_CONTAINS(*a, a1->x, a1->y))
254             return -1;
255         if (RECT_CONTAINS(*a, a2->x, a2->y))
256             return 1;
257     }
258
259     return MIN((a1->width - carea->width), (a1->height - carea->height)) -
260         MIN((a2->width - carea->width), (a2->height - carea->height));
261 }
262
263 typedef enum
264 {
265     SMART_FULL,
266     SMART_GROUP,
267     SMART_FOCUSED
268 } ObSmartType;
269
270 #define SMART_IGNORE(placer, c) \
271     (placer == c || c->shaded || !c->frame->visible || \
272      c->type == OB_CLIENT_TYPE_SPLASH || c->type == OB_CLIENT_TYPE_DESKTOP || \
273      ((c->type == OB_CLIENT_TYPE_MENU || c->type == OB_CLIENT_TYPE_TOOLBAR) &&\
274       client_has_parent(c)) || \
275      (c->desktop != DESKTOP_ALL && \
276       c->desktop != (placer->desktop == DESKTOP_ALL ? \
277                      screen_desktop : placer->desktop)))
278
279 static gboolean place_smart(ObClient *client, gint *x, gint *y,
280                             ObSmartType type, gboolean ignore_max)
281 {
282     gboolean ret = FALSE;
283     GSList *spaces = NULL, *sit;
284     GList *it;
285     Rect **areas;
286     guint i;
287
288     if (type == SMART_GROUP) {
289         /* has to be more than me in the group */
290         if (!client_has_group_siblings(client))
291             return FALSE;
292     }
293
294     areas = pick_head(client);
295
296     for (i = 0; i < screen_num_monitors && !ret; ++i) {
297         spaces = area_add(spaces, areas[i]);
298
299         /* stay out from under windows in higher layers */
300         for (it = stacking_list; it; it = g_list_next(it)) {
301             ObClient *c;
302
303             if (WINDOW_IS_CLIENT(it->data)) {
304                 c = it->data;
305                 if (ignore_max &&
306                     (c->fullscreen || (c->max_vert && c->max_horz)))
307                     continue;
308             } else
309                 continue;
310
311             if (c->layer > client->layer) {
312                 if (!SMART_IGNORE(client, c))
313                     spaces = area_remove(spaces, &c->frame->area);
314             } else
315                 break;
316         }
317
318         if (type == SMART_FULL || type == SMART_FOCUSED) {
319             gboolean found_foc = FALSE, stop = FALSE;
320             ObClient *foc;
321
322             foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
323                                          screen_desktop : client->desktop);
324
325             for (; it && !stop; it = g_list_next(it)) {
326                 ObClient *c;
327
328                 if (WINDOW_IS_CLIENT(it->data)) {
329                     c = it->data;
330                     if (ignore_max &&
331                         (c->fullscreen || (c->max_vert && c->max_horz)))
332                         continue;
333                 } else
334                     continue;
335
336                 if (!SMART_IGNORE(client, c)) {
337                     if (type == SMART_FOCUSED)
338                         if (found_foc)
339                             stop = TRUE;
340                     if (!stop)
341                         spaces = area_remove(spaces, &c->frame->area);
342                 }
343
344                 if (c == foc)
345                     found_foc = TRUE;
346             }
347         } else if (type == SMART_GROUP) {
348             for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
349                 ObClient *c = sit->data;
350                 if (!SMART_IGNORE(client, c))
351                     spaces = area_remove(spaces, &c->frame->area);
352             }
353         } else
354             g_assert_not_reached();
355
356         spaces = g_slist_sort_with_data(spaces, area_cmp, client);
357
358         for (sit = spaces; sit; sit = g_slist_next(sit)) {
359             Rect *r = sit->data;
360
361             if (!ret) {
362                 if (r->width >= client->frame->area.width &&
363                     r->height >= client->frame->area.height) {
364                     ret = TRUE;
365                     if (client->type == OB_CLIENT_TYPE_DIALOG ||
366                         type != SMART_FULL)
367                     {
368                         *x = r->x + (r->width - client->frame->area.width)/2;
369                         *y = r->y + (r->height - client->frame->area.height)/2;
370                     } else {
371                         *x = r->x;
372                         *y = r->y;
373                     }
374                 }
375             }
376
377             g_free(r);
378         }
379         g_slist_free(spaces);
380         spaces = NULL;
381     }
382
383     g_free(areas);
384
385     return ret;
386 }
387
388 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
389 {
390     gint l, r, t, b;
391     gint px, py;
392     Rect *area;
393
394     area = pick_pointer_head(client);
395     screen_pointer_pos(&px, &py);
396
397     l = area->x;
398     t = area->y;
399     r = area->x + area->width - client->frame->area.width;
400     b = area->y + area->height - client->frame->area.height;
401
402     *x = px - client->area.width / 2 - client->frame->size.left;
403     *x = MIN(MAX(*x, l), r);
404     *y = py - client->area.height / 2 - client->frame->size.top;
405     *y = MIN(MAX(*y, t), b);
406
407     return TRUE;
408 }
409
410 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
411                                       ObAppSettings *settings)
412 {
413     Rect *screen = NULL;
414
415     if (!settings || (settings && !settings->pos_given))
416         return FALSE;
417
418     /* Find which head the pointer is on */
419     if (settings->monitor == 0)
420         screen = pick_pointer_head(client);
421     else if (settings->monitor > 0 &&
422              (guint)settings->monitor <= screen_num_monitors)
423         screen = screen_area_monitor(client->desktop,
424                                      (guint)settings->monitor - 1);
425     else {
426         Rect **all = NULL;
427         all = pick_head(client);
428         screen = all[0];
429         g_free(all); /* the areas themselves don't need to be freed */
430     }
431
432     if (settings->center_x)
433         *x = screen->x + screen->width / 2 - client->area.width / 2;
434     else
435         *x = screen->x + settings->position.x;
436
437     if (settings->center_y)
438         *y = screen->y + screen->height / 2 - client->area.height / 2;
439     else
440         *y = screen->y + settings->position.y;
441
442     return TRUE;
443 }
444
445 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
446 {
447     if (client->transient_for && client->type == OB_CLIENT_TYPE_DIALOG) {
448         if (client->transient_for != OB_TRAN_GROUP && !client->iconic) {
449             ObClient *c = client;
450             ObClient *p = client->transient_for;
451
452             if (client_normal(p)) {
453                 *x = (p->frame->area.width - c->frame->area.width) / 2 +
454                     p->frame->area.x;
455                 *y = (p->frame->area.height - c->frame->area.height) / 2 +
456                     p->frame->area.y;
457                 return TRUE;
458             }
459         } else {
460             GSList *it;
461             gboolean first = TRUE;
462             gint l, r, t, b;
463             for (it = client->group->members; it; it = g_slist_next(it)) {
464                 ObClient *m = it->data;
465                 if (!(m == client || m->transient_for) && client_normal(m) &&
466                     !m->iconic)
467                 {
468                     if (first) {
469                         l = RECT_LEFT(m->frame->area);
470                         t = RECT_TOP(m->frame->area);
471                         r = RECT_RIGHT(m->frame->area);
472                         b = RECT_BOTTOM(m->frame->area);
473                         first = FALSE;
474                     } else {
475                         l = MIN(l, RECT_LEFT(m->frame->area));
476                         t = MIN(t, RECT_TOP(m->frame->area));
477                         r = MAX(r, RECT_RIGHT(m->frame->area));
478                         b = MAX(b, RECT_BOTTOM(m->frame->area));
479                     }
480                 }
481             }
482             if (!first) {
483                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
484                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
485                 return TRUE;
486             }
487         }
488     }
489
490     if ((client->transient && client->type == OB_CLIENT_TYPE_DIALOG)
491         || client->type == OB_CLIENT_TYPE_SPLASH)
492     {
493         Rect **areas;
494
495         areas = pick_head(client);
496
497         *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
498         *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
499
500         g_free(areas);
501         return TRUE;
502     }
503
504     return FALSE;
505 }
506
507 /* Return TRUE if we want client.c to enforce on-screen-keeping */
508 gboolean place_client(ObClient *client, gint *x, gint *y,
509                       ObAppSettings *settings)
510 {
511     gboolean ret = FALSE;
512     if (client->positioned)
513         return FALSE;
514     if (place_transient_splash(client, x, y))
515         ret = TRUE;
516     else if (!(
517         place_per_app_setting(client, x, y, settings) ||
518         ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
519          place_under_mouse(client, x, y) :
520          place_smart(client, x, y, SMART_FULL, FALSE)   ||
521          place_smart(client, x, y, SMART_FULL, TRUE)    ||
522          place_smart(client, x, y, SMART_GROUP, FALSE)  ||
523          place_smart(client, x, y, SMART_GROUP, TRUE)   ||
524          place_smart(client, x, y, SMART_FOCUSED, TRUE) ||
525          place_random(client, x, y))))
526         g_assert_not_reached(); /* the last one better succeed */
527     /* get where the client should be */
528     frame_frame_gravity(client->frame, x, y,
529                         client->area.width, client->area.height);
530     return ret;
531 }