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.
31 history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
33 history_t *h = malloc(sizeof(history_t));
34 h->loc = (coordinates_t) {m, d, n};
35 h->prev = h->next = NULL;
40 void history_add(monitor_t *m, desktop_t *d, node_t *n)
42 if (!record_history) {
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)) {
55 history_tail->next = h;
56 h->prev = history_tail;
63 void history_transfer_node(monitor_t *m, desktop_t *d, node_t *n)
65 for (history_t *h = history_head; h != NULL; h = h->next) {
66 if (is_descendant(h->loc.node, n)) {
73 void history_transfer_desktop(monitor_t *m, desktop_t *d)
75 for (history_t *h = history_head; h != NULL; h = h->next) {
76 if (h->loc.desktop == d) {
82 void history_swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
84 for (history_t *h = history_head; h != NULL; h = h->next) {
85 if (is_descendant(h->loc.node, n1)) {
88 } else if (is_descendant(h->loc.node, n2)) {
95 void history_swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
97 for (history_t *h = history_head; h != NULL; h = h->next) {
98 if (h->loc.desktop == d1) {
100 } else if (h->loc.desktop == d2) {
106 void history_remove(desktop_t *d, node_t *n, bool deep)
108 /* removing from the newest to the oldest is required */
109 /* for maintaining the *latest* attribute */
110 history_t *b = history_tail;
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;
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;
124 if (history_needle == c) {
125 history_needle = history_tail;
135 if (history_tail == b) {
138 if (history_head == b) {
141 if (history_needle == b) {
152 void empty_history(void)
154 history_t *h = history_head;
156 history_t *next = h->next;
160 history_head = history_tail = NULL;
163 node_t *history_last_node(desktop_t *d, node_t *n)
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) {
173 desktop_t *history_last_desktop(monitor_t *m, desktop_t *d)
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;
183 monitor_t *history_last_monitor(monitor_t *m)
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;
193 bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, node_select_t sel)
195 if (history_needle == NULL || record_history) {
196 history_needle = history_tail;
200 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
202 h->loc.node == NULL ||
203 h->loc.node == ref->node ||
204 !node_matches(&h->loc, ref, sel)) {
207 if (!record_history) {
216 bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel)
218 if (history_needle == NULL || record_history) {
219 history_needle = history_tail;
223 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
225 h->loc.desktop == ref->desktop ||
226 !desktop_matches(&h->loc, ref, sel)) {
229 if (!record_history) {
238 bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, monitor_select_t sel)
240 if (history_needle == NULL || record_history) {
241 history_needle = history_tail;
246 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
248 h->loc.monitor == ref->monitor ||
249 !monitor_matches(&h->loc, ref, sel)) {
252 if (!record_history) {
262 int history_rank(desktop_t *d, node_t *n)
265 history_t *h = history_tail;
266 while (h != NULL && (!h->latest || h->loc.node != n || h->loc.desktop != d)) {