]> git.lizzy.rs Git - bspwm.git/blob - stack.c
Be verbose regarding broken connections
[bspwm.git] / stack.c
1 /* Copyright (c) 2012, 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
25 #include <stdlib.h>
26 #include "bspwm.h"
27 #include "window.h"
28 #include "stack.h"
29
30 stacking_list_t *make_stack(node_t *n)
31 {
32         stacking_list_t *s = malloc(sizeof(stacking_list_t));
33         s->node = n;
34         s->prev = s->next = NULL;
35         return s;
36 }
37
38 void stack_insert_after(stacking_list_t *a, node_t *n)
39 {
40         stacking_list_t *s = make_stack(n);
41         if (a == NULL) {
42                 stack_head = stack_tail = s;
43         } else {
44                 if (a->node == n)
45                         return;
46                 remove_stack_node(n);
47                 stacking_list_t *b = a->next;
48                 if (b != NULL)
49                         b->prev = s;
50                 s->next = b;
51                 s->prev = a;
52                 a->next = s;
53                 if (stack_tail == a)
54                         stack_tail = s;
55         }
56 }
57
58 void stack_insert_before(stacking_list_t *a, node_t *n)
59 {
60         stacking_list_t *s = make_stack(n);
61         if (a == NULL) {
62                 stack_head = stack_tail = s;
63         } else {
64                 if (a->node == n)
65                         return;
66                 remove_stack_node(n);
67                 stacking_list_t *b = a->prev;
68                 if (b != NULL)
69                         b->next = s;
70                 s->prev = b;
71                 s->next = a;
72                 a->prev = s;
73                 if (stack_head == a)
74                         stack_head = s;
75         }
76 }
77
78 void remove_stack(stacking_list_t *s)
79 {
80         if (s == NULL)
81                 return;
82         stacking_list_t *a = s->prev;
83         stacking_list_t *b = s->next;
84         if (a != NULL)
85                 a->next = b;
86         if (b != NULL)
87                 b->prev = a;
88         if (s == stack_head)
89                 stack_head = b;
90         if (s == stack_tail)
91                 stack_tail = a;
92         free(s);
93 }
94
95 void remove_stack_node(node_t *n)
96 {
97         for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
98                 if (s->node == n) {
99                         remove_stack(s);
100                         return;
101                 }
102 }
103
104 void stack(node_t *n, stack_flavor_t f)
105 {
106         PRINTF("stack %X\n", n->client->window);
107
108         if (stack_head == NULL) {
109                 stack_insert_after(NULL, n);
110         } else if (n->client->fullscreen) {
111                 if (f == STACK_ABOVE) {
112                         stack_insert_after(stack_tail, n);
113                         window_raise(n->client->window);
114                 }
115         } else {
116                 if (f == STACK_ABOVE && n->client->floating && !auto_raise)
117                         return;
118                 stacking_list_t *latest_tiled = NULL;
119                 stacking_list_t *oldest_floating = NULL;
120                 stacking_list_t *oldest_fullscreen = NULL;
121                 for (stacking_list_t *s = (f == STACK_ABOVE ? stack_tail : stack_head); s != NULL; s = (f == STACK_ABOVE ? s->prev : s->next)) {
122                         if (s->node != n) {
123                                 if (s->node->client->fullscreen) {
124                                         if (oldest_fullscreen == NULL)
125                                                 oldest_fullscreen = s;
126                                         continue;
127                                 }
128                                 if (s->node->client->floating == n->client->floating) {
129                                         if (f == STACK_ABOVE) {
130                                                 stack_insert_after(s, n);
131                                                 window_above(n->client->window, s->node->client->window);
132                                         } else {
133                                                 stack_insert_before(s, n);
134                                                 window_below(n->client->window, s->node->client->window);
135                                         }
136                                         return;
137                                 } else if ((f != STACK_ABOVE || latest_tiled == NULL) && !s->node->client->floating) {
138                                         latest_tiled = s;
139                                 } else if ((f == STACK_ABOVE || oldest_floating == NULL) && s->node->client->floating) {
140                                         oldest_floating = s;
141                                 }
142                         }
143                 }
144                 if (latest_tiled == NULL && oldest_floating == NULL && oldest_fullscreen == NULL)
145                         return;
146                 if (n->client->floating) {
147                         if (latest_tiled != NULL) {
148                                 window_above(n->client->window, latest_tiled->node->client->window);
149                                 stack_insert_after(latest_tiled, n);
150                         } else if (oldest_fullscreen != NULL) {
151                                 window_below(n->client->window, oldest_fullscreen->node->client->window);
152                                 stack_insert_before(oldest_fullscreen, n);
153                         }
154                 } else {
155                         if (oldest_floating != NULL) {
156                                 window_below(n->client->window, oldest_floating->node->client->window);
157                                 stack_insert_before(oldest_floating, n);
158                         } else if (oldest_fullscreen != NULL) {
159                                 window_below(n->client->window, oldest_fullscreen->node->client->window);
160                                 stack_insert_before(oldest_fullscreen, n);
161                         }
162                 }
163         }
164 }