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