4 #include <xcb/xcb_event.h>
13 node_t *make_node(void)
15 node_t *n = malloc(sizeof(node_t));
16 n->parent = n->first_child = n->second_child = NULL;
17 n->split_ratio = split_ratio;
18 n->split_type = TYPE_VERTICAL;
19 n->birth_rotation = ROTATE_IDENTITY;
25 monitor_t *make_monitor(xcb_rectangle_t *rect)
27 monitor_t *m = malloc(sizeof(monitor_t));
28 snprintf(m->name, sizeof(m->name), "%s%02d", DEFAULT_MON_NAME, ++monitor_uid);
29 m->prev = m->next = NULL;
30 m->desk = m->last_desk = NULL;
34 warn("no rectangle was given for monitor '%s'\n", m->name);
35 m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 0;
40 monitor_t *find_monitor(char *name)
42 for (monitor_t *m = mon_head; m != NULL; m = m->next)
43 if (strcmp(m->name, name) == 0)
48 monitor_t *get_monitor_by_id(xcb_randr_output_t id)
50 for (monitor_t *m = mon_head; m != NULL; m = m->next)
56 monitor_t *add_monitor(xcb_rectangle_t *rect)
58 monitor_t *m = make_monitor(rect);
72 void remove_monitor(monitor_t *m)
74 while (m->desk_head != NULL)
75 remove_desktop(m, m->desk_head);
76 monitor_t *prev = m->prev;
77 monitor_t *next = m->next;
89 monitor_t *mm = (last_mon == NULL ? (prev == NULL ? next : prev) : last_mon);
91 focus_node(mm, mm->desk, mm->desk->focus);
102 void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
104 desktop_t *dd = ms->desk;
105 unlink_desktop(ms, d);
106 insert_desktop(md, d);
108 if (ms->desk != NULL)
109 desktop_show(ms->desk);
113 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
114 fit_monitor(md, n->client);
116 if (d != dd && md->desk == d) {
120 ewmh_update_desktop_names();
123 void merge_monitors(monitor_t *ms, monitor_t *md)
125 PRINTF("merge %s into %s\n", ms->name, md->name);
127 desktop_t *d = ms->desk_head;
129 desktop_t *next = d->next;
130 transfer_desktop(ms, md, d);
135 desktop_t *make_desktop(const char *name)
137 desktop_t *d = malloc(sizeof(desktop_t));
139 snprintf(d->name, sizeof(d->name), "%s%02d", DEFAULT_DESK_NAME, ++desktop_uid);
141 strncpy(d->name, name, sizeof(d->name));
142 d->layout = LAYOUT_TILED;
143 d->prev = d->next = NULL;
144 d->root = d->focus = NULL;
145 d->history = make_focus_history();
149 void insert_desktop(monitor_t *m, desktop_t *d)
151 if (m->desk == NULL) {
156 m->desk_tail->next = d;
157 d->prev = m->desk_tail;
162 void add_desktop(monitor_t *m, desktop_t *d)
164 PRINTF("add desktop %s\n", d->name);
166 insert_desktop(m, d);
168 ewmh_update_number_of_desktops();
169 ewmh_update_desktop_names();
173 void empty_desktop(desktop_t *d)
175 destroy_tree(d->root);
176 d->root = d->focus = NULL;
177 empty_history(d->history);
180 void unlink_desktop(monitor_t *m, desktop_t *d)
182 desktop_t *prev = d->prev;
183 desktop_t *next = d->next;
188 if (m->desk_head == d)
190 if (m->desk_tail == d)
192 if (m->last_desk == d)
195 m->desk = (m->last_desk == NULL ? (prev == NULL ? next : prev) : m->last_desk);
196 d->prev = d->next = NULL;
199 void remove_desktop(monitor_t *m, desktop_t *d)
201 PRINTF("remove desktop %s\n", d->name);
204 unlink_desktop(m, d);
208 ewmh_update_number_of_desktops();
209 ewmh_update_desktop_names();
213 client_t *make_client(xcb_window_t win)
215 client_t *c = malloc(sizeof(client_t));
216 strncpy(c->class_name, MISSING_VALUE, sizeof(c->class_name));
217 c->uid = ++client_uid;
218 c->border_width = border_width;
220 c->floating = c->transient = c->fullscreen = c->locked = c->urgent = false;
224 rule_t *make_rule(void)
226 rule_t *r = malloc(sizeof(rule_t));
228 r->effect.floating = false;
229 r->effect.follow = false;
230 r->effect.monitor = NULL;
231 r->effect.desktop = NULL;
237 pointer_state_t *make_pointer_state(void)
239 pointer_state_t *p = malloc(sizeof(pointer_state_t));
242 p->node = p->vertical_fence = p->horizontal_fence = NULL;
244 p->window = XCB_NONE;
248 focus_history_t *make_focus_history(void)
250 focus_history_t *f = malloc(sizeof(focus_history_t));
251 f->head = f->tail = NULL;
255 node_list_t *make_node_list(void)
257 node_list_t *n = malloc(sizeof(node_list_t));
259 n->prev = n->next = NULL;
264 void history_add(focus_history_t *f, node_t *n)
266 node_list_t *a = make_node_list();
268 if (f->head == NULL) {
269 f->head = f->tail = a;
270 } else if (f->head->node != n) {
271 for (node_list_t *b = f->head; b != NULL; b = b->next)
282 void history_remove(focus_history_t *f, node_t *n)
284 /* in order to maintain the `latest` node list state,
285 we remove node lists from head to tail */
286 node_list_t *b = f->head;
289 node_list_t *a = b->prev;
290 node_list_t *c = b->next;
292 /* remove duplicate entries */
293 while (c != NULL && c->node == a->node) {
294 node_list_t *d = c->next;
316 void empty_history(focus_history_t *f)
318 node_list_t *a = f->head;
320 node_list_t *b = a->next;
324 f->head = f->tail = NULL;
327 node_t *history_get(focus_history_t *f, int i)
329 node_list_t *a = f->head;
330 while (a != NULL && i > 0) {
340 int history_rank(focus_history_t *f, node_t *n)
343 node_list_t *a = f->head;
344 while (a != NULL && (!a->latest || a->node != n)) {