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