]> Dogcows Code - chaz/openbox/blob - openbox/place.c
no center in the first smart-place
[chaz/openbox] / 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 static gboolean place_random(ObClient *client, gint *x, gint *y)
43 {
44 int l, r, t, b;
45 Rect *area;
46
47 area = pick_head(client);
48 if (!area)
49 area = screen_area_monitor(client->desktop,
50 g_random_int_range(0, screen_num_monitors));
51
52 l = area->x;
53 t = area->y;
54 r = area->x + area->width - client->frame->area.width;
55 b = area->y + area->height - client->frame->area.height;
56
57 if (r > l) *x = g_random_int_range(l, r + 1);
58 else *x = 0;
59 if (b > t) *y = g_random_int_range(t, b + 1);
60 else *y = 0;
61
62 return TRUE;
63 }
64
65 static GSList* area_add(GSList *list, Rect *a)
66 {
67 Rect *r = g_new(Rect, 1);
68 *r = *a;
69 return g_slist_prepend(list, r);
70 }
71
72 static GSList* area_remove(GSList *list, Rect *a)
73 {
74 GSList *sit;
75 GSList *result = NULL;
76
77 for (sit = list; sit; sit = g_slist_next(sit)) {
78 Rect *r = sit->data;
79
80 if (!RECT_INTERSECTS_RECT(*r, *a)) {
81 result = g_slist_prepend(result, r);
82 r = NULL; /* dont free it */
83 } else {
84 Rect isect, extra;
85
86 /* Use an intersection of a and r to determine the space
87 around r that we can use.
88
89 NOTE: the spaces calculated can overlap.
90 */
91
92 RECT_SET_INTERSECTION(isect, *r, *a);
93
94 if (RECT_LEFT(isect) > RECT_LEFT(*r)) {
95 RECT_SET(extra, r->x, r->y,
96 RECT_LEFT(isect) - r->x, r->height);
97 result = area_add(result, &extra);
98 }
99
100 if (RECT_TOP(isect) > RECT_TOP(*r)) {
101 RECT_SET(extra, r->x, r->y,
102 r->width, RECT_TOP(isect) - r->y + 1);
103 result = area_add(result, &extra);
104 }
105
106 if (RECT_RIGHT(isect) < RECT_RIGHT(*r)) {
107 RECT_SET(extra, RECT_RIGHT(isect) + 1, r->y,
108 RECT_RIGHT(*r) - RECT_RIGHT(isect), r->height);
109 result = area_add(result, &extra);
110 }
111
112 if (RECT_BOTTOM(isect) < RECT_BOTTOM(*r)) {
113 RECT_SET(extra, r->x, RECT_BOTTOM(isect) + 1,
114 r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
115 result = area_add(result, &extra);
116 }
117 }
118
119 g_free(r);
120 }
121 g_slist_free(list);
122 return result;
123 }
124
125 static gint area_cmp(gconstpointer p1, gconstpointer p2)
126 {
127 const Rect *a1 = p1, *a2 = p2;
128
129 return a1->width * a1->height - a2->width * a2->height;
130 }
131
132 static gboolean place_smart(ObClient *client, gint *x, gint *y,
133 gboolean only_focused)
134 {
135 guint i;
136 gboolean ret = FALSE;
137 GSList *spaces = NULL, *sit;
138 GList *it, *list;
139
140 list = focus_order[client->desktop == DESKTOP_ALL ?
141 screen_desktop : client->desktop];
142
143 for (i = 0; i < screen_num_monitors; ++i)
144 spaces = area_add(spaces, screen_area_monitor(client->desktop, i));
145
146 for (it = list; it; it = g_list_next(it)) {
147 ObClient *c = it->data;
148
149 if (c != client && !c->shaded && client_normal(c)) {
150 spaces = area_remove(spaces, &c->frame->area);
151 if (only_focused)
152 break;
153 }
154 }
155
156 spaces = g_slist_sort(spaces, area_cmp);
157
158 for (sit = spaces; sit; sit = g_slist_next(sit)) {
159 Rect *r = sit->data;
160
161 if (!ret) {
162 if (r->width >= client->frame->area.width &&
163 r->height >= client->frame->area.height) {
164 ret = TRUE;
165 if (only_focused) {
166 *x = r->x + (r->width - client->frame->area.width) / 2;
167 *y = r->y + (r->height - client->frame->area.height) / 2;
168 } else {
169 *x = r->x;
170 *y = r->y;
171 }
172 }
173 }
174
175 g_free(r);
176 }
177 g_slist_free(spaces);
178
179 return ret;
180 }
181
182 static gboolean place_under_mouse(ObClient *client, gint *x, gint *y)
183 {
184 guint i;
185 gint l, r, t, b;
186 gint px, py;
187 Rect *area;
188
189 screen_pointer_pos(&px, &py);
190
191 for (i = 0; i < screen_num_monitors; ++i) {
192 area = screen_area_monitor(client->desktop, i);
193 if (RECT_CONTAINS(*area, px, py))
194 break;
195 }
196 if (i == screen_num_monitors)
197 area = screen_area_monitor(client->desktop, 0);
198
199 l = area->x;
200 t = area->y;
201 r = area->x + area->width - client->frame->area.width;
202 b = area->y + area->height - client->frame->area.height;
203
204 *x = px - client->area.width / 2 - client->frame->size.left;
205 *x = MIN(MAX(*x, l), r);
206 *y = py - client->area.height / 2 - client->frame->size.top;
207 *y = MIN(MAX(*y, t), b);
208
209 return TRUE;
210 }
211
212 static gboolean place_transient(ObClient *client, gint *x, gint *y)
213 {
214 if (client->transient_for) {
215 if (client->transient_for != OB_TRAN_GROUP) {
216 ObClient *c = client;
217 ObClient *p = client->transient_for;
218 *x = (p->frame->area.width - c->frame->area.width) / 2 +
219 p->frame->area.x;
220 *y = (p->frame->area.height - c->frame->area.height) / 2 +
221 p->frame->area.y;
222 return TRUE;
223 } else {
224 GSList *it;
225 gboolean first = TRUE;
226 int l, r, t, b;
227 for (it = client->group->members; it; it = it->next) {
228 ObClient *m = it->data;
229 if (!(m == client || m->transient_for)) {
230 if (first) {
231 l = RECT_LEFT(m->frame->area);
232 t = RECT_TOP(m->frame->area);
233 r = RECT_RIGHT(m->frame->area);
234 b = RECT_BOTTOM(m->frame->area);
235 first = FALSE;
236 } else {
237 l = MIN(l, RECT_LEFT(m->frame->area));
238 t = MIN(t, RECT_TOP(m->frame->area));
239 r = MAX(r, RECT_RIGHT(m->frame->area));
240 b = MAX(b, RECT_BOTTOM(m->frame->area));
241 }
242 }
243 }
244 if (!first) {
245 *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
246 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
247 return TRUE;
248 }
249 }
250 }
251 return FALSE;
252 }
253
254 static gboolean place_dialog(ObClient *client, gint *x, gint *y)
255 {
256 /* center parentless dialogs on the screen */
257 if (client->type == OB_CLIENT_TYPE_DIALOG) {
258 Rect *area;
259
260 area = pick_head(client);
261 if (!area)
262 area = screen_area_monitor(client->desktop, 0);
263
264 *x = (area->width - client->frame->area.width) / 2 + area->x;
265 *y = (area->height - client->frame->area.height) / 2 + area->y;
266 return TRUE;
267 }
268 return FALSE;
269 }
270
271 void place_client(ObClient *client, gint *x, gint *y)
272 {
273 if (client->positioned)
274 return;
275 if (place_transient(client, x, y) ||
276 place_dialog(client, x, y) ||
277 place_smart(client, x, y, FALSE) ||
278 place_smart(client, x, y, TRUE) ||
279 (config_focus_follow ?
280 place_under_mouse(client, x, y) :
281 place_random(client, x, y)))
282 {
283 /* get where the client should be */
284 frame_frame_gravity(client->frame, x, y);
285 } else
286 g_assert_not_reached(); /* the last one better succeed */
287 }
This page took 0.045976 seconds and 5 git commands to generate.