]> git.lizzy.rs Git - bspwm.git/blob - messages.c
Add new option for `control`: `--float-upcoming`
[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("-p", *args) || streq("--pad", *args)) {
392             num--, args++;
393             if (num < 4)
394                 return false;
395             char values[MAXLEN];
396             snprintf(values, sizeof(values), "%s %s %s %s", args[0], args[1], args[2], args[3]);
397             num -= 3;
398             args += 3;
399             if (sscanf(values, "%i %i %i %i", &trg.monitor->top_padding, &trg.monitor->right_padding, &trg.monitor->bottom_padding, &trg.monitor->left_padding) == 4)
400                 for (desktop_t *d = trg.monitor->desk_head; d != NULL; d = d->next)
401                     arrange(trg.monitor, d);
402             else
403                 return false;
404         } else if (streq("-n", *args) || streq("--rename", *args)) {
405             num--, args++;
406             if (num < 1)
407                 return false;
408             strncpy(trg.monitor->name, *args, sizeof(trg.monitor->name));
409             put_status();
410         } else if (streq("-s", *args) || streq("--swap", *args)) {
411             num--, args++;
412             if (num < 1)
413                 return false;
414             coordinates_t dst;
415             if (monitor_from_desc(*args, &trg, &dst))
416                 swap_monitors(trg.monitor, dst.monitor);
417             else
418                 return false;
419         } else {
420             return false;
421         }
422         num--, args++;
423     }
424
425     return true;
426 }
427
428 bool cmd_query(char **args, int num, char *rsp) {
429     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
430     coordinates_t trg = {NULL, NULL, NULL};
431     domain_t dom = DOMAIN_TREE;
432     int d = 0, t = 0;
433
434     while (num > 0) {
435         if (streq("-T", *args) || streq("--tree", *args)) {
436             dom = DOMAIN_TREE, d++;
437         } else if (streq("-M", *args) || streq("--monitors", *args)) {
438             dom = DOMAIN_MONITOR, d++;
439         } else if (streq("-D", *args) || streq("--desktops", *args)) {
440             dom = DOMAIN_DESKTOP, d++;
441         } else if (streq("-W", *args) || streq("--windows", *args)) {
442             dom = DOMAIN_WINDOW, d++;
443         } else if (streq("-H", *args) || streq("--history", *args)) {
444             dom = DOMAIN_HISTORY, d++;
445         } else if (streq("-m", *args) || streq("--monitor", *args)) {
446             trg.monitor = ref.monitor;
447             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
448                 num--, args++;
449                 if (!monitor_from_desc(*args, &ref, &trg))
450                     return false;
451             }
452             t++;
453         } else if (streq("-d", *args) || streq("--desktop", *args)) {
454             trg.monitor = ref.monitor;
455             trg.desktop = ref.desktop;
456             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
457                 num--, args++;
458                 if (!desktop_from_desc(*args, &ref, &trg))
459                     return false;
460             }
461             t++;
462         } else if (streq("-w", *args) || streq("--window", *args)) {
463             trg = ref;
464             if (num > 1 && *(args + 1)[0] != OPT_CHR) {
465                 num--, args++;
466                 if (!node_from_desc(*args, &ref, &trg))
467                     return false;
468             }
469             t++;
470         } else {
471             return false;
472         }
473         num--, args++;
474     }
475
476     if (d != 1 || t > 1)
477         return false;
478
479     if (dom == DOMAIN_HISTORY)
480         query_history(trg, rsp);
481     else if (dom == DOMAIN_WINDOW)
482         query_windows(trg, rsp);
483     else
484         query_monitors(trg, dom, rsp);
485
486     return true;
487 }
488
489 bool cmd_rule(char **args, int num, char *rsp) {
490     if (num < 1)
491         return false;
492     while (num > 0) {
493         if (streq("-a", *args) || streq("--add", *args)) {
494             num--, args++;
495             if (num < 2)
496                 return false;
497             rule_t *rule = make_rule();
498             strncpy(rule->cause.name, *args, sizeof(rule->cause.name));
499             num--, args++;
500             while (num > 0) {
501                 if (streq("--floating", *args)) {
502                     rule->effect.floating = true;
503                 } else if (streq("--follow", *args)) {
504                     rule->effect.follow = true;
505                 } else if (streq("--focus", *args)) {
506                     rule->effect.focus = true;
507                 } else if (streq("--unmanage", *args)) {
508                     rule->effect.unmanage = true;
509                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
510                     num--, args++;
511                     if (num < 1) {
512                         free(rule);
513                         rule_uid--;
514                         return false;
515                     }
516                     strncpy(rule->effect.desc, *args, sizeof(rule->effect.desc));
517                 } else {
518                     free(rule);
519                     rule_uid--;
520                     return false;
521                 }
522                 num--, args++;
523             }
524             add_rule(rule);
525         } else if (streq("-r", *args) || streq("--rm", *args)) {
526             num--, args++;
527             if (num < 1)
528                 return false;
529             unsigned int uid;
530             while (num > 0) {
531                 if (sscanf(*args, "%X", &uid) == 1)
532                     remove_rule_by_uid(uid);
533                 else
534                     return false;
535                 num--, args++;
536             }
537         } else if (streq("-l", *args) || streq("--list", *args)) {
538             num--, args++;
539             list_rules(num > 0 ? *args : NULL, rsp);
540         } else {
541             return false;
542         }
543         num--, args++;
544     }
545
546     return true;
547 }
548
549 bool cmd_pointer(char **args, int num) {
550     if (num < 1)
551         return false;
552     while (num > 0) {
553         if (streq("-t", *args) || streq("--track", *args)) {
554             num--, args++;
555             if (num < 2)
556                 return false;
557             int x, y;
558             if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
559                 track_pointer(x, y);
560             else
561                 return false;
562         } else if (streq("-g", *args) || streq("--grab", *args)) {
563             num--, args++;
564             if (num < 1)
565                 return false;
566             pointer_action_t pac;
567             if (parse_pointer_action(*args, &pac))
568                 grab_pointer(pac);
569             else
570                 return false;
571         } else {
572             return false;
573         }
574         num--, args++;
575     }
576
577     return true;
578 }
579
580 bool cmd_restore(char **args, int num) {
581     if (num < 1)
582         return false;
583     while (num > 0) {
584         if (streq("-T", *args) || streq("--tree", *args)) {
585             num--, args++;
586             if (num < 1)
587                 return false;
588             restore_tree(*args);
589         } else if (streq("-H", *args) || streq("--history", *args)) {
590             num--, args++;
591             if (num < 1)
592                 return false;
593             restore_history(*args);
594         } else {
595             return false;
596         }
597         num--, args++;
598     }
599
600     return true;
601 }
602
603 bool cmd_control(char **args, int num) {
604     if (num < 1)
605         return false;
606     while (num > 0) {
607         if (streq("--adopt-orphans", *args)) {
608             adopt_orphans();
609         } else if (streq("--put-status", *args)) {
610             put_status();
611         } else if (streq("--toggle-visibility", *args)) {
612             toggle_visibility();
613         } else if (streq("--float-upcoming", *args)) {
614             float_upcoming = !float_upcoming;
615         } else {
616             return false;
617         }
618         num--, args++;
619     }
620
621     return true;
622 }
623
624 bool cmd_config(char **args, int num, char *rsp) {
625     if (num < 1)
626         return false;
627     coordinates_t ref = {mon, mon->desk, mon->desk->focus};
628     coordinates_t trg = {NULL, NULL, NULL};
629     if (*args[0] == OPT_CHR) {
630         if (streq("-d", *args) || streq("--desktop", *args)) {
631             num--, args++;
632             if (num < 1)
633                 return false;
634             if (!desktop_from_desc(*args, &ref, &trg))
635                 return false;
636         } else if (streq("-m", *args) || streq("--monitor", *args)) {
637             num--, args++;
638             if (num < 1)
639                 return false;
640             if (!monitor_from_desc(*args, &ref, &trg))
641                 return false;
642         } else {
643             return false;
644         }
645         num--, args++;
646     }
647     if (num == 2)
648         return set_setting(trg, *args, *(args + 1));
649     else if (num == 1)
650         return get_setting(trg, *args, rsp);
651     else
652         return false;
653 }
654
655 bool cmd_quit(char **args, int num) {
656     if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
657         return false;
658     quit();
659     return true;
660 }
661
662 bool handle_message(char *msg, int msg_len, char *rsp)
663 {
664     int cap = INIT_CAP;
665     int num = 0;
666     char **args = malloc(cap * sizeof(char *));
667     if (args == NULL)
668         return false;
669
670     for (int i = 0, j = 0; i < msg_len; i++) {
671         if (msg[i] == 0) {
672             args[num++] = msg + j;
673             j = i + 1;
674         }
675         if (num >= cap) {
676             cap *= 2;
677             char **new = realloc(args, cap * sizeof(char *));
678             if (new == NULL) {
679                 free(args);
680                 return false;
681             } else {
682                 args = new;
683             }
684         }
685     }
686
687     if (num < 1) {
688         free(args);
689         return false;
690     }
691
692     char **args_orig = args;
693     bool ret = process_message(args, num, rsp);
694     free(args_orig);
695     return ret;
696 }
697
698 bool process_message(char **args, int num, char *rsp)
699 {
700     if (streq("window", *args)) {
701         return cmd_window(++args, --num);
702     } else if (streq("desktop", *args)) {
703         return cmd_desktop(++args, --num);
704     } else if (streq("monitor", *args)) {
705         return cmd_monitor(++args, --num);
706     } else if (streq("query", *args)) {
707         return cmd_query(++args, --num, rsp);
708     } else if (streq("restore", *args)) {
709         return cmd_restore(++args, --num);
710     } else if (streq("control", *args)) {
711         return cmd_control(++args, --num);
712     } else if (streq("rule", *args)) {
713         return cmd_rule(++args, --num, rsp);
714     } else if (streq("pointer", *args)) {
715         return cmd_pointer(++args, --num);
716     } else if (streq("config", *args)) {
717         return cmd_config(++args, --num, rsp);
718     } else if (streq("quit", *args)) {
719         return cmd_quit(++args, --num);
720     }
721
722     return false;
723 }
724
725 bool set_setting(coordinates_t loc, char *name, char *value)
726 {
727     if (streq("border_width", name)) {
728         if (sscanf(value, "%u", &border_width) != 1)
729             return false;
730     } else if (streq("window_gap", name)) {
731         int wg;
732         if (sscanf(value, "%i", &wg) != 1)
733             return false;
734         if (loc.desktop != NULL)
735             loc.desktop->window_gap = wg;
736         else if (loc.monitor != NULL)
737             for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next)
738                 d->window_gap = wg;
739         else
740             for (monitor_t *m = mon_head; m != NULL; m = m->next)
741                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
742                     d->window_gap = wg;
743 #define MONSET(k) \
744     } else if (streq(#k, name)) { \
745         int v; \
746         if (sscanf(value, "%i", &v) != 1) \
747             return false; \
748         if (loc.monitor != NULL) \
749             loc.monitor->k = v; \
750         else \
751             for (monitor_t *m = mon_head; m!= NULL; m = m->next) \
752                 m->k = v;
753     MONSET(top_padding)
754     MONSET(right_padding)
755     MONSET(bottom_padding)
756     MONSET(left_padding)
757 #undef MONSET
758     } else if (streq("split_ratio", name)) {
759         double rat;
760         if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1)
761             split_ratio = rat;
762         else
763             return false;
764 #define SETCOLOR(s) \
765     } else if (streq(#s, name)) { \
766         strncpy(s, value, sizeof(s));
767     SETCOLOR(focused_border_color)
768     SETCOLOR(active_border_color)
769     SETCOLOR(normal_border_color)
770     SETCOLOR(presel_border_color)
771     SETCOLOR(focused_locked_border_color)
772     SETCOLOR(active_locked_border_color)
773     SETCOLOR(normal_locked_border_color)
774     SETCOLOR(urgent_border_color)
775 #undef SETCOLOR
776     } else if (streq("focus_follows_pointer", name)) {
777         bool b;
778         if (parse_bool(value, &b) && b != focus_follows_pointer) {
779             uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
780             for (monitor_t *m = mon_head; m != NULL; m = m->next)
781                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
782                     for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
783                         xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
784             if (focus_follows_pointer)
785                 disable_motion_recorder();
786             else
787                 enable_motion_recorder();
788             focus_follows_pointer = b;
789             return true;
790         } else {
791             return false;
792         }
793 #define SETBOOL(s) \
794     } else if (streq(#s, name)) { \
795         if (!parse_bool(value, &s)) \
796             return false;
797         SETBOOL(borderless_monocle)
798         SETBOOL(gapless_monocle)
799         SETBOOL(pointer_follows_monitor)
800         SETBOOL(apply_floating_atom)
801         SETBOOL(auto_alternate)
802         SETBOOL(auto_cancel)
803         SETBOOL(history_aware_focus)
804 #undef SETBOOL
805     } else {
806         return false;
807     }
808
809     for (monitor_t *m = mon_head; m != NULL; m = m->next)
810         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
811             arrange(m, d);
812
813     return true;
814 }
815
816 bool get_setting(coordinates_t loc, char *name, char* rsp)
817 {
818     if (streq("border_width", name))
819         snprintf(rsp, BUFSIZ, "%u", border_width);
820     else if (streq("split_ratio", name))
821         snprintf(rsp, BUFSIZ, "%lf", split_ratio);
822     else if (streq("window_gap", name))
823         if (loc.desktop == NULL)
824             return false;
825         else
826             snprintf(rsp, BUFSIZ, "%i", loc.desktop->window_gap);
827 #define MONGET(k) \
828     else if (streq(#k, name)) \
829         if (loc.monitor == NULL) \
830             return false; \
831         else \
832             snprintf(rsp, BUFSIZ, "%i", loc.monitor->k);
833     MONGET(top_padding)
834     MONGET(right_padding)
835     MONGET(bottom_padding)
836     MONGET(left_padding)
837 #undef MONGET
838 #define GETCOLOR(s) \
839     else if (streq(#s, name)) \
840         snprintf(rsp, BUFSIZ, "%s", s);
841     GETCOLOR(focused_border_color)
842     GETCOLOR(active_border_color)
843     GETCOLOR(normal_border_color)
844     GETCOLOR(presel_border_color)
845     GETCOLOR(focused_locked_border_color)
846     GETCOLOR(active_locked_border_color)
847     GETCOLOR(normal_locked_border_color)
848     GETCOLOR(urgent_border_color)
849 #undef GETCOLOR
850 #define GETBOOL(s) \
851     else if (streq(#s, name)) \
852         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
853     GETBOOL(borderless_monocle)
854     GETBOOL(gapless_monocle)
855     GETBOOL(focus_follows_pointer)
856     GETBOOL(pointer_follows_monitor)
857     GETBOOL(apply_floating_atom)
858     GETBOOL(auto_alternate)
859     GETBOOL(auto_cancel)
860     GETBOOL(history_aware_focus)
861 #undef GETBOOL
862     else
863         return false;
864     return true;
865 }
866
867 bool parse_bool(char *value, bool *b)
868 {
869     if (streq("true", value) || streq("on", value)) {
870         *b = true;
871         return true;
872     } else if (streq("false", value) || streq("off", value)) {
873         *b = false;
874         return true;
875     }
876     return false;
877 }
878
879 bool parse_layout(char *s, layout_t *l)
880 {
881     if (streq("monocle", s)) {
882         *l = LAYOUT_MONOCLE;
883         return true;
884     } else if (streq("tiled", s)) {
885         *l = LAYOUT_TILED;
886         return true;
887     }
888     return false;
889 }
890
891 bool parse_direction(char *s, direction_t *d)
892 {
893     if (streq("right", s)) {
894         *d = DIR_RIGHT;
895         return true;
896     } else if (streq("down", s)) {
897         *d = DIR_DOWN;
898         return true;
899     } else if (streq("left", s)) {
900         *d = DIR_LEFT;
901         return true;
902     } else if (streq("up", s)) {
903         *d = DIR_UP;
904         return true;
905     }
906     return false;
907 }
908
909 bool parse_cycle_direction(char *s, cycle_dir_t *d)
910 {
911     if (streq("next", s)) {
912         *d = CYCLE_NEXT;
913         return true;
914     } else if (streq("prev", s)) {
915         *d = CYCLE_PREV;
916         return true;
917     }
918     return false;
919 }
920
921 bool parse_circulate_direction(char *s, circulate_dir_t *d)
922 {
923     if (streq("forward", s)) {
924         *d = CIRCULATE_FORWARD;
925         return true;
926     } else if (streq("backward", s)) {
927         *d = CIRCULATE_BACKWARD;
928         return true;
929     }
930     return false;
931 }
932
933 bool parse_flip(char *s, flip_t *f)
934 {
935     if (streq("horizontal", s)) {
936         *f = FLIP_HORIZONTAL;
937         return true;
938     } else if (streq("vertical", s)) {
939         *f = FLIP_VERTICAL;
940         return true;
941     }
942     return false;
943 }
944
945 bool parse_fence_move(char *s, fence_move_t *m)
946 {
947     if (streq("push", s)) {
948         *m = MOVE_PUSH;
949         return true;
950     } else if (streq("pull", s)) {
951         *m = MOVE_PULL;
952         return true;
953     }
954     return false;
955 }
956
957 bool parse_pointer_action(char *s, pointer_action_t *a)
958 {
959     if (streq("move", s)) {
960         *a = ACTION_MOVE;
961         return true;
962     } else if (streq("resize_corner", s)) {
963         *a = ACTION_RESIZE_CORNER;
964         return true;
965     } else if (streq("resize_side", s)) {
966         *a = ACTION_RESIZE_SIDE;
967         return true;
968     } else if (streq("focus", s)) {
969         *a = ACTION_FOCUS;
970         return true;
971     }
972     return false;
973 }
974
975 bool parse_degree(char *s, int *d)
976 {
977     int i = atoi(s);
978     while (i < 0)
979         i += 360;
980     while (i > 359)
981         i -= 360;
982     if ((i % 90) != 0) {
983         return false;
984     } else {
985         *d = i;
986         return true;
987     }
988 }
989
990 bool parse_window_id(char *s, long int *i)
991 {
992     char *end;
993     errno = 0;
994     long int ret = strtol(s, &end, 0);
995     if (errno != 0 || *end != '\0')
996         return false;
997     else
998         *i = ret;
999     return true;
1000 }
1001
1002 bool parse_index(char *s, int *i)
1003 {
1004     return sscanf(s, "^%i", i) == 1;
1005 }