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 #include "place_overlap.h"
32 static Rect
*pick_pointer_head(ObClient
*c
)
34 return screen_area(c
->desktop
, screen_monitor_pointer(), NULL
);
37 /* use the following priority lists for pick_head()
39 When a window is being placed in the FOREGROUND, use a monitor chosen in
42 2. same monitor as parent
43 3. primary monitor if placement=PRIMARY
44 active monitor if placement=ACTIVE
45 pointer monitor if placement=MOUSE
47 5. other monitors where the window has group members on the same desktop
48 6. other monitors where the window has group members on other desktops
51 When a window is being placed in the BACKGROUND, use a monitor chosen in the
54 2. same monitor as parent
55 3. other monitors where the window has group members on the same desktop
56 3a. primary monitor in this set
57 3b. other monitors in this set
58 4. other monitors where the window has group members on other desktops
59 4a. primary monitor in this set
60 4b. other monitors in this set
62 5a. primary monitor in this set
63 5b. other monitors in this set
66 /*! One for each possible head, used to sort them in order of precedence. */
72 /*! Flags for ObPlaceHead */
74 HEAD_PARENT
= 1 << 0, /* parent's monitor */
75 HEAD_PLACED
= 1 << 1, /* chosen monitor by placement */
76 HEAD_PRIMARY
= 1 << 2, /* primary monitor */
77 HEAD_GROUP_DESK
= 1 << 3, /* has a group member on the same desktop */
78 HEAD_GROUP
= 1 << 4, /* has a group member on another desktop */
79 HEAD_PERAPP
= 1 << 5, /* chosen by per-app settings */
82 gint
cmp_foreground(const void *a
, const void *b
)
84 const ObPlaceHead
*h1
= a
;
85 const ObPlaceHead
*h2
= b
;
88 if (h1
->monitor
== h2
->monitor
) return 0;
90 if (h1
->flags
& HEAD_PERAPP
) --i
;
91 if (h2
->flags
& HEAD_PERAPP
) ++i
;
94 if (h1
->flags
& HEAD_PARENT
) --i
;
95 if (h2
->flags
& HEAD_PARENT
) ++i
;
98 if (h1
->flags
& HEAD_PLACED
) --i
;
99 if (h2
->flags
& HEAD_PLACED
) ++i
;
102 if (h1
->flags
& HEAD_PRIMARY
) --i
;
103 if (h2
->flags
& HEAD_PRIMARY
) ++i
;
106 if (h1
->flags
& HEAD_GROUP_DESK
) --i
;
107 if (h2
->flags
& HEAD_GROUP_DESK
) ++i
;
110 if (h1
->flags
& HEAD_GROUP
) --i
;
111 if (h2
->flags
& HEAD_GROUP
) ++i
;
114 return h1
->monitor
- h2
->monitor
;
117 gint
cmp_background(const void *a
, const void *b
)
119 const ObPlaceHead
*h1
= a
;
120 const ObPlaceHead
*h2
= b
;
123 if (h1
->monitor
== h2
->monitor
) return 0;
125 if (h1
->flags
& HEAD_PERAPP
) --i
;
126 if (h2
->flags
& HEAD_PERAPP
) ++i
;
129 if (h1
->flags
& HEAD_PARENT
) --i
;
130 if (h2
->flags
& HEAD_PARENT
) ++i
;
133 if (h1
->flags
& HEAD_GROUP_DESK
|| h2
->flags
& HEAD_GROUP_DESK
) {
134 if (h1
->flags
& HEAD_GROUP_DESK
) --i
;
135 if (h2
->flags
& HEAD_GROUP_DESK
) ++i
;
137 if (h1
->flags
& HEAD_PRIMARY
) --i
;
138 if (h2
->flags
& HEAD_PRIMARY
) ++i
;
142 if (h1
->flags
& HEAD_GROUP
|| h2
->flags
& HEAD_GROUP
) {
143 if (h1
->flags
& HEAD_GROUP
) --i
;
144 if (h2
->flags
& HEAD_GROUP
) ++i
;
146 if (h1
->flags
& HEAD_PRIMARY
) --i
;
147 if (h2
->flags
& HEAD_PRIMARY
) ++i
;
151 if (h1
->flags
& HEAD_PRIMARY
) --i
;
152 if (h2
->flags
& HEAD_PRIMARY
) ++i
;
155 return h1
->monitor
- h2
->monitor
;
158 /*! Pick a monitor to place a window on. */
159 static Rect
*pick_head(ObClient
*c
, gboolean foreground
,
160 ObAppSettings
*settings
)
168 choice
= g_new(ObPlaceHead
, screen_num_monitors
);
169 for (i
= 0; i
< screen_num_monitors
; ++i
) {
170 choice
[i
].monitor
= i
;
174 /* find monitors with group members */
176 for (it
= c
->group
->members
; it
; it
= g_slist_next(it
)) {
177 ObClient
*itc
= it
->data
;
179 guint m
= client_monitor(itc
);
181 if (m
< screen_num_monitors
) {
182 if (screen_compare_desktops(itc
->desktop
, c
->desktop
))
183 choice
[m
].flags
|= HEAD_GROUP_DESK
;
185 choice
[m
].flags
|= HEAD_GROUP
;
191 i
= screen_monitor_primary(FALSE
);
192 if (i
< screen_num_monitors
) {
193 choice
[i
].flags
|= HEAD_PRIMARY
;
194 if (config_place_monitor
== OB_PLACE_MONITOR_PRIMARY
)
195 choice
[i
].flags
|= HEAD_PLACED
;
197 settings
->monitor_type
== OB_PLACE_MONITOR_PRIMARY
)
198 choice
[i
].flags
|= HEAD_PERAPP
;
201 i
= screen_monitor_active();
202 if (i
< screen_num_monitors
) {
203 if (config_place_monitor
== OB_PLACE_MONITOR_ACTIVE
)
204 choice
[i
].flags
|= HEAD_PLACED
;
206 settings
->monitor_type
== OB_PLACE_MONITOR_ACTIVE
)
207 choice
[i
].flags
|= HEAD_PERAPP
;
210 i
= screen_monitor_pointer();
211 if (i
< screen_num_monitors
) {
212 if (config_place_monitor
== OB_PLACE_MONITOR_MOUSE
)
213 choice
[i
].flags
|= HEAD_PLACED
;
215 settings
->monitor_type
== OB_PLACE_MONITOR_MOUSE
)
216 choice
[i
].flags
|= HEAD_PERAPP
;
220 i
= settings
->monitor
- 1;
221 if (i
< screen_num_monitors
)
222 choice
[i
].flags
|= HEAD_PERAPP
;
225 /* direct parent takes highest precedence */
226 if ((p
= client_direct_parent(c
))) {
227 i
= client_monitor(p
);
228 if (i
< screen_num_monitors
)
229 choice
[i
].flags
|= HEAD_PARENT
;
232 qsort(choice
, screen_num_monitors
, sizeof(ObPlaceHead
),
233 foreground
? cmp_foreground
: cmp_background
);
235 /* save the areas of the monitors in order of their being chosen */
236 for (i
= 0; i
< screen_num_monitors
; ++i
)
238 ob_debug("placement choice %d is monitor %d", i
, choice
[i
].monitor
);
239 if (choice
[i
].flags
& HEAD_PARENT
)
240 ob_debug(" - parent on monitor");
241 if (choice
[i
].flags
& HEAD_PLACED
)
242 ob_debug(" - placement choice");
243 if (choice
[i
].flags
& HEAD_PRIMARY
)
244 ob_debug(" - primary monitor");
245 if (choice
[i
].flags
& HEAD_GROUP_DESK
)
246 ob_debug(" - group on same desktop");
247 if (choice
[i
].flags
& HEAD_GROUP
)
248 ob_debug(" - group on other desktop");
251 area
= screen_area(c
->desktop
, choice
[0].monitor
, NULL
);
255 /* return the area for the chosen monitor */
259 static gboolean
place_under_mouse(ObClient
*client
, gint
*x
, gint
*y
)
265 ob_debug("placing under mouse");
267 if (!screen_pointer_pos(&px
, &py
))
269 area
= pick_pointer_head(client
);
273 r
= area
->x
+ area
->width
- client
->frame
->area
.width
;
274 b
= area
->y
+ area
->height
- client
->frame
->area
.height
;
276 *x
= px
- client
->area
.width
/ 2 - client
->frame
->size
.left
;
277 *x
= MIN(MAX(*x
, l
), r
);
278 *y
= py
- client
->area
.height
/ 2 - client
->frame
->size
.top
;
279 *y
= MIN(MAX(*y
, t
), b
);
281 g_slice_free(Rect
, area
);
286 static gboolean
place_per_app_setting(ObClient
*client
, Rect
*screen
,
288 ObAppSettings
*settings
)
290 if (!settings
|| (settings
&& !settings
->pos_given
))
293 ob_debug("placing by per-app settings");
295 if (settings
->position
.x
.center
)
296 *x
= screen
->x
+ screen
->width
/ 2 - client
->area
.width
/ 2;
297 else if (settings
->position
.x
.opposite
)
298 *x
= screen
->x
+ screen
->width
- client
->frame
->area
.width
-
299 settings
->position
.x
.pos
;
301 *x
= screen
->x
+ settings
->position
.x
.pos
;
302 if (settings
->position
.x
.denom
)
303 *x
= (*x
* screen
->width
) / settings
->position
.x
.denom
;
305 if (settings
->position
.y
.center
)
306 *y
= screen
->y
+ screen
->height
/ 2 - client
->area
.height
/ 2;
307 else if (settings
->position
.y
.opposite
)
308 *y
= screen
->y
+ screen
->height
- client
->frame
->area
.height
-
309 settings
->position
.y
.pos
;
311 *y
= screen
->y
+ settings
->position
.y
.pos
;
312 if (settings
->position
.y
.denom
)
313 *y
= (*y
* screen
->height
) / settings
->position
.y
.denom
;
318 static gboolean
place_transient_splash(ObClient
*client
, Rect
*area
,
321 if (client
->type
== OB_CLIENT_TYPE_DIALOG
) {
323 gboolean first
= TRUE
;
326 ob_debug("placing dialog");
328 for (it
= client
->parents
; it
; it
= g_slist_next(it
)) {
329 ObClient
*m
= it
->data
;
332 l
= RECT_LEFT(m
->frame
->area
);
333 t
= RECT_TOP(m
->frame
->area
);
334 r
= RECT_RIGHT(m
->frame
->area
);
335 b
= RECT_BOTTOM(m
->frame
->area
);
338 l
= MIN(l
, RECT_LEFT(m
->frame
->area
));
339 t
= MIN(t
, RECT_TOP(m
->frame
->area
));
340 r
= MAX(r
, RECT_RIGHT(m
->frame
->area
));
341 b
= MAX(b
, RECT_BOTTOM(m
->frame
->area
));
345 *x
= ((r
+ 1 - l
) - client
->frame
->area
.width
) / 2 + l
;
346 *y
= ((b
+ 1 - t
) - client
->frame
->area
.height
) / 2 + t
;
352 if (client
->type
== OB_CLIENT_TYPE_DIALOG
||
353 client
->type
== OB_CLIENT_TYPE_SPLASH
)
355 ob_debug("placing dialog or splash");
357 *x
= (area
->width
- client
->frame
->area
.width
) / 2 + area
->x
;
358 *y
= (area
->height
- client
->frame
->area
.height
) / 2 + area
->y
;
365 static gboolean
place_least_overlap(ObClient
*c
, Rect
* const head
,
368 /* Assemble the list of windows that could overlap with @c in the user's
370 GSList
* potential_overlap_clients
= NULL
;
371 int n_client_rects
= 0;
373 /* if we're "showing desktop", ignore all existing windows */
374 if (!screen_showing_desktop
) {
376 for (it
= client_list
; it
!= NULL
; it
= g_list_next(it
)) {
377 ObClient
* maybe_client
= (ObClient
*)it
->data
;
378 if (maybe_client
== c
)
380 if (maybe_client
->iconic
)
382 if (!client_occupies_space(maybe_client
))
384 if (c
->desktop
!= DESKTOP_ALL
) {
385 if (maybe_client
->desktop
!= c
->desktop
&&
386 maybe_client
->desktop
!= DESKTOP_ALL
)
389 if (maybe_client
->desktop
!= screen_desktop
&&
390 maybe_client
->desktop
!= DESKTOP_ALL
)
394 potential_overlap_clients
= g_slist_prepend(
395 potential_overlap_clients
, maybe_client
);
399 Rect client_rects
[n_client_rects
];
402 for (it
= potential_overlap_clients
; it
!= NULL
; it
= g_slist_next(it
)) {
403 ObClient
* potential_overlap_client
= (ObClient
*)it
->data
;
404 client_rects
[i
] = potential_overlap_client
->frame
->area
;
407 g_slist_free(potential_overlap_clients
);
411 SIZE_SET(req_size
, c
->frame
->area
.width
, c
->frame
->area
.height
);
412 place_overlap_find_least_placement(client_rects
, n_client_rects
, head
,
420 /*! Return TRUE if openbox chose the position for the window, and FALSE if
421 the application chose it */
422 gboolean
place_client(ObClient
*client
, gboolean foreground
, gint
*x
, gint
*y
,
423 ObAppSettings
*settings
)
428 /* per-app settings override program specified position
429 * but not user specified, unless pos_force is enabled */
430 if (((client
->positioned
& USPosition
) &&
431 !(settings
&& settings
->pos_given
&& settings
->pos_force
)) ||
432 ((client
->positioned
& PPosition
) &&
433 !(settings
&& settings
->pos_given
)))
436 area
= pick_head(client
, foreground
, settings
);
438 /* try a number of methods */
439 ret
= place_per_app_setting(client
, area
, x
, y
, settings
) ||
440 place_transient_splash(client
, area
, x
, y
) ||
441 (config_place_policy
== OB_PLACE_POLICY_MOUSE
&&
442 place_under_mouse(client
, x
, y
)) ||
443 place_least_overlap(client
, area
, x
, y
);
446 g_slice_free(Rect
, area
);
448 /* get where the client should be */
449 frame_frame_gravity(client
->frame
, x
, y
);