]> git.lizzy.rs Git - bspwm.git/blob - messages.c
Add example bindings for history navigation
[bspwm.git] / messages.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "bspwm.h"
6 #include "desktop.h"
7 #include "ewmh.h"
8 #include "history.h"
9 #include "monitor.h"
10 #include "tag.h"
11 #include "pointer.h"
12 #include "query.h"
13 #include "restore.h"
14 #include "rule.h"
15 #include "settings.h"
16 #include "tree.h"
17 #include "window.h"
18 #include "messages.h"
19
20 bool handle_message(char *msg, int msg_len, char *rsp)
21 {
22     int cap = INIT_CAP;
23     int num = 0;
24     char **args = malloc(cap * sizeof(char *));
25     if (args == NULL)
26         return false;
27
28     for (int i = 0, j = 0; i < msg_len; i++) {
29         if (msg[i] == 0) {
30             args[num++] = msg + j;
31             j = i + 1;
32         }
33         if (num >= cap) {
34             cap *= 2;
35             char **new = realloc(args, cap * sizeof(char *));
36             if (new == NULL) {
37                 free(args);
38                 return false;
39             } else {
40                 args = new;
41             }
42         }
43     }
44
45     if (num < 1) {
46         free(args);
47         return false;
48     }
49
50     char **args_orig = args;
51     bool ret = process_message(args, num, rsp);
52     free(args_orig);
53     return ret;
54 }
55
56 bool process_message(char **args, int num, char *rsp)
57 {
58     if (streq("window", *args)) {
59         return cmd_window(++args, --num);
60     } else if (streq("desktop", *args)) {
61         return cmd_desktop(++args, --num);
62     } else if (streq("monitor", *args)) {
63         return cmd_monitor(++args, --num);
64     } else if (streq("query", *args)) {
65         return cmd_query(++args, --num, rsp);
66     } else if (streq("tag", *args)) {
67         return cmd_tag(++args, --num, rsp);
68     } else if (streq("restore", *args)) {
69         return cmd_restore(++args, --num);
70     } else if (streq("control", *args)) {
71         return cmd_control(++args, --num);
72     } else if (streq("rule", *args)) {
73         return cmd_rule(++args, --num, rsp);
74     } else if (streq("pointer", *args)) {
75         return cmd_pointer(++args, --num);
76     } else if (streq("config", *args)) {
77         return cmd_config(++args, --num, rsp);
78     } else if (streq("quit", *args)) {
79         return cmd_quit(++args, --num);
80     }
81
82     return false;
83 }
84
85 bool cmd_window(char **args, int num)
86 {
87     if (num < 1)
88         return false;
89
90     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
91     coordinates_t trg = ref;
92
93     if (*args[0] != OPT_CHR) {
94         if (node_from_desc(*args, &ref, &trg))
95             num--, args++;
96         else
97             return false;
98     }
99
100     if (trg.node == NULL)
101         return false;
102
103     bool dirty = false;
104
105     while (num > 0) {
106         if (streq("-f", *args) || streq("--focus", *args)) {
107             coordinates_t dst = trg;
108             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
109                 num--, args++;
110                 if (!node_from_desc(*args, &trg, &dst))
111                     return false;
112             }
113             focus_node(dst.monitor, dst.desktop, dst.node);
114         } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
115             num--, args++;
116             coordinates_t dst;
117             if (desktop_from_desc(*args, &trg, &dst)) {
118                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus)) {
119                     trg.monitor = dst.monitor;
120                     trg.desktop = dst.desktop;
121                 }
122             } else {
123                 return false;
124             }
125         } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
126             num--, args++;
127             if (num < 1)
128                 return false;
129             coordinates_t dst;
130             if (monitor_from_desc(*args, &trg, &dst)) {
131                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus)) {
132                     trg.monitor = dst.monitor;
133                     trg.desktop = dst.monitor->desk;
134                 }
135             } else {
136                 return false;
137             }
138         } else if (streq("-w", *args) || streq("--to-window", *args)) {
139             num--, args++;
140             if (num < 1)
141                 return false;
142             coordinates_t dst;
143             if (node_from_desc(*args, &trg, &dst)) {
144                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
145                     trg.monitor = dst.monitor;
146                     trg.desktop = dst.desktop;
147                 }
148             } else {
149                 return false;
150             }
151         } else if (streq("-s", *args) || streq("--swap", *args)) {
152             num--, args++;
153             if (num < 1)
154                 return false;
155             coordinates_t dst;
156             if (node_from_desc(*args, &trg, &dst)) {
157                 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
158                     if (trg.desktop != dst.desktop)
159                         arrange(trg.monitor, trg.desktop);
160                     trg.monitor = dst.monitor;
161                     trg.desktop = dst.desktop;
162                     dirty = true;
163                 }
164             } else {
165                 return false;
166             }
167         } else if (streq("-t", *args) || streq("--toggle", *args)) {
168             num--, args++;
169             if (num < 1)
170                 return false;
171             char *key = strtok(*args, EQL_TOK);
172             char *val = strtok(NULL, EQL_TOK);
173             alter_state_t a;
174             bool b;
175             if (val == NULL) {
176                 a = ALTER_TOGGLE;
177             } else {
178                 if (parse_bool(val, &b))
179                     a = ALTER_SET;
180                 else
181                     return false;
182             }
183             if (streq("fullscreen", key)) {
184                 set_fullscreen(trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
185                 dirty = true;
186             } else if (streq("floating", key)) {
187                 set_floating(trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
188                 dirty = true;
189             } else if (streq("locked", key)) {
190                 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
191             } else if (streq("sticky", key)) {
192                 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->sticky));
193             } else if (streq("visible", key)) {
194                 set_presence(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !is_visible(trg.desktop, trg.node)));
195             }
196         } else if (streq("-p", *args) || streq("--presel", *args)) {
197             num--, args++;
198             if (num < 1 || !is_tiled(trg.node->client)
199                     || trg.desktop->layout != LAYOUT_TILED)
200                 return false;
201             if (streq("cancel", *args)) {
202                 reset_mode(&trg);
203             } else {
204                 direction_t dir;
205                 if (parse_direction(*args, &dir)) {
206                     double rat = trg.node->split_ratio;
207                     if (num > 1 && *(args + 1)[0] != OPT_CHR) {
208                         num--, args++;
209                         if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1)
210                             return false;
211                     }
212                     if (auto_cancel && trg.node->split_mode == MODE_MANUAL
213                             && dir == trg.node->split_dir
214                             && rat == trg.node->split_ratio) {
215                         reset_mode(&trg);
216                     } else {
217                         trg.node->split_mode = MODE_MANUAL;
218                         trg.node->split_dir = dir;
219                         trg.node->split_ratio = rat;
220                     }
221                     window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
222                 } else {
223                     return false;
224                 }
225             }
226         } else if (streq("-e", *args) || streq("--edge", *args)) {
227             num--, args++;
228             if (num < 2)
229                 return false;
230             direction_t dir;
231             if (!parse_direction(*args, &dir))
232                 return false;
233             node_t *n = find_fence(trg.node, dir);
234             if (n == NULL)
235                 return false;
236             num--, args++;
237             fence_move_t fmo;
238             if (parse_fence_move(*args, &fmo)) {
239                 move_fence(n, dir, fmo);
240             } else {
241                 double rat;
242                 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1)
243                     n->split_ratio = rat;
244                 else
245                     return false;
246             }
247             dirty = true;
248         } else if (streq("-r", *args) || streq("--ratio", *args)) {
249             num--, args++;
250             if (num < 1)
251                 return false;
252             double rat;
253             if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
254                 trg.node->split_ratio = rat;
255                 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
256             } else {
257                 return false;
258             }
259         } else if (streq("-R", *args) || streq("--rotate", *args)) {
260             num--, args++;
261             if (num < 2)
262                 return false;
263             direction_t dir;
264             if (!parse_direction(*args, &dir))
265                 return false;
266             node_t *n = find_fence(trg.node, dir);
267             if (n == NULL)
268                 return false;
269             num--, args++;
270             int deg;
271             if (parse_degree(*args, &deg)) {
272                 rotate_tree(n, deg);
273                 dirty = true;
274             } else {
275                 return false;
276             }
277         } else if (streq("-c", *args) || streq("--close", *args)) {
278             if (num > 1)
279                 return false;
280             window_close(trg.node);
281         } else if (streq("-k", *args) || streq("--kill", *args)) {
282             if (num > 1)
283                 return false;
284             window_kill(trg.monitor, trg.desktop, trg.node);
285             dirty = true;
286         } else {
287             return false;
288         }
289
290         num--, args++;
291     }
292
293     if (dirty)
294         arrange(trg.monitor, trg.desktop);
295
296     return true;
297 }
298
299 bool cmd_desktop(char **args, int num)
300 {
301     if (num < 1)
302         return false;
303
304     coordinates_t ref = {mon, mon->desk, NULL};
305     coordinates_t trg = ref;
306
307     if (*args[0] != OPT_CHR) {
308         if (desktop_from_desc(*args, &ref, &trg))
309             num--, args++;
310         else
311             return false;
312     }
313
314     bool dirty = false;
315
316     while (num > 0) {
317         if (streq("-f", *args) || streq("--focus", *args)) {
318             coordinates_t dst = trg;
319             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
320                 num--, args++;
321                 if (!desktop_from_desc(*args, &trg, &dst))
322                     return false;
323             }
324             if (auto_alternate && dst.desktop == mon->desk) {
325                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
326                 history_find_desktop(HISTORY_OLDER, &trg, &dst, sel);
327             }
328             focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
329         } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
330             num--, args++;
331             if (num < 1 || trg.monitor->desk_head == trg.monitor->desk_tail)
332                 return false;
333             coordinates_t dst;
334             if (monitor_from_desc(*args, &trg, &dst)) {
335                 transfer_desktop(trg.monitor, dst.monitor, trg.desktop);
336                 trg.monitor = dst.monitor;
337                 update_current();
338             } else {
339                 return false;
340             }
341         } else if (streq("-s", *args) || streq("--swap", *args)) {
342             num--, args++;
343             if (num < 1)
344                 return false;
345             coordinates_t dst;
346             if (desktop_from_desc(*args, &trg, &dst) && trg.monitor == dst.monitor)
347                 swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop);
348             else
349                 return false;
350         } else if (streq("-l", *args) || streq("--layout", *args)) {
351             num--, args++;
352             if (num < 1)
353                 return false;
354             layout_t lyt;
355             cycle_dir_t cyc;
356             if (parse_cycle_direction(*args, &cyc))
357                 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
358             else if (parse_layout(*args, &lyt))
359                 change_layout(trg.monitor, trg.desktop, lyt);
360             else
361                 return false;
362         } else if (streq("-n", *args) || streq("--rename", *args)) {
363             num--, args++;
364             if (num < 1)
365                 return false;
366             snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
367             ewmh_update_desktop_names();
368             put_status();
369         } else if (streq("-r", *args) || streq("--remove", *args)) {
370             if (trg.desktop->root == NULL
371                     && trg.monitor->desk_head != trg.monitor->desk_tail) {
372                 remove_desktop(trg.monitor, trg.desktop);
373                 show_desktop(trg.monitor->desk);
374                 update_current();
375                 return true;
376             } else {
377                 return false;
378             }
379         } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
380             reset_mode(&trg);
381         } else if (streq("-F", *args) || streq("--flip", *args)) {
382             num--, args++;
383             if (num < 1)
384                 return false;
385             flip_t flp;
386             if (parse_flip(*args, &flp)) {
387                 flip_tree(trg.desktop->root, flp);
388                 dirty = true;
389             } else {
390                 return false;
391             }
392         } else if (streq("-R", *args) || streq("--rotate", *args)) {
393             num--, args++;
394             if (num < 1)
395                 return false;
396             int deg;
397             if (parse_degree(*args, &deg)) {
398                 rotate_tree(trg.desktop->root, deg);
399                 dirty = true;
400             } else {
401                 return false;
402             }
403         } else if (streq("-B", *args) || streq("--balance", *args)) {
404             balance_tree(trg.desktop->root);
405             dirty = true;
406         } else if (streq("-C", *args) || streq("--circulate", *args)) {
407             num--, args++;
408             if (num < 1)
409                 return false;
410             circulate_dir_t cir;
411             if (parse_circulate_direction(*args, &cir)) {
412                 circulate_leaves(trg.monitor, trg.desktop, cir);
413                 dirty = true;
414             } else {
415                 return false;
416             }
417         } else {
418             return false;
419         }
420         num--, args++;
421     }
422
423     if (dirty)
424         arrange(trg.monitor, trg.desktop);
425
426     return true;
427 }
428
429 bool cmd_monitor(char **args, int num)
430 {
431     if (num < 1)
432         return false;
433
434     coordinates_t ref = {mon, NULL, NULL};
435     coordinates_t trg = ref;
436
437     if (*args[0] != OPT_CHR) {
438         if (monitor_from_desc(*args, &ref, &trg))
439             num--, args++;
440         else
441             return false;
442     }
443
444     while (num > 0) {
445         if (streq("-f", *args) || streq("--focus", *args)) {
446             coordinates_t dst = trg;
447             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
448                 num--, args++;
449                 if (!monitor_from_desc(*args, &trg, &dst))
450                     return false;
451             }
452             if (auto_alternate && dst.monitor == mon) {
453                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
454                 history_find_monitor(HISTORY_OLDER, &trg, &dst, sel);
455             }
456             focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
457         } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
458             num--, args++;
459             if (num < 1)
460                 return false;
461             desktop_t *d = trg.monitor->desk_head;
462             while (num > 0 && d != NULL) {
463                 snprintf(d->name, sizeof(d->name), "%s", *args);
464                 d = d->next;
465                 num--, args++;
466             }
467             put_status();
468             while (num > 0) {
469                 add_desktop(trg.monitor, make_desktop(*args));
470                 num--, args++;
471             }
472             while (d != NULL) {
473                 desktop_t *next = d->next;
474                 if (d == mon->desk)
475                     focus_node(trg.monitor, d->prev, d->prev->focus);
476                 merge_desktops(trg.monitor, d, mon, mon->desk);
477                 remove_desktop(trg.monitor, d);
478                 d = next;
479             }
480         } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
481             num--, args++;
482             if (num < 1)
483                 return false;
484             while (num > 0) {
485                 add_desktop(trg.monitor, make_desktop(*args));
486                 num--, args++;
487             }
488         } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
489             num--, args++;
490             if (num < 1)
491                 return false;
492             while (num > 0) {
493                 coordinates_t dst;
494                 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
495                     remove_desktop(dst.monitor, dst.desktop);
496                     show_desktop(dst.monitor->desk);
497                 }
498                 num--, args++;
499             }
500         } else if (streq("-n", *args) || streq("--rename", *args)) {
501             num--, args++;
502             if (num < 1)
503                 return false;
504             snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
505             put_status();
506         } else if (streq("-s", *args) || streq("--swap", *args)) {
507             num--, args++;
508             if (num < 1)
509                 return false;
510             coordinates_t dst;
511             if (monitor_from_desc(*args, &trg, &dst))
512                 swap_monitors(trg.monitor, dst.monitor);
513             else
514                 return false;
515         } else {
516             return false;
517         }
518         num--, args++;
519     }
520
521     return true;
522 }
523
524 bool cmd_tag(char **args, int num, char *rsp)
525 {
526     if (num < 1)
527         return false;
528
529     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
530     coordinates_t trg = {NULL, NULL, NULL};
531
532     while (num > 0) {
533         if (streq("-d", *args) || streq("--desktop", *args)) {
534             num--, args++;
535             if (num < 1)
536                 return false;
537             if (!desktop_from_desc(*args, &ref, &trg))
538                 return false;
539         } else if (streq("-w", *args) || streq("--window", *args)) {
540             num--, args++;
541             if (num < 1)
542                 return false;
543             if (!node_from_desc(*args, &ref, &trg))
544                 return false;
545         } else if (streq("-s", *args) || streq("--set-tags", *args)) {
546             num--, args++;
547             if (num < 1 || trg.desktop == NULL)
548                 return false;
549             unsigned int tf = 0;
550             if (streq("all", *args))
551                 tf = (1 << num_tags) - 1;
552             else
553                 while (num > 0) {
554                     tag_t *tag = NULL;
555                     int idx;
556                     if (parse_index(*args, &idx))
557                         tag = get_tag_by_index(idx - 1);
558                     else
559                         tag = get_tag(*args);
560                     if (tag != NULL)
561                         tf |= tag->mask;
562                     num--, args++;
563                 }
564             if (trg.node == NULL)
565                 tag_desktop(trg.monitor, trg.desktop, tf);
566             else
567                 tag_node(trg.monitor, trg.desktop, trg.node, trg.desktop, tf);
568             return true;
569         } else if (streq("-t", *args) || streq("--toggle-tags", *args)) {
570             num--, args++;
571             if (num < 1 || trg.desktop == NULL)
572                 return false;
573             unsigned int tf;
574             if (trg.node == NULL)
575                 tf = trg.desktop->tags_field;
576             else
577                 tf = trg.node->client->tags_field;
578             while (num > 0) {
579                 char *key;
580                 bool value;
581                 alter_state_t alt;
582                 if (parse_bool_declaration(*args, &key, &value, &alt)) {
583                     tag_t *tag = NULL;
584                     int idx;
585                     if (parse_index(key, &idx))
586                         tag = get_tag_by_index(idx - 1);
587                     else
588                         tag = get_tag(key);
589                     if (tag != NULL) {
590                         if (alt == ALTER_SET) {
591                             if (value)
592                                 tf |= tag->mask;
593                             else
594                                 tf &= ~tag->mask;
595                         } else {
596                             tf ^= tag->mask;
597                         }
598                     }
599                 }
600                 num--, args++;
601             }
602             if (trg.node == NULL)
603                 tag_desktop(trg.monitor, trg.desktop, tf);
604             else
605                 tag_node(trg.monitor, trg.desktop, trg.node, trg.desktop, tf);
606             return true;
607         } else if (streq("-e", *args) || streq("--enumerate-tags", *args)) {
608             num--, args++;
609             if (num < 1)
610                 return false;
611             int i = 0;
612             while (i < num_tags && num > 0) {
613                 snprintf(tags[i]->name, sizeof(tags[i]->name), "%s", *args);
614                 tags[i]->mask = 1 << i;
615                 i++, num--, args++;
616             }
617             while (i < num_tags) {
618                 remove_tag_by_index(i);
619             }
620             while (num > 0) {
621                 add_tag(*args);
622                 num--, args++;
623             }
624             put_status();
625         } else if (streq("-a", *args) || streq("--add", *args)) {
626             num--, args++;
627             if (num < 1)
628                 return false;
629             while (num > 0) {
630                 add_tag(*args);
631                 num--, args++;
632             }
633         } else if (streq("-r", *args) || streq("--remove", *args)) {
634             num--, args++;
635             if (num < 1)
636                 return false;
637             int idx;
638             while (num > 0) {
639                 if (parse_index(*args, &idx))
640                     remove_tag_by_index(idx - 1);
641                 else
642                     remove_tag(*args);
643                 num--, args++;
644             }
645         } else if (streq("-l", *args) || streq("--list", *args)) {
646             list_tags(rsp);
647         } else {
648             return false;
649         }
650         num--, args++;
651     }
652
653     return true;
654 }
655
656 bool cmd_query(char **args, int num, char *rsp)
657 {
658     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
659     coordinates_t trg = {NULL, NULL, NULL};
660     domain_t dom = DOMAIN_TREE;
661     int d = 0, t = 0;
662
663     while (num > 0) {
664         if (streq("-T", *args) || streq("--tree", *args)) {
665             dom = DOMAIN_TREE, d++;
666         } else if (streq("-M", *args) || streq("--monitors", *args)) {
667             dom = DOMAIN_MONITOR, d++;
668         } else if (streq("-D", *args) || streq("--desktops", *args)) {
669             dom = DOMAIN_DESKTOP, d++;
670         } else if (streq("-W", *args) || streq("--windows", *args)) {
671             dom = DOMAIN_WINDOW, d++;
672         } else if (streq("-H", *args) || streq("--history", *args)) {
673             dom = DOMAIN_HISTORY, d++;
674         } else if (streq("-S", *args) || streq("--stack", *args)) {
675             dom = DOMAIN_STACK, d++;
676         } else if (streq("-m", *args) || streq("--monitor", *args)) {
677             trg.monitor = ref.monitor;
678             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
679                 num--, args++;
680                 if (!monitor_from_desc(*args, &ref, &trg))
681                     return false;
682             }
683             t++;
684         } else if (streq("-d", *args) || streq("--desktop", *args)) {
685             trg.monitor = ref.monitor;
686             trg.desktop = ref.desktop;
687             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
688                 num--, args++;
689                 if (!desktop_from_desc(*args, &ref, &trg))
690                     return false;
691             }
692             t++;
693         } else if (streq("-w", *args) || streq("--window", *args)) {
694             trg = ref;
695             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
696                 num--, args++;
697                 if (!node_from_desc(*args, &ref, &trg))
698                     return false;
699             }
700             t++;
701         } else {
702             return false;
703         }
704         num--, args++;
705     }
706
707     if (d != 1 || t > 1)
708         return false;
709
710     if (dom == DOMAIN_HISTORY)
711         query_history(trg, rsp);
712     else if (dom == DOMAIN_STACK)
713         query_stack(rsp);
714     else if (dom == DOMAIN_WINDOW)
715         query_windows(trg, rsp);
716     else
717         query_monitors(trg, dom, rsp);
718
719     return true;
720 }
721
722 bool cmd_rule(char **args, int num, char *rsp)
723 {
724     if (num < 1)
725         return false;
726     while (num > 0) {
727         if (streq("-a", *args) || streq("--add", *args)) {
728             num--, args++;
729             if (num < 2)
730                 return false;
731             rule_t *rule = make_rule();
732             snprintf(rule->cause.name, sizeof(rule->cause.name), "%s", *args);
733             num--, args++;
734             while (num > 0) {
735                 if (streq("--floating", *args)) {
736                     rule->effect.floating = true;
737                 } else if (streq("--fullscreen", *args)) {
738                     rule->effect.fullscreen = true;
739                 } else if (streq("--locked", *args)) {
740                     rule->effect.locked = true;
741                 } else if (streq("--sticky", *args)) {
742                     rule->effect.sticky = true;
743                 } else if (streq("--follow", *args)) {
744                     rule->effect.follow = true;
745                 } else if (streq("--focus", *args)) {
746                     rule->effect.focus = true;
747                 } else if (streq("--unmanage", *args)) {
748                     rule->effect.unmanage = true;
749                 } else if (streq("--one-shot", *args)) {
750                     rule->one_shot = true;
751                 } else if (streq("--tags", *args)) {
752                     num--, args++;
753                     if (num < 1) {
754                         free(rule);
755                         return false;
756                     }
757                     snprintf(rule->effect.tags, sizeof(rule->effect.tags), "%s", *args);
758                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
759                     num--, args++;
760                     if (num < 1) {
761                         free(rule);
762                         return false;
763                     }
764                     snprintf(rule->effect.desc, sizeof(rule->effect.desc), "%s", *args);
765                 } else {
766                     free(rule);
767                     return false;
768                 }
769                 num--, args++;
770             }
771             add_rule(rule);
772         } else if (streq("-r", *args) || streq("--remove", *args)) {
773             num--, args++;
774             if (num < 1)
775                 return false;
776             int idx;
777             while (num > 0) {
778                 if (parse_index(*args, &idx))
779                     remove_rule_by_index(idx - 1);
780                 else if (streq("tail", *args))
781                     remove_rule(rule_tail);
782                 else if (streq("head", *args))
783                     remove_rule(rule_head);
784                 else
785                     remove_rule_by_name(*args);
786                 num--, args++;
787             }
788         } else if (streq("-l", *args) || streq("--list", *args)) {
789             num--, args++;
790             list_rules(num > 0 ? *args : NULL, rsp);
791         } else {
792             return false;
793         }
794         num--, args++;
795     }
796
797     return true;
798 }
799
800 bool cmd_pointer(char **args, int num)
801 {
802     if (num < 1)
803         return false;
804     while (num > 0) {
805         if (streq("-t", *args) || streq("--track", *args)) {
806             num--, args++;
807             if (num < 2)
808                 return false;
809             int x, y;
810             if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
811                 track_pointer(x, y);
812             else
813                 return false;
814         } else if (streq("-g", *args) || streq("--grab", *args)) {
815             num--, args++;
816             if (num < 1)
817                 return false;
818             pointer_action_t pac;
819             if (parse_pointer_action(*args, &pac))
820                 grab_pointer(pac);
821             else
822                 return false;
823         } else {
824             return false;
825         }
826         num--, args++;
827     }
828
829     return true;
830 }
831
832 bool cmd_restore(char **args, int num)
833 {
834     if (num < 1)
835         return false;
836     while (num > 0) {
837         if (streq("-T", *args) || streq("--tree", *args)) {
838             num--, args++;
839             if (num < 1)
840                 return false;
841             restore_tree(*args);
842         } else if (streq("-H", *args) || streq("--history", *args)) {
843             num--, args++;
844             if (num < 1)
845                 return false;
846             restore_history(*args);
847         } else if (streq("-S", *args) || streq("--stack", *args)) {
848             num--, args++;
849             if (num < 1)
850                 return false;
851             restore_stack(*args);
852         } else {
853             return false;
854         }
855         num--, args++;
856     }
857
858     return true;
859 }
860
861 bool cmd_control(char **args, int num)
862 {
863     if (num < 1)
864         return false;
865     while (num > 0) {
866         if (streq("--adopt-orphans", *args)) {
867             adopt_orphans();
868         } else if (streq("--put-status", *args)) {
869             put_status();
870         } else if (streq("--toggle-visibility", *args)) {
871             toggle_visibility();
872         } else if (streq("--record-history", *args)) {
873             num--, args++;
874             if (num < 1)
875                 return false;
876             bool b;
877             if (parse_bool(*args, &b))
878                 record_history = b;
879             else
880                 return false;
881         } else {
882             return false;
883         }
884         num--, args++;
885     }
886
887     return true;
888 }
889
890 bool cmd_config(char **args, int num, char *rsp)
891 {
892     if (num < 1)
893         return false;
894     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
895     coordinates_t trg = {NULL, NULL, NULL};
896     if (*args[0] == OPT_CHR) {
897         if (streq("-d", *args) || streq("--desktop", *args)) {
898             num--, args++;
899             if (num < 1)
900                 return false;
901             if (!desktop_from_desc(*args, &ref, &trg))
902                 return false;
903         } else if (streq("-m", *args) || streq("--monitor", *args)) {
904             num--, args++;
905             if (num < 1)
906                 return false;
907             if (!monitor_from_desc(*args, &ref, &trg))
908                 return false;
909         } else {
910             return false;
911         }
912         num--, args++;
913     }
914     if (num == 2)
915         return set_setting(trg, *args, *(args + 1));
916     else if (num == 1)
917         return get_setting(trg, *args, rsp);
918     else
919         return false;
920 }
921
922 bool cmd_quit(char **args, int num)
923 {
924     if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
925         return false;
926     quit();
927     return true;
928 }
929
930 bool set_setting(coordinates_t loc, char *name, char *value)
931 {
932 #define DESKSET(k, v) \
933         if (loc.desktop != NULL) \
934             loc.desktop->k = v; \
935         else if (loc.monitor != NULL) \
936             for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
937                 d->k = v; \
938         else \
939             for (monitor_t *m = mon_head; m != NULL; m = m->next) \
940                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \
941                     d->k = v;
942     if (streq("border_width", name)) {
943         unsigned int bw;
944         if (sscanf(value, "%u", &bw) != 1)
945             return false;
946         DESKSET(border_width, bw)
947     } else if (streq("window_gap", name)) {
948         int wg;
949         if (sscanf(value, "%i", &wg) != 1)
950             return false;
951         DESKSET(window_gap, wg)
952 #undef DESKSET
953 #define MONSET(k) \
954     } else if (streq(#k, name)) { \
955         int v; \
956         if (sscanf(value, "%i", &v) != 1) \
957             return false; \
958         if (loc.monitor != NULL) \
959             loc.monitor->k = v; \
960         else \
961             for (monitor_t *m = mon_head; m!= NULL; m = m->next) \
962                 m->k = v;
963     MONSET(top_padding)
964     MONSET(right_padding)
965     MONSET(bottom_padding)
966     MONSET(left_padding)
967 #undef MONSET
968     } else if (streq("split_ratio", name)) {
969         double r;
970         if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
971             split_ratio = r;
972         else
973             return false;
974         return true;
975     } else if (streq("growth_factor", name)) {
976         double g;
977         if (sscanf(value, "%lf", &g) == 1 && g > 1)
978             growth_factor = g;
979         else
980             return false;
981         return true;
982 #define SETCOLOR(s) \
983     } else if (streq(#s, name)) { \
984         snprintf(s, sizeof(s), "%s", value);
985     SETCOLOR(focused_border_color)
986     SETCOLOR(active_border_color)
987     SETCOLOR(normal_border_color)
988     SETCOLOR(presel_border_color)
989     SETCOLOR(focused_locked_border_color)
990     SETCOLOR(active_locked_border_color)
991     SETCOLOR(normal_locked_border_color)
992     SETCOLOR(focused_sticky_border_color)
993     SETCOLOR(active_sticky_border_color)
994     SETCOLOR(normal_sticky_border_color)
995     SETCOLOR(urgent_border_color)
996 #undef SETCOLOR
997     } else if (streq("focus_follows_pointer", name)) {
998         bool b;
999         if (parse_bool(value, &b) && b != focus_follows_pointer) {
1000             uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
1001             for (monitor_t *m = mon_head; m != NULL; m = m->next)
1002                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1003                     for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
1004                         xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
1005             if (focus_follows_pointer) {
1006                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
1007                     window_hide(m->root);
1008                 disable_motion_recorder();
1009             } else {
1010                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
1011                     window_show(m->root);
1012                 enable_motion_recorder();
1013             }
1014             focus_follows_pointer = b;
1015             return true;
1016         } else {
1017             return false;
1018         }
1019 #define SETBOOL(s) \
1020     } else if (streq(#s, name)) { \
1021         if (!parse_bool(value, &s)) \
1022             return false;
1023         SETBOOL(borderless_monocle)
1024         SETBOOL(gapless_monocle)
1025         SETBOOL(pointer_follows_monitor)
1026         SETBOOL(apply_floating_atom)
1027         SETBOOL(auto_alternate)
1028         SETBOOL(auto_cancel)
1029         SETBOOL(history_aware_focus)
1030         SETBOOL(honor_ewmh_focus)
1031 #undef SETBOOL
1032     } else {
1033         return false;
1034     }
1035
1036     for (monitor_t *m = mon_head; m != NULL; m = m->next)
1037         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1038             arrange(m, d);
1039
1040     return true;
1041 }
1042
1043 bool get_setting(coordinates_t loc, char *name, char* rsp)
1044 {
1045     if (streq("split_ratio", name))
1046         snprintf(rsp, BUFSIZ, "%lf", split_ratio);
1047     else if (streq("growth_factor", name))
1048         snprintf(rsp, BUFSIZ, "%lf", growth_factor);
1049     else if (streq("window_gap", name))
1050         if (loc.desktop == NULL)
1051             return false;
1052         else
1053             snprintf(rsp, BUFSIZ, "%i", loc.desktop->window_gap);
1054     else if (streq("border_width", name))
1055         if (loc.desktop == NULL)
1056             return false;
1057         else
1058             snprintf(rsp, BUFSIZ, "%u", loc.desktop->border_width);
1059 #define MONGET(k) \
1060     else if (streq(#k, name)) \
1061         if (loc.monitor == NULL) \
1062             return false; \
1063         else \
1064             snprintf(rsp, BUFSIZ, "%i", loc.monitor->k);
1065     MONGET(top_padding)
1066     MONGET(right_padding)
1067     MONGET(bottom_padding)
1068     MONGET(left_padding)
1069 #undef MONGET
1070 #define GETCOLOR(s) \
1071     else if (streq(#s, name)) \
1072         snprintf(rsp, BUFSIZ, "%s", s);
1073     GETCOLOR(focused_border_color)
1074     GETCOLOR(active_border_color)
1075     GETCOLOR(normal_border_color)
1076     GETCOLOR(presel_border_color)
1077     GETCOLOR(focused_locked_border_color)
1078     GETCOLOR(active_locked_border_color)
1079     GETCOLOR(normal_locked_border_color)
1080     GETCOLOR(focused_sticky_border_color)
1081     GETCOLOR(active_sticky_border_color)
1082     GETCOLOR(normal_sticky_border_color)
1083     GETCOLOR(urgent_border_color)
1084 #undef GETCOLOR
1085 #define GETBOOL(s) \
1086     else if (streq(#s, name)) \
1087         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
1088     GETBOOL(borderless_monocle)
1089     GETBOOL(gapless_monocle)
1090     GETBOOL(focus_follows_pointer)
1091     GETBOOL(pointer_follows_monitor)
1092     GETBOOL(apply_floating_atom)
1093     GETBOOL(auto_alternate)
1094     GETBOOL(auto_cancel)
1095     GETBOOL(history_aware_focus)
1096     GETBOOL(honor_ewmh_focus)
1097 #undef GETBOOL
1098     else
1099         return false;
1100     return true;
1101 }
1102
1103 bool parse_bool(char *value, bool *b)
1104 {
1105     if (streq("true", value) || streq("on", value)) {
1106         *b = true;
1107         return true;
1108     } else if (streq("false", value) || streq("off", value)) {
1109         *b = false;
1110         return true;
1111     }
1112     return false;
1113 }
1114
1115 bool parse_layout(char *s, layout_t *l)
1116 {
1117     if (streq("monocle", s)) {
1118         *l = LAYOUT_MONOCLE;
1119         return true;
1120     } else if (streq("tiled", s)) {
1121         *l = LAYOUT_TILED;
1122         return true;
1123     }
1124     return false;
1125 }
1126
1127 bool parse_direction(char *s, direction_t *d)
1128 {
1129     if (streq("right", s)) {
1130         *d = DIR_RIGHT;
1131         return true;
1132     } else if (streq("down", s)) {
1133         *d = DIR_DOWN;
1134         return true;
1135     } else if (streq("left", s)) {
1136         *d = DIR_LEFT;
1137         return true;
1138     } else if (streq("up", s)) {
1139         *d = DIR_UP;
1140         return true;
1141     }
1142     return false;
1143 }
1144
1145 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1146 {
1147     if (streq("next", s)) {
1148         *d = CYCLE_NEXT;
1149         return true;
1150     } else if (streq("prev", s)) {
1151         *d = CYCLE_PREV;
1152         return true;
1153     }
1154     return false;
1155 }
1156
1157 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1158 {
1159     if (streq("forward", s)) {
1160         *d = CIRCULATE_FORWARD;
1161         return true;
1162     } else if (streq("backward", s)) {
1163         *d = CIRCULATE_BACKWARD;
1164         return true;
1165     }
1166     return false;
1167 }
1168
1169 bool parse_history_direction(char *s, history_dir_t *d)
1170 {
1171     if (streq("older", s)) {
1172         *d = HISTORY_OLDER;
1173         return true;
1174     } else if (streq("newer", s)) {
1175         *d = HISTORY_NEWER;
1176         return true;
1177     }
1178     return false;
1179 }
1180
1181
1182 bool parse_flip(char *s, flip_t *f)
1183 {
1184     if (streq("horizontal", s)) {
1185         *f = FLIP_HORIZONTAL;
1186         return true;
1187     } else if (streq("vertical", s)) {
1188         *f = FLIP_VERTICAL;
1189         return true;
1190     }
1191     return false;
1192 }
1193
1194 bool parse_fence_move(char *s, fence_move_t *m)
1195 {
1196     if (streq("push", s)) {
1197         *m = MOVE_PUSH;
1198         return true;
1199     } else if (streq("pull", s)) {
1200         *m = MOVE_PULL;
1201         return true;
1202     }
1203     return false;
1204 }
1205
1206 bool parse_pointer_action(char *s, pointer_action_t *a)
1207 {
1208     if (streq("move", s)) {
1209         *a = ACTION_MOVE;
1210         return true;
1211     } else if (streq("resize_corner", s)) {
1212         *a = ACTION_RESIZE_CORNER;
1213         return true;
1214     } else if (streq("resize_side", s)) {
1215         *a = ACTION_RESIZE_SIDE;
1216         return true;
1217     } else if (streq("focus", s)) {
1218         *a = ACTION_FOCUS;
1219         return true;
1220     }
1221     return false;
1222 }
1223
1224 bool parse_degree(char *s, int *d)
1225 {
1226     int i = atoi(s);
1227     while (i < 0)
1228         i += 360;
1229     while (i > 359)
1230         i -= 360;
1231     if ((i % 90) != 0) {
1232         return false;
1233     } else {
1234         *d = i;
1235         return true;
1236     }
1237 }
1238
1239 bool parse_window_id(char *s, long int *i)
1240 {
1241     char *end;
1242     errno = 0;
1243     long int ret = strtol(s, &end, 0);
1244     if (errno != 0 || *end != '\0')
1245         return false;
1246     else
1247         *i = ret;
1248     return true;
1249 }
1250
1251 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1252 {
1253     *key = strtok(s, EQL_TOK);
1254     char *v = strtok(NULL, EQL_TOK);
1255     if (v == NULL) {
1256         *state = ALTER_TOGGLE;
1257         return true;
1258     } else {
1259         if (parse_bool(v, value)) {
1260             *state = ALTER_SET;
1261             return true;
1262         } else {
1263             return false;
1264         }
1265     }
1266     return false;
1267 }
1268
1269 bool parse_index(char *s, int *i)
1270 {
1271     int idx;
1272     if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1273         return false;
1274     *i = idx;
1275     return true;
1276 }