]> Dogcows Code - chaz/openbox/blob - openbox/place.c
Fix shadowed variables
[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 g_free(choice);
138
139 return area;
140 }
141
142 static gboolean place_random(ObClient *client, gint *x, gint *y)
143 {
144 gint l, r, t, b;
145 Rect **areas;
146 guint i;
147
148 areas = pick_head(client);
149 i = g_random_int_range(0, screen_num_monitors);
150
151 l = areas[i]->x;
152 t = areas[i]->y;
153 r = areas[i]->x + areas[i]->width - client->frame->area.width;
154 b = areas[i]->y + areas[i]->height - client->frame->area.height;
155
156 if (r > l) *x = g_random_int_range(l, r + 1);
157 else *x = areas[i]->x;
158 if (b > t) *y = g_random_int_range(t, b + 1);
159 else *y = areas[i]->y;
160
161 for (i = 0; i < screen_num_monitors; ++i)
162 g_free(areas[i]);
163 g_free(areas);
164
165 return TRUE;
166 }
167
168 static GSList* area_add(GSList *list, Rect *a)
169 {
170 Rect *r = g_new(Rect, 1);
171 *r = *a;
172 return g_slist_prepend(list, r);
173 }
174
175 static GSList* area_remove(GSList *list, Rect *a)
176 {
177 GSList *sit;
178 GSList *result = NULL;
179
180 for (sit = list; sit; sit = g_slist_next(sit)) {
181 Rect *r = sit->data;
182
183 if (!RECT_INTERSECTS_RECT(*r, *a)) {
184 result = g_slist_prepend(result, r);
185 /* dont free r, it's moved to the result list */
186 } else {
187 Rect isect, extra;
188
189 /* Use an intersection of a and r to determine the space
190 around r that we can use.
191
192 NOTE: the spaces calculated can overlap.
193 */
194
195 RECT_SET_INTERSECTION(isect, *r, *a);
196
197 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
198 RECT_SET(extra, r->x, r->y,
199 RECT_LEFT(isect) - r->x, r->height);
200 result = area_add(result, &extra);
201 }
202
203 if (RECT_TOP(isect) > RECT_TOP(*r)) {
204 RECT_SET(extra, r->x, r->y,
205 r->width, RECT_TOP(isect) - r->y + 1);
206 result = area_add(result, &extra);
207 }
208
209 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
210 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
211 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
212 result = area_add(result, &extra);
213 }
214
215 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
216 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
217 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
218 result = area_add(result, &extra);
219 }
220
221 /* 'r' is not being added to the result list, so free it */
222 g_free(r);
223 }
224 }
225 g_slist_free(list);
226 return result;
227 }
228
229 enum {
230 IGNORE_FULLSCREEN = 1,
231 IGNORE_MAXIMIZED = 2,
232 IGNORE_MENUTOOL = 3,
233 /*IGNORE_SHADED = 3,*/
234 IGNORE_NONGROUP = 4,
235 IGNORE_BELOW = 5,
236 /*IGNORE_NONFOCUS = 1 << 5,*/
237 IGNORE_DOCK = 6,
238 IGNORE_END = 7
239 };
240
241 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
242 {
243 Rect **areas;
244 gint ignore;
245 gboolean ret;
246 gint maxsize;
247 GSList *spaces = NULL, *sit, *maxit;
248 guint i;
249
250 areas = pick_head(c);
251 ret = FALSE;
252 maxsize = 0;
253 maxit = NULL;
254
255 /* try ignoring different things to find empty space */
256 for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
257 /* try all monitors in order of preference */
258 for (i = 0; i < screen_num_monitors && !ret; ++i) {
259 GList *it;
260
261 /* add the whole monitor */
262 spaces = area_add(spaces, areas[i]);
263
264 /* go thru all the windows */
265 for (it = client_list; it; it = g_list_next(it)) {
266 ObClient *test = it->data;
267
268 /* should we ignore this client? */
269 if (screen_showing_desktop) continue;
270 if (c == test) continue;
271 if (test->iconic) continue;
272 if (c->desktop != DESKTOP_ALL) {
273 if (test->desktop != c->desktop &&
274 test->desktop != DESKTOP_ALL) continue;
275 } else {
276 if (test->desktop != screen_desktop &&
277 test->desktop != DESKTOP_ALL) continue;
278 }
279 if (test->type == OB_CLIENT_TYPE_SPLASH ||
280 test->type == OB_CLIENT_TYPE_DESKTOP) continue;
281
282
283 if ((ignore >= IGNORE_FULLSCREEN) &&
284 test->fullscreen) continue;
285 if ((ignore >= IGNORE_MAXIMIZED) &&
286 test->max_horz && test->max_vert) continue;
287 if ((ignore >= IGNORE_MENUTOOL) &&
288 (test->type == OB_CLIENT_TYPE_MENU ||
289 test->type == OB_CLIENT_TYPE_TOOLBAR) &&
290 client_has_parent(c)) continue;
291 /*
292 if ((ignore >= IGNORE_SHADED) &&
293 test->shaded) continue;
294 */
295 if ((ignore >= IGNORE_NONGROUP) &&
296 client_has_group_siblings(c) &&
297 test->group != c->group) continue;
298 if ((ignore >= IGNORE_BELOW) &&
299 test->layer < c->layer) continue;
300 /*
301 if ((ignore >= IGNORE_NONFOCUS) &&
302 focus_client != test) continue;
303 */
304 /* don't ignore this window, so remove it from the available
305 area */
306 spaces = area_remove(spaces, &test->frame->area);
307 }
308
309 if (ignore < IGNORE_DOCK) {
310 Rect a;
311 dock_get_area(&a);
312 spaces = area_remove(spaces, &a);
313 }
314
315 for (sit = spaces; sit; sit = g_slist_next(sit)) {
316 Rect *r = sit->data;
317
318 if (r->width >= c->frame->area.width &&
319 r->height >= c->frame->area.height &&
320 r->width * r->height > maxsize)
321 {
322 maxsize = r->width * r->height;
323 maxit = sit;
324 }
325 }
326
327 if (maxit) {
328 Rect *r = maxit->data;
329
330 /* center it in the area */
331 *x = r->x;
332 *y = r->y;
333 if (config_place_center) {
334 *x += (r->width - c->frame->area.width) / 2;
335 *y += (r->height - c->frame->area.height) / 2;
336 }
337 ret = TRUE;
338 }
339
340 while (spaces) {
341 g_free(spaces->data);
342 spaces = g_slist_delete_link(spaces, spaces);
343 }
344 }
345 }
346
347 for (i = 0; i < screen_num_monitors; ++i)
348 g_free(areas[i]);
349 g_free(areas);
350 return ret;
351 }
352
353 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
354 {
355 gint l, r, t, b;
356 gint px, py;
357 Rect *area;
358
359 if (!screen_pointer_pos(&px, &py))
360 return FALSE;
361 area = pick_pointer_head(client);
362
363 l = area->x;
364 t = area->y;
365 r = area->x + area->width - client->frame->area.width;
366 b = area->y + area->height - client->frame->area.height;
367
368 *x = px - client->area.width / 2 - client->frame->size.left;
369 *x = MIN(MAX(*x, l), r);
370 *y = py - client->area.height / 2 - client->frame->size.top;
371 *y = MIN(MAX(*y, t), b);
372
373 return TRUE;
374 }
375
376 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
377 ObAppSettings *settings)
378 {
379 Rect *screen = NULL;
380
381 if (!settings || (settings && !settings->pos_given))
382 return FALSE;
383
384 /* Find which head the pointer is on */
385 if (settings->monitor == 0)
386 /* this can return NULL */
387 screen = pick_pointer_head(client);
388 else if (settings->monitor > 0 &&
389 (guint)settings->monitor <= screen_num_monitors)
390 screen = screen_area(client->desktop, (guint)settings->monitor - 1,
391 NULL);
392
393 /* if we have't found a screen yet.. */
394 if (!screen) {
395 Rect **areas;
396 guint i;
397
398 areas = pick_head(client);
399 screen = areas[0];
400
401 /* don't free the first one, it's being set as "screen" */
402 for (i = 1; i < screen_num_monitors; ++i)
403 g_free(areas[i]);
404 g_free(areas);
405 }
406
407 if (settings->center_x)
408 *x = screen->x + screen->width / 2 - client->area.width / 2;
409 else if (settings->opposite_x)
410 *x = screen->x + screen->width - client->frame->area.width -
411 settings->position.x;
412 else
413 *x = screen->x + settings->position.x;
414
415 if (settings->center_y)
416 *y = screen->y + screen->height / 2 - client->area.height / 2;
417 else if (settings->opposite_y)
418 *y = screen->y + screen->height - client->frame->area.height -
419 settings->position.y;
420 else
421 *y = screen->y + settings->position.y;
422
423 g_free(screen);
424 return TRUE;
425 }
426
427 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
428 {
429 if (client->type == OB_CLIENT_TYPE_DIALOG) {
430 GSList *it;
431 gboolean first = TRUE;
432 gint l, r, t, b;
433 for (it = client->parents; it; it = g_slist_next(it)) {
434 ObClient *m = it->data;
435 if (!m->iconic) {
436 if (first) {
437 l = RECT_LEFT(m->frame->area);
438 t = RECT_TOP(m->frame->area);
439 r = RECT_RIGHT(m->frame->area);
440 b = RECT_BOTTOM(m->frame->area);
441 first = FALSE;
442 } else {
443 l = MIN(l, RECT_LEFT(m->frame->area));
444 t = MIN(t, RECT_TOP(m->frame->area));
445 r = MAX(r, RECT_RIGHT(m->frame->area));
446 b = MAX(b, RECT_BOTTOM(m->frame->area));
447 }
448 }
449 if (!first) {
450 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
451 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
452 return TRUE;
453 }
454 }
455 }
456
457 if (client->type == OB_CLIENT_TYPE_DIALOG ||
458 client->type == OB_CLIENT_TYPE_SPLASH)
459 {
460 Rect **areas;
461 guint i;
462
463 areas = pick_head(client);
464
465 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
466 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
467
468 for (i = 0; i < screen_num_monitors; ++i)
469 g_free(areas[i]);
470 g_free(areas);
471 return TRUE;
472 }
473
474 return FALSE;
475 }
476
477 /* Return TRUE if we want client.c to enforce on-screen-keeping */
478 gboolean place_client(ObClient *client, gint *x, gint *y,
479 ObAppSettings *settings)
480 {
481 gboolean ret;
482 gboolean userplaced = FALSE;
483
484 /* per-app settings override program specified position
485 * but not user specified */
486 if ((client->positioned & USPosition) ||
487 ((client->positioned & PPosition) &&
488 !(settings && settings->pos_given)))
489 return FALSE;
490
491 /* try a number of methods */
492 ret = place_transient_splash(client, x, y) ||
493 (userplaced = place_per_app_setting(client, x, y, settings)) ||
494 (config_place_policy == OB_PLACE_POLICY_MOUSE &&
495 place_under_mouse(client, x, y)) ||
496 place_nooverlap(client, x, y) ||
497 place_random(client, x, y);
498 g_assert(ret);
499
500 /* get where the client should be */
501 frame_frame_gravity(client->frame, x, y);
502 return !userplaced;
503 }
This page took 0.099052 seconds and 5 git commands to generate.