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