]> icculus.org git repositories - dana/openbox.git/blob - openbox/place.c
Rename ObFramePlugin to ObFrameEngine
[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 = render_plugin->frame_get_window_area(client->frame);
153
154     areas = pick_head(client);
155     i = (config_place_monitor != OB_PLACE_MONITOR_ANY) ?
156         0 : g_random_int_range(0, screen_num_monitors);
157
158     l = areas[i]->x;
159     t = areas[i]->y;
160     r = areas[i]->x + areas[i]->width - area.width;
161     b = areas[i]->y + areas[i]->height - area.height;
162
163     if (r > l) *x = g_random_int_range(l, r + 1);
164     else       *x = areas[i]->x;
165     if (b > t) *y = g_random_int_range(t, b + 1);
166     else       *y = areas[i]->y;
167
168     for (i = 0; i < screen_num_monitors; ++i)
169         g_free(areas[i]);
170     g_free(areas);
171
172     return TRUE;
173 }
174
175 static GSList* area_add(GSList *list, Rect *a)
176 {
177     Rect *r = g_new(Rect, 1);
178     *r = *a;
179     return g_slist_prepend(list, r);
180 }
181
182 static GSList* area_remove(GSList *list, Rect *a)
183 {
184     GSList *sit;
185     GSList *result = NULL;
186
187     for (sit = list; sit; sit = g_slist_next(sit)) {
188         Rect *r = sit->data;
189
190         if (!RECT_INTERSECTS_RECT(*r, *a)) {
191             result = g_slist_prepend(result, r);
192             /* dont free r, it's moved to the result list */
193         } else {
194             Rect isect, extra;
195
196             /* Use an intersection of a and r to determine the space
197                around r that we can use.
198
199                NOTE: the spaces calculated can overlap.
200             */
201
202             RECT_SET_INTERSECTION(isect, *r, *a);
203
204             if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
205                 RECT_SET(extra, r->x, r->y,
206                          RECT_LEFT(isect) - r->x, r->height);
207                 result = area_add(result, &extra);
208             }
209
210             if (RECT_TOP(isect) > RECT_TOP(*r)) {
211                 RECT_SET(extra, r->x, r->y,
212                          r->width, RECT_TOP(isect) - r->y + 1);
213                 result = area_add(result, &extra);
214             }
215
216             if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
217                 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
218                          RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
219                 result = area_add(result, &extra);
220             }
221
222             if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
223                 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
224                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
225                 result = area_add(result, &extra);
226             }
227
228             /* 'r' is not being added to the result list, so free it */
229             g_free(r);
230         }
231     }
232     g_slist_free(list);
233     return result;
234 }
235
236 enum {
237     IGNORE_FULLSCREEN = 1,
238     IGNORE_MAXIMIZED  = 2,
239     IGNORE_MENUTOOL   = 3,
240     /*IGNORE_SHADED     = 3,*/
241     IGNORE_NONGROUP   = 4,
242     IGNORE_BELOW      = 5,
243     /*IGNORE_NONFOCUS   = 1 << 5,*/
244     IGNORE_DOCK       = 6,
245     IGNORE_END        = 7
246 };
247
248 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
249 {
250     Rect **areas;
251     gint ignore;
252     gboolean ret;
253     gint maxsize;
254     GSList *spaces = NULL, *sit, *maxit;
255     guint i;
256
257     areas = pick_head(c);
258     ret = FALSE;
259     maxsize = 0;
260     maxit = NULL;
261
262     /* try ignoring different things to find empty space */
263     for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
264         /* try all monitors in order of preference, but only the first one
265            if config_place_monitor is MOUSE or ACTIVE */
266         for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
267                           1 : screen_num_monitors) && !ret); ++i)
268         {
269             GList *it;
270
271             /* add the whole monitor */
272             spaces = area_add(spaces, areas[i]);
273
274             /* go thru all the windows */
275             for (it = client_list; it; it = g_list_next(it)) {
276                 ObClient *test = it->data;
277
278                 /* should we ignore this client? */
279                 if (screen_showing_desktop) continue;
280                 if (c == test) continue;
281                 if (test->iconic) continue;
282                 if (c->desktop != DESKTOP_ALL) {
283                     if (test->desktop != c->desktop &&
284                         test->desktop != DESKTOP_ALL) continue;
285                 } else {
286                     if (test->desktop != screen_desktop &&
287                         test->desktop != DESKTOP_ALL) continue;
288                 }
289                 if (test->type == OB_CLIENT_TYPE_SPLASH ||
290                     test->type == OB_CLIENT_TYPE_DESKTOP) continue;
291
292
293                 if ((ignore >= IGNORE_FULLSCREEN) &&
294                     test->fullscreen) continue;
295                 if ((ignore >= IGNORE_MAXIMIZED) &&
296                     test->max_horz && test->max_vert) continue;
297                 if ((ignore >= IGNORE_MENUTOOL) &&
298                     (test->type == OB_CLIENT_TYPE_MENU ||
299                      test->type == OB_CLIENT_TYPE_TOOLBAR) &&
300                     client_has_parent(c)) continue;
301                 /*
302                 if ((ignore >= IGNORE_SHADED) &&
303                     test->shaded) continue;
304                 */
305                 if ((ignore >= IGNORE_NONGROUP) &&
306                     client_has_group_siblings(c) &&
307                     test->group != c->group) continue;
308                 if ((ignore >= IGNORE_BELOW) &&
309                     test->layer < c->layer) continue;
310                 /*
311                 if ((ignore >= IGNORE_NONFOCUS) &&
312                     focus_client != test) continue;
313                 */
314                 /* don't ignore this window, so remove it from the available
315                    area */
316                 Rect test_area = render_plugin->frame_get_window_area(test->frame);
317                 spaces = area_remove(spaces, &test_area);
318             }
319
320             if (ignore < IGNORE_DOCK) {
321                 Rect a;
322                 dock_get_area(&a);
323                 spaces = area_remove(spaces, &a);
324             }
325
326             Rect c_area = render_plugin->frame_get_window_area(c->frame);
327             for (sit = spaces; sit; sit = g_slist_next(sit)) {
328                 Rect *r = sit->data;
329
330                 if (r->width >= c_area.width &&
331                     r->height >= c_area.height &&
332                     r->width * r->height > maxsize)
333                 {
334                     maxsize = r->width * r->height;
335                     maxit = sit;
336                 }
337             }
338
339             if (maxit) {
340                 Rect *r = maxit->data;
341
342                 /* center it in the area */
343                 *x = r->x;
344                 *y = r->y;
345                 if (config_place_center) {
346                     *x += (r->width - c_area.width) / 2;
347                     *y += (r->height - c_area.height) / 2;
348                 }
349                 ret = TRUE;
350             }
351
352             while (spaces) {
353                 g_free(spaces->data);
354                 spaces = g_slist_delete_link(spaces, spaces);
355             }
356         }
357     }
358
359     for (i = 0; i < screen_num_monitors; ++i)
360         g_free(areas[i]);
361     g_free(areas);
362     return ret;
363 }
364
365 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
366 {
367     gint l, r, t, b;
368     gint px, py;
369     Rect *area;
370
371     Strut fsize = render_plugin->frame_get_size(client->frame);
372     Rect farea = render_plugin->frame_get_window_area(client->frame);
373
374     if (!screen_pointer_pos(&px, &py))
375         return FALSE;
376     area = pick_pointer_head(client);
377
378     l = area->x;
379     t = area->y;
380     r = area->x + area->width - farea.width;
381     b = area->y + area->height - farea.height;
382
383     *x = px - client->area.width / 2 - fsize.left;
384     *x = MIN(MAX(*x, l), r);
385     *y = py - client->area.height / 2 - fsize.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         /* this can return NULL */
402         screen = pick_pointer_head(client);
403     else if (settings->monitor > 0 &&
404              (guint)settings->monitor <= screen_num_monitors)
405         screen = screen_area(client->desktop, (guint)settings->monitor - 1,
406                              NULL);
407
408     /* if we have't found a screen yet.. */
409     if (!screen) {
410         Rect **areas;
411         guint i;
412
413         areas = pick_head(client);
414         screen = areas[0];
415
416         /* don't free the first one, it's being set as "screen" */
417         for (i = 1; i < screen_num_monitors; ++i)
418             g_free(areas[i]);
419         g_free(areas);
420     }
421
422     Rect farea = render_plugin->frame_get_window_area(client->frame);
423     if (settings->position.x.center)
424         *x = screen->x + screen->width / 2 - client->area.width / 2;
425     else if (settings->position.x.opposite)
426         *x = screen->x + screen->width - farea.width -
427             settings->position.x.pos;
428     else
429         *x = screen->x + settings->position.x.pos;
430
431     if (settings->position.y.center)
432         *y = screen->y + screen->height / 2 - client->area.height / 2;
433     else if (settings->position.y.opposite)
434         *y = screen->y + screen->height - farea.height -
435             settings->position.y.pos;
436     else
437         *y = screen->y + settings->position.y.pos;
438
439     g_free(screen);
440     return TRUE;
441 }
442
443 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
444 {
445     if (client->type == OB_CLIENT_TYPE_DIALOG) {
446         GSList *it;
447         gboolean first = TRUE;
448         gint l, r, t, b;
449         for (it = client->parents; it; it = g_slist_next(it)) {
450             ObClient *m = it->data;
451             Rect area = render_plugin->frame_get_window_area(m->frame);
452             if (!m->iconic) {
453                 if (first) {
454                     l = RECT_LEFT(area);
455                     t = RECT_TOP(area);
456                     r = RECT_RIGHT(area);
457                     b = RECT_BOTTOM(area);
458                     first = FALSE;
459                 } else {
460                     l = MIN(l, RECT_LEFT(area));
461                     t = MIN(t, RECT_TOP(area));
462                     r = MAX(r, RECT_RIGHT(area));
463                     b = MAX(b, RECT_BOTTOM(area));
464                 }
465             }
466             if (!first) {
467                 *x = ((r + 1 - l) - area.width) / 2 + l;
468                 *y = ((b + 1 - t) - area.height) / 2 + t;
469                 return TRUE;
470             }
471         }
472     }
473
474     if (client->type == OB_CLIENT_TYPE_DIALOG ||
475         client->type == OB_CLIENT_TYPE_SPLASH)
476     {
477         Rect **areas;
478         Rect area = render_plugin->frame_get_window_area(client->frame);
479         guint i;
480
481         areas = pick_head(client);
482
483         *x = (areas[0]->width - area.width) / 2 + areas[0]->x;
484         *y = (areas[0]->height - area.height) / 2 + areas[0]->y;
485
486         for (i = 0; i < screen_num_monitors; ++i)
487             g_free(areas[i]);
488         g_free(areas);
489         return TRUE;
490     }
491
492     return FALSE;
493 }
494
495 /* Return TRUE if we want client.c to enforce on-screen-keeping */
496 gboolean place_client(ObClient *client, gint *x, gint *y,
497                       ObAppSettings *settings)
498 {
499     gboolean ret;
500     gboolean userplaced = FALSE;
501
502     /* per-app settings override program specified position
503      * but not user specified, unless pos_force is enabled */
504     if (((client->positioned & USPosition) &&
505          !(settings && settings->pos_given && settings->pos_force)) ||
506         ((client->positioned & PPosition) &&
507          !(settings && settings->pos_given)))
508         return FALSE;
509
510     /* try a number of methods */
511     ret = place_transient_splash(client, x, y) ||
512         (userplaced = place_per_app_setting(client, x, y, settings)) ||
513         (config_place_policy == OB_PLACE_POLICY_MOUSE &&
514          place_under_mouse(client, x, y)) ||
515         place_nooverlap(client, x, y) ||
516         place_random(client, x, y);
517     g_assert(ret);
518
519     /* get where the client should be */
520     frame_frame_gravity(client, x, y);
521     return !userplaced;
522 }