]> git.lizzy.rs Git - bspwm.git/blob - query.c
6592dc9ed6f65e77859477bdfd11202c226e4990
[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                         sel.layer = malloc(sizeof(stack_layer_t));
236                         *(sel.layer) = LAYER_BELOW;
237                 } else if (streq("normal", tok)) {
238                         sel.layer = malloc(sizeof(stack_layer_t));
239                         *(sel.layer) = LAYER_NORMAL;
240                 } else if (streq("above", tok)) {
241                         sel.layer = malloc(sizeof(stack_layer_t));
242                         *(sel.layer) = LAYER_ABOVE;
243                 }
244         }
245
246         dst->monitor = ref->monitor;
247         dst->desktop = ref->desktop;
248         dst->node = NULL;
249
250         direction_t dir;
251         cycle_dir_t cyc;
252         history_dir_t hdi;
253         if (parse_direction(desc, &dir)) {
254                 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
255                 if (dst->node == NULL && num_monitors > 1) {
256                         monitor_t *m = nearest_monitor(ref->monitor, dir, make_desktop_select());
257                         if (m != NULL) {
258                                 coordinates_t loc = {m, m->desk, m->desk->focus};
259                                 if (node_matches(&loc, ref, sel)) {
260                                         dst->monitor = m;
261                                         dst->desktop = m->desk;
262                                         dst->node = m->desk->focus;
263                                 }
264                         }
265                 }
266         } else if (parse_cycle_direction(desc, &cyc)) {
267                 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
268         } else if (parse_history_direction(desc, &hdi)) {
269                 history_find_node(hdi, ref, dst, sel);
270         } else if (streq("last", desc)) {
271                 history_find_node(HISTORY_OLDER, ref, dst, sel);
272         } else if (streq("biggest", desc)) {
273                 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
274         } else if (streq("focused", desc)) {
275                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
276                 if (node_matches(&loc, ref, sel)) {
277                         dst->monitor = mon;
278                         dst->desktop = mon->desk;
279                         dst->node = mon->desk->focus;
280                 }
281         } else {
282                 long int wid;
283                 if (parse_window_id(desc, &wid))
284                         locate_window(wid, dst);
285         }
286
287         cleanup_client_select(&sel);
288
289         return (dst->node != NULL);
290 }
291
292 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
293 {
294         desktop_select_t sel = make_desktop_select();
295         char *tok;
296         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
297                 tok[0] = '\0';
298                 tok++;
299                 if (streq("occupied", tok)) {
300                         sel.occupied = OPTION_FALSE;
301                 } else if (streq("!occupied", tok)) {
302                         sel.occupied = OPTION_TRUE;
303                 } else if (streq("urgent", tok)) {
304                         sel.urgent = OPTION_TRUE;
305                 } else if (streq("!urgent", tok)) {
306                         sel.urgent = OPTION_FALSE;
307                 } else if (streq("local", tok)) {
308                         sel.local = OPTION_TRUE;
309                 } else if (streq("!local", tok)) {
310                         sel.local = OPTION_FALSE;
311                 }
312         }
313
314         dst->desktop = NULL;
315
316         cycle_dir_t cyc;
317         history_dir_t hdi;
318         char *colon;
319         int idx;
320         if (parse_cycle_direction(desc, &cyc)) {
321                 dst->monitor = ref->monitor;
322                 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
323         } else if (parse_history_direction(desc, &hdi)) {
324                 history_find_desktop(hdi, ref, dst, sel);
325         } else if (streq("last", desc)) {
326                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
327         } else if (streq("focused", desc)) {
328                 coordinates_t loc = {mon, mon->desk, NULL};
329                 if (desktop_matches(&loc, ref, sel)) {
330                         dst->monitor = mon;
331                         dst->desktop = mon->desk;
332                 }
333         } else if ((colon = strchr(desc, ':')) != NULL) {
334                 *colon = '\0';
335                 if (monitor_from_desc(desc, ref, dst)) {
336                         if (streq("focused", colon + 1)) {
337                                 dst->desktop = dst->monitor->desk;
338                         } else if (parse_index(colon + 1, &idx)) {
339                                 desktop_from_index(idx, dst, dst->monitor);
340                         }
341                 }
342         } else if (parse_index(desc, &idx)) {
343                 desktop_from_index(idx, dst, NULL);
344         } else {
345                 locate_desktop(desc, dst);
346         }
347
348         return (dst->desktop != NULL);
349 }
350
351 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
352 {
353         desktop_select_t sel = make_desktop_select();
354         char *tok;
355         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
356                 tok[0] = '\0';
357                 tok++;
358                 if (streq("occupied", tok)) {
359                         sel.occupied = OPTION_FALSE;
360                 } else if (streq("!occupied", tok)) {
361                         sel.occupied = OPTION_TRUE;
362                 }
363         }
364
365         dst->monitor = NULL;
366
367         direction_t dir;
368         cycle_dir_t cyc;
369         history_dir_t hdi;
370         int idx;
371         if (parse_direction(desc, &dir)) {
372                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
373         } else if (parse_cycle_direction(desc, &cyc)) {
374                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
375         } else if (parse_history_direction(desc, &hdi)) {
376                 history_find_monitor(hdi, ref, dst, sel);
377         } else if (streq("last", desc)) {
378                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
379         } else if (streq("primary", desc)) {
380                 if (pri_mon != NULL) {
381                         coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
382                         if (desktop_matches(&loc, ref, sel))
383                                 dst->monitor = pri_mon;
384                 }
385         } else if (streq("focused", desc)) {
386                 coordinates_t loc = {mon, mon->desk, NULL};
387                 if (desktop_matches(&loc, ref, sel))
388                         dst->monitor = mon;
389         } else if (parse_index(desc, &idx)) {
390                 monitor_from_index(idx, dst);
391         } else {
392                 locate_monitor(desc, dst);
393         }
394
395         return (dst->monitor != NULL);
396 }
397
398 bool locate_window(xcb_window_t win, coordinates_t *loc)
399 {
400         for (monitor_t *m = mon_head; m != NULL; m = m->next)
401                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
402                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
403                                 if (n->client->window == win) {
404                                         loc->monitor = m;
405                                         loc->desktop = d;
406                                         loc->node = n;
407                                         return true;
408                                 }
409         return false;
410 }
411
412 bool locate_desktop(char *name, coordinates_t *loc)
413 {
414         for (monitor_t *m = mon_head; m != NULL; m = m->next)
415                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
416                         if (streq(d->name, name)) {
417                                 loc->monitor = m;
418                                 loc->desktop = d;
419                                 return true;
420                         }
421         return false;
422 }
423
424 bool locate_monitor(char *name, coordinates_t *loc)
425 {
426         for (monitor_t *m = mon_head; m != NULL; m = m->next)
427                 if (streq(m->name, name)) {
428                         loc->monitor = m;
429                         return true;
430                 }
431         return false;
432 }
433
434 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
435 {
436         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
437                 if (mm != NULL && m != mm)
438                         continue;
439                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
440                         if (i == 1) {
441                                 loc->monitor = m;
442                                 loc->desktop = d;
443                                 loc->node = NULL;
444                                 return true;
445                         }
446         }
447         return false;
448 }
449
450 bool monitor_from_index(int i, coordinates_t *loc)
451 {
452         for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
453                 if (i == 1) {
454                         loc->monitor = m;
455                         loc->desktop = NULL;
456                         loc->node = NULL;
457                         return true;
458                 }
459         return false;
460 }
461
462 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
463 {
464         if (loc->node == NULL)
465                 return false;
466
467 #define WSTATE(prop) \
468         if (sel.prop != OPTION_NONE && \
469             !loc->node->client->prop \
470             ? sel.prop == OPTION_TRUE \
471             : sel.prop == OPTION_FALSE) { \
472                 return false; \
473         }
474         WSTATE(locked)
475         WSTATE(sticky)
476         WSTATE(private)
477         WSTATE(urgent)
478 #undef MATCHSTATE
479
480         if (sel.tiled != OPTION_NONE &&
481             loc->node->client->state != STATE_TILED
482             ? sel.tiled == OPTION_TRUE
483             : sel.tiled == OPTION_FALSE) {
484                 return false;
485         }
486
487         if (sel.pseudo_tiled != OPTION_NONE &&
488             loc->node->client->state != STATE_PSEUDO_TILED
489             ? sel.pseudo_tiled == OPTION_TRUE
490             : sel.pseudo_tiled == OPTION_FALSE) {
491                 return false;
492         }
493
494         if (sel.floating != OPTION_NONE &&
495             loc->node->client->state != STATE_FLOATING
496             ? sel.floating == OPTION_TRUE
497             : sel.floating == OPTION_FALSE) {
498                 return false;
499         }
500
501         if (sel.fullscreen != OPTION_NONE &&
502             loc->node->client->state != STATE_FULLSCREEN
503             ? sel.fullscreen == OPTION_TRUE
504             : sel.fullscreen == OPTION_FALSE) {
505                 return false;
506         }
507
508         if (sel.same_class != OPTION_NONE && ref->node != NULL &&
509             streq(loc->node->client->class_name, ref->node->client->class_name)
510             ? sel.same_class == OPTION_FALSE
511             : sel.same_class == OPTION_TRUE) {
512                 return false;
513         }
514
515         if (sel.automatic != OPTION_NONE &&
516             loc->node->split_mode == MODE_MANUAL
517             ? sel.automatic == OPTION_TRUE
518             : sel.automatic == OPTION_FALSE) {
519                 return false;
520         }
521
522         if (sel.local != OPTION_NONE &&
523             loc->desktop != ref->desktop
524             ? sel.local == OPTION_TRUE
525             : sel.local == OPTION_FALSE) {
526                 return false;
527         }
528
529         if (sel.layer != NULL && loc->node->client->layer != *(sel.layer)) {
530                 return false;
531         }
532
533         if (sel.focused != OPTION_NONE &&
534             loc->node != loc->desktop->focus
535             ? sel.focused == OPTION_TRUE
536             : sel.focused == OPTION_FALSE) {
537                 return false;
538         }
539
540         return true;
541 }
542
543 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
544 {
545         if (sel.occupied != OPTION_NONE &&
546             loc->desktop->root == NULL
547             ? sel.occupied == OPTION_TRUE
548             : sel.occupied == OPTION_FALSE) {
549                 return false;
550         }
551
552         if (sel.urgent != OPTION_NONE &&
553             !is_urgent(loc->desktop)
554             ? sel.urgent == OPTION_TRUE
555             : sel.urgent == OPTION_FALSE) {
556                 return false;
557         }
558
559         if (sel.local != OPTION_NONE &&
560             ref->monitor != loc->monitor
561             ? sel.local == OPTION_TRUE
562             : sel.local == OPTION_FALSE) {
563                 return false;
564         }
565
566         return true;
567 }