]>
Dogcows Code - chaz/openbox/blob - openbox/place.c
6c81df6189c336f3e01225178249c2ed6ee3740b
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
)
67 area
= g_new(Rect
*, screen_num_monitors
);
68 choice
= g_new(guint
, screen_num_monitors
);
69 for (i
= 0; i
< screen_num_monitors
; ++i
)
70 choice
[i
] = screen_num_monitors
; /* make them all invalid to start */
72 /* try direct parent first */
73 if ((p
= client_direct_parent(c
))) {
74 add_choice(choice
, client_monitor(p
));
75 ob_debug("placement adding choice %d for parent\n",
79 /* more than one window in its group (more than just this window) */
80 if (client_has_group_siblings(c
)) {
83 /* try on the client's desktop */
84 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
85 ObClient
*itc
= it
->data
;
87 (itc
->desktop
== c
->desktop
||
88 itc
->desktop
== DESKTOP_ALL
|| c
->desktop
== DESKTOP_ALL
))
90 add_choice(choice
, client_monitor(it
->data
));
91 ob_debug("placement adding choice %d for group sibling\n",
92 client_monitor(it
->data
));
96 /* try on all desktops */
97 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
98 ObClient
*itc
= it
->data
;
100 add_choice(choice
, client_monitor(it
->data
));
101 ob_debug("placement adding choice %d for group sibling on "
102 "another desktop\n", client_monitor(it
->data
));
108 add_choice(choice
, client_monitor(focus_client
));
109 ob_debug("placement adding choice %d for focused window\n",
110 client_monitor(focus_client
));
113 screen_pointer_pos(&px
, &py
);
115 for (i
= 0; i
< screen_num_monitors
; i
++)
116 if (RECT_CONTAINS(*screen_physical_area_monitor(i
), px
, py
)) {
117 add_choice(choice
, i
);
118 ob_debug("placement adding choice %d for mouse pointer\n", i
);
122 /* add any leftover choices */
123 for (i
= 0; i
< screen_num_monitors
; ++i
)
124 add_choice(choice
, i
);
126 for (i
= 0; i
< screen_num_monitors
; ++i
)
127 area
[i
] = screen_area_monitor(c
->desktop
, choice
[i
]);
132 static gboolean
place_random(ObClient
*client
, gint
*x
, gint
*y
)
138 areas
= pick_head(client
);
139 i
= g_random_int_range(0, screen_num_monitors
);
143 r
= areas
[i
]->x
+ areas
[i
]->width
- client
->frame
->area
.width
;
144 b
= areas
[i
]->y
+ areas
[i
]->height
- client
->frame
->area
.height
;
146 if (r
> l
) *x
= g_random_int_range(l
, r
+ 1);
147 else *x
= areas
[i
]->x
;
148 if (b
> t
) *y
= g_random_int_range(t
, b
+ 1);
149 else *y
= areas
[i
]->y
;
156 static GSList
* area_add(GSList
*list
, Rect
*a
)
158 Rect
*r
= g_new(Rect
, 1);
160 return g_slist_prepend(list
, r
);
163 static GSList
* area_remove(GSList
*list
, Rect
*a
)
166 GSList
*result
= NULL
;
168 for (sit
= list
; sit
; sit
= g_slist_next(sit
)) {
171 if (!RECT_INTERSECTS_RECT(*r
, *a
)) {
172 result
= g_slist_prepend(result
, r
);
173 r
= NULL
; /* dont free it */
177 /* Use an intersection of a and r to determine the space
178 around r that we can use.
180 NOTE: the spaces calculated can overlap.
183 RECT_SET_INTERSECTION(isect
, *r
, *a
);
185 if (RECT_LEFT(isect
) > RECT_LEFT(*r
)) {
186 RECT_SET(extra
, r
->x
, r
->y
,
187 RECT_LEFT(isect
) - r
->x
, r
->height
);
188 result
= area_add(result
, &extra
);
191 if (RECT_TOP(isect
) > RECT_TOP(*r
)) {
192 RECT_SET(extra
, r
->x
, r
->y
,
193 r
->width
, RECT_TOP(isect
) - r
->y
+ 1);
194 result
= area_add(result
, &extra
);
197 if (RECT_RIGHT(isect
) < RECT_RIGHT(*r
)) {
198 RECT_SET(extra
, RECT_RIGHT(isect
) + 1, r
->y
,
199 RECT_RIGHT(*r
) - RECT_RIGHT(isect
), r
->height
);
200 result
= area_add(result
, &extra
);
203 if (RECT_BOTTOM(isect
) < RECT_BOTTOM(*r
)) {
204 RECT_SET(extra
, r
->x
, RECT_BOTTOM(isect
) + 1,
205 r
->width
, RECT_BOTTOM(*r
) - RECT_BOTTOM(isect
));
206 result
= area_add(result
, &extra
);
217 IGNORE_FULLSCREEN
= 1 << 0,
218 IGNORE_MAXIMIZED
= 1 << 1,
219 IGNORE_MENUTOOL
= 1 << 2,
220 IGNORE_SHADED
= 1 << 3,
221 IGNORE_NONGROUP
= 1 << 4,
222 IGNORE_BELOW
= 1 << 5,
223 IGNORE_NONFOCUS
= 1 << 6,
227 static gboolean
place_nooverlap(ObClient
*c
, gint
*x
, gint
*y
)
233 GSList
*spaces
= NULL
, *sit
, *maxit
;
235 areas
= pick_head(c
);
240 /* try ignoring different things to find empty space */
241 for (ignore
= 0; ignore
< IGNORE_END
&& !ret
; ignore
= (ignore
<< 1) + 1) {
244 /* try all monitors in order of preference */
245 for (i
= 0; i
< screen_num_monitors
&& !ret
; ++i
) {
248 /* add the whole monitor */
249 spaces
= area_add(spaces
, areas
[i
]);
251 /* go thru all the windows */
252 for (it
= client_list
; it
; it
= g_list_next(it
)) {
253 ObClient
*test
= it
->data
;
255 /* should we ignore this client? */
256 if (screen_showing_desktop
) continue;
257 if (c
== test
) continue;
258 if (test
->iconic
) continue;
259 if (c
->desktop
!= DESKTOP_ALL
) {
260 if (test
->desktop
!= c
->desktop
&&
261 test
->desktop
!= DESKTOP_ALL
) continue;
263 if (test
->desktop
!= screen_desktop
&&
264 test
->desktop
!= DESKTOP_ALL
) continue;
266 if (test
->type
== OB_CLIENT_TYPE_SPLASH
||
267 test
->type
== OB_CLIENT_TYPE_DESKTOP
) continue;
270 if ((ignore
& IGNORE_FULLSCREEN
) &&
271 test
->fullscreen
) continue;
272 if ((ignore
& IGNORE_MAXIMIZED
) &&
273 test
->max_horz
&& test
->max_vert
) continue;
274 if ((ignore
& IGNORE_MENUTOOL
) &&
275 (test
->type
== OB_CLIENT_TYPE_MENU
||
276 test
->type
== OB_CLIENT_TYPE_TOOLBAR
) &&
277 client_has_parent(c
)) continue;
278 if ((ignore
& IGNORE_SHADED
) &&
279 test
->shaded
) continue;
280 if ((ignore
& IGNORE_NONGROUP
) &&
281 client_has_group_siblings(test
) &&
282 test
->group
!= c
->group
) continue;
283 if ((ignore
& IGNORE_BELOW
) &&
284 test
->layer
< c
->layer
) continue;
285 if ((ignore
& IGNORE_NONFOCUS
) &&
286 focus_client
!= test
) continue;
288 /* don't ignore this window, so remove it from the available
290 spaces
= area_remove(spaces
, &test
->frame
->area
);
293 for (sit
= spaces
; sit
; sit
= g_slist_next(sit
)) {
296 if (r
->width
>= c
->frame
->area
.width
&&
297 r
->height
>= c
->frame
->area
.height
&&
306 Rect
*r
= maxit
->data
;
308 /* center it in the area */
309 *x
= r
->x
+ (r
->width
- c
->frame
->area
.width
) / 2;
310 *y
= r
->y
+ (r
->height
- c
->frame
->area
.height
) / 2;
315 g_free(spaces
->data
);
316 spaces
= g_slist_delete_link(spaces
, spaces
);
325 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
331 if (!screen_pointer_pos(&px
, &py
))
333 area
= pick_pointer_head(client
);
337 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
338 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
340 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
341 *x
= MIN(MAX(*x
, l
), r
);
342 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
343 *y
= MIN(MAX(*y
, t
), b
);
348 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
349 ObAppSettings
*settings
)
353 if (!settings
|| (settings
&& !settings
->pos_given
))
356 /* Find which head the pointer is on */
357 if (settings
->monitor
== 0)
358 screen
= pick_pointer_head(client
);
359 else if (settings
->monitor
> 0 &&
360 (guint
)settings
->monitor
<= screen_num_monitors
)
361 screen
= screen_area_monitor(client
->desktop
,
362 (guint
)settings
->monitor
- 1);
365 all
= pick_head(client
);
367 g_free(all
); /* the areas themselves don't need to be freed */
370 if (settings
->center_x
)
371 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
372 else if (settings
->opposite_x
)
373 *x
= screen
->x
+ screen
->width
- client
->frame
->area
.width
-
374 settings
->position
.x
;
376 *x
= screen
->x
+ settings
->position
.x
;
378 if (settings
->center_y
)
379 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
380 else if (settings
->opposite_y
)
381 *y
= screen
->y
+ screen
->height
- client
->frame
->area
.height
-
382 settings
->position
.y
;
384 *y
= screen
->y
+ settings
->position
.y
;
389 static gboolean
place_transient_splash(ObClient
*client
, gint
*x
, gint
*y
)
391 if (client
->type
== OB_CLIENT_TYPE_DIALOG
) {
393 gboolean first
= TRUE
;
395 for (it
= client
->parents
; it
; it
= g_slist_next(it
)) {
396 ObClient
*m
= it
->data
;
399 l
= RECT_LEFT(m
->frame
->area
);
400 t
= RECT_TOP(m
->frame
->area
);
401 r
= RECT_RIGHT(m
->frame
->area
);
402 b
= RECT_BOTTOM(m
->frame
->area
);
405 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
406 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
407 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
408 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
412 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
413 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
419 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
420 client
->type
== OB_CLIENT_TYPE_SPLASH
)
424 areas
= pick_head(client
);
426 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
427 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
436 /* Return TRUE if we want client.c to enforce on-screen-keeping */
437 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
438 ObAppSettings
*settings
)
442 if (client
->positioned
)
445 /* try a number of methods */
446 ret
= place_transient_splash(client
, x
, y
) ||
447 place_per_app_setting(client
, x
, y
, settings
) ||
448 (config_place_policy
== OB_PLACE_POLICY_MOUSE
&&
449 place_under_mouse(client
, x
, y
)) ||
450 place_nooverlap(client
, x
, y
) ||
451 place_under_mouse(client
, x
, y
) ||
452 place_random(client
, x
, y
);
455 /* get where the client should be */
456 frame_frame_gravity(client
->frame
, x
, y
,
457 client
->area
.width
, client
->area
.height
);
This page took 0.054106 seconds and 4 git commands to generate.