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