1 /* Copyright (c) 2012-2014, 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.
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.
33 history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
35 history_t *h = malloc(sizeof(history_t));
36 h->loc = (coordinates_t) {m, d, n};
37 h->prev = h->next = NULL;
42 void history_add(monitor_t *m, desktop_t *d, node_t *n)
46 history_needle = NULL;
47 history_t *h = make_history(m, d, n);
48 if (history_head == NULL) {
49 history_head = history_tail = h;
50 } else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
51 for (history_t *hh = history_tail; hh != NULL; hh = hh->prev)
52 if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop))
54 history_tail->next = h;
55 h->prev = history_tail;
62 void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
64 for (history_t *h = history_head; h != NULL; h = h->next)
65 if (h->loc.node == n) {
71 void history_transfer_desktop(monitor_t *m, desktop_t *d)
73 for (history_t *h = history_head; h != NULL; h = h->next)
74 if (h->loc.desktop == d)
78 void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
80 for (history_t *h = history_head; h != NULL; h = h->next)
81 if (h->loc.node == n1) {
84 } else if (h->loc.node == n2) {
90 void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
92 for (history_t *h = history_head; h != NULL; h = h->next)
93 if (h->loc.desktop == d1)
95 else if (h->loc.desktop == d2)
99 void history_remove(desktop_t *d, node_t *n)
101 /* removing from the newest to the oldest is required */
102 /* for maintaining the *latest* attribute */
103 history_t *b = history_tail;
105 if ((n != NULL && n == b->loc.node) || (n == NULL && d == b->loc.desktop)) {
106 history_t *a = b->next;
107 history_t *c = b->prev;
109 /* remove duplicate entries */
110 while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node) ||
111 (a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
112 history_t *d = c->prev;
113 if (history_head == c)
114 history_head = history_tail;
115 if (history_needle == c)
116 history_needle = history_tail;
124 if (history_tail == b)
126 if (history_head == b)
128 if (history_needle == b)
138 void empty_history(void)
140 history_t *h = history_head;
142 history_t *next = h->next;
146 history_head = history_tail = NULL;
149 node_t *history_get_node(desktop_t *d, node_t *n)
151 for (history_t *h = history_tail; h != NULL; h = h->prev)
152 if (h->latest && h->loc.node != NULL && h->loc.node != n && h->loc.desktop == d)
157 desktop_t *history_get_desktop(monitor_t *m, desktop_t *d)
159 for (history_t *h = history_tail; h != NULL; h = h->prev)
160 if (h->latest && h->loc.desktop != d && h->loc.monitor == m)
161 return h->loc.desktop;
165 monitor_t *history_get_monitor(monitor_t *m)
167 for (history_t *h = history_tail; h != NULL; h = h->prev)
168 if (h->latest && h->loc.monitor != m)
169 return h->loc.monitor;
173 bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, client_select_t sel)
175 if (history_needle == NULL || record_history)
176 history_needle = history_tail;
179 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
181 h->loc.node == NULL ||
182 h->loc.node == ref->node ||
183 !node_matches(&h->loc, ref, sel))
193 bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
195 if (history_needle == NULL || record_history)
196 history_needle = history_tail;
199 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
201 h->loc.desktop == ref->desktop ||
202 !desktop_matches(&h->loc, ref, sel))
212 bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
214 if (history_needle == NULL || record_history)
215 history_needle = history_tail;
218 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
220 h->loc.monitor == ref->monitor ||
221 !desktop_matches(&h->loc, ref, sel))
231 int history_rank(desktop_t *d, node_t *n)
234 history_t *h = history_tail;
235 while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {