]>
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.
28 static void add_choice(guint
*choice
, guint mychoice
)
31 for (i
= 0; i
< screen_num_monitors
; ++i
) {
32 if (choice
[i
] == mychoice
)
34 else if (choice
[i
] == screen_num_monitors
) {
41 static Rect
*pick_pointer_head(ObClient
*c
)
46 screen_pointer_pos(&px
, &py
);
48 for (i
= 0; i
< screen_num_monitors
; ++i
) {
49 if (RECT_CONTAINS(*screen_physical_area_monitor(i
), px
, py
)) {
50 return screen_area_monitor(c
->desktop
, i
);
53 g_assert_not_reached();
56 /*! Pick a monitor to place a window on.
57 The returned array value should be freed with g_free. The areas within the
58 array should not be freed. */
59 static Rect
**pick_head(ObClient
*c
)
66 area
= g_new(Rect
*, screen_num_monitors
);
67 choice
= g_new(guint
, screen_num_monitors
);
68 for (i
= 0; i
< screen_num_monitors
; ++i
)
69 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
71 /* try direct parent first */
72 if (c
->transient_for
&& c
->transient_for
!= OB_TRAN_GROUP
) {
73 add_choice(choice
, client_monitor(c
->transient_for
));
74 ob_debug("placement adding choice %d for parent\n",
75 client_monitor(c
->transient_for
));
78 /* more than one window in its group (more than just this window) */
79 if (client_has_group_siblings(c
)) {
82 /* try on the client's desktop */
83 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
84 ObClient
*itc
= it
->data
;
86 (itc
->desktop
== c
->desktop
||
87 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
89 add_choice(choice
, client_monitor(it
->data
));
90 ob_debug("placement adding choice %d for group sibling\n",
91 client_monitor(it
->data
));
95 /* try on all desktops */
96 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
97 ObClient
*itc
= it
->data
;
99 add_choice(choice
, client_monitor(it
->data
));
100 ob_debug("placement adding choice %d for group sibling on "
101 "another desktop\n", client_monitor(it
->data
));
107 add_choice(choice
, client_monitor(focus_client
));
108 ob_debug("placement adding choice %d for focused window\n",
109 client_monitor(focus_client
));
112 screen_pointer_pos(&px
, &py
);
114 for (i
= 0; i
< screen_num_monitors
; i
++)
115 if (RECT_CONTAINS(*screen_physical_area_monitor(i
), px
, py
)) {
116 add_choice(choice
, i
);
117 ob_debug("placement adding choice %d for mouse pointer\n", i
);
121 /* add any leftover choices */
122 for (i
= 0; i
< screen_num_monitors
; ++i
)
123 add_choice(choice
, i
);
125 for (i
= 0; i
< screen_num_monitors
; ++i
)
126 area
[i
] = screen_area_monitor(c
->desktop
, choice
[i
]);
131 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
137 areas
= pick_head(client
);
138 i
= g_random_int_range(0, screen_num_monitors
);
142 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
143 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
145 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
146 else *x
= areas
[i
]->x
;
147 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
148 else *y
= areas
[i
]->y
;
155 static GSList
* area_add(GSList
*list
, Rect
*a
)
157 Rect
*r
= g_new(Rect
, 1);
159 return g_slist_prepend(list
, r
);
162 static GSList
* area_remove(GSList
*list
, Rect
*a
)
165 GSList
*result
= NULL
;
167 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
170 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
171 result
= g_slist_prepend(result
, r
);
172 r
= NULL
; /* dont free it */
176 /* Use an intersection of a and r to determine the space
177 around r that we can use.
179 NOTE: the spaces calculated can overlap.
182 RECT_SET_INTERSECTION(isect
, *r
, *a
);
184 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
185 RECT_SET(extra
, r
->x
, r
->y
,
186 RECT_LEFT(isect
) - r
->x
, r
->height
);
187 result
= area_add(result
, &extra
);
190 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
191 RECT_SET(extra
, r
->x
, r
->y
,
192 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
193 result
= area_add(result
, &extra
);
196 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
197 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
198 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
199 result
= area_add(result
, &extra
);
202 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
203 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
204 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
205 result
= area_add(result
, &extra
);
215 static gint
area_cmp(gconstpointer p1
, gconstpointer p2
, gpointer data
)
218 Rect
*carea
= &c
->frame
->area
;
219 const Rect
*a1
= p1
, *a2
= p2
;
220 gboolean diffhead
= FALSE
;
224 for (i
= 0; i
< screen_num_monitors
; ++i
) {
225 a
= screen_physical_area_monitor(i
);
226 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
) &&
227 !RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
234 /* has to be more than me in the group */
235 if (diffhead
&& client_has_group_siblings(c
)) {
239 /* find how many clients in the group are on each monitor, use the
240 monitor with the most in it */
241 num
= g_new0(guint
, screen_num_monitors
);
242 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
))
244 ++num
[client_monitor(it
->data
)];
246 for (i
= 1; i
< screen_num_monitors
; ++i
)
247 if (num
[i
] > num
[most
])
252 a
= screen_physical_area_monitor(most
);
253 if (RECT_CONTAINS(*a
, a1
->x
, a1
->y
))
255 if (RECT_CONTAINS(*a
, a2
->x
, a2
->y
))
259 return MIN((a1
->width
- carea
->width
), (a1
->height
- carea
->height
)) -
260 MIN((a2
->width
- carea
->width
), (a2
->height
- carea
->height
));
270 #define SMART_IGNORE(placer, c) \
271 (placer == c || c->shaded || !client_normal(c) || !c->frame->visible || \
272 (c->desktop != DESKTOP_ALL && \
273 c->desktop != (placer->desktop == DESKTOP_ALL ? \
274 screen_desktop : placer->desktop)))
276 static gboolean
place_smart(ObClient
*client
, gint
*x
, gint
*y
,
279 gboolean ret
= FALSE
;
280 GSList
*spaces
= NULL
, *sit
;
285 if (type
== SMART_GROUP
) {
286 /* has to be more than me in the group */
287 if (!client_has_group_siblings(client
))
291 areas
= pick_head(client
);
293 for (i
= 0; i
< screen_num_monitors
&& !ret
; ++i
) {
294 spaces
= area_add(spaces
, areas
[i
]);
296 /* stay out from under windows in higher layers */
297 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
300 if (WINDOW_IS_CLIENT(it
->data
)) {
302 if (c
->fullscreen
|| (c
->max_vert
&& c
->max_horz
))
307 if (c
->layer
> client
->layer
) {
308 if (!SMART_IGNORE(client
, c
))
309 spaces
= area_remove(spaces
, &c
->frame
->area
);
314 if (type
== SMART_FULL
|| type
== SMART_FOCUSED
) {
315 gboolean found_foc
= FALSE
, stop
= FALSE
;
318 foc
= focus_order_find_first(client
->desktop
== DESKTOP_ALL
?
319 screen_desktop
: client
->desktop
);
321 for (; it
&& !stop
; it
= g_list_next(it
)) {
324 if (WINDOW_IS_CLIENT(it
->data
)) {
326 if (c
->fullscreen
|| (c
->max_vert
&& c
->max_horz
))
331 if (!SMART_IGNORE(client
, c
)) {
332 if (type
== SMART_FOCUSED
)
336 spaces
= area_remove(spaces
, &c
->frame
->area
);
342 } else if (type
== SMART_GROUP
) {
343 for (sit
= client
->group
->members
; sit
; sit
= g_slist_next(sit
)) {
344 ObClient
*c
= sit
->data
;
345 if (!SMART_IGNORE(client
, c
))
346 spaces
= area_remove(spaces
, &c
->frame
->area
);
349 g_assert_not_reached();
351 spaces
= g_slist_sort_with_data(spaces
, area_cmp
, client
);
353 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
357 if (r
->width
>= client
->frame
->area
.width
&&
358 r
->height
>= client
->frame
->area
.height
) {
360 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
363 *x
= r
->x
+ (r
->width
- client
->frame
->area
.width
)/2;
364 *y
= r
->y
+ (r
->height
- client
->frame
->area
.height
)/2;
374 g_slist_free(spaces
);
383 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
389 area
= pick_pointer_head(client
);
390 screen_pointer_pos(&px
, &py
);
394 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
395 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
397 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
398 *x
= MIN(MAX(*x
, l
), r
);
399 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
400 *y
= MIN(MAX(*y
, t
), b
);
405 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
406 ObAppSettings
*settings
)
410 if (!settings
|| (settings
&& !settings
->pos_given
))
413 /* Find which head the pointer is on */
414 if (settings
->monitor
== 0)
415 screen
= pick_pointer_head(client
);
416 else if (settings
->monitor
> 0 &&
417 (guint
)settings
->monitor
<= screen_num_monitors
)
418 screen
= screen_area_monitor(client
->desktop
,
419 (guint
)settings
->monitor
- 1);
422 all
= pick_head(client
);
424 g_free(all
); /* the areas themselves don't need to be freed */
427 if (settings
->center_x
)
428 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
430 *x
= screen
->x
+ settings
->position
.x
;
432 if (settings
->center_y
)
433 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
435 *y
= screen
->y
+ settings
->position
.y
;
440 static gboolean
place_transient(ObClient
*client
, gint
*x
, gint
*y
)
442 if (client
->transient_for
&& client
->type
== OB_CLIENT_TYPE_DIALOG
) {
443 if (client
->transient_for
!= OB_TRAN_GROUP
) {
444 ObClient
*c
= client
;
445 ObClient
*p
= client
->transient_for
;
447 if (client_normal(p
)) {
448 *x
= (p
->frame
->area
.width
- c
->frame
->area
.width
) / 2 +
450 *y
= (p
->frame
->area
.height
- c
->frame
->area
.height
) / 2 +
456 gboolean first
= TRUE
;
458 for (it
= client
->group
->members
; it
; it
= g_slist_next(it
)) {
459 ObClient
*m
= it
->data
;
460 if (!(m
== client
|| m
->transient_for
) && client_normal(m
)) {
462 l
= RECT_LEFT(m
->frame
->area
);
463 t
= RECT_TOP(m
->frame
->area
);
464 r
= RECT_RIGHT(m
->frame
->area
);
465 b
= RECT_BOTTOM(m
->frame
->area
);
468 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
469 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
470 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
471 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
476 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
477 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
483 if (client
->transient
) {
486 areas
= pick_head(client
);
488 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
489 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
498 /* Return TRUE if we want client.c to enforce on-screen-keeping */
499 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
500 ObAppSettings
*settings
)
502 gboolean ret
= FALSE
;
503 if (client
->positioned
)
505 if (place_transient(client
, x
, y
))
508 place_per_app_setting(client
, x
, y
, settings
) ||
509 ((config_place_policy
== OB_PLACE_POLICY_MOUSE
) ?
510 place_under_mouse(client
, x
, y
) :
511 place_smart(client
, x
, y
, SMART_FULL
) ||
512 place_smart(client
, x
, y
, SMART_GROUP
) ||
513 place_smart(client
, x
, y
, SMART_FOCUSED
) ||
514 place_random(client
, x
, y
))))
515 g_assert_not_reached(); /* the last one better succeed */
516 /* get where the client should be */
517 frame_frame_gravity(client
->frame
, x
, y
,
518 client
->area
.width
, client
->area
.height
);
This page took 0.05573 seconds and 4 git commands to generate.