]> git.lizzy.rs Git - bspwm.git/blob - messages.c
Update monitors when setting *_monitors
[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                         if (!is_tiled(trg.node->client))
265                                 return MSG_FAILURE;
266                         direction_t dir;
267                         if (!parse_direction(*args, &dir))
268                                 return MSG_FAILURE;
269                         node_t *n = find_fence(trg.node, dir);
270                         if (n == NULL)
271                                 return MSG_FAILURE;
272                         num--, args++;
273                         if ((*args)[0] == '+' || (*args)[0] == '-') {
274                                 int pix;
275                                 if (sscanf(*args, "%i", &pix) == 1) {
276                                         int max = (n->split_type == TYPE_HORIZONTAL ? n->rectangle.height : n->rectangle.width);
277                                         double rat = ((max * n->split_ratio) + pix) / max;
278                                         if (rat > 0 && rat < 1)
279                                                 n->split_ratio = rat;
280                                         else
281                                                 return MSG_FAILURE;
282                                 } else {
283                                         return MSG_FAILURE;
284                                 }
285                         } else {
286                                 double rat;
287                                 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1)
288                                         n->split_ratio = rat;
289                                 else
290                                         return MSG_FAILURE;
291                         }
292                         dirty = true;
293                 } else if (streq("-r", *args) || streq("--ratio", *args)) {
294                         num--, args++;
295                         if (num < 1)
296                                 return MSG_SYNTAX;
297                         double rat;
298                         if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
299                                 trg.node->split_ratio = rat;
300                                 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
301                         } else {
302                                 return MSG_FAILURE;
303                         }
304                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
305                         num--, args++;
306                         if (num < 2)
307                                 return MSG_SYNTAX;
308                         direction_t dir;
309                         if (!parse_direction(*args, &dir))
310                                 return MSG_FAILURE;
311                         node_t *n = find_fence(trg.node, dir);
312                         if (n == NULL)
313                                 return MSG_FAILURE;
314                         num--, args++;
315                         int deg;
316                         if (parse_degree(*args, &deg)) {
317                                 rotate_tree(n, deg);
318                                 dirty = true;
319                         } else {
320                                 return MSG_FAILURE;
321                         }
322                 } else if (streq("-c", *args) || streq("--close", *args)) {
323                         if (num > 1)
324                                 return MSG_SYNTAX;
325                         window_close(trg.node);
326                 } else if (streq("-k", *args) || streq("--kill", *args)) {
327                         if (num > 1)
328                                 return MSG_SYNTAX;
329                         window_kill(trg.monitor, trg.desktop, trg.node);
330                         dirty = true;
331                 } else {
332                         return MSG_SYNTAX;
333                 }
334
335                 num--, args++;
336         }
337
338         if (dirty)
339                 arrange(trg.monitor, trg.desktop);
340
341         return MSG_SUCCESS;
342 }
343
344 int cmd_desktop(char **args, int num)
345 {
346         if (num < 1)
347                 return MSG_SYNTAX;
348
349         coordinates_t ref = {mon, mon->desk, NULL};
350         coordinates_t trg = ref;
351
352         if ((*args)[0] != OPT_CHR) {
353                 if (desktop_from_desc(*args, &ref, &trg))
354                         num--, args++;
355                 else
356                         return MSG_FAILURE;
357         }
358
359         bool dirty = false;
360
361         while (num > 0) {
362                 if (streq("-f", *args) || streq("--focus", *args)) {
363                         coordinates_t dst = trg;
364                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
365                                 num--, args++;
366                                 if (!desktop_from_desc(*args, &trg, &dst))
367                                         return MSG_FAILURE;
368                         }
369                         if (auto_alternate && dst.desktop == mon->desk) {
370                                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
371                                 history_find_desktop(HISTORY_OLDER, &trg, &dst, sel);
372                         }
373                         focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
374                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
375                         num--, args++;
376                         if (num < 1)
377                                 return MSG_SYNTAX;
378                         if (trg.monitor->desk_head == trg.monitor->desk_tail)
379                                 return MSG_FAILURE;
380                         coordinates_t dst;
381                         if (monitor_from_desc(*args, &trg, &dst)) {
382                                 transfer_desktop(trg.monitor, dst.monitor, trg.desktop);
383                                 trg.monitor = dst.monitor;
384                                 update_current();
385                         } else {
386                                 return MSG_FAILURE;
387                         }
388                 } else if (streq("-s", *args) || streq("--swap", *args)) {
389                         num--, args++;
390                         if (num < 1)
391                                 return MSG_SYNTAX;
392                         coordinates_t dst;
393                         if (desktop_from_desc(*args, &trg, &dst))
394                                 swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop);
395                         else
396                                 return MSG_FAILURE;
397                 } else if (streq("-l", *args) || streq("--layout", *args)) {
398                         num--, args++;
399                         if (num < 1)
400                                 return MSG_SYNTAX;
401                         layout_t lyt;
402                         cycle_dir_t cyc;
403                         if (parse_cycle_direction(*args, &cyc))
404                                 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
405                         else if (parse_layout(*args, &lyt))
406                                 change_layout(trg.monitor, trg.desktop, lyt);
407                         else
408                                 return MSG_FAILURE;
409                 } else if (streq("-n", *args) || streq("--rename", *args)) {
410                         num--, args++;
411                         if (num < 1)
412                                 return MSG_SYNTAX;
413                         snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
414                         ewmh_update_desktop_names();
415                         put_status();
416                 } else if (streq("-r", *args) || streq("--remove", *args)) {
417                         if (trg.desktop->root == NULL &&
418                             trg.monitor->desk_head != trg.monitor->desk_tail) {
419                                 remove_desktop(trg.monitor, trg.desktop);
420                                 show_desktop(trg.monitor->desk);
421                                 update_current();
422                                 return MSG_SUCCESS;
423                         } else {
424                                 return MSG_FAILURE;
425                         }
426                 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
427                         reset_mode(&trg);
428                 } else if (streq("-F", *args) || streq("--flip", *args)) {
429                         num--, args++;
430                         if (num < 1)
431                                 return MSG_SYNTAX;
432                         flip_t flp;
433                         if (parse_flip(*args, &flp)) {
434                                 flip_tree(trg.desktop->root, flp);
435                                 dirty = true;
436                         } else {
437                                 return MSG_FAILURE;
438                         }
439                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
440                         num--, args++;
441                         if (num < 1)
442                                 return MSG_SYNTAX;
443                         int deg;
444                         if (parse_degree(*args, &deg)) {
445                                 rotate_tree(trg.desktop->root, deg);
446                                 dirty = true;
447                         } else {
448                                 return MSG_FAILURE;
449                         }
450                 } else if (streq("-E", *args) || streq("--equalize", *args)) {
451                         equalize_tree(trg.desktop->root);
452                         dirty = true;
453                 } else if (streq("-B", *args) || streq("--balance", *args)) {
454                         balance_tree(trg.desktop->root);
455                         dirty = true;
456                 } else if (streq("-C", *args) || streq("--circulate", *args)) {
457                         num--, args++;
458                         if (num < 1)
459                                 return MSG_SYNTAX;
460                         circulate_dir_t cir;
461                         if (parse_circulate_direction(*args, &cir)) {
462                                 circulate_leaves(trg.monitor, trg.desktop, cir);
463                                 dirty = true;
464                         } else {
465                                 return MSG_FAILURE;
466                         }
467                 } else if (streq("-t", *args) || streq("--toggle", *args)) {
468                         num--, args++;
469                         if (num < 1)
470                                 return MSG_SYNTAX;
471                         char *key = strtok(*args, EQL_TOK);
472                         char *val = strtok(NULL, EQL_TOK);
473                         alter_state_t a;
474                         bool b;
475                         if (val == NULL) {
476                                 a = ALTER_TOGGLE;
477                         } else {
478                                 if (parse_bool(val, &b))
479                                         a = ALTER_SET;
480                                 else
481                                         return MSG_FAILURE;
482                         }
483                         if (streq("floating", key))
484                                 trg.desktop->floating = (a == ALTER_SET ? b : !trg.desktop->floating);
485                         else
486                                 return MSG_FAILURE;
487                 } else {
488                         return MSG_SYNTAX;
489                 }
490                 num--, args++;
491         }
492
493         if (dirty)
494                 arrange(trg.monitor, trg.desktop);
495
496         return MSG_SUCCESS;
497 }
498
499 int cmd_monitor(char **args, int num)
500 {
501         if (num < 1)
502                 return MSG_SYNTAX;
503
504         coordinates_t ref = {mon, NULL, NULL};
505         coordinates_t trg = ref;
506
507         if ((*args)[0] != OPT_CHR) {
508                 if (monitor_from_desc(*args, &ref, &trg))
509                         num--, args++;
510                 else
511                         return MSG_FAILURE;
512         }
513
514         while (num > 0) {
515                 if (streq("-f", *args) || streq("--focus", *args)) {
516                         coordinates_t dst = trg;
517                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
518                                 num--, args++;
519                                 if (!monitor_from_desc(*args, &trg, &dst))
520                                         return MSG_FAILURE;
521                         }
522                         if (auto_alternate && dst.monitor == mon) {
523                                 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
524                                 history_find_monitor(HISTORY_OLDER, &trg, &dst, sel);
525                         }
526                         focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
527                 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
528                         num--, args++;
529                         if (num < 1)
530                                 return MSG_SYNTAX;
531                         desktop_t *d = trg.monitor->desk_head;
532                         while (num > 0 && d != NULL) {
533                                 snprintf(d->name, sizeof(d->name), "%s", *args);
534                                 initialize_desktop(d);
535                                 arrange(trg.monitor, d);
536                                 d = d->next;
537                                 num--, args++;
538                         }
539                         put_status();
540                         while (num > 0) {
541                                 add_desktop(trg.monitor, make_desktop(*args));
542                                 num--, args++;
543                         }
544                         while (d != NULL) {
545                                 desktop_t *next = d->next;
546                                 if (d == mon->desk)
547                                         focus_node(trg.monitor, d->prev, d->prev->focus);
548                                 merge_desktops(trg.monitor, d, mon, mon->desk);
549                                 remove_desktop(trg.monitor, d);
550                                 d = next;
551                         }
552                 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
553                         num--, args++;
554                         if (num < 1)
555                                 return MSG_SYNTAX;
556                         while (num > 0) {
557                                 add_desktop(trg.monitor, make_desktop(*args));
558                                 num--, args++;
559                         }
560                 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
561                         num--, args++;
562                         if (num < 1)
563                                 return MSG_SYNTAX;
564                         while (num > 0) {
565                                 coordinates_t dst;
566                                 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
567                                         remove_desktop(dst.monitor, dst.desktop);
568                                         show_desktop(dst.monitor->desk);
569                                 }
570                                 num--, args++;
571                         }
572                 } else if (streq("-o", *args) || streq("--order-desktops", *args)) {
573                         num--, args++;
574                         if (num < 1)
575                                 return MSG_SYNTAX;
576                         desktop_t *d = trg.monitor->desk_head;
577                         while (d != NULL && num > 0) {
578                                 desktop_t *next = d->next;
579                                 coordinates_t dst;
580                                 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
581                                         swap_desktops(trg.monitor, d, dst.monitor, dst.desktop);
582                                         if (next == dst.desktop)
583                                                 next = d;
584                                 }
585                                 d = next;
586                                 num--, args++;
587                         }
588                 } else if (streq("-n", *args) || streq("--rename", *args)) {
589                         num--, args++;
590                         if (num < 1)
591                                 return MSG_SYNTAX;
592                         snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
593                         put_status();
594                 } else if (streq("-s", *args) || streq("--swap", *args)) {
595                         num--, args++;
596                         if (num < 1)
597                                 return MSG_SYNTAX;
598                         coordinates_t dst;
599                         if (monitor_from_desc(*args, &trg, &dst))
600                                 swap_monitors(trg.monitor, dst.monitor);
601                         else
602                                 return MSG_FAILURE;
603                 } else {
604                         return MSG_SYNTAX;
605                 }
606                 num--, args++;
607         }
608
609         return MSG_SUCCESS;
610 }
611
612 int cmd_query(char **args, int num, FILE *rsp)
613 {
614         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
615         coordinates_t trg = {NULL, NULL, NULL};
616         domain_t dom = DOMAIN_TREE;
617         int d = 0, t = 0;
618
619         while (num > 0) {
620                 if (streq("-T", *args) || streq("--tree", *args)) {
621                         dom = DOMAIN_TREE, d++;
622                 } else if (streq("-M", *args) || streq("--monitors", *args)) {
623                         dom = DOMAIN_MONITOR, d++;
624                 } else if (streq("-D", *args) || streq("--desktops", *args)) {
625                         dom = DOMAIN_DESKTOP, d++;
626                 } else if (streq("-W", *args) || streq("--windows", *args)) {
627                         dom = DOMAIN_WINDOW, d++;
628                 } else if (streq("-H", *args) || streq("--history", *args)) {
629                         dom = DOMAIN_HISTORY, d++;
630                 } else if (streq("-S", *args) || streq("--stack", *args)) {
631                         dom = DOMAIN_STACK, d++;
632                 } else if (streq("-m", *args) || streq("--monitor", *args)) {
633                         trg.monitor = ref.monitor;
634                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
635                                 num--, args++;
636                                 if (!monitor_from_desc(*args, &ref, &trg))
637                                         return MSG_FAILURE;
638                         }
639                         t++;
640                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
641                         trg.monitor = ref.monitor;
642                         trg.desktop = ref.desktop;
643                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
644                                 num--, args++;
645                                 if (!desktop_from_desc(*args, &ref, &trg))
646                                         return MSG_FAILURE;
647                         }
648                         t++;
649                 } else if (streq("-w", *args) || streq("--window", *args)) {
650                         trg = ref;
651                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
652                                 num--, args++;
653                                 if (!node_from_desc(*args, &ref, &trg))
654                                         return MSG_FAILURE;
655                         }
656                         t++;
657                 } else {
658                         return MSG_SYNTAX;
659                 }
660                 num--, args++;
661         }
662
663         if (d != 1 || t > 1)
664                 return MSG_SYNTAX;
665
666         if (dom == DOMAIN_HISTORY)
667                 query_history(trg, rsp);
668         else if (dom == DOMAIN_STACK)
669                 query_stack(rsp);
670         else if (dom == DOMAIN_WINDOW)
671                 query_windows(trg, rsp);
672         else
673                 query_monitors(trg, dom, rsp);
674
675         return MSG_SUCCESS;
676 }
677
678 int cmd_rule(char **args, int num, FILE *rsp)
679 {
680         if (num < 1)
681                 return MSG_SYNTAX;
682         while (num > 0) {
683                 if (streq("-a", *args) || streq("--add", *args)) {
684                         num--, args++;
685                         if (num < 2)
686                                 return MSG_SYNTAX;
687                         rule_t *rule = make_rule();
688                         snprintf(rule->cause, sizeof(rule->cause), "%s", *args);
689                         num--, args++;
690                         size_t i = 0;
691                         while (num > 0) {
692                                 if (streq("-o", *args) || streq("--one-shot", *args)) {
693                                         rule->one_shot = true;
694                                 } else {
695                                         for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++)
696                                                 rule->effect[i] = (*args)[j];
697                                         if (num > 1 && i < sizeof(rule->effect))
698                                                 rule->effect[i++] = ' ';
699                                 }
700                                 num--, args++;
701                         }
702                         rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
703                         add_rule(rule);
704                 } else if (streq("-r", *args) || streq("--remove", *args)) {
705                         num--, args++;
706                         if (num < 1)
707                                 return MSG_SYNTAX;
708                         int idx;
709                         while (num > 0) {
710                                 if (parse_index(*args, &idx))
711                                         remove_rule_by_index(idx - 1);
712                                 else if (streq("tail", *args))
713                                         remove_rule(rule_tail);
714                                 else if (streq("head", *args))
715                                         remove_rule(rule_head);
716                                 else
717                                         remove_rule_by_cause(*args);
718                                 num--, args++;
719                         }
720                 } else if (streq("-l", *args) || streq("--list", *args)) {
721                         num--, args++;
722                         list_rules(num > 0 ? *args : NULL, rsp);
723                 } else {
724                         return MSG_SYNTAX;
725                 }
726                 num--, args++;
727         }
728
729         return MSG_SUCCESS;
730 }
731
732 int cmd_pointer(char **args, int num)
733 {
734         if (num < 1)
735                 return MSG_SYNTAX;
736         while (num > 0) {
737                 if (streq("-t", *args) || streq("--track", *args)) {
738                         num--, args++;
739                         if (num < 2)
740                                 return MSG_SYNTAX;
741                         int x, y;
742                         if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
743                                 track_pointer(x, y);
744                         else
745                                 return MSG_FAILURE;
746                         num--, args++;
747                 } else if (streq("-g", *args) || streq("--grab", *args)) {
748                         num--, args++;
749                         if (num < 1)
750                                 return MSG_SYNTAX;
751                         pointer_action_t pac;
752                         if (parse_pointer_action(*args, &pac))
753                                 grab_pointer(pac);
754                         else
755                                 return MSG_FAILURE;
756                 } else if (streq("-u", *args) || streq("--ungrab", *args)) {
757                         ungrab_pointer();
758                 } else {
759                         return MSG_SYNTAX;
760                 }
761                 num--, args++;
762         }
763
764         return MSG_SUCCESS;
765 }
766
767 int cmd_restore(char **args, int num)
768 {
769         if (num < 1)
770                 return MSG_SYNTAX;
771         while (num > 0) {
772                 if (streq("-T", *args) || streq("--tree", *args)) {
773                         num--, args++;
774                         if (num < 1)
775                                 return MSG_SYNTAX;
776                         restore_tree(*args);
777                 } else if (streq("-H", *args) || streq("--history", *args)) {
778                         num--, args++;
779                         if (num < 1)
780                                 return MSG_SYNTAX;
781                         restore_history(*args);
782                 } else if (streq("-S", *args) || streq("--stack", *args)) {
783                         num--, args++;
784                         if (num < 1)
785                                 return MSG_SYNTAX;
786                         restore_stack(*args);
787                 } else {
788                         return MSG_SYNTAX;
789                 }
790                 num--, args++;
791         }
792
793         return MSG_SUCCESS;
794 }
795
796 int cmd_control(char **args, int num, FILE *rsp)
797 {
798         if (num < 1)
799                 return MSG_SYNTAX;
800         while (num > 0) {
801                 if (streq("--adopt-orphans", *args)) {
802                         adopt_orphans();
803                 } else if (streq("--toggle-visibility", *args)) {
804                         toggle_visibility();
805                 } else if (streq("--subscribe", *args)) {
806                         return MSG_SUBSCRIBE;
807                 } else if (streq("--get-status", *args)) {
808                         print_status(rsp);
809                 } else if (streq("--record-history", *args)) {
810                         num--, args++;
811                         if (num < 1)
812                                 return MSG_SYNTAX;
813                         bool b;
814                         if (parse_bool(*args, &b))
815                                 record_history = b;
816                         else
817                                 return MSG_SYNTAX;
818                 } else {
819                         return MSG_SYNTAX;
820                 }
821                 num--, args++;
822         }
823
824         return MSG_SUCCESS;
825 }
826
827 int cmd_config(char **args, int num, FILE *rsp)
828 {
829         if (num < 1)
830                 return MSG_SYNTAX;
831         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
832         coordinates_t trg = {NULL, NULL, NULL};
833         if ((*args)[0] == OPT_CHR) {
834                 if (streq("-m", *args) || streq("--monitor", *args)) {
835                         num--, args++;
836                         if (num < 1)
837                                 return MSG_SYNTAX;
838                         if (!monitor_from_desc(*args, &ref, &trg))
839                                 return MSG_FAILURE;
840                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
841                         num--, args++;
842                         if (num < 1)
843                                 return MSG_SYNTAX;
844                         if (!desktop_from_desc(*args, &ref, &trg))
845                                 return MSG_FAILURE;
846                 } else if (streq("-w", *args) || streq("--window", *args)) {
847                         num--, args++;
848                         if (num < 1)
849                                 return MSG_SYNTAX;
850                         if (!node_from_desc(*args, &ref, &trg))
851                                 return MSG_FAILURE;
852                 } else {
853                         return MSG_SYNTAX;
854                 }
855                 num--, args++;
856         }
857         if (num == 2)
858                 return set_setting(trg, *args, *(args + 1));
859         else if (num == 1)
860                 return get_setting(trg, *args, rsp);
861         else
862                 return MSG_SYNTAX;
863 }
864
865 int cmd_quit(char **args, int num)
866 {
867         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
868                 return MSG_FAILURE;
869         running = false;
870         return MSG_SUCCESS;
871 }
872
873 int set_setting(coordinates_t loc, char *name, char *value)
874 {
875 #define DESKWINDEFSET(k, v) \
876                 if (loc.node != NULL) \
877                         loc.node->client->k = v; \
878                 else if (loc.desktop != NULL) \
879                         loc.desktop->k = v; \
880                 else if (loc.monitor != NULL) \
881                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
882                                 d->k = v; \
883                 else \
884                         k = v;
885         if (streq("border_width", name)) {
886                 unsigned int bw;
887                 if (sscanf(value, "%u", &bw) != 1)
888                         return MSG_FAILURE;
889                 DESKWINDEFSET(border_width, bw)
890 #undef DESKWINDEFSET
891 #define DESKDEFSET(k, v) \
892                 if (loc.desktop != NULL) \
893                         loc.desktop->k = v; \
894                 else if (loc.monitor != NULL) \
895                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
896                                 d->k = v; \
897                 else \
898                         k = v;
899         } else if (streq("window_gap", name)) {
900                 int wg;
901                 if (sscanf(value, "%i", &wg) != 1)
902                         return MSG_FAILURE;
903                 DESKDEFSET(window_gap, wg)
904 #undef DESKDEFSET
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(focus_by_distance)
1000                 SETBOOL(ignore_ewmh_focus)
1001 #undef SETBOOL
1002 #define SETMONBOOL(s) \
1003         } else if (streq(#s, name)) { \
1004                 if (!parse_bool(value, &s)) \
1005                         return MSG_FAILURE; \
1006                 if (s) \
1007                         update_monitors();
1008                 SETMONBOOL(remove_disabled_monitors)
1009                 SETMONBOOL(remove_unplugged_monitors)
1010                 SETMONBOOL(merge_overlapping_monitors)
1011 #undef SETMONBOOL
1012         } else {
1013                 return MSG_FAILURE;
1014         }
1015
1016         for (monitor_t *m = mon_head; m != NULL; m = m->next)
1017                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1018                         arrange(m, d);
1019
1020         return MSG_SUCCESS;
1021 }
1022
1023 int get_setting(coordinates_t loc, char *name, FILE* rsp)
1024 {
1025         if (streq("split_ratio", name))
1026                 fprintf(rsp, "%lf", split_ratio);
1027         else if (streq("window_gap", name))
1028                 if (loc.desktop != NULL)
1029                         fprintf(rsp, "%i", loc.desktop->window_gap);
1030                 else
1031                         fprintf(rsp, "%i", window_gap);
1032         else if (streq("border_width", name))
1033                 if (loc.node != NULL)
1034                         fprintf(rsp, "%u", loc.node->client->border_width);
1035                 else if (loc.desktop != NULL)
1036                         fprintf(rsp, "%u", loc.desktop->border_width);
1037                 else
1038                         fprintf(rsp, "%u", border_width);
1039         else if (streq("external_rules_command", name))
1040                 fprintf(rsp, "%s", external_rules_command);
1041         else if (streq("status_prefix", name))
1042                 fprintf(rsp, "%s", status_prefix);
1043 #define MONDESKGET(k) \
1044         else if (streq(#k, name)) \
1045                 if (loc.desktop != NULL) \
1046                         fprintf(rsp, "%i", loc.desktop->k); \
1047                 else if (loc.monitor != NULL) \
1048                         fprintf(rsp, "%i", loc.monitor->k); \
1049                 else \
1050                         return MSG_FAILURE;
1051         MONDESKGET(top_padding)
1052         MONDESKGET(right_padding)
1053         MONDESKGET(bottom_padding)
1054         MONDESKGET(left_padding)
1055 #undef DESKGET
1056 #define GETCOLOR(s) \
1057         else if (streq(#s, name)) \
1058                 fprintf(rsp, "%s", s);
1059         GETCOLOR(focused_border_color)
1060         GETCOLOR(active_border_color)
1061         GETCOLOR(normal_border_color)
1062         GETCOLOR(presel_border_color)
1063         GETCOLOR(focused_locked_border_color)
1064         GETCOLOR(active_locked_border_color)
1065         GETCOLOR(normal_locked_border_color)
1066         GETCOLOR(focused_sticky_border_color)
1067         GETCOLOR(active_sticky_border_color)
1068         GETCOLOR(normal_sticky_border_color)
1069         GETCOLOR(urgent_border_color)
1070 #undef GETCOLOR
1071 #define GETBOOL(s) \
1072         else if (streq(#s, name)) \
1073                 fprintf(rsp, "%s", BOOLSTR(s));
1074         GETBOOL(borderless_monocle)
1075         GETBOOL(gapless_monocle)
1076         GETBOOL(focus_follows_pointer)
1077         GETBOOL(pointer_follows_monitor)
1078         GETBOOL(apply_floating_atom)
1079         GETBOOL(auto_alternate)
1080         GETBOOL(auto_cancel)
1081         GETBOOL(history_aware_focus)
1082         GETBOOL(focus_by_distance)
1083         GETBOOL(ignore_ewmh_focus)
1084         GETBOOL(remove_disabled_monitors)
1085         GETBOOL(remove_unplugged_monitors)
1086         GETBOOL(merge_overlapping_monitors)
1087 #undef GETBOOL
1088         else
1089                 return MSG_FAILURE;
1090         return MSG_SUCCESS;
1091 }
1092
1093 bool parse_bool(char *value, bool *b)
1094 {
1095         if (streq("true", value) || streq("on", value)) {
1096                 *b = true;
1097                 return true;
1098         } else if (streq("false", value) || streq("off", value)) {
1099                 *b = false;
1100                 return true;
1101         }
1102         return false;
1103 }
1104
1105 bool parse_layout(char *s, layout_t *l)
1106 {
1107         if (streq("monocle", s)) {
1108                 *l = LAYOUT_MONOCLE;
1109                 return true;
1110         } else if (streq("tiled", s)) {
1111                 *l = LAYOUT_TILED;
1112                 return true;
1113         }
1114         return false;
1115 }
1116
1117 bool parse_direction(char *s, direction_t *d)
1118 {
1119         if (streq("right", s)) {
1120                 *d = DIR_RIGHT;
1121                 return true;
1122         } else if (streq("down", s)) {
1123                 *d = DIR_DOWN;
1124                 return true;
1125         } else if (streq("left", s)) {
1126                 *d = DIR_LEFT;
1127                 return true;
1128         } else if (streq("up", s)) {
1129                 *d = DIR_UP;
1130                 return true;
1131         }
1132         return false;
1133 }
1134
1135 bool parse_cycle_direction(char *s, cycle_dir_t *d)
1136 {
1137         if (streq("next", s)) {
1138                 *d = CYCLE_NEXT;
1139                 return true;
1140         } else if (streq("prev", s)) {
1141                 *d = CYCLE_PREV;
1142                 return true;
1143         }
1144         return false;
1145 }
1146
1147 bool parse_circulate_direction(char *s, circulate_dir_t *d)
1148 {
1149         if (streq("forward", s)) {
1150                 *d = CIRCULATE_FORWARD;
1151                 return true;
1152         } else if (streq("backward", s)) {
1153                 *d = CIRCULATE_BACKWARD;
1154                 return true;
1155         }
1156         return false;
1157 }
1158
1159 bool parse_history_direction(char *s, history_dir_t *d)
1160 {
1161         if (streq("older", s)) {
1162                 *d = HISTORY_OLDER;
1163                 return true;
1164         } else if (streq("newer", s)) {
1165                 *d = HISTORY_NEWER;
1166                 return true;
1167         }
1168         return false;
1169 }
1170
1171
1172 bool parse_flip(char *s, flip_t *f)
1173 {
1174         if (streq("horizontal", s)) {
1175                 *f = FLIP_HORIZONTAL;
1176                 return true;
1177         } else if (streq("vertical", s)) {
1178                 *f = FLIP_VERTICAL;
1179                 return true;
1180         }
1181         return false;
1182 }
1183
1184 bool parse_pointer_action(char *s, pointer_action_t *a)
1185 {
1186         if (streq("move", s)) {
1187                 *a = ACTION_MOVE;
1188                 return true;
1189         } else if (streq("resize_corner", s)) {
1190                 *a = ACTION_RESIZE_CORNER;
1191                 return true;
1192         } else if (streq("resize_side", s)) {
1193                 *a = ACTION_RESIZE_SIDE;
1194                 return true;
1195         } else if (streq("focus", s)) {
1196                 *a = ACTION_FOCUS;
1197                 return true;
1198         }
1199         return false;
1200 }
1201
1202 bool parse_degree(char *s, int *d)
1203 {
1204         int i = atoi(s);
1205         while (i < 0)
1206                 i += 360;
1207         while (i > 359)
1208                 i -= 360;
1209         if ((i % 90) != 0) {
1210                 return false;
1211         } else {
1212                 *d = i;
1213                 return true;
1214         }
1215 }
1216
1217 bool parse_window_id(char *s, long int *i)
1218 {
1219         char *end;
1220         errno = 0;
1221         long int ret = strtol(s, &end, 0);
1222         if (errno != 0 || *end != '\0')
1223                 return false;
1224         else
1225                 *i = ret;
1226         return true;
1227 }
1228
1229 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
1230 {
1231         *key = strtok(s, EQL_TOK);
1232         char *v = strtok(NULL, EQL_TOK);
1233         if (v == NULL) {
1234                 *state = ALTER_TOGGLE;
1235                 return true;
1236         } else {
1237                 if (parse_bool(v, value)) {
1238                         *state = ALTER_SET;
1239                         return true;
1240                 } else {
1241                         return false;
1242                 }
1243         }
1244         return false;
1245 }
1246
1247 bool parse_index(char *s, int *i)
1248 {
1249         int idx;
1250         if (sscanf(s, "^%i", &idx) != 1 || idx < 1)
1251                 return false;
1252         *i = idx;
1253         return true;
1254 }