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