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