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