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