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