]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/stacking.c
dont set focus_client to NULL when sending focus to nothing, let that happen from...
[mikachu/openbox.git] / openbox / stacking.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    stacking.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "prop.h"
22 #include "screen.h"
23 #include "focus.h"
24 #include "client.h"
25 #include "group.h"
26 #include "frame.h"
27 #include "window.h"
28 #include "debug.h"
29
30 GList  *stacking_list = NULL;
31
32 void stacking_set_list()
33 {
34     Window *windows = NULL;
35     GList *it;
36     guint i = 0;
37
38     /* on shutdown, don't update the properties, so that we can read it back
39        in on startup and re-stack the windows as they were before we shut down
40     */
41     if (ob_state() == OB_STATE_EXITING) return;
42
43     /* create an array of the window ids (from bottom to top,
44        reverse order!) */
45     if (stacking_list) {
46         windows = g_new(Window, g_list_length(stacking_list));
47         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
48             if (WINDOW_IS_CLIENT(it->data))
49                 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
50         }
51     }
52
53     PROP_SETA32(RootWindow(ob_display, ob_screen),
54                 net_client_list_stacking, window, (gulong*)windows, i);
55
56     g_free(windows);
57 }
58
59 static void do_restack(GList *wins, GList *before)
60 {
61     GList *it;
62     Window *win;
63     gint i;
64
65 #ifdef DEBUG
66     GList *next;
67     /* pls only restack stuff in the same layer at a time */
68     for (it = wins; it; it = next) {
69         next = g_list_next(it);
70         if (!next) break;
71         g_assert (window_layer(it->data) == window_layer(next->data));
72     }
73     if (before)
74         g_assert(window_layer(it->data) >= window_layer(before->data));
75 #endif
76
77     win = g_new(Window, g_list_length(wins) + 1);
78
79     if (before == stacking_list)
80         win[0] = screen_support_win;
81     else if (!before)
82         win[0] = window_top(g_list_last(stacking_list)->data);
83     else
84         win[0] = window_top(g_list_previous(before)->data);
85
86     for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
87         win[i] = window_top(it->data);
88         g_assert(win[i] != None); /* better not call stacking shit before
89                                      setting your top level window value */
90         stacking_list = g_list_insert_before(stacking_list, before, it->data);
91     }
92
93 #ifdef DEBUG
94     /* some debug checking of the stacking list's order */
95     for (it = stacking_list; ; it = next) {
96         next = g_list_next(it);
97         if (!next) break;
98         g_assert(window_layer(it->data) >= window_layer(next->data));
99     }
100 #endif
101
102     XRestackWindows(ob_display, win, i);
103     g_free(win);
104
105     stacking_set_list();
106 }
107
108 static void do_raise(GList *wins)
109 {
110     GList *it;
111     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
112     gint i;
113
114     for (it = wins; it; it = g_list_next(it)) {
115         ObStackingLayer l;
116
117         l = window_layer(it->data);
118         layer[l] = g_list_append(layer[l], it->data);
119     }
120
121     it = stacking_list;
122     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
123         if (layer[i]) {
124             for (; it; it = g_list_next(it)) {
125                 /* look for the top of the layer */
126                 if (window_layer(it->data) <= (ObStackingLayer) i)
127                     break;
128             }
129             do_restack(layer[i], it);
130             g_list_free(layer[i]);
131         }
132     }
133 }
134
135 static void do_lower(GList *wins)
136 {
137     GList *it;
138     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
139     gint i;
140
141     for (it = wins; it; it = g_list_next(it)) {
142         ObStackingLayer l;
143
144         l = window_layer(it->data);
145         layer[l] = g_list_append(layer[l], it->data);
146     }
147
148     it = stacking_list;
149     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
150         if (layer[i]) {
151             for (; it; it = g_list_next(it)) {
152                 /* look for the top of the next layer down */
153                 if (window_layer(it->data) < (ObStackingLayer) i)
154                     break;
155             }
156             do_restack(layer[i], it);
157             g_list_free(layer[i]);
158         }
159     }
160 }
161
162 static void restack_windows(ObClient *selected, gboolean raise)
163 {
164     GList *it, *last, *below, *above, *next;
165     GList *wins = NULL;
166
167     GList *group_modals = NULL;
168     GList *group_trans = NULL;
169     GList *modals = NULL;
170     GList *trans = NULL;
171
172     if (!raise && selected->transient_for) {
173         GSList *top, *top_it;
174         GSList *top_reorder = NULL;
175         
176         /* if it's a transient lowering, lower its parents so that we can lower
177            this window, or it won't move */
178         top = client_search_all_top_parents_layer(selected);
179
180         /* that is, if it has any parents */
181         if (!(top->data == selected && top->next == NULL)) {
182             /* go thru stacking list backwards so we can use g_slist_prepend */
183             for (it = g_list_last(stacking_list); it && top;
184                  it = g_list_previous(it))
185                 if ((top_it = g_slist_find(top, it->data))) {
186                     top_reorder = g_slist_prepend(top_reorder, top_it->data);
187                     top = g_slist_delete_link(top, top_it);
188                 }
189             g_assert(top == NULL);
190
191             /* call restack for each of these to lower them */
192             for (top_it = top_reorder; top_it; top_it = g_slist_next(top_it))
193                 restack_windows(top_it->data, raise);
194             return;
195         }
196     }
197
198     /* remove first so we can't run into ourself */
199     it = g_list_find(stacking_list, selected);
200     g_assert(it);
201     stacking_list = g_list_delete_link(stacking_list, it);
202
203     /* go from the bottom of the stacking list up */
204     for (it = g_list_last(stacking_list); it; it = next) {
205         next = g_list_previous(it);
206
207         if (WINDOW_IS_CLIENT(it->data)) {
208             ObClient *ch = it->data;
209
210             /* only move windows in the same stacking layer */
211             if (ch->layer == selected->layer &&
212                 client_search_transient(selected, ch))
213             {
214                 if (client_is_direct_child(selected, ch)) {
215                     if (ch->modal)
216                         modals = g_list_prepend(modals, ch);
217                     else
218                         trans = g_list_prepend(trans, ch);
219                 }
220                 else {
221                     if (ch->modal)
222                         group_modals = g_list_prepend(group_modals, ch);
223                     else
224                         group_trans = g_list_prepend(group_trans, ch);
225                 }
226                 stacking_list = g_list_delete_link(stacking_list, it);
227             }
228         }
229     }
230
231     /* put transients of the selected window right above it */
232     wins = g_list_concat(modals, trans);
233     wins = g_list_append(wins, selected);
234
235     /* if selected window is transient for group then raise it above others */
236     if (selected->transient_for == OB_TRAN_GROUP) {
237         /* if it's modal, raise it above those also */
238         if (selected->modal) {
239             wins = g_list_concat(wins, group_modals);
240             group_modals = NULL;
241         }
242         wins = g_list_concat(wins, group_trans);
243         group_trans = NULL;
244     }
245
246     /* find where to put the selected window, start from bottom of list,
247        this is the window below everything we are re-adding to the list */
248     last = NULL;
249     for (it = g_list_last(stacking_list); it; it = g_list_previous(it))
250     {
251         if (window_layer(it->data) < selected->layer) {
252             last = it;
253             continue;
254         }
255         /* if lowering, stop at the beginning of the layer */
256         if (!raise)
257             break;
258         /* if raising, stop at the end of the layer */
259         if (window_layer(it->data) > selected->layer)
260             break;
261
262         last = it;
263     }
264
265     /* save this position in the stacking list */
266     below = last;
267
268     /* find where to put the group transients, start from the top of list */
269     for (it = stacking_list; it; it = g_list_next(it)) {
270         /* skip past higher layers */
271         if (window_layer(it->data) > selected->layer)
272             continue;
273         /* if we reach the end of the layer (how?) then don't go further */
274         if (window_layer(it->data) < selected->layer)
275             break;
276         /* stop when we reach the first window in the group */
277         if (WINDOW_IS_CLIENT(it->data)) {
278             ObClient *c = it->data;
279             if (c->group == selected->group)
280                 break;
281         }
282         /* if we don't hit any other group members, stop here because this
283            is where we are putting the selected window (and its children) */
284         if (it == below)
285             break;
286     }
287
288     /* save this position, this is the top of the group of windows between the
289        group transient ones we're restacking and the others up above that we're
290        restacking
291
292        we actually want to save 1 position _above_ that, for for loops to work
293        nicely, so move back one position in the list while saving it
294     */
295     above = it ? g_list_previous(it) : g_list_last(stacking_list);
296
297     /* put the windows inside the gap to the other windows we're stacking
298        into the restacking list, go from the bottom up so that we can use
299        g_list_prepend */
300     if (below) it = g_list_previous(below);
301     else       it = g_list_last(stacking_list);
302     for (; it != above; it = next) {
303         next = g_list_previous(it);
304         wins = g_list_prepend(wins, it->data);
305         stacking_list = g_list_delete_link(stacking_list, it);
306     }
307
308     /* group transients go above the rest of the stuff acquired to now */
309     wins = g_list_concat(group_trans, wins);
310     /* group modals go on the very top */
311     wins = g_list_concat(group_modals, wins);
312
313     do_restack(wins, below);
314     g_list_free(wins);
315 }
316
317 void stacking_raise(ObWindow *window)
318 {
319     if (WINDOW_IS_CLIENT(window)) {
320         ObClient *selected;
321         selected = WINDOW_AS_CLIENT(window);
322         restack_windows(selected, TRUE);
323     } else {
324         GList *wins;
325         wins = g_list_append(NULL, window);
326         stacking_list = g_list_remove(stacking_list, window);
327         do_raise(wins);
328         g_list_free(wins);
329     }
330 }
331
332 void stacking_lower(ObWindow *window)
333 {
334     if (WINDOW_IS_CLIENT(window)) {
335         ObClient *selected;
336         selected = WINDOW_AS_CLIENT(window);
337         restack_windows(selected, FALSE);
338     } else {
339         GList *wins;
340         wins = g_list_append(NULL, window);
341         stacking_list = g_list_remove(stacking_list, window);
342         do_lower(wins);
343         g_list_free(wins);
344     }
345 }
346
347 void stacking_below(ObWindow *window, ObWindow *below)
348 {
349     GList *wins, *before;
350
351     if (window_layer(window) != window_layer(below))
352         return;
353
354     wins = g_list_append(NULL, window);
355     stacking_list = g_list_remove(stacking_list, window);
356     before = g_list_next(g_list_find(stacking_list, below));
357     do_restack(wins, before);
358     g_list_free(wins);
359 }
360
361 void stacking_add(ObWindow *win)
362 {
363     g_assert(screen_support_win != None); /* make sure I dont break this in the
364                                              future */
365
366     stacking_list = g_list_append(stacking_list, win);
367     stacking_raise(win);
368 }
369
370 static GList *find_highest_relative(ObClient *client)
371 {    
372     GList *ret = NULL;
373
374     if (client->transient_for) {
375         GList *it;
376         GSList *top;
377
378         /* get all top level relatives of this client */
379         top = client_search_all_top_parents_layer(client);
380
381         /* go from the top of the stacking order down */
382         for (it = stacking_list; !ret && it; it = g_list_next(it)) {
383             if (WINDOW_IS_CLIENT(it->data)) {
384                 ObClient *c = it->data;
385                 /* only look at windows in the same layer */
386                 if (c->layer == client->layer) {
387                     GSList *sit;
388
389                     /* go through each top level parent and see it this window
390                        is related to them */
391                     for (sit = top; !ret && sit; sit = g_slist_next(sit)) {
392                         ObClient *topc = sit->data;
393
394                         /* are they related ? */
395                         if (topc == c || client_search_transient(topc, c))
396                             ret = it;
397                     }
398                 }
399             }
400         }
401     }
402     return ret;
403 }
404
405 void stacking_add_nonintrusive(ObWindow *win)
406 {
407     ObClient *client;
408     GList *it_below = NULL;
409
410     if (!WINDOW_IS_CLIENT(win)) {
411         stacking_add(win); /* no special rules for others */
412         return;
413     }
414
415     client = WINDOW_AS_CLIENT(win);
416
417     /* insert above its highest parent (or its highest child !) */
418     it_below = find_highest_relative(client);
419
420     if (!it_below && client != focus_client) {
421         /* nothing to put it directly above, so try find the focused client to
422            put it underneath it */
423         if (focus_client && focus_client->layer == client->layer) {
424             if ((it_below = g_list_find(stacking_list, focus_client)))
425                 it_below = it_below->next;
426         }
427     }
428     if (!it_below) {
429         /* There is no window to put this directly above, so put it at the
430            top, so you know it is there.
431
432            It used to do this only if the window was focused and lower
433            it otherwise.
434
435            We also put it at the top not the bottom to fix a bug with
436            fullscreen windows. When focusLast is off and followsMouse is
437            on, when you switch desktops, the fullscreen window loses
438            focus and goes into its lower layer. If this puts it at the
439            bottom then when you come back to the desktop, the window is
440            at the bottom and won't get focus back.
441         */
442         stacking_list = g_list_append(stacking_list, win);
443         stacking_raise(win);
444     } else {
445         /* make sure it's not in the wrong layer though ! */
446         for (; it_below; it_below = g_list_next(it_below))
447         {
448             /* stop when the window is not in a higher layer than the window
449                it is going above (it_below) */
450             if (client->layer >= window_layer(it_below->data))
451                 break;
452         }
453         for (; it_below != stacking_list;
454              it_below = g_list_previous(it_below))
455         {
456             /* stop when the window is not in a lower layer than the
457                window it is going under (it_above) */
458             GList *it_above = g_list_previous(it_below);
459             if (client->layer <= window_layer(it_above->data))
460                 break;
461         }
462
463         GList *wins = g_list_append(NULL, win);
464         do_restack(wins, it_below);
465         g_list_free(wins);
466     }
467 }
468
469 /*! Returns TRUE if client is occluded by the sibling. If sibling is NULL it
470   tries against all other clients.
471 */
472 static gboolean stacking_occluded(ObClient *client, ObClient *sibling)
473 {
474     GList *it;
475     gboolean occluded = FALSE;
476     gboolean found = FALSE;
477
478     /* no need for any looping in this case */
479     if (sibling && client->layer != sibling->layer)
480         return occluded;
481
482     for (it = stacking_list; it;
483          it = (found ? g_list_previous(it) :g_list_next(it)))
484         if (WINDOW_IS_CLIENT(it->data)) {
485             ObClient *c = it->data;
486             if (found) {
487                 if (RECT_INTERSECTS_RECT(c->frame->area, client->frame->area))
488                 {
489                     if (sibling != NULL) {
490                         if (c == sibling) {
491                             occluded = TRUE;
492                             break;
493                         }
494                     }
495                     else if (c->layer == client->layer) {
496                         occluded = TRUE;
497                         break;
498                     }
499                     else if (c->layer > client->layer)
500                         break; /* we past its layer */
501                 }
502             }
503             else if (c == client)
504                 found = TRUE;
505         }
506     return occluded;
507 }
508
509 /*! Returns TRUE if client is occludes the sibling. If sibling is NULL it tries
510   against all other clients.
511 */
512 static gboolean stacking_occludes(ObClient *client, ObClient *sibling)
513 {
514     GList *it;
515     gboolean occludes = FALSE;
516     gboolean found = FALSE;
517
518     /* no need for any looping in this case */
519     if (sibling && client->layer != sibling->layer)
520         return occludes;
521
522     for (it = stacking_list; it; it = g_list_next(it))
523         if (WINDOW_IS_CLIENT(it->data)) {
524             ObClient *c = it->data;
525             if (found) {
526                 if (RECT_INTERSECTS_RECT(c->frame->area, client->frame->area))
527                 {
528                     if (sibling != NULL) {
529                         if (c == sibling) {
530                             occludes = TRUE;
531                             break;
532                         }
533                     }
534                     else if (c->layer == client->layer) {
535                         occludes = TRUE;
536                         break;
537                     }
538                     else if (c->layer < client->layer)
539                         break; /* we past its layer */
540                 }
541             }
542             else if (c == client)
543                 found = TRUE;
544         }
545     return occludes;
546 }
547
548 void stacking_restack_request(ObClient *client, ObClient *sibling,
549                               gint detail, gboolean activate)
550 {
551     switch (detail) {
552     case Below:
553         ob_debug("Restack request Below for client %s sibling %s\n",
554                  client->title, sibling ? sibling->title : "(all)");
555         /* just lower it */
556         stacking_lower(CLIENT_AS_WINDOW(client));
557         break;
558     case BottomIf:
559         ob_debug("Restack request BottomIf for client %s sibling "
560                  "%s\n",
561                  client->title, sibling ? sibling->title : "(all)");
562         /* if this client occludes sibling (or anything if NULL), then
563            lower it to the bottom */
564         if (stacking_occludes(client, sibling))
565             stacking_lower(CLIENT_AS_WINDOW(client));
566         break;
567     case Above:
568         ob_debug("Restack request Above for client %s sibling %s\n",
569                  client->title, sibling ? sibling->title : "(all)");
570         if (activate && !client->iconic)
571             /* use user=TRUE because it is impossible to get a timestamp
572                for this */
573             client_activate(client, FALSE, TRUE);
574         else
575             stacking_raise(CLIENT_AS_WINDOW(client));
576         break;
577     case TopIf:
578         ob_debug("Restack request TopIf for client %s sibling %s\n",
579                  client->title, sibling ? sibling->title : "(all)");
580         if (stacking_occluded(client, sibling)) {
581             if (activate && !client->iconic)
582                 /* use user=TRUE because it is impossible to get a timestamp
583                    for this */
584                 client_activate(client, FALSE, TRUE);
585             else
586                 stacking_raise(CLIENT_AS_WINDOW(client));
587         }
588         break;
589     case Opposite:
590         ob_debug("Restack request Opposite for client %s sibling "
591                  "%s\n",
592                  client->title, sibling ? sibling->title : "(all)");
593         if (stacking_occluded(client, sibling)) {
594             if (activate && !client->iconic)
595                 /* use user=TRUE because it is impossible to get a timestamp
596                    for this */
597                 client_activate(client, FALSE, TRUE);
598             else
599                 stacking_raise(CLIENT_AS_WINDOW(client));
600         }
601         else if (stacking_occludes(client, sibling))
602             stacking_lower(CLIENT_AS_WINDOW(client));
603         break;
604     }
605 }