]> git.lizzy.rs Git - bspwm.git/blob - query.c
Rewrite the handling of overlapping monitors
[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 <strings.h>
27 #include <string.h>
28 #include "bspwm.h"
29 #include "desktop.h"
30 #include "history.h"
31 #include "messages.h"
32 #include "monitor.h"
33 #include "tree.h"
34 #include "query.h"
35
36 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
37 {
38         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
39                 if (loc.monitor != NULL && m != loc.monitor)
40                         continue;
41                 if (dom != DOMAIN_DESKTOP) {
42                         if (dom == DOMAIN_MONITOR) {
43                                 fprintf(rsp, "%s\n", m->name);
44                                 continue;
45                         } else {
46                                 fprintf(rsp, "%s %ux%u%+i%+i %i,%i,%i,%i%s\n", m->name,
47                                          m->rectangle.width,m->rectangle.height, m->rectangle.x, m->rectangle.y,
48                                          m->top_padding, m->right_padding, m->bottom_padding, m->left_padding,
49                                          (m == mon ? " *" : ""));
50                         }
51                 }
52                 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
53         }
54 }
55
56 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
57 {
58         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
59                 if (loc.desktop != NULL && d != loc.desktop)
60                         continue;
61                 for (unsigned int i = 0; i < depth; i++)
62                         fprintf(rsp, "\t");
63                 if (dom == DOMAIN_DESKTOP) {
64                         fprintf(rsp, "%s\n", d->name);
65                         continue;
66                 } else {
67                         fprintf(rsp, "%s %u %i %i,%i,%i,%i %c %c%s\n", d->name, d->border_width,
68                                 d->window_gap,
69                                 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
70                                 (d->layout == LAYOUT_TILED ? 'T' : 'M'), (d->floating ? 'f' : '-'),
71                                 (d == m->desk ? " *" : ""));
72                 }
73                 query_tree(d, d->root, rsp, depth + 1);
74         }
75 }
76
77 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
78 {
79         if (n == NULL)
80                 return;
81
82         for (unsigned int i = 0; i < depth; i++)
83                 fprintf(rsp, "\t");
84
85         if (is_leaf(n)) {
86                 client_t *c = n->client;
87                 fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c %c%c%c%c%c%c%c%c%s\n",
88                          (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
89                          c->class_name, c->instance_name, c->window, c->border_width,
90                          c->floating_rectangle.width, c->floating_rectangle.height,
91                          c->floating_rectangle.x, c->floating_rectangle.y,
92                          (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))),
93                          (c->floating ? 'f' : '-'), (c->pseudo_tiled ? 'd' : '-'), (c->fullscreen ? 'F' : '-'),
94                          (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'),
95                          (c->private ? 'i' : '-'), (n->split_mode ? 'p' : '-'),
96                          (n == d->focus ? " *" : ""));
97         } else {
98                 fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
99                         (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
100         }
101
102         query_tree(d, n->first_child, rsp, depth + 1);
103         query_tree(d, n->second_child, rsp, depth + 1);
104 }
105
106 void query_history(coordinates_t loc, FILE *rsp)
107 {
108         for (history_t *h = history_head; h != NULL; h = h->next) {
109                 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
110                                 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
111                         continue;
112                 xcb_window_t win = XCB_NONE;
113                 if (h->loc.node != NULL)
114                         win = h->loc.node->client->window;
115                 fprintf(rsp, "%s %s 0x%X\n", h->loc.monitor->name, h->loc.desktop->name, win);
116         }
117 }
118
119 void query_stack(FILE *rsp)
120 {
121         for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
122                 fprintf(rsp, "0x%X\n", s->node->client->window);
123 }
124
125 void query_windows(coordinates_t loc, FILE *rsp)
126 {
127         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
128                 if (loc.monitor != NULL && m != loc.monitor)
129                         continue;
130                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
131                         if (loc.desktop != NULL && d != loc.desktop)
132                                 continue;
133                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
134                                 if (loc.node != NULL && n != loc.node)
135                                         continue;
136                                 fprintf(rsp, "0x%X\n", n->client->window);
137                         }
138                 }
139         }
140 }
141
142 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
143 {
144         client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, CLIENT_MODE_ALL, false, false, false};
145         char *tok;
146         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
147                 tok[0] = '\0';
148                 tok++;
149                 if (streq("tiled", tok)) {
150                         sel.type = CLIENT_TYPE_TILED;
151                 } else if (streq("floating", tok)) {
152                         sel.type = CLIENT_TYPE_FLOATING;
153                 } else if (streq("like", tok)) {
154                         sel.class = CLIENT_CLASS_EQUAL;
155                 } else if (streq("unlike", tok)) {
156                         sel.class = CLIENT_CLASS_DIFFER;
157                 } else if (streq("manual", tok)) {
158                         sel.mode = CLIENT_MODE_MANUAL;
159                 } else if (streq("automatic", tok)) {
160                         sel.mode = CLIENT_MODE_AUTOMATIC;
161                 } else if (streq("urgent", tok)) {
162                         sel.urgent = true;
163                 } else if (streq("local", tok)) {
164                         sel.local = true;
165                 } else if (streq("unfocused", tok)) {
166                         sel.unfocused = true;
167                 }
168         }
169
170         dst->monitor = ref->monitor;
171         dst->desktop = ref->desktop;
172         dst->node = NULL;
173
174         direction_t dir;
175         cycle_dir_t cyc;
176         history_dir_t hdi;
177         if (parse_direction(desc, &dir)) {
178                 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
179                 if (dst->node == NULL && num_monitors > 1) {
180                         monitor_t *m = nearest_monitor(ref->monitor, dir, (desktop_select_t) {DESKTOP_STATUS_ALL, false, false});
181                         if (m != NULL) {
182                                 coordinates_t loc = {m, m->desk, m->desk->focus};
183                                 if (node_matches(&loc, ref, sel)) {
184                                         dst->monitor = m;
185                                         dst->desktop = m->desk;
186                                         dst->node = m->desk->focus;
187                                 }
188                         }
189                 }
190         } else if (parse_cycle_direction(desc, &cyc)) {
191                 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
192         } else if (parse_history_direction(desc, &hdi)) {
193                 history_find_node(hdi, ref, dst, sel);
194         } else if (streq("last", desc)) {
195                 history_find_node(HISTORY_OLDER, ref, dst, sel);
196         } else if (streq("biggest", desc)) {
197                 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
198         } else if (streq("focused", desc)) {
199                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
200                 if (node_matches(&loc, ref, sel)) {
201                         dst->monitor = mon;
202                         dst->desktop = mon->desk;
203                         dst->node = mon->desk->focus;
204                 }
205         } else {
206                 long int wid;
207                 if (parse_window_id(desc, &wid))
208                         locate_window(wid, dst);
209         }
210
211         return (dst->node != NULL);
212 }
213
214 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
215 {
216         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
217         char *tok;
218         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
219                 tok[0] = '\0';
220                 tok++;
221                 if (streq("free", tok)) {
222                         sel.status = DESKTOP_STATUS_FREE;
223                 } else if (streq("occupied", tok)) {
224                         sel.status = DESKTOP_STATUS_OCCUPIED;
225                 } else if (streq("urgent", tok)) {
226                         sel.urgent = true;
227                 } else if (streq("local", tok)) {
228                         sel.local = true;
229                 }
230         }
231
232         dst->desktop = NULL;
233
234         cycle_dir_t cyc;
235         history_dir_t hdi;
236         char *colon;
237         int idx;
238         if (parse_cycle_direction(desc, &cyc)) {
239                 dst->monitor = ref->monitor;
240                 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
241         } else if (parse_history_direction(desc, &hdi)) {
242                 history_find_desktop(hdi, ref, dst, sel);
243         } else if (streq("last", desc)) {
244                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
245         } else if (streq("focused", desc)) {
246                 coordinates_t loc = {mon, mon->desk, NULL};
247                 if (desktop_matches(&loc, ref, sel)) {
248                         dst->monitor = mon;
249                         dst->desktop = mon->desk;
250                 }
251         } else if ((colon = strchr(desc, ':')) != NULL) {
252                 *colon = '\0';
253                 if (monitor_from_desc(desc, ref, dst)) {
254                         if (streq("focused", colon + 1)) {
255                                 dst->desktop = dst->monitor->desk;
256                         } else if (parse_index(colon + 1, &idx)) {
257                                 desktop_from_index(idx, dst, dst->monitor);
258                         }
259                 }
260         } else if (parse_index(desc, &idx)) {
261                 desktop_from_index(idx, dst, NULL);
262         } else {
263                 locate_desktop(desc, dst);
264         }
265
266         return (dst->desktop != NULL);
267 }
268
269 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
270 {
271         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
272         char *tok;
273         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
274                 tok[0] = '\0';
275                 tok++;
276                 if (streq("free", tok)) {
277                         sel.status = DESKTOP_STATUS_FREE;
278                 } else if (streq("occupied", tok)) {
279                         sel.status = DESKTOP_STATUS_OCCUPIED;
280                 }
281         }
282
283         dst->monitor = NULL;
284
285         direction_t dir;
286         cycle_dir_t cyc;
287         history_dir_t hdi;
288         int idx;
289         if (parse_direction(desc, &dir)) {
290                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
291         } else if (parse_cycle_direction(desc, &cyc)) {
292                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
293         } else if (parse_history_direction(desc, &hdi)) {
294                 history_find_monitor(hdi, ref, dst, sel);
295         } else if (streq("last", desc)) {
296                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
297         } else if (streq("primary", desc)) {
298                 if (pri_mon != NULL) {
299                         coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
300                         if (desktop_matches(&loc, ref, sel))
301                                 dst->monitor = pri_mon;
302                 }
303         } else if (streq("focused", desc)) {
304                 coordinates_t loc = {mon, mon->desk, NULL};
305                 if (desktop_matches(&loc, ref, sel))
306                         dst->monitor = mon;
307         } else if (parse_index(desc, &idx)) {
308                 monitor_from_index(idx, dst);
309         } else {
310                 locate_monitor(desc, dst);
311         }
312
313         return (dst->monitor != NULL);
314 }
315
316 bool locate_window(xcb_window_t win, coordinates_t *loc)
317 {
318         for (monitor_t *m = mon_head; m != NULL; m = m->next)
319                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
320                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
321                                 if (n->client->window == win) {
322                                         loc->monitor = m;
323                                         loc->desktop = d;
324                                         loc->node = n;
325                                         return true;
326                                 }
327         return false;
328 }
329
330 bool locate_desktop(char *name, coordinates_t *loc)
331 {
332         for (monitor_t *m = mon_head; m != NULL; m = m->next)
333                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
334                         if (streq(d->name, name)) {
335                                 loc->monitor = m;
336                                 loc->desktop = d;
337                                 return true;
338                         }
339         return false;
340 }
341
342 bool locate_monitor(char *name, coordinates_t *loc)
343 {
344         for (monitor_t *m = mon_head; m != NULL; m = m->next)
345                 if (streq(m->name, name)) {
346                         loc->monitor = m;
347                         return true;
348                 }
349         return false;
350 }
351
352 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
353 {
354         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
355                 if (mm != NULL && m != mm)
356                         continue;
357                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
358                         if (i == 1) {
359                                 loc->monitor = m;
360                                 loc->desktop = d;
361                                 loc->node = NULL;
362                                 return true;
363                         }
364         }
365         return false;
366 }
367
368 bool monitor_from_index(int i, coordinates_t *loc)
369 {
370         for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
371                 if (i == 1) {
372                         loc->monitor = m;
373                         loc->desktop = NULL;
374                         loc->node = NULL;
375                         return true;
376                 }
377         return false;
378 }
379
380 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
381 {
382         if (loc->node == NULL)
383                 return false;
384
385         if (sel.type != CLIENT_TYPE_ALL &&
386             is_tiled(loc->node->client)
387             ? sel.type == CLIENT_TYPE_FLOATING
388             : sel.type == CLIENT_TYPE_TILED)
389                 return false;
390
391         if (sel.class != CLIENT_CLASS_ALL && ref->node != NULL &&
392             streq(loc->node->client->class_name, ref->node->client->class_name)
393             ? sel.class == CLIENT_CLASS_DIFFER
394             : sel.class == CLIENT_CLASS_EQUAL)
395                 return false;
396
397         if (sel.mode != CLIENT_MODE_ALL &&
398             loc->node->split_mode == MODE_MANUAL
399             ? sel.mode == CLIENT_MODE_AUTOMATIC
400             : sel.mode == CLIENT_MODE_MANUAL)
401                 return false;
402
403         if (sel.local && loc->desktop != ref->desktop)
404                 return false;
405
406         if (sel.urgent && !loc->node->client->urgent)
407                 return false;
408
409         if (sel.unfocused && loc->node == mon->desk->focus)
410                 return false;
411
412         return true;
413 }
414
415 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
416 {
417         if (sel.status != DESKTOP_STATUS_ALL &&
418             loc->desktop->root == NULL
419             ? sel.status == DESKTOP_STATUS_OCCUPIED
420             : sel.status == DESKTOP_STATUS_FREE)
421                 return false;
422
423         if (sel.urgent && !is_urgent(loc->desktop))
424                 return false;
425
426         if (sel.local && ref->monitor != loc->monitor)
427                 return false;
428
429         return true;
430 }