]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/place.c
lots of fixes for the iconify animation. i think it should all work perfectly now ?
[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) 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
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 = areas[i]->x;
130     if (b > t) *y = g_random_int_range(t, b + 1);
131     else       *y = areas[i]->y;
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->shaded || !client_normal(c) || \
255      !frame_visible(c->frame) || \
256      (c->desktop != DESKTOP_ALL && \
257       c->desktop != (placer->desktop == DESKTOP_ALL ? \
258                      screen_desktop : placer->desktop)))
259
260 static gboolean place_smart(ObClient *client, gint *x, gint *y,
261                             ObSmartType type)
262 {
263     gboolean ret = FALSE;
264     GSList *spaces = NULL, *sit;
265     GList *it;
266     Rect **areas;
267     guint i;
268
269     if (type == SMART_GROUP) {
270         /* has to be more than me in the group */
271         if (!client_has_group_siblings(client))
272             return FALSE;
273     }
274
275     areas = pick_head(client);
276
277     for (i = 0; i < screen_num_monitors; ++i) {
278         spaces = area_add(spaces, areas[i]);
279
280         /* stay out from under windows in higher layers */
281         for (it = stacking_list; it; it = g_list_next(it)) {
282             ObClient *c;
283
284             if (WINDOW_IS_CLIENT(it->data)) {
285                 c = it->data;
286                 if (c->fullscreen || (c->max_vert && c->max_horz))
287                     continue;
288             } else
289                 continue;
290
291             if (c->layer > client->layer) {
292                 if (!SMART_IGNORE(client, c))
293                     spaces = area_remove(spaces, &c->frame->area);
294             } else
295                 break;
296         }
297
298         if (type == SMART_FULL || type == SMART_FOCUSED) {
299             gboolean found_foc = FALSE, stop = FALSE;
300             ObClient *foc;
301
302             foc = focus_order_find_first(client->desktop == DESKTOP_ALL ?
303                                          screen_desktop : client->desktop);
304
305             for (; it && !stop; it = g_list_next(it)) {
306                 ObClient *c;
307
308                 if (WINDOW_IS_CLIENT(it->data)) {
309                     c = it->data;
310                     if (c->fullscreen || (c->max_vert && c->max_horz))
311                         continue;
312                 } else
313                     continue;
314
315                 if (!SMART_IGNORE(client, c)) {
316                     if (type == SMART_FOCUSED)
317                         if (found_foc)
318                             stop = TRUE;
319                     if (!stop)
320                         spaces = area_remove(spaces, &c->frame->area);
321                 }
322
323                 if (c == foc)
324                     found_foc = TRUE;
325             }
326         } else if (type == SMART_GROUP) {
327             for (sit = client->group->members; sit; sit = g_slist_next(sit)) {
328                 ObClient *c = sit->data;
329                 if (!SMART_IGNORE(client, c))
330                     spaces = area_remove(spaces, &c->frame->area);
331             }
332         } else
333             g_assert_not_reached();
334
335         spaces = g_slist_sort_with_data(spaces, area_cmp, client);
336
337         for (sit = spaces; sit; sit = g_slist_next(sit)) {
338             Rect *r = sit->data;
339
340             if (!ret) {
341                 if (r->width >= client->frame->area.width &&
342                     r->height >= client->frame->area.height) {
343                     ret = TRUE;
344                     if (client->type == OB_CLIENT_TYPE_DIALOG ||
345                         type != SMART_FULL)
346                     {
347                         *x = r->x + (r->width - client->frame->area.width)/2;
348                         *y = r->y + (r->height - client->frame->area.height)/2;
349                     } else {
350                         *x = r->x;
351                         *y = r->y;
352                     }
353                 }
354             }
355
356             g_free(r);
357         }
358         g_slist_free(spaces);
359         spaces = NULL;
360     }
361
362     g_free(areas);
363
364     return ret;
365 }
366
367 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
368 {
369     gint l, r, t, b;
370     gint px, py;
371     Rect *area;
372
373     area = pick_pointer_head(client);
374     screen_pointer_pos(&px, &py);
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
423             if (client_normal(p)) {
424                 *x = (p->frame->area.width - c->frame->area.width) / 2 +
425                     p->frame->area.x;
426                 *y = (p->frame->area.height - c->frame->area.height) / 2 +
427                     p->frame->area.y;
428                 return TRUE;
429             }
430         } else {
431             GSList *it;
432             gboolean first = TRUE;
433             gint l, r, t, b;
434             for (it = client->group->members; it; it = g_slist_next(it)) {
435                 ObClient *m = it->data;
436                 if (!(m == client || m->transient_for) && client_normal(m)) {
437                     if (first) {
438                         l = RECT_LEFT(m->frame->area);
439                         t = RECT_TOP(m->frame->area);
440                         r = RECT_RIGHT(m->frame->area);
441                         b = RECT_BOTTOM(m->frame->area);
442                         first = FALSE;
443                     } else {
444                         l = MIN(l, RECT_LEFT(m->frame->area));
445                         t = MIN(t, RECT_TOP(m->frame->area));
446                         r = MAX(r, RECT_RIGHT(m->frame->area));
447                         b = MAX(b, RECT_BOTTOM(m->frame->area));
448                     }
449                 }
450             }
451             if (!first) {
452                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
453                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
454                 return TRUE;
455             }
456         }
457     }
458
459     if (client->transient) {
460         Rect **areas;
461
462         areas = pick_head(client);
463
464         *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
465         *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
466
467         g_free(areas);
468         return TRUE;
469     }
470
471     return FALSE;
472 }
473
474 /* Return TRUE if we want client.c to enforce on-screen-keeping */
475 gboolean place_client(ObClient *client, gint *x, gint *y,
476                       ObAppSettings *settings)
477 {
478     gboolean ret = FALSE;
479     if (client->positioned)
480         return FALSE;
481     if (place_transient(client, x, y))
482         ret = TRUE;
483     else if (!(
484         place_per_app_setting(client, x, y, settings) ||
485         ((config_place_policy == OB_PLACE_POLICY_MOUSE) ?
486          place_under_mouse(client, x, y) :
487          place_smart(client, x, y, SMART_FULL)    ||
488          place_smart(client, x, y, SMART_GROUP)   ||
489          place_smart(client, x, y, SMART_FOCUSED) ||
490          place_random(client, x, y))))
491         g_assert_not_reached(); /* the last one better succeed */
492     /* get where the client should be */
493     frame_frame_gravity(client->frame, x, y,
494                         client->area.width, client->area.height);
495     return ret;
496 }