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