forked from baskerville/bspwm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
history.c
243 lines (224 loc) · 6.86 KB
/
history.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright (c) 2012-2014, Bastien Dejean
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the FreeBSD Project.
*/
#include <stdlib.h>
#include "bspwm.h"
#include "query.h"
history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
{
history_t *h = malloc(sizeof(history_t));
h->loc = (coordinates_t) {m, d, n};
h->prev = h->next = NULL;
h->latest = true;
return h;
}
void history_add(monitor_t *m, desktop_t *d, node_t *n)
{
if (!record_history)
return;
history_needle = NULL;
history_t *h = make_history(m, d, n);
if (history_head == NULL) {
history_head = history_tail = h;
} else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
for (history_t *hh = history_tail; hh != NULL; hh = hh->prev)
if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop))
hh->latest = false;
history_tail->next = h;
h->prev = history_tail;
history_tail = h;
} else {
free(h);
}
}
void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
{
for (history_t *h = history_head; h != NULL; h = h->next)
if (h->loc.node == n) {
h->loc.monitor = m;
h->loc.desktop = d;
}
}
void history_transfer_desktop(monitor_t *m, desktop_t *d)
{
for (history_t *h = history_head; h != NULL; h = h->next)
if (h->loc.desktop == d)
h->loc.monitor = m;
}
void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
{
for (history_t *h = history_head; h != NULL; h = h->next)
if (h->loc.node == n1) {
h->loc.monitor = m2;
h->loc.desktop = d2;
} else if (h->loc.node == n2) {
h->loc.monitor = m1;
h->loc.desktop = d1;
}
}
void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
{
for (history_t *h = history_head; h != NULL; h = h->next)
if (h->loc.desktop == d1)
h->loc.monitor = m2;
else if (h->loc.desktop == d2)
h->loc.monitor = m1;
}
void history_remove(desktop_t *d, node_t *n)
{
/* removing from the newest to the oldest is required */
/* for maintaining the *latest* attribute */
history_t *b = history_tail;
while (b != NULL) {
if ((n != NULL && n == b->loc.node) || (n == NULL && d == b->loc.desktop)) {
history_t *a = b->next;
history_t *c = b->prev;
if (a != NULL) {
/* remove duplicate entries */
while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node) ||
(a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
history_t *d = c->prev;
if (history_head == c)
history_head = history_tail;
if (history_needle == c)
history_needle = history_tail;
free(c);
c = d;
}
a->prev = c;
}
if (c != NULL)
c->next = a;
if (history_tail == b)
history_tail = c;
if (history_head == b)
history_head = a;
if (history_needle == b)
history_needle = c;
free(b);
b = c;
} else {
b = b->prev;
}
}
}
void empty_history(void)
{
history_t *h = history_head;
while (h != NULL) {
history_t *next = h->next;
free(h);
h = next;
}
history_head = history_tail = NULL;
}
node_t *history_get_node(desktop_t *d, node_t *n)
{
for (history_t *h = history_tail; h != NULL; h = h->prev)
if (h->latest && h->loc.node != NULL && h->loc.node != n && h->loc.desktop == d)
return h->loc.node;
return NULL;
}
desktop_t *history_get_desktop(monitor_t *m, desktop_t *d)
{
for (history_t *h = history_tail; h != NULL; h = h->prev)
if (h->latest && h->loc.desktop != d && h->loc.monitor == m)
return h->loc.desktop;
return NULL;
}
monitor_t *history_get_monitor(monitor_t *m)
{
for (history_t *h = history_tail; h != NULL; h = h->prev)
if (h->latest && h->loc.monitor != m)
return h->loc.monitor;
return NULL;
}
bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, client_select_t sel)
{
if (history_needle == NULL || record_history)
history_needle = history_tail;
history_t *h;
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.node == NULL ||
h->loc.node == ref->node ||
!node_matches(&h->loc, ref, sel))
continue;
if (!record_history)
history_needle = h;
*dst = h->loc;
return true;
}
return false;
}
bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
{
if (history_needle == NULL || record_history)
history_needle = history_tail;
history_t *h;
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.desktop == ref->desktop ||
!desktop_matches(&h->loc, ref, sel))
continue;
if (!record_history)
history_needle = h;
*dst = h->loc;
return true;
}
return false;
}
bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
{
if (history_needle == NULL || record_history)
history_needle = history_tail;
history_t *h;
for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
if (!h->latest ||
h->loc.monitor == ref->monitor ||
!desktop_matches(&h->loc, ref, sel))
continue;
if (!record_history)
history_needle = h;
*dst = h->loc;
return true;
}
return false;
}
int history_rank(desktop_t *d, node_t *n)
{
int i = 0;
history_t *h = history_tail;
while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {
h = h->prev;
i++;
}
if (h == NULL)
return -1;
else
return i;
}