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