]> git.lizzy.rs Git - bspwm.git/blob - stack.c
Keep normal windows below fullscreen windows
[bspwm.git] / stack.c
1 /* Copyright (c) 2012-2014, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those
25  * of the authors and should not be interpreted as representing official policies,
26  * either expressed or implied, of the FreeBSD Project.
27  */
28
29 #include <stdlib.h>
30 #include "bspwm.h"
31 #include "window.h"
32 #include "stack.h"
33
34 stacking_list_t *make_stack(node_t *n)
35 {
36         stacking_list_t *s = malloc(sizeof(stacking_list_t));
37         s->node = n;
38         s->prev = s->next = NULL;
39         return s;
40 }
41
42 void stack_insert_after(stacking_list_t *a, node_t *n)
43 {
44         stacking_list_t *s = make_stack(n);
45         if (a == NULL) {
46                 stack_head = stack_tail = s;
47         } else {
48                 if (a->node == n)
49                         return;
50                 remove_stack_node(n);
51                 stacking_list_t *b = a->next;
52                 if (b != NULL)
53                         b->prev = s;
54                 s->next = b;
55                 s->prev = a;
56                 a->next = s;
57                 if (stack_tail == a)
58                         stack_tail = s;
59         }
60 }
61
62 void stack_insert_before(stacking_list_t *a, node_t *n)
63 {
64         stacking_list_t *s = make_stack(n);
65         if (a == NULL) {
66                 stack_head = stack_tail = s;
67         } else {
68                 if (a->node == n)
69                         return;
70                 remove_stack_node(n);
71                 stacking_list_t *b = a->prev;
72                 if (b != NULL)
73                         b->next = s;
74                 s->prev = b;
75                 s->next = a;
76                 a->prev = s;
77                 if (stack_head == a)
78                         stack_head = s;
79         }
80 }
81
82 void remove_stack(stacking_list_t *s)
83 {
84         if (s == NULL)
85                 return;
86         stacking_list_t *a = s->prev;
87         stacking_list_t *b = s->next;
88         if (a != NULL)
89                 a->next = b;
90         if (b != NULL)
91                 b->prev = a;
92         if (s == stack_head)
93                 stack_head = b;
94         if (s == stack_tail)
95                 stack_tail = a;
96         free(s);
97 }
98
99 void remove_stack_node(node_t *n)
100 {
101         for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
102                 if (s->node == n) {
103                         remove_stack(s);
104                         return;
105                 }
106 }
107
108 void stack(node_t *n, stack_flavor_t f)
109 {
110         PRINTF("stack %X\n", n->client->window);
111
112         if (stack_head == NULL) {
113                 stack_insert_after(NULL, n);
114         } else if (n->client->fullscreen) {
115                 if (f == STACK_ABOVE) {
116                         stack_insert_after(stack_tail, n);
117                         window_raise(n->client->window);
118                 }
119         } else {
120                 if (f == STACK_ABOVE && n->client->floating && !auto_raise)
121                         return;
122                 stacking_list_t *latest_tiled = NULL;
123                 stacking_list_t *oldest_floating = NULL;
124                 stacking_list_t *oldest_fullscreen = NULL;
125                 for (stacking_list_t *s = (f == STACK_ABOVE ? stack_tail : stack_head); s != NULL; s = (f == STACK_ABOVE ? s->prev : s->next)) {
126                         if (s->node != n) {
127                                 if (s->node->client->fullscreen) {
128                                         if (oldest_fullscreen == NULL)
129                                                 oldest_fullscreen = s;
130                                         continue;
131                                 }
132                                 if (s->node->client->floating == n->client->floating) {
133                                         if (f == STACK_ABOVE) {
134                                                 stack_insert_after(s, n);
135                                                 window_above(n->client->window, s->node->client->window);
136                                         } else {
137                                                 stack_insert_before(s, n);
138                                                 window_below(n->client->window, s->node->client->window);
139                                         }
140                                         return;
141                                 } else if ((f != STACK_ABOVE || latest_tiled == NULL) && !s->node->client->floating) {
142                                         latest_tiled = s;
143                                 } else if ((f == STACK_ABOVE || oldest_floating == NULL) && s->node->client->floating) {
144                                         oldest_floating = s;
145                                 }
146                         }
147                 }
148                 if (latest_tiled == NULL && oldest_floating == NULL && oldest_fullscreen == NULL)
149                         return;
150                 if (n->client->floating) {
151                         if (latest_tiled != NULL) {
152                                 window_above(n->client->window, latest_tiled->node->client->window);
153                                 stack_insert_after(latest_tiled, n);
154                         } else if (oldest_fullscreen != NULL) {
155                                 window_below(n->client->window, oldest_fullscreen->node->client->window);
156                                 stack_insert_before(oldest_fullscreen, n);
157                         }
158                 } else {
159                         if (oldest_floating != NULL) {
160                                 window_below(n->client->window, oldest_floating->node->client->window);
161                                 stack_insert_before(oldest_floating, n);
162                         } else if (oldest_fullscreen != NULL) {
163                                 window_below(n->client->window, oldest_fullscreen->node->client->window);
164                                 stack_insert_before(oldest_fullscreen, n);
165                         }
166                 }
167         }
168 }