]> git.lizzy.rs Git - bspwm.git/blob - query.c
Rewrite message handling
[bspwm.git] / query.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "bspwm.h"
5 #include "tree.h"
6 #include "settings.h"
7 #include "messages.h"
8 #include "query.h"
9
10 void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
11 {
12     char line[MAXLEN];
13     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
14         if (loc.monitor != NULL && m != loc.monitor)
15             continue;
16         if (dom != DOMAIN_DESKTOP) {
17             if (dom == DOMAIN_MONITOR) {
18                 snprintf(line, sizeof(line), "%s\n", m->name);
19                 strncat(rsp, line, REMLEN(rsp));
20                 continue;
21             } else {
22                 snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
23                 strncat(rsp, line, REMLEN(rsp));
24                 if (m == mon)
25                     strncat(rsp, " #", REMLEN(rsp));
26                 else if (m == last_mon)
27                     strncat(rsp, " ~", REMLEN(rsp));
28                 strncat(rsp, "\n", REMLEN(rsp));
29             }
30         }
31         query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
32     }
33 }
34
35 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, char *rsp)
36 {
37     char line[MAXLEN];
38     for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
39         if (loc.desktop != NULL && d != loc.desktop)
40             continue;
41         for (unsigned int i = 0; i < depth; i++)
42             strncat(rsp, "  ", REMLEN(rsp));
43         if (dom == DOMAIN_DESKTOP) {
44             snprintf(line, sizeof(line), "%s\n", d->name);
45             strncat(rsp, line, REMLEN(rsp));
46             continue;
47         } else {
48             snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
49             strncat(rsp, line, REMLEN(rsp));
50             if (d == m->desk)
51                 strncat(rsp, " @", REMLEN(rsp));
52             else if (d == m->last_desk)
53                 strncat(rsp, " ~", REMLEN(rsp));
54             strncat(rsp, "\n", REMLEN(rsp));
55         }
56         query_tree(d, d->root, rsp, depth + 1);
57     }
58 }
59
60 void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
61 {
62     if (n == NULL)
63         return;
64
65     char line[MAXLEN];
66
67     for (unsigned int i = 0; i < depth; i++)
68         strncat(rsp, "  ", REMLEN(rsp));
69
70     if (is_leaf(n)) {
71         client_t *c = n->client;
72         snprintf(line, sizeof(line), "%c %s %X %u %ux%u%+i%+i %c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'));
73     } else {
74         snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
75     }
76
77     strncat(rsp, line, REMLEN(rsp));
78
79     if (n == d->focus)
80         strncat(rsp, " *", REMLEN(rsp));
81     strncat(rsp, "\n", REMLEN(rsp));
82
83     query_tree(d, n->first_child, rsp, depth + 1);
84     query_tree(d, n->second_child, rsp, depth + 1);
85 }
86
87 void query_history(coordinates_t loc, char *rsp)
88 {
89     char line[MAXLEN];
90     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
91         if (loc.monitor != NULL && m != loc.monitor)
92             continue;
93         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
94             if (loc.desktop != NULL && d != loc.desktop)
95                 continue;
96             snprintf(line, sizeof(line), "%s\n", d->name);
97             strncat(rsp, line, REMLEN(rsp));
98             for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
99                 snprintf(line, sizeof(line), "  %X\n", a->node->client->window);
100                 strncat(rsp, line, REMLEN(rsp));
101             }
102         }
103     }
104 }
105
106 void query_windows(coordinates_t loc, char *rsp)
107 {
108     char line[MAXLEN];
109
110     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
111         if (loc.monitor != NULL && m != loc.monitor)
112             continue;
113         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
114             if (loc.desktop != NULL && d != loc.desktop)
115                 continue;
116             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
117                 if (loc.node != NULL && n != loc.node)
118                     continue;
119                 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
120                 strncat(rsp, line, REMLEN(rsp));
121             }
122         }
123     }
124 }
125
126 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
127 {
128     client_select_t sel;
129     sel.type = CLIENT_TYPE_ALL;
130     sel.class = CLIENT_CLASS_ALL;
131     char *tok;
132     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
133         tok[0] = '\0';
134         tok++;
135         if (streq("tiled", tok)) {
136             sel.type = CLIENT_TYPE_TILED;
137         } else if (streq("floating", tok)) {
138             sel.type = CLIENT_TYPE_FLOATING;
139         } else if (streq("like", tok)) {
140             sel.class = CLIENT_CLASS_EQUAL;
141         } else if (streq("unlike", tok)) {
142             sel.class = CLIENT_CLASS_DIFFER;
143         }
144     }
145
146     dst->monitor = ref->monitor;
147     dst->desktop = ref->desktop;
148     dst->node = NULL;
149
150     direction_t dir;
151     cycle_dir_t cyc;
152     if (parse_direction(desc, &dir)) {
153         dst->node = nearest_neighbor(dst->desktop, ref->node, dir);
154         if (dst->node == NULL && monitor_focus_fallback) {
155             if (monitor_from_desc(desc, ref, dst)) {
156                 dst->desktop = dst->monitor->desk;
157                 dst->node = dst->monitor->desk->focus;
158                 return true;
159             }
160         }
161     } else if (parse_cycle_direction(desc, &cyc)) {
162         dst->node = closest_node(ref->desktop, ref->node, cyc, sel);
163     } else if (streq("last", desc)) {
164         dst->node = history_get(ref->desktop->history, 1);
165     } else if (streq("biggest", desc)) {
166         dst->node = find_biggest(ref->desktop);
167     } else if (streq("focused", desc)) {
168         dst->monitor = mon;
169         dst->desktop = mon->desk;
170         dst->node = mon->desk->focus;
171     } else {
172         long int wid;
173         if (parse_window_id(desc, &wid))
174             locate_window(wid, dst);
175     }
176
177     return (dst->node != NULL);
178 }
179
180 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
181 {
182     desktop_select_t sel;
183     sel = DESKTOP_ALL;
184     char *tok;
185     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
186         tok[0] = '\0';
187         tok++;
188         if (streq("free", tok)) {
189             sel = DESKTOP_FREE;
190         } else if (streq("occupied", tok)) {
191             sel = DESKTOP_OCCUPIED;
192         }
193     }
194
195     dst->desktop = NULL;
196
197     cycle_dir_t cyc;
198     if (parse_cycle_direction(desc, &cyc)) {
199         dst->monitor = ref->monitor;
200         dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
201     } else if (streq("last", desc)) {
202         dst->monitor = mon;
203         dst->desktop = mon->last_desk;
204     } else if (streq("focused", desc)) {
205         dst->monitor = mon;
206         dst->desktop = mon->desk;
207     } else {
208         locate_desktop(desc, dst);
209     }
210
211     return (dst->desktop != NULL);
212 }
213
214 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
215 {
216     desktop_select_t sel;
217     sel = DESKTOP_ALL;
218     char *tok;
219     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
220         tok[0] = '\0';
221         tok++;
222         if (streq("free", tok)) {
223             sel = DESKTOP_FREE;
224         } else if (streq("occupied", tok)) {
225             sel = DESKTOP_OCCUPIED;
226         }
227     }
228
229     dst->monitor = NULL;
230
231     direction_t dir;
232     cycle_dir_t cyc;
233     if (parse_direction(desc, &dir)) {
234         dst->monitor = nearest_monitor(ref->monitor, dir);
235     } else if (parse_cycle_direction(desc, &cyc)) {
236         dst->monitor = closest_monitor(ref->monitor, cyc, sel);
237     } else if (streq("last", desc)) {
238         dst->monitor = last_mon;
239     } else if (streq("focused", desc)) {
240         dst->monitor = mon;
241     } else {
242         locate_monitor(desc, dst);
243     }
244
245     return (dst->monitor != NULL);
246 }
247
248 bool locate_window(xcb_window_t win, coordinates_t *loc)
249 {
250     for (monitor_t *m = mon_head; m != NULL; m = m->next)
251         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
252             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
253                 if (n->client->window == win) {
254                     loc->monitor = m;
255                     loc->desktop = d;
256                     loc->node = n;
257                     return true;
258                 }
259     return false;
260 }
261
262 bool locate_desktop(char *name, coordinates_t *loc)
263 {
264     for (monitor_t *m = mon_head; m != NULL; m = m->next)
265         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
266             if (streq(d->name, name)) {
267                 loc->monitor = m;
268                 loc->desktop = d;
269                 return true;
270             }
271     return false;
272 }
273
274 bool locate_monitor(char *name, coordinates_t *loc)
275 {
276     for (monitor_t *m = mon_head; m != NULL; m = m->next)
277         if (streq(m->name, name)) {
278             loc->monitor = m;
279             return true;
280         }
281     return false;
282 }