]> git.lizzy.rs Git - bspwm.git/blob - query.c
New setting: merge_overlapping_monitors
[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                 if (dst->node == NULL && num_monitors > 1) {
182                         monitor_t *m = nearest_monitor(ref->monitor, dir, (desktop_select_t) {DESKTOP_STATUS_ALL, false, false});
183                         if (m != NULL) {
184                                 dst->monitor = m;
185                                 dst->desktop = m->desk;
186                                 dst->node = m->desk->focus;
187                         }
188                 }
189         } else if (parse_cycle_direction(desc, &cyc)) {
190                 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
191         } else if (parse_history_direction(desc, &hdi)) {
192                 history_find_node(hdi, ref, dst, sel);
193         } else if (streq("last", desc)) {
194                 history_find_node(HISTORY_OLDER, ref, dst, sel);
195         } else if (streq("biggest", desc)) {
196                 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
197         } else if (streq("focused", desc)) {
198                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
199                 if (node_matches(&loc, ref, sel)) {
200                         dst->monitor = mon;
201                         dst->desktop = mon->desk;
202                         dst->node = mon->desk->focus;
203                 }
204         } else {
205                 long int wid;
206                 if (parse_window_id(desc, &wid))
207                         locate_window(wid, dst);
208         }
209
210         return (dst->node != NULL);
211 }
212
213 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
214 {
215         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
216         char *tok;
217         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
218                 tok[0] = '\0';
219                 tok++;
220                 if (streq("free", tok)) {
221                         sel.status = DESKTOP_STATUS_FREE;
222                 } else if (streq("occupied", tok)) {
223                         sel.status = DESKTOP_STATUS_OCCUPIED;
224                 } else if (streq("urgent", tok)) {
225                         sel.urgent = true;
226                 } else if (streq("local", tok)) {
227                         sel.local = true;
228                 }
229         }
230
231         dst->desktop = NULL;
232
233         cycle_dir_t cyc;
234         history_dir_t hdi;
235         char *colon;
236         int idx;
237         if (parse_cycle_direction(desc, &cyc)) {
238                 dst->monitor = ref->monitor;
239                 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
240         } else if (parse_history_direction(desc, &hdi)) {
241                 history_find_desktop(hdi, ref, dst, sel);
242         } else if (streq("last", desc)) {
243                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
244         } else if (streq("focused", desc)) {
245                 coordinates_t loc = {mon, mon->desk, NULL};
246                 if (desktop_matches(&loc, ref, sel)) {
247                         dst->monitor = mon;
248                         dst->desktop = mon->desk;
249                 }
250         } else if ((colon = strchr(desc, ':')) != NULL) {
251                 *colon = '\0';
252                 if (monitor_from_desc(desc, ref, dst)) {
253                         if (streq("focused", colon + 1)) {
254                                 dst->desktop = dst->monitor->desk;
255                         } else if (parse_index(colon + 1, &idx)) {
256                                 desktop_from_index(idx, dst, dst->monitor);
257                         }
258                 }
259         } else if (parse_index(desc, &idx)) {
260                 desktop_from_index(idx, dst, NULL);
261         } else {
262                 locate_desktop(desc, dst);
263         }
264
265         return (dst->desktop != NULL);
266 }
267
268 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
269 {
270         desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
271         char *tok;
272         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
273                 tok[0] = '\0';
274                 tok++;
275                 if (streq("free", tok)) {
276                         sel.status = DESKTOP_STATUS_FREE;
277                 } else if (streq("occupied", tok)) {
278                         sel.status = DESKTOP_STATUS_OCCUPIED;
279                 }
280         }
281
282         dst->monitor = NULL;
283
284         direction_t dir;
285         cycle_dir_t cyc;
286         history_dir_t hdi;
287         int idx;
288         if (parse_direction(desc, &dir)) {
289                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
290         } else if (parse_cycle_direction(desc, &cyc)) {
291                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
292         } else if (parse_history_direction(desc, &hdi)) {
293                 history_find_monitor(hdi, ref, dst, sel);
294         } else if (streq("last", desc)) {
295                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
296         } else if (streq("primary", desc)) {
297                 if (pri_mon != NULL) {
298                         coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
299                         if (desktop_matches(&loc, ref, sel))
300                                 dst->monitor = pri_mon;
301                 }
302         } else if (streq("focused", desc)) {
303                 coordinates_t loc = {mon, mon->desk, NULL};
304                 if (desktop_matches(&loc, ref, sel))
305                         dst->monitor = mon;
306         } else if (parse_index(desc, &idx)) {
307                 monitor_from_index(idx, dst);
308         } else {
309                 locate_monitor(desc, dst);
310         }
311
312         return (dst->monitor != NULL);
313 }
314
315 bool locate_window(xcb_window_t win, coordinates_t *loc)
316 {
317         for (monitor_t *m = mon_head; m != NULL; m = m->next)
318                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
319                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
320                                 if (n->client->window == win) {
321                                         loc->monitor = m;
322                                         loc->desktop = d;
323                                         loc->node = n;
324                                         return true;
325                                 }
326         return false;
327 }
328
329 bool locate_desktop(char *name, coordinates_t *loc)
330 {
331         for (monitor_t *m = mon_head; m != NULL; m = m->next)
332                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
333                         if (streq(d->name, name)) {
334                                 loc->monitor = m;
335                                 loc->desktop = d;
336                                 return true;
337                         }
338         return false;
339 }
340
341 bool locate_monitor(char *name, coordinates_t *loc)
342 {
343         for (monitor_t *m = mon_head; m != NULL; m = m->next)
344                 if (streq(m->name, name)) {
345                         loc->monitor = m;
346                         return true;
347                 }
348         return false;
349 }
350
351 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
352 {
353         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
354                 if (mm != NULL && m != mm)
355                         continue;
356                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
357                         if (i == 1) {
358                                 loc->monitor = m;
359                                 loc->desktop = d;
360                                 loc->node = NULL;
361                                 return true;
362                         }
363         }
364         return false;
365 }
366
367 bool monitor_from_index(int i, coordinates_t *loc)
368 {
369         for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
370                 if (i == 1) {
371                         loc->monitor = m;
372                         loc->desktop = NULL;
373                         loc->node = NULL;
374                         return true;
375                 }
376         return false;
377 }
378
379 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
380 {
381         if (ref->node == NULL || loc->node == NULL)
382                 return false;
383
384         if (sel.type != CLIENT_TYPE_ALL &&
385             is_tiled(loc->node->client)
386             ? sel.type == CLIENT_TYPE_FLOATING
387             : sel.type == CLIENT_TYPE_TILED)
388                 return false;
389
390         if (sel.class != CLIENT_CLASS_ALL &&
391             streq(loc->node->client->class_name, ref->node->client->class_name)
392             ? sel.class == CLIENT_CLASS_DIFFER
393             : sel.class == CLIENT_CLASS_EQUAL)
394                 return false;
395
396         if (sel.mode != CLIENT_MODE_ALL &&
397             loc->node->split_mode == MODE_MANUAL
398             ? sel.mode == CLIENT_MODE_AUTOMATIC
399             : sel.mode == CLIENT_MODE_MANUAL)
400                 return false;
401
402         if (sel.local && loc->desktop != ref->desktop)
403                 return false;
404
405         if (sel.urgent && !loc->node->client->urgent)
406                 return false;
407
408         return true;
409 }
410
411 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
412 {
413         if (sel.status != DESKTOP_STATUS_ALL &&
414             loc->desktop->root == NULL
415             ? sel.status == DESKTOP_STATUS_OCCUPIED
416             : sel.status == DESKTOP_STATUS_FREE)
417                 return false;
418
419         if (sel.urgent && !is_urgent(loc->desktop))
420                 return false;
421
422         if (sel.local && ref->monitor != loc->monitor)
423                 return false;
424
425         return true;
426 }