]>
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-2007 Dana 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 void add_choice(guint
*choice
, guint mychoice
)
30 for (i
= 0; i
< screen_num_monitors
; ++i
) {
31 if (choice
[i
] == mychoice
)
33 else if (choice
[i
] == screen_num_monitors
) {
40 static Rect
*pick_pointer_head(ObClient
*c
)
45 screen_pointer_pos(&px
, &py
);
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
);
52 g_assert_not_reached();
55 /*! Pick a monitor to place a window on.
56 The returned array value should be freed with g_free. The areas within the
57 array should not be freed. */
58 static Rect
**pick_head(ObClient
*c
)
65 area
= g_new(Rect
*, screen_num_monitors
);
66 choice
= g_new(guint
, screen_num_monitors
);
67 for (i
= 0; i
< screen_num_monitors
; ++i
)
68 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
70 /* try direct parent first */
71 if (c
->transient_for
&& c
->transient_for
!= OB_TRAN_GROUP
) {
72 add_choice(choice
, client_monitor(c
->transient_for
));
75 /* more than one window in its group (more than just this window) */
76 if (client_has_group_siblings(c
)) {
79 /* try on the client's desktop */
80 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
81 ObClient
*itc
= it
->data
;
83 (itc
->desktop
== c
->desktop
||
84 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
85 add_choice(choice
, client_monitor(it
->data
));
88 /* try on all desktops */
89 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
90 ObClient
*itc
= it
->data
;
92 add_choice(choice
, client_monitor(it
->data
));
97 add_choice(choice
, client_monitor(focus_client
));
99 screen_pointer_pos(&px
, &py
);
101 for (i
= 0; i
< screen_num_monitors
; i
++)
102 if (RECT_CONTAINS(*screen_physical_area_monitor(i
), px
, py
)) {
103 add_choice(choice
, i
);
107 /* add any leftover choices */
108 for (i
= 0; i
< screen_num_monitors
; ++i
)
109 add_choice(choice
, i
);
111 for (i
= 0; i
< screen_num_monitors
; ++i
)
112 area
[i
] = screen_area_monitor(c
->desktop
, choice
[i
]);
117 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
123 areas
= pick_head(client
);
124 i
= g_random_int_range(0, screen_num_monitors
);
128 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
129 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
131 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
132 else *x
= areas
[i
]->x
;
133 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
134 else *y
= areas
[i
]->y
;
141 static GSList
* area_add(GSList
*list
, Rect
*a
)
143 Rect
*r
= g_new(Rect
, 1);
145 return g_slist_prepend(list
, r
);
148 static GSList
* area_remove(GSList
*list
, Rect
*a
)
151 GSList
*result
= NULL
;
153 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
156 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
157 result
= g_slist_prepend(result
, r
);
158 r
= NULL
; /* dont free it */
162 /* Use an intersection of a and r to determine the space
163 around r that we can use.
165 NOTE: the spaces calculated can overlap.
168 RECT_SET_INTERSECTION(isect
, *r
, *a
);
170 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
171 RECT_SET(extra
, r
->x
, r
->y
,
172 RECT_LEFT(isect
) - r
->x
, r
->height
);
173 result
= area_add(result
, &extra
);
176 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
177 RECT_SET(extra
, r
->x
, r
->y
,
178 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
179 result
= area_add(result
, &extra
);
182 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
183 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
184 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
185 result
= area_add(result
, &extra
);
188 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
189 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
190 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
191 result
= area_add(result
, &extra
);
201 static gint
area_cmp(gconstpointer p1
, gconstpointer p2
, gpointer data
)
204 Rect
*carea
= &c
->frame
->area
;
205 const Rect
*a1
= p1
, *a2
= p2
;
206 gboolean diffhead
= FALSE
;
210 for (i
= 0; i
< screen_num_monitors
; ++i
) {
211 a
= screen_physical_area_monitor(i
);
212 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
) &&
213 !RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
220 /* has to be more than me in the group */
221 if (diffhead
&& client_has_group_siblings(c
)) {
225 /* find how many clients in the group are on each monitor, use the
226 monitor with the most in it */
227 num
= g_new0(guint
, screen_num_monitors
);
228 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
))
230 ++num
[client_monitor(it
->data
)];
232 for (i
= 1; i
< screen_num_monitors
; ++i
)
233 if (num
[i
] > num
[most
])
238 a
= screen_physical_area_monitor(most
);
239 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
))
241 if (RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
245 return MIN((a1
->width
- carea
->width
), (a1
->height
- carea
->height
)) -
246 MIN((a2
->width
- carea
->width
), (a2
->height
- carea
->height
));
256 #define SMART_IGNORE(placer, c) \
257 (placer == c || c->shaded || !client_normal(c) || !c->frame->visible || \
258 (c->desktop != DESKTOP_ALL && \
259 c->desktop != (placer->desktop == DESKTOP_ALL ? \
260 screen_desktop : placer->desktop)))
262 static gboolean
place_smart(ObClient
*client
, gint
*x
, gint
*y
,
265 gboolean ret
= FALSE
;
266 GSList
*spaces
= NULL
, *sit
;
271 if (type
== SMART_GROUP
) {
272 /* has to be more than me in the group */
273 if (!client_has_group_siblings(client
))
277 areas
= pick_head(client
);
279 for (i
= 0; i
< screen_num_monitors
; ++i
) {
280 spaces
= area_add(spaces
, areas
[i
]);
282 /* stay out from under windows in higher layers */
283 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
286 if (WINDOW_IS_CLIENT(it
->data
)) {
288 if (c
->fullscreen
|| (c
->max_vert
&& c
->max_horz
))
293 if (c
->layer
> client
->layer
) {
294 if (!SMART_IGNORE(client
, c
))
295 spaces
= area_remove(spaces
, &c
->frame
->area
);
300 if (type
== SMART_FULL
|| type
== SMART_FOCUSED
) {
301 gboolean found_foc
= FALSE
, stop
= FALSE
;
304 foc
= focus_order_find_first(client
->desktop
== DESKTOP_ALL
?
305 screen_desktop
: client
->desktop
);
307 for (; it
&& !stop
; it
= g_list_next(it
)) {
310 if (WINDOW_IS_CLIENT(it
->data
)) {
312 if (c
->fullscreen
|| (c
->max_vert
&& c
->max_horz
))
317 if (!SMART_IGNORE(client
, c
)) {
318 if (type
== SMART_FOCUSED
)
322 spaces
= area_remove(spaces
, &c
->frame
->area
);
328 } else if (type
== SMART_GROUP
) {
329 for (sit
= client
->group
->members
; sit
; sit
= g_slist_next(sit
)) {
330 ObClient
*c
= sit
->data
;
331 if (!SMART_IGNORE(client
, c
))
332 spaces
= area_remove(spaces
, &c
->frame
->area
);
335 g_assert_not_reached();
337 spaces
= g_slist_sort_with_data(spaces
, area_cmp
, client
);
339 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
343 if (r
->width
>= client
->frame
->area
.width
&&
344 r
->height
>= client
->frame
->area
.height
) {
346 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
349 *x
= r
->x
+ (r
->width
- client
->frame
->area
.width
)/2;
350 *y
= r
->y
+ (r
->height
- client
->frame
->area
.height
)/2;
360 g_slist_free(spaces
);
369 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
375 area
= pick_pointer_head(client
);
376 screen_pointer_pos(&px
, &py
);
380 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
381 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
383 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
384 *x
= MIN(MAX(*x
, l
), r
);
385 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
386 *y
= MIN(MAX(*y
, t
), b
);
391 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
392 ObAppSettings
*settings
)
396 if (!settings
|| (settings
&& !settings
->pos_given
))
399 /* Find which head the pointer is on */
400 if (settings
->monitor
== 0)
401 screen
= pick_pointer_head(client
);
402 else if (settings
->monitor
> 0 &&
403 (guint
)settings
->monitor
<= screen_num_monitors
)
404 screen
= screen_area_monitor(client
->desktop
,
405 (guint
)settings
->monitor
- 1);
408 all
= pick_head(client
);
410 g_free(all
); /* the areas themselves don't need to be freed */
413 if (settings
->center_x
)
414 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
416 *x
= screen
->x
+ settings
->position
.x
;
418 if (settings
->center_y
)
419 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
421 *y
= screen
->y
+ settings
->position
.y
;
426 static gboolean
place_transient(ObClient
*client
, gint
*x
, gint
*y
)
428 if (client
->transient_for
&& client
->type
== OB_CLIENT_TYPE_DIALOG
) {
429 if (client
->transient_for
!= OB_TRAN_GROUP
) {
430 ObClient
*c
= client
;
431 ObClient
*p
= client
->transient_for
;
433 if (client_normal(p
)) {
434 *x
= (p
->frame
->area
.width
- c
->frame
->area
.width
) / 2 +
436 *y
= (p
->frame
->area
.height
- c
->frame
->area
.height
) / 2 +
442 gboolean first
= TRUE
;
444 for (it
= client
->group
->members
; it
; it
= g_slist_next(it
)) {
445 ObClient
*m
= it
->data
;
446 if (!(m
== client
|| m
->transient_for
) && client_normal(m
)) {
448 l
= RECT_LEFT(m
->frame
->area
);
449 t
= RECT_TOP(m
->frame
->area
);
450 r
= RECT_RIGHT(m
->frame
->area
);
451 b
= RECT_BOTTOM(m
->frame
->area
);
454 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
455 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
456 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
457 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
462 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
463 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
469 if (client
->transient
) {
472 areas
= pick_head(client
);
474 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
475 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
484 /* Return TRUE if we want client.c to enforce on-screen-keeping */
485 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
486 ObAppSettings
*settings
)
488 gboolean ret
= FALSE
;
489 if (client
->positioned
)
491 if (place_transient(client
, x
, y
))
494 place_per_app_setting(client
, x
, y
, settings
) ||
495 ((config_place_policy
== OB_PLACE_POLICY_MOUSE
) ?
496 place_under_mouse(client
, x
, y
) :
497 place_smart(client
, x
, y
, SMART_FULL
) ||
498 place_smart(client
, x
, y
, SMART_GROUP
) ||
499 place_smart(client
, x
, y
, SMART_FOCUSED
) ||
500 place_random(client
, x
, y
))))
501 g_assert_not_reached(); /* the last one better succeed */
502 /* get where the client should be */
503 frame_frame_gravity(client
->frame
, x
, y
,
504 client
->area
.width
, client
->area
.height
);
This page took 0.058538 seconds and 4 git commands to generate.