]> git.lizzy.rs Git - bspwm.git/blob - messages.c
20e18fbe98b8844db588aa13beb19c056ece7f4a
[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\n", 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                                 snprintf(d->name, sizeof(d->name), "%s", *args);
558                                 initialize_desktop(d);
559                                 arrange(trg.monitor, d);
560                                 d = d->next;
561                                 num--, args++;
562                         }
563                         put_status(SBSC_MASK_REPORT);
564                         while (num > 0) {
565                                 add_desktop(trg.monitor, make_desktop(*args));
566                                 num--, args++;
567                         }
568                         while (d != NULL) {
569                                 desktop_t *next = d->next;
570                                 if (d == mon->desk)
571                                         focus_node(trg.monitor, d->prev, d->prev->focus);
572                                 merge_desktops(trg.monitor, d, mon, mon->desk);
573                                 remove_desktop(trg.monitor, d);
574                                 d = next;
575                         }
576                 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
577                         num--, args++;
578                         if (num < 1)
579                                 return MSG_SYNTAX;
580                         while (num > 0) {
581                                 add_desktop(trg.monitor, make_desktop(*args));
582                                 num--, args++;
583                         }
584                 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
585                         num--, args++;
586                         if (num < 1)
587                                 return MSG_SYNTAX;
588                         while (num > 0) {
589                                 coordinates_t dst;
590                                 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
591                                         remove_desktop(dst.monitor, dst.desktop);
592                                         show_desktop(dst.monitor->desk);
593                                 }
594                                 num--, args++;
595                         }
596                 } else if (streq("-o", *args) || streq("--order-desktops", *args)) {
597                         num--, args++;
598                         if (num < 1)
599                                 return MSG_SYNTAX;
600                         desktop_t *d = trg.monitor->desk_head;
601                         while (d != NULL && num > 0) {
602                                 desktop_t *next = d->next;
603                                 coordinates_t dst;
604                                 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
605                                         swap_desktops(trg.monitor, d, dst.monitor, dst.desktop);
606                                         if (next == dst.desktop)
607                                                 next = d;
608                                 }
609                                 d = next;
610                                 num--, args++;
611                         }
612                 } else if (streq("-n", *args) || streq("--rename", *args)) {
613                         num--, args++;
614                         if (num < 1)
615                                 return MSG_SYNTAX;
616                         put_status(SBSC_MASK_MONITOR_RENAME, "monitor_rename %s %s\n", trg.monitor->name, *args);
617                         snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
618                         put_status(SBSC_MASK_REPORT);
619                 } else if (streq("-s", *args) || streq("--swap", *args)) {
620                         num--, args++;
621                         if (num < 1)
622                                 return MSG_SYNTAX;
623                         coordinates_t dst;
624                         if (monitor_from_desc(*args, &trg, &dst))
625                                 swap_monitors(trg.monitor, dst.monitor);
626                         else
627                                 return MSG_FAILURE;
628                 } else {
629                         return MSG_SYNTAX;
630                 }
631                 num--, args++;
632         }
633
634         return MSG_SUCCESS;
635 }
636
637 int cmd_query(char **args, int num, FILE *rsp)
638 {
639         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
640         coordinates_t trg = {NULL, NULL, NULL};
641         domain_t dom = DOMAIN_TREE;
642         int d = 0, t = 0;
643
644         while (num > 0) {
645                 if (streq("-T", *args) || streq("--tree", *args)) {
646                         dom = DOMAIN_TREE, d++;
647                 } else if (streq("-M", *args) || streq("--monitors", *args)) {
648                         dom = DOMAIN_MONITOR, d++;
649                 } else if (streq("-D", *args) || streq("--desktops", *args)) {
650                         dom = DOMAIN_DESKTOP, d++;
651                 } else if (streq("-W", *args) || streq("--windows", *args)) {
652                         dom = DOMAIN_WINDOW, d++;
653                 } else if (streq("-H", *args) || streq("--history", *args)) {
654                         dom = DOMAIN_HISTORY, d++;
655                 } else if (streq("-S", *args) || streq("--stack", *args)) {
656                         dom = DOMAIN_STACK, d++;
657                 } else if (streq("-m", *args) || streq("--monitor", *args)) {
658                         trg.monitor = ref.monitor;
659                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
660                                 num--, args++;
661                                 if (!monitor_from_desc(*args, &ref, &trg))
662                                         return MSG_FAILURE;
663                         }
664                         t++;
665                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
666                         trg.monitor = ref.monitor;
667                         trg.desktop = ref.desktop;
668                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
669                                 num--, args++;
670                                 if (!desktop_from_desc(*args, &ref, &trg))
671                                         return MSG_FAILURE;
672                         }
673                         t++;
674                 } else if (streq("-w", *args) || streq("--window", *args)) {
675                         trg = ref;
676                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
677                                 num--, args++;
678                                 if (!node_from_desc(*args, &ref, &trg))
679                                         return MSG_FAILURE;
680                         }
681                         t++;
682                 } else {
683                         return MSG_SYNTAX;
684                 }
685                 num--, args++;
686         }
687
688         if (d != 1 || t > 1)
689                 return MSG_SYNTAX;
690
691         if (dom == DOMAIN_HISTORY)
692                 query_history(trg, rsp);
693         else if (dom == DOMAIN_STACK)
694                 query_stack(rsp);
695         else if (dom == DOMAIN_WINDOW)
696                 query_windows(trg, rsp);
697         else
698                 query_monitors(trg, dom, rsp);
699
700         return MSG_SUCCESS;
701 }
702
703 int cmd_rule(char **args, int num, FILE *rsp)
704 {
705         if (num < 1)
706                 return MSG_SYNTAX;
707         while (num > 0) {
708                 if (streq("-a", *args) || streq("--add", *args)) {
709                         num--, args++;
710                         if (num < 2)
711                                 return MSG_SYNTAX;
712                         rule_t *rule = make_rule();
713                         snprintf(rule->cause, sizeof(rule->cause), "%s", *args);
714                         num--, args++;
715                         size_t i = 0;
716                         while (num > 0) {
717                                 if (streq("-o", *args) || streq("--one-shot", *args)) {
718                                         rule->one_shot = true;
719                                 } else {
720                                         for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++)
721                                                 rule->effect[i] = (*args)[j];
722                                         if (num > 1 && i < sizeof(rule->effect))
723                                                 rule->effect[i++] = ' ';
724                                 }
725                                 num--, args++;
726                         }
727                         rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
728                         add_rule(rule);
729                 } else if (streq("-r", *args) || streq("--remove", *args)) {
730                         num--, args++;
731                         if (num < 1)
732                                 return MSG_SYNTAX;
733                         int idx;
734                         while (num > 0) {
735                                 if (parse_index(*args, &idx))
736                                         remove_rule_by_index(idx - 1);
737                                 else if (streq("tail", *args))
738                                         remove_rule(rule_tail);
739                                 else if (streq("head", *args))
740                                         remove_rule(rule_head);
741                                 else
742                                         remove_rule_by_cause(*args);
743                                 num--, args++;
744                         }
745                 } else if (streq("-l", *args) || streq("--list", *args)) {
746                         num--, args++;
747                         list_rules(num > 0 ? *args : NULL, rsp);
748                 } else {
749                         return MSG_SYNTAX;
750                 }
751                 num--, args++;
752         }
753
754         return MSG_SUCCESS;
755 }
756
757 int cmd_pointer(char **args, int num)
758 {
759         if (num < 1)
760                 return MSG_SYNTAX;
761         while (num > 0) {
762                 if (streq("-t", *args) || streq("--track", *args)) {
763                         num--, args++;
764                         if (num < 2)
765                                 return MSG_SYNTAX;
766                         int x, y;
767                         if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
768                                 track_pointer(x, y);
769                         else
770                                 return MSG_FAILURE;
771                         num--, args++;
772                 } else if (streq("-g", *args) || streq("--grab", *args)) {
773                         num--, args++;
774                         if (num < 1)
775                                 return MSG_SYNTAX;
776                         pointer_action_t pac;
777                         if (parse_pointer_action(*args, &pac))
778                                 grab_pointer(pac);
779                         else
780                                 return MSG_FAILURE;
781                 } else if (streq("-u", *args) || streq("--ungrab", *args)) {
782                         ungrab_pointer();
783                 } else {
784                         return MSG_SYNTAX;
785                 }
786                 num--, args++;
787         }
788
789         return MSG_SUCCESS;
790 }
791
792 int cmd_restore(char **args, int num)
793 {
794         if (num < 1)
795                 return MSG_SYNTAX;
796         while (num > 0) {
797                 if (streq("-T", *args) || streq("--tree", *args)) {
798                         num--, args++;
799                         if (num < 1)
800                                 return MSG_SYNTAX;
801                         restore_tree(*args);
802                 } else if (streq("-H", *args) || streq("--history", *args)) {
803                         num--, args++;
804                         if (num < 1)
805                                 return MSG_SYNTAX;
806                         restore_history(*args);
807                 } else if (streq("-S", *args) || streq("--stack", *args)) {
808                         num--, args++;
809                         if (num < 1)
810                                 return MSG_SYNTAX;
811                         restore_stack(*args);
812                 } else {
813                         return MSG_SYNTAX;
814                 }
815                 num--, args++;
816         }
817
818         return MSG_SUCCESS;
819 }
820
821 int cmd_control(char **args, int num, FILE *rsp)
822 {
823         if (num < 1)
824                 return MSG_SYNTAX;
825         while (num > 0) {
826                 if (streq("--adopt-orphans", *args)) {
827                         adopt_orphans();
828                 } else if (streq("--toggle-visibility", *args)) {
829                         toggle_visibility();
830                 } else if (streq("--subscribe", *args)) {
831                         num--, args++;
832                         int field = 0;
833                         if (num < 1) {
834                                 field = SBSC_MASK_REPORT;
835                         } else {
836                                 subscriber_mask_t mask;
837                                 while (num > 0) {
838                                         if (parse_subscriber_mask(*args, &mask)) {
839                                                 field |= mask;
840                                         } else {
841                                                 return MSG_SYNTAX;
842                                         }
843                                         num--, args++;
844                                 }
845                         }
846                         add_subscriber(rsp, field);
847                         return MSG_SUBSCRIBE;
848                 } else if (streq("--get-status", *args)) {
849                         print_report(rsp);
850                 } else if (streq("--record-history", *args)) {
851                         num--, args++;
852                         if (num < 1)
853                                 return MSG_SYNTAX;
854                         bool b;
855                         if (parse_bool(*args, &b))
856                                 record_history = b;
857                         else
858                                 return MSG_SYNTAX;
859                 } else {
860                         return MSG_SYNTAX;
861                 }
862                 num--, args++;
863         }
864
865         return MSG_SUCCESS;
866 }
867
868 int cmd_config(char **args, int num, FILE *rsp)
869 {
870         if (num < 1)
871                 return MSG_SYNTAX;
872         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
873         coordinates_t trg = {NULL, NULL, NULL};
874         if ((*args)[0] == OPT_CHR) {
875                 if (streq("-m", *args) || streq("--monitor", *args)) {
876                         num--, args++;
877                         if (num < 1)
878                                 return MSG_SYNTAX;
879                         if (!monitor_from_desc(*args, &ref, &trg))
880                                 return MSG_FAILURE;
881                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
882                         num--, args++;
883                         if (num < 1)
884                                 return MSG_SYNTAX;
885                         if (!desktop_from_desc(*args, &ref, &trg))
886                                 return MSG_FAILURE;
887                 } else if (streq("-w", *args) || streq("--window", *args)) {
888                         num--, args++;
889                         if (num < 1)
890                                 return MSG_SYNTAX;
891                         if (!node_from_desc(*args, &ref, &trg))
892                                 return MSG_FAILURE;
893                 } else {
894                         return MSG_SYNTAX;
895                 }
896                 num--, args++;
897         }
898         if (num == 2)
899                 return set_setting(trg, *args, *(args + 1));
900         else if (num == 1)
901                 return get_setting(trg, *args, rsp);
902         else
903                 return MSG_SYNTAX;
904 }
905
906 int cmd_quit(char **args, int num)
907 {
908         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
909                 return MSG_FAILURE;
910         running = false;
911         return MSG_SUCCESS;
912 }
913
914 int set_setting(coordinates_t loc, char *name, char *value)
915 {
916 #define DESKWINDEFSET(k, v) \
917                 if (loc.node != NULL) \
918                         loc.node->client->k = v; \
919                 else if (loc.desktop != NULL) \
920                         loc.desktop->k = v; \
921                 else if (loc.monitor != NULL) \
922                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
923                                 d->k = v; \
924                 else \
925                         k = v;
926         if (streq("border_width", name)) {
927                 unsigned int bw;
928                 if (sscanf(value, "%u", &bw) != 1)
929                         return MSG_FAILURE;
930                 DESKWINDEFSET(border_width, bw)
931 #undef DESKWINDEFSET
932 #define DESKDEFSET(k, v) \
933                 if (loc.desktop != NULL) \
934                         loc.desktop->k = v; \
935                 else if (loc.monitor != NULL) \
936                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
937                                 d->k = v; \
938                 else \
939                         k = v;
940         } else if (streq("window_gap", name)) {
941                 int wg;
942                 if (sscanf(value, "%i", &wg) != 1)
943                         return MSG_FAILURE;
944                 DESKDEFSET(window_gap, wg)
945 #undef DESKDEFSET
946 #define MONDESKSET(k, v) \
947                 if (loc.desktop != NULL) \
948                         loc.desktop->k = v; \
949                 else if (loc.monitor != NULL) \
950                         loc.monitor->k = v; \
951                 else \
952                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
953                                 m->k = v;
954         } else if (streq("top_padding", name)) {
955                 int tp;
956                 if (sscanf(value, "%i", &tp) != 1)
957                         return MSG_FAILURE;
958                 MONDESKSET(top_padding, tp)
959         } else if (streq("right_padding", name)) {
960                 int rp;
961                 if (sscanf(value, "%i", &rp) != 1)
962                         return MSG_FAILURE;
963                 MONDESKSET(right_padding, rp)
964         } else if (streq("bottom_padding", name)) {
965                 int bp;
966                 if (sscanf(value, "%i", &bp) != 1)
967                         return MSG_FAILURE;
968                 MONDESKSET(bottom_padding, bp)
969         } else if (streq("left_padding", name)) {
970                 int lp;
971                 if (sscanf(value, "%i", &lp) != 1)
972                         return MSG_FAILURE;
973                 MONDESKSET(left_padding, lp)
974 #undef MONDESKSET
975 #define SETSTR(s) \
976         } else if (streq(#s, name)) { \
977                 return snprintf(s, sizeof(s), "%s", value) >= 0;
978         SETSTR(external_rules_command)
979         SETSTR(status_prefix)
980 #undef SETSTR
981         } else if (streq("split_ratio", name)) {
982                 double r;
983                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
984                         split_ratio = r;
985                 else
986                         return MSG_FAILURE;
987                 return MSG_SUCCESS;
988 #define SETCOLOR(s) \
989         } else if (streq(#s, name)) { \
990                 snprintf(s, sizeof(s), "%s", value);
991         SETCOLOR(focused_border_color)
992         SETCOLOR(active_border_color)
993         SETCOLOR(normal_border_color)
994         SETCOLOR(presel_border_color)
995         SETCOLOR(focused_locked_border_color)
996         SETCOLOR(active_locked_border_color)
997         SETCOLOR(normal_locked_border_color)
998         SETCOLOR(focused_sticky_border_color)
999         SETCOLOR(active_sticky_border_color)
1000         SETCOLOR(normal_sticky_border_color)
1001         SETCOLOR(focused_private_border_color)
1002         SETCOLOR(active_private_border_color)
1003         SETCOLOR(normal_private_border_color)
1004         SETCOLOR(urgent_border_color)
1005 #undef SETCOLOR
1006         } else if (streq("initial_polarity", name)) {
1007                 child_polarity_t p;
1008                 if (parse_child_polarity(value, &p)) {
1009                         initial_polarity = p;
1010                 } else {
1011                         return MSG_FAILURE;
1012                 }
1013         } else if (streq("focus_follows_pointer", name)) {
1014                 bool b;
1015                 if (parse_bool(value, &b) && b != focus_follows_pointer) {
1016                         focus_follows_pointer = b;
1017                         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1018                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1019                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
1020                                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
1021                                                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
1022                                         }
1023                         if (focus_follows_pointer) {
1024                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
1025                                         window_show(m->root);
1026                                 enable_motion_recorder();
1027                         } else {
1028                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
1029                                         window_hide(m->root);
1030                                 disable_motion_recorder();
1031                         }
1032                         return MSG_SUCCESS;
1033                 } else {
1034                         return MSG_FAILURE;
1035                 }
1036 #define SETBOOL(s) \
1037         } else if (streq(#s, name)) { \
1038                 if (!parse_bool(value, &s)) \
1039                         return MSG_FAILURE;
1040                 SETBOOL(borderless_monocle)
1041                 SETBOOL(gapless_monocle)
1042                 SETBOOL(pointer_follows_focus)
1043                 SETBOOL(pointer_follows_monitor)
1044                 SETBOOL(apply_floating_atom)
1045                 SETBOOL(auto_alternate)
1046                 SETBOOL(auto_cancel)
1047                 SETBOOL(history_aware_focus)
1048                 SETBOOL(focus_by_distance)
1049                 SETBOOL(ignore_ewmh_focus)
1050                 SETBOOL(center_pseudo_tiled)
1051 #undef SETBOOL
1052 #define SETMONBOOL(s) \
1053         } else if (streq(#s, name)) { \
1054                 if (!parse_bool(value, &s)) \
1055                         return MSG_FAILURE; \
1056                 if (s) \
1057                         update_monitors();
1058                 SETMONBOOL(remove_disabled_monitors)
1059                 SETMONBOOL(remove_unplugged_monitors)
1060                 SETMONBOOL(merge_overlapping_monitors)
1061 #undef SETMONBOOL
1062         } else {
1063                 return MSG_FAILURE;
1064         }
1065
1066         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1067                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1068                         arrange(m, d);
1069
1070         return MSG_SUCCESS;
1071 }
1072
1073 int get_setting(coordinates_t loc, char *name, FILE* rsp)
1074 {
1075         if (streq("split_ratio", name))
1076                 fprintf(rsp, "%lf", split_ratio);
1077         else if (streq("window_gap", name))
1078                 if (loc.desktop != NULL)
1079                         fprintf(rsp, "%i", loc.desktop->window_gap);
1080                 else
1081                         fprintf(rsp, "%i", window_gap);
1082         else if (streq("border_width", name))
1083                 if (loc.node != NULL)
1084                         fprintf(rsp, "%u", loc.node->client->border_width);
1085                 else if (loc.desktop != NULL)
1086                         fprintf(rsp, "%u", loc.desktop->border_width);
1087                 else
1088                         fprintf(rsp, "%u", border_width);
1089         else if (streq("external_rules_command", name))
1090                 fprintf(rsp, "%s", external_rules_command);
1091         else if (streq("status_prefix", name))
1092                 fprintf(rsp, "%s", status_prefix);
1093         else if (streq("initial_polarity", name))
1094                 fprintf(rsp, "%s", initial_polarity == FIRST_CHILD ? "first_child" : "second_child");
1095 #define MONDESKGET(k) \
1096         else if (streq(#k, name)) \
1097                 if (loc.desktop != NULL) \
1098                         fprintf(rsp, "%i", loc.desktop->k); \
1099                 else if (loc.monitor != NULL) \
1100                         fprintf(rsp, "%i", loc.monitor->k); \
1101                 else \
1102                         return MSG_FAILURE;
1103         MONDESKGET(top_padding)
1104         MONDESKGET(right_padding)
1105         MONDESKGET(bottom_padding)
1106         MONDESKGET(left_padding)
1107 #undef DESKGET
1108 #define GETCOLOR(s) \
1109         else if (streq(#s, name)) \
1110                 fprintf(rsp, "%s", s);
1111         GETCOLOR(focused_border_color)
1112         GETCOLOR(active_border_color)
1113         GETCOLOR(normal_border_color)
1114         GETCOLOR(presel_border_color)
1115         GETCOLOR(focused_locked_border_color)
1116         GETCOLOR(active_locked_border_color)
1117         GETCOLOR(normal_locked_border_color)
1118         GETCOLOR(focused_sticky_border_color)
1119         GETCOLOR(active_sticky_border_color)
1120         GETCOLOR(normal_sticky_border_color)
1121         GETCOLOR(urgent_border_color)
1122 #undef GETCOLOR
1123 #define GETBOOL(s) \
1124         else if (streq(#s, name)) \
1125                 fprintf(rsp, "%s", BOOLSTR(s));
1126         GETBOOL(borderless_monocle)
1127         GETBOOL(gapless_monocle)
1128         GETBOOL(focus_follows_pointer)
1129         GETBOOL(pointer_follows_focus)
1130         GETBOOL(pointer_follows_monitor)
1131         GETBOOL(apply_floating_atom)
1132         GETBOOL(auto_alternate)
1133         GETBOOL(auto_cancel)
1134         GETBOOL(history_aware_focus)
1135         GETBOOL(focus_by_distance)
1136         GETBOOL(ignore_ewmh_focus)
1137         GETBOOL(center_pseudo_tiled)
1138         GETBOOL(remove_disabled_monitors)
1139         GETBOOL(remove_unplugged_monitors)
1140         GETBOOL(merge_overlapping_monitors)
1141 #undef GETBOOL
1142         else
1143                 return MSG_FAILURE;
1144         return MSG_SUCCESS;
1145 }
1146
1147 bool parse_subscriber_mask(char *s, subscriber_mask_t *mask)
1148 {
1149         if (streq("all", s)) {
1150                 *mask = SBSC_MASK_ALL;
1151         } else if (streq("window", s)) {
1152                 *mask = SBSC_MASK_WINDOW;
1153         } else if (streq("desktop", s)) {
1154                 *mask = SBSC_MASK_DESKTOP;
1155         } else if (streq("monitor", s)) {
1156                 *mask = SBSC_MASK_MONITOR;
1157         } else if (streq("window_manage", s)) {
1158                 *mask = SBSC_MASK_WINDOW_MANAGE;
1159         } else if (streq("window_unmanage", s)) {
1160                 *mask = SBSC_MASK_WINDOW_UNMANAGE;
1161         } else if (streq("window_urgent", s)) {
1162                 *mask = SBSC_MASK_WINDOW_URGENT;
1163         } else if (streq("window_fullscreen", s)) {
1164                 *mask = SBSC_MASK_WINDOW_FULLSCREEN;
1165         } else if (streq("desktop_add", s)) {
1166                 *mask = SBSC_MASK_DESKTOP_ADD;
1167         } else if (streq("desktop_rename", s)) {
1168                 *mask = SBSC_MASK_DESKTOP_RENAME;
1169         } else if (streq("desktop_remove", s)) {
1170                 *mask = SBSC_MASK_DESKTOP_REMOVE;
1171         } else if (streq("monitor_add", s)) {
1172                 *mask = SBSC_MASK_MONITOR_ADD;
1173         } else if (streq("monitor_rename", s)) {
1174                 *mask = SBSC_MASK_MONITOR_RENAME;
1175         } else if (streq("monitor_remove", s)) {
1176                 *mask = SBSC_MASK_MONITOR_REMOVE;
1177         } else if (streq("report", s)) {
1178                 *mask = SBSC_MASK_REPORT;
1179         } else {
1180                 return false;
1181         }
1182         return true;
1183 }
1184
1185 bool parse_bool(char *value, bool *b)
1186 {
1187         if (streq("true", value) || streq("on", value)) {
1188                 *b = true;
1189                 return true;
1190         } else if (streq("false", value) || streq("off", value)) {
1191                 *b = false;
1192                 return true;
1193         }
1194         return false;
1195 }
1196
1197 bool parse_layout(char *s, layout_t *l)
1198 {
1199         if (streq("monocle", s)) {
1200                 *l = LAYOUT_MONOCLE;
1201                 return true;
1202         } else if (streq("tiled", s)) {
1203                 *l = LAYOUT_TILED;
1204                 return true;
1205         }
1206         return false;
1207 }
1208
1209 bool parse_direction(char *s, direction_t *d)
1210 {
1211         if (streq("right", s)) {
1212                 *d = DIR_RIGHT;
1213                 return true;
1214         } else if (streq("down", s)) {
1215                 *d = DIR_DOWN;
1216                 return true;
1217         } else if (streq("left", s)) {
1218                 *d = DIR_LEFT;
1219                 return true;
1220         } else if (streq("up", s)) {
1221                 *d = DIR_UP;
1222                 return true;
1223         }
1224         return false;
1225 }
1226
1227 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1228 {
1229         if (streq("next", s)) {
1230                 *d = CYCLE_NEXT;
1231                 return true;
1232         } else if (streq("prev", s)) {
1233                 *d = CYCLE_PREV;
1234                 return true;
1235         }
1236         return false;
1237 }
1238
1239 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1240 {
1241         if (streq("forward", s)) {
1242                 *d = CIRCULATE_FORWARD;
1243                 return true;
1244         } else if (streq("backward", s)) {
1245                 *d = CIRCULATE_BACKWARD;
1246                 return true;
1247         }
1248         return false;
1249 }
1250
1251 bool parse_history_direction(char *s, history_dir_t *d)
1252 {
1253         if (streq("older", s)) {
1254                 *d = HISTORY_OLDER;
1255                 return true;
1256         } else if (streq("newer", s)) {
1257                 *d = HISTORY_NEWER;
1258                 return true;
1259         }
1260         return false;
1261 }
1262
1263
1264 bool parse_flip(char *s, flip_t *f)
1265 {
1266         if (streq("horizontal", s)) {
1267                 *f = FLIP_HORIZONTAL;
1268                 return true;
1269         } else if (streq("vertical", s)) {
1270                 *f = FLIP_VERTICAL;
1271                 return true;
1272         }
1273         return false;
1274 }
1275
1276 bool parse_pointer_action(char *s, pointer_action_t *a)
1277 {
1278         if (streq("move", s)) {
1279                 *a = ACTION_MOVE;
1280                 return true;
1281         } else if (streq("resize_corner", s)) {
1282                 *a = ACTION_RESIZE_CORNER;
1283                 return true;
1284         } else if (streq("resize_side", s)) {
1285                 *a = ACTION_RESIZE_SIDE;
1286                 return true;
1287         } else if (streq("focus", s)) {
1288                 *a = ACTION_FOCUS;
1289                 return true;
1290         }
1291         return false;
1292 }
1293
1294 bool parse_child_polarity(char *s, child_polarity_t *p)
1295 {
1296         if (streq("first_child", s)) {
1297                 *p = FIRST_CHILD;
1298                 return true;
1299         } else if (streq("second_child", s)) {
1300                 *p = SECOND_CHILD;
1301                 return true;
1302         }
1303         return false;
1304 }
1305
1306 bool parse_degree(char *s, int *d)
1307 {
1308         int i = atoi(s);
1309         while (i < 0)
1310                 i += 360;
1311         while (i > 359)
1312                 i -= 360;
1313         if ((i % 90) != 0) {
1314                 return false;
1315         } else {
1316                 *d = i;
1317                 return true;
1318         }
1319 }
1320
1321 bool parse_window_id(char *s, long int *i)
1322 {
1323         char *end;
1324         errno = 0;
1325         long int ret = strtol(s, &end, 0);
1326         if (errno != 0 || *end != '\0')
1327                 return false;
1328         else
1329                 *i = ret;
1330         return true;
1331 }
1332
1333 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1334 {
1335         *key = strtok(s, EQL_TOK);
1336         char *v = strtok(NULL, EQL_TOK);
1337         if (v == NULL) {
1338                 *state = ALTER_TOGGLE;
1339                 return true;
1340         } else {
1341                 if (parse_bool(v, value)) {
1342                         *state = ALTER_SET;
1343                         return true;
1344                 } else {
1345                         return false;
1346                 }
1347         }
1348         return false;
1349 }
1350
1351 bool parse_index(char *s, int *i)
1352 {
1353         int idx;
1354         if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1355                 return false;
1356         *i = idx;
1357         return true;
1358 }