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