]>
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 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 static Rect
**pick_head(ObClient
*c
)
62 area
= g_new(Rect
*, screen_num_monitors
);
63 choice
= g_new(guint
, screen_num_monitors
);
64 for (i
= 0; i
< screen_num_monitors
; ++i
)
65 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
67 /* try direct parent first */
68 if (c
->transient_for
&& c
->transient_for
!= OB_TRAN_GROUP
) {
69 add_choice(choice
, client_monitor(c
->transient_for
));
72 /* more than one window in its group (more than just this window) */
73 if (client_has_group_siblings(c
)) {
76 /* try on the client's desktop */
77 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
78 ObClient
*itc
= it
->data
;
80 (itc
->desktop
== c
->desktop
||
81 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
82 add_choice(choice
, client_monitor(it
->data
));
85 /* try on all desktops */
86 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
87 ObClient
*itc
= it
->data
;
89 add_choice(choice
, client_monitor(it
->data
));
94 add_choice(choice
, client_monitor(focus_client
));
96 screen_pointer_pos(&px
, &py
);
98 for (i
= 0; i
< screen_num_monitors
; i
++)
99 if (RECT_CONTAINS(*screen_physical_area_monitor(i
), px
, py
)) {
100 add_choice(choice
, i
);
104 /* add any leftover choices */
105 for (i
= 0; i
< screen_num_monitors
; ++i
)
106 add_choice(choice
, i
);
108 for (i
= 0; i
< screen_num_monitors
; ++i
)
109 area
[i
] = screen_area_monitor(c
->desktop
, choice
[i
]);
114 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
120 areas
= pick_head(client
);
121 i
= g_random_int_range(0, screen_num_monitors
);
125 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
126 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
128 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
130 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
138 static GSList
* area_add(GSList
*list
, Rect
*a
)
140 Rect
*r
= g_new(Rect
, 1);
142 return g_slist_prepend(list
, r
);
145 static GSList
* area_remove(GSList
*list
, Rect
*a
)
148 GSList
*result
= NULL
;
150 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
153 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
154 result
= g_slist_prepend(result
, r
);
155 r
= NULL
; /* dont free it */
159 /* Use an intersection of a and r to determine the space
160 around r that we can use.
162 NOTE: the spaces calculated can overlap.
165 RECT_SET_INTERSECTION(isect
, *r
, *a
);
167 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
168 RECT_SET(extra
, r
->x
, r
->y
,
169 RECT_LEFT(isect
) - r
->x
, r
->height
);
170 result
= area_add(result
, &extra
);
173 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
174 RECT_SET(extra
, r
->x
, r
->y
,
175 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
176 result
= area_add(result
, &extra
);
179 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
180 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
181 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
182 result
= area_add(result
, &extra
);
185 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
186 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
187 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
188 result
= area_add(result
, &extra
);
198 static gint
area_cmp(gconstpointer p1
, gconstpointer p2
, gpointer data
)
201 Rect
*carea
= &c
->frame
->area
;
202 const Rect
*a1
= p1
, *a2
= p2
;
203 gboolean diffhead
= FALSE
;
207 for (i
= 0; i
< screen_num_monitors
; ++i
) {
208 a
= screen_physical_area_monitor(i
);
209 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
) &&
210 !RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
217 /* has to be more than me in the group */
218 if (diffhead
&& client_has_group_siblings(c
)) {
222 /* find how many clients in the group are on each monitor, use the
223 monitor with the most in it */
224 num
= g_new0(guint
, screen_num_monitors
);
225 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
))
227 ++num
[client_monitor(it
->data
)];
229 for (i
= 1; i
< screen_num_monitors
; ++i
)
230 if (num
[i
] > num
[most
])
235 a
= screen_physical_area_monitor(most
);
236 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
))
238 if (RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
242 return MIN((a1
->width
- carea
->width
), (a1
->height
- carea
->height
)) -
243 MIN((a2
->width
- carea
->width
), (a2
->height
- carea
->height
));
253 #define SMART_IGNORE(placer, c) \
254 (placer == c || !c->frame->visible || c->shaded || !client_normal(c) || \
255 (c->desktop != DESKTOP_ALL && \
256 c->desktop != (placer->desktop == DESKTOP_ALL ? \
257 screen_desktop : placer->desktop)))
259 static gboolean
place_smart(ObClient
*client
, gint
*x
, gint
*y
,
262 gboolean ret
= FALSE
;
263 GSList
*spaces
= NULL
, *sit
;
268 if (type
== SMART_GROUP
) {
269 /* has to be more than me in the group */
270 if (!client_has_group_siblings(client
))
274 areas
= pick_head(client
);
276 for (i
= 0; i
< screen_num_monitors
; ++i
) {
277 spaces
= area_add(spaces
, areas
[i
]);
279 /* stay out from under windows in higher layers */
280 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
283 if (WINDOW_IS_CLIENT(it
->data
)) {
290 if (c
->layer
> client
->layer
) {
291 if (!SMART_IGNORE(client
, c
))
292 spaces
= area_remove(spaces
, &c
->frame
->area
);
297 if (type
== SMART_FULL
|| type
== SMART_FOCUSED
) {
298 gboolean found_foc
= FALSE
, stop
= FALSE
;
302 list
= focus_order
[client
->desktop
== DESKTOP_ALL
?
303 screen_desktop
: client
->desktop
];
304 foc
= list
? list
->data
: NULL
;
306 for (; it
&& !stop
; it
= g_list_next(it
)) {
309 if (WINDOW_IS_CLIENT(it
->data
)) {
316 if (!SMART_IGNORE(client
, c
)) {
317 if (type
== SMART_FOCUSED
)
321 spaces
= area_remove(spaces
, &c
->frame
->area
);
327 } else if (type
== SMART_GROUP
) {
328 for (sit
= client
->group
->members
; sit
; sit
= g_slist_next(sit
)) {
329 ObClient
*c
= sit
->data
;
330 if (!SMART_IGNORE(client
, c
))
331 spaces
= area_remove(spaces
, &c
->frame
->area
);
334 g_assert_not_reached();
336 spaces
= g_slist_sort_with_data(spaces
, area_cmp
, client
);
338 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
342 if (r
->width
>= client
->frame
->area
.width
&&
343 r
->height
>= client
->frame
->area
.height
) {
345 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
348 *x
= r
->x
+ (r
->width
- client
->frame
->area
.width
)/2;
349 *y
= r
->y
+ (r
->height
- client
->frame
->area
.height
)/2;
359 g_slist_free(spaces
);
368 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
374 area
= pick_pointer_head(client
);
378 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
379 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
381 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
382 *x
= MIN(MAX(*x
, l
), r
);
383 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
384 *y
= MIN(MAX(*y
, t
), b
);
389 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
390 ObAppSettings
*settings
)
394 if (!settings
|| (settings
&& !settings
->pos_given
))
397 /* Find which head the pointer is on */
398 if (settings
->head
== -1)
399 screen
= pick_pointer_head(client
);
401 screen
= screen_area_monitor(client
->desktop
, settings
->head
);
403 if (settings
->center_x
)
404 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
406 *x
= screen
->x
+ settings
->position
.x
;
408 if (settings
->center_y
)
409 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
411 *y
= screen
->y
+ settings
->position
.y
;
416 static gboolean
place_transient(ObClient
*client
, gint
*x
, gint
*y
)
418 if (client
->transient_for
) {
419 if (client
->transient_for
!= OB_TRAN_GROUP
) {
420 ObClient
*c
= client
;
421 ObClient
*p
= client
->transient_for
;
422 *x
= (p
->frame
->area
.width
- c
->frame
->area
.width
) / 2 +
424 *y
= (p
->frame
->area
.height
- c
->frame
->area
.height
) / 2 +
429 gboolean first
= TRUE
;
431 for (it
= client
->group
->members
; it
; it
= g_slist_next(it
)) {
432 ObClient
*m
= it
->data
;
433 if (!(m
== client
|| m
->transient_for
)) {
435 l
= RECT_LEFT(m
->frame
->area
);
436 t
= RECT_TOP(m
->frame
->area
);
437 r
= RECT_RIGHT(m
->frame
->area
);
438 b
= RECT_BOTTOM(m
->frame
->area
);
441 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
442 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
443 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
444 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
449 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
450 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
458 /* Return TRUE if we want client.c to enforce on-screen-keeping */
459 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
460 ObAppSettings
*settings
)
462 gboolean ret
= FALSE
;
463 if (client
->positioned
)
465 if (place_transient(client
, x
, y
))
468 place_per_app_setting(client
, x
, y
, settings
) ||
469 ((config_place_policy
== OB_PLACE_POLICY_MOUSE
) ?
470 place_under_mouse(client
, x
, y
) :
471 place_smart(client
, x
, y
, SMART_FULL
) ||
472 place_smart(client
, x
, y
, SMART_GROUP
) ||
473 place_smart(client
, x
, y
, SMART_FOCUSED
) ||
474 place_random(client
, x
, y
))))
475 g_assert_not_reached(); /* the last one better succeed */
476 /* get where the client should be */
477 frame_frame_gravity(client
->frame
, x
, y
);
This page took 0.054369 seconds and 4 git commands to generate.