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.
32 history_t *make_history(monitor_t *m, desktop_t *d, node_t *n)
34 history_t *h = calloc(1, sizeof(history_t));
35 h->loc = (coordinates_t) {m, d, n};
36 h->prev = h->next = NULL;
41 void history_add(monitor_t *m, desktop_t *d, node_t *n, bool focused)
43 if (!record_history) {
48 history_needle = NULL;
51 history_t *h = make_history(m, d, n);
53 if (history_head == NULL) {
54 history_head = history_tail = h;
55 } else if ((n != NULL && history_tail->loc.node != n) || (n == NULL && d != history_tail->loc.desktop)) {
56 history_t *ip = focused ? history_tail : NULL;
58 for (history_t *hh = history_tail; hh != NULL; hh = hh->prev) {
59 if ((n != NULL && hh->loc.node == n) || (n == NULL && d == hh->loc.desktop)) {
62 if (ip == NULL && ((n != NULL && hh->loc.desktop == d) || (n == NULL && hh->loc.monitor == m))) {
68 history_insert_after(h, ip);
72 for (history_t *hh = history_head; hh != NULL; hh = hh->next) {
73 if (hh->latest && hh->loc.monitor == m) {
79 history_insert_before(h, ip);
86 // Inserts `a` after `b`.
87 void history_insert_after(history_t *a, history_t *b)
90 if (b->next != NULL) {
95 if (history_tail == b) {
100 // Inserts `a` before `b`.
101 void history_insert_before(history_t *a, history_t *b)
104 if (b->prev != NULL) {
109 if (history_head == b) {
114 void history_remove(desktop_t *d, node_t *n, bool deep)
116 /* removing from the newest to the oldest is required */
117 /* for maintaining the *latest* attribute */
118 history_t *b = history_tail;
120 if ((n != NULL && ((deep && is_descendant(b->loc.node, n)) || (!deep && b->loc.node == n))) ||
121 (n == NULL && d == b->loc.desktop)) {
122 history_t *a = b->next;
123 history_t *c = b->prev;
125 /* remove duplicate entries */
126 while (c != NULL && ((a->loc.node != NULL && a->loc.node == c->loc.node) ||
127 (a->loc.node == NULL && a->loc.desktop == c->loc.desktop))) {
128 history_t *p = c->prev;
129 if (history_head == c) {
130 history_head = history_tail;
132 if (history_needle == c) {
133 history_needle = history_tail;
143 if (history_tail == b) {
146 if (history_head == b) {
149 if (history_needle == b) {
160 void empty_history(void)
162 history_t *h = history_head;
164 history_t *next = h->next;
168 history_head = history_tail = NULL;
171 node_t *history_last_node(desktop_t *d, node_t *n)
173 for (history_t *h = history_tail; h != NULL; h = h->prev) {
174 if (h->latest && h->loc.node != NULL && !h->loc.node->hidden &&
175 !is_descendant(h->loc.node, n) && h->loc.desktop == d) {
182 desktop_t *history_last_desktop(monitor_t *m, desktop_t *d)
184 for (history_t *h = history_tail; h != NULL; h = h->prev) {
185 if (h->latest && h->loc.desktop != d && h->loc.monitor == m) {
186 return h->loc.desktop;
192 monitor_t *history_last_monitor(monitor_t *m)
194 for (history_t *h = history_tail; h != NULL; h = h->prev) {
195 if (h->latest && h->loc.monitor != m) {
196 return h->loc.monitor;
202 bool history_find_newest_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
204 for (history_t *h = history_tail; h != NULL; h = h->prev) {
205 if (h->loc.node == NULL ||
206 h->loc.node->hidden ||
207 !node_matches(&h->loc, ref, sel)) {
217 bool history_find_node(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
219 if (history_needle == NULL || record_history) {
220 history_needle = history_tail;
224 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
226 h->loc.node == NULL ||
227 h->loc.node == ref->node ||
228 h->loc.node->hidden ||
229 !node_matches(&h->loc, ref, sel)) {
232 if (!record_history) {
241 bool history_find_newest_desktop(coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel)
243 for (history_t *h = history_tail; h != NULL; h = h->prev) {
244 if (desktop_matches(&h->loc, ref, sel)) {
253 bool history_find_desktop(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel)
255 if (history_needle == NULL || record_history) {
256 history_needle = history_tail;
260 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
262 h->loc.desktop == ref->desktop ||
263 !desktop_matches(&h->loc, ref, sel)) {
266 if (!record_history) {
275 bool history_find_newest_monitor(coordinates_t *ref, coordinates_t *dst, monitor_select_t *sel)
277 for (history_t *h = history_tail; h != NULL; h = h->prev) {
278 if (monitor_matches(&h->loc, ref, sel)) {
287 bool history_find_monitor(history_dir_t hdi, coordinates_t *ref, coordinates_t *dst, monitor_select_t *sel)
289 if (history_needle == NULL || record_history) {
290 history_needle = history_tail;
295 for (h = history_needle; h != NULL; h = (hdi == HISTORY_OLDER ? h->prev : h->next)) {
297 h->loc.monitor == ref->monitor ||
298 !monitor_matches(&h->loc, ref, sel)) {
301 if (!record_history) {
311 uint32_t history_rank(node_t *n)
314 history_t *h = history_tail;
315 while (h != NULL && (!h->latest || h->loc.node != n)) {