]> Dogcows Code - chaz/openbox/blob - openbox/place.c
don't place windows over docks. but let them place over menus and toolbars. i guess...
[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 "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);
51 }
52 }
53 g_assert_not_reached();
54 }
55
56 /*! Pick a monitor to place a window on.
57 The returned array value should be freed with g_free. The areas within the
58 array should not be freed. */
59 static Rect **pick_head(ObClient *c)
60 {
61 Rect **area;
62 guint *choice;
63 guint i;
64 gint px, py;
65
66 area = g_new(Rect*, screen_num_monitors);
67 choice = g_new(guint, screen_num_monitors);
68 for (i = 0; i < screen_num_monitors; ++i)
69 choice[i] = screen_num_monitors; /* make them all invalid to start */
70
71 /* try direct parent first */
72 if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
73 add_choice(choice, client_monitor(c->transient_for));
74 ob_debug("placement adding choice %d for parent\n",
75 client_monitor(c->transient_for));
76 }
77
78 /* more than one window in its group (more than just this window) */
79 if (client_has_group_siblings(c)) {
80 GSList *it;
81
82 /* try on the client's desktop */
83 for (it = c->group->members; it; it = g_slist_next(it)) {
84 ObClient *itc = it->data;
85 if (itc != c &&
86 (itc->desktop == c->desktop ||
87 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
88 {
89 add_choice(choice, client_monitor(it->data));
90 ob_debug("placement adding choice %d for group sibling\n",
91 client_monitor(it->data));
92 }
93 }
94
95 /* try on all desktops */
96 for (it = c->group->members; it; it = g_slist_next(it)) {
97 ObClient *itc = it->data;
98 if (itc != c) {
99 add_choice(choice, client_monitor(it->data));
100 ob_debug("placement adding choice %d for group sibling on "
101 "another desktop\n", client_monitor(it->data));
102 }
103 }
104 }
105
106 if (focus_client) {
107 add_choice(choice, client_monitor(focus_client));
108 ob_debug("placement adding choice %d for focused window\n",
109 client_monitor(focus_client));
110 }
111
112 screen_pointer_pos(&px, &py);
113
114 for (i = 0; i < screen_num_monitors; i++)
115 if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
116 add_choice(choice, i);
117 ob_debug("placement adding choice %d for mouse pointer\n", i);
118 break;
119 }
120
121 /* add any leftover choices */
122 for (i = 0; i < screen_num_monitors; ++i)
123 add_choice(choice, i);
124
125 for (i = 0; i < screen_num_monitors; ++i)
126 area[i] = screen_area_monitor(c->desktop, choice[i]);
127
128 return area;
129 }
130
131 static gboolean place_random(ObClient *client, gint *x, gint *y)
132 {
133 gint l, r, t, b;
134 Rect **areas;
135 guint i;
136
137 areas = pick_head(client);
138 i = g_random_int_range(0, screen_num_monitors);
139
140 l = areas[i]->x;
141 t = areas[i]->y;
142 r = areas[i]->x + areas[i]->width - client->frame->area.width;
143 b = areas[i]->y + areas[i]->height - client->frame->area.height;
144
145 if (r > l) *x = g_random_int_range(l, r + 1);
146 else *x = areas[i]->x;
147 if (b > t) *y = g_random_int_range(t, b + 1);
148 else *y = areas[i]->y;
149
150 g_free(areas);
151
152 return TRUE;
153 }
154
155 static GSList* area_add(GSList *list, Rect *a)
156 {
157 Rect *r = g_new(Rect, 1);
158 *r = *a;
159 return g_slist_prepend(list, r);
160 }
161
162 static GSList* area_remove(GSList *list, Rect *a)
163 {
164 GSList *sit;
165 GSList *result = NULL;
166
167 for (sit = list; sit; sit = g_slist_next(sit)) {
168 Rect *r = sit->data;
169
170 if (!RECT_INTERSECTS_RECT(*r, *a)) {
171 result = g_slist_prepend(result, r);
172 r = NULL; /* dont free it */
173 } else {
174 Rect isect, extra;
175
176 /* Use an intersection of a and r to determine the space
177 around r that we can use.
178
179 NOTE: the spaces calculated can overlap.
180 */
181
182 RECT_SET_INTERSECTION(isect, *r, *a);
183
184 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
185 RECT_SET(extra, r->x, r->y,
186 RECT_LEFT(isect) - r->x, r->height);
187 result = area_add(result, &extra);
188 }
189
190 if (RECT_TOP(isect) > RECT_TOP(*r)) {
191 RECT_SET(extra, r->x, r->y,
192 r->width, RECT_TOP(isect) - r->y + 1);
193 result = area_add(result, &extra);
194 }
195
196 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
197 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
198 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
199 result = area_add(result, &extra);
200 }
201
202 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
203 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
204 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
205 result = area_add(result, &extra);
206 }
207 }
208
209 g_free(r);
210 }
211 g_slist_free(list);
212 return result;
213 }
214
215 static gint area_cmp(gconstpointer p1, gconstpointer p2, gpointer data)
216 {
217 ObClient *c = data;
218 Rect *carea = &c->frame->area;
219 const Rect *a1 = p1, *a2 = p2;
220 gboolean diffhead = FALSE;
221 guint i;
222 Rect *a;
223
224 for (i = 0; i < screen_num_monitors; ++i) {
225 a = screen_physical_area_monitor(i);
226 if (RECT_CONTAINS(*a, a1->x, a1->y) &&
227 !RECT_CONTAINS(*a, a2->x, a2->y))
228 {
229 diffhead = TRUE;
230 break;
231 }
232 }
233
234 /* has to be more than me in the group */
235 if (diffhead && client_has_group_siblings(c)) {
236 guint *num, most;
237 GSList *it;
238
239 /* find how many clients in the group are on each monitor, use the
240 monitor with the most in it */
241 num = g_new0(guint, screen_num_monitors);
242 for (it = c->group->members; it; it = g_slist_next(it))
243 if (it->data != c)
244 ++num[client_monitor(it->data)];
245 most = 0;
246 for (i = 1; i < screen_num_monitors; ++i)
247 if (num[i] > num[most])
248 most = i;
249
250 g_free(num);
251
252 a = screen_physical_area_monitor(most);
253 if (RECT_CONTAINS(*a, a1->x, a1->y))
254 return -1;
255 if (RECT_CONTAINS(*a, a2->x, a2->y))
256 return 1;
257 }
258
259 return MIN((a1->width - carea->width), (a1->height - carea->height)) -
260 MIN((a2->width - carea->width), (a2->height - carea->height));
261 }
262
263 typedef enum
264 {
265 SMART_FULL,
266 SMART_GROUP,
267 SMART_FOCUSED
268 } ObSmartType;
269
270 #define SMART_IGNORE(placer, c) \
271 (placer == c || c->shaded || !c->frame->visible || \
272 c->type == OB_CLIENT_TYPE_SPLASH || c->type == OB_CLIENT_TYPE_DESKTOP || \
273 c->type == OB_CLIENT_TYPE_MENU || c->type == OB_CLIENT_TYPE_TOOLBAR || \
274 (c->desktop != DESKTOP_ALL && \
275 c->desktop != (placer->desktop == DESKTOP_ALL ? \
276 screen_desktop : placer->desktop)))
277
278 static gboolean place_smart(ObClient *client, gint *x, gint *y,
279 ObSmartType type, gboolean ignore_max)
280 {
281 gboolean ret = FALSE;
282 GSList *spaces = NULL, *sit;
283 GList *it;
284 Rect **areas;
285 guint i;
286
287 if (type == SMART_GROUP) {
288 /* has to be more than me in the group */
289 if (!client_has_group_siblings(client))
290 return FALSE;
291 }
292
293 areas = pick_head(client);
294
295 for (i = 0; i < screen_num_monitors && !ret; ++i) {
296 spaces = area_add(spaces, areas[i]);
297
298 /* stay out from under windows in higher layers */
299 for (it = stacking_list; it; it = g_list_next(it)) {
300 ObClient *c;
301
302 if (WINDOW_IS_CLIENT(it->data)) {
303 c = it->data;
304 if (ignore_max &&
305 (c->fullscreen || (c->max_vert && c->max_horz)))
306 continue;
307 } else
308 continue;
309
310 if (c->layer > client->layer) {
311 if (!SMART_IGNORE(client, c))
312 spaces = area_remove(spaces, &c->frame->area);
313 } else
314 break;
315 }
316
317 if (type == SMART_FULL || type == SMART_FOCUSED) {
318 gboolean found_foc = FALSE, stop = FALSE;
319 ObClient *foc;
320
321 foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
322 screen_desktop : client->desktop);
323
324 for (; it && !stop; it = g_list_next(it)) {
325 ObClient *c;
326
327 if (WINDOW_IS_CLIENT(it->data)) {
328 c = it->data;
329 if (ignore_max &&
330 (c->fullscreen || (c->max_vert && c->max_horz)))
331 continue;
332 } else
333 continue;
334
335 if (!SMART_IGNORE(client, c)) {
336 if (type == SMART_FOCUSED)
337 if (found_foc)
338 stop = TRUE;
339 if (!stop)
340 spaces = area_remove(spaces, &c->frame->area);
341 }
342
343 if (c == foc)
344 found_foc = TRUE;
345 }
346 } else if (type == SMART_GROUP) {
347 for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
348 ObClient *c = sit->data;
349 if (!SMART_IGNORE(client, c))
350 spaces = area_remove(spaces, &c->frame->area);
351 }
352 } else
353 g_assert_not_reached();
354
355 spaces = g_slist_sort_with_data(spaces, area_cmp, client);
356
357 for (sit = spaces; sit; sit = g_slist_next(sit)) {
358 Rect *r = sit->data;
359
360 if (!ret) {
361 if (r->width >= client->frame->area.width &&
362 r->height >= client->frame->area.height) {
363 ret = TRUE;
364 if (client->type == OB_CLIENT_TYPE_DIALOG ||
365 type != SMART_FULL)
366 {
367 *x = r->x + (r->width - client->frame->area.width)/2;
368 *y = r->y + (r->height - client->frame->area.height)/2;
369 } else {
370 *x = r->x;
371 *y = r->y;
372 }
373 }
374 }
375
376 g_free(r);
377 }
378 g_slist_free(spaces);
379 spaces = NULL;
380 }
381
382 g_free(areas);
383
384 return ret;
385 }
386
387 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
388 {
389 gint l, r, t, b;
390 gint px, py;
391 Rect *area;
392
393 area = pick_pointer_head(client);
394 screen_pointer_pos(&px, &py);
395
396 l = area->x;
397 t = area->y;
398 r = area->x + area->width - client->frame->area.width;
399 b = area->y + area->height - client->frame->area.height;
400
401 *x = px - client->area.width / 2 - client->frame->size.left;
402 *x = MIN(MAX(*x, l), r);
403 *y = py - client->area.height / 2 - client->frame->size.top;
404 *y = MIN(MAX(*y, t), b);
405
406 return TRUE;
407 }
408
409 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
410 ObAppSettings *settings)
411 {
412 Rect *screen = NULL;
413
414 if (!settings || (settings && !settings->pos_given))
415 return FALSE;
416
417 /* Find which head the pointer is on */
418 if (settings->monitor == 0)
419 screen = pick_pointer_head(client);
420 else if (settings->monitor > 0 &&
421 (guint)settings->monitor <= screen_num_monitors)
422 screen = screen_area_monitor(client->desktop,
423 (guint)settings->monitor - 1);
424 else {
425 Rect **all = NULL;
426 all = pick_head(client);
427 screen = all[0];
428 g_free(all); /* the areas themselves don't need to be freed */
429 }
430
431 if (settings->center_x)
432 *x = screen->x + screen->width / 2 - client->area.width / 2;
433 else
434 *x = screen->x + settings->position.x;
435
436 if (settings->center_y)
437 *y = screen->y + screen->height / 2 - client->area.height / 2;
438 else
439 *y = screen->y + settings->position.y;
440
441 return TRUE;
442 }
443
444 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
445 {
446 if (client->transient_for && client->type == OB_CLIENT_TYPE_DIALOG) {
447 if (client->transient_for != OB_TRAN_GROUP && !client->iconic) {
448 ObClient *c = client;
449 ObClient *p = client->transient_for;
450
451 if (client_normal(p)) {
452 *x = (p->frame->area.width - c->frame->area.width) / 2 +
453 p->frame->area.x;
454 *y = (p->frame->area.height - c->frame->area.height) / 2 +
455 p->frame->area.y;
456 return TRUE;
457 }
458 } else {
459 GSList *it;
460 gboolean first = TRUE;
461 gint l, r, t, b;
462 for (it = client->group->members; it; it = g_slist_next(it)) {
463 ObClient *m = it->data;
464 if (!(m == client || m->transient_for) && client_normal(m) &&
465 !m->iconic)
466 {
467 if (first) {
468 l = RECT_LEFT(m->frame->area);
469 t = RECT_TOP(m->frame->area);
470 r = RECT_RIGHT(m->frame->area);
471 b = RECT_BOTTOM(m->frame->area);
472 first = FALSE;
473 } else {
474 l = MIN(l, RECT_LEFT(m->frame->area));
475 t = MIN(t, RECT_TOP(m->frame->area));
476 r = MAX(r, RECT_RIGHT(m->frame->area));
477 b = MAX(b, RECT_BOTTOM(m->frame->area));
478 }
479 }
480 }
481 if (!first) {
482 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
483 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
484 return TRUE;
485 }
486 }
487 }
488
489 if ((client->transient && client->type == OB_CLIENT_TYPE_DIALOG)
490 || client->type == OB_CLIENT_TYPE_SPLASH)
491 {
492 Rect **areas;
493
494 areas = pick_head(client);
495
496 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
497 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
498
499 g_free(areas);
500 return TRUE;
501 }
502
503 return FALSE;
504 }
505
506 /* Return TRUE if we want client.c to enforce on-screen-keeping */
507 gboolean place_client(ObClient *client, gint *x, gint *y,
508 ObAppSettings *settings)
509 {
510 gboolean ret = FALSE;
511 if (client->positioned)
512 return FALSE;
513 if (place_transient_splash(client, x, y))
514 ret = TRUE;
515 else if (!(
516 place_per_app_setting(client, x, y, settings) ||
517 ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
518 place_under_mouse(client, x, y) :
519 place_smart(client, x, y, SMART_FULL, FALSE) ||
520 place_smart(client, x, y, SMART_FULL, TRUE) ||
521 place_smart(client, x, y, SMART_GROUP, FALSE) ||
522 place_smart(client, x, y, SMART_GROUP, TRUE) ||
523 place_smart(client, x, y, SMART_FOCUSED, TRUE) ||
524 place_random(client, x, y))))
525 g_assert_not_reached(); /* the last one better succeed */
526 /* get where the client should be */
527 frame_frame_gravity(client->frame, x, y,
528 client->area.width, client->area.height);
529 return ret;
530 }
This page took 0.057951 seconds and 5 git commands to generate.