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