]> git.lizzy.rs Git - bspwm.git/blob - query.c
Refactor error reporting
[bspwm.git] / query.c
1 /* Copyright (c) 2012-2014, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those
25  * of the authors and should not be interpreted as representing official policies,
26  * either expressed or implied, of the FreeBSD Project.
27  */
28
29 #include <stdio.h>
30 #include <strings.h>
31 #include <string.h>
32 #include "bspwm.h"
33 #include "desktop.h"
34 #include "history.h"
35 #include "messages.h"
36 #include "monitor.h"
37 #include "tree.h"
38 #include "query.h"
39
40 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
41 {
42         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
43                 if (loc.monitor != NULL && m != loc.monitor)
44                         continue;
45                 if (dom != DOMAIN_DESKTOP) {
46                         if (dom == DOMAIN_MONITOR) {
47                                 fprintf(rsp, "%s\n", m->name);
48                                 continue;
49                         } else {
50                                 fprintf(rsp, "%s %ux%u%+i%+i %i,%i,%i,%i%s\n", m->name,
51                                          m->rectangle.width,m->rectangle.height, m->rectangle.x, m->rectangle.y,
52                                          m->top_padding, m->right_padding, m->bottom_padding, m->left_padding,
53                                          (m == mon ? " *" : ""));
54                         }
55                 }
56                 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
57         }
58 }
59
60 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
61 {
62         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
63                 if (loc.desktop != NULL && d != loc.desktop)
64                         continue;
65                 for (unsigned int i = 0; i < depth; i++)
66                         fprintf(rsp, "\t");
67                 if (dom == DOMAIN_DESKTOP) {
68                         fprintf(rsp, "%s\n", d->name);
69                         continue;
70                 } else {
71                         fprintf(rsp, "%s %u %i %i,%i,%i,%i %c %c%s\n", d->name, d->border_width,
72                                 d->window_gap,
73                                 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
74                                 (d->layout == LAYOUT_TILED ? 'T' : 'M'), (d->floating ? 'f' : '-'),
75                                 (d == m->desk ? " *" : ""));
76                 }
77                 query_tree(d, d->root, rsp, depth + 1);
78         }
79 }
80
81 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
82 {
83         if (n == NULL)
84                 return;
85
86         for (unsigned int i = 0; i < depth; i++)
87                 fprintf(rsp, "\t");
88
89         if (is_leaf(n)) {
90                 client_t *c = n->client;
91                 fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c %c%c%c%c%c%c%c%c%s\n",
92                          (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
93                          c->class_name, c->instance_name, c->window, c->border_width,
94                          c->floating_rectangle.width, c->floating_rectangle.height,
95                          c->floating_rectangle.x, c->floating_rectangle.y,
96                          (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))),
97                          (c->floating ? 'f' : '-'), (c->pseudo_tiled ? 'd' : '-'), (c->fullscreen ? 'F' : '-'),
98                          (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'),
99                          (c->private ? 'i' : '-'), (n->split_mode ? 'p' : '-'),
100                          (n == d->focus ? " *" : ""));
101         } else {
102                 fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
103                         (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
104         }
105
106         query_tree(d, n->first_child, rsp, depth + 1);
107         query_tree(d, n->second_child, rsp, depth + 1);
108 }
109
110 void query_history(coordinates_t loc, FILE *rsp)
111 {
112         for (history_t *h = history_head; h != NULL; h = h->next) {
113                 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
114                                 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
115                         continue;
116                 xcb_window_t win = XCB_NONE;
117                 if (h->loc.node != NULL)
118                         win = h->loc.node->client->window;
119                 fprintf(rsp, "%s %s 0x%X\n", h->loc.monitor->name, h->loc.desktop->name, win);
120         }
121 }
122
123 void query_stack(FILE *rsp)
124 {
125         for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
126                 fprintf(rsp, "0x%X\n", s->node->client->window);
127 }
128
129 void query_windows(coordinates_t loc, FILE *rsp)
130 {
131         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
132                 if (loc.monitor != NULL && m != loc.monitor)
133                         continue;
134                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
135                         if (loc.desktop != NULL && d != loc.desktop)
136                                 continue;
137                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
138                                 if (loc.node != NULL && n != loc.node)
139                                         continue;
140                                 fprintf(rsp, "0x%X\n", n->client->window);
141                         }
142                 }
143         }
144 }
145
146 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
147 {
148         client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, CLIENT_MODE_ALL, false, false};
149         char *tok;
150         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
151                 tok[0] = '\0';
152                 tok++;
153                 if (streq("tiled", tok)) {
154                         sel.type = CLIENT_TYPE_TILED;
155                 } else if (streq("floating", tok)) {
156                         sel.type = CLIENT_TYPE_FLOATING;
157                 } else if (streq("like", tok)) {
158                         sel.class = CLIENT_CLASS_EQUAL;
159                 } else if (streq("unlike", tok)) {
160                         sel.class = CLIENT_CLASS_DIFFER;
161                 } else if (streq("manual", tok)) {
162                         sel.mode = CLIENT_MODE_MANUAL;
163                 } else if (streq("automatic", tok)) {
164                         sel.mode = CLIENT_MODE_AUTOMATIC;
165                 } else if (streq("urgent", tok)) {
166                         sel.urgent = true;
167                 } else if (streq("local", tok)) {
168                         sel.local = true;
169                 }
170         }
171
172         dst->monitor = ref->monitor;
173         dst->desktop = ref->desktop;
174         dst->node = NULL;
175
176         direction_t dir;
177         cycle_dir_t cyc;
178         history_dir_t hdi;
179         if (parse_direction(desc, &dir)) {
180                 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
181         } else if (parse_cycle_direction(desc, &cyc)) {
182                 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
183         } else if (parse_history_direction(desc, &hdi)) {
184                 history_find_node(hdi, ref, dst, sel);
185         } else if (streq("last", desc)) {
186                 history_find_node(HISTORY_OLDER, ref, dst, sel);
187         } else if (streq("biggest", desc)) {
188                 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
189         } else if (streq("focused", desc)) {
190                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
191                 if (node_matches(&loc, ref, sel)) {
192                         dst->monitor = mon;
193                         dst->desktop = mon->desk;
194                         dst->node = mon->desk->focus;
195                 }
196         } else {
197                 long int wid;
198                 if (parse_window_id(desc, &wid))
199                         locate_window(wid, dst);
200         }
201
202         return (dst->node != NULL);
203 }
204
205 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
206 {
207         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
208         char *tok;
209         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
210                 tok[0] = '\0';
211                 tok++;
212                 if (streq("free", tok)) {
213                         sel.status = DESKTOP_STATUS_FREE;
214                 } else if (streq("occupied", tok)) {
215                         sel.status = DESKTOP_STATUS_OCCUPIED;
216                 } else if (streq("urgent", tok)) {
217                         sel.urgent = true;
218                 } else if (streq("local", tok)) {
219                         sel.local = true;
220                 }
221         }
222
223         dst->desktop = NULL;
224
225         cycle_dir_t cyc;
226         history_dir_t hdi;
227         char *colon;
228         int idx;
229         if (parse_cycle_direction(desc, &cyc)) {
230                 dst->monitor = ref->monitor;
231                 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
232         } else if (parse_history_direction(desc, &hdi)) {
233                 history_find_desktop(hdi, ref, dst, sel);
234         } else if (streq("last", desc)) {
235                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
236         } else if (streq("focused", desc)) {
237                 coordinates_t loc = {mon, mon->desk, NULL};
238                 if (desktop_matches(&loc, ref, sel)) {
239                         dst->monitor = mon;
240                         dst->desktop = mon->desk;
241                 }
242         } else if ((colon = index(desc, ':')) != NULL) {
243                 *colon = '\0';
244                 if (monitor_from_desc(desc, ref, dst)) {
245                         if (streq("focused", colon + 1)) {
246                                 dst->desktop = dst->monitor->desk;
247                         } else if (parse_index(colon + 1, &idx)) {
248                                 desktop_from_index(idx, dst, dst->monitor);
249                         }
250                 }
251         } else if (parse_index(desc, &idx)) {
252                 desktop_from_index(idx, dst, NULL);
253         } else {
254                 locate_desktop(desc, dst);
255         }
256
257         return (dst->desktop != NULL);
258 }
259
260 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
261 {
262         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
263         char *tok;
264         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
265                 tok[0] = '\0';
266                 tok++;
267                 if (streq("free", tok)) {
268                         sel.status = DESKTOP_STATUS_FREE;
269                 } else if (streq("occupied", tok)) {
270                         sel.status = DESKTOP_STATUS_OCCUPIED;
271                 }
272         }
273
274         dst->monitor = NULL;
275
276         direction_t dir;
277         cycle_dir_t cyc;
278         history_dir_t hdi;
279         int idx;
280         if (parse_direction(desc, &dir)) {
281                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
282         } else if (parse_cycle_direction(desc, &cyc)) {
283                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
284         } else if (parse_history_direction(desc, &hdi)) {
285                 history_find_monitor(hdi, ref, dst, sel);
286         } else if (streq("last", desc)) {
287                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
288         } else if (streq("primary", desc)) {
289                 if (pri_mon != NULL) {
290                         coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
291                         if (desktop_matches(&loc, ref, sel))
292                                 dst->monitor = pri_mon;
293                 }
294         } else if (streq("focused", desc)) {
295                 coordinates_t loc = {mon, mon->desk, NULL};
296                 if (desktop_matches(&loc, ref, sel))
297                         dst->monitor = mon;
298         } else if (parse_index(desc, &idx)) {
299                 monitor_from_index(idx, dst);
300         } else {
301                 locate_monitor(desc, dst);
302         }
303
304         return (dst->monitor != NULL);
305 }
306
307 bool locate_window(xcb_window_t win, coordinates_t *loc)
308 {
309         for (monitor_t *m = mon_head; m != NULL; m = m->next)
310                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
311                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
312                                 if (n->client->window == win) {
313                                         loc->monitor = m;
314                                         loc->desktop = d;
315                                         loc->node = n;
316                                         return true;
317                                 }
318         return false;
319 }
320
321 bool locate_desktop(char *name, coordinates_t *loc)
322 {
323         for (monitor_t *m = mon_head; m != NULL; m = m->next)
324                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
325                         if (streq(d->name, name)) {
326                                 loc->monitor = m;
327                                 loc->desktop = d;
328                                 return true;
329                         }
330         return false;
331 }
332
333 bool locate_monitor(char *name, coordinates_t *loc)
334 {
335         for (monitor_t *m = mon_head; m != NULL; m = m->next)
336                 if (streq(m->name, name)) {
337                         loc->monitor = m;
338                         return true;
339                 }
340         return false;
341 }
342
343 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
344 {
345         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
346                 if (mm != NULL && m != mm)
347                         continue;
348                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
349                         if (i == 1) {
350                                 loc->monitor = m;
351                                 loc->desktop = d;
352                                 loc->node = NULL;
353                                 return true;
354                         }
355         }
356         return false;
357 }
358
359 bool monitor_from_index(int i, coordinates_t *loc)
360 {
361         for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
362                 if (i == 1) {
363                         loc->monitor = m;
364                         loc->desktop = NULL;
365                         loc->node = NULL;
366                         return true;
367                 }
368         return false;
369 }
370
371 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
372 {
373         if (ref->node == NULL || loc->node == NULL)
374                 return false;
375
376         if (sel.type != CLIENT_TYPE_ALL &&
377             is_tiled(loc->node->client)
378             ? sel.type == CLIENT_TYPE_FLOATING
379             : sel.type == CLIENT_TYPE_TILED)
380                 return false;
381
382         if (sel.class != CLIENT_CLASS_ALL &&
383             streq(loc->node->client->class_name, ref->node->client->class_name)
384             ? sel.class == CLIENT_CLASS_DIFFER
385             : sel.class == CLIENT_CLASS_EQUAL)
386                 return false;
387
388         if (sel.mode != CLIENT_MODE_ALL &&
389             loc->node->split_mode == MODE_MANUAL
390             ? sel.mode == CLIENT_MODE_AUTOMATIC
391             : sel.mode == CLIENT_MODE_MANUAL)
392                 return false;
393
394         if (sel.local && loc->desktop != ref->desktop)
395                 return false;
396
397         if (sel.urgent && !loc->node->client->urgent)
398                 return false;
399
400         return true;
401 }
402
403 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
404 {
405         if (sel.status != DESKTOP_STATUS_ALL &&
406             loc->desktop->root == NULL
407             ? sel.status == DESKTOP_STATUS_OCCUPIED
408             : sel.status == DESKTOP_STATUS_FREE)
409                 return false;
410
411         if (sel.urgent && !is_urgent(loc->desktop))
412                 return false;
413
414         if (sel.local && ref->monitor != loc->monitor)
415                 return false;
416
417         return true;
418 }