]> git.lizzy.rs Git - bspwm.git/blob - messages.c
83dfb8faba906bd241f33bf1f7222470dd570661
[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                         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 DESKWINSET(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                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
881                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \
882                                         d->k = v;
883         if (streq("border_width", name)) {
884                 unsigned int bw;
885                 if (sscanf(value, "%u", &bw) != 1)
886                         return MSG_FAILURE;
887                 DESKWINSET(border_width, bw)
888 #undef DESKWINSET
889 #define DESKSET(k, v) \
890                 if (loc.desktop != NULL) \
891                         loc.desktop->k = v; \
892                 else if (loc.monitor != NULL) \
893                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
894                                 d->k = v; \
895                 else \
896                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
897                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \
898                                         d->k = v;
899         } else if (streq("window_gap", name)) {
900                 int wg;
901                 if (sscanf(value, "%i", &wg) != 1)
902                         return MSG_FAILURE;
903                 DESKSET(window_gap, wg)
904 #undef DESKSET
905 #define MONDESKSET(k, v) \
906                 if (loc.desktop != NULL) \
907                         loc.desktop->k = v; \
908                 else if (loc.monitor != NULL) \
909                         loc.monitor->k = v; \
910                 else \
911                         for (monitor_t *m = mon_head; m != NULL; m = m->next) \
912                                 m->k = v;
913         } else if (streq("top_padding", name)) {
914                 int tp;
915                 if (sscanf(value, "%i", &tp) != 1)
916                         return MSG_FAILURE;
917                 MONDESKSET(top_padding, tp)
918         } else if (streq("right_padding", name)) {
919                 int rp;
920                 if (sscanf(value, "%i", &rp) != 1)
921                         return MSG_FAILURE;
922                 MONDESKSET(right_padding, rp)
923         } else if (streq("bottom_padding", name)) {
924                 int bp;
925                 if (sscanf(value, "%i", &bp) != 1)
926                         return MSG_FAILURE;
927                 MONDESKSET(bottom_padding, bp)
928         } else if (streq("left_padding", name)) {
929                 int lp;
930                 if (sscanf(value, "%i", &lp) != 1)
931                         return MSG_FAILURE;
932                 MONDESKSET(left_padding, lp)
933 #undef MONDESKSET
934 #define SETSTR(s) \
935         } else if (streq(#s, name)) { \
936                 return snprintf(s, sizeof(s), "%s", value) >= 0;
937         SETSTR(external_rules_command)
938         SETSTR(status_prefix)
939 #undef SETSTR
940         } else if (streq("split_ratio", name)) {
941                 double r;
942                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
943                         split_ratio = r;
944                 else
945                         return MSG_FAILURE;
946                 return MSG_SUCCESS;
947 #define SETCOLOR(s) \
948         } else if (streq(#s, name)) { \
949                 snprintf(s, sizeof(s), "%s", value);
950         SETCOLOR(focused_border_color)
951         SETCOLOR(active_border_color)
952         SETCOLOR(normal_border_color)
953         SETCOLOR(presel_border_color)
954         SETCOLOR(focused_locked_border_color)
955         SETCOLOR(active_locked_border_color)
956         SETCOLOR(normal_locked_border_color)
957         SETCOLOR(focused_sticky_border_color)
958         SETCOLOR(active_sticky_border_color)
959         SETCOLOR(normal_sticky_border_color)
960         SETCOLOR(focused_private_border_color)
961         SETCOLOR(active_private_border_color)
962         SETCOLOR(normal_private_border_color)
963         SETCOLOR(urgent_border_color)
964 #undef SETCOLOR
965         } else if (streq("focus_follows_pointer", name)) {
966                 bool b;
967                 if (parse_bool(value, &b) && b != focus_follows_pointer) {
968                         focus_follows_pointer = b;
969                         for (monitor_t *m = mon_head; m != NULL; m = m->next)
970                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
971                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
972                                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
973                                                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
974                                         }
975                         if (focus_follows_pointer) {
976                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
977                                         window_show(m->root);
978                                 enable_motion_recorder();
979                         } else {
980                                 for (monitor_t *m = mon_head; m != NULL; m = m->next)
981                                         window_hide(m->root);
982                                 disable_motion_recorder();
983                         }
984                         return MSG_SUCCESS;
985                 } else {
986                         return MSG_FAILURE;
987                 }
988 #define SETBOOL(s) \
989         } else if (streq(#s, name)) { \
990                 if (!parse_bool(value, &s)) \
991                         return MSG_FAILURE;
992                 SETBOOL(borderless_monocle)
993                 SETBOOL(gapless_monocle)
994                 SETBOOL(pointer_follows_monitor)
995                 SETBOOL(apply_floating_atom)
996                 SETBOOL(auto_alternate)
997                 SETBOOL(auto_cancel)
998                 SETBOOL(history_aware_focus)
999                 SETBOOL(ignore_ewmh_focus)
1000                 SETBOOL(remove_disabled_monitor)
1001                 SETBOOL(persistent_monitors)
1002 #undef SETBOOL
1003         } else {
1004                 return MSG_FAILURE;
1005         }
1006
1007         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1008                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1009                         arrange(m, d);
1010
1011         return MSG_SUCCESS;
1012 }
1013
1014 int get_setting(coordinates_t loc, char *name, FILE* rsp)
1015 {
1016         if (streq("split_ratio", name))
1017                 fprintf(rsp, "%lf", split_ratio);
1018         else if (streq("window_gap", name))
1019                 if (loc.desktop == NULL)
1020                         return MSG_FAILURE;
1021                 else
1022                         fprintf(rsp, "%i", loc.desktop->window_gap);
1023         else if (streq("border_width", name))
1024                 if (loc.node != NULL)
1025                         fprintf(rsp, "%u", loc.node->client->border_width);
1026                 else if (loc.desktop != NULL)
1027                         fprintf(rsp, "%u", loc.desktop->border_width);
1028                 else
1029                         return MSG_FAILURE;
1030         else if (streq("external_rules_command", name))
1031                 fprintf(rsp, "%s", external_rules_command);
1032         else if (streq("status_prefix", name))
1033                 fprintf(rsp, "%s", status_prefix);
1034 #define MONDESKGET(k) \
1035         else if (streq(#k, name)) \
1036                 if (loc.desktop != NULL) \
1037                         fprintf(rsp, "%i", loc.desktop->k); \
1038                 else if (loc.monitor != NULL) \
1039                         fprintf(rsp, "%i", loc.monitor->k); \
1040                 else \
1041                         return MSG_FAILURE;
1042         MONDESKGET(top_padding)
1043         MONDESKGET(right_padding)
1044         MONDESKGET(bottom_padding)
1045         MONDESKGET(left_padding)
1046 #undef DESKGET
1047 #define GETCOLOR(s) \
1048         else if (streq(#s, name)) \
1049                 fprintf(rsp, "%s", s);
1050         GETCOLOR(focused_border_color)
1051         GETCOLOR(active_border_color)
1052         GETCOLOR(normal_border_color)
1053         GETCOLOR(presel_border_color)
1054         GETCOLOR(focused_locked_border_color)
1055         GETCOLOR(active_locked_border_color)
1056         GETCOLOR(normal_locked_border_color)
1057         GETCOLOR(focused_sticky_border_color)
1058         GETCOLOR(active_sticky_border_color)
1059         GETCOLOR(normal_sticky_border_color)
1060         GETCOLOR(urgent_border_color)
1061 #undef GETCOLOR
1062 #define GETBOOL(s) \
1063         else if (streq(#s, name)) \
1064                 fprintf(rsp, "%s", BOOLSTR(s));
1065         GETBOOL(borderless_monocle)
1066         GETBOOL(gapless_monocle)
1067         GETBOOL(focus_follows_pointer)
1068         GETBOOL(pointer_follows_monitor)
1069         GETBOOL(apply_floating_atom)
1070         GETBOOL(auto_alternate)
1071         GETBOOL(auto_cancel)
1072         GETBOOL(history_aware_focus)
1073         GETBOOL(ignore_ewmh_focus)
1074         GETBOOL(remove_disabled_monitor)
1075         GETBOOL(persistent_monitors)
1076 #undef GETBOOL
1077         else
1078                 return MSG_FAILURE;
1079         return MSG_SUCCESS;
1080 }
1081
1082 bool parse_bool(char *value, bool *b)
1083 {
1084         if (streq("true", value) || streq("on", value)) {
1085                 *b = true;
1086                 return true;
1087         } else if (streq("false", value) || streq("off", value)) {
1088                 *b = false;
1089                 return true;
1090         }
1091         return false;
1092 }
1093
1094 bool parse_layout(char *s, layout_t *l)
1095 {
1096         if (streq("monocle", s)) {
1097                 *l = LAYOUT_MONOCLE;
1098                 return true;
1099         } else if (streq("tiled", s)) {
1100                 *l = LAYOUT_TILED;
1101                 return true;
1102         }
1103         return false;
1104 }
1105
1106 bool parse_direction(char *s, direction_t *d)
1107 {
1108         if (streq("right", s)) {
1109                 *d = DIR_RIGHT;
1110                 return true;
1111         } else if (streq("down", s)) {
1112                 *d = DIR_DOWN;
1113                 return true;
1114         } else if (streq("left", s)) {
1115                 *d = DIR_LEFT;
1116                 return true;
1117         } else if (streq("up", s)) {
1118                 *d = DIR_UP;
1119                 return true;
1120         }
1121         return false;
1122 }
1123
1124 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1125 {
1126         if (streq("next", s)) {
1127                 *d = CYCLE_NEXT;
1128                 return true;
1129         } else if (streq("prev", s)) {
1130                 *d = CYCLE_PREV;
1131                 return true;
1132         }
1133         return false;
1134 }
1135
1136 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1137 {
1138         if (streq("forward", s)) {
1139                 *d = CIRCULATE_FORWARD;
1140                 return true;
1141         } else if (streq("backward", s)) {
1142                 *d = CIRCULATE_BACKWARD;
1143                 return true;
1144         }
1145         return false;
1146 }
1147
1148 bool parse_history_direction(char *s, history_dir_t *d)
1149 {
1150         if (streq("older", s)) {
1151                 *d = HISTORY_OLDER;
1152                 return true;
1153         } else if (streq("newer", s)) {
1154                 *d = HISTORY_NEWER;
1155                 return true;
1156         }
1157         return false;
1158 }
1159
1160
1161 bool parse_flip(char *s, flip_t *f)
1162 {
1163         if (streq("horizontal", s)) {
1164                 *f = FLIP_HORIZONTAL;
1165                 return true;
1166         } else if (streq("vertical", s)) {
1167                 *f = FLIP_VERTICAL;
1168                 return true;
1169         }
1170         return false;
1171 }
1172
1173 bool parse_pointer_action(char *s, pointer_action_t *a)
1174 {
1175         if (streq("move", s)) {
1176                 *a = ACTION_MOVE;
1177                 return true;
1178         } else if (streq("resize_corner", s)) {
1179                 *a = ACTION_RESIZE_CORNER;
1180                 return true;
1181         } else if (streq("resize_side", s)) {
1182                 *a = ACTION_RESIZE_SIDE;
1183                 return true;
1184         } else if (streq("focus", s)) {
1185                 *a = ACTION_FOCUS;
1186                 return true;
1187         }
1188         return false;
1189 }
1190
1191 bool parse_degree(char *s, int *d)
1192 {
1193         int i = atoi(s);
1194         while (i < 0)
1195                 i += 360;
1196         while (i > 359)
1197                 i -= 360;
1198         if ((i % 90) != 0) {
1199                 return false;
1200         } else {
1201                 *d = i;
1202                 return true;
1203         }
1204 }
1205
1206 bool parse_window_id(char *s, long int *i)
1207 {
1208         char *end;
1209         errno = 0;
1210         long int ret = strtol(s, &end, 0);
1211         if (errno != 0 || *end != '\0')
1212                 return false;
1213         else
1214                 *i = ret;
1215         return true;
1216 }
1217
1218 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1219 {
1220         *key = strtok(s, EQL_TOK);
1221         char *v = strtok(NULL, EQL_TOK);
1222         if (v == NULL) {
1223                 *state = ALTER_TOGGLE;
1224                 return true;
1225         } else {
1226                 if (parse_bool(v, value)) {
1227                         *state = ALTER_SET;
1228                         return true;
1229                 } else {
1230                         return false;
1231                 }
1232         }
1233         return false;
1234 }
1235
1236 bool parse_index(char *s, int *i)
1237 {
1238         int idx;
1239         if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1240                 return false;
1241         *i = idx;
1242         return true;
1243 }