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