]>
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 */
329 *x
= r
->x
+ (r
->width
- c
->frame
->area
.width
) / 2;
330 *y
= r
->y
+ (r
->height
- c
->frame
->area
.height
) / 2;
335 g_free(spaces
->data
);
336 spaces
= g_slist_delete_link(spaces
, spaces
);
341 for (i
= 0; i
< screen_num_monitors
; ++i
)
347 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
353 if (!screen_pointer_pos(&px
, &py
))
355 area
= pick_pointer_head(client
);
359 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
360 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
362 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
363 *x
= MIN(MAX(*x
, l
), r
);
364 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
365 *y
= MIN(MAX(*y
, t
), b
);
370 static gboolean
place_per_app_setting(ObClient
*client
, gint
*x
, gint
*y
,
371 ObAppSettings
*settings
)
375 if (!settings
|| (settings
&& !settings
->pos_given
))
378 /* Find which head the pointer is on */
379 if (settings
->monitor
== 0)
380 screen
= pick_pointer_head(client
);
381 else if (settings
->monitor
> 0 &&
382 (guint
)settings
->monitor
<= screen_num_monitors
)
383 screen
= screen_area(client
->desktop
, (guint
)settings
->monitor
- 1,
389 areas
= pick_head(client
);
392 for (i
= 0; i
< screen_num_monitors
; ++i
)
397 if (settings
->center_x
)
398 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
399 else if (settings
->opposite_x
)
400 *x
= screen
->x
+ screen
->width
- client
->frame
->area
.width
-
401 settings
->position
.x
;
403 *x
= screen
->x
+ settings
->position
.x
;
405 if (settings
->center_y
)
406 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
407 else if (settings
->opposite_y
)
408 *y
= screen
->y
+ screen
->height
- client
->frame
->area
.height
-
409 settings
->position
.y
;
411 *y
= screen
->y
+ settings
->position
.y
;
416 static gboolean
place_transient_splash(ObClient
*client
, gint
*x
, gint
*y
)
418 if (client
->type
== OB_CLIENT_TYPE_DIALOG
) {
420 gboolean first
= TRUE
;
422 for (it
= client
->parents
; it
; it
= g_slist_next(it
)) {
423 ObClient
*m
= it
->data
;
426 l
= RECT_LEFT(m
->frame
->area
);
427 t
= RECT_TOP(m
->frame
->area
);
428 r
= RECT_RIGHT(m
->frame
->area
);
429 b
= RECT_BOTTOM(m
->frame
->area
);
432 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
433 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
434 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
435 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
439 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
440 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
446 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
447 client
->type
== OB_CLIENT_TYPE_SPLASH
)
452 areas
= pick_head(client
);
454 *x
= (areas
[0]->width
- client
->frame
->area
.width
) / 2 + areas
[0]->x
;
455 *y
= (areas
[0]->height
- client
->frame
->area
.height
) / 2 + areas
[0]->y
;
457 for (i
= 0; i
< screen_num_monitors
; ++i
)
466 /* Return TRUE if we want client.c to enforce on-screen-keeping */
467 gboolean
place_client(ObClient
*client
, gint
*x
, gint
*y
,
468 ObAppSettings
*settings
)
472 if (client
->positioned
)
475 /* try a number of methods */
476 ret
= place_transient_splash(client
, x
, y
) ||
477 place_per_app_setting(client
, x
, y
, settings
) ||
478 (config_place_policy
== OB_PLACE_POLICY_MOUSE
&&
479 place_under_mouse(client
, x
, y
)) ||
480 place_nooverlap(client
, x
, y
) ||
481 place_under_mouse(client
, x
, y
) ||
482 place_random(client
, x
, y
);
485 /* get where the client should be */
486 frame_frame_gravity(client
->frame
, x
, y
,
487 client
->area
.width
, client
->area
.height
);
This page took 0.061081 seconds and 4 git commands to generate.