]> git.lizzy.rs Git - bspwm.git/blob - history.c
Move a few functions from tree to desktop/monitor
[bspwm.git] / history.c
1 #include <stdlib.h>
2 #include "types.h"
3 #include "query.h"
4
5 focus_history_t *make_focus_history(void)
6 {
7     focus_history_t *f = malloc(sizeof(focus_history_t));
8     f->head = f->tail = NULL;
9     return f;
10 }
11
12 node_list_t *make_node_list(void)
13 {
14     node_list_t *n = malloc(sizeof(node_list_t));
15     n->node = NULL;
16     n->prev = n->next = NULL;
17     n->latest = true;
18     return n;
19 }
20
21 void history_add(focus_history_t *f, node_t *n)
22 {
23     node_list_t *a = make_node_list();
24     a->node = n;
25     if (f->head == NULL) {
26         f->head = f->tail = a;
27     } else if (f->head->node != n) {
28         for (node_list_t *b = f->head; b != NULL; b = b->next)
29             if (b->node == n)
30                 b->latest = false;
31         f->head->prev = a;
32         a->next = f->head;
33         f->head = a;
34     } else {
35         free(a);
36     }
37 }
38
39 void history_remove(focus_history_t *f, node_t *n)
40 {
41     /* in order to maintain the `latest` node list state,
42        we remove node lists from head to tail */
43     node_list_t *b = f->head;
44     while (b != NULL) {
45         if (b->node == n) {
46             node_list_t *a = b->prev;
47             node_list_t *c = b->next;
48             if (a != NULL) {
49                 /* remove duplicate entries */
50                 while (c != NULL && c->node == a->node) {
51                     node_list_t *d = c->next;
52                     if (f->tail == c)
53                         f->tail = f->head;
54                     free(c);
55                     c = d;
56                 }
57                 a->next = c;
58             }
59             if (c != NULL)
60                 c->prev = a;
61             if (f->head == b)
62                 f->head = c;
63             if (f->tail == b)
64                 f->tail = a;
65             free(b);
66             b = c;
67         } else {
68             b = b->next;
69         }
70     }
71 }
72
73 void empty_history(focus_history_t *f)
74 {
75     node_list_t *a = f->head;
76     while (a != NULL) {
77         node_list_t *b = a->next;
78         free(a);
79         a = b;
80     }
81     f->head = f->tail = NULL;
82 }
83
84 node_t *history_get(focus_history_t *f, int i)
85 {
86     node_list_t *a = f->head;
87     while (a != NULL && i > 0) {
88         a = a->next;
89         i--;
90     }
91     if (a == NULL)
92         return NULL;
93     else
94         return a->node;
95 }
96
97 node_t *history_last(focus_history_t *f, node_t *n, client_select_t sel)
98 {
99     for (node_list_t *a = f->head; a != NULL; a = a->next) {
100         if (!a->latest || a->node == n || !node_matches(n, a->node, sel))
101             continue;
102         return a->node;
103     }
104     return NULL;
105 }
106
107 int history_rank(focus_history_t *f, node_t *n)
108 {
109     int i = 0;
110     node_list_t *a = f->head;
111     while (a != NULL && (!a->latest || a->node != n)) {
112         a = a->next;
113         i++;
114     }
115     if (a == NULL)
116         return -1;
117     else
118         return i;
119 }