1 /* Copyright (c) 2012-2014, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
40 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
42 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
43 if (loc.monitor != NULL && m != loc.monitor)
45 if (dom != DOMAIN_DESKTOP) {
46 if (dom == DOMAIN_MONITOR) {
47 fprintf(rsp, "%s\n", m->name);
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 ? " *" : ""));
56 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
60 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
62 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
63 if (loc.desktop != NULL && d != loc.desktop)
65 for (unsigned int i = 0; i < depth; i++)
67 if (dom == DOMAIN_DESKTOP) {
68 fprintf(rsp, "%s\n", d->name);
71 fprintf(rsp, "%s %u %i %i,%i,%i,%i %c %c%s\n", d->name, d->border_width,
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 ? " *" : ""));
77 query_tree(d, d->root, rsp, depth + 1);
81 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
86 for (unsigned int i = 0; i < depth; i++)
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 ? " *" : ""));
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);
106 query_tree(d, n->first_child, rsp, depth + 1);
107 query_tree(d, n->second_child, rsp, depth + 1);
110 void query_history(coordinates_t loc, FILE *rsp)
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))
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);
123 void query_stack(FILE *rsp)
125 for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
126 fprintf(rsp, "0x%X\n", s->node->client->window);
129 void query_windows(coordinates_t loc, FILE *rsp)
131 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
132 if (loc.monitor != NULL && m != loc.monitor)
134 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
135 if (loc.desktop != NULL && d != loc.desktop)
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)
140 fprintf(rsp, "0x%X\n", n->client->window);
146 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
148 client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, CLIENT_MODE_ALL, false, false};
150 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
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)) {
167 } else if (streq("local", tok)) {
172 dst->monitor = ref->monitor;
173 dst->desktop = ref->desktop;
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});
185 dst->desktop = m->desk;
186 dst->node = m->desk->focus;
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)) {
201 dst->desktop = mon->desk;
202 dst->node = mon->desk->focus;
206 if (parse_window_id(desc, &wid))
207 locate_window(wid, dst);
210 return (dst->node != NULL || dst->desktop != ref->desktop);
213 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
215 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
217 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
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)) {
226 } else if (streq("local", tok)) {
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)) {
248 dst->desktop = mon->desk;
250 } else if ((colon = index(desc, ':')) != NULL) {
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);
259 } else if (parse_index(desc, &idx)) {
260 desktop_from_index(idx, dst, NULL);
262 locate_desktop(desc, dst);
265 return (dst->desktop != NULL);
268 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
270 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
272 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
275 if (streq("free", tok)) {
276 sel.status = DESKTOP_STATUS_FREE;
277 } else if (streq("occupied", tok)) {
278 sel.status = DESKTOP_STATUS_OCCUPIED;
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;
302 } else if (streq("focused", desc)) {
303 coordinates_t loc = {mon, mon->desk, NULL};
304 if (desktop_matches(&loc, ref, sel))
306 } else if (parse_index(desc, &idx)) {
307 monitor_from_index(idx, dst);
309 locate_monitor(desc, dst);
312 return (dst->monitor != NULL);
315 bool locate_window(xcb_window_t win, coordinates_t *loc)
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) {
329 bool locate_desktop(char *name, coordinates_t *loc)
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)) {
341 bool locate_monitor(char *name, coordinates_t *loc)
343 for (monitor_t *m = mon_head; m != NULL; m = m->next)
344 if (streq(m->name, name)) {
351 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
353 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
354 if (mm != NULL && m != mm)
356 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
367 bool monitor_from_index(int i, coordinates_t *loc)
369 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
379 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
381 if (ref->node == NULL || loc->node == NULL)
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)
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)
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)
402 if (sel.local && loc->desktop != ref->desktop)
405 if (sel.urgent && !loc->node->client->urgent)
411 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
413 if (sel.status != DESKTOP_STATUS_ALL &&
414 loc->desktop->root == NULL
415 ? sel.status == DESKTOP_STATUS_OCCUPIED
416 : sel.status == DESKTOP_STATUS_FREE)
419 if (sel.urgent && !is_urgent(loc->desktop))
422 if (sel.local && ref->monitor != loc->monitor)