]> git.lizzy.rs Git - bspwm.git/blob - query.c
Implement tags
[bspwm.git] / query.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "bspwm.h"
4 #include "desktop.h"
5 #include "history.h"
6 #include "messages.h"
7 #include "monitor.h"
8 #include "tree.h"
9 #include "query.h"
10
11 void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
12 {
13     char line[MAXLEN];
14     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
15         if (loc.monitor != NULL && m != loc.monitor)
16             continue;
17         if (dom != DOMAIN_DESKTOP) {
18             if (dom == DOMAIN_MONITOR) {
19                 snprintf(line, sizeof(line), "%s\n", m->name);
20                 strncat(rsp, line, REMLEN(rsp));
21                 continue;
22             } else {
23                 snprintf(line, sizeof(line), "%s %ux%u%+i%+i %i,%i,%i,%i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y, m->top_padding, m->right_padding, m->bottom_padding, m->left_padding);
24                 strncat(rsp, line, REMLEN(rsp));
25                 if (m == mon)
26                     strncat(rsp, " *", REMLEN(rsp));
27                 strncat(rsp, "\n", REMLEN(rsp));
28             }
29         }
30         query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
31     }
32 }
33
34 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, char *rsp)
35 {
36     char line[MAXLEN];
37     for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
38         if (loc.desktop != NULL && d != loc.desktop)
39             continue;
40         for (unsigned int i = 0; i < depth; i++)
41             strncat(rsp, "  ", REMLEN(rsp));
42         if (dom == DOMAIN_DESKTOP) {
43             snprintf(line, sizeof(line), "%s\n", d->name);
44             strncat(rsp, line, REMLEN(rsp));
45             continue;
46         } else {
47             snprintf(line, sizeof(line), "%s %u %i %u %c", d->name, d->border_width, d->window_gap, d->tags_field, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
48             strncat(rsp, line, REMLEN(rsp));
49             if (d == m->desk)
50                 strncat(rsp, " *", REMLEN(rsp));
51             strncat(rsp, "\n", REMLEN(rsp));
52         }
53         query_tree(d, d->root, rsp, depth + 1);
54     }
55 }
56
57 void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
58 {
59     if (n == NULL)
60         return;
61
62     char line[MAXLEN];
63
64     for (unsigned int i = 0; i < depth; i++)
65         strncat(rsp, "  ", REMLEN(rsp));
66
67     if (is_leaf(n)) {
68         client_t *c = n->client;
69         snprintf(line, sizeof(line), "%c %s 0x%X %u %u %ux%u%+i%+i %c %c%c%c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, c->tags_field, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))), (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (n->split_mode ? 'p' : '-'));
70     } else {
71         snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
72     }
73
74     strncat(rsp, line, REMLEN(rsp));
75
76     if (n == d->focus)
77         strncat(rsp, " *", REMLEN(rsp));
78     strncat(rsp, "\n", REMLEN(rsp));
79
80     query_tree(d, n->first_child, rsp, depth + 1);
81     query_tree(d, n->second_child, rsp, depth + 1);
82 }
83
84 void query_history(coordinates_t loc, char *rsp)
85 {
86     char line[MAXLEN];
87     for (history_t *h = history_head; h != NULL; h = h->next) {
88         if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
89                 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
90             continue;
91         xcb_window_t win = XCB_NONE;
92         if (h->loc.node != NULL)
93             win = h->loc.node->client->window;
94         snprintf(line, sizeof(line), "%s %s 0x%X", h->loc.monitor->name, h->loc.desktop->name, win);
95         strncat(rsp, line, REMLEN(rsp));
96         strncat(rsp, "\n", REMLEN(rsp));
97     }
98 }
99
100 void query_stack(char *rsp)
101 {
102     char line[MAXLEN];
103     for (stack_t *s = stack_head; s != NULL; s = s->next) {
104         snprintf(line, sizeof(line), "0x%X", s->node->client->window);
105         strncat(rsp, line, REMLEN(rsp));
106         strncat(rsp, "\n", REMLEN(rsp));
107     }
108 }
109
110 void query_windows(coordinates_t loc, char *rsp)
111 {
112     char line[MAXLEN];
113
114     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
115         if (loc.monitor != NULL && m != loc.monitor)
116             continue;
117         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
118             if (loc.desktop != NULL && d != loc.desktop)
119                 continue;
120             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
121                 if (loc.node != NULL && n != loc.node)
122                     continue;
123                 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
124                 strncat(rsp, line, REMLEN(rsp));
125             }
126         }
127     }
128 }
129
130 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
131 {
132     client_select_t sel;
133     sel.type = CLIENT_TYPE_ALL;
134     sel.class = CLIENT_CLASS_ALL;
135     sel.mode = CLIENT_MODE_ALL;
136     sel.urgency = CLIENT_URGENCY_ALL;
137     char *tok;
138     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
139         tok[0] = '\0';
140         tok++;
141         if (streq("tiled", tok)) {
142             sel.type = CLIENT_TYPE_TILED;
143         } else if (streq("floating", tok)) {
144             sel.type = CLIENT_TYPE_FLOATING;
145         } else if (streq("like", tok)) {
146             sel.class = CLIENT_CLASS_EQUAL;
147         } else if (streq("unlike", tok)) {
148             sel.class = CLIENT_CLASS_DIFFER;
149         } else if (streq("automatic", tok)) {
150             sel.mode = CLIENT_MODE_AUTOMATIC;
151         } else if (streq("manual", tok)) {
152             sel.mode = CLIENT_MODE_MANUAL;
153         } else if (streq("urgent", tok)) {
154             sel.urgency = CLIENT_URGENCY_ON;
155         } else if (streq("nonurgent", tok)) {
156             sel.urgency = CLIENT_URGENCY_OFF;
157         }
158     }
159
160     dst->monitor = ref->monitor;
161     dst->desktop = ref->desktop;
162     dst->node = NULL;
163
164     direction_t dir;
165     cycle_dir_t cyc;
166     if (parse_direction(desc, &dir)) {
167         dst->node = nearest_neighbor(dst->desktop, ref->node, dir, sel);
168     } else if (parse_cycle_direction(desc, &cyc)) {
169         dst->node = closest_node(ref->desktop, ref->node, cyc, sel);
170     } else if (streq("last", desc)) {
171         history_last_node(ref->node, sel, dst);
172     } else if (streq("biggest", desc)) {
173         dst->node = find_biggest(ref->desktop, ref->node, sel);
174     } else if (streq("focused", desc)) {
175         if (node_matches(ref->node, mon->desk->focus, sel)) {
176             dst->monitor = mon;
177             dst->desktop = mon->desk;
178             dst->node = mon->desk->focus;
179         }
180     } else {
181         long int wid;
182         if (parse_window_id(desc, &wid))
183             locate_window(wid, dst);
184     }
185
186     return (dst->node != NULL);
187 }
188
189 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
190 {
191     desktop_select_t sel;
192     sel.status = DESKTOP_STATUS_ALL;
193     sel.urgency = DESKTOP_URGENCY_ALL;
194     char *tok;
195     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
196         tok[0] = '\0';
197         tok++;
198         if (streq("free", tok)) {
199             sel.status = DESKTOP_STATUS_FREE;
200         } else if (streq("occupied", tok)) {
201             sel.status = DESKTOP_STATUS_OCCUPIED;
202         } else if (streq("urgent", tok)) {
203             sel.urgency = DESKTOP_URGENCY_ON;
204         } else if (streq("nonurgent", tok)) {
205             sel.urgency = DESKTOP_URGENCY_OFF;
206         }
207     }
208
209     dst->desktop = NULL;
210
211     cycle_dir_t cyc;
212     int idx;
213     if (parse_cycle_direction(desc, &cyc)) {
214         dst->monitor = ref->monitor;
215         dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
216     } else if (parse_index(desc, &idx)) {
217         desktop_from_index(idx, dst);
218     } else if (streq("last", desc)) {
219         history_last_desktop(ref->desktop, sel, dst);
220     } else if (streq("focused", desc)) {
221         if (desktop_matches(mon->desk, sel)) {
222             dst->monitor = mon;
223             dst->desktop = mon->desk;
224         }
225     } else {
226         locate_desktop(desc, dst);
227     }
228
229     return (dst->desktop != NULL);
230 }
231
232 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
233 {
234     desktop_select_t sel;
235     sel.status = DESKTOP_STATUS_ALL;
236     sel.urgency = DESKTOP_URGENCY_ALL;
237     char *tok;
238     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
239         tok[0] = '\0';
240         tok++;
241         if (streq("free", tok)) {
242             sel.status = DESKTOP_STATUS_FREE;
243         } else if (streq("occupied", tok)) {
244             sel.status = DESKTOP_STATUS_OCCUPIED;
245         }
246     }
247
248     dst->monitor = NULL;
249
250     direction_t dir;
251     cycle_dir_t cyc;
252     int idx;
253     if (parse_direction(desc, &dir)) {
254         dst->monitor = nearest_monitor(ref->monitor, dir, sel);
255     } else if (parse_cycle_direction(desc, &cyc)) {
256         dst->monitor = closest_monitor(ref->monitor, cyc, sel);
257     } else if (parse_index(desc, &idx)) {
258         monitor_from_index(idx, dst);
259     } else if (streq("last", desc)) {
260         history_last_monitor(ref->monitor, sel, dst);
261     } else if (streq("primary", desc)) {
262         if (pri_mon != NULL && desktop_matches(pri_mon->desk, sel))
263             dst->monitor = pri_mon;
264     } else if (streq("focused", desc)) {
265         if (desktop_matches(mon->desk, sel))
266             dst->monitor = mon;
267     } else {
268         locate_monitor(desc, dst);
269     }
270
271     return (dst->monitor != NULL);
272 }
273
274 bool locate_window(xcb_window_t win, coordinates_t *loc)
275 {
276     for (monitor_t *m = mon_head; m != NULL; m = m->next)
277         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
278             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
279                 if (n->client->window == win) {
280                     loc->monitor = m;
281                     loc->desktop = d;
282                     loc->node = n;
283                     return true;
284                 }
285     return false;
286 }
287
288 bool locate_desktop(char *name, coordinates_t *loc)
289 {
290     for (monitor_t *m = mon_head; m != NULL; m = m->next)
291         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
292             if (streq(d->name, name)) {
293                 loc->monitor = m;
294                 loc->desktop = d;
295                 return true;
296             }
297     return false;
298 }
299
300 bool locate_monitor(char *name, coordinates_t *loc)
301 {
302     for (monitor_t *m = mon_head; m != NULL; m = m->next)
303         if (streq(m->name, name)) {
304             loc->monitor = m;
305             return true;
306         }
307     return false;
308 }
309
310 bool desktop_from_index(int i, coordinates_t *loc)
311 {
312     for (monitor_t *m = mon_head; m != NULL; m = m->next)
313         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
314             if (i == 1) {
315                 loc->monitor = m;
316                 loc->desktop = d;
317                 loc->node = NULL;
318                 return true;
319             }
320     return false;
321 }
322
323 bool monitor_from_index(int i, coordinates_t *loc)
324 {
325     for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
326         if (i == 1) {
327             loc->monitor = m;
328             loc->desktop = NULL;
329             loc->node = NULL;
330             return true;
331         }
332     return false;
333 }
334
335 /**
336  * Check if the specified node matches the selection criteria.
337  *
338  * Arguments:
339  *  node_t *c           - the active node
340  *  node_t *t           - the node to test
341  *  client_sel_t sel    - the selection criteria
342  *
343  * Returns true if the node matches.
344  **/
345 bool node_matches(node_t *c, node_t *t, client_select_t sel)
346 {
347     if (sel.type != CLIENT_TYPE_ALL &&
348             is_tiled(t->client)
349             ? sel.type == CLIENT_TYPE_FLOATING
350             : sel.type == CLIENT_TYPE_TILED
351        ) return false;
352
353     if (sel.class != CLIENT_CLASS_ALL &&
354             streq(c->client->class_name, t->client->class_name)
355             ? sel.class == CLIENT_CLASS_DIFFER
356             : sel.class == CLIENT_CLASS_EQUAL
357        ) return false;
358
359     if (sel.mode != CLIENT_MODE_ALL &&
360             t->split_mode == MODE_MANUAL
361             ? sel.mode == CLIENT_MODE_AUTOMATIC
362             : sel.mode == CLIENT_MODE_MANUAL)
363         return false;
364
365     if (sel.urgency != CLIENT_URGENCY_ALL &&
366             t->client->urgent
367             ? sel.urgency == CLIENT_URGENCY_OFF
368             : sel.urgency == CLIENT_URGENCY_ON
369        ) return false;
370
371     return true;
372 }
373
374 bool desktop_matches(desktop_t *t, desktop_select_t sel)
375 {
376     if (sel.status != DESKTOP_STATUS_ALL &&
377             t->root == NULL
378             ? sel.status == DESKTOP_STATUS_OCCUPIED
379             : sel.status == DESKTOP_STATUS_FREE
380        ) return false;
381
382     if (sel.urgency != DESKTOP_URGENCY_ALL &&
383             is_urgent(t)
384             ? sel.urgency == DESKTOP_URGENCY_OFF
385             : sel.urgency == DESKTOP_URGENCY_ON
386        ) return false;
387
388     return true;
389 }