plug memory leaks from the new strut code
[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         Rect *monitor = screen_physical_area_monitor(i);
50         gboolean contain = RECT_CONTAINS(*monitor, px, py);
51         g_free(monitor);
52         if (contain)
53             return screen_area_monitor(c->desktop, i, NULL);
54     }
55     g_assert_not_reached();
56 }
57
58 /*! Pick a monitor to place a window on. */
59 static Rect **pick_head(ObClient *c)
60 {
61     Rect **area;
62     guint *choice;
63     guint i;
64     gint px, py;
65     ObClient *p;
66
67     area = g_new(Rect*, screen_num_monitors);
68     choice = g_new(guint, screen_num_monitors);
69     for (i = 0; i < screen_num_monitors; ++i)
70         choice[i] = screen_num_monitors; /* make them all invalid to start */
71
72     /* try direct parent first */
73     if ((p = client_direct_parent(c))) {
74         add_choice(choice, client_monitor(p));
75         ob_debug("placement adding choice %d for parent\n",
76                  client_monitor(p));
77     }
78
79     /* more than one window in its group (more than just this window) */
80     if (client_has_group_siblings(c)) {
81         GSList *it;
82
83         /* try on the client's desktop */
84         for (it = c->group->members; it; it = g_slist_next(it)) {
85             ObClient *itc = it->data;            
86             if (itc != c &&
87                 (itc->desktop == c->desktop ||
88                  itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
89             {
90                 add_choice(choice, client_monitor(it->data));
91                 ob_debug("placement adding choice %d for group sibling\n",
92                          client_monitor(it->data));
93             }
94         }
95
96         /* try on all desktops */
97         for (it = c->group->members; it; it = g_slist_next(it)) {
98             ObClient *itc = it->data;            
99             if (itc != c) {
100                 add_choice(choice, client_monitor(it->data));
101                 ob_debug("placement adding choice %d for group sibling on "
102                          "another desktop\n", client_monitor(it->data));
103             }
104         }
105     }
106
107     if (focus_client) {
108         add_choice(choice, client_monitor(focus_client));
109         ob_debug("placement adding choice %d for focused window\n",
110                  client_monitor(focus_client));
111     }
112
113     screen_pointer_pos(&px, &py);
114
115     for (i = 0; i < screen_num_monitors; i++)
116         Rect *monitor = screen_physical_area_monitor(i);
117         gboolean contain = RECT_CONTAINS(*monitor, px, py);
118         g_free(monitor);
119         if (contain)
120             add_choice(choice, i);
121             ob_debug("placement adding choice %d for mouse pointer\n", i);
122             break;
123         }
124
125     /* add any leftover choices */
126     for (i = 0; i < screen_num_monitors; ++i)
127         add_choice(choice, i);
128
129     for (i = 0; i < screen_num_monitors; ++i)
130         area[i] = screen_area_monitor(c->desktop, choice[i], NULL);
131
132     return area;
133 }
134
135 static gboolean place_random(ObClient *client, gint *x, gint *y)
136 {
137     gint l, r, t, b;
138     Rect **areas;
139     guint i;
140
141     areas = pick_head(client);
142     i = g_random_int_range(0, screen_num_monitors);
143
144     l = areas[i]->x;
145     t = areas[i]->y;
146     r = areas[i]->x + areas[i]->width - client->frame->area.width;
147     b = areas[i]->y + areas[i]->height - client->frame->area.height;
148
149     if (r > l) *x = g_random_int_range(l, r + 1);
150     else       *x = areas[i]->x;
151     if (b > t) *y = g_random_int_range(t, b + 1);
152     else       *y = areas[i]->y;
153
154     for (i = 0; i < screen_num_monitors; ++i)
155         g_free(areas[i]);
156     g_free(areas);
157
158     return TRUE;
159 }
160
161 static GSList* area_add(GSList *list, Rect *a)
162 {
163     Rect *r = g_new(Rect, 1);
164     *r = *a;
165     return g_slist_prepend(list, r);
166 }
167
168 static GSList* area_remove(GSList *list, Rect *a)
169 {
170     GSList *sit;
171     GSList *result = NULL;
172
173     for (sit = list; sit; sit = g_slist_next(sit)) {
174         Rect *r = sit->data;
175
176         if (!RECT_INTERSECTS_RECT(*r, *a)) {
177             result = g_slist_prepend(result, r);
178             r = NULL; /* dont free it */
179         } else {
180             Rect isect, extra;
181
182             /* Use an intersection of a and r to determine the space
183                around r that we can use.
184
185                NOTE: the spaces calculated can overlap.
186             */
187
188             RECT_SET_INTERSECTION(isect, *r, *a);
189
190             if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
191                 RECT_SET(extra, r->x, r->y,
192                          RECT_LEFT(isect) - r->x, r->height);
193                 result = area_add(result, &extra);
194             }
195
196             if (RECT_TOP(isect) > RECT_TOP(*r)) {
197                 RECT_SET(extra, r->x, r->y,
198                          r->width, RECT_TOP(isect) - r->y + 1);
199                 result = area_add(result, &extra);
200             }
201
202             if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
203                 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
204                          RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
205                 result = area_add(result, &extra);
206             }
207
208             if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
209                 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
210                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
211                 result = area_add(result, &extra);
212             }
213         }
214
215         g_free(r);
216     }
217     g_slist_free(list);
218     return result;
219 }
220
221 enum {
222     IGNORE_FULLSCREEN = 1 << 0,
223     IGNORE_MAXIMIZED  = 1 << 1,
224     IGNORE_MENUTOOL   = 1 << 2,
225     /*IGNORE_SHADED     = 1 << 3,*/
226     IGNORE_NONGROUP   = 1 << 3,
227     IGNORE_BELOW      = 1 << 4,
228     IGNORE_NONFOCUS   = 1 << 5,
229     IGNORE_END        = 1 << 6
230 };
231
232 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
233 {
234     Rect **areas;
235     gint ignore;
236     gboolean ret;
237     gint maxsize;
238     GSList *spaces = NULL, *sit, *maxit;
239     guint i;
240
241     areas = pick_head(c);
242     ret = FALSE;
243     maxsize = 0;
244     maxit = NULL;
245
246     /* try ignoring different things to find empty space */
247     for (ignore = 0; ignore < IGNORE_END && !ret; ignore = (ignore << 1) + 1) {
248         guint i;
249
250         /* try all monitors in order of preference */
251         for (i = 0; i < screen_num_monitors && !ret; ++i) {
252             GList *it;
253
254             /* add the whole monitor */
255             spaces = area_add(spaces, areas[i]);
256
257             /* go thru all the windows */
258             for (it = client_list; it; it = g_list_next(it)) {
259                 ObClient *test = it->data;
260
261                 /* should we ignore this client? */
262                 if (screen_showing_desktop) continue;
263                 if (c == test) continue;
264                 if (test->iconic) continue;
265                 if (c->desktop != DESKTOP_ALL) {
266                     if (test->desktop != c->desktop &&
267                         test->desktop != DESKTOP_ALL) continue;
268                 } else {
269                     if (test->desktop != screen_desktop &&
270                         test->desktop != DESKTOP_ALL) continue;
271                 }
272                 if (test->type == OB_CLIENT_TYPE_SPLASH ||
273                     test->type == OB_CLIENT_TYPE_DESKTOP) continue;
274
275
276                 if ((ignore & IGNORE_FULLSCREEN) &&
277                     test->fullscreen) continue;
278                 if ((ignore & IGNORE_MAXIMIZED) &&
279                     test->max_horz && test->max_vert) continue;
280                 if ((ignore & IGNORE_MENUTOOL) &&
281                     (test->type == OB_CLIENT_TYPE_MENU ||
282                      test->type == OB_CLIENT_TYPE_TOOLBAR) &&
283                     client_has_parent(c)) continue;
284                 /*
285                 if ((ignore & IGNORE_SHADED) &&
286                     test->shaded) continue;
287                 */
288                 if ((ignore & IGNORE_NONGROUP) &&
289                     client_has_group_siblings(c) &&
290                     test->group != c->group) continue;
291                 if ((ignore & IGNORE_BELOW) &&
292                     test->layer < c->layer) continue;
293                 if ((ignore & IGNORE_NONFOCUS) &&
294                     focus_client != test) continue;
295
296                 /* don't ignore this window, so remove it from the available
297                    area */
298                 spaces = area_remove(spaces, &test->frame->area);
299             }
300
301             for (sit = spaces; sit; sit = g_slist_next(sit)) {
302                 Rect *r = sit->data;
303
304                 if (r->width >= c->frame->area.width &&
305                     r->height >= c->frame->area.height &&
306                     r->width > maxsize)
307                 {
308                     maxsize = r->width;
309                     maxit = sit;
310                 }
311             }
312
313             if (maxit) {
314                 Rect *r = maxit->data;
315
316                 /* center it in the area */
317                 *x = r->x + (r->width - c->frame->area.width) / 2;
318                 *y = r->y + (r->height - c->frame->area.height) / 2;
319                 ret = TRUE;
320             }
321
322             while (spaces) {
323                 g_free(spaces->data);
324                 spaces = g_slist_delete_link(spaces, spaces);
325             }
326         }
327     }
328
329     for (i = 0; i < screen_num_monitors; ++i)
330         g_free(areas[i]);
331     g_free(areas);
332     return ret;
333 }
334
335 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
336 {
337     gint l, r, t, b;
338     gint px, py;
339     Rect *area;
340
341     if (!screen_pointer_pos(&px, &py))
342         return FALSE;
343     area = pick_pointer_head(client);
344
345     l = area->x;
346     t = area->y;
347     r = area->x + area->width - client->frame->area.width;
348     b = area->y + area->height - client->frame->area.height;
349
350     *x = px - client->area.width / 2 - client->frame->size.left;
351     *x = MIN(MAX(*x, l), r);
352     *y = py - client->area.height / 2 - client->frame->size.top;
353     *y = MIN(MAX(*y, t), b);
354
355     return TRUE;
356 }
357
358 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
359                                       ObAppSettings *settings)
360 {
361     Rect *screen = NULL;
362
363     if (!settings || (settings && !settings->pos_given))
364         return FALSE;
365
366     /* Find which head the pointer is on */
367     if (settings->monitor == 0)
368         screen = pick_pointer_head(client);
369     else if (settings->monitor > 0 &&
370              (guint)settings->monitor <= screen_num_monitors)
371         screen = screen_area_monitor(client->desktop,
372                                      (guint)settings->monitor - 1, NULL);
373     else {
374         Rect **areas;
375         guint i;
376
377         areas = pick_head(client);
378         screen = areas[0];
379
380         for (i = 0; i < screen_num_monitors; ++i)
381             g_free(areas[i]);
382         g_free(areas);
383     }
384
385     if (settings->center_x)
386         *x = screen->x + screen->width / 2 - client->area.width / 2;
387     else if (settings->opposite_x)
388         *x = screen->x + screen->width - client->frame->area.width -
389             settings->position.x;
390     else
391         *x = screen->x + settings->position.x;
392
393     if (settings->center_y)
394         *y = screen->y + screen->height / 2 - client->area.height / 2;
395     else if (settings->opposite_y)
396         *y = screen->y + screen->height - client->frame->area.height -
397             settings->position.y;
398     else
399         *y = screen->y + settings->position.y;
400
401     return TRUE;
402 }
403
404 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
405 {
406     if (client->type == OB_CLIENT_TYPE_DIALOG) {
407         GSList *it;
408         gboolean first = TRUE;
409         gint l, r, t, b;
410         for (it = client->parents; it; it = g_slist_next(it)) {
411             ObClient *m = it->data;
412             if (!m->iconic) {
413                 if (first) {
414                     l = RECT_LEFT(m->frame->area);
415                     t = RECT_TOP(m->frame->area);
416                     r = RECT_RIGHT(m->frame->area);
417                     b = RECT_BOTTOM(m->frame->area);
418                     first = FALSE;
419                 } else {
420                     l = MIN(l, RECT_LEFT(m->frame->area));
421                     t = MIN(t, RECT_TOP(m->frame->area));
422                     r = MAX(r, RECT_RIGHT(m->frame->area));
423                     b = MAX(b, RECT_BOTTOM(m->frame->area));
424                 }
425             }
426             if (!first) {
427                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
428                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
429                 return TRUE;
430             }
431         }
432     }
433
434     if (client->type == OB_CLIENT_TYPE_DIALOG ||
435         client->type == OB_CLIENT_TYPE_SPLASH)
436     {
437         Rect **areas;
438         guint i;
439
440         areas = pick_head(client);
441
442         *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
443         *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
444
445         for (i = 0; i < screen_num_monitors; ++i)
446             g_free(areas[i]);
447         g_free(areas);
448         return TRUE;
449     }
450
451     return FALSE;
452 }
453
454 /* Return TRUE if we want client.c to enforce on-screen-keeping */
455 gboolean place_client(ObClient *client, gint *x, gint *y,
456                       ObAppSettings *settings)
457 {
458     gboolean ret;
459
460     if (client->positioned)
461         return FALSE;
462
463     /* try a number of methods */
464     ret = place_transient_splash(client, x, y) ||
465         place_per_app_setting(client, x, y, settings) ||
466         (config_place_policy == OB_PLACE_POLICY_MOUSE &&
467          place_under_mouse(client, x, y)) ||
468         place_nooverlap(client, x, y) ||
469         place_under_mouse(client, x, y) ||
470         place_random(client, x, y);
471     g_assert(ret);
472
473     /* get where the client should be */
474     frame_frame_gravity(client->frame, x, y,
475                         client->area.width, client->area.height);
476     return ret;
477 }