1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation and/or
11 * other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20 * 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.
29 history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
31 history_t *h = malloc(sizeof(history_t));
32 h->loc = (coordinates_t) {m, d, n};
33 h->prev = h->next = NULL;
38 void history_add(monitor_t *m, desktop_t *d, node_t *n)
42 history_needle = NULL;
43 history_t *h = make_history(m, d, n);
44 if (history_head == NULL) {
45 history_head = history_tail = h;
46 } else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
47 for (history_t *hh = history_tail; hh != NULL; hh = hh->prev)
48 if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop))
50 history_tail->next = h;
51 h->prev = history_tail;
58 void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
60 for (history_t *h = history_head; h != NULL; h = h->next)
61 if (h->loc.node == n) {
67 void history_transfer_desktop(monitor_t *m, desktop_t *d)
69 for (history_t *h = history_head; h != NULL; h = h->next)
70 if (h->loc.desktop == d)
74 void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
76 for (history_t *h = history_head; h != NULL; h = h->next)
77 if (h->loc.node == n1) {
80 } else if (h->loc.node == n2) {
86 void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
88 for (history_t *h = history_head; h != NULL; h = h->next)
89 if (h->loc.desktop == d1)
91 else if (h->loc.desktop == d2)
95 void history_remove(desktop_t *d, node_t *n)
97 /* removing from the newest to the oldest is required */
98 /* for maintaining the *latest* attribute */
99 history_t *b = history_tail;
101 if ((n != NULL && n == b->loc.node) || (n == NULL && d == b->loc.desktop)) {
102 history_t *a = b->next;
103 history_t *c = b->prev;
105 /* remove duplicate entries */
106 while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node)
107 || (a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
108 history_t *d = c->prev;
109 if (history_head == c)
110 history_head = history_tail;
111 if (history_needle == c)
112 history_needle = history_tail;
120 if (history_tail == b)
122 if (history_head == b)
124 if (history_needle == b)
134 void empty_history(void)
136 history_t *h = history_head;
138 history_t *next = h->next;
142 history_head = history_tail = NULL;
145 node_t *history_get_node(desktop_t *d, node_t *n)
147 for (history_t *h = history_tail; h != NULL; h = h->prev)
148 if (h->latest && h->loc.node != NULL && h->loc.node != n && h->loc.desktop == d)
153 desktop_t *history_get_desktop(monitor_t *m, desktop_t *d)
155 for (history_t *h = history_tail; h != NULL; h = h->prev)
156 if (h->latest && h->loc.desktop != d && h->loc.monitor == m)
157 return h->loc.desktop;
161 monitor_t *history_get_monitor(monitor_t *m)
163 for (history_t *h = history_tail; h != NULL; h = h->prev)
164 if (h->latest && h->loc.monitor != m)
165 return h->loc.monitor;
169 bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, client_select_t sel)
171 if (history_needle == NULL || record_history)
172 history_needle = history_tail;
175 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
177 || h->loc.node == NULL
178 || h->loc.node == ref->node
179 || !node_matches(&h->loc, ref, sel))
189 bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
191 if (history_needle == NULL || record_history)
192 history_needle = history_tail;
195 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
197 || h->loc.desktop == ref->desktop
198 || !desktop_matches(&h->loc, ref, sel))
208 bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
210 if (history_needle == NULL || record_history)
211 history_needle = history_tail;
214 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
216 || h->loc.monitor == ref->monitor
217 || !desktop_matches(&h->loc, ref, sel))
227 int history_rank(desktop_t *d, node_t *n)
230 history_t *h = history_tail;
231 while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {