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