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