]>
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 screen_pointer_pos(&px
, &py
);
51 for (i
= 0; i
< screen_num_monitors
; ++i
) {
52 Rect
*monitor
= screen_physical_area_monitor(i
);
53 gboolean contain
= RECT_CONTAINS(*monitor
, px
, py
);
56 return screen_area(c
->desktop
, i
, NULL
);
58 g_assert_not_reached();
61 /*! Pick a monitor to place a window on. */
62 static Rect
**pick_head(ObClient
*c
)
70 area
= g_new(Rect
*, screen_num_monitors
);
71 choice
= g_new(guint
, screen_num_monitors
);
72 for (i
= 0; i
< screen_num_monitors
; ++i
)
73 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
75 /* try direct parent first */
76 if ((p
= client_direct_parent(c
))) {
77 add_choice(choice
, client_monitor(p
));
78 ob_debug("placement adding choice %d for parent\n",
82 /* more than one window in its group (more than just this window) */
83 if (client_has_group_siblings(c
)) {
86 /* try on the client's desktop */
87 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
88 ObClient
*itc
= it
->data
;
90 (itc
->desktop
== c
->desktop
||
91 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
93 add_choice(choice
, client_monitor(it
->data
));
94 ob_debug("placement adding choice %d for group sibling\n",
95 client_monitor(it
->data
));
99 /* try on all desktops */
100 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
101 ObClient
*itc
= it
->data
;
103 add_choice(choice
, client_monitor(it
->data
));
104 ob_debug("placement adding choice %d for group sibling on "
105 "another desktop\n", client_monitor(it
->data
));
111 add_choice(choice
, client_monitor(focus_client
));
112 ob_debug("placement adding choice %d for focused window\n",
113 client_monitor(focus_client
));
116 screen_pointer_pos(&px
, &py
);
118 for (i
= 0; i
< screen_num_monitors
; i
++) {
119 Rect
*monitor
= screen_physical_area_monitor(i
);
120 gboolean contain
= RECT_CONTAINS(*monitor
, px
, py
);
123 add_choice(choice
, i
);
124 ob_debug("placement adding choice %d for mouse pointer\n", i
);
129 /* add any leftover choices */
130 for (i
= 0; i
< screen_num_monitors
; ++i
)
131 add_choice(choice
, i
);
133 for (i
= 0; i
< screen_num_monitors
; ++i
)
134 area
[i
] = screen_area(c
->desktop
, choice
[i
], NULL
);
139 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
145 areas
= pick_head(client
);
146 i
= g_random_int_range(0, screen_num_monitors
);
150 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
151 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
153 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
154 else *x
= areas
[i
]->x
;
155 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
156 else *y
= areas
[i
]->y
;
158 for (i
= 0; i
< screen_num_monitors
; ++i
)
165 static GSList
* area_add(GSList
*list
, Rect
*a
)
167 Rect
*r
= g_new(Rect
, 1);
169 return g_slist_prepend(list
, r
);
172 static GSList
* area_remove(GSList
*list
, Rect
*a
)
175 GSList
*result
= NULL
;
177 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
180 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
181 result
= g_slist_prepend(result
, r
);
182 r
= NULL
; /* dont free it */
186 /* Use an intersection of a and r to determine the space
187 around r that we can use.
189 NOTE: the spaces calculated can overlap.
192 RECT_SET_INTERSECTION(isect
, *r
, *a
);
194 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
195 RECT_SET(extra
, r
->x
, r
->y
,
196 RECT_LEFT(isect
) - r
->x
, r
->height
);
197 result
= area_add(result
, &extra
);
200 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
201 RECT_SET(extra
, r
->x
, r
->y
,
202 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
203 result
= area_add(result
, &extra
);
206 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
207 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
208 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
209 result
= area_add(result
, &extra
);
212 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
213 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
214 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
215 result
= area_add(result
, &extra
);
226 IGNORE_FULLSCREEN
= 1,
227 IGNORE_MAXIMIZED
= 2,
229 /*IGNORE_SHADED = 3,*/
232 /*IGNORE_NONFOCUS = 1 << 5,*/
237 static gboolean
place_nooverlap(ObClient
*c
, gint
*x
, gint
*y
)
243 GSList
*spaces
= NULL
, *sit
, *maxit
;
246 areas
= pick_head(c
);
251 /* try ignoring different things to find empty space */
252 for (ignore
= 0; ignore
< IGNORE_END
&& !ret
; ignore
++) {
255 /* try all monitors in order of preference */
256 for (i
= 0; i
< screen_num_monitors
&& !ret
; ++i
) {
259 /* add the whole monitor */
260 spaces
= area_add(spaces
, areas
[i
]);
262 /* go thru all the windows */
263 for (it
= client_list
; it
; it
= g_list_next(it
)) {
264 ObClient
*test
= it
->data
;
266 /* should we ignore this client? */
267 if (screen_showing_desktop
) continue;
268 if (c
== test
) continue;
269 if (test
->iconic
) continue;
270 if (c
->desktop
!= DESKTOP_ALL
) {
271 if (test
->desktop
!= c
->desktop
&&
272 test
->desktop
!= DESKTOP_ALL
) continue;
274 if (test
->desktop
!= screen_desktop
&&
275 test
->desktop
!= DESKTOP_ALL
) continue;
277 if (test
->type
== OB_CLIENT_TYPE_SPLASH
||
278 test
->type
== OB_CLIENT_TYPE_DESKTOP
) continue;
281 if ((ignore
>= IGNORE_FULLSCREEN
) &&
282 test
->fullscreen
) continue;
283 if ((ignore
>= IGNORE_MAXIMIZED
) &&
284 test
->max_horz
&& test
->max_vert
) continue;
285 if ((ignore
>= IGNORE_MENUTOOL
) &&
286 (test
->type
== OB_CLIENT_TYPE_MENU
||
287 test
->type
== OB_CLIENT_TYPE_TOOLBAR
) &&
288 client_has_parent(c
)) continue;
290 if ((ignore >= IGNORE_SHADED) &&
291 test->shaded) continue;
293 if ((ignore
>= IGNORE_NONGROUP
) &&
294 client_has_group_siblings(c
) &&
295 test
->group
!= c
->group
) continue;
296 if ((ignore
>= IGNORE_BELOW
) &&
297 test
->layer
< c
->layer
) continue;
299 if ((ignore >= IGNORE_NONFOCUS) &&
300 focus_client != test) continue;
302 /* don't ignore this window, so remove it from the available
304 spaces
= area_remove(spaces
, &test
->frame
->area
);
307 if (ignore
< IGNORE_DOCK
) {
310 spaces
= area_remove(spaces
, &a
);
313 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
316 if (r
->width
>= c
->frame
->area
.width
&&
317 r
->height
>= c
->frame
->area
.height
&&
326 Rect
*r
= maxit
->data
;
328 /* center it in the area */
331 if (config_place_center
) {
332 *x
+= (r
->width
- c
->frame
->area
.width
) / 2;
333 *y
+= (r
->height
- c
->frame
->area
.height
) / 2;
339 g_free(spaces
->data
);
340 spaces
= g_slist_delete_link(spaces
, spaces
);
345 for (i
= 0; i
< screen_num_monitors
; ++i
)
351 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
357 if (!screen_pointer_pos(&px
, &py
))
359 area
= pick_pointer_head(client
);
363 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
364 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
366 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
367 *x
= MIN(MAX(*x
, l
), r
);
368 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
369 *y
= MIN(MAX(*y
, t
), b
);
374 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
375 ObAppSettings
*settings
)
379 if (!settings
|| (settings
&& !settings
->pos_given
))
382 /* Find which head the pointer is on */
383 if (settings
->monitor
== 0)
384 screen
= pick_pointer_head(client
);
385 else if (settings
->monitor
> 0 &&
386 (guint
)settings
->monitor
<= screen_num_monitors
)
387 screen
= screen_area(client
->desktop
, (guint
)settings
->monitor
- 1,
393 areas
= pick_head(client
);
396 for (i
= 0; i
< screen_num_monitors
; ++i
)
401 if (settings
->center_x
)
402 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
403 else if (settings
->opposite_x
)
404 *x
= screen
->x
+ screen
->width
- client
->frame
->area
.width
-
405 settings
->position
.x
;
407 *x
= screen
->x
+ settings
->position
.x
;
409 if (settings
->center_y
)
410 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
411 else if (settings
->opposite_y
)
412 *y
= screen
->y
+ screen
->height
- client
->frame
->area
.height
-
413 settings
->position
.y
;
415 *y
= screen
->y
+ settings
->position
.y
;
420 static gboolean
place_transient_splash(ObClient
*client
, gint
*x
, gint
*y
)
422 if (client
->type
== OB_CLIENT_TYPE_DIALOG
) {
424 gboolean first
= TRUE
;
426 for (it
= client
->parents
; it
; it
= g_slist_next(it
)) {
427 ObClient
*m
= it
->data
;
430 l
= RECT_LEFT(m
->frame
->area
);
431 t
= RECT_TOP(m
->frame
->area
);
432 r
= RECT_RIGHT(m
->frame
->area
);
433 b
= RECT_BOTTOM(m
->frame
->area
);
436 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
437 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
438 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
439 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
443 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
444 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
450 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
451 client
->type
== OB_CLIENT_TYPE_SPLASH
)
456 areas
= pick_head(client
);
458 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
459 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
461 for (i
= 0; i
< screen_num_monitors
; ++i
)
470 /* Return TRUE if we want client.c to enforce on-screen-keeping */
471 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
472 ObAppSettings
*settings
)
476 if (client
->positioned
)
479 /* try a number of methods */
480 ret
= place_transient_splash(client
, x
, y
) ||
481 place_per_app_setting(client
, x
, y
, settings
) ||
482 (config_place_policy
== OB_PLACE_POLICY_MOUSE
&&
483 place_under_mouse(client
, x
, y
)) ||
484 place_nooverlap(client
, x
, y
) ||
485 place_random(client
, x
, y
);
488 /* get where the client should be */
489 frame_frame_gravity(client
->frame
, x
, y
);
This page took 0.058874 seconds and 4 git commands to generate.