]> Dogcows Code - chaz/openbox/blob - openbox/place.c
.. i am pretending to be mika :D :D ok this time for real
[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 Ben 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
27 static void add_choice(guint *choice, guint mychoice)
28 {
29 guint i;
30 for (i = 0; i < screen_num_monitors; ++i) {
31 if (choice[i] == mychoice)
32 return;
33 else if (choice[i] == screen_num_monitors) {
34 choice[i] = mychoice;
35 return;
36 }
37 }
38 }
39
40 static Rect *pick_pointer_head(ObClient *c)
41 {
42 guint i;
43 gint px, py;
44
45 screen_pointer_pos(&px, &py);
46
47 for (i = 0; i < screen_num_monitors; ++i) {
48 if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
49 return screen_area_monitor(c->desktop, i);
50 }
51 }
52 g_assert_not_reached();
53 }
54
55 static Rect **pick_head(ObClient *c)
56 {
57 Rect **area;
58 guint *choice;
59 guint i;
60 gint px, py;
61
62 area = g_new(Rect*, screen_num_monitors);
63 choice = g_new(guint, screen_num_monitors);
64 for (i = 0; i < screen_num_monitors; ++i)
65 choice[i] = screen_num_monitors; /* make them all invalid to start */
66
67 /* try direct parent first */
68 if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
69 add_choice(choice, client_monitor(c->transient_for));
70 }
71
72 /* more than one window in its group (more than just this window) */
73 if (client_has_group_siblings(c)) {
74 GSList *it;
75
76 /* try on the client's desktop */
77 for (it = c->group->members; it; it = g_slist_next(it)) {
78 ObClient *itc = it->data;
79 if (itc != c &&
80 (itc->desktop == c->desktop ||
81 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
82 add_choice(choice, client_monitor(it->data));
83 }
84
85 /* try on all desktops */
86 for (it = c->group->members; it; it = g_slist_next(it)) {
87 ObClient *itc = it->data;
88 if (itc != c)
89 add_choice(choice, client_monitor(it->data));
90 }
91 }
92
93 if (focus_client)
94 add_choice(choice, client_monitor(focus_client));
95
96 screen_pointer_pos(&px, &py);
97
98 for (i = 0; i < screen_num_monitors; i++)
99 if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
100 add_choice(choice, i);
101 break;
102 }
103
104 /* add any leftover choices */
105 for (i = 0; i < screen_num_monitors; ++i)
106 add_choice(choice, i);
107
108 for (i = 0; i < screen_num_monitors; ++i)
109 area[i] = screen_area_monitor(c->desktop, choice[i]);
110
111 return area;
112 }
113
114 static gboolean place_random(ObClient *client, gint *x, gint *y)
115 {
116 gint l, r, t, b;
117 Rect **areas;
118 guint i;
119
120 areas = pick_head(client);
121 i = g_random_int_range(0, screen_num_monitors);
122
123 l = areas[i]->x;
124 t = areas[i]->y;
125 r = areas[i]->x + areas[i]->width - client->frame->area.width;
126 b = areas[i]->y + areas[i]->height - client->frame->area.height;
127
128 if (r > l) *x = g_random_int_range(l, r + 1);
129 else *x = 0;
130 if (b > t) *y = g_random_int_range(t, b + 1);
131 else *y = 0;
132
133 g_free(areas);
134
135 return TRUE;
136 }
137
138 static GSList* area_add(GSList *list, Rect *a)
139 {
140 Rect *r = g_new(Rect, 1);
141 *r = *a;
142 return g_slist_prepend(list, r);
143 }
144
145 static GSList* area_remove(GSList *list, Rect *a)
146 {
147 GSList *sit;
148 GSList *result = NULL;
149
150 for (sit = list; sit; sit = g_slist_next(sit)) {
151 Rect *r = sit->data;
152
153 if (!RECT_INTERSECTS_RECT(*r, *a)) {
154 result = g_slist_prepend(result, r);
155 r = NULL; /* dont free it */
156 } else {
157 Rect isect, extra;
158
159 /* Use an intersection of a and r to determine the space
160 around r that we can use.
161
162 NOTE: the spaces calculated can overlap.
163 */
164
165 RECT_SET_INTERSECTION(isect, *r, *a);
166
167 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
168 RECT_SET(extra, r->x, r->y,
169 RECT_LEFT(isect) - r->x, r->height);
170 result = area_add(result, &extra);
171 }
172
173 if (RECT_TOP(isect) > RECT_TOP(*r)) {
174 RECT_SET(extra, r->x, r->y,
175 r->width, RECT_TOP(isect) - r->y + 1);
176 result = area_add(result, &extra);
177 }
178
179 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
180 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
181 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
182 result = area_add(result, &extra);
183 }
184
185 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
186 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
187 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
188 result = area_add(result, &extra);
189 }
190 }
191
192 g_free(r);
193 }
194 g_slist_free(list);
195 return result;
196 }
197
198 static gint area_cmp(gconstpointer p1, gconstpointer p2, gpointer data)
199 {
200 ObClient *c = data;
201 Rect *carea = &c->frame->area;
202 const Rect *a1 = p1, *a2 = p2;
203 gboolean diffhead = FALSE;
204 guint i;
205 Rect *a;
206
207 for (i = 0; i < screen_num_monitors; ++i) {
208 a = screen_physical_area_monitor(i);
209 if (RECT_CONTAINS(*a, a1->x, a1->y) &&
210 !RECT_CONTAINS(*a, a2->x, a2->y))
211 {
212 diffhead = TRUE;
213 break;
214 }
215 }
216
217 /* has to be more than me in the group */
218 if (diffhead && client_has_group_siblings(c)) {
219 guint *num, most;
220 GSList *it;
221
222 /* find how many clients in the group are on each monitor, use the
223 monitor with the most in it */
224 num = g_new0(guint, screen_num_monitors);
225 for (it = c->group->members; it; it = g_slist_next(it))
226 if (it->data != c)
227 ++num[client_monitor(it->data)];
228 most = 0;
229 for (i = 1; i < screen_num_monitors; ++i)
230 if (num[i] > num[most])
231 most = i;
232
233 g_free(num);
234
235 a = screen_physical_area_monitor(most);
236 if (RECT_CONTAINS(*a, a1->x, a1->y))
237 return -1;
238 if (RECT_CONTAINS(*a, a2->x, a2->y))
239 return 1;
240 }
241
242 return MIN((a1->width - carea->width), (a1->height - carea->height)) -
243 MIN((a2->width - carea->width), (a2->height - carea->height));
244 }
245
246 typedef enum
247 {
248 SMART_FULL,
249 SMART_GROUP,
250 SMART_FOCUSED
251 } ObSmartType;
252
253 #define SMART_IGNORE(placer, c) \
254 (placer == c || !c->frame->visible || c->shaded || !client_normal(c) || \
255 (c->desktop != DESKTOP_ALL && \
256 c->desktop != (placer->desktop == DESKTOP_ALL ? \
257 screen_desktop : placer->desktop)))
258
259 static gboolean place_smart(ObClient *client, gint *x, gint *y,
260 ObSmartType type)
261 {
262 gboolean ret = FALSE;
263 GSList *spaces = NULL, *sit;
264 GList *it;
265 Rect **areas;
266 guint i;
267
268 if (type == SMART_GROUP) {
269 /* has to be more than me in the group */
270 if (!client_has_group_siblings(client))
271 return FALSE;
272 }
273
274 areas = pick_head(client);
275
276 for (i = 0; i < screen_num_monitors; ++i) {
277 spaces = area_add(spaces, areas[i]);
278
279 /* stay out from under windows in higher layers */
280 for (it = stacking_list; it; it = g_list_next(it)) {
281 ObClient *c;
282
283 if (WINDOW_IS_CLIENT(it->data)) {
284 c = it->data;
285 if (c->fullscreen)
286 continue;
287 } else
288 continue;
289
290 if (c->layer > client->layer) {
291 if (!SMART_IGNORE(client, c))
292 spaces = area_remove(spaces, &c->frame->area);
293 } else
294 break;
295 }
296
297 if (type == SMART_FULL || type == SMART_FOCUSED) {
298 gboolean found_foc = FALSE, stop = FALSE;
299 ObClient *foc;
300 GList *list;
301
302 list = focus_order[client->desktop == DESKTOP_ALL ?
303 screen_desktop : client->desktop];
304 foc = list ? list->data : NULL;
305
306 for (; it && !stop; it = g_list_next(it)) {
307 ObClient *c;
308
309 if (WINDOW_IS_CLIENT(it->data)) {
310 c = it->data;
311 if (c->fullscreen)
312 continue;
313 } else
314 continue;
315
316 if (!SMART_IGNORE(client, c)) {
317 if (type == SMART_FOCUSED)
318 if (found_foc)
319 stop = TRUE;
320 if (!stop)
321 spaces = area_remove(spaces, &c->frame->area);
322 }
323
324 if (c == foc)
325 found_foc = TRUE;
326 }
327 } else if (type == SMART_GROUP) {
328 for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
329 ObClient *c = sit->data;
330 if (!SMART_IGNORE(client, c))
331 spaces = area_remove(spaces, &c->frame->area);
332 }
333 } else
334 g_assert_not_reached();
335
336 spaces = g_slist_sort_with_data(spaces, area_cmp, client);
337
338 for (sit = spaces; sit; sit = g_slist_next(sit)) {
339 Rect *r = sit->data;
340
341 if (!ret) {
342 if (r->width >= client->frame->area.width &&
343 r->height >= client->frame->area.height) {
344 ret = TRUE;
345 if (client->type == OB_CLIENT_TYPE_DIALOG ||
346 type != SMART_FULL)
347 {
348 *x = r->x + (r->width - client->frame->area.width)/2;
349 *y = r->y + (r->height - client->frame->area.height)/2;
350 } else {
351 *x = r->x;
352 *y = r->y;
353 }
354 }
355 }
356
357 g_free(r);
358 }
359 g_slist_free(spaces);
360 spaces = NULL;
361 }
362
363 g_free(areas);
364
365 return ret;
366 }
367
368 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
369 {
370 gint l, r, t, b;
371 gint px, py;
372 Rect *area;
373
374 area = pick_pointer_head(client);
375
376 l = area->x;
377 t = area->y;
378 r = area->x + area->width - client->frame->area.width;
379 b = area->y + area->height - client->frame->area.height;
380
381 *x = px - client->area.width / 2 - client->frame->size.left;
382 *x = MIN(MAX(*x, l), r);
383 *y = py - client->area.height / 2 - client->frame->size.top;
384 *y = MIN(MAX(*y, t), b);
385
386 return TRUE;
387 }
388
389 static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
390 ObAppSettings *settings)
391 {
392 Rect *screen;
393
394 if (!settings || (settings && !settings->pos_given))
395 return FALSE;
396
397 /* Find which head the pointer is on */
398 if (settings->head == -1)
399 screen = pick_pointer_head(client);
400 else
401 screen = screen_area_monitor(client->desktop, settings->head);
402
403 if (settings->center_x)
404 *x = screen->x + screen->width / 2 - client->area.width / 2;
405 else
406 *x = screen->x + settings->position.x;
407
408 if (settings->center_y)
409 *y = screen->y + screen->height / 2 - client->area.height / 2;
410 else
411 *y = screen->y + settings->position.y;
412
413 return TRUE;
414 }
415
416 static gboolean place_transient(ObClient *client, gint *x, gint *y)
417 {
418 if (client->transient_for) {
419 if (client->transient_for != OB_TRAN_GROUP) {
420 ObClient *c = client;
421 ObClient *p = client->transient_for;
422 *x = (p->frame->area.width - c->frame->area.width) / 2 +
423 p->frame->area.x;
424 *y = (p->frame->area.height - c->frame->area.height) / 2 +
425 p->frame->area.y;
426 return TRUE;
427 } else {
428 GSList *it;
429 gboolean first = TRUE;
430 gint l, r, t, b;
431 for (it = client->group->members; it; it = g_slist_next(it)) {
432 ObClient *m = it->data;
433 if (!(m == client || m->transient_for)) {
434 if (first) {
435 l = RECT_LEFT(m->frame->area);
436 t = RECT_TOP(m->frame->area);
437 r = RECT_RIGHT(m->frame->area);
438 b = RECT_BOTTOM(m->frame->area);
439 first = FALSE;
440 } else {
441 l = MIN(l, RECT_LEFT(m->frame->area));
442 t = MIN(t, RECT_TOP(m->frame->area));
443 r = MAX(r, RECT_RIGHT(m->frame->area));
444 b = MAX(b, RECT_BOTTOM(m->frame->area));
445 }
446 }
447 }
448 if (!first) {
449 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
450 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
451 return TRUE;
452 }
453 }
454 }
455 return FALSE;
456 }
457
458 /* Return TRUE if we want client.c to enforce on-screen-keeping */
459 gboolean place_client(ObClient *client, gint *x, gint *y,
460 ObAppSettings *settings)
461 {
462 gboolean ret = FALSE;
463 if (client->positioned)
464 return FALSE;
465 if (place_transient(client, x, y))
466 ret = TRUE;
467 else if (!(
468 place_per_app_setting(client, x, y, settings) ||
469 ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
470 place_under_mouse(client, x, y) :
471 place_smart(client, x, y, SMART_FULL) ||
472 place_smart(client, x, y, SMART_GROUP) ||
473 place_smart(client, x, y, SMART_FOCUSED) ||
474 place_random(client, x, y))))
475 g_assert_not_reached(); /* the last one better succeed */
476 /* get where the client should be */
477 frame_frame_gravity(client->frame, x, y);
478 return ret;
479 }
This page took 0.054369 seconds and 4 git commands to generate.