1 /* Copyright (c) 2012, 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.
37 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
39 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
40 if (loc.monitor != NULL && m != loc.monitor)
42 if (dom != DOMAIN_DESKTOP) {
43 if (dom == DOMAIN_MONITOR) {
44 fprintf(rsp, "%s\n", m->name);
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 ? " *" : ""));
53 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
57 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
59 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
60 if (loc.desktop != NULL && d != loc.desktop)
62 for (unsigned int i = 0; i < depth; i++)
64 if (dom == DOMAIN_DESKTOP) {
65 fprintf(rsp, "%s\n", d->name);
68 fprintf(rsp, "%s %u %i %i,%i,%i,%i %c%s\n", d->name, d->border_width,
70 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
71 (d->layout == LAYOUT_TILED ? 'T' : 'M'),
72 (d == m->desk ? " *" : ""));
74 query_tree(d, d->root, rsp, depth + 1);
78 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
83 for (unsigned int i = 0; i < depth; i++)
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 ? " *" : ""));
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);
104 query_tree(d, n->first_child, rsp, depth + 1);
105 query_tree(d, n->second_child, rsp, depth + 1);
108 void query_history(coordinates_t loc, FILE *rsp)
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))
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);
121 void query_stack(FILE *rsp)
123 for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
124 fprintf(rsp, "0x%X\n", s->node->client->window);
127 void query_windows(coordinates_t loc, FILE *rsp)
129 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
130 if (loc.monitor != NULL && m != loc.monitor)
132 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
133 if (loc.desktop != NULL && d != loc.desktop)
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)
138 fprintf(rsp, "0x%X\n", n->client->window);
144 client_select_t make_client_select(void)
146 client_select_t sel = {
164 desktop_select_t make_desktop_select(void)
166 desktop_select_t sel = {
174 void cleanup_client_select(client_select_t *sel)
179 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
181 client_select_t sel = make_client_select();
183 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
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;
246 dst->monitor = ref->monitor;
247 dst->desktop = ref->desktop;
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());
258 coordinates_t loc = {m, m->desk, m->desk->focus};
259 if (node_matches(&loc, ref, sel)) {
261 dst->desktop = m->desk;
262 dst->node = m->desk->focus;
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)) {
278 dst->desktop = mon->desk;
279 dst->node = mon->desk->focus;
283 if (parse_window_id(desc, &wid))
284 locate_window(wid, dst);
287 cleanup_client_select(&sel);
289 return (dst->node != NULL);
292 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
294 desktop_select_t sel = make_desktop_select();
296 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
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;
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)) {
331 dst->desktop = mon->desk;
333 } else if ((colon = strchr(desc, ':')) != NULL) {
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);
342 } else if (parse_index(desc, &idx)) {
343 desktop_from_index(idx, dst, NULL);
345 locate_desktop(desc, dst);
348 return (dst->desktop != NULL);
351 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
353 desktop_select_t sel = make_desktop_select();
355 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
358 if (streq("occupied", tok)) {
359 sel.occupied = OPTION_FALSE;
360 } else if (streq("!occupied", tok)) {
361 sel.occupied = OPTION_TRUE;
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;
385 } else if (streq("focused", desc)) {
386 coordinates_t loc = {mon, mon->desk, NULL};
387 if (desktop_matches(&loc, ref, sel))
389 } else if (parse_index(desc, &idx)) {
390 monitor_from_index(idx, dst);
392 locate_monitor(desc, dst);
395 return (dst->monitor != NULL);
398 bool locate_window(xcb_window_t win, coordinates_t *loc)
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) {
412 bool locate_desktop(char *name, coordinates_t *loc)
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)) {
424 bool locate_monitor(char *name, coordinates_t *loc)
426 for (monitor_t *m = mon_head; m != NULL; m = m->next)
427 if (streq(m->name, name)) {
434 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
436 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
437 if (mm != NULL && m != mm)
439 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
450 bool monitor_from_index(int i, coordinates_t *loc)
452 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
462 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
464 if (loc->node == NULL)
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) { \
480 if (sel.tiled != OPTION_NONE &&
481 loc->node->client->state != STATE_TILED
482 ? sel.tiled == OPTION_TRUE
483 : sel.tiled == OPTION_FALSE) {
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) {
494 if (sel.floating != OPTION_NONE &&
495 loc->node->client->state != STATE_FLOATING
496 ? sel.floating == OPTION_TRUE
497 : sel.floating == OPTION_FALSE) {
501 if (sel.fullscreen != OPTION_NONE &&
502 loc->node->client->state != STATE_FULLSCREEN
503 ? sel.fullscreen == OPTION_TRUE
504 : sel.fullscreen == OPTION_FALSE) {
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) {
515 if (sel.automatic != OPTION_NONE &&
516 loc->node->split_mode == MODE_MANUAL
517 ? sel.automatic == OPTION_TRUE
518 : sel.automatic == OPTION_FALSE) {
522 if (sel.local != OPTION_NONE &&
523 loc->desktop != ref->desktop
524 ? sel.local == OPTION_TRUE
525 : sel.local == OPTION_FALSE) {
529 if (sel.layer != NULL && loc->node->client->layer != *(sel.layer)) {
533 if (sel.focused != OPTION_NONE &&
534 loc->node != loc->desktop->focus
535 ? sel.focused == OPTION_TRUE
536 : sel.focused == OPTION_FALSE) {
543 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
545 if (sel.occupied != OPTION_NONE &&
546 loc->desktop->root == NULL
547 ? sel.occupied == OPTION_TRUE
548 : sel.occupied == OPTION_FALSE) {
552 if (sel.urgent != OPTION_NONE &&
553 !is_urgent(loc->desktop)
554 ? sel.urgent == OPTION_TRUE
555 : sel.urgent == OPTION_FALSE) {
559 if (sel.local != OPTION_NONE &&
560 ref->monitor != loc->monitor
561 ? sel.local == OPTION_TRUE
562 : sel.local == OPTION_FALSE) {