]> git.lizzy.rs Git - bspwm.git/blob - messages.c
66bcd872e43f892057327616b7dd1a038f690d57
[bspwm.git] / messages.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 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
20  * ON 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 "rule.h"
37 #include "restore.h"
38 #include "settings.h"
39 #include "tree.h"
40 #include "window.h"
41 #include "common.h"
42 #include "subscribe.h"
43 #include "messages.h"
44
45 int handle_message(char *msg, int msg_len, FILE *rsp)
46 {
47         int cap = INIT_CAP;
48         int num = 0;
49         char **args = malloc(cap * sizeof(char *));
50         if (args == NULL)
51                 return MSG_FAILURE;
52
53         for (int i = 0, j = 0; i < msg_len; i++) {
54                 if (msg[i] == 0) {
55                         args[num++] = msg + j;
56                         j = i + 1;
57                 }
58                 if (num >= cap) {
59                         cap *= 2;
60                         char **new = realloc(args, cap * sizeof(char *));
61                         if (new == NULL) {
62                                 free(args);
63                                 return MSG_FAILURE;
64                         } else {
65                                 args = new;
66                         }
67                 }
68         }
69
70         if (num < 1) {
71                 free(args);
72                 return MSG_SYNTAX;
73         }
74
75         char **args_orig = args;
76         int ret = process_message(args, num, rsp);
77         free(args_orig);
78         return ret;
79 }
80
81 int process_message(char **args, int num, FILE *rsp)
82 {
83         if (streq("window", *args)) {
84                 return cmd_window(++args, --num);
85         } else if (streq("desktop", *args)) {
86                 return cmd_desktop(++args, --num);
87         } else if (streq("monitor", *args)) {
88                 return cmd_monitor(++args, --num);
89         } else if (streq("query", *args)) {
90                 return cmd_query(++args, --num, rsp);
91         } else if (streq("restore", *args)) {
92                 return cmd_restore(++args, --num);
93         } else if (streq("control", *args)) {
94                 return cmd_control(++args, --num, rsp);
95         } else if (streq("rule", *args)) {
96                 return cmd_rule(++args, --num, rsp);
97         } else if (streq("pointer", *args)) {
98                 return cmd_pointer(++args, --num);
99         } else if (streq("config", *args)) {
100                 return cmd_config(++args, --num, rsp);
101         } else if (streq("quit", *args)) {
102                 return cmd_quit(++args, --num);
103         }
104
105         return MSG_UNKNOWN;
106 }
107
108 int cmd_window(char **args, int num)
109 {
110         if (num < 1)
111                 return MSG_SYNTAX;
112
113         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
114         coordinates_t trg = ref;
115
116         if ((*args)[0] != OPT_CHR) {
117                 if (node_from_desc(*args, &ref, &trg))
118                         num--, args++;
119                 else
120                         return MSG_FAILURE;
121         }
122
123         if (trg.node == NULL)
124                 return MSG_FAILURE;
125
126         bool dirty = false;
127
128         while (num > 0) {
129                 if (streq("-f", *args) || streq("--focus", *args)) {
130                         coordinates_t dst = trg;
131                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
132                                 num--, args++;
133                                 if (!node_from_desc(*args, &trg, &dst))
134                                         return MSG_FAILURE;
135                         }
136                         focus_node(dst.monitor, dst.desktop, dst.node);
137                 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
138                         num--, args++;
139                         coordinates_t dst;
140                         if (desktop_from_desc(*args, &trg, &dst)) {
141                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus)) {
142                                         trg.monitor = dst.monitor;
143                                         trg.desktop = dst.desktop;
144                                 }
145                         } else {
146                                 return MSG_FAILURE;
147                         }
148                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
149                         num--, args++;
150                         if (num < 1)
151                                 return MSG_SYNTAX;
152                         coordinates_t dst;
153                         if (monitor_from_desc(*args, &trg, &dst)) {
154                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus)) {
155                                         trg.monitor = dst.monitor;
156                                         trg.desktop = dst.monitor->desk;
157                                 }
158                         } else {
159                                 return MSG_FAILURE;
160                         }
161                 } else if (streq("-w", *args) || streq("--to-window", *args)) {
162                         num--, args++;
163                         if (num < 1)
164                                 return MSG_SYNTAX;
165                         coordinates_t dst;
166                         if (node_from_desc(*args, &trg, &dst)) {
167                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
168                                         trg.monitor = dst.monitor;
169                                         trg.desktop = dst.desktop;
170                                 }
171                         } else {
172                                 return MSG_FAILURE;
173                         }
174                 } else if (streq("-s", *args) || streq("--swap", *args)) {
175                         num--, args++;
176                         if (num < 1)
177                                 return MSG_SYNTAX;
178                         coordinates_t dst;
179                         if (node_from_desc(*args, &trg, &dst)) {
180                                 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
181                                         if (trg.desktop != dst.desktop)
182                                                 arrange(trg.monitor, trg.desktop);
183                                         trg.monitor = dst.monitor;
184                                         trg.desktop = dst.desktop;
185                                         dirty = true;
186                                 }
187                         } else {
188                                 return MSG_FAILURE;
189                         }
190                 } else if (streq("-t", *args) || streq("--toggle", *args)) {
191                         num--, args++;
192                         if (num < 1)
193                                 return MSG_SYNTAX;
194                         char *key = strtok(*args, EQL_TOK);
195                         char *val = strtok(NULL, EQL_TOK);
196                         alter_state_t a;
197                         bool b;
198                         if (val == NULL) {
199                                 a = ALTER_TOGGLE;
200                         } else {
201                                 if (parse_bool(val, &b))
202                                         a = ALTER_SET;
203                                 else
204                                         return MSG_FAILURE;
205                         }
206                         if (streq("fullscreen", key)) {
207                                 set_fullscreen(trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
208                                 dirty = true;
209                         } else if (streq("pseudo_tiled", key)) {
210                                 set_pseudo_tiled(trg.node, (a == ALTER_SET ? b : !trg.node->client->pseudo_tiled));
211                                 dirty = true;
212                         } else if (streq("floating", key)) {
213                                 set_floating(trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
214                                 dirty = true;
215                         } else if (streq("locked", key)) {
216                                 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
217                         } else if (streq("sticky", key)) {
218                                 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->sticky));
219                         } else if (streq("private", key)) {
220                                 set_private(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->private));
221                         } else {
222                                 return MSG_FAILURE;
223                         }
224                 } else if (streq("-p", *args) || streq("--presel", *args)) {
225                         num--, args++;
226                         if (num < 1)
227                                 return MSG_SYNTAX;
228                         if (!is_tiled(trg.node->client) ||
229                             trg.desktop->layout != LAYOUT_TILED)
230                                 return MSG_FAILURE;
231                         if (streq("cancel", *args)) {
232                                 reset_mode(&trg);
233                         } else {
234                                 direction_t dir;
235                                 if (parse_direction(*args, &dir)) {
236                                         double rat = trg.node->split_ratio;
237                                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
238                                                 num--, args++;
239                                                 if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1)
240                                                         return MSG_FAILURE;
241                                         }
242                                         if (auto_cancel && trg.node->split_mode == MODE_MANUAL &&
243                                             dir == trg.node->split_dir &&
244                                             rat == trg.node->split_ratio) {
245                                                 reset_mode(&trg);
246                                         } else {
247                                                 trg.node->split_mode = MODE_MANUAL;
248                                                 trg.node->split_dir = dir;
249                                                 trg.node->split_ratio = rat;
250                                         }
251                                         window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
252                                 } else {
253                                         return MSG_FAILURE;
254                                 }
255                         }
256                 } else if (streq("-e", *args) || streq("--edge", *args)) {
257                         num--, args++;
258                         if (num < 2)
259                                 return MSG_SYNTAX;
260                         if (!is_tiled(trg.node->client))
261                                 return MSG_FAILURE;
262                         direction_t dir;
263                         if (!parse_direction(*args, &dir))
264                                 return MSG_FAILURE;
265                         node_t *n = find_fence(trg.node, dir);
266                         if (n == NULL)
267                                 return MSG_FAILURE;
268                         num--, args++;
269                         if ((*args)[0] == '+' || (*args)[0] == '-') {
270                                 int pix;
271                                 if (sscanf(*args, "%i", &pix) == 1) {
272                                         int max = (n->split_type == TYPE_HORIZONTAL ? n->rectangle.height : n->rectangle.width);
273                                         double rat = ((max * n->split_ratio) + pix) / max;
274                                         if (rat > 0 && rat < 1)
275                                                 n->split_ratio = rat;
276                                         else
277                                                 return MSG_FAILURE;
278                                 } else {
279                                         return MSG_FAILURE;
280                                 }
281                         } else {
282                                 double rat;
283                                 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1)
284                                         n->split_ratio = rat;
285                                 else
286                                         return MSG_FAILURE;
287                         }
288                         dirty = true;
289                 } else if (streq("-r", *args) || streq("--ratio", *args)) {
290                         num--, args++;
291                         if (num < 1)
292                                 return MSG_SYNTAX;
293                         double rat;
294                         if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
295                                 trg.node->split_ratio = rat;
296                                 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
297                         } else {
298                                 return MSG_FAILURE;
299                         }
300                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
301                         num--, args++;
302                         if (num < 2)
303                                 return MSG_SYNTAX;
304                         direction_t dir;
305                         if (!parse_direction(*args, &dir))
306                                 return MSG_FAILURE;
307                         node_t *n = find_fence(trg.node, dir);
308                         if (n == NULL)
309                                 return MSG_FAILURE;
310                         num--, args++;
311                         int deg;
312                         if (parse_degree(*args, &deg)) {
313                                 rotate_tree(n, deg);
314                                 dirty = true;
315                         } else {
316                                 return MSG_FAILURE;
317                         }
318                 } else if (streq("-c", *args) || streq("--close", *args)) {
319                         if (num > 1)
320                                 return MSG_SYNTAX;
321                         window_close(trg.node);
322                 } else if (streq("-k", *args) || streq("--kill", *args)) {
323                         if (num > 1)
324                                 return MSG_SYNTAX;
325                         window_kill(trg.monitor, trg.desktop, trg.node);
326                         dirty = true;
327                 } else {
328                         return MSG_SYNTAX;
329                 }
330
331                 num--, args++;
332         }
333
334         if (dirty)
335                 arrange(trg.monitor, trg.desktop);
336
337         return MSG_SUCCESS;
338 }
339
340 int cmd_desktop(char **args, int num)
341 {
342         if (num < 1)
343                 return MSG_SYNTAX;
344
345         coordinates_t ref = {mon, mon->desk, NULL};
346         coordinates_t trg = ref;
347
348         if ((*args)[0] != OPT_CHR) {
349                 if (desktop_from_desc(*args, &ref, &trg))
350                         num--, args++;
351                 else
352                         return MSG_FAILURE;
353         }
354
355         bool dirty = false;
356
357         while (num > 0) {
358                 if (streq("-f", *args) || streq("--focus", *args)) {
359                         coordinates_t dst = trg;
360                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
361                                 num--, args++;
362                                 if (!desktop_from_desc(*args, &trg, &dst))
363                                         return MSG_FAILURE;
364                         }
365                         if (auto_alternate && dst.desktop == mon->desk) {
366                                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
367                                 history_find_desktop(HISTORY_OLDER, &trg, &dst, sel);
368                         }
369                         focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
370                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
371                         num--, args++;
372                         if (num < 1)
373                                 return MSG_SYNTAX;
374                         if (trg.monitor->desk_head == trg.monitor->desk_tail)
375                                 return MSG_FAILURE;
376                         coordinates_t dst;
377                         if (monitor_from_desc(*args, &trg, &dst)) {
378                                 transfer_desktop(trg.monitor, dst.monitor, trg.desktop);
379                                 trg.monitor = dst.monitor;
380                                 update_current();
381                         } else {
382                                 return MSG_FAILURE;
383                         }
384                 } else if (streq("-s", *args) || streq("--swap", *args)) {
385                         num--, args++;
386                         if (num < 1)
387                                 return MSG_SYNTAX;
388                         coordinates_t dst;
389                         if (desktop_from_desc(*args, &trg, &dst))
390                                 swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop);
391                         else
392                                 return MSG_FAILURE;
393                 } else if (streq("-b", *args) || streq("--bubble", *args)) {
394                         num--, args++;
395                         if (num < 1)
396                                 return MSG_SYNTAX;
397                         cycle_dir_t cyc;
398                         if (parse_cycle_direction(*args, &cyc)) {
399                                 desktop_t *d = trg.desktop;
400                                 if (cyc == CYCLE_PREV) {
401                                         if (d->prev == NULL) {
402                                                 while (d->next != NULL) {
403                                                         swap_desktops(trg.monitor, d, trg.monitor, d->next);
404                                                 }
405                                         } else {
406                                                 swap_desktops(trg.monitor, d, trg.monitor, d->prev);
407                                         }
408                                 } else {
409                                         if (d->next == NULL) {
410                                                 while (d->prev != NULL) {
411                                                         swap_desktops(trg.monitor, d, trg.monitor, d->prev);
412                                                 }
413                                         } else {
414                                                 swap_desktops(trg.monitor, d, trg.monitor, d->next);
415                                         }
416                                 }
417                         } else {
418                                 return MSG_FAILURE;
419                         }
420                 } else if (streq("-l", *args) || streq("--layout", *args)) {
421                         num--, args++;
422                         if (num < 1)
423                                 return MSG_SYNTAX;
424                         layout_t lyt;
425                         cycle_dir_t cyc;
426                         if (parse_cycle_direction(*args, &cyc))
427                                 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
428                         else if (parse_layout(*args, &lyt))
429                                 change_layout(trg.monitor, trg.desktop, lyt);
430                         else
431                                 return MSG_FAILURE;
432                 } else if (streq("-n", *args) || streq("--rename", *args)) {
433                         num--, args++;
434                         if (num < 1)
435                                 return MSG_SYNTAX;
436                         put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename %s %s %s\n", trg.monitor->name, trg.desktop->name, *args);
437                         snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
438                         ewmh_update_desktop_names();
439                         put_status(SBSC_MASK_REPORT);
440                 } else if (streq("-r", *args) || streq("--remove", *args)) {
441                         if (trg.desktop->root == NULL &&
442                             trg.monitor->desk_head != trg.monitor->desk_tail) {
443                                 remove_desktop(trg.monitor, trg.desktop);
444                                 show_desktop(trg.monitor->desk);
445                                 update_current();
446                                 return MSG_SUCCESS;
447                         } else {
448                                 return MSG_FAILURE;
449                         }
450                 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
451                         reset_mode(&trg);
452                 } else if (streq("-F", *args) || streq("--flip", *args)) {
453                         num--, args++;
454                         if (num < 1)
455                                 return MSG_SYNTAX;
456                         flip_t flp;
457                         if (parse_flip(*args, &flp)) {
458                                 flip_tree(trg.desktop->root, flp);
459                                 dirty = true;
460                         } else {
461                                 return MSG_FAILURE;
462                         }
463                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
464                         num--, args++;
465                         if (num < 1)
466                                 return MSG_SYNTAX;
467                         int deg;
468                         if (parse_degree(*args, &deg)) {
469                                 rotate_tree(trg.desktop->root, deg);
470                                 dirty = true;
471                         } else {
472                                 return MSG_FAILURE;
473                         }
474                 } else if (streq("-E", *args) || streq("--equalize", *args)) {
475                         equalize_tree(trg.desktop->root);
476                         dirty = true;
477                 } else if (streq("-B", *args) || streq("--balance", *args)) {
478                         balance_tree(trg.desktop->root);
479                         dirty = true;
480                 } else if (streq("-C", *args) || streq("--circulate", *args)) {
481                         num--, args++;
482                         if (num < 1)
483                                 return MSG_SYNTAX;
484                         circulate_dir_t cir;
485                         if (parse_circulate_direction(*args, &cir)) {
486                                 circulate_leaves(trg.monitor, trg.desktop, cir);
487                                 dirty = true;
488                         } else {
489                                 return MSG_FAILURE;
490                         }
491                 } else if (streq("-t", *args) || streq("--toggle", *args)) {
492                         num--, args++;
493                         if (num < 1)
494                                 return MSG_SYNTAX;
495                         char *key = strtok(*args, EQL_TOK);
496                         char *val = strtok(NULL, EQL_TOK);
497                         alter_state_t a;
498                         bool b;
499                         if (val == NULL) {
500                                 a = ALTER_TOGGLE;
501                         } else {
502                                 if (parse_bool(val, &b))
503                                         a = ALTER_SET;
504                                 else
505                                         return MSG_FAILURE;
506                         }
507                         if (streq("floating", key))
508                                 trg.desktop->floating = (a == ALTER_SET ? b : !trg.desktop->floating);
509                         else
510                                 return MSG_FAILURE;
511                 } else {
512                         return MSG_SYNTAX;
513                 }
514                 num--, args++;
515         }
516
517         if (dirty)
518                 arrange(trg.monitor, trg.desktop);
519
520         return MSG_SUCCESS;
521 }
522
523 int cmd_monitor(char **args, int num)
524 {
525         if (num < 1)
526                 return MSG_SYNTAX;
527
528         coordinates_t ref = {mon, NULL, NULL};
529         coordinates_t trg = ref;
530
531         if ((*args)[0] != OPT_CHR) {
532                 if (monitor_from_desc(*args, &ref, &trg))
533                         num--, args++;
534                 else
535                         return MSG_FAILURE;
536         }
537
538         while (num > 0) {
539                 if (streq("-f", *args) || streq("--focus", *args)) {
540                         coordinates_t dst = trg;
541                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
542                                 num--, args++;
543                                 if (!monitor_from_desc(*args, &trg, &dst))
544                                         return MSG_FAILURE;
545                         }
546                         if (auto_alternate && dst.monitor == mon) {
547                                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
548                                 history_find_monitor(HISTORY_OLDER, &trg, &dst, sel);
549                         }
550                         focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
551                 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
552                         num--, args++;
553                         if (num < 1)
554                                 return MSG_SYNTAX;
555                         desktop_t *d = trg.monitor->desk_head;
556                         while (num > 0 && d != NULL) {
557                                 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename %s %s %s\n", trg.monitor->name, d->name, *args);
558                                 snprintf(d->name, sizeof(d->name), "%s", *args);
559                                 initialize_desktop(d);
560                                 arrange(trg.monitor, d);
561                                 d = d->next;
562                                 num--, args++;
563                         }
564                         put_status(SBSC_MASK_REPORT);
565                         while (num > 0) {
566                                 add_desktop(trg.monitor, make_desktop(*args));
567                                 num--, args++;
568                         }
569                         while (d != NULL) {
570                                 desktop_t *next = d->next;
571                                 if (d == mon->desk)
572                                         focus_node(trg.monitor, d->prev, d->prev->focus);
573                                 merge_desktops(trg.monitor, d, mon, mon->desk);
574                                 remove_desktop(trg.monitor, d);
575                                 d = next;
576                         }
577                 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
578                         num--, args++;
579                         if (num < 1)
580                                 return MSG_SYNTAX;
581                         while (num > 0) {
582                                 add_desktop(trg.monitor, make_desktop(*args));
583                                 num--, args++;
584                         }
585                 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
586                         num--, args++;
587                         if (num < 1)
588                                 return MSG_SYNTAX;
589                         while (num > 0) {
590                                 coordinates_t dst;
591                                 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
592                                         remove_desktop(dst.monitor, dst.desktop);
593                                         show_desktop(dst.monitor->desk);
594                                 }
595                                 num--, args++;
596                         }
597                 } else if (streq("-o", *args) || streq("--order-desktops", *args)) {
598                         num--, args++;
599                         if (num < 1)
600                                 return MSG_SYNTAX;
601                         desktop_t *d = trg.monitor->desk_head;
602                         while (d != NULL && num > 0) {
603                                 desktop_t *next = d->next;
604                                 coordinates_t dst;
605                                 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
606                                         swap_desktops(trg.monitor, d, dst.monitor, dst.desktop);
607                                         if (next == dst.desktop)
608                                                 next = d;
609                                 }
610                                 d = next;
611                                 num--, args++;
612                         }
613                 } else if (streq("-n", *args) || streq("--rename", *args)) {
614                         num--, args++;
615                         if (num < 1)
616                                 return MSG_SYNTAX;
617                         put_status(SBSC_MASK_MONITOR_RENAME, "monitor_rename %s %s\n", trg.monitor->name, *args);
618                         snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
619                         put_status(SBSC_MASK_REPORT);
620                 } else if (streq("-s", *args) || streq("--swap", *args)) {
621                         num--, args++;
622                         if (num < 1)
623                                 return MSG_SYNTAX;
624                         coordinates_t dst;
625                         if (monitor_from_desc(*args, &trg, &dst))
626                                 swap_monitors(trg.monitor, dst.monitor);
627                         else
628                                 return MSG_FAILURE;
629                 } else {
630                         return MSG_SYNTAX;
631                 }
632                 num--, args++;
633         }
634
635         return MSG_SUCCESS;
636 }
637
638 int cmd_query(char **args, int num, FILE *rsp)
639 {
640         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
641         coordinates_t trg = {NULL, NULL, NULL};
642         domain_t dom = DOMAIN_TREE;
643         int d = 0, t = 0;
644
645         while (num > 0) {
646                 if (streq("-T", *args) || streq("--tree", *args)) {
647                         dom = DOMAIN_TREE, d++;
648                 } else if (streq("-M", *args) || streq("--monitors", *args)) {
649                         dom = DOMAIN_MONITOR, d++;
650                 } else if (streq("-D", *args) || streq("--desktops", *args)) {
651                         dom = DOMAIN_DESKTOP, d++;
652                 } else if (streq("-W", *args) || streq("--windows", *args)) {
653                         dom = DOMAIN_WINDOW, d++;
654                 } else if (streq("-H", *args) || streq("--history", *args)) {
655                         dom = DOMAIN_HISTORY, d++;
656                 } else if (streq("-S", *args) || streq("--stack", *args)) {
657                         dom = DOMAIN_STACK, d++;
658                 } else if (streq("-m", *args) || streq("--monitor", *args)) {
659                         trg.monitor = ref.monitor;
660                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
661                                 num--, args++;
662                                 if (!monitor_from_desc(*args, &ref, &trg))
663                                         return MSG_FAILURE;
664                         }
665                         t++;
666                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
667                         trg.monitor = ref.monitor;
668                         trg.desktop = ref.desktop;
669                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
670                                 num--, args++;
671                                 if (!desktop_from_desc(*args, &ref, &trg))
672                                         return MSG_FAILURE;
673                         }
674                         t++;
675                 } else if (streq("-w", *args) || streq("--window", *args)) {
676                         trg = ref;
677                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
678                                 num--, args++;
679                                 if (!node_from_desc(*args, &ref, &trg))
680                                         return MSG_FAILURE;
681                         }
682                         t++;
683                 } else {
684                         return MSG_SYNTAX;
685                 }
686                 num--, args++;
687         }
688
689         if (d != 1 || t > 1)
690                 return MSG_SYNTAX;
691
692         if (dom == DOMAIN_HISTORY)
693                 query_history(trg, rsp);
694         else if (dom == DOMAIN_STACK)
695                 query_stack(rsp);
696         else if (dom == DOMAIN_WINDOW)
697                 query_windows(trg, rsp);
698         else
699                 query_monitors(trg, dom, rsp);
700
701         return MSG_SUCCESS;
702 }
703
704 int cmd_rule(char **args, int num, FILE *rsp)
705 {
706         if (num < 1)
707                 return MSG_SYNTAX;
708         while (num > 0) {
709                 if (streq("-a", *args) || streq("--add", *args)) {
710                         num--, args++;
711                         if (num < 2)
712                                 return MSG_SYNTAX;
713                         rule_t *rule = make_rule();
714                         snprintf(rule->cause, sizeof(rule->cause), "%s", *args);
715                         num--, args++;
716                         size_t i = 0;
717                         while (num > 0) {
718                                 if (streq("-o", *args) || streq("--one-shot", *args)) {
719                                         rule->one_shot = true;
720                                 } else {
721                                         for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++)
722                                                 rule->effect[i] = (*args)[j];
723                                         if (num > 1 && i < sizeof(rule->effect))
724                                                 rule->effect[i++] = ' ';
725                                 }
726                                 num--, args++;
727                         }
728                         rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
729                         add_rule(rule);
730                 } else if (streq("-r", *args) || streq("--remove", *args)) {
731                         num--, args++;
732                         if (num < 1)
733                                 return MSG_SYNTAX;
734                         int idx;
735                         while (num > 0) {
736                                 if (parse_index(*args, &idx))
737                                         remove_rule_by_index(idx - 1);
738                                 else if (streq("tail", *args))
739                                         remove_rule(rule_tail);
740                                 else if (streq("head", *args))
741                                         remove_rule(rule_head);
742                                 else
743                                         remove_rule_by_cause(*args);
744                                 num--, args++;
745                         }
746                 } else if (streq("-l", *args) || streq("--list", *args)) {
747                         num--, args++;
748                         list_rules(num > 0 ? *args : NULL, rsp);
749                 } else {
750                         return MSG_SYNTAX;
751                 }
752                 num--, args++;
753         }
754
755         return MSG_SUCCESS;
756 }
757
758 int cmd_pointer(char **args, int num)
759 {
760         if (num < 1)
761                 return MSG_SYNTAX;
762         while (num > 0) {
763                 if (streq("-t", *args) || streq("--track", *args)) {
764                         num--, args++;
765                         if (num < 2)
766                                 return MSG_SYNTAX;
767                         int x, y;
768                         if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
769                                 track_pointer(x, y);
770                         else
771                                 return MSG_FAILURE;
772                         num--, args++;
773                 } else if (streq("-g", *args) || streq("--grab", *args)) {
774                         num--, args++;
775                         if (num < 1)
776                                 return MSG_SYNTAX;
777                         pointer_action_t pac;
778                         if (parse_pointer_action(*args, &pac))
779                                 grab_pointer(pac);
780                         else
781                                 return MSG_FAILURE;
782                 } else if (streq("-u", *args) || streq("--ungrab", *args)) {
783                         ungrab_pointer();
784                 } else {
785                         return MSG_SYNTAX;
786                 }
787                 num--, args++;
788         }
789
790         return MSG_SUCCESS;
791 }
792
793 int cmd_restore(char **args, int num)
794 {
795         if (num < 1)
796                 return MSG_SYNTAX;
797         while (num > 0) {
798                 if (streq("-T", *args) || streq("--tree", *args)) {
799                         num--, args++;
800                         if (num < 1)
801                                 return MSG_SYNTAX;
802                         restore_tree(*args);
803                 } else if (streq("-H", *args) || streq("--history", *args)) {
804                         num--, args++;
805                         if (num < 1)
806                                 return MSG_SYNTAX;
807                         restore_history(*args);
808                 } else if (streq("-S", *args) || streq("--stack", *args)) {
809                         num--, args++;
810                         if (num < 1)
811                                 return MSG_SYNTAX;
812                         restore_stack(*args);
813                 } else {
814                         return MSG_SYNTAX;
815                 }
816                 num--, args++;
817         }
818
819         return MSG_SUCCESS;
820 }
821
822 int cmd_control(char **args, int num, FILE *rsp)
823 {
824         if (num < 1)
825                 return MSG_SYNTAX;
826         while (num > 0) {
827                 if (streq("--adopt-orphans", *args)) {
828                         adopt_orphans();
829                 } else if (streq("--toggle-visibility", *args)) {
830                         toggle_visibility();
831                 } else if (streq("--subscribe", *args)) {
832                         num--, args++;
833                         int field = 0;
834                         if (num < 1) {
835                                 field = SBSC_MASK_REPORT;
836                         } else {
837                                 subscriber_mask_t mask;
838                                 while (num > 0) {
839                                         if (parse_subscriber_mask(*args, &mask)) {
840                                                 field |= mask;
841                                         } else {
842                                                 return MSG_SYNTAX;
843                                         }
844                                         num--, args++;
845                                 }
846                         }
847                         add_subscriber(rsp, field);
848                         return MSG_SUBSCRIBE;
849                 } else if (streq("--get-status", *args)) {
850                         print_report(rsp);
851                 } else if (streq("--record-history", *args)) {
852                         num--, args++;
853                         if (num < 1)
854                                 return MSG_SYNTAX;
855                         bool b;
856                         if (parse_bool(*args, &b))
857                                 record_history = b;
858                         else
859                                 return MSG_SYNTAX;
860                 } else {
861                         return MSG_SYNTAX;
862                 }
863                 num--, args++;
864         }
865
866         return MSG_SUCCESS;
867 }
868
869 int cmd_config(char **args, int num, FILE *rsp)
870 {
871         if (num < 1)
872                 return MSG_SYNTAX;
873         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
874         coordinates_t trg = {NULL, NULL, NULL};
875         if ((*args)[0] == OPT_CHR) {
876                 if (streq("-m", *args) || streq("--monitor", *args)) {
877                         num--, args++;
878                         if (num < 1)
879                                 return MSG_SYNTAX;
880                         if (!monitor_from_desc(*args, &ref, &trg))
881                                 return MSG_FAILURE;
882                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
883                         num--, args++;
884                         if (num < 1)
885                                 return MSG_SYNTAX;
886                         if (!desktop_from_desc(*args, &ref, &trg))
887                                 return MSG_FAILURE;
888                 } else if (streq("-w", *args) || streq("--window", *args)) {
889                         num--, args++;
890                         if (num < 1)
891                                 return MSG_SYNTAX;
892                         if (!node_from_desc(*args, &ref, &trg))
893                                 return MSG_FAILURE;
894                 } else {
895                         return MSG_SYNTAX;
896                 }
897                 num--, args++;
898         }
899         if (num == 2)
900                 return set_setting(trg, *args, *(args + 1));
901         else if (num == 1)
902                 return get_setting(trg, *args, rsp);
903         else
904                 return MSG_SYNTAX;
905 }
906
907 int cmd_quit(char **args, int num)
908 {
909         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
910                 return MSG_FAILURE;
911         running = false;
912         return MSG_SUCCESS;
913 }
914
915 int set_setting(coordinates_t loc, char *name, char *value)
916 {
917 #define DESKWINDEFSET(k, v) \
918                 if (loc.node != NULL) \
919                         loc.node->client->k = v; \
920                 else if (loc.desktop != NULL) \
921                         loc.desktop->k = v; \
922                 else if (loc.monitor != NULL) \
923                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
924                                 d->k = v; \
925                 else \
926                         k = v;
927         if (streq("border_width", name)) {
928                 unsigned int bw;
929                 if (sscanf(value, "%u", &bw) != 1)
930                         return MSG_FAILURE;
931                 DESKWINDEFSET(border_width, bw)
932 #undef DESKWINDEFSET
933 #define DESKDEFSET(k, v) \
934                 if (loc.desktop != NULL) \
935                         loc.desktop->k = v; \
936                 else if (loc.monitor != NULL) \
937                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
938                                 d->k = v; \
939                 else \
940                         k = v;
941         } else if (streq("window_gap", name)) {
942                 int wg;
943                 if (sscanf(value, "%i", &wg) != 1)
944                         return MSG_FAILURE;
945                 DESKDEFSET(window_gap, wg)
946 #undef DESKDEFSET
947 #define MONDESKSET(k, v) \
948                 if (loc.desktop != NULL) \
949                         loc.desktop->k = v; \
950                 else if (loc.monitor != NULL) \
951                         loc.monitor->k = v; \
952                 else \
953                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
954                                 m->k = v;
955         } else if (streq("top_padding", name)) {
956                 int tp;
957                 if (sscanf(value, "%i", &tp) != 1)
958                         return MSG_FAILURE;
959                 MONDESKSET(top_padding, tp)
960         } else if (streq("right_padding", name)) {
961                 int rp;
962                 if (sscanf(value, "%i", &rp) != 1)
963                         return MSG_FAILURE;
964                 MONDESKSET(right_padding, rp)
965         } else if (streq("bottom_padding", name)) {
966                 int bp;
967                 if (sscanf(value, "%i", &bp) != 1)
968                         return MSG_FAILURE;
969                 MONDESKSET(bottom_padding, bp)
970         } else if (streq("left_padding", name)) {
971                 int lp;
972                 if (sscanf(value, "%i", &lp) != 1)
973                         return MSG_FAILURE;
974                 MONDESKSET(left_padding, lp)
975 #undef MONDESKSET
976 #define SETSTR(s) \
977         } else if (streq(#s, name)) { \
978                 if (snprintf(s, sizeof(s), "%s", value) < 0) \
979                         return MSG_FAILURE;
980         SETSTR(external_rules_command)
981         SETSTR(status_prefix)
982 #undef SETSTR
983         } else if (streq("split_ratio", name)) {
984                 double r;
985                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
986                         split_ratio = r;
987                 else
988                         return MSG_FAILURE;
989                 return MSG_SUCCESS;
990 #define SETCOLOR(s) \
991         } else if (streq(#s, name)) { \
992                 snprintf(s, sizeof(s), "%s", value);
993         SETCOLOR(focused_border_color)
994         SETCOLOR(active_border_color)
995         SETCOLOR(normal_border_color)
996         SETCOLOR(presel_border_color)
997         SETCOLOR(focused_locked_border_color)
998         SETCOLOR(active_locked_border_color)
999         SETCOLOR(normal_locked_border_color)
1000         SETCOLOR(focused_sticky_border_color)
1001         SETCOLOR(active_sticky_border_color)
1002         SETCOLOR(normal_sticky_border_color)
1003         SETCOLOR(focused_private_border_color)
1004         SETCOLOR(active_private_border_color)
1005         SETCOLOR(normal_private_border_color)
1006         SETCOLOR(urgent_border_color)
1007 #undef SETCOLOR
1008         } else if (streq("initial_polarity", name)) {
1009                 child_polarity_t p;
1010                 if (parse_child_polarity(value, &p)) {
1011                         initial_polarity = p;
1012                 } else {
1013                         return MSG_FAILURE;
1014                 }
1015         } else if (streq("focus_follows_pointer", name)) {
1016                 bool b;
1017                 if (parse_bool(value, &b) && b != focus_follows_pointer) {
1018                         focus_follows_pointer = b;
1019                         uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
1020                         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1021                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1022                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
1023                                                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
1024                                         }
1025                                 }
1026                         }
1027                         if (focus_follows_pointer) {
1028                                 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1029                                         window_show(m->root);
1030                                 }
1031                         } else {
1032                                 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1033                                         window_hide(m->root);
1034                                 }
1035                                 disable_motion_recorder();
1036                         }
1037                         return MSG_SUCCESS;
1038                 } else {
1039                         return MSG_FAILURE;
1040                 }
1041 #define SETBOOL(s) \
1042         } else if (streq(#s, name)) { \
1043                 if (!parse_bool(value, &s)) \
1044                         return MSG_FAILURE;
1045                 SETBOOL(borderless_monocle)
1046                 SETBOOL(gapless_monocle)
1047                 SETBOOL(leaf_monocle)
1048                 SETBOOL(pointer_follows_focus)
1049                 SETBOOL(pointer_follows_monitor)
1050                 SETBOOL(apply_floating_atom)
1051                 SETBOOL(auto_alternate)
1052                 SETBOOL(auto_cancel)
1053                 SETBOOL(history_aware_focus)
1054                 SETBOOL(focus_by_distance)
1055                 SETBOOL(ignore_ewmh_focus)
1056                 SETBOOL(center_pseudo_tiled)
1057 #undef SETBOOL
1058 #define SETMONBOOL(s) \
1059         } else if (streq(#s, name)) { \
1060                 if (!parse_bool(value, &s)) \
1061                         return MSG_FAILURE; \
1062                 if (s) \
1063                         update_monitors();
1064                 SETMONBOOL(remove_disabled_monitors)
1065                 SETMONBOOL(remove_unplugged_monitors)
1066                 SETMONBOOL(merge_overlapping_monitors)
1067 #undef SETMONBOOL
1068         } else {
1069                 return MSG_FAILURE;
1070         }
1071
1072         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1073                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1074                         arrange(m, d);
1075
1076         return MSG_SUCCESS;
1077 }
1078
1079 int get_setting(coordinates_t loc, char *name, FILE* rsp)
1080 {
1081         if (streq("split_ratio", name))
1082                 fprintf(rsp, "%lf", split_ratio);
1083         else if (streq("window_gap", name))
1084                 if (loc.desktop != NULL)
1085                         fprintf(rsp, "%i", loc.desktop->window_gap);
1086                 else
1087                         fprintf(rsp, "%i", window_gap);
1088         else if (streq("border_width", name))
1089                 if (loc.node != NULL)
1090                         fprintf(rsp, "%u", loc.node->client->border_width);
1091                 else if (loc.desktop != NULL)
1092                         fprintf(rsp, "%u", loc.desktop->border_width);
1093                 else
1094                         fprintf(rsp, "%u", border_width);
1095         else if (streq("external_rules_command", name))
1096                 fprintf(rsp, "%s", external_rules_command);
1097         else if (streq("status_prefix", name))
1098                 fprintf(rsp, "%s", status_prefix);
1099         else if (streq("initial_polarity", name))
1100                 fprintf(rsp, "%s", initial_polarity == FIRST_CHILD ? "first_child" : "second_child");
1101 #define MONDESKGET(k) \
1102         else if (streq(#k, name)) \
1103                 if (loc.desktop != NULL) \
1104                         fprintf(rsp, "%i", loc.desktop->k); \
1105                 else if (loc.monitor != NULL) \
1106                         fprintf(rsp, "%i", loc.monitor->k); \
1107                 else \
1108                         return MSG_FAILURE;
1109         MONDESKGET(top_padding)
1110         MONDESKGET(right_padding)
1111         MONDESKGET(bottom_padding)
1112         MONDESKGET(left_padding)
1113 #undef DESKGET
1114 #define GETCOLOR(s) \
1115         else if (streq(#s, name)) \
1116                 fprintf(rsp, "%s", s);
1117         GETCOLOR(focused_border_color)
1118         GETCOLOR(active_border_color)
1119         GETCOLOR(normal_border_color)
1120         GETCOLOR(presel_border_color)
1121         GETCOLOR(focused_locked_border_color)
1122         GETCOLOR(active_locked_border_color)
1123         GETCOLOR(normal_locked_border_color)
1124         GETCOLOR(focused_sticky_border_color)
1125         GETCOLOR(active_sticky_border_color)
1126         GETCOLOR(normal_sticky_border_color)
1127         GETCOLOR(urgent_border_color)
1128 #undef GETCOLOR
1129 #define GETBOOL(s) \
1130         else if (streq(#s, name)) \
1131                 fprintf(rsp, "%s", BOOLSTR(s));
1132         GETBOOL(borderless_monocle)
1133         GETBOOL(gapless_monocle)
1134         GETBOOL(leaf_monocle)
1135         GETBOOL(focus_follows_pointer)
1136         GETBOOL(pointer_follows_focus)
1137         GETBOOL(pointer_follows_monitor)
1138         GETBOOL(apply_floating_atom)
1139         GETBOOL(auto_alternate)
1140         GETBOOL(auto_cancel)
1141         GETBOOL(history_aware_focus)
1142         GETBOOL(focus_by_distance)
1143         GETBOOL(ignore_ewmh_focus)
1144         GETBOOL(center_pseudo_tiled)
1145         GETBOOL(remove_disabled_monitors)
1146         GETBOOL(remove_unplugged_monitors)
1147         GETBOOL(merge_overlapping_monitors)
1148 #undef GETBOOL
1149         else
1150                 return MSG_FAILURE;
1151         return MSG_SUCCESS;
1152 }
1153
1154 bool parse_subscriber_mask(char *s, subscriber_mask_t *mask)
1155 {
1156         if (streq("all", s)) {
1157                 *mask = SBSC_MASK_ALL;
1158         } else if (streq("window", s)) {
1159                 *mask = SBSC_MASK_WINDOW;
1160         } else if (streq("desktop", s)) {
1161                 *mask = SBSC_MASK_DESKTOP;
1162         } else if (streq("monitor", s)) {
1163                 *mask = SBSC_MASK_MONITOR;
1164         } else if (streq("window_manage", s)) {
1165                 *mask = SBSC_MASK_WINDOW_MANAGE;
1166         } else if (streq("window_unmanage", s)) {
1167                 *mask = SBSC_MASK_WINDOW_UNMANAGE;
1168         } else if (streq("window_swap", s)) {
1169                 *mask = SBSC_MASK_WINDOW_SWAP;
1170         } else if (streq("window_transfer", s)) {
1171                 *mask = SBSC_MASK_WINDOW_TRANSFER;
1172         } else if (streq("window_focus", s)) {
1173                 *mask = SBSC_MASK_WINDOW_FOCUS;
1174         } else if (streq("window_resize", s)) {
1175                 *mask = SBSC_MASK_WINDOW_RESIZE;
1176         } else if (streq("window_move", s)) {
1177                 *mask = SBSC_MASK_WINDOW_MOVE;
1178         } else if (streq("window_state", s)) {
1179                 *mask = SBSC_MASK_WINDOW_STATE;
1180         } else if (streq("desktop_add", s)) {
1181                 *mask = SBSC_MASK_DESKTOP_ADD;
1182         } else if (streq("desktop_rename", s)) {
1183                 *mask = SBSC_MASK_DESKTOP_RENAME;
1184         } else if (streq("desktop_remove", s)) {
1185                 *mask = SBSC_MASK_DESKTOP_REMOVE;
1186         } else if (streq("desktop_swap", s)) {
1187                 *mask = SBSC_MASK_DESKTOP_SWAP;
1188         } else if (streq("desktop_transfer", s)) {
1189                 *mask = SBSC_MASK_DESKTOP_TRANSFER;
1190         } else if (streq("desktop_focus", s)) {
1191                 *mask = SBSC_MASK_DESKTOP_FOCUS;
1192         } else if (streq("desktop_layout", s)) {
1193                 *mask = SBSC_MASK_DESKTOP_LAYOUT;
1194         } else if (streq("desktop_state", s)) {
1195                 *mask = SBSC_MASK_DESKTOP_STATE;
1196         } else if (streq("monitor_add", s)) {
1197                 *mask = SBSC_MASK_MONITOR_ADD;
1198         } else if (streq("monitor_rename", s)) {
1199                 *mask = SBSC_MASK_MONITOR_RENAME;
1200         } else if (streq("monitor_remove", s)) {
1201                 *mask = SBSC_MASK_MONITOR_REMOVE;
1202         } else if (streq("monitor_focus", s)) {
1203                 *mask = SBSC_MASK_MONITOR_FOCUS;
1204         } else if (streq("monitor_resize", s)) {
1205                 *mask = SBSC_MASK_MONITOR_RESIZE;
1206         } else if (streq("report", s)) {
1207                 *mask = SBSC_MASK_REPORT;
1208         } else {
1209                 return false;
1210         }
1211         return true;
1212 }
1213
1214 bool parse_bool(char *value, bool *b)
1215 {
1216         if (streq("true", value) || streq("on", value)) {
1217                 *b = true;
1218                 return true;
1219         } else if (streq("false", value) || streq("off", value)) {
1220                 *b = false;
1221                 return true;
1222         }
1223         return false;
1224 }
1225
1226 bool parse_layout(char *s, layout_t *l)
1227 {
1228         if (streq("monocle", s)) {
1229                 *l = LAYOUT_MONOCLE;
1230                 return true;
1231         } else if (streq("tiled", s)) {
1232                 *l = LAYOUT_TILED;
1233                 return true;
1234         }
1235         return false;
1236 }
1237
1238 bool parse_direction(char *s, direction_t *d)
1239 {
1240         if (streq("right", s)) {
1241                 *d = DIR_RIGHT;
1242                 return true;
1243         } else if (streq("down", s)) {
1244                 *d = DIR_DOWN;
1245                 return true;
1246         } else if (streq("left", s)) {
1247                 *d = DIR_LEFT;
1248                 return true;
1249         } else if (streq("up", s)) {
1250                 *d = DIR_UP;
1251                 return true;
1252         }
1253         return false;
1254 }
1255
1256 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1257 {
1258         if (streq("next", s)) {
1259                 *d = CYCLE_NEXT;
1260                 return true;
1261         } else if (streq("prev", s)) {
1262                 *d = CYCLE_PREV;
1263                 return true;
1264         }
1265         return false;
1266 }
1267
1268 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1269 {
1270         if (streq("forward", s)) {
1271                 *d = CIRCULATE_FORWARD;
1272                 return true;
1273         } else if (streq("backward", s)) {
1274                 *d = CIRCULATE_BACKWARD;
1275                 return true;
1276         }
1277         return false;
1278 }
1279
1280 bool parse_history_direction(char *s, history_dir_t *d)
1281 {
1282         if (streq("older", s)) {
1283                 *d = HISTORY_OLDER;
1284                 return true;
1285         } else if (streq("newer", s)) {
1286                 *d = HISTORY_NEWER;
1287                 return true;
1288         }
1289         return false;
1290 }
1291
1292
1293 bool parse_flip(char *s, flip_t *f)
1294 {
1295         if (streq("horizontal", s)) {
1296                 *f = FLIP_HORIZONTAL;
1297                 return true;
1298         } else if (streq("vertical", s)) {
1299                 *f = FLIP_VERTICAL;
1300                 return true;
1301         }
1302         return false;
1303 }
1304
1305 bool parse_pointer_action(char *s, pointer_action_t *a)
1306 {
1307         if (streq("move", s)) {
1308                 *a = ACTION_MOVE;
1309                 return true;
1310         } else if (streq("resize_corner", s)) {
1311                 *a = ACTION_RESIZE_CORNER;
1312                 return true;
1313         } else if (streq("resize_side", s)) {
1314                 *a = ACTION_RESIZE_SIDE;
1315                 return true;
1316         } else if (streq("focus", s)) {
1317                 *a = ACTION_FOCUS;
1318                 return true;
1319         }
1320         return false;
1321 }
1322
1323 bool parse_child_polarity(char *s, child_polarity_t *p)
1324 {
1325         if (streq("first_child", s)) {
1326                 *p = FIRST_CHILD;
1327                 return true;
1328         } else if (streq("second_child", s)) {
1329                 *p = SECOND_CHILD;
1330                 return true;
1331         }
1332         return false;
1333 }
1334
1335 bool parse_degree(char *s, int *d)
1336 {
1337         int i = atoi(s);
1338         while (i < 0)
1339                 i += 360;
1340         while (i > 359)
1341                 i -= 360;
1342         if ((i % 90) != 0) {
1343                 return false;
1344         } else {
1345                 *d = i;
1346                 return true;
1347         }
1348 }
1349
1350 bool parse_window_id(char *s, long int *i)
1351 {
1352         char *end;
1353         errno = 0;
1354         long int ret = strtol(s, &end, 0);
1355         if (errno != 0 || *end != '\0')
1356                 return false;
1357         else
1358                 *i = ret;
1359         return true;
1360 }
1361
1362 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1363 {
1364         *key = strtok(s, EQL_TOK);
1365         char *v = strtok(NULL, EQL_TOK);
1366         if (v == NULL) {
1367                 *state = ALTER_TOGGLE;
1368                 return true;
1369         } else {
1370                 if (parse_bool(v, value)) {
1371                         *state = ALTER_SET;
1372                         return true;
1373                 } else {
1374                         return false;
1375                 }
1376         }
1377         return false;
1378 }
1379
1380 bool parse_index(char *s, int *i)
1381 {
1382         int idx;
1383         if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1384                 return false;
1385         *i = idx;
1386         return true;
1387 }