]>
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.
31 static void add_choice(guint
*choice
, guint mychoice
)
34 for (i
= 0; i
< screen_num_monitors
; ++i
) {
35 if (choice
[i
] == mychoice
)
37 else if (choice
[i
] == screen_num_monitors
) {
44 static Rect
*pick_pointer_head(ObClient
*c
)
49 if (screen_pointer_pos(&px
, &py
)) {
50 for (i
= 0; i
< screen_num_monitors
; ++i
) {
51 Rect
*monitor
= screen_physical_area_monitor(i
);
52 gboolean contain
= RECT_CONTAINS(*monitor
, px
, py
);
55 return screen_area(c
->desktop
, i
, NULL
);
57 g_assert_not_reached();
62 /*! Pick a monitor to place a window on. */
63 static Rect
**pick_head(ObClient
*c
)
71 area
= g_new(Rect
*, screen_num_monitors
);
72 choice
= g_new(guint
, screen_num_monitors
);
73 for (i
= 0; i
< screen_num_monitors
; ++i
)
74 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
76 /* try direct parent first */
77 if ((p
= client_direct_parent(c
))) {
78 add_choice(choice
, client_monitor(p
));
79 ob_debug("placement adding choice %d for parent\n",
83 /* more than one window in its group (more than just this window) */
84 if (client_has_group_siblings(c
)) {
87 /* try on the client's desktop */
88 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
89 ObClient
*itc
= it
->data
;
91 (itc
->desktop
== c
->desktop
||
92 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
94 add_choice(choice
, client_monitor(it
->data
));
95 ob_debug("placement adding choice %d for group sibling\n",
96 client_monitor(it
->data
));
100 /* try on all desktops */
101 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
102 ObClient
*itc
= it
->data
;
104 add_choice(choice
, client_monitor(it
->data
));
105 ob_debug("placement adding choice %d for group sibling on "
106 "another desktop\n", client_monitor(it
->data
));
112 add_choice(choice
, client_monitor(focus_client
));
113 ob_debug("placement adding choice %d for focused window\n",
114 client_monitor(focus_client
));
117 screen_pointer_pos(&px
, &py
);
119 for (i
= 0; i
< screen_num_monitors
; i
++) {
120 Rect
*monitor
= screen_physical_area_monitor(i
);
121 gboolean contain
= RECT_CONTAINS(*monitor
, px
, py
);
124 add_choice(choice
, i
);
125 ob_debug("placement adding choice %d for mouse pointer\n", i
);
130 /* add any leftover choices */
131 for (i
= 0; i
< screen_num_monitors
; ++i
)
132 add_choice(choice
, i
);
134 for (i
= 0; i
< screen_num_monitors
; ++i
)
135 area
[i
] = screen_area(c
->desktop
, choice
[i
], NULL
);
142 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
148 areas
= pick_head(client
);
149 i
= g_random_int_range(0, screen_num_monitors
);
153 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
154 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
156 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
157 else *x
= areas
[i
]->x
;
158 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
159 else *y
= areas
[i
]->y
;
161 for (i
= 0; i
< screen_num_monitors
; ++i
)
168 static GSList
* area_add(GSList
*list
, Rect
*a
)
170 Rect
*r
= g_new(Rect
, 1);
172 return g_slist_prepend(list
, r
);
175 static GSList
* area_remove(GSList
*list
, Rect
*a
)
178 GSList
*result
= NULL
;
180 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
183 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
184 result
= g_slist_prepend(result
, r
);
185 r
= NULL
; /* dont free it */
189 /* Use an intersection of a and r to determine the space
190 around r that we can use.
192 NOTE: the spaces calculated can overlap.
195 RECT_SET_INTERSECTION(isect
, *r
, *a
);
197 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
198 RECT_SET(extra
, r
->x
, r
->y
,
199 RECT_LEFT(isect
) - r
->x
, r
->height
);
200 result
= area_add(result
, &extra
);
203 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
204 RECT_SET(extra
, r
->x
, r
->y
,
205 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
206 result
= area_add(result
, &extra
);
209 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
210 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
211 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
212 result
= area_add(result
, &extra
);
215 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
216 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
217 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
218 result
= area_add(result
, &extra
);
229 IGNORE_FULLSCREEN
= 1,
230 IGNORE_MAXIMIZED
= 2,
232 /*IGNORE_SHADED = 3,*/
235 /*IGNORE_NONFOCUS = 1 << 5,*/
240 static gboolean
place_nooverlap(ObClient
*c
, gint
*x
, gint
*y
)
246 GSList
*spaces
= NULL
, *sit
, *maxit
;
249 areas
= pick_head(c
);
254 /* try ignoring different things to find empty space */
255 for (ignore
= 0; ignore
< IGNORE_END
&& !ret
; ignore
++) {
258 /* try all monitors in order of preference */
259 for (i
= 0; i
< screen_num_monitors
&& !ret
; ++i
) {
262 /* add the whole monitor */
263 spaces
= area_add(spaces
, areas
[i
]);
265 /* go thru all the windows */
266 for (it
= client_list
; it
; it
= g_list_next(it
)) {
267 ObClient
*test
= it
->data
;
269 /* should we ignore this client? */
270 if (screen_showing_desktop
) continue;
271 if (c
== test
) continue;
272 if (test
->iconic
) continue;
273 if (c
->desktop
!= DESKTOP_ALL
) {
274 if (test
->desktop
!= c
->desktop
&&
275 test
->desktop
!= DESKTOP_ALL
) continue;
277 if (test
->desktop
!= screen_desktop
&&
278 test
->desktop
!= DESKTOP_ALL
) continue;
280 if (test
->type
== OB_CLIENT_TYPE_SPLASH
||
281 test
->type
== OB_CLIENT_TYPE_DESKTOP
) continue;
284 if ((ignore
>= IGNORE_FULLSCREEN
) &&
285 test
->fullscreen
) continue;
286 if ((ignore
>= IGNORE_MAXIMIZED
) &&
287 test
->max_horz
&& test
->max_vert
) continue;
288 if ((ignore
>= IGNORE_MENUTOOL
) &&
289 (test
->type
== OB_CLIENT_TYPE_MENU
||
290 test
->type
== OB_CLIENT_TYPE_TOOLBAR
) &&
291 client_has_parent(c
)) continue;
293 if ((ignore >= IGNORE_SHADED) &&
294 test->shaded) continue;
296 if ((ignore
>= IGNORE_NONGROUP
) &&
297 client_has_group_siblings(c
) &&
298 test
->group
!= c
->group
) continue;
299 if ((ignore
>= IGNORE_BELOW
) &&
300 test
->layer
< c
->layer
) continue;
302 if ((ignore >= IGNORE_NONFOCUS) &&
303 focus_client != test) continue;
305 /* don't ignore this window, so remove it from the available
307 spaces
= area_remove(spaces
, &test
->frame
->area
);
310 if (ignore
< IGNORE_DOCK
) {
313 spaces
= area_remove(spaces
, &a
);
316 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
319 if (r
->width
>= c
->frame
->area
.width
&&
320 r
->height
>= c
->frame
->area
.height
&&
321 r
->width
* r
->height
> maxsize
)
323 maxsize
= r
->width
* r
->height
;
329 Rect
*r
= maxit
->data
;
331 /* center it in the area */
334 if (config_place_center
) {
335 *x
+= (r
->width
- c
->frame
->area
.width
) / 2;
336 *y
+= (r
->height
- c
->frame
->area
.height
) / 2;
342 g_free(spaces
->data
);
343 spaces
= g_slist_delete_link(spaces
, spaces
);
348 for (i
= 0; i
< screen_num_monitors
; ++i
)
354 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
360 if (!screen_pointer_pos(&px
, &py
))
362 area
= pick_pointer_head(client
);
366 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
367 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
369 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
370 *x
= MIN(MAX(*x
, l
), r
);
371 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
372 *y
= MIN(MAX(*y
, t
), b
);
377 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
378 ObAppSettings
*settings
)
382 if (!settings
|| (settings
&& !settings
->pos_given
))
385 /* Find which head the pointer is on */
386 if (settings
->monitor
== 0)
387 /* this can return NULL */
388 screen
= pick_pointer_head(client
);
389 else if (settings
->monitor
> 0 &&
390 (guint
)settings
->monitor
<= screen_num_monitors
)
391 screen
= screen_area(client
->desktop
, (guint
)settings
->monitor
- 1,
394 /* if we have't found a screen yet.. */
399 areas
= pick_head(client
);
402 /* don't free the first one, it's being set as "screen" */
403 for (i
= 1; i
< screen_num_monitors
; ++i
)
408 if (settings
->center_x
)
409 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
410 else if (settings
->opposite_x
)
411 *x
= screen
->x
+ screen
->width
- client
->frame
->area
.width
-
412 settings
->position
.x
;
414 *x
= screen
->x
+ settings
->position
.x
;
416 if (settings
->center_y
)
417 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
418 else if (settings
->opposite_y
)
419 *y
= screen
->y
+ screen
->height
- client
->frame
->area
.height
-
420 settings
->position
.y
;
422 *y
= screen
->y
+ settings
->position
.y
;
428 static gboolean
place_transient_splash(ObClient
*client
, gint
*x
, gint
*y
)
430 if (client
->type
== OB_CLIENT_TYPE_DIALOG
) {
432 gboolean first
= TRUE
;
434 for (it
= client
->parents
; it
; it
= g_slist_next(it
)) {
435 ObClient
*m
= it
->data
;
438 l
= RECT_LEFT(m
->frame
->area
);
439 t
= RECT_TOP(m
->frame
->area
);
440 r
= RECT_RIGHT(m
->frame
->area
);
441 b
= RECT_BOTTOM(m
->frame
->area
);
444 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
445 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
446 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
447 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
451 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
452 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
458 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
459 client
->type
== OB_CLIENT_TYPE_SPLASH
)
464 areas
= pick_head(client
);
466 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
467 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
469 for (i
= 0; i
< screen_num_monitors
; ++i
)
478 /* Return TRUE if we want client.c to enforce on-screen-keeping */
479 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
480 ObAppSettings
*settings
)
483 gboolean userplaced
= FALSE
;
485 /* per-app settings override program specified position
486 * but not user specified */
487 if ((client
->positioned
& USPosition
) ||
488 ((client
->positioned
& PPosition
) &&
489 !(settings
&& settings
->pos_given
)))
492 /* try a number of methods */
493 ret
= place_transient_splash(client
, x
, y
) ||
494 (userplaced
= 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_nooverlap(client
, x
, y
) ||
498 place_random(client
, x
, y
);
501 /* get where the client should be */
502 frame_frame_gravity(client
->frame
, x
, y
);
This page took 0.053241 seconds and 4 git commands to generate.