]> git.lizzy.rs Git - bspwm.git/blob - messages.c
New setting: center_pseudo_tiled
[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("-l", *args) || streq("--layout", *args)) {
394                         num--, args++;
395                         if (num < 1)
396                                 return MSG_SYNTAX;
397                         layout_t lyt;
398                         cycle_dir_t cyc;
399                         if (parse_cycle_direction(*args, &cyc))
400                                 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
401                         else if (parse_layout(*args, &lyt))
402                                 change_layout(trg.monitor, trg.desktop, lyt);
403                         else
404                                 return MSG_FAILURE;
405                 } else if (streq("-n", *args) || streq("--rename", *args)) {
406                         num--, args++;
407                         if (num < 1)
408                                 return MSG_SYNTAX;
409                         snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
410                         ewmh_update_desktop_names();
411                         put_status();
412                 } else if (streq("-r", *args) || streq("--remove", *args)) {
413                         if (trg.desktop->root == NULL &&
414                             trg.monitor->desk_head != trg.monitor->desk_tail) {
415                                 remove_desktop(trg.monitor, trg.desktop);
416                                 show_desktop(trg.monitor->desk);
417                                 update_current();
418                                 return MSG_SUCCESS;
419                         } else {
420                                 return MSG_FAILURE;
421                         }
422                 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
423                         reset_mode(&trg);
424                 } else if (streq("-F", *args) || streq("--flip", *args)) {
425                         num--, args++;
426                         if (num < 1)
427                                 return MSG_SYNTAX;
428                         flip_t flp;
429                         if (parse_flip(*args, &flp)) {
430                                 flip_tree(trg.desktop->root, flp);
431                                 dirty = true;
432                         } else {
433                                 return MSG_FAILURE;
434                         }
435                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
436                         num--, args++;
437                         if (num < 1)
438                                 return MSG_SYNTAX;
439                         int deg;
440                         if (parse_degree(*args, &deg)) {
441                                 rotate_tree(trg.desktop->root, deg);
442                                 dirty = true;
443                         } else {
444                                 return MSG_FAILURE;
445                         }
446                 } else if (streq("-E", *args) || streq("--equalize", *args)) {
447                         equalize_tree(trg.desktop->root);
448                         dirty = true;
449                 } else if (streq("-B", *args) || streq("--balance", *args)) {
450                         balance_tree(trg.desktop->root);
451                         dirty = true;
452                 } else if (streq("-C", *args) || streq("--circulate", *args)) {
453                         num--, args++;
454                         if (num < 1)
455                                 return MSG_SYNTAX;
456                         circulate_dir_t cir;
457                         if (parse_circulate_direction(*args, &cir)) {
458                                 circulate_leaves(trg.monitor, trg.desktop, cir);
459                                 dirty = true;
460                         } else {
461                                 return MSG_FAILURE;
462                         }
463                 } else if (streq("-t", *args) || streq("--toggle", *args)) {
464                         num--, args++;
465                         if (num < 1)
466                                 return MSG_SYNTAX;
467                         char *key = strtok(*args, EQL_TOK);
468                         char *val = strtok(NULL, EQL_TOK);
469                         alter_state_t a;
470                         bool b;
471                         if (val == NULL) {
472                                 a = ALTER_TOGGLE;
473                         } else {
474                                 if (parse_bool(val, &b))
475                                         a = ALTER_SET;
476                                 else
477                                         return MSG_FAILURE;
478                         }
479                         if (streq("floating", key))
480                                 trg.desktop->floating = (a == ALTER_SET ? b : !trg.desktop->floating);
481                         else
482                                 return MSG_FAILURE;
483                 } else {
484                         return MSG_SYNTAX;
485                 }
486                 num--, args++;
487         }
488
489         if (dirty)
490                 arrange(trg.monitor, trg.desktop);
491
492         return MSG_SUCCESS;
493 }
494
495 int cmd_monitor(char **args, int num)
496 {
497         if (num < 1)
498                 return MSG_SYNTAX;
499
500         coordinates_t ref = {mon, NULL, NULL};
501         coordinates_t trg = ref;
502
503         if ((*args)[0] != OPT_CHR) {
504                 if (monitor_from_desc(*args, &ref, &trg))
505                         num--, args++;
506                 else
507                         return MSG_FAILURE;
508         }
509
510         while (num > 0) {
511                 if (streq("-f", *args) || streq("--focus", *args)) {
512                         coordinates_t dst = trg;
513                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
514                                 num--, args++;
515                                 if (!monitor_from_desc(*args, &trg, &dst))
516                                         return MSG_FAILURE;
517                         }
518                         if (auto_alternate && dst.monitor == mon) {
519                                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
520                                 history_find_monitor(HISTORY_OLDER, &trg, &dst, sel);
521                         }
522                         focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
523                 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
524                         num--, args++;
525                         if (num < 1)
526                                 return MSG_SYNTAX;
527                         desktop_t *d = trg.monitor->desk_head;
528                         while (num > 0 && d != NULL) {
529                                 snprintf(d->name, sizeof(d->name), "%s", *args);
530                                 initialize_desktop(d);
531                                 arrange(trg.monitor, d);
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                         num--, args++;
743                 } else if (streq("-g", *args) || streq("--grab", *args)) {
744                         num--, args++;
745                         if (num < 1)
746                                 return MSG_SYNTAX;
747                         pointer_action_t pac;
748                         if (parse_pointer_action(*args, &pac))
749                                 grab_pointer(pac);
750                         else
751                                 return MSG_FAILURE;
752                 } else if (streq("-u", *args) || streq("--ungrab", *args)) {
753                         ungrab_pointer();
754                 } else {
755                         return MSG_SYNTAX;
756                 }
757                 num--, args++;
758         }
759
760         return MSG_SUCCESS;
761 }
762
763 int cmd_restore(char **args, int num)
764 {
765         if (num < 1)
766                 return MSG_SYNTAX;
767         while (num > 0) {
768                 if (streq("-T", *args) || streq("--tree", *args)) {
769                         num--, args++;
770                         if (num < 1)
771                                 return MSG_SYNTAX;
772                         restore_tree(*args);
773                 } else if (streq("-H", *args) || streq("--history", *args)) {
774                         num--, args++;
775                         if (num < 1)
776                                 return MSG_SYNTAX;
777                         restore_history(*args);
778                 } else if (streq("-S", *args) || streq("--stack", *args)) {
779                         num--, args++;
780                         if (num < 1)
781                                 return MSG_SYNTAX;
782                         restore_stack(*args);
783                 } else {
784                         return MSG_SYNTAX;
785                 }
786                 num--, args++;
787         }
788
789         return MSG_SUCCESS;
790 }
791
792 int cmd_control(char **args, int num, FILE *rsp)
793 {
794         if (num < 1)
795                 return MSG_SYNTAX;
796         while (num > 0) {
797                 if (streq("--adopt-orphans", *args)) {
798                         adopt_orphans();
799                 } else if (streq("--toggle-visibility", *args)) {
800                         toggle_visibility();
801                 } else if (streq("--subscribe", *args)) {
802                         return MSG_SUBSCRIBE;
803                 } else if (streq("--get-status", *args)) {
804                         print_status(rsp);
805                 } else if (streq("--record-history", *args)) {
806                         num--, args++;
807                         if (num < 1)
808                                 return MSG_SYNTAX;
809                         bool b;
810                         if (parse_bool(*args, &b))
811                                 record_history = b;
812                         else
813                                 return MSG_SYNTAX;
814                 } else {
815                         return MSG_SYNTAX;
816                 }
817                 num--, args++;
818         }
819
820         return MSG_SUCCESS;
821 }
822
823 int cmd_config(char **args, int num, FILE *rsp)
824 {
825         if (num < 1)
826                 return MSG_SYNTAX;
827         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
828         coordinates_t trg = {NULL, NULL, NULL};
829         if ((*args)[0] == OPT_CHR) {
830                 if (streq("-m", *args) || streq("--monitor", *args)) {
831                         num--, args++;
832                         if (num < 1)
833                                 return MSG_SYNTAX;
834                         if (!monitor_from_desc(*args, &ref, &trg))
835                                 return MSG_FAILURE;
836                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
837                         num--, args++;
838                         if (num < 1)
839                                 return MSG_SYNTAX;
840                         if (!desktop_from_desc(*args, &ref, &trg))
841                                 return MSG_FAILURE;
842                 } else if (streq("-w", *args) || streq("--window", *args)) {
843                         num--, args++;
844                         if (num < 1)
845                                 return MSG_SYNTAX;
846                         if (!node_from_desc(*args, &ref, &trg))
847                                 return MSG_FAILURE;
848                 } else {
849                         return MSG_SYNTAX;
850                 }
851                 num--, args++;
852         }
853         if (num == 2)
854                 return set_setting(trg, *args, *(args + 1));
855         else if (num == 1)
856                 return get_setting(trg, *args, rsp);
857         else
858                 return MSG_SYNTAX;
859 }
860
861 int cmd_quit(char **args, int num)
862 {
863         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
864                 return MSG_FAILURE;
865         running = false;
866         return MSG_SUCCESS;
867 }
868
869 int set_setting(coordinates_t loc, char *name, char *value)
870 {
871 #define DESKWINDEFSET(k, v) \
872                 if (loc.node != NULL) \
873                         loc.node->client->k = v; \
874                 else if (loc.desktop != NULL) \
875                         loc.desktop->k = v; \
876                 else if (loc.monitor != NULL) \
877                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
878                                 d->k = v; \
879                 else \
880                         k = v;
881         if (streq("border_width", name)) {
882                 unsigned int bw;
883                 if (sscanf(value, "%u", &bw) != 1)
884                         return MSG_FAILURE;
885                 DESKWINDEFSET(border_width, bw)
886 #undef DESKWINDEFSET
887 #define DESKDEFSET(k, v) \
888                 if (loc.desktop != NULL) \
889                         loc.desktop->k = v; \
890                 else if (loc.monitor != NULL) \
891                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
892                                 d->k = v; \
893                 else \
894                         k = v;
895         } else if (streq("window_gap", name)) {
896                 int wg;
897                 if (sscanf(value, "%i", &wg) != 1)
898                         return MSG_FAILURE;
899                 DESKDEFSET(window_gap, wg)
900 #undef DESKDEFSET
901 #define MONDESKSET(k, v) \
902                 if (loc.desktop != NULL) \
903                         loc.desktop->k = v; \
904                 else if (loc.monitor != NULL) \
905                         loc.monitor->k = v; \
906                 else \
907                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
908                                 m->k = v;
909         } else if (streq("top_padding", name)) {
910                 int tp;
911                 if (sscanf(value, "%i", &tp) != 1)
912                         return MSG_FAILURE;
913                 MONDESKSET(top_padding, tp)
914         } else if (streq("right_padding", name)) {
915                 int rp;
916                 if (sscanf(value, "%i", &rp) != 1)
917                         return MSG_FAILURE;
918                 MONDESKSET(right_padding, rp)
919         } else if (streq("bottom_padding", name)) {
920                 int bp;
921                 if (sscanf(value, "%i", &bp) != 1)
922                         return MSG_FAILURE;
923                 MONDESKSET(bottom_padding, bp)
924         } else if (streq("left_padding", name)) {
925                 int lp;
926                 if (sscanf(value, "%i", &lp) != 1)
927                         return MSG_FAILURE;
928                 MONDESKSET(left_padding, lp)
929 #undef MONDESKSET
930 #define SETSTR(s) \
931         } else if (streq(#s, name)) { \
932                 return snprintf(s, sizeof(s), "%s", value) >= 0;
933         SETSTR(external_rules_command)
934         SETSTR(status_prefix)
935 #undef SETSTR
936         } else if (streq("split_ratio", name)) {
937                 double r;
938                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
939                         split_ratio = r;
940                 else
941                         return MSG_FAILURE;
942                 return MSG_SUCCESS;
943 #define SETCOLOR(s) \
944         } else if (streq(#s, name)) { \
945                 snprintf(s, sizeof(s), "%s", value);
946         SETCOLOR(focused_border_color)
947         SETCOLOR(active_border_color)
948         SETCOLOR(normal_border_color)
949         SETCOLOR(presel_border_color)
950         SETCOLOR(focused_locked_border_color)
951         SETCOLOR(active_locked_border_color)
952         SETCOLOR(normal_locked_border_color)
953         SETCOLOR(focused_sticky_border_color)
954         SETCOLOR(active_sticky_border_color)
955         SETCOLOR(normal_sticky_border_color)
956         SETCOLOR(focused_private_border_color)
957         SETCOLOR(active_private_border_color)
958         SETCOLOR(normal_private_border_color)
959         SETCOLOR(urgent_border_color)
960 #undef SETCOLOR
961         } else if (streq("initial_polarity", name)) {
962                 child_polarity_t p;
963                 if (parse_child_polarity(value, &p)) {
964                         initial_polarity = p;
965                 } else {
966                         return MSG_FAILURE;
967                 }
968         } else if (streq("focus_follows_pointer", name)) {
969                 bool b;
970                 if (parse_bool(value, &b) && b != focus_follows_pointer) {
971                         focus_follows_pointer = b;
972                         for (monitor_t *m = mon_head; m != NULL; m = m->next)
973                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
974                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
975                                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
976                                                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
977                                         }
978                         if (focus_follows_pointer) {
979                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
980                                         window_show(m->root);
981                                 enable_motion_recorder();
982                         } else {
983                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
984                                         window_hide(m->root);
985                                 disable_motion_recorder();
986                         }
987                         return MSG_SUCCESS;
988                 } else {
989                         return MSG_FAILURE;
990                 }
991 #define SETBOOL(s) \
992         } else if (streq(#s, name)) { \
993                 if (!parse_bool(value, &s)) \
994                         return MSG_FAILURE;
995                 SETBOOL(borderless_monocle)
996                 SETBOOL(gapless_monocle)
997                 SETBOOL(pointer_follows_focus)
998                 SETBOOL(pointer_follows_monitor)
999                 SETBOOL(apply_floating_atom)
1000                 SETBOOL(auto_alternate)
1001                 SETBOOL(auto_cancel)
1002                 SETBOOL(history_aware_focus)
1003                 SETBOOL(focus_by_distance)
1004                 SETBOOL(ignore_ewmh_focus)
1005                 SETBOOL(center_pseudo_tiled)
1006 #undef SETBOOL
1007 #define SETMONBOOL(s) \
1008         } else if (streq(#s, name)) { \
1009                 if (!parse_bool(value, &s)) \
1010                         return MSG_FAILURE; \
1011                 if (s) \
1012                         update_monitors();
1013                 SETMONBOOL(remove_disabled_monitors)
1014                 SETMONBOOL(remove_unplugged_monitors)
1015                 SETMONBOOL(merge_overlapping_monitors)
1016 #undef SETMONBOOL
1017         } else {
1018                 return MSG_FAILURE;
1019         }
1020
1021         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1022                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1023                         arrange(m, d);
1024
1025         return MSG_SUCCESS;
1026 }
1027
1028 int get_setting(coordinates_t loc, char *name, FILE* rsp)
1029 {
1030         if (streq("split_ratio", name))
1031                 fprintf(rsp, "%lf", split_ratio);
1032         else if (streq("window_gap", name))
1033                 if (loc.desktop != NULL)
1034                         fprintf(rsp, "%i", loc.desktop->window_gap);
1035                 else
1036                         fprintf(rsp, "%i", window_gap);
1037         else if (streq("border_width", name))
1038                 if (loc.node != NULL)
1039                         fprintf(rsp, "%u", loc.node->client->border_width);
1040                 else if (loc.desktop != NULL)
1041                         fprintf(rsp, "%u", loc.desktop->border_width);
1042                 else
1043                         fprintf(rsp, "%u", border_width);
1044         else if (streq("external_rules_command", name))
1045                 fprintf(rsp, "%s", external_rules_command);
1046         else if (streq("status_prefix", name))
1047                 fprintf(rsp, "%s", status_prefix);
1048         else if (streq("initial_polarity", name))
1049                 fprintf(rsp, "%s", initial_polarity == FIRST_CHILD ? "first_child" : "second_child");
1050 #define MONDESKGET(k) \
1051         else if (streq(#k, name)) \
1052                 if (loc.desktop != NULL) \
1053                         fprintf(rsp, "%i", loc.desktop->k); \
1054                 else if (loc.monitor != NULL) \
1055                         fprintf(rsp, "%i", loc.monitor->k); \
1056                 else \
1057                         return MSG_FAILURE;
1058         MONDESKGET(top_padding)
1059         MONDESKGET(right_padding)
1060         MONDESKGET(bottom_padding)
1061         MONDESKGET(left_padding)
1062 #undef DESKGET
1063 #define GETCOLOR(s) \
1064         else if (streq(#s, name)) \
1065                 fprintf(rsp, "%s", s);
1066         GETCOLOR(focused_border_color)
1067         GETCOLOR(active_border_color)
1068         GETCOLOR(normal_border_color)
1069         GETCOLOR(presel_border_color)
1070         GETCOLOR(focused_locked_border_color)
1071         GETCOLOR(active_locked_border_color)
1072         GETCOLOR(normal_locked_border_color)
1073         GETCOLOR(focused_sticky_border_color)
1074         GETCOLOR(active_sticky_border_color)
1075         GETCOLOR(normal_sticky_border_color)
1076         GETCOLOR(urgent_border_color)
1077 #undef GETCOLOR
1078 #define GETBOOL(s) \
1079         else if (streq(#s, name)) \
1080                 fprintf(rsp, "%s", BOOLSTR(s));
1081         GETBOOL(borderless_monocle)
1082         GETBOOL(gapless_monocle)
1083         GETBOOL(focus_follows_pointer)
1084         GETBOOL(pointer_follows_focus)
1085         GETBOOL(pointer_follows_monitor)
1086         GETBOOL(apply_floating_atom)
1087         GETBOOL(auto_alternate)
1088         GETBOOL(auto_cancel)
1089         GETBOOL(history_aware_focus)
1090         GETBOOL(focus_by_distance)
1091         GETBOOL(ignore_ewmh_focus)
1092         GETBOOL(center_pseudo_tiled)
1093         GETBOOL(remove_disabled_monitors)
1094         GETBOOL(remove_unplugged_monitors)
1095         GETBOOL(merge_overlapping_monitors)
1096 #undef GETBOOL
1097         else
1098                 return MSG_FAILURE;
1099         return MSG_SUCCESS;
1100 }
1101
1102 bool parse_bool(char *value, bool *b)
1103 {
1104         if (streq("true", value) || streq("on", value)) {
1105                 *b = true;
1106                 return true;
1107         } else if (streq("false", value) || streq("off", value)) {
1108                 *b = false;
1109                 return true;
1110         }
1111         return false;
1112 }
1113
1114 bool parse_layout(char *s, layout_t *l)
1115 {
1116         if (streq("monocle", s)) {
1117                 *l = LAYOUT_MONOCLE;
1118                 return true;
1119         } else if (streq("tiled", s)) {
1120                 *l = LAYOUT_TILED;
1121                 return true;
1122         }
1123         return false;
1124 }
1125
1126 bool parse_direction(char *s, direction_t *d)
1127 {
1128         if (streq("right", s)) {
1129                 *d = DIR_RIGHT;
1130                 return true;
1131         } else if (streq("down", s)) {
1132                 *d = DIR_DOWN;
1133                 return true;
1134         } else if (streq("left", s)) {
1135                 *d = DIR_LEFT;
1136                 return true;
1137         } else if (streq("up", s)) {
1138                 *d = DIR_UP;
1139                 return true;
1140         }
1141         return false;
1142 }
1143
1144 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1145 {
1146         if (streq("next", s)) {
1147                 *d = CYCLE_NEXT;
1148                 return true;
1149         } else if (streq("prev", s)) {
1150                 *d = CYCLE_PREV;
1151                 return true;
1152         }
1153         return false;
1154 }
1155
1156 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1157 {
1158         if (streq("forward", s)) {
1159                 *d = CIRCULATE_FORWARD;
1160                 return true;
1161         } else if (streq("backward", s)) {
1162                 *d = CIRCULATE_BACKWARD;
1163                 return true;
1164         }
1165         return false;
1166 }
1167
1168 bool parse_history_direction(char *s, history_dir_t *d)
1169 {
1170         if (streq("older", s)) {
1171                 *d = HISTORY_OLDER;
1172                 return true;
1173         } else if (streq("newer", s)) {
1174                 *d = HISTORY_NEWER;
1175                 return true;
1176         }
1177         return false;
1178 }
1179
1180
1181 bool parse_flip(char *s, flip_t *f)
1182 {
1183         if (streq("horizontal", s)) {
1184                 *f = FLIP_HORIZONTAL;
1185                 return true;
1186         } else if (streq("vertical", s)) {
1187                 *f = FLIP_VERTICAL;
1188                 return true;
1189         }
1190         return false;
1191 }
1192
1193 bool parse_pointer_action(char *s, pointer_action_t *a)
1194 {
1195         if (streq("move", s)) {
1196                 *a = ACTION_MOVE;
1197                 return true;
1198         } else if (streq("resize_corner", s)) {
1199                 *a = ACTION_RESIZE_CORNER;
1200                 return true;
1201         } else if (streq("resize_side", s)) {
1202                 *a = ACTION_RESIZE_SIDE;
1203                 return true;
1204         } else if (streq("focus", s)) {
1205                 *a = ACTION_FOCUS;
1206                 return true;
1207         }
1208         return false;
1209 }
1210
1211 bool parse_child_polarity(char *s, child_polarity_t *p)
1212 {
1213         if (streq("first_child", s)) {
1214                 *p = FIRST_CHILD;
1215                 return true;
1216         } else if (streq("second_child", s)) {
1217                 *p = SECOND_CHILD;
1218                 return true;
1219         }
1220         return false;
1221 }
1222
1223 bool parse_degree(char *s, int *d)
1224 {
1225         int i = atoi(s);
1226         while (i < 0)
1227                 i += 360;
1228         while (i > 359)
1229                 i -= 360;
1230         if ((i % 90) != 0) {
1231                 return false;
1232         } else {
1233                 *d = i;
1234                 return true;
1235         }
1236 }
1237
1238 bool parse_window_id(char *s, long int *i)
1239 {
1240         char *end;
1241         errno = 0;
1242         long int ret = strtol(s, &end, 0);
1243         if (errno != 0 || *end != '\0')
1244                 return false;
1245         else
1246                 *i = ret;
1247         return true;
1248 }
1249
1250 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1251 {
1252         *key = strtok(s, EQL_TOK);
1253         char *v = strtok(NULL, EQL_TOK);
1254         if (v == NULL) {
1255                 *state = ALTER_TOGGLE;
1256                 return true;
1257         } else {
1258                 if (parse_bool(v, value)) {
1259                         *state = ALTER_SET;
1260                         return true;
1261                 } else {
1262                         return false;
1263                 }
1264         }
1265         return false;
1266 }
1267
1268 bool parse_index(char *s, int *i)
1269 {
1270         int idx;
1271         if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1272                 return false;
1273         *i = idx;
1274         return true;
1275 }