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 } else if (parse_cycle_direction(desc, &cyc)) {
182 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
183 } else if (parse_history_direction(desc, &hdi)) {
184 history_find_node(hdi, ref, dst, sel);
185 } else if (streq("last", desc)) {
186 history_find_node(HISTORY_OLDER, ref, dst, sel);
187 } else if (streq("biggest", desc)) {
188 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
189 } else if (streq("focused", desc)) {
190 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
191 if (node_matches(&loc, ref, sel)) {
193 dst->desktop = mon->desk;
194 dst->node = mon->desk->focus;
198 if (parse_window_id(desc, &wid))
199 locate_window(wid, dst);
202 return (dst->node != NULL);
205 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
207 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
209 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
212 if (streq("free", tok)) {
213 sel.status = DESKTOP_STATUS_FREE;
214 } else if (streq("occupied", tok)) {
215 sel.status = DESKTOP_STATUS_OCCUPIED;
216 } else if (streq("urgent", tok)) {
218 } else if (streq("local", tok)) {
229 if (parse_cycle_direction(desc, &cyc)) {
230 dst->monitor = ref->monitor;
231 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
232 } else if (parse_history_direction(desc, &hdi)) {
233 history_find_desktop(hdi, ref, dst, sel);
234 } else if (streq("last", desc)) {
235 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
236 } else if (streq("focused", desc)) {
237 coordinates_t loc = {mon, mon->desk, NULL};
238 if (desktop_matches(&loc, ref, sel)) {
240 dst->desktop = mon->desk;
242 } else if ((colon = index(desc, ':')) != NULL) {
244 if (monitor_from_desc(desc, ref, dst)) {
245 if (streq("focused", colon + 1)) {
246 dst->desktop = dst->monitor->desk;
247 } else if (parse_index(colon + 1, &idx)) {
248 desktop_from_index(idx, dst, dst->monitor);
251 } else if (parse_index(desc, &idx)) {
252 desktop_from_index(idx, dst, NULL);
254 locate_desktop(desc, dst);
257 return (dst->desktop != NULL);
260 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
262 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
264 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
267 if (streq("free", tok)) {
268 sel.status = DESKTOP_STATUS_FREE;
269 } else if (streq("occupied", tok)) {
270 sel.status = DESKTOP_STATUS_OCCUPIED;
280 if (parse_direction(desc, &dir)) {
281 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
282 } else if (parse_cycle_direction(desc, &cyc)) {
283 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
284 } else if (parse_history_direction(desc, &hdi)) {
285 history_find_monitor(hdi, ref, dst, sel);
286 } else if (streq("last", desc)) {
287 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
288 } else if (streq("primary", desc)) {
289 if (pri_mon != NULL) {
290 coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
291 if (desktop_matches(&loc, ref, sel))
292 dst->monitor = pri_mon;
294 } else if (streq("focused", desc)) {
295 coordinates_t loc = {mon, mon->desk, NULL};
296 if (desktop_matches(&loc, ref, sel))
298 } else if (parse_index(desc, &idx)) {
299 monitor_from_index(idx, dst);
301 locate_monitor(desc, dst);
304 return (dst->monitor != NULL);
307 bool locate_window(xcb_window_t win, coordinates_t *loc)
309 for (monitor_t *m = mon_head; m != NULL; m = m->next)
310 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
311 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
312 if (n->client->window == win) {
321 bool locate_desktop(char *name, coordinates_t *loc)
323 for (monitor_t *m = mon_head; m != NULL; m = m->next)
324 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
325 if (streq(d->name, name)) {
333 bool locate_monitor(char *name, coordinates_t *loc)
335 for (monitor_t *m = mon_head; m != NULL; m = m->next)
336 if (streq(m->name, name)) {
343 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
345 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
346 if (mm != NULL && m != mm)
348 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
359 bool monitor_from_index(int i, coordinates_t *loc)
361 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
371 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
373 if (ref->node == NULL || loc->node == NULL)
376 if (sel.type != CLIENT_TYPE_ALL &&
377 is_tiled(loc->node->client)
378 ? sel.type == CLIENT_TYPE_FLOATING
379 : sel.type == CLIENT_TYPE_TILED)
382 if (sel.class != CLIENT_CLASS_ALL &&
383 streq(loc->node->client->class_name, ref->node->client->class_name)
384 ? sel.class == CLIENT_CLASS_DIFFER
385 : sel.class == CLIENT_CLASS_EQUAL)
388 if (sel.mode != CLIENT_MODE_ALL &&
389 loc->node->split_mode == MODE_MANUAL
390 ? sel.mode == CLIENT_MODE_AUTOMATIC
391 : sel.mode == CLIENT_MODE_MANUAL)
394 if (sel.local && loc->desktop != ref->desktop)
397 if (sel.urgent && !loc->node->client->urgent)
403 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
405 if (sel.status != DESKTOP_STATUS_ALL &&
406 loc->desktop->root == NULL
407 ? sel.status == DESKTOP_STATUS_OCCUPIED
408 : sel.status == DESKTOP_STATUS_FREE)
411 if (sel.urgent && !is_urgent(loc->desktop))
414 if (sel.local && ref->monitor != loc->monitor)