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