placement improvements?
[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 || !client_normal(c) || !c->frame->visible || \
272      (c->desktop != DESKTOP_ALL && \
273       c->desktop != (placer->desktop == DESKTOP_ALL ? \
274                      screen_desktop : placer->desktop)))
275
276 static gboolean place_smart(ObClient *client, gint *x, gint *y,
277                             ObSmartType type, gboolean ignore_max)
278 {
279     gboolean ret = FALSE;
280     GSList *spaces = NULL, *sit;
281     GList *it;
282     Rect **areas;
283     guint i;
284
285     if (type == SMART_GROUP) {
286         /* has to be more than me in the group */
287         if (!client_has_group_siblings(client))
288             return FALSE;
289     }
290
291     areas = pick_head(client);
292
293     for (i = 0; i < screen_num_monitors && !ret; ++i) {
294         spaces = area_add(spaces, areas[i]);
295
296         /* stay out from under windows in higher layers */
297         for (it = stacking_list; it; it = g_list_next(it)) {
298             ObClient *c;
299
300             if (WINDOW_IS_CLIENT(it->data)) {
301                 c = it->data;
302                 if (ignore_max &&
303                     (c->fullscreen || (c->max_vert && c->max_horz)))
304                     continue;
305             } else
306                 continue;
307
308             if (c->layer > client->layer) {
309                 if (!SMART_IGNORE(client, c))
310                     spaces = area_remove(spaces, &c->frame->area);
311             } else
312                 break;
313         }
314
315         if (type == SMART_FULL || type == SMART_FOCUSED) {
316             gboolean found_foc = FALSE, stop = FALSE;
317             ObClient *foc;
318
319             foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
320                                          screen_desktop : client->desktop);
321
322             for (; it && !stop; it = g_list_next(it)) {
323                 ObClient *c;
324
325                 if (WINDOW_IS_CLIENT(it->data)) {
326                     c = it->data;
327                     if (ignore_max &&
328                         (c->fullscreen || (c->max_vert && c->max_horz)))
329                         continue;
330                 } else
331                     continue;
332
333                 if (!SMART_IGNORE(client, c)) {
334                     if (type == SMART_FOCUSED)
335                         if (found_foc)
336                             stop = TRUE;
337                     if (!stop)
338                         spaces = area_remove(spaces, &c->frame->area);
339                 }
340
341                 if (c == foc)
342                     found_foc = TRUE;
343             }
344         } else if (type == SMART_GROUP) {
345             for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
346                 ObClient *c = sit->data;
347                 if (!SMART_IGNORE(client, c))
348                     spaces = area_remove(spaces, &c->frame->area);
349             }
350         } else
351             g_assert_not_reached();
352
353         spaces = g_slist_sort_with_data(spaces, area_cmp, client);
354
355         for (sit = spaces; sit; sit = g_slist_next(sit)) {
356             Rect *r = sit->data;
357
358             if (!ret) {
359                 if (r->width >= client->frame->area.width &&
360                     r->height >= client->frame->area.height) {
361                     ret = TRUE;
362                     if (client->type == OB_CLIENT_TYPE_DIALOG ||
363                         type != SMART_FULL)
364                     {
365                         *x = r->x + (r->width - client->frame->area.width)/2;
366                         *y = r->y + (r->height - client->frame->area.height)/2;
367                     } else {
368                         *x = r->x;
369                         *y = r->y;
370                     }
371                 }
372             }
373
374             g_free(r);
375         }
376         g_slist_free(spaces);
377         spaces = NULL;
378     }
379
380     g_free(areas);
381
382     return ret;
383 }
384
385 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
386 {
387     gint l, r, t, b;
388     gint px, py;
389     Rect *area;
390
391     area = pick_pointer_head(client);
392     screen_pointer_pos(&px, &py);
393
394     l = area->x;
395     t = area->y;
396     r = area->x + area->width - client->frame->area.width;
397     b = area->y + area->height - client->frame->area.height;
398
399     *x = px - client->area.width / 2 - client->frame->size.left;
400     *x = MIN(MAX(*x, l), r);
401     *y = py - client->area.height / 2 - client->frame->size.top;
402     *y = MIN(MAX(*y, t), b);
403
404     return TRUE;
405 }
406
407 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
408                                       ObAppSettings *settings)
409 {
410     Rect *screen = NULL;
411
412     if (!settings || (settings && !settings->pos_given))
413         return FALSE;
414
415     /* Find which head the pointer is on */
416     if (settings->monitor == 0)
417         screen = pick_pointer_head(client);
418     else if (settings->monitor > 0 &&
419              (guint)settings->monitor <= screen_num_monitors)
420         screen = screen_area_monitor(client->desktop,
421                                      (guint)settings->monitor - 1);
422     else {
423         Rect **all = NULL;
424         all = pick_head(client);
425         screen = all[0];
426         g_free(all); /* the areas themselves don't need to be freed */
427     }
428
429     if (settings->center_x)
430         *x = screen->x + screen->width / 2 - client->area.width / 2;
431     else
432         *x = screen->x + settings->position.x;
433
434     if (settings->center_y)
435         *y = screen->y + screen->height / 2 - client->area.height / 2;
436     else
437         *y = screen->y + settings->position.y;
438
439     return TRUE;
440 }
441
442 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
443 {
444     if (client->transient_for && client->type == OB_CLIENT_TYPE_DIALOG) {
445         if (client->transient_for != OB_TRAN_GROUP && !client->iconic) {
446             ObClient *c = client;
447             ObClient *p = client->transient_for;
448
449             if (client_normal(p)) {
450                 *x = (p->frame->area.width - c->frame->area.width) / 2 +
451                     p->frame->area.x;
452                 *y = (p->frame->area.height - c->frame->area.height) / 2 +
453                     p->frame->area.y;
454                 return TRUE;
455             }
456         } else {
457             GSList *it;
458             gboolean first = TRUE;
459             gint l, r, t, b;
460             for (it = client->group->members; it; it = g_slist_next(it)) {
461                 ObClient *m = it->data;
462                 if (!(m == client || m->transient_for) && client_normal(m) &&
463                     !m->iconic)
464                 {
465                     if (first) {
466                         l = RECT_LEFT(m->frame->area);
467                         t = RECT_TOP(m->frame->area);
468                         r = RECT_RIGHT(m->frame->area);
469                         b = RECT_BOTTOM(m->frame->area);
470                         first = FALSE;
471                     } else {
472                         l = MIN(l, RECT_LEFT(m->frame->area));
473                         t = MIN(t, RECT_TOP(m->frame->area));
474                         r = MAX(r, RECT_RIGHT(m->frame->area));
475                         b = MAX(b, RECT_BOTTOM(m->frame->area));
476                     }
477                 }
478             }
479             if (!first) {
480                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
481                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
482                 return TRUE;
483             }
484         }
485     }
486
487     if ((client->transient && client->type == OB_CLIENT_TYPE_DIALOG)
488         || client->type == OB_CLIENT_TYPE_SPLASH)
489     {
490         Rect **areas;
491
492         areas = pick_head(client);
493
494         *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
495         *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
496
497         g_free(areas);
498         return TRUE;
499     }
500
501     return FALSE;
502 }
503
504 /* Return TRUE if we want client.c to enforce on-screen-keeping */
505 gboolean place_client(ObClient *client, gint *x, gint *y,
506                       ObAppSettings *settings)
507 {
508     gboolean ret = FALSE;
509     if (client->positioned)
510         return FALSE;
511     if (place_transient_splash(client, x, y))
512         ret = TRUE;
513     else if (!(
514         place_per_app_setting(client, x, y, settings) ||
515         ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
516          place_under_mouse(client, x, y) :
517          place_smart(client, x, y, SMART_FULL, FALSE)   ||
518          place_smart(client, x, y, SMART_FULL, TRUE)    ||
519          place_smart(client, x, y, SMART_GROUP, FALSE)  ||
520          place_smart(client, x, y, SMART_GROUP, TRUE)   ||
521          place_smart(client, x, y, SMART_FOCUSED, TRUE) ||
522          place_random(client, x, y))))
523         g_assert_not_reached(); /* the last one better succeed */
524     /* get where the client should be */
525     frame_frame_gravity(client->frame, x, y,
526                         client->area.width, client->area.height);
527     return ret;
528 }