]> git.lizzy.rs Git - bspwm.git/blob - query.c
Remove `--float-upcoming` from `control`
[bspwm.git] / query.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "bspwm.h"
5 #include "tree.h"
6 #include "settings.h"
7 #include "messages.h"
8 #include "query.h"
9
10 void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
11 {
12     char line[MAXLEN];
13     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
14         if (loc.monitor != NULL && m != loc.monitor)
15             continue;
16         if (dom != DOMAIN_DESKTOP) {
17             if (dom == DOMAIN_MONITOR) {
18                 snprintf(line, sizeof(line), "%s\n", m->name);
19                 strncat(rsp, line, REMLEN(rsp));
20                 continue;
21             } else {
22                 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);
23                 strncat(rsp, line, REMLEN(rsp));
24                 if (m == mon)
25                     strncat(rsp, " #", REMLEN(rsp));
26                 else if (m == last_mon)
27                     strncat(rsp, " ~", REMLEN(rsp));
28                 strncat(rsp, "\n", REMLEN(rsp));
29             }
30         }
31         query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
32     }
33 }
34
35 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, char *rsp)
36 {
37     char line[MAXLEN];
38     for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
39         if (loc.desktop != NULL && d != loc.desktop)
40             continue;
41         for (unsigned int i = 0; i < depth; i++)
42             strncat(rsp, "  ", REMLEN(rsp));
43         if (dom == DOMAIN_DESKTOP) {
44             snprintf(line, sizeof(line), "%s\n", d->name);
45             strncat(rsp, line, REMLEN(rsp));
46             continue;
47         } else {
48             snprintf(line, sizeof(line), "%s %i %c", d->name, d->window_gap, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
49             strncat(rsp, line, REMLEN(rsp));
50             if (d == m->desk)
51                 strncat(rsp, " @", REMLEN(rsp));
52             else if (d == m->last_desk)
53                 strncat(rsp, " ~", REMLEN(rsp));
54             strncat(rsp, "\n", REMLEN(rsp));
55         }
56         query_tree(d, d->root, rsp, depth + 1);
57     }
58 }
59
60 void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
61 {
62     if (n == NULL)
63         return;
64
65     char line[MAXLEN];
66
67     for (unsigned int i = 0; i < depth; i++)
68         strncat(rsp, "  ", REMLEN(rsp));
69
70     if (is_leaf(n)) {
71         client_t *c = n->client;
72         snprintf(line, sizeof(line), "%c %s %X %u %ux%u%+i%+i %c %c%c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, 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' : '-'), (n->split_mode ? 'p' : '-'));
73     } else {
74         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);
75     }
76
77     strncat(rsp, line, REMLEN(rsp));
78
79     if (n == d->focus)
80         strncat(rsp, " *", REMLEN(rsp));
81     strncat(rsp, "\n", REMLEN(rsp));
82
83     query_tree(d, n->first_child, rsp, depth + 1);
84     query_tree(d, n->second_child, rsp, depth + 1);
85 }
86
87 void query_history(coordinates_t loc, char *rsp)
88 {
89     char line[MAXLEN];
90     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
91         if (loc.monitor != NULL && m != loc.monitor)
92             continue;
93         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
94             if (loc.desktop != NULL && d != loc.desktop)
95                 continue;
96             snprintf(line, sizeof(line), "%s\n", d->name);
97             strncat(rsp, line, REMLEN(rsp));
98             for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
99                 snprintf(line, sizeof(line), "  %X\n", a->node->client->window);
100                 strncat(rsp, line, REMLEN(rsp));
101             }
102         }
103     }
104 }
105
106 void query_windows(coordinates_t loc, char *rsp)
107 {
108     char line[MAXLEN];
109
110     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
111         if (loc.monitor != NULL && m != loc.monitor)
112             continue;
113         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
114             if (loc.desktop != NULL && d != loc.desktop)
115                 continue;
116             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
117                 if (loc.node != NULL && n != loc.node)
118                     continue;
119                 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
120                 strncat(rsp, line, REMLEN(rsp));
121             }
122         }
123     }
124 }
125
126 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
127 {
128     client_select_t sel;
129     sel.type = CLIENT_TYPE_ALL;
130     sel.class = CLIENT_CLASS_ALL;
131     sel.mode = CLIENT_MODE_ALL;
132     sel.urgency = CLIENT_URGENCY_ALL;
133     char *tok;
134     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
135         tok[0] = '\0';
136         tok++;
137         if (streq("tiled", tok)) {
138             sel.type = CLIENT_TYPE_TILED;
139         } else if (streq("floating", tok)) {
140             sel.type = CLIENT_TYPE_FLOATING;
141         } else if (streq("like", tok)) {
142             sel.class = CLIENT_CLASS_EQUAL;
143         } else if (streq("unlike", tok)) {
144             sel.class = CLIENT_CLASS_DIFFER;
145         } else if (streq("automatic", tok)) {
146             sel.mode = CLIENT_MODE_AUTOMATIC;
147         } else if (streq("manual", tok)) {
148             sel.mode = CLIENT_MODE_MANUAL;
149         } else if (streq("urgent", tok)) {
150             sel.urgency = CLIENT_URGENCY_ON;
151         } else if (streq("nonurgent", tok)) {
152             sel.urgency = CLIENT_URGENCY_OFF;
153         }
154     }
155
156     dst->monitor = ref->monitor;
157     dst->desktop = ref->desktop;
158     dst->node = NULL;
159
160     direction_t dir;
161     cycle_dir_t cyc;
162     if (parse_direction(desc, &dir)) {
163         dst->node = nearest_neighbor(dst->desktop, ref->node, dir, sel);
164     } else if (parse_cycle_direction(desc, &cyc)) {
165         dst->node = closest_node(ref->desktop, ref->node, cyc, sel);
166     } else if (streq("last", desc)) {
167         dst->node = history_last(ref->desktop->history, ref->node, sel);
168     } else if (streq("biggest", desc)) {
169         dst->node = find_biggest(ref->desktop, ref->node, sel);
170     } else if (streq("focused", desc)) {
171         if (node_matches(ref->node, mon->desk->focus, sel)) {
172             dst->monitor = mon;
173             dst->desktop = mon->desk;
174             dst->node = mon->desk->focus;
175         }
176     } else {
177         long int wid;
178         if (parse_window_id(desc, &wid))
179             locate_window(wid, dst);
180     }
181
182     return (dst->node != NULL);
183 }
184
185 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
186 {
187     desktop_select_t sel;
188     sel.status = DESKTOP_STATUS_ALL;
189     sel.urgency = DESKTOP_URGENCY_ALL;
190     char *tok;
191     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
192         tok[0] = '\0';
193         tok++;
194         if (streq("free", tok)) {
195             sel.status = DESKTOP_STATUS_FREE;
196         } else if (streq("occupied", tok)) {
197             sel.status = DESKTOP_STATUS_OCCUPIED;
198         } else if (streq("urgent", tok)) {
199             sel.urgency = DESKTOP_URGENCY_ON;
200         } else if (streq("nonurgent", tok)) {
201             sel.urgency = DESKTOP_URGENCY_OFF;
202         }
203     }
204
205     dst->desktop = NULL;
206
207     cycle_dir_t cyc;
208     int idx;
209     if (parse_cycle_direction(desc, &cyc)) {
210         dst->monitor = ref->monitor;
211         dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
212     } else if (parse_index(desc, &idx)) {
213         desktop_from_index(idx, dst);
214     } else if (streq("last", desc)) {
215         if (mon->last_desk != NULL && desktop_matches(mon->last_desk, sel)) {
216             dst->monitor = mon;
217             dst->desktop = mon->last_desk;
218         }
219     } else if (streq("focused", desc)) {
220         if (desktop_matches(mon->desk, sel)) {
221             dst->monitor = mon;
222             dst->desktop = mon->desk;
223         }
224     } else {
225         locate_desktop(desc, dst);
226     }
227
228     return (dst->desktop != NULL);
229 }
230
231 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
232 {
233     desktop_select_t sel;
234     sel.status = DESKTOP_STATUS_ALL;
235     sel.urgency = DESKTOP_URGENCY_ALL;
236     char *tok;
237     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
238         tok[0] = '\0';
239         tok++;
240         if (streq("free", tok)) {
241             sel.status = DESKTOP_STATUS_FREE;
242         } else if (streq("occupied", tok)) {
243             sel.status = DESKTOP_STATUS_OCCUPIED;
244         }
245     }
246
247     dst->monitor = NULL;
248
249     direction_t dir;
250     cycle_dir_t cyc;
251     int idx;
252     if (parse_direction(desc, &dir)) {
253         dst->monitor = nearest_monitor(ref->monitor, dir, sel);
254     } else if (parse_cycle_direction(desc, &cyc)) {
255         dst->monitor = closest_monitor(ref->monitor, cyc, sel);
256     } else if (parse_index(desc, &idx)) {
257         monitor_from_index(idx, dst);
258     } else if (streq("last", desc)) {
259         if (last_mon != NULL && desktop_matches(last_mon->desk, sel))
260             dst->monitor = last_mon;
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 }