1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 stacking.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 GList *stacking_list = NULL;
32 /*! When true, stacking changes will not be reflected on the screen. This is
33 to freeze the on-screen stacking order while a window is being temporarily
34 raised during focus cycling */
35 static gboolean pause_changes = FALSE;
37 void stacking_set_list(void)
39 Window *windows = NULL;
43 /* on shutdown, don't update the properties, so that we can read it back
44 in on startup and re-stack the windows as they were before we shut down
46 if (ob_state() == OB_STATE_EXITING) return;
48 /* create an array of the window ids (from bottom to top,
51 windows = g_new(Window, g_list_length(stacking_list));
52 for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
53 if (WINDOW_IS_CLIENT(it->data))
54 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
58 PROP_SETA32(RootWindow(ob_display, ob_screen),
59 net_client_list_stacking, window, (gulong*)windows, i);
64 static void do_restack(GList *wins, GList *before)
72 /* pls only restack stuff in the same layer at a time */
73 for (it = wins; it; it = next) {
74 next = g_list_next(it);
76 g_assert (window_layer(it->data) == window_layer(next->data));
79 g_assert(window_layer(it->data) >= window_layer(before->data));
82 win = g_new(Window, g_list_length(wins) + 1);
84 if (before == stacking_list)
85 win[0] = screen_support_win;
87 win[0] = window_top(g_list_last(stacking_list)->data);
89 win[0] = window_top(g_list_previous(before)->data);
91 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
92 win[i] = window_top(it->data);
93 g_assert(win[i] != None); /* better not call stacking shit before
94 setting your top level window value */
95 stacking_list = g_list_insert_before(stacking_list, before, it->data);
99 /* some debug checking of the stacking list's order */
100 for (it = stacking_list; ; it = next) {
101 next = g_list_next(it);
103 g_assert(window_layer(it->data) >= window_layer(next->data));
108 XRestackWindows(ob_display, win, i);
114 void stacking_temp_raise(ObWindow *window)
120 /* don't use this for internal windows..! it would lower them.. */
121 g_assert(window_layer(window) < OB_STACKING_LAYER_INTERNAL);
123 /* find the window to drop it underneath */
124 win[0] = screen_support_win;
125 for (it = stacking_list; it; it = g_list_next(it)) {
126 ObWindow *w = it->data;
127 if (window_layer(w) >= OB_STACKING_LAYER_INTERNAL)
128 win[0] = window_top(w);
133 win[1] = window_top(window);
134 start = event_start_ignore_all_enters();
135 XRestackWindows(ob_display, win, 2);
136 event_end_ignore_all_enters(start);
138 pause_changes = TRUE;
141 void stacking_restore(void)
148 win = g_new(Window, g_list_length(stacking_list) + 1);
149 win[0] = screen_support_win;
150 for (i = 1, it = stacking_list; it; ++i, it = g_list_next(it))
151 win[i] = window_top(it->data);
152 start = event_start_ignore_all_enters();
153 XRestackWindows(ob_display, win, i);
154 event_end_ignore_all_enters(start);
157 pause_changes = FALSE;
160 static void do_raise(GList *wins)
163 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
166 for (it = wins; it; it = g_list_next(it)) {
169 l = window_layer(it->data);
170 layer[l] = g_list_append(layer[l], it->data);
174 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
176 for (; it; it = g_list_next(it)) {
177 /* look for the top of the layer */
178 if (window_layer(it->data) <= (ObStackingLayer) i)
181 do_restack(layer[i], it);
182 g_list_free(layer[i]);
187 static void do_lower(GList *wins)
190 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
193 for (it = wins; it; it = g_list_next(it)) {
196 l = window_layer(it->data);
197 layer[l] = g_list_append(layer[l], it->data);
201 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
203 for (; it; it = g_list_next(it)) {
204 /* look for the top of the next layer down */
205 if (window_layer(it->data) < (ObStackingLayer) i)
208 do_restack(layer[i], it);
209 g_list_free(layer[i]);
214 static void restack_windows(ObClient *selected, gboolean raise)
216 GList *it, *last, *below, *above, *next;
219 GList *group_modals = NULL;
220 GList *group_trans = NULL;
221 GList *modals = NULL;
227 /* if a window is modal for another single window, then raise it to the
228 top too, the same is done with the focus order */
229 while (selected->modal && (p = client_direct_parent(selected)))
233 /* remove first so we can't run into ourself */
234 it = g_list_find(stacking_list, selected);
236 stacking_list = g_list_delete_link(stacking_list, it);
238 /* go from the bottom of the stacking list up. don't move any other windows
239 when lowering, we call this for each window independently */
241 for (it = g_list_last(stacking_list); it; it = next) {
242 next = g_list_previous(it);
244 if (WINDOW_IS_CLIENT(it->data)) {
245 ObClient *ch = it->data;
247 /* only move windows in the same stacking layer */
248 if (ch->layer == selected->layer &&
249 client_search_transient(selected, ch))
251 if (client_is_direct_child(selected, ch)) {
253 modals = g_list_prepend(modals, ch);
255 trans = g_list_prepend(trans, ch);
259 group_modals = g_list_prepend(group_modals, ch);
261 group_trans = g_list_prepend(group_trans, ch);
263 stacking_list = g_list_delete_link(stacking_list, it);
269 /* put transients of the selected window right above it */
270 wins = g_list_concat(modals, trans);
271 wins = g_list_append(wins, selected);
273 /* if selected window is transient for group then raise it above others */
274 if (selected->transient_for_group) {
275 /* if it's modal, raise it above those also */
276 if (selected->modal) {
277 wins = g_list_concat(wins, group_modals);
280 wins = g_list_concat(wins, group_trans);
284 /* find where to put the selected window, start from bottom of list,
285 this is the window below everything we are re-adding to the list */
287 for (it = g_list_last(stacking_list); it; it = g_list_previous(it))
289 if (window_layer(it->data) < selected->layer) {
293 /* if lowering, stop at the beginning of the layer */
296 /* if raising, stop at the end of the layer */
297 if (window_layer(it->data) > selected->layer)
303 /* save this position in the stacking list */
306 /* find where to put the group transients, start from the top of list */
307 for (it = stacking_list; it; it = g_list_next(it)) {
308 /* skip past higher layers */
309 if (window_layer(it->data) > selected->layer)
311 /* if we reach the end of the layer (how?) then don't go further */
312 if (window_layer(it->data) < selected->layer)
314 /* stop when we reach the first window in the group */
315 if (WINDOW_IS_CLIENT(it->data)) {
316 ObClient *c = it->data;
317 if (c->group == selected->group)
320 /* if we don't hit any other group members, stop here because this
321 is where we are putting the selected window (and its children) */
326 /* save this position, this is the top of the group of windows between the
327 group transient ones we're restacking and the others up above that we're
330 we actually want to save 1 position _above_ that, for for loops to work
331 nicely, so move back one position in the list while saving it
333 above = it ? g_list_previous(it) : g_list_last(stacking_list);
335 /* put the windows inside the gap to the other windows we're stacking
336 into the restacking list, go from the bottom up so that we can use
338 if (below) it = g_list_previous(below);
339 else it = g_list_last(stacking_list);
340 for (; it != above; it = next) {
341 next = g_list_previous(it);
342 wins = g_list_prepend(wins, it->data);
343 stacking_list = g_list_delete_link(stacking_list, it);
346 /* group transients go above the rest of the stuff acquired to now */
347 wins = g_list_concat(group_trans, wins);
348 /* group modals go on the very top */
349 wins = g_list_concat(group_modals, wins);
351 do_restack(wins, below);
354 /* lower our parents after us, so they go below us */
355 if (!raise && selected->parents) {
356 GSList *parents_copy, *sit;
357 GSList *reorder = NULL;
359 parents_copy = g_slist_copy(selected->parents);
361 /* go thru stacking list backwards so we can use g_slist_prepend */
362 for (it = g_list_last(stacking_list); it && parents_copy;
363 it = g_list_previous(it))
364 if ((sit = g_slist_find(parents_copy, it->data))) {
365 reorder = g_slist_prepend(reorder, sit->data);
366 parents_copy = g_slist_delete_link(parents_copy, sit);
368 g_assert(parents_copy == NULL);
370 /* call restack for each of these to lower them */
371 for (sit = reorder; sit; sit = g_slist_next(sit))
372 restack_windows(sit->data, raise);
376 void stacking_raise(ObWindow *window)
378 if (WINDOW_IS_CLIENT(window)) {
380 selected = WINDOW_AS_CLIENT(window);
381 restack_windows(selected, TRUE);
384 wins = g_list_append(NULL, window);
385 stacking_list = g_list_remove(stacking_list, window);
391 void stacking_lower(ObWindow *window)
393 if (WINDOW_IS_CLIENT(window)) {
395 selected = WINDOW_AS_CLIENT(window);
396 restack_windows(selected, FALSE);
399 wins = g_list_append(NULL, window);
400 stacking_list = g_list_remove(stacking_list, window);
406 void stacking_below(ObWindow *window, ObWindow *below)
408 GList *wins, *before;
410 if (window_layer(window) != window_layer(below))
413 wins = g_list_append(NULL, window);
414 stacking_list = g_list_remove(stacking_list, window);
415 before = g_list_next(g_list_find(stacking_list, below));
416 do_restack(wins, before);
420 void stacking_add(ObWindow *win)
422 g_assert(screen_support_win != None); /* make sure I dont break this in the
425 stacking_list = g_list_append(stacking_list, win);
429 static GList *find_highest_relative(ObClient *client)
433 if (client->parents) {
437 /* get all top level relatives of this client */
438 top = client_search_all_top_parents_layer(client);
440 /* go from the top of the stacking order down */
441 for (it = stacking_list; !ret && it; it = g_list_next(it)) {
442 if (WINDOW_IS_CLIENT(it->data)) {
443 ObClient *c = it->data;
444 /* only look at windows in the same layer and that are
446 if (c->layer == client->layer &&
448 (c->desktop == client->desktop ||
449 c->desktop == DESKTOP_ALL ||
450 client->desktop == DESKTOP_ALL))
454 /* go through each top level parent and see it this window
455 is related to them */
456 for (sit = top; !ret && sit; sit = g_slist_next(sit)) {
457 ObClient *topc = sit->data;
459 /* are they related ? */
460 if (topc == c || client_search_transient(topc, c))
470 void stacking_add_nonintrusive(ObWindow *win)
473 GList *it_below = NULL; /* this client will be below us */
477 if (!WINDOW_IS_CLIENT(win)) {
478 stacking_add(win); /* no special rules for others */
482 client = WINDOW_AS_CLIENT(win);
484 /* insert above its highest parent (or its highest child !) */
485 it_below = find_highest_relative(client);
488 /* nothing to put it directly above, so try find the focused client
489 to put it underneath it */
490 if (focus_client && client != focus_client &&
491 focus_client->layer == client->layer)
493 it_below = g_list_find(stacking_list, focus_client);
494 /* this can give NULL, but it means the focused window is on the
495 bottom of the stacking order, so go to the bottom in that case,
497 it_below = g_list_next(it_below);
500 /* There is no window to put this directly above, so put it at the
501 top, so you know it is there.
503 It used to do this only if the window was focused and lower
506 We also put it at the top not the bottom to fix a bug with
507 fullscreen windows. When focusLast is off and followsMouse is
508 on, when you switch desktops, the fullscreen window loses
509 focus and goes into its lower layer. If this puts it at the
510 bottom then when you come back to the desktop, the window is
511 at the bottom and won't get focus back.
513 it_below = stacking_list;
517 /* make sure it's not in the wrong layer though ! */
518 for (; it_below; it_below = g_list_next(it_below)) {
519 /* stop when the window is not in a higher layer than the window
520 it is going above (it_below) */
521 if (client->layer >= window_layer(it_below->data))
524 for (; it_below != stacking_list; it_below = it_above) {
525 /* stop when the window is not in a lower layer than the
526 window it is going under (it_above) */
527 it_above = it_below ?
528 g_list_previous(it_below) : g_list_last(stacking_list);
529 if (client->layer <= window_layer(it_above->data))
533 wins = g_list_append(NULL, win);
534 do_restack(wins, it_below);
538 /*! Returns TRUE if client is occluded by the sibling. If sibling is NULL it
539 tries against all other clients.
541 static gboolean stacking_occluded(ObClient *client, ObClient *sibling)
544 gboolean occluded = FALSE;
545 gboolean found = FALSE;
547 /* no need for any looping in this case */
548 if (sibling && client->layer != sibling->layer)
551 for (it = stacking_list; it;
552 it = (found ? g_list_previous(it) :g_list_next(it)))
553 if (WINDOW_IS_CLIENT(it->data)) {
554 ObClient *c = it->data;
555 if (found && !c->iconic &&
556 (c->desktop == DESKTOP_ALL || client->desktop == DESKTOP_ALL ||
557 c->desktop == client->desktop) &&
558 !client_search_transient(client, c))
560 if (RECT_INTERSECTS_RECT(c->frame->area, client->frame->area))
562 if (sibling != NULL) {
568 else if (c->layer == client->layer) {
572 else if (c->layer > client->layer)
573 break; /* we past its layer */
576 else if (c == client)
582 /*! Returns TRUE if client occludes the sibling. If sibling is NULL it tries
583 against all other clients.
585 static gboolean stacking_occludes(ObClient *client, ObClient *sibling)
588 gboolean occludes = FALSE;
589 gboolean found = FALSE;
591 /* no need for any looping in this case */
592 if (sibling && client->layer != sibling->layer)
595 for (it = stacking_list; it; it = g_list_next(it))
596 if (WINDOW_IS_CLIENT(it->data)) {
597 ObClient *c = it->data;
598 if (found && !c->iconic &&
599 (c->desktop == DESKTOP_ALL || client->desktop == DESKTOP_ALL ||
600 c->desktop == client->desktop) &&
601 !client_search_transient(c, client))
603 if (RECT_INTERSECTS_RECT(c->frame->area, client->frame->area))
605 if (sibling != NULL) {
611 else if (c->layer == client->layer) {
615 else if (c->layer < client->layer)
616 break; /* we past its layer */
619 else if (c == client)
625 gboolean stacking_restack_request(ObClient *client, ObClient *sibling,
628 gboolean ret = FALSE;
630 if (sibling && ((client->desktop != sibling->desktop &&
631 client->desktop != DESKTOP_ALL &&
632 sibling->desktop != DESKTOP_ALL) ||
635 ob_debug("Setting restack sibling to NULL, they are not on the same "
636 "desktop or it is iconified\n");
642 ob_debug("Restack request Below for client %s sibling %s\n",
643 client->title, sibling ? sibling->title : "(all)");
645 stacking_lower(CLIENT_AS_WINDOW(client));
649 ob_debug("Restack request BottomIf for client %s sibling "
651 client->title, sibling ? sibling->title : "(all)");
652 /* if this client occludes sibling (or anything if NULL), then
653 lower it to the bottom */
654 if (stacking_occludes(client, sibling)) {
655 stacking_lower(CLIENT_AS_WINDOW(client));
660 ob_debug("Restack request Above for client %s sibling %s\n",
661 client->title, sibling ? sibling->title : "(all)");
662 stacking_raise(CLIENT_AS_WINDOW(client));
666 ob_debug("Restack request TopIf for client %s sibling %s\n",
667 client->title, sibling ? sibling->title : "(all)");
668 if (stacking_occluded(client, sibling)) {
669 stacking_raise(CLIENT_AS_WINDOW(client));
674 ob_debug("Restack request Opposite for client %s sibling "
676 client->title, sibling ? sibling->title : "(all)");
677 if (stacking_occluded(client, sibling)) {
678 stacking_raise(CLIENT_AS_WINDOW(client));
681 else if (stacking_occludes(client, sibling)) {
682 stacking_lower(CLIENT_AS_WINDOW(client));