1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
30 stacking_list_t *make_stack(node_t *n)
32 stacking_list_t *s = malloc(sizeof(stacking_list_t));
34 s->prev = s->next = NULL;
38 void stack_insert_after(stacking_list_t *a, node_t *n)
40 stacking_list_t *s = make_stack(n);
42 stack_head = stack_tail = s;
47 stacking_list_t *b = a->next;
58 void stack_insert_before(stacking_list_t *a, node_t *n)
60 stacking_list_t *s = make_stack(n);
62 stack_head = stack_tail = s;
67 stacking_list_t *b = a->prev;
78 void remove_stack(stacking_list_t *s)
82 stacking_list_t *a = s->prev;
83 stacking_list_t *b = s->next;
95 void remove_stack_node(node_t *n)
97 for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
105 int stack_level(client_t *c)
107 int layer_level = (c->layer == LAYER_NORMAL ? 1 : (c->layer == LAYER_BELOW ? 0 : 2));
108 int state_level = (IS_TILED(c) ? 0 : (IS_FLOATING(c) ? 2 : 1));
109 return 3 * layer_level + state_level;
112 int stack_cmp(client_t *c1, client_t *c2)
114 return stack_level(c1) - stack_level(c2);
117 stacking_list_t *limit_above(node_t *n)
119 stacking_list_t *s = stack_head;
120 while (s != NULL && stack_cmp(n->client, s->node->client) >= 0) {
132 stacking_list_t *limit_below(node_t *n)
134 stacking_list_t *s = stack_tail;
135 while (s != NULL && stack_cmp(n->client, s->node->client) <= 0) {
147 void stack(node_t *n, bool focused)
149 if (IS_FLOATING(n->client) && !auto_raise) {
153 PRINTF("stack %X\n", n->client->window);
155 if (stack_head == NULL) {
156 stack_insert_after(NULL, n);
158 stacking_list_t *s = (focused ? limit_above(n) : limit_below(n));
162 int i = stack_cmp(n->client, s->node->client);
163 if (i < 0 || (i == 0 && !focused)) {
164 stack_insert_before(s, n);
165 window_below(n->client->window, s->node->client->window);
167 stack_insert_after(s, n);
168 window_above(n->client->window, s->node->client->window);