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