]> git.lizzy.rs Git - bspwm.git/blobdiff - types.c
Merge branch 'master' into history
[bspwm.git] / types.c
diff --git a/types.c b/types.c
index e675aad2877a9794b2cfa8fe35c90e9bd41047d2..7715699bf6accd12bf8999940aeb19e6d78500f5 100644 (file)
--- a/types.c
+++ b/types.c
@@ -83,7 +83,8 @@ desktop_t *make_desktop(const char *name)
         strncpy(d->name, name, sizeof(d->name));
     d->layout = LAYOUT_TILED;
     d->prev = d->next = NULL;
-    d->root = d->focus = d->last_focus = NULL;
+    d->root = d->focus = NULL;
+    d->history = make_focus_history();
     return d;
 }
 
@@ -108,7 +109,8 @@ void add_desktop(monitor_t *m, char *name)
 void empty_desktop(desktop_t *d)
 {
     destroy_tree(d->root);
-    d->root = d->focus = d->last_focus = NULL;
+    d->root = d->focus = NULL;
+    empty_history(d->history);
 }
 
 void remove_desktop(monitor_t *m, desktop_t *d)
@@ -162,3 +164,88 @@ pointer_state_t *make_pointer_state(void)
     p->window = XCB_NONE;
     return p;
 }
+
+focus_history_t *make_focus_history(void)
+{
+    focus_history_t *f = malloc(sizeof(focus_history_t));
+    f->head = f->tail = NULL;
+    return f;
+}
+
+node_list_t *make_node_list(void)
+{
+    node_list_t *n = malloc(sizeof(node_list_t));
+    n->node = NULL;
+    n->prev = n->next = NULL;
+    return n;
+}
+
+void history_add(focus_history_t *f, node_t *n)
+{
+    node_list_t *a = make_node_list();
+    a->node = n;
+    if (f->head == NULL) {
+        f->head = f->tail = a;
+    } else if (f->tail->node != n) {
+        f->tail->next = a;
+        a->prev = f->tail;
+        f->tail = a;
+    } else {
+        free(a);
+    }
+}
+
+void history_remove(focus_history_t *f, node_t *n)
+{
+    node_list_t *b = f->head;
+    while (b != NULL) {
+        if (b->node == n) {
+            node_list_t *a = b->prev;
+            node_list_t *c = b->next;
+            if (a != NULL) {
+                while (c != NULL && (c->node == a->node || c->node == n)) {
+                    node_list_t *d = c->next;
+                    if (f->tail == c)
+                        f->tail = (d == NULL ? b : d);
+                    free(c);
+                    c = d;
+                }
+                a->next = c;
+            }
+            if (c != NULL)
+                c->prev = a;
+            if (f->head == b)
+                f->head = c;
+            if (f->tail == b)
+                f->tail = a;
+            free(b);
+            b = c;
+        } else {
+            b = b->next;
+        }
+    }
+}
+
+void empty_history(focus_history_t *f)
+{
+    node_list_t *a = f->head;
+    while (a != NULL) {
+        node_list_t *b = a->next;
+        free(a);
+        a = b;
+    }
+    f->head = f->tail = NULL;
+}
+
+node_t *history_get(focus_history_t *f, int i)
+{
+    node_list_t *a = f->tail;
+    while (a != NULL && i > 0) {
+        a = a->prev;
+        i--;
+    }
+    if (a == NULL)
+        return NULL;
+    else
+        return a->node;
+}