]> git.lizzy.rs Git - bspwm.git/blob - query.c
Prevent potential memory leak
[bspwm.git] / query.c
1 /* Copyright (c) 2012, 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
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <strings.h>
28 #include <string.h>
29 #include "bspwm.h"
30 #include "desktop.h"
31 #include "history.h"
32 #include "messages.h"
33 #include "monitor.h"
34 #include "tree.h"
35 #include "query.h"
36
37 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
38 {
39         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
40                 if (loc.monitor != NULL && m != loc.monitor)
41                         continue;
42                 if (dom != DOMAIN_DESKTOP) {
43                         if (dom == DOMAIN_MONITOR) {
44                                 fprintf(rsp, "%s\n", m->name);
45                                 continue;
46                         } else {
47                                 fprintf(rsp, "%s %ux%u%+i%+i %i,%i,%i,%i%s\n", m->name,
48                                          m->rectangle.width,m->rectangle.height, m->rectangle.x, m->rectangle.y,
49                                          m->top_padding, m->right_padding, m->bottom_padding, m->left_padding,
50                                          (m == mon ? " *" : ""));
51                         }
52                 }
53                 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
54         }
55 }
56
57 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
58 {
59         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
60                 if (loc.desktop != NULL && d != loc.desktop)
61                         continue;
62                 for (unsigned int i = 0; i < depth; i++)
63                         fprintf(rsp, "\t");
64                 if (dom == DOMAIN_DESKTOP) {
65                         fprintf(rsp, "%s\n", d->name);
66                         continue;
67                 } else {
68                         fprintf(rsp, "%s %u %i %i,%i,%i,%i %c%s\n", d->name, d->border_width,
69                                 d->window_gap,
70                                 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
71                                 (d->layout == LAYOUT_TILED ? 'T' : 'M'),
72                                 (d == m->desk ? " *" : ""));
73                 }
74                 query_tree(d, d->root, rsp, depth + 1);
75         }
76 }
77
78 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
79 {
80         if (n == NULL)
81                 return;
82
83         for (unsigned int i = 0; i < depth; i++)
84                 fprintf(rsp, "\t");
85
86         if (is_leaf(n)) {
87                 client_t *c = n->client;
88                 fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c%c %c%c %c%c%c%c%s\n",
89                          (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
90                          c->class_name, c->instance_name, c->window, c->border_width,
91                          c->floating_rectangle.width, c->floating_rectangle.height,
92                          c->floating_rectangle.x, c->floating_rectangle.y,
93                          (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))),
94                          (n->split_mode == MODE_AUTOMATIC ? '-' : 'p'),
95                          (c->state == STATE_TILED ? '-' : (c->state == STATE_FLOATING ? 'f' : (c->state == STATE_FULLSCREEN ? 'F' : 'p'))),
96                          (c->layer == LAYER_NORMAL ? '-' : (c->layer == LAYER_ABOVE ? 'a' : 'b')),
97                          (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (c->private ? 'i' : '-'),
98                          (n == d->focus ? " *" : ""));
99         } else {
100                 fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
101                         (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
102         }
103
104         query_tree(d, n->first_child, rsp, depth + 1);
105         query_tree(d, n->second_child, rsp, depth + 1);
106 }
107
108 void query_history(coordinates_t loc, FILE *rsp)
109 {
110         for (history_t *h = history_head; h != NULL; h = h->next) {
111                 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
112                                 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
113                         continue;
114                 xcb_window_t win = XCB_NONE;
115                 if (h->loc.node != NULL)
116                         win = h->loc.node->client->window;
117                 fprintf(rsp, "%s %s 0x%X\n", h->loc.monitor->name, h->loc.desktop->name, win);
118         }
119 }
120
121 void query_stack(FILE *rsp)
122 {
123         for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
124                 fprintf(rsp, "0x%X\n", s->node->client->window);
125 }
126
127 void query_windows(coordinates_t loc, FILE *rsp)
128 {
129         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
130                 if (loc.monitor != NULL && m != loc.monitor)
131                         continue;
132                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
133                         if (loc.desktop != NULL && d != loc.desktop)
134                                 continue;
135                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
136                                 if (loc.node != NULL && n != loc.node)
137                                         continue;
138                                 fprintf(rsp, "0x%X\n", n->client->window);
139                         }
140                 }
141         }
142 }
143
144 client_select_t make_client_select(void)
145 {
146         client_select_t sel = {
147                 OPTION_NONE,
148                 OPTION_NONE,
149                 OPTION_NONE,
150                 OPTION_NONE,
151                 OPTION_NONE,
152                 OPTION_NONE,
153                 OPTION_NONE,
154                 OPTION_NONE,
155                 OPTION_NONE,
156                 OPTION_NONE,
157                 OPTION_NONE,
158                 OPTION_NONE,
159                 NULL
160         };
161         return sel;
162 }
163
164 desktop_select_t make_desktop_select(void)
165 {
166         desktop_select_t sel = {
167                 OPTION_NONE,
168                 OPTION_NONE,
169                 OPTION_NONE
170         };
171         return sel;
172 }
173
174 void cleanup_client_select(client_select_t *sel)
175 {
176         free(sel->layer);
177 }
178
179 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
180 {
181         client_select_t sel = make_client_select();
182         char *tok;
183         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
184                 tok[0] = '\0';
185                 tok++;
186                 if (streq("tiled", tok)) {
187                         sel.tiled = OPTION_TRUE;
188                 } else if (streq("!tiled", tok)) {
189                         sel.tiled = OPTION_FALSE;
190                 } else if (streq("pseudo_tiled", tok)) {
191                         sel.pseudo_tiled = OPTION_TRUE;
192                 } else if (streq("!pseudo_tiled", tok)) {
193                         sel.pseudo_tiled = OPTION_FALSE;
194                 } else if (streq("floating", tok)) {
195                         sel.floating = OPTION_TRUE;
196                 } else if (streq("!floating", tok)) {
197                         sel.floating = OPTION_FALSE;
198                 } else if (streq("same_class", tok)) {
199                         sel.same_class = OPTION_TRUE;
200                 } else if (streq("!same_class", tok)) {
201                         sel.same_class = OPTION_FALSE;
202                 } else if (streq("automatic", tok)) {
203                         sel.automatic = OPTION_TRUE;
204                 } else if (streq("!automatic", tok)) {
205                         sel.automatic = OPTION_FALSE;
206                 } else if (streq("fullscreen", tok)) {
207                         sel.fullscreen = OPTION_TRUE;
208                 } else if (streq("!fullscreen", tok)) {
209                         sel.fullscreen = OPTION_FALSE;
210                 } else if (streq("urgent", tok)) {
211                         sel.urgent = OPTION_TRUE;
212                 } else if (streq("!urgent", tok)) {
213                         sel.urgent = OPTION_FALSE;
214                 } else if (streq("local", tok)) {
215                         sel.local = OPTION_TRUE;
216                 } else if (streq("!local", tok)) {
217                         sel.local = OPTION_FALSE;
218                 } else if (streq("private", tok)) {
219                         sel.private = OPTION_TRUE;
220                 } else if (streq("!private", tok)) {
221                         sel.private = OPTION_FALSE;
222                 } else if (streq("sticky", tok)) {
223                         sel.sticky = OPTION_TRUE;
224                 } else if (streq("!sticky", tok)) {
225                         sel.sticky = OPTION_FALSE;
226                 } else if (streq("locked", tok)) {
227                         sel.locked = OPTION_TRUE;
228                 } else if (streq("!locked", tok)) {
229                         sel.locked = OPTION_FALSE;
230                 } else if (streq("focused", tok)) {
231                         sel.focused = OPTION_TRUE;
232                 } else if (streq("!focused", tok)) {
233                         sel.focused = OPTION_FALSE;
234                 } else if (streq("below", tok)) {
235                         if (sel.layer == NULL) {
236                                 sel.layer = malloc(sizeof(stack_layer_t));
237                         }
238                         *(sel.layer) = LAYER_BELOW;
239                 } else if (streq("normal", tok)) {
240                         if (sel.layer == NULL) {
241                                 sel.layer = malloc(sizeof(stack_layer_t));
242                         }
243                         *(sel.layer) = LAYER_NORMAL;
244                 } else if (streq("above", tok)) {
245                         if (sel.layer == NULL) {
246                                 sel.layer = malloc(sizeof(stack_layer_t));
247                         }
248                         *(sel.layer) = LAYER_ABOVE;
249                 }
250         }
251
252         dst->monitor = ref->monitor;
253         dst->desktop = ref->desktop;
254         dst->node = NULL;
255
256         direction_t dir;
257         cycle_dir_t cyc;
258         history_dir_t hdi;
259         if (parse_direction(desc, &dir)) {
260                 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
261                 if (dst->node == NULL && num_monitors > 1) {
262                         monitor_t *m = nearest_monitor(ref->monitor, dir, make_desktop_select());
263                         if (m != NULL) {
264                                 coordinates_t loc = {m, m->desk, m->desk->focus};
265                                 if (node_matches(&loc, ref, sel)) {
266                                         dst->monitor = m;
267                                         dst->desktop = m->desk;
268                                         dst->node = m->desk->focus;
269                                 }
270                         }
271                 }
272         } else if (parse_cycle_direction(desc, &cyc)) {
273                 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
274         } else if (parse_history_direction(desc, &hdi)) {
275                 history_find_node(hdi, ref, dst, sel);
276         } else if (streq("last", desc)) {
277                 history_find_node(HISTORY_OLDER, ref, dst, sel);
278         } else if (streq("biggest", desc)) {
279                 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
280         } else if (streq("focused", desc)) {
281                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
282                 if (node_matches(&loc, ref, sel)) {
283                         dst->monitor = mon;
284                         dst->desktop = mon->desk;
285                         dst->node = mon->desk->focus;
286                 }
287         } else {
288                 long int wid;
289                 if (parse_window_id(desc, &wid))
290                         locate_window(wid, dst);
291         }
292
293         cleanup_client_select(&sel);
294
295         return (dst->node != NULL);
296 }
297
298 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
299 {
300         desktop_select_t sel = make_desktop_select();
301         char *tok;
302         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
303                 tok[0] = '\0';
304                 tok++;
305                 if (streq("occupied", tok)) {
306                         sel.occupied = OPTION_FALSE;
307                 } else if (streq("!occupied", tok)) {
308                         sel.occupied = OPTION_TRUE;
309                 } else if (streq("urgent", tok)) {
310                         sel.urgent = OPTION_TRUE;
311                 } else if (streq("!urgent", tok)) {
312                         sel.urgent = OPTION_FALSE;
313                 } else if (streq("local", tok)) {
314                         sel.local = OPTION_TRUE;
315                 } else if (streq("!local", tok)) {
316                         sel.local = OPTION_FALSE;
317                 }
318         }
319
320         dst->desktop = NULL;
321
322         cycle_dir_t cyc;
323         history_dir_t hdi;
324         char *colon;
325         int idx;
326         if (parse_cycle_direction(desc, &cyc)) {
327                 dst->monitor = ref->monitor;
328                 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
329         } else if (parse_history_direction(desc, &hdi)) {
330                 history_find_desktop(hdi, ref, dst, sel);
331         } else if (streq("last", desc)) {
332                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
333         } else if (streq("focused", desc)) {
334                 coordinates_t loc = {mon, mon->desk, NULL};
335                 if (desktop_matches(&loc, ref, sel)) {
336                         dst->monitor = mon;
337                         dst->desktop = mon->desk;
338                 }
339         } else if ((colon = strchr(desc, ':')) != NULL) {
340                 *colon = '\0';
341                 if (monitor_from_desc(desc, ref, dst)) {
342                         if (streq("focused", colon + 1)) {
343                                 dst->desktop = dst->monitor->desk;
344                         } else if (parse_index(colon + 1, &idx)) {
345                                 desktop_from_index(idx, dst, dst->monitor);
346                         }
347                 }
348         } else if (parse_index(desc, &idx)) {
349                 desktop_from_index(idx, dst, NULL);
350         } else {
351                 locate_desktop(desc, dst);
352         }
353
354         return (dst->desktop != NULL);
355 }
356
357 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
358 {
359         desktop_select_t sel = make_desktop_select();
360         char *tok;
361         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
362                 tok[0] = '\0';
363                 tok++;
364                 if (streq("occupied", tok)) {
365                         sel.occupied = OPTION_FALSE;
366                 } else if (streq("!occupied", tok)) {
367                         sel.occupied = OPTION_TRUE;
368                 }
369         }
370
371         dst->monitor = NULL;
372
373         direction_t dir;
374         cycle_dir_t cyc;
375         history_dir_t hdi;
376         int idx;
377         if (parse_direction(desc, &dir)) {
378                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
379         } else if (parse_cycle_direction(desc, &cyc)) {
380                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
381         } else if (parse_history_direction(desc, &hdi)) {
382                 history_find_monitor(hdi, ref, dst, sel);
383         } else if (streq("last", desc)) {
384                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
385         } else if (streq("primary", desc)) {
386                 if (pri_mon != NULL) {
387                         coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
388                         if (desktop_matches(&loc, ref, sel))
389                                 dst->monitor = pri_mon;
390                 }
391         } else if (streq("focused", desc)) {
392                 coordinates_t loc = {mon, mon->desk, NULL};
393                 if (desktop_matches(&loc, ref, sel))
394                         dst->monitor = mon;
395         } else if (parse_index(desc, &idx)) {
396                 monitor_from_index(idx, dst);
397         } else {
398                 locate_monitor(desc, dst);
399         }
400
401         return (dst->monitor != NULL);
402 }
403
404 bool locate_window(xcb_window_t win, coordinates_t *loc)
405 {
406         for (monitor_t *m = mon_head; m != NULL; m = m->next)
407                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
408                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
409                                 if (n->client->window == win) {
410                                         loc->monitor = m;
411                                         loc->desktop = d;
412                                         loc->node = n;
413                                         return true;
414                                 }
415         return false;
416 }
417
418 bool locate_desktop(char *name, coordinates_t *loc)
419 {
420         for (monitor_t *m = mon_head; m != NULL; m = m->next)
421                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
422                         if (streq(d->name, name)) {
423                                 loc->monitor = m;
424                                 loc->desktop = d;
425                                 return true;
426                         }
427         return false;
428 }
429
430 bool locate_monitor(char *name, coordinates_t *loc)
431 {
432         for (monitor_t *m = mon_head; m != NULL; m = m->next)
433                 if (streq(m->name, name)) {
434                         loc->monitor = m;
435                         return true;
436                 }
437         return false;
438 }
439
440 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
441 {
442         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
443                 if (mm != NULL && m != mm)
444                         continue;
445                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
446                         if (i == 1) {
447                                 loc->monitor = m;
448                                 loc->desktop = d;
449                                 loc->node = NULL;
450                                 return true;
451                         }
452         }
453         return false;
454 }
455
456 bool monitor_from_index(int i, coordinates_t *loc)
457 {
458         for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
459                 if (i == 1) {
460                         loc->monitor = m;
461                         loc->desktop = NULL;
462                         loc->node = NULL;
463                         return true;
464                 }
465         return false;
466 }
467
468 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
469 {
470         if (loc->node == NULL)
471                 return false;
472
473 #define WSTATE(prop) \
474         if (sel.prop != OPTION_NONE && \
475             !loc->node->client->prop \
476             ? sel.prop == OPTION_TRUE \
477             : sel.prop == OPTION_FALSE) { \
478                 return false; \
479         }
480         WSTATE(locked)
481         WSTATE(sticky)
482         WSTATE(private)
483         WSTATE(urgent)
484 #undef MATCHSTATE
485
486         if (sel.tiled != OPTION_NONE &&
487             loc->node->client->state != STATE_TILED
488             ? sel.tiled == OPTION_TRUE
489             : sel.tiled == OPTION_FALSE) {
490                 return false;
491         }
492
493         if (sel.pseudo_tiled != OPTION_NONE &&
494             loc->node->client->state != STATE_PSEUDO_TILED
495             ? sel.pseudo_tiled == OPTION_TRUE
496             : sel.pseudo_tiled == OPTION_FALSE) {
497                 return false;
498         }
499
500         if (sel.floating != OPTION_NONE &&
501             loc->node->client->state != STATE_FLOATING
502             ? sel.floating == OPTION_TRUE
503             : sel.floating == OPTION_FALSE) {
504                 return false;
505         }
506
507         if (sel.fullscreen != OPTION_NONE &&
508             loc->node->client->state != STATE_FULLSCREEN
509             ? sel.fullscreen == OPTION_TRUE
510             : sel.fullscreen == OPTION_FALSE) {
511                 return false;
512         }
513
514         if (sel.same_class != OPTION_NONE && ref->node != NULL &&
515             streq(loc->node->client->class_name, ref->node->client->class_name)
516             ? sel.same_class == OPTION_FALSE
517             : sel.same_class == OPTION_TRUE) {
518                 return false;
519         }
520
521         if (sel.automatic != OPTION_NONE &&
522             loc->node->split_mode == MODE_MANUAL
523             ? sel.automatic == OPTION_TRUE
524             : sel.automatic == OPTION_FALSE) {
525                 return false;
526         }
527
528         if (sel.local != OPTION_NONE &&
529             loc->desktop != ref->desktop
530             ? sel.local == OPTION_TRUE
531             : sel.local == OPTION_FALSE) {
532                 return false;
533         }
534
535         if (sel.layer != NULL && loc->node->client->layer != *(sel.layer)) {
536                 return false;
537         }
538
539         if (sel.focused != OPTION_NONE &&
540             loc->node != loc->desktop->focus
541             ? sel.focused == OPTION_TRUE
542             : sel.focused == OPTION_FALSE) {
543                 return false;
544         }
545
546         return true;
547 }
548
549 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
550 {
551         if (sel.occupied != OPTION_NONE &&
552             loc->desktop->root == NULL
553             ? sel.occupied == OPTION_TRUE
554             : sel.occupied == OPTION_FALSE) {
555                 return false;
556         }
557
558         if (sel.urgent != OPTION_NONE &&
559             !is_urgent(loc->desktop)
560             ? sel.urgent == OPTION_TRUE
561             : sel.urgent == OPTION_FALSE) {
562                 return false;
563         }
564
565         if (sel.local != OPTION_NONE &&
566             ref->monitor != loc->monitor
567             ? sel.local == OPTION_TRUE
568             : sel.local == OPTION_FALSE) {
569                 return false;
570         }
571
572         return true;
573 }