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