]> git.lizzy.rs Git - bspwm.git/blob - history.c
Transfer nodes moved via configure requests
[bspwm.git] / history.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 <stdbool.h>
27 #include "bspwm.h"
28 #include "tree.h"
29 #include "query.h"
30
31 history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
32 {
33         history_t *h = malloc(sizeof(history_t));
34         h->loc = (coordinates_t) {m, d, n};
35         h->prev = h->next = NULL;
36         h->latest = true;
37         return h;
38 }
39
40 void history_add(monitor_t *m, desktop_t *d, node_t *n)
41 {
42         if (!record_history) {
43                 return;
44         }
45         history_needle = NULL;
46         history_t *h = make_history(m, d, n);
47         if (history_head == NULL) {
48                 history_head = history_tail = h;
49         } else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
50                 for (history_t *hh = history_tail; hh != NULL; hh = hh->prev) {
51                         if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop)) {
52                                 hh->latest = false;
53                         }
54                 }
55                 history_tail->next = h;
56                 h->prev = history_tail;
57                 history_tail = h;
58         } else {
59                 free(h);
60         }
61 }
62
63 void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
64 {
65         for (history_t *h = history_head; h != NULL; h = h->next) {
66                 if (is_descendant(h->loc.node, n)) {
67                         h->loc.monitor = m;
68                         h->loc.desktop = d;
69                 }
70         }
71 }
72
73 void history_transfer_desktop(monitor_t *m, desktop_t *d)
74 {
75         for (history_t *h = history_head; h != NULL; h = h->next) {
76                 if (h->loc.desktop == d) {
77                         h->loc.monitor = m;
78                 }
79         }
80 }
81
82 void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
83 {
84         for (history_t *h = history_head; h != NULL; h = h->next) {
85                 if (is_descendant(h->loc.node, n1)) {
86                         h->loc.monitor = m2;
87                         h->loc.desktop = d2;
88                 } else if (is_descendant(h->loc.node, n2)) {
89                         h->loc.monitor = m1;
90                         h->loc.desktop = d1;
91                 }
92         }
93 }
94
95 void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
96 {
97         for (history_t *h = history_head; h != NULL; h = h->next) {
98                 if (h->loc.desktop == d1) {
99                         h->loc.monitor = m2;
100                 } else if (h->loc.desktop == d2) {
101                         h->loc.monitor = m1;
102                 }
103         }
104 }
105
106 void history_remove(desktop_t *d, node_t *n, bool deep)
107 {
108    /* removing from the newest to the oldest is required */
109    /* for maintaining the *latest* attribute */
110         history_t *b = history_tail;
111         while (b != NULL) {
112                 if ((n != NULL && ((deep && is_descendant(b->loc.node, n)) || (!deep && b->loc.node == n))) ||
113                     (n == NULL && d == b->loc.desktop)) {
114                         history_t *a = b->next;
115                         history_t *c = b->prev;
116                         if (a != NULL) {
117                                 /* remove duplicate entries */
118                                 while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node) ||
119                                        (a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
120                                         history_t *p = c->prev;
121                                         if (history_head == c) {
122                                                 history_head = history_tail;
123                                         }
124                                         if (history_needle == c) {
125                                                 history_needle = history_tail;
126                                         }
127                                         free(c);
128                                         c = p;
129                                 }
130                                 a->prev = c;
131                         }
132                         if (c != NULL) {
133                                 c->next = a;
134                         }
135                         if (history_tail == b) {
136                                 history_tail = c;
137                         }
138                         if (history_head == b) {
139                                 history_head = a;
140                         }
141                         if (history_needle == b) {
142                                 history_needle = c;
143                         }
144                         free(b);
145                         b = c;
146                 } else {
147                         b = b->prev;
148                 }
149         }
150 }
151
152 void empty_history(void)
153 {
154         history_t *h = history_head;
155         while (h != NULL) {
156                 history_t *next = h->next;
157                 free(h);
158                 h = next;
159         }
160         history_head = history_tail = NULL;
161 }
162
163 node_t *history_last_node(desktop_t *d, node_t *n)
164 {
165         for (history_t *h = history_tail; h != NULL; h = h->prev) {
166                 if (h->latest && h->loc.node != NULL && !is_descendant(h->loc.node, n) && h->loc.desktop == d) {
167                         return h->loc.node;
168                 }
169         }
170         return NULL;
171 }
172
173 desktop_t *history_last_desktop(monitor_t *m, desktop_t *d)
174 {
175         for (history_t *h = history_tail; h != NULL; h = h->prev) {
176                 if (h->latest && h->loc.desktop != d && h->loc.monitor == m) {
177                         return h->loc.desktop;
178                 }
179         }
180         return NULL;
181 }
182
183 monitor_t *history_last_monitor(monitor_t *m)
184 {
185         for (history_t *h = history_tail; h != NULL; h = h->prev) {
186                 if (h->latest && h->loc.monitor != m) {
187                         return h->loc.monitor;
188                 }
189         }
190         return NULL;
191 }
192
193 bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, node_select_t sel)
194 {
195         if (history_needle == NULL || record_history) {
196                 history_needle = history_tail;
197         }
198
199         history_t *h;
200         for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
201                 if (!h->latest ||
202                     h->loc.node == NULL ||
203                     h->loc.node == ref->node ||
204                     !node_matches(&h->loc, ref, sel)) {
205                         continue;
206                 }
207                 if (!record_history) {
208                         history_needle = h;
209                 }
210                 *dst = h->loc;
211                 return true;
212         }
213         return false;
214 }
215
216 bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
217 {
218         if (history_needle == NULL || record_history) {
219                 history_needle = history_tail;
220         }
221
222         history_t *h;
223         for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
224                 if (!h->latest ||
225                     h->loc.desktop == ref->desktop ||
226                     !desktop_matches(&h->loc, ref, sel)) {
227                         continue;
228                 }
229                 if (!record_history) {
230                         history_needle = h;
231                 }
232                 *dst = h->loc;
233                 return true;
234         }
235         return false;
236 }
237
238 bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, monitor_select_t sel)
239 {
240         if (history_needle == NULL || record_history) {
241                 history_needle = history_tail;
242         }
243
244         history_t *h;
245
246         for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
247                 if (!h->latest ||
248                     h->loc.monitor == ref->monitor ||
249                     !monitor_matches(&h->loc, ref, sel)) {
250                         continue;
251                 }
252                 if (!record_history) {
253                         history_needle = h;
254                 }
255                 *dst = h->loc;
256                 return true;
257         }
258
259         return false;
260 }
261
262 int history_rank(desktop_t *d, node_t *n)
263 {
264         int i = 0;
265         history_t *h = history_tail;
266         while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {
267                 h = h->prev;
268                 i++;
269         }
270         if (h == NULL) {
271                 return -1;
272         } else {
273                 return i;
274         }
275 }