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