]>
Dogcows Code - chaz/openbox/blob - openbox/place.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 place.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
27 static Rect
*pick_head(ObClient
*c
)
33 /* try direct parent first */
34 if (c
->transient_for
&& c
->transient_for
!= OB_TRAN_GROUP
) {
35 return screen_area_monitor(c
->desktop
,
36 client_monitor(c
->transient_for
));
39 /* more than one guy in his group (more than just him) */
40 if (client_has_group_siblings(c
)) {
43 /* try on the client's desktop */
44 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
45 ObClient
*itc
= it
->data
;
47 (itc
->desktop
== c
->desktop
||
48 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
49 return screen_area_monitor(c
->desktop
,
50 client_monitor(it
->data
));
53 /* try on all desktops */
54 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
55 ObClient
*itc
= it
->data
;
57 return screen_area_monitor(c
->desktop
,
58 client_monitor(it
->data
));
62 screen_pointer_pos(&px
, &py
);
64 for (i
= 0; i
< screen_num_monitors
; i
++) {
65 area
= screen_area_monitor(c
->desktop
, i
);
66 if (RECT_CONTAINS(*area
, px
, py
))
69 if (i
== screen_num_monitors
)
70 area
= screen_area_monitor(c
->desktop
, 0);
74 area
= screen_area_monitor(c
->desktop
,
75 g_random_int_range(0, screen_num_monitors
));
80 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
85 area
= pick_head(client
);
89 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
90 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
92 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
94 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
100 static GSList
* area_add(GSList
*list
, Rect
*a
)
102 Rect
*r
= g_new(Rect
, 1);
104 return g_slist_prepend(list
, r
);
107 static GSList
* area_remove(GSList
*list
, Rect
*a
)
110 GSList
*result
= NULL
;
112 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
115 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
116 result
= g_slist_prepend(result
, r
);
117 r
= NULL
; /* dont free it */
121 /* Use an intersection of a and r to determine the space
122 around r that we can use.
124 NOTE: the spaces calculated can overlap.
127 RECT_SET_INTERSECTION(isect
, *r
, *a
);
129 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
130 RECT_SET(extra
, r
->x
, r
->y
,
131 RECT_LEFT(isect
) - r
->x
, r
->height
);
132 result
= area_add(result
, &extra
);
135 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
136 RECT_SET(extra
, r
->x
, r
->y
,
137 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
138 result
= area_add(result
, &extra
);
141 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
142 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
143 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
144 result
= area_add(result
, &extra
);
147 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
148 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
149 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
150 result
= area_add(result
, &extra
);
160 static gint
area_cmp(gconstpointer p1
, gconstpointer p2
, gpointer data
)
163 Rect
*carea
= &c
->frame
->area
;
164 const Rect
*a1
= p1
, *a2
= p2
;
165 gboolean diffhead
= FALSE
;
169 for (i
= 0; i
< screen_num_monitors
; ++i
) {
170 a
= screen_physical_area_monitor(i
);
171 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
) &&
172 !RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
179 /* has to be more than me in the group */
180 if (diffhead
&& client_has_group_siblings(c
)) {
184 /* find how many clients in the group are on each monitor, use the
185 monitor with the most in it */
186 num
= g_new0(guint
, screen_num_monitors
);
187 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
))
189 ++num
[client_monitor(it
->data
)];
191 for (i
= 1; i
< screen_num_monitors
; ++i
)
192 if (num
[i
] > num
[most
])
197 a
= screen_physical_area_monitor(most
);
198 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
))
200 if (RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
204 return MIN((a1
->width
- carea
->width
), (a1
->height
- carea
->height
)) -
205 MIN((a2
->width
- carea
->width
), (a2
->height
- carea
->height
));
215 #define SMART_IGNORE(placer, c) \
216 (placer == c || !c->frame->visible || c->shaded || !client_normal(c) || \
217 (c->desktop != DESKTOP_ALL && \
218 c->desktop != (placer->desktop == DESKTOP_ALL ? \
219 screen_desktop : placer->desktop)))
221 static gboolean
place_smart(ObClient
*client
, gint
*x
, gint
*y
,
224 gboolean ret
= FALSE
;
225 GSList
*spaces
= NULL
, *sit
;
228 spaces
= area_add(spaces
, pick_head(client
));
230 /* stay out from under windows in higher layers */
231 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
234 if (WINDOW_IS_CLIENT(it
->data
)) {
241 if (c
->layer
> client
->layer
) {
242 if (!SMART_IGNORE(client
, c
))
243 spaces
= area_remove(spaces
, &c
->frame
->area
);
248 if (client
->type
== OB_CLIENT_TYPE_NORMAL
) {
249 if (type
== SMART_FULL
|| type
== SMART_FOCUSED
) {
250 gboolean found_foc
= FALSE
, stop
= FALSE
;
254 list
= focus_order
[client
->desktop
== DESKTOP_ALL
?
255 screen_desktop
: client
->desktop
];
256 foc
= list
? list
->data
: NULL
;
258 for (; it
&& !stop
; it
= g_list_next(it
)) {
261 if (WINDOW_IS_CLIENT(it
->data
)) {
268 if (!SMART_IGNORE(client
, c
)) {
269 if (type
== SMART_FOCUSED
)
273 spaces
= area_remove(spaces
, &c
->frame
->area
);
279 } else if (type
== SMART_GROUP
) {
280 /* has to be more than me in the group */
281 if (!client_has_group_siblings(client
))
284 for (sit
= client
->group
->members
; sit
; sit
= g_slist_next(sit
)) {
285 ObClient
*c
= sit
->data
;
286 if (!SMART_IGNORE(client
, c
))
287 spaces
= area_remove(spaces
, &c
->frame
->area
);
290 g_assert_not_reached();
293 spaces
= g_slist_sort_with_data(spaces
, area_cmp
, client
);
295 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
299 if (r
->width
>= client
->frame
->area
.width
&&
300 r
->height
>= client
->frame
->area
.height
) {
302 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
305 *x
= r
->x
+ (r
->width
- client
->frame
->area
.width
) / 2;
306 *y
= r
->y
+ (r
->height
- client
->frame
->area
.height
) / 2;
316 g_slist_free(spaces
);
321 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
327 area
= pick_head(client
);
328 screen_pointer_pos(&px
, &py
);
332 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
333 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
335 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
336 *x
= MIN(MAX(*x
, l
), r
);
337 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
338 *y
= MIN(MAX(*y
, t
), b
);
343 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
344 ObAppSettings
*settings
)
348 if (!settings
|| (settings
&& !settings
->pos_given
))
351 /* Find which head the pointer is on */
352 if (settings
->head
== -1)
353 screen
= pick_head(client
);
355 screen
= screen_area_monitor(client
->desktop
, settings
->head
);
357 if (settings
->center_x
)
358 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
360 *x
= screen
->x
+ settings
->position
.x
;
362 if (settings
->center_y
)
363 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
365 *y
= screen
->y
+ settings
->position
.y
;
370 static gboolean
place_transient(ObClient
*client
, gint
*x
, gint
*y
)
372 if (client
->transient_for
) {
373 if (client
->transient_for
!= OB_TRAN_GROUP
) {
374 ObClient
*c
= client
;
375 ObClient
*p
= client
->transient_for
;
376 *x
= (p
->frame
->area
.width
- c
->frame
->area
.width
) / 2 +
378 *y
= (p
->frame
->area
.height
- c
->frame
->area
.height
) / 2 +
383 gboolean first
= TRUE
;
385 for (it
= client
->group
->members
; it
; it
= g_slist_next(it
)) {
386 ObClient
*m
= it
->data
;
387 if (!(m
== client
|| m
->transient_for
)) {
389 l
= RECT_LEFT(m
->frame
->area
);
390 t
= RECT_TOP(m
->frame
->area
);
391 r
= RECT_RIGHT(m
->frame
->area
);
392 b
= RECT_BOTTOM(m
->frame
->area
);
395 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
396 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
397 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
398 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
403 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
404 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
412 /* Return TRUE if we want client.c to enforce on-screen-keeping */
413 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
414 ObAppSettings
*settings
)
416 gboolean ret
= FALSE
;
417 if (client
->positioned
)
419 if (place_transient(client
, x
, y
))
422 place_per_app_setting(client
, x
, y
, settings
) ||
423 ((config_place_policy
== OB_PLACE_POLICY_MOUSE
) ?
424 place_under_mouse(client
, x
, y
) :
425 place_smart(client
, x
, y
, SMART_FULL
) ||
426 place_smart(client
, x
, y
, SMART_GROUP
) ||
427 place_smart(client
, x
, y
, SMART_FOCUSED
) ||
428 place_random(client
, x
, y
))))
429 g_assert_not_reached(); /* the last one better succeed */
430 /* get where the client should be */
431 frame_frame_gravity(client
->frame
, x
, y
);
This page took 0.05167 seconds and 4 git commands to generate.