]> git.lizzy.rs Git - bspwm.git/blob - query.c
895dd2e43406ee5261c24c60b307cef688c982a8
[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 (stacking_list_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     history_dir_t hdi;
167     if (parse_direction(desc, &dir)) {
168         dst->node = nearest_neighbor(dst->desktop, ref->node, dir, sel);
169     } else if (parse_cycle_direction(desc, &cyc)) {
170         dst->node = closest_node(ref->desktop, ref->node, cyc, sel);
171     } else if (parse_history_direction(desc, &hdi)) {
172         history_navigate(hdi, dst);
173     } else if (streq("last", desc)) {
174         history_last_node(ref->node, sel, dst);
175     } else if (streq("last_local", desc)) {
176         dst->node = history_get_node(ref->desktop, ref->node);
177     } else if (streq("biggest", desc)) {
178         dst->node = find_biggest(ref->desktop, ref->node, sel);
179     } else if (streq("focused", desc)) {
180         if (node_matches(ref->node, mon->desk->focus, sel)) {
181             dst->monitor = mon;
182             dst->desktop = mon->desk;
183             dst->node = mon->desk->focus;
184         }
185     } else {
186         long int wid;
187         if (parse_window_id(desc, &wid))
188             locate_window(wid, dst);
189     }
190
191     return (dst->node != NULL);
192 }
193
194 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
195 {
196     desktop_select_t sel;
197     sel.status = DESKTOP_STATUS_ALL;
198     sel.urgency = DESKTOP_URGENCY_ALL;
199     char *tok;
200     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
201         tok[0] = '\0';
202         tok++;
203         if (streq("free", tok)) {
204             sel.status = DESKTOP_STATUS_FREE;
205         } else if (streq("occupied", tok)) {
206             sel.status = DESKTOP_STATUS_OCCUPIED;
207         } else if (streq("urgent", tok)) {
208             sel.urgency = DESKTOP_URGENCY_ON;
209         } else if (streq("nonurgent", tok)) {
210             sel.urgency = DESKTOP_URGENCY_OFF;
211         }
212     }
213
214     dst->desktop = NULL;
215
216     cycle_dir_t cyc;
217     int idx;
218     if (parse_cycle_direction(desc, &cyc)) {
219         dst->monitor = ref->monitor;
220         dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
221     } else if (parse_index(desc, &idx)) {
222         desktop_from_index(idx, dst);
223     } else if (streq("last", desc)) {
224         history_last_desktop(ref->desktop, sel, dst);
225     } else if (streq("last_local", desc)) {
226         dst->monitor = ref->monitor;
227         dst->desktop = history_get_desktop(ref->monitor, ref->desktop);
228     } else if (streq("focused", desc)) {
229         if (desktop_matches(mon->desk, sel)) {
230             dst->monitor = mon;
231             dst->desktop = mon->desk;
232         }
233     } else {
234         locate_desktop(desc, dst);
235     }
236
237     return (dst->desktop != NULL);
238 }
239
240 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
241 {
242     desktop_select_t sel;
243     sel.status = DESKTOP_STATUS_ALL;
244     sel.urgency = DESKTOP_URGENCY_ALL;
245     char *tok;
246     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
247         tok[0] = '\0';
248         tok++;
249         if (streq("free", tok)) {
250             sel.status = DESKTOP_STATUS_FREE;
251         } else if (streq("occupied", tok)) {
252             sel.status = DESKTOP_STATUS_OCCUPIED;
253         }
254     }
255
256     dst->monitor = NULL;
257
258     direction_t dir;
259     cycle_dir_t cyc;
260     int idx;
261     if (parse_direction(desc, &dir)) {
262         dst->monitor = nearest_monitor(ref->monitor, dir, sel);
263     } else if (parse_cycle_direction(desc, &cyc)) {
264         dst->monitor = closest_monitor(ref->monitor, cyc, sel);
265     } else if (parse_index(desc, &idx)) {
266         monitor_from_index(idx, dst);
267     } else if (streq("last", desc)) {
268         history_last_monitor(ref->monitor, sel, dst);
269     } else if (streq("primary", desc)) {
270         if (pri_mon != NULL && desktop_matches(pri_mon->desk, sel))
271             dst->monitor = pri_mon;
272     } else if (streq("focused", desc)) {
273         if (desktop_matches(mon->desk, sel))
274             dst->monitor = mon;
275     } else {
276         locate_monitor(desc, dst);
277     }
278
279     return (dst->monitor != NULL);
280 }
281
282 bool locate_window(xcb_window_t win, coordinates_t *loc)
283 {
284     for (monitor_t *m = mon_head; m != NULL; m = m->next)
285         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
286             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
287                 if (n->client->window == win) {
288                     loc->monitor = m;
289                     loc->desktop = d;
290                     loc->node = n;
291                     return true;
292                 }
293     return false;
294 }
295
296 bool locate_desktop(char *name, coordinates_t *loc)
297 {
298     for (monitor_t *m = mon_head; m != NULL; m = m->next)
299         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
300             if (streq(d->name, name)) {
301                 loc->monitor = m;
302                 loc->desktop = d;
303                 return true;
304             }
305     return false;
306 }
307
308 bool locate_monitor(char *name, coordinates_t *loc)
309 {
310     for (monitor_t *m = mon_head; m != NULL; m = m->next)
311         if (streq(m->name, name)) {
312             loc->monitor = m;
313             return true;
314         }
315     return false;
316 }
317
318 bool desktop_from_index(int i, coordinates_t *loc)
319 {
320     for (monitor_t *m = mon_head; m != NULL; m = m->next)
321         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
322             if (i == 1) {
323                 loc->monitor = m;
324                 loc->desktop = d;
325                 loc->node = NULL;
326                 return true;
327             }
328     return false;
329 }
330
331 bool monitor_from_index(int i, coordinates_t *loc)
332 {
333     for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
334         if (i == 1) {
335             loc->monitor = m;
336             loc->desktop = NULL;
337             loc->node = NULL;
338             return true;
339         }
340     return false;
341 }
342
343 /**
344  * Check if the specified node matches the selection criteria.
345  *
346  * Arguments:
347  *  node_t *c           - the active node
348  *  node_t *t           - the node to test
349  *  client_sel_t sel    - the selection criteria
350  *
351  * Returns true if the node matches.
352  **/
353 bool node_matches(node_t *c, node_t *t, client_select_t sel)
354 {
355     if (sel.type != CLIENT_TYPE_ALL &&
356             is_tiled(t->client)
357             ? sel.type == CLIENT_TYPE_FLOATING
358             : sel.type == CLIENT_TYPE_TILED
359        ) return false;
360
361     if (sel.class != CLIENT_CLASS_ALL &&
362             streq(c->client->class_name, t->client->class_name)
363             ? sel.class == CLIENT_CLASS_DIFFER
364             : sel.class == CLIENT_CLASS_EQUAL
365        ) return false;
366
367     if (sel.mode != CLIENT_MODE_ALL &&
368             t->split_mode == MODE_MANUAL
369             ? sel.mode == CLIENT_MODE_AUTOMATIC
370             : sel.mode == CLIENT_MODE_MANUAL)
371         return false;
372
373     if (sel.urgency != CLIENT_URGENCY_ALL &&
374             t->client->urgent
375             ? sel.urgency == CLIENT_URGENCY_OFF
376             : sel.urgency == CLIENT_URGENCY_ON
377        ) return false;
378
379     return true;
380 }
381
382 bool desktop_matches(desktop_t *t, desktop_select_t sel)
383 {
384     if (sel.status != DESKTOP_STATUS_ALL &&
385             t->root == NULL
386             ? sel.status == DESKTOP_STATUS_OCCUPIED
387             : sel.status == DESKTOP_STATUS_FREE
388        ) return false;
389
390     if (sel.urgency != DESKTOP_URGENCY_ALL &&
391             is_urgent(t)
392             ? sel.urgency == DESKTOP_URGENCY_OFF
393             : sel.urgency == DESKTOP_URGENCY_ON
394        ) return false;
395
396     return true;
397 }