]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/place.c
always use smart placement, add under-mouse placement for the fallback
[mikachu/openbox.git] / openbox / place.c
1 #include "client.h"
2 #include "group.h"
3 #include "screen.h"
4 #include "frame.h"
5 #include "focus.h"
6 #include "config.h"
7
8 static Rect* pick_head(ObClient *c)
9 {
10     /* try direct parent first */
11     if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
12         return screen_area_monitor(c->desktop,
13                                    client_monitor(c->transient_for));
14     }
15
16     /* more than one guy in his group (more than just him) */
17     if (c->group && c->group->members->next) {
18         GSList *it;
19
20         /* try on the client's desktop */
21         for (it = c->group->members; it; it = g_slist_next(it)) {
22             ObClient *itc = it->data;            
23             if (itc != c &&
24                 (itc->desktop == c->desktop ||
25                  itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
26                 return screen_area_monitor(c->desktop,
27                                            client_monitor(it->data));
28         }
29
30         /* try on all desktops */
31         for (it = c->group->members; it; it = g_slist_next(it)) {
32             ObClient *itc = it->data;            
33             if (itc != c)
34                 return screen_area_monitor(c->desktop,
35                                            client_monitor(it->data));
36         }
37     }
38
39     return NULL;
40 }
41
42 #if 0
43 static gboolean place_random(ObClient *client, gint *x, gint *y)
44 {
45     int l, r, t, b;
46     Rect *area;
47
48     area = pick_head(client);
49     if (!area)
50         area = screen_area_monitor(client->desktop,
51                                    g_random_int_range(0, screen_num_monitors));
52
53     l = area->x;
54     t = area->y;
55     r = area->x + area->width - client->frame->area.width;
56     b = area->y + area->height - client->frame->area.height;
57
58     if (r > l) *x = g_random_int_range(l, r + 1);
59     else       *x = 0;
60     if (b > t) *y = g_random_int_range(t, b + 1);
61     else       *y = 0;
62
63     /* get where the client should be */
64     frame_frame_gravity(client->frame, x, y);
65
66     return TRUE;
67 }
68 #endif
69
70 static GSList* area_add(GSList *list, Rect *a)
71 {
72     Rect *r = g_new(Rect, 1);
73     *r = *a;
74     return g_slist_prepend(list, r);
75 }
76
77
78 static GSList* area_remove(GSList *list, Rect *a)
79 {
80     GSList *sit;
81     GSList *result = NULL;
82
83     for (sit = list; sit; sit = g_slist_next(sit)) {
84         Rect *r = sit->data;
85
86         if (!RECT_INTERSECTS_RECT(*r, *a)) {
87             result = g_slist_prepend(result, r);
88             r = NULL; /* dont free it */
89         } else {
90             Rect isect, extra;
91
92             /* Use an intersection of win and curr to determine the space
93                around curr that we can use.
94
95                NOTE: the spaces calculated can overlap.
96             */
97
98             RECT_SET_INTERSECTION(isect, *r, *a);
99
100             if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
101                 RECT_SET(extra, r->x, r->y,
102                          RECT_LEFT(isect) - r->x, r->height);
103                 result = area_add(result, &extra);
104             }
105
106             if (RECT_TOP(isect) > RECT_TOP(*r)) {
107                 RECT_SET(extra, r->x, r->y,
108                          r->width, RECT_TOP(isect) - r->y + 1);
109                 result = area_add(result, &extra);
110             }
111
112             if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
113                 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
114                          RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
115                 result = area_add(result, &extra);
116             }
117
118             if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
119                 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
120                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
121                 result = area_add(result, &extra);
122             }
123         }
124
125         g_free(r);
126     }
127     g_slist_free(list);
128     return result;
129 }
130
131 static gint area_cmp(gconstpointer p1, gconstpointer p2)
132 {
133     gint ret;
134     const Rect *a1 = p1, *a2 = p2;
135
136     ret = RECT_BOTTOM(*a1) - RECT_BOTTOM(*a2);
137     if (!ret)
138         ret = RECT_LEFT(*a1) - RECT_LEFT(*a2);
139     return ret;
140 }
141
142 static gboolean place_smart(ObClient *client, gint *x, gint *y)
143 {
144     guint i;
145     gboolean ret = FALSE;
146     GSList *spaces, *sit;
147     GList *it, *list;
148
149     list = focus_order[client->desktop == DESKTOP_ALL ?
150                        screen_desktop : client->desktop];
151
152     for (i = 0; i < screen_num_monitors; ++i)
153         spaces = area_add(spaces, screen_area_monitor(client->desktop, i));
154
155     for (it = list; it; it = g_list_next(it)) {
156         ObClient *c = it->data;
157
158         if (c == client || c->shaded || !client_normal(c))
159             continue;
160         spaces = area_remove(spaces, &c->frame->area);
161     }
162
163     spaces = g_slist_sort(spaces, area_cmp);
164
165     for (sit = spaces; sit; sit = g_slist_next(sit)) {
166         Rect *r = sit->data;
167
168         if (!ret) {
169             if (r->width >= client->frame->area.width &&
170                 r->height >= client->frame->area.height) {
171                 ret = TRUE;
172                 *x = r->x;
173                 *y = r->y;
174             }
175         }
176
177         g_free(r);
178     }
179     g_slist_free(spaces);
180
181     return ret;
182 }
183
184 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
185 {
186     int px, py;
187
188     screen_pointer_pos(&px, &py);
189
190     *x = px - client->area.width / 2 - client->frame->size.left;
191     *y = py - client->area.height / 2 - client->frame->size.top;
192
193     return TRUE;
194 }
195
196 static gboolean place_transient(ObClient *client, gint *x, gint *y)
197 {
198     if (client->transient_for) {
199         if (client->transient_for != OB_TRAN_GROUP) {
200             ObClient *c = client;
201             ObClient *p = client->transient_for;
202             *x = (p->frame->area.width - c->frame->area.width) / 2 +
203                 p->frame->area.x;
204             *y = (p->frame->area.height - c->frame->area.height) / 2 +
205                 p->frame->area.y;
206             return TRUE;
207         } else {
208             GSList *it;
209             gboolean first = TRUE;
210             int l, r, t, b;
211             for (it = client->group->members; it; it = it->next) {
212                 ObClient *m = it->data;
213                 if (!(m == client || m->transient_for)) {
214                     if (first) {
215                         l = RECT_LEFT(m->frame->area);
216                         t = RECT_TOP(m->frame->area);
217                         r = RECT_RIGHT(m->frame->area);
218                         b = RECT_BOTTOM(m->frame->area);
219                         first = FALSE;
220                     } else {
221                         l = MIN(l, RECT_LEFT(m->frame->area));
222                         t = MIN(t, RECT_TOP(m->frame->area));
223                         r = MAX(r, RECT_RIGHT(m->frame->area));
224                         b = MAX(b, RECT_BOTTOM(m->frame->area));
225                     }
226                 }
227             }
228             if (!first) {
229                 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
230                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
231                 return TRUE;
232             }
233         }
234     }
235     return FALSE;
236 }
237
238 static gboolean place_dialog(ObClient *client, gint *x, gint *y)
239 {
240     /* center parentless dialogs on the screen */
241     if (client->type == OB_CLIENT_TYPE_DIALOG) {
242         Rect *area;
243
244         area = pick_head(client);
245         if (!area)
246             area = screen_area_monitor(client->desktop, 0);
247
248         *x = (area->width - client->frame->area.width) / 2 + area->x;
249         *y = (area->height - client->frame->area.height) / 2 + area->y;
250         return TRUE;
251     }
252     return FALSE;
253 }
254
255 void place_client(ObClient *client, gint *x, gint *y)
256 {
257     if (client->positioned)
258         return;
259     if (place_transient(client, x, y))
260         return;
261     if (place_dialog(client, x, y))
262         return;
263     if (place_smart(client, x, y))
264         return;
265     if (place_under_mouse(client, x, y))
266         return;
267     g_assert_not_reached(); /* the last one better succeed */
268 }