woot sexy
[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     int 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         a = screen_physical_area_monitor(most);
179         if (RECT_CONTAINS(*a, a1->x, a1->y))
180             return -1;
181         if (RECT_CONTAINS(*a, a2->x, a2->y))
182             return 1;
183     }
184
185     return MIN((a1->width - carea->width), (a1->height - carea->height)) -
186         MIN((a2->width - carea->width), (a2->height - carea->height));
187 }
188
189 typedef enum
190 {
191     SMART_FULL,
192     SMART_GROUP,
193     SMART_FOCUSED
194 } ObSmartType;
195
196 #define SMART_IGNORE(placer, c) \
197     (placer == c || !c->frame->visible || c->shaded || !client_normal(c) || \
198      (c->desktop != DESKTOP_ALL && \
199       c->desktop != (placer->desktop == DESKTOP_ALL ? \
200                      screen_desktop : placer->desktop)))
201
202 static gboolean place_smart(ObClient *client, gint *x, gint *y,
203                             ObSmartType type)
204 {
205     guint i;
206     gboolean ret = FALSE;
207     GSList *spaces = NULL, *sit;
208     GList *it;
209
210     for (i = 0; i < screen_num_monitors; ++i)
211         spaces = area_add(spaces, screen_area_monitor(client->desktop, i));
212
213     if (type == SMART_FULL || type == SMART_FOCUSED) {
214         gboolean found_foc = FALSE, stop = FALSE;
215         ObClient *foc;
216         GList *list;
217
218         list = focus_order[client->desktop == DESKTOP_ALL ?
219                            screen_desktop : client->desktop];
220         foc = list ? list->data : NULL;
221
222         for (it = stacking_list; it && !stop; it = g_list_next(it)) {
223             ObClient *c;
224
225             if (WINDOW_IS_CLIENT(it->data))
226                 c = it->data;
227             else
228                 continue;
229
230             if (!SMART_IGNORE(client, c)) {
231                 if (type == SMART_FOCUSED) {
232                     if (c->layer <= client->layer && found_foc)
233                         stop = TRUE;
234                 }
235                 if (!stop)
236                     spaces = area_remove(spaces, &c->frame->area);
237             }
238
239             if (c == foc)
240                 found_foc = TRUE;
241         }
242     } else if (type == SMART_GROUP) {
243         /* has to be more than me in the group */
244         if (!client_has_group_siblings(client))
245             return FALSE;
246
247         for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
248             ObClient *c = sit->data;
249             if (!SMART_IGNORE(client, c))
250                 spaces = area_remove(spaces, &c->frame->area);
251         }
252
253         /* stay out from under windows in higher layers */
254         for (it = stacking_list; it; it = g_list_next(it)) {
255             ObClient *c;
256
257             if (WINDOW_IS_CLIENT(it->data))
258                 c = it->data;
259             else
260                 continue;
261
262             if (c->layer > client->layer) {
263                 if (!SMART_IGNORE(client, c))
264                     spaces = area_remove(spaces, &c->frame->area);
265             } else
266                 break;
267         }
268     } else
269         g_assert_not_reached();
270
271     spaces = g_slist_sort_with_data(spaces, area_cmp, client);
272
273     for (sit = spaces; sit; sit = g_slist_next(sit)) {
274         Rect *r = sit->data;
275
276         if (!ret) {
277             if (r->width >= client->frame->area.width &&
278                 r->height >= client->frame->area.height) {
279                 ret = TRUE;
280                 if (type != SMART_FULL) {
281                     *x = r->x + (r->width - client->frame->area.width) / 2;
282                     *y = r->y + (r->height - client->frame->area.height) / 2;
283                 } else {
284                     *x = r->x;
285                     *y = r->y;
286                 }
287             }
288         }
289
290         g_free(r);
291     }
292     g_slist_free(spaces);
293
294     return ret;
295 }
296
297 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
298 {
299     guint i;
300     gint l, r, t, b;
301     gint px, py;
302     Rect *area;
303
304     screen_pointer_pos(&px, &py);
305
306     for (i = 0; i < screen_num_monitors; ++i) {
307         area = screen_area_monitor(client->desktop, i);
308         if (RECT_CONTAINS(*area, px, py))
309             break;
310     }
311     if (i == screen_num_monitors)
312         area = screen_area_monitor(client->desktop, 0);
313
314     l = area->x;
315     t = area->y;
316     r = area->x + area->width - client->frame->area.width;
317     b = area->y + area->height - client->frame->area.height;
318
319     *x = px - client->area.width / 2 - client->frame->size.left;
320     *x = MIN(MAX(*x, l), r);
321     *y = py - client->area.height / 2 - client->frame->size.top;
322     *y = MIN(MAX(*y, t), b);
323
324     return TRUE;
325 }
326
327 static gboolean place_transient(ObClient *client, gint *x, gint *y)
328 {
329     if (client->transient_for) {
330         if (client->transient_for != OB_TRAN_GROUP) {
331             ObClient *c = client;
332             ObClient *p = client->transient_for;
333             *x = (p->frame->area.width - c->frame->area.width) / 2 +
334                 p->frame->area.x;
335             *y = (p->frame->area.height - c->frame->area.height) / 2 +
336                 p->frame->area.y;
337             return TRUE;
338         } else {
339             GSList *it;
340             gboolean first = TRUE;
341             int l, r, t, b;
342             for (it = client->group->members; it; it = it->next) {
343                 ObClient *m = it->data;
344                 if (!(m == client || m->transient_for)) {
345                     if (first) {
346                         l = RECT_LEFT(m->frame->area);
347                         t = RECT_TOP(m->frame->area);
348                         r = RECT_RIGHT(m->frame->area);
349                         b = RECT_BOTTOM(m->frame->area);
350                         first = FALSE;
351                     } else {
352                         l = MIN(l, RECT_LEFT(m->frame->area));
353                         t = MIN(t, RECT_TOP(m->frame->area));
354                         r = MAX(r, RECT_RIGHT(m->frame->area));
355                         b = MAX(b, RECT_BOTTOM(m->frame->area));
356                     }
357                 }
358             }
359             if (!first) {
360                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
361                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
362                 return TRUE;
363             }
364         }
365     }
366     return FALSE;
367 }
368
369 static gboolean place_dialog(ObClient *client, gint *x, gint *y)
370 {
371     /* center parentless dialogs on the screen */
372     if (client->type == OB_CLIENT_TYPE_DIALOG) {
373         Rect *area;
374
375         area = pick_head(client);
376         if (!area)
377             area = screen_area_monitor(client->desktop, 0);
378
379         *x = (area->width - client->frame->area.width) / 2 + area->x;
380         *y = (area->height - client->frame->area.height) / 2 + area->y;
381         return TRUE;
382     }
383     return FALSE;
384 }
385
386 void place_client(ObClient *client, gint *x, gint *y)
387 {
388     if (client->positioned)
389         return;
390     if (place_transient(client, x, y)             ||
391         place_dialog(client, x, y)                ||
392         ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
393          place_under_mouse(client, x, y) :
394          place_smart(client, x, y, SMART_FULL)    ||
395          place_smart(client, x, y, SMART_GROUP)   ||
396          place_smart(client, x, y, SMART_FOCUSED) ||
397          place_random(client, x, y)))
398     {
399         /* get where the client should be */
400         frame_frame_gravity(client->frame, x, y);
401     } else
402         g_assert_not_reached(); /* the last one better succeed */
403 }