]> Dogcows Code - chaz/openbox/blob - openbox/place.c
initalize static stuff.
[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 + (r->width - c->frame->area.width) / 2;
330 *y = r->y + (r->height - c->frame->area.height) / 2;
331 ret = TRUE;
332 }
333
334 while (spaces) {
335 g_free(spaces->data);
336 spaces = g_slist_delete_link(spaces, spaces);
337 }
338 }
339 }
340
341 for (i = 0; i < screen_num_monitors; ++i)
342 g_free(areas[i]);
343 g_free(areas);
344 return ret;
345 }
346
347 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
348 {
349 gint l, r, t, b;
350 gint px, py;
351 Rect *area;
352
353 if (!screen_pointer_pos(&px, &py))
354 return FALSE;
355 area = pick_pointer_head(client);
356
357 l = area->x;
358 t = area->y;
359 r = area->x + area->width - client->frame->area.width;
360 b = area->y + area->height - client->frame->area.height;
361
362 *x = px - client->area.width / 2 - client->frame->size.left;
363 *x = MIN(MAX(*x, l), r);
364 *y = py - client->area.height / 2 - client->frame->size.top;
365 *y = MIN(MAX(*y, t), b);
366
367 return TRUE;
368 }
369
370 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
371 ObAppSettings *settings)
372 {
373 Rect *screen = NULL;
374
375 if (!settings || (settings && !settings->pos_given))
376 return FALSE;
377
378 /* Find which head the pointer is on */
379 if (settings->monitor == 0)
380 screen = pick_pointer_head(client);
381 else if (settings->monitor > 0 &&
382 (guint)settings->monitor <= screen_num_monitors)
383 screen = screen_area(client->desktop, (guint)settings->monitor - 1,
384 NULL);
385 else {
386 Rect **areas;
387 guint i;
388
389 areas = pick_head(client);
390 screen = areas[0];
391
392 for (i = 0; i < screen_num_monitors; ++i)
393 g_free(areas[i]);
394 g_free(areas);
395 }
396
397 if (settings->center_x)
398 *x = screen->x + screen->width / 2 - client->area.width / 2;
399 else if (settings->opposite_x)
400 *x = screen->x + screen->width - client->frame->area.width -
401 settings->position.x;
402 else
403 *x = screen->x + settings->position.x;
404
405 if (settings->center_y)
406 *y = screen->y + screen->height / 2 - client->area.height / 2;
407 else if (settings->opposite_y)
408 *y = screen->y + screen->height - client->frame->area.height -
409 settings->position.y;
410 else
411 *y = screen->y + settings->position.y;
412
413 return TRUE;
414 }
415
416 static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
417 {
418 if (client->type == OB_CLIENT_TYPE_DIALOG) {
419 GSList *it;
420 gboolean first = TRUE;
421 gint l, r, t, b;
422 for (it = client->parents; it; it = g_slist_next(it)) {
423 ObClient *m = it->data;
424 if (!m->iconic) {
425 if (first) {
426 l = RECT_LEFT(m->frame->area);
427 t = RECT_TOP(m->frame->area);
428 r = RECT_RIGHT(m->frame->area);
429 b = RECT_BOTTOM(m->frame->area);
430 first = FALSE;
431 } else {
432 l = MIN(l, RECT_LEFT(m->frame->area));
433 t = MIN(t, RECT_TOP(m->frame->area));
434 r = MAX(r, RECT_RIGHT(m->frame->area));
435 b = MAX(b, RECT_BOTTOM(m->frame->area));
436 }
437 }
438 if (!first) {
439 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
440 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
441 return TRUE;
442 }
443 }
444 }
445
446 if (client->type == OB_CLIENT_TYPE_DIALOG ||
447 client->type == OB_CLIENT_TYPE_SPLASH)
448 {
449 Rect **areas;
450 guint i;
451
452 areas = pick_head(client);
453
454 *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
455 *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
456
457 for (i = 0; i < screen_num_monitors; ++i)
458 g_free(areas[i]);
459 g_free(areas);
460 return TRUE;
461 }
462
463 return FALSE;
464 }
465
466 /* Return TRUE if we want client.c to enforce on-screen-keeping */
467 gboolean place_client(ObClient *client, gint *x, gint *y,
468 ObAppSettings *settings)
469 {
470 gboolean ret;
471
472 if (client->positioned)
473 return FALSE;
474
475 /* try a number of methods */
476 ret = place_transient_splash(client, x, y) ||
477 place_per_app_setting(client, x, y, settings) ||
478 (config_place_policy == OB_PLACE_POLICY_MOUSE &&
479 place_under_mouse(client, x, y)) ||
480 place_nooverlap(client, x, y) ||
481 place_under_mouse(client, x, y) ||
482 place_random(client, x, y);
483 g_assert(ret);
484
485 /* get where the client should be */
486 frame_frame_gravity(client->frame, x, y,
487 client->area.width, client->area.height);
488 return ret;
489 }
This page took 0.061081 seconds and 4 git commands to generate.