]> Dogcows Code - chaz/openbox/blob - openbox/place.c
make the doc and data/xsession directories in configure.ac
[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 || !client_normal(c) || !c->frame->visible || \
272 (c->desktop != DESKTOP_ALL && \
273 c->desktop != (placer->desktop == DESKTOP_ALL ? \
274 screen_desktop : placer->desktop)))
275
276 static gboolean place_smart(ObClient *client, gint *x, gint *y,
277 ObSmartType type)
278 {
279 gboolean ret = FALSE;
280 GSList *spaces = NULL, *sit;
281 GList *it;
282 Rect **areas;
283 guint i;
284
285 if (type == SMART_GROUP) {
286 /* has to be more than me in the group */
287 if (!client_has_group_siblings(client))
288 return FALSE;
289 }
290
291 areas = pick_head(client);
292
293 for (i = 0; i < screen_num_monitors && !ret; ++i) {
294 spaces = area_add(spaces, areas[i]);
295
296 /* stay out from under windows in higher layers */
297 for (it = stacking_list; it; it = g_list_next(it)) {
298 ObClient *c;
299
300 if (WINDOW_IS_CLIENT(it->data)) {
301 c = it->data;
302 if (c->fullscreen || (c->max_vert && c->max_horz))
303 continue;
304 } else
305 continue;
306
307 if (c->layer > client->layer) {
308 if (!SMART_IGNORE(client, c))
309 spaces = area_remove(spaces, &c->frame->area);
310 } else
311 break;
312 }
313
314 if (type == SMART_FULL || type == SMART_FOCUSED) {
315 gboolean found_foc = FALSE, stop = FALSE;
316 ObClient *foc;
317
318 foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
319 screen_desktop : client->desktop);
320
321 for (; it && !stop; it = g_list_next(it)) {
322 ObClient *c;
323
324 if (WINDOW_IS_CLIENT(it->data)) {
325 c = it->data;
326 if (c->fullscreen || (c->max_vert && c->max_horz))
327 continue;
328 } else
329 continue;
330
331 if (!SMART_IGNORE(client, c)) {
332 if (type == SMART_FOCUSED)
333 if (found_foc)
334 stop = TRUE;
335 if (!stop)
336 spaces = area_remove(spaces, &c->frame->area);
337 }
338
339 if (c == foc)
340 found_foc = TRUE;
341 }
342 } else if (type == SMART_GROUP) {
343 for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
344 ObClient *c = sit->data;
345 if (!SMART_IGNORE(client, c))
346 spaces = area_remove(spaces, &c->frame->area);
347 }
348 } else
349 g_assert_not_reached();
350
351 spaces = g_slist_sort_with_data(spaces, area_cmp, client);
352
353 for (sit = spaces; sit; sit = g_slist_next(sit)) {
354 Rect *r = sit->data;
355
356 if (!ret) {
357 if (r->width >= client->frame->area.width &&
358 r->height >= client->frame->area.height) {
359 ret = TRUE;
360 if (client->type == OB_CLIENT_TYPE_DIALOG ||
361 type != SMART_FULL)
362 {
363 *x = r->x + (r->width - client->frame->area.width)/2;
364 *y = r->y + (r->height - client->frame->area.height)/2;
365 } else {
366 *x = r->x;
367 *y = r->y;
368 }
369 }
370 }
371
372 g_free(r);
373 }
374 g_slist_free(spaces);
375 spaces = NULL;
376 }
377
378 g_free(areas);
379
380 return ret;
381 }
382
383 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
384 {
385 gint l, r, t, b;
386 gint px, py;
387 Rect *area;
388
389 area = pick_pointer_head(client);
390 screen_pointer_pos(&px, &py);
391
392 l = area->x;
393 t = area->y;
394 r = area->x + area->width - client->frame->area.width;
395 b = area->y + area->height - client->frame->area.height;
396
397 *x = px - client->area.width / 2 - client->frame->size.left;
398 *x = MIN(MAX(*x, l), r);
399 *y = py - client->area.height / 2 - client->frame->size.top;
400 *y = MIN(MAX(*y, t), b);
401
402 return TRUE;
403 }
404
405 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
406 ObAppSettings *settings)
407 {
408 Rect *screen = NULL;
409
410 if (!settings || (settings && !settings->pos_given))
411 return FALSE;
412
413 /* Find which head the pointer is on */
414 if (settings->monitor == 0)
415 screen = pick_pointer_head(client);
416 else if (settings->monitor > 0 &&
417 (guint)settings->monitor <= screen_num_monitors)
418 screen = screen_area_monitor(client->desktop,
419 (guint)settings->monitor - 1);
420 else {
421 Rect **all = NULL;
422 all = pick_head(client);
423 screen = all[0];
424 g_free(all); /* the areas themselves don't need to be freed */
425 }
426
427 if (settings->center_x)
428 *x = screen->x + screen->width / 2 - client->area.width / 2;
429 else
430 *x = screen->x + settings->position.x;
431
432 if (settings->center_y)
433 *y = screen->y + screen->height / 2 - client->area.height / 2;
434 else
435 *y = screen->y + settings->position.y;
436
437 return TRUE;
438 }
439
440 static gboolean place_transient(ObClient *client, gint *x, gint *y)
441 {
442 if (client->transient_for && client->type == OB_CLIENT_TYPE_DIALOG) {
443 if (client->transient_for != OB_TRAN_GROUP) {
444 ObClient *c = client;
445 ObClient *p = client->transient_for;
446
447 if (client_normal(p)) {
448 *x = (p->frame->area.width - c->frame->area.width) / 2 +
449 p->frame->area.x;
450 *y = (p->frame->area.height - c->frame->area.height) / 2 +
451 p->frame->area.y;
452 return TRUE;
453 }
454 } else {
455 GSList *it;
456 gboolean first = TRUE;
457 gint l, r, t, b;
458 for (it = client->group->members; it; it = g_slist_next(it)) {
459 ObClient *m = it->data;
460 if (!(m == client || m->transient_for) && client_normal(m)) {
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 }
475 if (!first) {
476 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
477 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
478 return TRUE;
479 }
480 }
481 }
482
483 if (client->transient) {
484 Rect **areas;
485
486 areas = pick_head(client);
487
488 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
489 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
490
491 g_free(areas);
492 return TRUE;
493 }
494
495 return FALSE;
496 }
497
498 /* Return TRUE if we want client.c to enforce on-screen-keeping */
499 gboolean place_client(ObClient *client, gint *x, gint *y,
500 ObAppSettings *settings)
501 {
502 gboolean ret = FALSE;
503 if (client->positioned)
504 return FALSE;
505 if (place_transient(client, x, y))
506 ret = TRUE;
507 else if (!(
508 place_per_app_setting(client, x, y, settings) ||
509 ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
510 place_under_mouse(client, x, y) :
511 place_smart(client, x, y, SMART_FULL) ||
512 place_smart(client, x, y, SMART_GROUP) ||
513 place_smart(client, x, y, SMART_FOCUSED) ||
514 place_random(client, x, y))))
515 g_assert_not_reached(); /* the last one better succeed */
516 /* get where the client should be */
517 frame_frame_gravity(client->frame, x, y,
518 client->area.width, client->area.height);
519 return ret;
520 }
This page took 0.062141 seconds and 4 git commands to generate.