]> git.lizzy.rs Git - bspwm.git/blob - src/messages.c
Add setting: presel_feedback
[bspwm.git] / src / messages.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <stdarg.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32 #include "bspwm.h"
33 #include "desktop.h"
34 #include "monitor.h"
35 #include "pointer.h"
36 #include "query.h"
37 #include "rule.h"
38 #include "restore.h"
39 #include "settings.h"
40 #include "tree.h"
41 #include "window.h"
42 #include "common.h"
43 #include "parse.h"
44 #include "messages.h"
45
46 void handle_message(char *msg, int msg_len, FILE *rsp)
47 {
48         int cap = INIT_CAP;
49         int num = 0;
50         char **args = calloc(cap, sizeof(char *));
51
52         if (args == NULL) {
53                 perror("Handle message: calloc");
54                 return;
55         }
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                                 perror("Handle message: realloc");
68                                 return;
69                         } else {
70                                 args = new;
71                         }
72                 }
73         }
74
75         if (num < 1) {
76                 free(args);
77                 fail(rsp, "No arguments given.\n");
78                 return;
79         }
80
81         char **args_orig = args;
82         process_message(args, num, rsp);
83         free(args_orig);
84 }
85
86 void process_message(char **args, int num, FILE *rsp)
87 {
88         if (streq("node", *args)) {
89                 cmd_node(++args, --num, rsp);
90         } else if (streq("desktop", *args)) {
91                 cmd_desktop(++args, --num, rsp);
92         } else if (streq("monitor", *args)) {
93                 cmd_monitor(++args, --num, rsp);
94         } else if (streq("query", *args)) {
95                 cmd_query(++args, --num, rsp);
96         } else if (streq("subscribe", *args)) {
97                 cmd_subscribe(++args, --num, rsp);
98                 return;
99         } else if (streq("wm", *args)) {
100                 cmd_wm(++args, --num, rsp);
101         } else if (streq("rule", *args)) {
102                 cmd_rule(++args, --num, rsp);
103         } else if (streq("config", *args)) {
104                 cmd_config(++args, --num, rsp);
105         } else if (streq("quit", *args)) {
106                 cmd_quit(++args, --num, rsp);
107         } else {
108                 fail(rsp, "Unknown domain or command: '%s'.\n", *args);
109         }
110
111         fflush(rsp);
112         fclose(rsp);
113 }
114
115 void cmd_node(char **args, int num, FILE *rsp)
116 {
117         if (num < 1) {
118                 fail(rsp, "node: Missing arguments.\n");
119                 return;
120         }
121
122         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
123         coordinates_t trg = ref;
124
125         if ((*args)[0] != OPT_CHR) {
126                 int ret;
127                 if ((ret = node_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
128                         num--, args++;
129                 } else {
130                         handle_failure(ret, "node", *args, rsp);
131                         return;
132                 }
133         }
134
135         if (num < 1) {
136                 fail(rsp, "node: Missing commands.\n");
137                 return;
138         }
139
140         bool changed = false;
141
142         while (num > 0) {
143                 if (streq("-f", *args) || streq("--focus", *args)) {
144                         coordinates_t dst = trg;
145                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
146                                 num--, args++;
147                                 int ret;
148                                 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
149                                         handle_failure(ret, "node -f", *args, rsp);
150                                         break;
151                                 }
152                         }
153                         if (dst.node == NULL || !focus_node(dst.monitor, dst.desktop, dst.node)) {
154                                 fail(rsp, "");
155                                 break;
156                         }
157                 } else if (streq("-a", *args) || streq("--activate", *args)) {
158                         coordinates_t dst = trg;
159                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
160                                 num--, args++;
161                                 int ret;
162                                 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
163                                         handle_failure(ret, "node -a", *args, rsp);
164                                         break;
165                                 }
166                         }
167                         if (dst.node == NULL || !activate_node(dst.monitor, dst.desktop, dst.node)) {
168                                 fail(rsp, "");
169                                 break;
170                         }
171                 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
172                         num--, args++;
173                         if (num < 1) {
174                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
175                                 break;
176                         }
177                         coordinates_t dst;
178                         int ret;
179                         if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
180                                 bool follow = false;
181                                 if (num > 1 && streq("--follow", *(args+1))) {
182                                         follow = true;
183                                         num--, args++;
184                                 }
185                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus, follow)) {
186                                         trg.monitor = dst.monitor;
187                                         trg.desktop = dst.desktop;
188                                 } else {
189                                         fail(rsp, "");
190                                         break;
191                                 }
192                         } else {
193                                 handle_failure(ret, "node -d", *args, rsp);
194                                 break;
195                         }
196                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
197                         num--, args++;
198                         if (num < 1) {
199                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
200                                 break;
201                         }
202                         coordinates_t dst;
203                         int ret;
204                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
205                                 bool follow = false;
206                                 if (num > 1 && streq("--follow", *(args+1))) {
207                                         follow = true;
208                                         num--, args++;
209                                 }
210                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus, follow)) {
211                                         trg.monitor = dst.monitor;
212                                         trg.desktop = dst.monitor->desk;
213                                 } else {
214                                         fail(rsp, "");
215                                         break;
216                                 }
217                         } else {
218                                 handle_failure(ret, "node -m", *args, rsp);
219                                 break;
220                         }
221                 } else if (streq("-n", *args) || streq("--to-node", *args)) {
222                         num--, args++;
223                         if (num < 1) {
224                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
225                                 break;
226                         }
227                         coordinates_t dst;
228                         int ret;
229                         if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
230                                 bool follow = false;
231                                 if (num > 1 && streq("--follow", *(args+1))) {
232                                         follow = true;
233                                         num--, args++;
234                                 }
235                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
236                                         trg.monitor = dst.monitor;
237                                         trg.desktop = dst.desktop;
238                                 } else {
239                                         fail(rsp, "");
240                                         break;
241                                 }
242                         } else {
243                                 handle_failure(ret, "node -n", *args, rsp);
244                                 break;
245                         }
246                 } else if (streq("-s", *args) || streq("--swap", *args)) {
247                         num--, args++;
248                         if (num < 1) {
249                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
250                                 break;
251                         }
252                         coordinates_t dst;
253                         int ret;
254                         if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
255                                 bool follow = false;
256                                 if (num > 1 && streq("--follow", *(args+1))) {
257                                         follow = true;
258                                         num--, args++;
259                                 }
260                                 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
261                                         trg.monitor = dst.monitor;
262                                         trg.desktop = dst.desktop;
263                                 } else {
264                                         fail(rsp, "");
265                                         break;
266                                 }
267                         } else {
268                                 handle_failure(ret, "node -s", *args, rsp);
269                                 break;
270                         }
271                 } else if (streq("-l", *args) || streq("--layer", *args)) {
272                         num--, args++;
273                         if (num < 1) {
274                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
275                                 break;
276                         }
277                         stack_layer_t lyr;
278                         if (parse_stack_layer(*args, &lyr)) {
279                                 if (!set_layer(trg.monitor, trg.desktop, trg.node, lyr)) {
280                                         fail(rsp, "");
281                                         break;
282                                 }
283                         } else {
284                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
285                                 break;
286                         }
287                 } else if (streq("-t", *args) || streq("--state", *args)) {
288                         num--, args++;
289                         if (num < 1) {
290                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
291                                 break;
292                         }
293                         client_state_t cst;
294                         bool alternate = false;
295                         if ((*args)[0] == '~') {
296                                 alternate = true;
297                                 (*args)++;
298                         }
299                         if (parse_client_state(*args, &cst)) {
300                                 if (alternate && trg.node != NULL && trg.node->client != NULL &&
301                                     trg.node->client->state == cst) {
302                                         cst = trg.node->client->last_state;
303                                 }
304                                 if (!set_state(trg.monitor, trg.desktop, trg.node, cst)) {
305                                         fail(rsp, "");
306                                         break;
307                                 }
308                                 changed = true;
309                         } else {
310                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
311                                 break;
312                         }
313                 } else if (streq("-g", *args) || streq("--flag", *args)) {
314                         num--, args++;
315                         if (num < 1) {
316                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
317                                 break;
318                         }
319                         if (trg.node == NULL) {
320                                 fail(rsp, "");
321                                 break;
322                         }
323                         char *key = strtok(*args, EQL_TOK);
324                         char *val = strtok(NULL, EQL_TOK);
325                         alter_state_t a;
326                         bool b;
327                         if (val == NULL) {
328                                 a = ALTER_TOGGLE;
329                         } else {
330                                 if (parse_bool(val, &b)) {
331                                         a = ALTER_SET;
332                                 } else {
333                                         fail(rsp, "node %s: Invalid value for %s: '%s'.\n", *(args - 1), key, val);
334                                         break;
335                                 }
336                         }
337                         if (streq("hidden", key)) {
338                                 set_hidden(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->hidden));
339                                 changed = true;
340                         } else if (streq("sticky", key)) {
341                                 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->sticky));
342                         } else if (streq("private", key)) {
343                                 set_private(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->private));
344                         } else if (streq("locked", key)) {
345                                 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->locked));
346                         } else if (streq("marked", key)) {
347                                 set_marked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->marked));
348                         } else {
349                                 fail(rsp, "node %s: Invalid key: '%s'.\n", *(args - 1), key);
350                                 break;
351                         }
352                 } else if (streq("-p", *args) || streq("--presel-dir", *args)) {
353                         num--, args++;
354                         if (num < 1) {
355                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
356                                 break;
357                         }
358                         if (trg.node == NULL || trg.node->vacant) {
359                                 fail(rsp, "");
360                                 break;
361                         }
362                         if (streq("cancel", *args)) {
363                                 cancel_presel(trg.monitor, trg.desktop, trg.node);
364                         } else {
365                                 bool alternate = false;
366                                 if ((*args)[0] == '~') {
367                                         alternate = true;
368                                         (*args)++;
369                                 }
370                                 direction_t dir;
371                                 if (parse_direction(*args, &dir)) {
372                                         if (alternate && trg.node->presel != NULL && trg.node->presel->split_dir == dir) {
373                                                 cancel_presel(trg.monitor, trg.desktop, trg.node);
374                                         } else {
375                                                 presel_dir(trg.monitor, trg.desktop, trg.node, dir);
376                                                 if (!IS_RECEPTACLE(trg.node)) {
377                                                         draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
378                                                 }
379                                         }
380                                 } else {
381                                         fail(rsp, "node %s: Invalid argument: '%s%s'.\n", *(args - 1), alternate?"~":"", *args);
382                                         break;
383                                 }
384                         }
385                 } else if (streq("-o", *args) || streq("--presel-ratio", *args)) {
386                         num--, args++;
387                         if (num < 1) {
388                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
389                                 break;
390                         }
391                         if (trg.node == NULL || trg.node->vacant) {
392                                 fail(rsp, "");
393                                 break;
394                         }
395                         double rat;
396                         if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1) {
397                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
398                                 break;
399                         } else {
400                                 presel_ratio(trg.monitor, trg.desktop, trg.node, rat);
401                                 draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
402                         }
403                 } else if (streq("-v", *args) || streq("--move", *args)) {
404                         num--, args++;
405                         if (num < 2) {
406                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
407                                 break;
408                         }
409                         int dx = 0, dy = 0;
410                         if (sscanf(*args, "%i", &dx) == 1) {
411                                 num--, args++;
412                                 if (sscanf(*args, "%i", &dy) == 1) {
413                                         if (!move_client(&trg, dx, dy)) {
414                                                 fail(rsp, "");
415                                                 break;
416                                         }
417                                 } else {
418                                         fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
419                                         break;
420                                 }
421                         } else {
422                                 fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
423                                 break;
424                         }
425                 } else if (streq("-z", *args) || streq("--resize", *args)) {
426                         num--, args++;
427                         if (num < 3) {
428                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
429                                 break;
430                         }
431                         resize_handle_t rh;
432                         if (parse_resize_handle(*args, &rh)) {
433                                 num--, args++;
434                                 int dx = 0, dy = 0;
435                                 if (sscanf(*args, "%i", &dx) == 1) {
436                                         num--, args++;
437                                         if (sscanf(*args, "%i", &dy) == 1) {
438                                                 if (!resize_client(&trg, rh, dx, dy, true)) {
439                                                         fail(rsp, "");
440                                                         break;
441                                                 }
442                                         } else {
443                                                 fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
444                                                 break;
445                                         }
446                                 } else {
447                                         fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
448                                         break;
449                                 }
450                         } else {
451                                 fail(rsp, "node %s: Invalid resize handle argument: '%s'.\n", *(args - 1), *args);
452                                 break;
453                         }
454                 } else if (streq("-r", *args) || streq("--ratio", *args)) {
455                         num--, args++;
456                         if (num < 1) {
457                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
458                                 break;
459                         }
460                         if (trg.node == NULL) {
461                                 fail(rsp, "");
462                                 break;
463                         }
464                         if ((*args)[0] == '+' || (*args)[0] == '-') {
465                                 float delta;
466                                 if (sscanf(*args, "%f", &delta) == 1) {
467                                         double rat = trg.node->split_ratio;
468                                         if (delta > -1 && delta < 1) {
469                                                 rat += delta;
470                                         } else {
471                                                 int max = (trg.node->split_type == TYPE_HORIZONTAL ? trg.node->rectangle.height : trg.node->rectangle.width);
472                                                 rat = ((max * rat) + delta) / max;
473                                         }
474                                         if (rat > 0 && rat < 1) {
475                                                 set_ratio(trg.node, rat);
476                                         } else {
477                                                 fail(rsp, "");
478                                                 break;
479                                         }
480                                 } else {
481                                         fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
482                                         break;
483                                 }
484                         } else {
485                                 double rat;
486                                 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
487                                         set_ratio(trg.node, rat);
488                                 } else {
489                                         fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
490                                         break;
491                                 }
492                         }
493                         changed = true;
494                 } else if (streq("-F", *args) || streq("--flip", *args)) {
495                         num--, args++;
496                         if (num < 1) {
497                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
498                                 break;
499                         }
500                         if (trg.node == NULL) {
501                                 fail(rsp, "");
502                                 break;
503                         }
504                         flip_t flp;
505                         if (parse_flip(*args, &flp)) {
506                                 flip_tree(trg.node, flp);
507                                 changed = true;
508                         } else {
509                                 fail(rsp, "");
510                                 break;
511                         }
512                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
513                         num--, args++;
514                         if (num < 1) {
515                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
516                                 break;
517                         }
518                         if (trg.node == NULL) {
519                                 fail(rsp, "");
520                                 break;
521                         }
522                         int deg;
523                         if (parse_degree(*args, &deg)) {
524                                 rotate_tree(trg.node, deg);
525                                 changed = true;
526                         } else {
527                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
528                                 break;
529                         }
530                 } else if (streq("-E", *args) || streq("--equalize", *args)) {
531                         if (trg.node == NULL) {
532                                 fail(rsp, "");
533                                 break;
534                         }
535                         equalize_tree(trg.node);
536                         changed = true;
537                 } else if (streq("-B", *args) || streq("--balance", *args)) {
538                         if (trg.node == NULL) {
539                                 fail(rsp, "");
540                                 break;
541                         }
542                         balance_tree(trg.node);
543                         changed = true;
544                 } else if (streq("-C", *args) || streq("--circulate", *args)) {
545                         num--, args++;
546                         if (num < 1) {
547                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
548                                 break;
549                         }
550                         if (trg.node == NULL) {
551                                 fail(rsp, "");
552                                 break;
553                         }
554                         circulate_dir_t cir;
555                         if (parse_circulate_direction(*args, &cir)) {
556                                 circulate_leaves(trg.monitor, trg.desktop, trg.node, cir);
557                                 changed = true;
558                         } else {
559                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
560                                 break;
561                         }
562                 } else if (streq("-i", *args) || streq("--insert-receptacle", *args)) {
563                         insert_receptacle(trg.monitor, trg.desktop, trg.node);
564                         changed = true;
565                 } else if (streq("-c", *args) || streq("--close", *args)) {
566                         if (num > 1) {
567                                 fail(rsp, "node %s: Trailing commands.\n", *args);
568                                 break;
569                         }
570                         if (trg.node == NULL || locked_count(trg.node) > 0) {
571                                 fail(rsp, "");
572                                 break;
573                         }
574                         close_node(trg.node);
575                         break;
576                 } else if (streq("-k", *args) || streq("--kill", *args)) {
577                         if (num > 1) {
578                                 fail(rsp, "node %s: Trailing commands.\n", *args);
579                                 break;
580                         }
581                         if (trg.node == NULL) {
582                                 fail(rsp, "");
583                                 break;
584                         }
585                         kill_node(trg.monitor, trg.desktop, trg.node);
586                         changed = true;
587                         break;
588                 } else {
589                         fail(rsp, "node: Unknown command: '%s'.\n", *args);
590                         break;
591                 }
592
593                 num--, args++;
594         }
595
596         if (changed) {
597                 arrange(trg.monitor, trg.desktop);
598         }
599 }
600
601 void cmd_desktop(char **args, int num, FILE *rsp)
602 {
603         if (num < 1) {
604                 fail(rsp, "desktop: Missing arguments.\n");
605                 return;
606         }
607
608         coordinates_t ref = {mon, mon->desk, NULL};
609         coordinates_t trg = ref;
610
611         if ((*args)[0] != OPT_CHR) {
612                 int ret;
613                 if ((ret = desktop_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
614                         num--, args++;
615                 } else {
616                         handle_failure(ret, "desktop", *args, rsp);
617                         return;
618                 }
619         }
620
621         if (num < 1) {
622                 fail(rsp, "desktop: Missing commands.\n");
623                 return;
624         }
625
626         bool changed = false;
627
628         while (num > 0) {
629                 if (streq("-f", *args) || streq("--focus", *args)) {
630                         coordinates_t dst = trg;
631                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
632                                 num--, args++;
633                                 int ret;
634                                 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
635                                         handle_failure(ret, "desktop -f", *args, rsp);
636                                         break;
637                                 }
638                         }
639                         focus_node(dst.monitor, dst.desktop, NULL);
640                 } else if (streq("-a", *args) || streq("--activate", *args)) {
641                         coordinates_t dst = trg;
642                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
643                                 num--, args++;
644                                 int ret;
645                                 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
646                                         handle_failure(ret, "desktop -a", *args, rsp);
647                                         break;
648                                 }
649                         }
650                         if (activate_desktop(dst.monitor, dst.desktop)) {
651                                 activate_node(dst.monitor, dst.desktop, NULL);
652                         } else {
653                                 fail(rsp, "");
654                                 break;
655                         }
656                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
657                         num--, args++;
658                         if (num < 1) {
659                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
660                                 break;
661                         }
662                         if (trg.monitor->desk_head == trg.monitor->desk_tail) {
663                                 fail(rsp, "");
664                                 break;
665                         }
666                         coordinates_t dst;
667                         int ret;
668                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
669                                 bool follow = false;
670                                 if (num > 1 && streq("--follow", *(args+1))) {
671                                         follow = true;
672                                         num--, args++;
673                                 }
674                                 if (transfer_desktop(trg.monitor, dst.monitor, trg.desktop, follow)) {
675                                         trg.monitor = dst.monitor;
676                                 } else {
677                                         fail(rsp, "");
678                                         break;
679                                 }
680                         } else {
681                                 handle_failure(ret, "desktop -m", *args, rsp);
682                                 break;
683                         }
684                 } else if (streq("-s", *args) || streq("--swap", *args)) {
685                         num--, args++;
686                         if (num < 1) {
687                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
688                                 break;
689                         }
690                         coordinates_t dst;
691                         int ret;
692                         if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
693                                 bool follow = false;
694                                 if (num > 1 && streq("--follow", *(args+1))) {
695                                         follow = true;
696                                         num--, args++;
697                                 }
698                                 if (swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop, follow)) {
699                                         trg.monitor = dst.monitor;
700                                 } else {
701                                         fail(rsp, "");
702                                         break;
703                                 }
704                         } else {
705                                 handle_failure(ret, "desktop -s", *args, rsp);
706                                 break;
707                         }
708                 } else if (streq("-b", *args) || streq("--bubble", *args)) {
709                         num--, args++;
710                         if (num < 1) {
711                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
712                                 break;
713                         }
714                         cycle_dir_t cyc;
715                         if (parse_cycle_direction(*args, &cyc)) {
716                                 desktop_t *d = trg.desktop;
717                                 if (cyc == CYCLE_PREV) {
718                                         if (d->prev == NULL) {
719                                                 while (d->next != NULL) {
720                                                         swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
721                                                 }
722                                         } else {
723                                                 swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
724                                         }
725                                 } else {
726                                         if (d->next == NULL) {
727                                                 while (d->prev != NULL) {
728                                                         swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
729                                                 }
730                                         } else {
731                                                 swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
732                                         }
733                                 }
734                         } else {
735                                 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
736                                 break;
737                         }
738                 } else if (streq("-l", *args) || streq("--layout", *args)) {
739                         num--, args++;
740                         if (num < 1) {
741                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
742                                 break;
743                         }
744                         bool ret;
745                         layout_t lyt;
746                         cycle_dir_t cyc;
747                         if (parse_cycle_direction(*args, &cyc)) {
748                                 ret = set_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
749                         } else if (parse_layout(*args, &lyt)) {
750                                 ret = set_layout(trg.monitor, trg.desktop, lyt);
751                         } else {
752                                 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
753                                 break;
754                         }
755                         if (!ret) {
756                                 fail(rsp, "");
757                                 break;
758                         }
759                 } else if (streq("-n", *args) || streq("--rename", *args)) {
760                         num--, args++;
761                         if (num < 1) {
762                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
763                                 break;
764                         }
765                         rename_desktop(trg.monitor, trg.desktop, *args);
766                 } else if (streq("-r", *args) || streq("--remove", *args)) {
767                         if (num > 1) {
768                                 fail(rsp, "desktop %s: Trailing commands.\n", *args);
769                                 break;
770                         }
771                         if (trg.monitor->desk_head != trg.monitor->desk_tail) {
772                                 desktop_t *fallback = trg.desktop->prev == NULL ?
773                                                       trg.desktop->next :
774                                                       trg.desktop->prev;
775                                 merge_desktops(trg.monitor, trg.desktop, trg.monitor, fallback);
776                                 remove_desktop(trg.monitor, trg.desktop);
777                                 return;
778                         } else {
779                                 fail(rsp, "");
780                                 break;
781                         }
782                 } else {
783                         fail(rsp, "desktop: Unknown command: '%s'.\n", *args);
784                         break;
785                 }
786                 num--, args++;
787         }
788
789         if (changed) {
790                 arrange(trg.monitor, trg.desktop);
791         }
792 }
793
794 void cmd_monitor(char **args, int num, FILE *rsp)
795 {
796         if (num < 1) {
797                 fail(rsp, "monitor: Missing arguments.\n");
798                 return;
799         }
800
801         coordinates_t ref = {mon, NULL, NULL};
802         coordinates_t trg = ref;
803
804         if ((*args)[0] != OPT_CHR) {
805                 int ret;
806                 if ((ret = monitor_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
807                         num--, args++;
808                 } else {
809                         handle_failure(ret, "monitor", *args, rsp);
810                         return;
811                 }
812         }
813
814         if (num < 1) {
815                 fail(rsp, "monitor: Missing commands.\n");
816                 return;
817         }
818
819         while (num > 0) {
820                 if (streq("-f", *args) || streq("--focus", *args)) {
821                         coordinates_t dst = trg;
822                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
823                                 num--, args++;
824                                 int ret;
825                                 if ((ret = monitor_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
826                                         handle_failure(ret, "monitor -f", *args, rsp);
827                                         fail(rsp, "");
828                                         return;
829                                 }
830                         }
831                         focus_node(dst.monitor, NULL, NULL);
832                 } else if (streq("-s", *args) || streq("--swap", *args)) {
833                         num--, args++;
834                         if (num < 1) {
835                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
836                                 return;
837                         }
838                         coordinates_t dst;
839                         int ret;
840                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
841                                 if (!swap_monitors(trg.monitor, dst.monitor)) {
842                                         fail(rsp, "");
843                                         return;
844                                 }
845                         } else {
846                                 handle_failure(ret, "monitor -s", *args, rsp);
847                                 return;
848                         }
849                 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
850                         num--, args++;
851                         if (num < 1) {
852                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
853                                 return;
854                         }
855                         desktop_t *d = trg.monitor->desk_head;
856                         while (num > 0 && d != NULL) {
857                                 rename_desktop(trg.monitor, d, *args);
858                                 d = d->next;
859                                 num--, args++;
860                         }
861                         put_status(SBSC_MASK_REPORT);
862                         while (num > 0) {
863                                 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
864                                 num--, args++;
865                         }
866                         while (d != NULL) {
867                                 desktop_t *next = d->next;
868                                 if (d == mon->desk) {
869                                         focus_node(trg.monitor, d->prev, d->prev->focus);
870                                 }
871                                 merge_desktops(trg.monitor, d, mon, mon->desk);
872                                 remove_desktop(trg.monitor, d);
873                                 d = next;
874                         }
875                 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
876                         num--, args++;
877                         if (num < 1) {
878                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
879                                 return;
880                         }
881                         while (num > 0) {
882                                 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
883                                 num--, args++;
884                         }
885                 } else if (streq("-r", *args) || streq("--remove", *args)) {
886                         if (num > 1) {
887                                 fail(rsp, "monitor %s: Trailing commands.\n", *args);
888                                 return;
889                         }
890                         if (mon_head == mon_tail) {
891                                 fail(rsp, "");
892                                 return;
893                         }
894                         remove_monitor(trg.monitor);
895                         return;
896                 } else if (streq("-o", *args) || streq("--reorder-desktops", *args)) {
897                         num--, args++;
898                         if (num < 1) {
899                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
900                                 return;
901                         }
902                         desktop_t *d = trg.monitor->desk_head;
903                         while (d != NULL && num > 0) {
904                                 desktop_t *next = d->next;
905                                 coordinates_t dst;
906                                 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
907                                         swap_desktops(trg.monitor, d, dst.monitor, dst.desktop, false);
908                                         if (next == dst.desktop) {
909                                                 next = d;
910                                         }
911                                 }
912                                 d = next;
913                                 num--, args++;
914                         }
915                 } else if (streq("-g", *args) || streq("--rectangle", *args)) {
916                         num--, args++;
917                         if (num < 1) {
918                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
919                                 return;
920                         }
921                         xcb_rectangle_t r;
922                         if (parse_rectangle(*args, &r)) {
923                                 update_root(trg.monitor, &r);
924                         } else {
925                                 fail(rsp, "monitor %s: Invalid argument: '%s'.\n", *(args - 1), *args);
926                                 return;
927                         }
928                 } else if (streq("-n", *args) || streq("--rename", *args)) {
929                         num--, args++;
930                         if (num < 1) {
931                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
932                                 return;
933                         }
934                         rename_monitor(trg.monitor, *args);
935                 } else {
936                         fail(rsp, "monitor: Unknown command: '%s'.\n", *args);
937                         return;
938                 }
939                 num--, args++;
940         }
941 }
942
943 void cmd_query(char **args, int num, FILE *rsp)
944 {
945         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
946         coordinates_t trg = {NULL, NULL, NULL};
947         monitor_select_t *monitor_sel = NULL;
948         desktop_select_t *desktop_sel = NULL;
949         node_select_t *node_sel = NULL;
950         domain_t dom = DOMAIN_TREE;
951         bool print_ids = true;
952         uint8_t d = 0;
953
954         if (num < 1) {
955                 fail(rsp, "query: Missing arguments.\n");
956                 return;
957         }
958
959         while (num > 0) {
960                 if (streq("-T", *args) || streq("--tree", *args)) {
961                         dom = DOMAIN_TREE, d++;
962                 } else if (streq("-M", *args) || streq("--monitors", *args)) {
963                         dom = DOMAIN_MONITOR, d++;
964                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
965                                 num--, args++;
966                                 int ret;
967                                 coordinates_t tmp = ref;
968                                 if ((ret = monitor_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
969                                         handle_failure(ret, "query -M", *args, rsp);
970                                         goto end;
971                                 }
972                         }
973                 } else if (streq("-D", *args) || streq("--desktops", *args)) {
974                         dom = DOMAIN_DESKTOP, d++;
975                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
976                                 num--, args++;
977                                 int ret;
978                                 coordinates_t tmp = ref;
979                                 if ((ret = desktop_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
980                                         handle_failure(ret, "query -D", *args, rsp);
981                                         goto end;
982                                 }
983                         }
984                 } else if (streq("-N", *args) || streq("--nodes", *args)) {
985                         dom = DOMAIN_NODE, d++;
986                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
987                                 num--, args++;
988                                 int ret;
989                                 coordinates_t tmp = ref;
990                                 if ((ret = node_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
991                                         handle_failure(ret, "query -N", *args, rsp);
992                                         goto end;
993                                 }
994                         }
995                 } else if (streq("-m", *args) || streq("--monitor", *args)) {
996                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
997                                 num--, args++;
998                                 int ret;
999                                 if ((*args)[0] == '.') {
1000                                         free(monitor_sel);
1001                                         monitor_sel = malloc(sizeof(monitor_select_t));
1002                                         *monitor_sel = make_monitor_select();
1003                                         char *desc = copy_string(*args, strlen(*args));
1004                                         if (!parse_monitor_modifiers(desc, monitor_sel)) {
1005                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -m", *args, rsp);
1006                                                 free(desc);
1007                                                 goto end;
1008                                         }
1009                                         free(desc);
1010                                 } else if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1011                                         handle_failure(ret, "query -m", *args, rsp);
1012                                         goto end;
1013                                 }
1014                         } else {
1015                                 trg.monitor = ref.monitor;
1016                         }
1017                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1018                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1019                                 num--, args++;
1020                                 int ret;
1021                                 if ((*args)[0] == '.') {
1022                                         free(desktop_sel);
1023                                         desktop_sel = malloc(sizeof(desktop_select_t));
1024                                         *desktop_sel = make_desktop_select();
1025                                         char *desc = copy_string(*args, strlen(*args));
1026                                         if (!parse_desktop_modifiers(desc, desktop_sel)) {
1027                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -d", *args, rsp);
1028                                                 free(desc);
1029                                                 goto end;
1030                                         }
1031                                         free(desc);
1032                                 } else if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1033                                         handle_failure(ret, "query -d", *args, rsp);
1034                                         goto end;
1035                                 }
1036                         } else {
1037                                 trg.monitor = ref.monitor;
1038                                 trg.desktop = ref.desktop;
1039                         }
1040                 } else if (streq("-n", *args) || streq("--node", *args)) {
1041                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1042                                 num--, args++;
1043                                 int ret;
1044                                 if ((*args)[0] == '.') {
1045                                         free(node_sel);
1046                                         node_sel = malloc(sizeof(node_select_t));
1047                                         *node_sel = make_node_select();
1048                                         char *desc = copy_string(*args, strlen(*args));
1049                                         if (!parse_node_modifiers(desc, node_sel)) {
1050                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -n", *args, rsp);
1051                                                 free(desc);
1052                                                 goto end;
1053                                         }
1054                                         free(desc);
1055                                 } else if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1056                                         handle_failure(ret, "query -n", *args, rsp);
1057                                         goto end;
1058                                 }
1059                         } else {
1060                                 trg = ref;
1061                                 if (ref.node == NULL) {
1062                                         fail(rsp, "");
1063                                         goto end;
1064                                 }
1065                         }
1066                 } else if (streq("--names", *args)) {
1067                         print_ids = false;
1068                 } else {
1069                         fail(rsp, "query: Unknown option: '%s'.\n", *args);
1070                         goto end;
1071                 }
1072                 num--, args++;
1073         }
1074
1075         if (d < 1) {
1076                 fail(rsp, "query: No commands given.\n");
1077                 goto end;
1078         }
1079
1080         if (d > 1) {
1081                 fail(rsp, "query: Multiple commands given.\n");
1082                 goto end;
1083         }
1084
1085         if (dom == DOMAIN_TREE && trg.monitor == NULL) {
1086                 fail(rsp, "query -T: No options given.\n");
1087                 goto end;
1088         }
1089
1090         if (!print_ids && (dom == DOMAIN_NODE || dom == DOMAIN_TREE)) {
1091                 fail(rsp, "query -%c: --names only applies to -M and -D.\n", dom == DOMAIN_NODE ? 'N' : 'T');
1092                 goto end;
1093         }
1094
1095         if (dom == DOMAIN_NODE) {
1096                 if (query_node_ids(&ref, &trg, node_sel, rsp) < 1) {
1097                         fail(rsp, "");
1098                 }
1099         } else if (dom == DOMAIN_DESKTOP) {
1100                 if (query_desktop_ids(&ref, &trg, desktop_sel, print_ids ? fprint_desktop_id : fprint_desktop_name, rsp) < 1) {
1101                         fail(rsp, "");
1102                 }
1103         } else if (dom == DOMAIN_MONITOR) {
1104                 if (query_monitor_ids(&ref, &trg, monitor_sel, print_ids ? fprint_monitor_id : fprint_monitor_name, rsp) < 1) {
1105                         fail(rsp, "");
1106                 }
1107         } else {
1108                 if (trg.node != NULL) {
1109                         query_node(trg.node, rsp);
1110                 } else if (trg.desktop != NULL) {
1111                         query_desktop(trg.desktop, rsp);
1112                 } else  {
1113                         query_monitor(trg.monitor, rsp);
1114                 }
1115                 fprintf(rsp, "\n");
1116         }
1117
1118 end:
1119         free(monitor_sel);
1120         free(desktop_sel);
1121         free(node_sel);
1122 }
1123
1124 void cmd_rule(char **args, int num, FILE *rsp)
1125 {
1126         if (num < 1) {
1127                 fail(rsp, "rule: Missing commands.\n");
1128                 return;
1129         }
1130
1131         while (num > 0) {
1132                 if (streq("-a", *args) || streq("--add", *args)) {
1133                         num--, args++;
1134                         if (num < 2) {
1135                                 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1136                                 return;
1137                         }
1138                         rule_t *rule = make_rule();
1139                         char *class_name = strtok(*args, COL_TOK);
1140                         char *instance_name = strtok(NULL, COL_TOK);
1141                         snprintf(rule->class_name, sizeof(rule->class_name), "%s", class_name);
1142                         snprintf(rule->instance_name, sizeof(rule->instance_name), "%s", instance_name==NULL?MATCH_ANY:instance_name);
1143                         num--, args++;
1144                         size_t i = 0;
1145                         while (num > 0) {
1146                                 if (streq("-o", *args) || streq("--one-shot", *args)) {
1147                                         rule->one_shot = true;
1148                                 } else {
1149                                         for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++) {
1150                                                 rule->effect[i] = (*args)[j];
1151                                         }
1152                                         if (num > 1 && i < sizeof(rule->effect)) {
1153                                                 rule->effect[i++] = ' ';
1154                                         }
1155                                 }
1156                                 num--, args++;
1157                         }
1158                         rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
1159                         add_rule(rule);
1160                 } else if (streq("-r", *args) || streq("--remove", *args)) {
1161                         num--, args++;
1162                         if (num < 1) {
1163                                 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1164                                 return;
1165                         }
1166                         uint16_t idx;
1167                         while (num > 0) {
1168                                 if (parse_index(*args, &idx)) {
1169                                         remove_rule_by_index(idx - 1);
1170                                 } else if (streq("tail", *args)) {
1171                                         remove_rule(rule_tail);
1172                                 } else if (streq("head", *args)) {
1173                                         remove_rule(rule_head);
1174                                 } else {
1175                                         remove_rule_by_cause(*args);
1176                                 }
1177                                 num--, args++;
1178                         }
1179                 } else if (streq("-l", *args) || streq("--list", *args)) {
1180                         list_rules(rsp);
1181                 } else {
1182                         fail(rsp, "rule: Unknown command: '%s'.\n", *args);
1183                         return;
1184                 }
1185                 num--, args++;
1186         }
1187 }
1188
1189 void cmd_wm(char **args, int num, FILE *rsp)
1190 {
1191         if (num < 1) {
1192                 fail(rsp, "wm: Missing commands.\n");
1193                 return;
1194         }
1195
1196         while (num > 0) {
1197                 if (streq("-d", *args) || streq("--dump-state", *args)) {
1198                         query_tree(rsp);
1199                         fprintf(rsp, "\n");
1200                 } else if (streq("-l", *args) || streq("--load-state", *args)) {
1201                         num--, args++;
1202                         if (num < 1) {
1203                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1204                                 break;
1205                         }
1206                         if (!restore_tree(*args)) {
1207                                 fail(rsp, "");
1208                                 break;
1209                         }
1210                 } else if (streq("-a", *args) || streq("--add-monitor", *args)) {
1211                         num--, args++;
1212                         if (num < 2) {
1213                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1214                                 break;
1215                         }
1216                         char *name = *args;
1217                         num--, args++;
1218                         xcb_rectangle_t r;
1219                         if (parse_rectangle(*args, &r)) {
1220                                 monitor_t *m = make_monitor(name, &r, XCB_NONE);
1221                                 add_monitor(m);
1222                                 add_desktop(m, make_desktop(NULL, XCB_NONE));
1223                         } else {
1224                                 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1225                                 break;
1226                         }
1227                 } else if (streq("-O", *args) || streq("--reorder-monitors", *args)) {
1228                         num--, args++;
1229                         if (num < 1) {
1230                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1231                                 return;
1232                         }
1233                         monitor_t *m = mon_head;
1234                         while (m != NULL && num > 0) {
1235                                 monitor_t *next = m->next;
1236                                 coordinates_t dst;
1237                                 if (locate_monitor(*args, &dst)) {
1238                                         swap_monitors(m, dst.monitor);
1239                                         if (next == dst.monitor) {
1240                                                 next = m;
1241                                         }
1242                                 }
1243                                 m = next;
1244                                 num--, args++;
1245                         }
1246                 } else if (streq("-o", *args) || streq("--adopt-orphans", *args)) {
1247                         adopt_orphans();
1248                 } else if (streq("-g", *args) || streq("--get-status", *args)) {
1249                         print_report(rsp);
1250                 } else if (streq("-h", *args) || streq("--record-history", *args)) {
1251                         num--, args++;
1252                         if (num < 1) {
1253                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1254                                 break;
1255                         }
1256                         bool b;
1257                         if (parse_bool(*args, &b)) {
1258                                 record_history = b;
1259                         } else {
1260                                 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1261                                 break;
1262                         }
1263                 } else {
1264                         fail(rsp, "wm: Unknown command: '%s'.\n", *args);
1265                         break;
1266                 }
1267                 num--, args++;
1268         }
1269 }
1270
1271 void cmd_subscribe(char **args, int num, FILE *rsp)
1272 {
1273         int field = 0;
1274         int count = -1;
1275         FILE *stream = rsp;
1276         char *fifo_path = NULL;
1277         subscriber_mask_t mask;
1278
1279         while (num > 0) {
1280                 if (streq("-c", *args) || streq("--count", *args)) {
1281                         num--, args++;
1282                         if (num < 1) {
1283                                 fail(rsp, "subscribe %s: Not enough arguments.\n", *(args - 1));
1284                                 goto failed;
1285                         }
1286                         if (sscanf(*args, "%i", &count) != 1 || count < 1) {
1287                                 fail(rsp, "subscribe %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1288                                 goto failed;
1289                         }
1290                 } else if (streq("-f", *args) || streq("--fifo", *args)) {
1291                         fifo_path = mktempfifo(FIFO_TEMPLATE);
1292                         if (fifo_path == NULL) {
1293                                 fail(rsp, "subscribe %s: Can't create FIFO.\n", *(args - 1));
1294                                 goto failed;
1295                         }
1296                 } else if (parse_subscriber_mask(*args, &mask)) {
1297                         field |= mask;
1298                 } else {
1299                         fail(rsp, "subscribe: Invalid argument: '%s'.\n", *args);
1300                         goto failed;
1301                 }
1302                 num--, args++;
1303         }
1304
1305         if (field == 0) {
1306                 field = SBSC_MASK_REPORT;
1307         }
1308
1309         if (fifo_path) {
1310                 fprintf(rsp, "%s\n", fifo_path);
1311                 fflush(rsp);
1312                 fclose(rsp);
1313
1314                 stream = fopen(fifo_path, "w");
1315
1316                 if (stream == NULL) {
1317                         perror("subscribe: fopen");
1318                         goto free_fifo_path;
1319                 }
1320         }
1321
1322         add_subscriber(stream, fifo_path, field, count);
1323         return;
1324
1325 failed:
1326         fflush(rsp);
1327         fclose(rsp);
1328
1329 free_fifo_path:
1330         if (fifo_path) {
1331                 unlink(fifo_path);
1332                 free(fifo_path);
1333         }
1334 }
1335
1336 void cmd_quit(char **args, int num, FILE *rsp)
1337 {
1338         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1) {
1339                 fail(rsp, "%s: Invalid argument: '%s'.\n", *(args - 1), *args);
1340                 return;
1341         }
1342         running = false;
1343 }
1344
1345 void cmd_config(char **args, int num, FILE *rsp)
1346 {
1347         if (num < 1) {
1348                 fail(rsp, "config: Missing arguments.\n");
1349                 return;
1350         }
1351
1352         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
1353         coordinates_t trg = {NULL, NULL, NULL};
1354
1355         while (num > 0 && (*args)[0] == OPT_CHR) {
1356                 if (streq("-m", *args) || streq("--monitor", *args)) {
1357                         num--, args++;
1358                         if (num < 1) {
1359                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1360                                 return;
1361                         }
1362                         int ret;
1363                         if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1364                                 handle_failure(ret, "config -m", *args, rsp);
1365                                 return;
1366                         }
1367                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1368                         num--, args++;
1369                         if (num < 1) {
1370                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1371                                 return;
1372                         }
1373                         int ret;
1374                         if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1375                                 handle_failure(ret, "config -d", *args, rsp);
1376                                 return;
1377                         }
1378                 } else if (streq("-n", *args) || streq("--node", *args)) {
1379                         num--, args++;
1380                         if (num < 1) {
1381                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1382                                 return;
1383                         }
1384                         int ret;
1385                         if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1386                                 handle_failure(ret, "config -n", *args, rsp);
1387                                 return;
1388                         }
1389                 } else {
1390                         fail(rsp, "config: Unknown option: '%s'.\n", *args);
1391                         return;
1392                 }
1393                 num--, args++;
1394         }
1395         if (num == 2) {
1396                 set_setting(trg, *args, *(args + 1), rsp);
1397         } else if (num == 1) {
1398                 get_setting(trg, *args, rsp);
1399         } else {
1400                 fail(rsp, "config: Was expecting 1 or 2 arguments, received %i.\n", num);
1401         }
1402 }
1403
1404 void set_setting(coordinates_t loc, char *name, char *value, FILE *rsp)
1405 {
1406         bool colors_changed = false;
1407 #define SET_DEF_DEFMON_DEFDESK_WIN(k, v) \
1408                 if (loc.node != NULL) { \
1409                         for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) { \
1410                                 if (n->client != NULL) { \
1411                                         n->client->k = v; \
1412                                 } \
1413                         } \
1414                 } else if (loc.desktop != NULL) { \
1415                         loc.desktop->k = v; \
1416                         for (node_t *n = first_extrema(loc.desktop->root); n != NULL; n = next_leaf(n, loc.desktop->root)) { \
1417                                 if (n->client != NULL) { \
1418                                         n->client->k = v; \
1419                                 } \
1420                         } \
1421                 } else if (loc.monitor != NULL) { \
1422                         loc.monitor->k = v; \
1423                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1424                                 d->k = v; \
1425                                 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1426                                         if (n->client != NULL) { \
1427                                                 n->client->k = v; \
1428                                         } \
1429                                 } \
1430                         } \
1431                 } else { \
1432                         k = v; \
1433                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1434                                 m->k = v; \
1435                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1436                                         d->k = v; \
1437                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1438                                                 if (n->client != NULL) { \
1439                                                         n->client->k = v; \
1440                                                 } \
1441                                         } \
1442                                 } \
1443                         } \
1444                 }
1445         if (streq("border_width", name)) {
1446                 unsigned int bw;
1447                 if (sscanf(value, "%u", &bw) != 1) {
1448                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1449                         return;
1450                 }
1451                 SET_DEF_DEFMON_DEFDESK_WIN(border_width, bw)
1452 #undef SET_DEF_DEFMON_DEFDESK_WIN
1453 #define SET_DEF_DEFMON_DESK(k, v) \
1454                 if (loc.desktop != NULL) { \
1455                         loc.desktop->k = v; \
1456                 } else if (loc.monitor != NULL) { \
1457                         loc.monitor->k = v; \
1458                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1459                                 d->k = v; \
1460                         } \
1461                 } else { \
1462                         k = v; \
1463                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1464                                 m->k = v; \
1465                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1466                                         d->k = v; \
1467                                 } \
1468                         } \
1469                 }
1470         } else if (streq("window_gap", name)) {
1471                 int wg;
1472                 if (sscanf(value, "%i", &wg) != 1) {
1473                         fail(rsp, "");
1474                         return;
1475                 }
1476                 SET_DEF_DEFMON_DESK(window_gap, wg)
1477 #undef SET_DEF_DEFMON_DESK
1478 #define SET_DEF_MON_DESK(k, v) \
1479                 if (loc.desktop != NULL) { \
1480                         loc.desktop->k = v; \
1481                 } else if (loc.monitor != NULL) { \
1482                         loc.monitor->k = v; \
1483                 } else { \
1484                         k = v; \
1485                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1486                                 m->k = v; \
1487                         } \
1488                 }
1489         } else if (streq("top_padding", name)) {
1490                 int tp;
1491                 if (sscanf(value, "%i", &tp) != 1) {
1492                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1493                         return;
1494                 }
1495                 SET_DEF_MON_DESK(padding.top, tp)
1496         } else if (streq("right_padding", name)) {
1497                 int rp;
1498                 if (sscanf(value, "%i", &rp) != 1) {
1499                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1500                         return;
1501                 }
1502                 SET_DEF_MON_DESK(padding.right, rp)
1503         } else if (streq("bottom_padding", name)) {
1504                 int bp;
1505                 if (sscanf(value, "%i", &bp) != 1) {
1506                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1507                         return;
1508                 }
1509                 SET_DEF_MON_DESK(padding.bottom, bp)
1510         } else if (streq("left_padding", name)) {
1511                 int lp;
1512                 if (sscanf(value, "%i", &lp) != 1) {
1513                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1514                         return;
1515                 }
1516                 SET_DEF_MON_DESK(padding.left, lp)
1517 #undef SET_DEF_MON_DESK
1518         } else if (streq("top_monocle_padding", name)) {
1519                 if (sscanf(value, "%i", &monocle_padding.top) != 1) {
1520                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1521                 }
1522         } else if (streq("right_monocle_padding", name)) {
1523                 if (sscanf(value, "%i", &monocle_padding.right) != 1) {
1524                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1525                 }
1526         } else if (streq("bottom_monocle_padding", name)) {
1527                 if (sscanf(value, "%i", &monocle_padding.bottom) != 1) {
1528                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1529                 }
1530         } else if (streq("left_monocle_padding", name)) {
1531                 if (sscanf(value, "%i", &monocle_padding.left) != 1) {
1532                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1533                 }
1534 #define SET_STR(s) \
1535         } else if (streq(#s, name)) { \
1536                 if (snprintf(s, sizeof(s), "%s", value) < 0) { \
1537                         fail(rsp, ""); \
1538                         return; \
1539                 }
1540         SET_STR(external_rules_command)
1541         SET_STR(status_prefix)
1542 #undef SET_STR
1543         } else if (streq("split_ratio", name)) {
1544                 double r;
1545                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1) {
1546                         split_ratio = r;
1547                 } else {
1548                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1549                         return;
1550                 }
1551                 return;
1552 #define SET_COLOR(s) \
1553         } else if (streq(#s, name)) { \
1554                 if (!is_hex_color(value)) { \
1555                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1556                         return; \
1557                 } else { \
1558                         snprintf(s, sizeof(s), "%s", value); \
1559                         colors_changed = true; \
1560                 }
1561         SET_COLOR(normal_border_color)
1562         SET_COLOR(active_border_color)
1563         SET_COLOR(focused_border_color)
1564         SET_COLOR(presel_feedback_color)
1565 #undef SET_COLOR
1566         } else if (streq("initial_polarity", name)) {
1567                 child_polarity_t p;
1568                 if (parse_child_polarity(value, &p)) {
1569                         initial_polarity = p;
1570                 } else {
1571                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1572                         return;
1573                 }
1574         } else if (streq("automatic_scheme", name)) {
1575                 automatic_scheme_t a;
1576                 if (parse_automatic_scheme(value, &a)) {
1577                         automatic_scheme = a;
1578                 } else {
1579                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1580                         return;
1581                 }
1582         } else if (streq("mapping_events_count", name)) {
1583                 if (sscanf(value, "%" SCNi8, &mapping_events_count) != 1) {
1584                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1585                         return;
1586                 }
1587         } else if (streq("directional_focus_tightness", name)) {
1588                 tightness_t p;
1589                 if (parse_tightness(value, &p)) {
1590                         directional_focus_tightness = p;
1591                 } else {
1592                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1593                         return;
1594                 }
1595         } else if (streq("ignore_ewmh_fullscreen", name)) {
1596                 state_transition_t m;
1597                 if (parse_state_transition(value, &m)) {
1598                         ignore_ewmh_fullscreen = m;
1599                 } else {
1600                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1601                         return;
1602                 }
1603         } else if (streq("pointer_modifier", name)) {
1604                 if (parse_modifier_mask(value, &pointer_modifier)) {
1605                         ungrab_buttons();
1606                         grab_buttons();
1607                 } else {
1608                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1609                         return;
1610                 }
1611         } else if (streq("pointer_motion_interval", name)) {
1612                 if (sscanf(value, "%u", &pointer_motion_interval) != 1) {
1613                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1614                         return;
1615                 }
1616         } else if (streq("pointer_action1", name) ||
1617                    streq("pointer_action2", name) ||
1618                    streq("pointer_action3", name)) {
1619                 int index = name[14] - '1';
1620                 if (parse_pointer_action(value, &pointer_actions[index])) {
1621                         ungrab_buttons();
1622                         grab_buttons();
1623                 } else {
1624                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1625                         return;
1626                 }
1627         } else if (streq("click_to_focus", name)) {
1628                 if (parse_button_index(value, &click_to_focus)) {
1629                         ungrab_buttons();
1630                         grab_buttons();
1631                 } else {
1632                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1633                         return;
1634                 }
1635         } else if (streq("focus_follows_pointer", name)) {
1636                 bool b;
1637                 if (parse_bool(value, &b)) {
1638                         if (b == focus_follows_pointer) {
1639                                 fail(rsp, "");
1640                                 return;
1641                         }
1642                         focus_follows_pointer = b;
1643                         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1644                                 if (focus_follows_pointer) {
1645                                         window_show(m->root);
1646                                 } else {
1647                                         window_hide(m->root);
1648                                 }
1649                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1650                                         listen_enter_notify(d->root, focus_follows_pointer);
1651                                 }
1652                         }
1653                         if (focus_follows_pointer) {
1654                                 update_motion_recorder();
1655                         } else {
1656                                 disable_motion_recorder();
1657                         }
1658                         return;
1659                 } else {
1660                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1661                         return;
1662                 }
1663 #define SET_BOOL(s) \
1664         } else if (streq(#s, name)) { \
1665                 if (!parse_bool(value, &s)) { \
1666                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1667                         return; \
1668                 }
1669                 SET_BOOL(presel_feedback)
1670                 SET_BOOL(borderless_monocle)
1671                 SET_BOOL(gapless_monocle)
1672                 SET_BOOL(single_monocle)
1673                 SET_BOOL(swallow_first_click)
1674                 SET_BOOL(pointer_follows_focus)
1675                 SET_BOOL(pointer_follows_monitor)
1676                 SET_BOOL(ignore_ewmh_focus)
1677                 SET_BOOL(ignore_ewmh_struts)
1678                 SET_BOOL(center_pseudo_tiled)
1679                 SET_BOOL(honor_size_hints)
1680                 SET_BOOL(removal_adjustment)
1681 #undef SET_BOOL
1682 #define SET_MON_BOOL(s) \
1683         } else if (streq(#s, name)) { \
1684                 if (!parse_bool(value, &s)) { \
1685                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1686                         return; \
1687                 } \
1688                 if (s) { \
1689                         update_monitors(); \
1690                 }
1691                 SET_MON_BOOL(remove_disabled_monitors)
1692                 SET_MON_BOOL(remove_unplugged_monitors)
1693                 SET_MON_BOOL(merge_overlapping_monitors)
1694 #undef SET_MON_BOOL
1695         } else {
1696                 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1697                 return;
1698         }
1699
1700         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1701                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1702                         arrange(m, d);
1703                         if (colors_changed) {
1704                                 update_colors_in(d->root, d, m);
1705                         }
1706                 }
1707         }
1708 }
1709
1710 void get_setting(coordinates_t loc, char *name, FILE* rsp)
1711 {
1712         if (streq("split_ratio", name)) {
1713                 fprintf(rsp, "%lf", split_ratio);
1714         } else if (streq("border_width", name)) {
1715                 if (loc.node != NULL) {
1716                         for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) {
1717                                 if (n->client != NULL) {
1718                                         fprintf(rsp, "%u", n->client->border_width);
1719                                         break;
1720                                 }
1721                         }
1722                 } else if (loc.desktop != NULL) {
1723                         fprintf(rsp, "%u", loc.desktop->border_width);
1724                 } else if (loc.monitor != NULL) {
1725                         fprintf(rsp, "%u", loc.monitor->border_width);
1726                 } else {
1727                         fprintf(rsp, "%u", border_width);
1728                 }
1729         } else if (streq("window_gap", name)) {
1730                 if (loc.desktop != NULL) {
1731                         fprintf(rsp, "%i", loc.desktop->window_gap);
1732                 } else if (loc.monitor != NULL) {
1733                         fprintf(rsp, "%i", loc.monitor->window_gap);
1734                 } else {
1735                         fprintf(rsp, "%i", window_gap);
1736                 }
1737 #define GET_DEF_MON_DESK(k) \
1738                 if (loc.desktop != NULL) { \
1739                         fprintf(rsp, "%i", loc.desktop->k); \
1740                 } else if (loc.monitor != NULL) { \
1741                         fprintf(rsp, "%i", loc.monitor->k); \
1742                 } else { \
1743                         fprintf(rsp, "%i", k); \
1744                 }
1745         } else if (streq("top_padding", name)) {
1746                 GET_DEF_MON_DESK(padding.top)
1747         } else if (streq("right_padding", name)) {
1748                 GET_DEF_MON_DESK(padding.right)
1749         } else if (streq("bottom_padding", name)) {
1750                 GET_DEF_MON_DESK(padding.bottom)
1751         } else if (streq("left_padding", name)) {
1752                 GET_DEF_MON_DESK(padding.left)
1753 #undef GET_DEF_MON_DESK
1754         } else if (streq("top_monocle_padding", name)) {
1755                 fprintf(rsp, "%i", monocle_padding.top);
1756         } else if (streq("right_monocle_padding", name)) {
1757                 fprintf(rsp, "%i", monocle_padding.right);
1758         } else if (streq("bottom_monocle_padding", name)) {
1759                 fprintf(rsp, "%i", monocle_padding.bottom);
1760         } else if (streq("left_monocle_padding", name)) {
1761                 fprintf(rsp, "%i", monocle_padding.left);
1762         } else if (streq("external_rules_command", name)) {
1763                 fprintf(rsp, "%s", external_rules_command);
1764         } else if (streq("status_prefix", name)) {
1765                 fprintf(rsp, "%s", status_prefix);
1766         } else if (streq("initial_polarity", name)) {
1767                 fprintf(rsp, "%s", CHILD_POL_STR(initial_polarity));
1768         } else if (streq("automatic_scheme", name)) {
1769                 fprintf(rsp, "%s", AUTO_SCM_STR(automatic_scheme));
1770         } else if (streq("mapping_events_count", name)) {
1771                 fprintf(rsp, "%" PRIi8, mapping_events_count);
1772         } else if (streq("directional_focus_tightness", name)) {
1773                 fprintf(rsp, "%s", TIGHTNESS_STR(directional_focus_tightness));
1774         } else if (streq("ignore_ewmh_fullscreen", name)) {
1775                 print_ignore_request(ignore_ewmh_fullscreen, rsp);
1776         } else if (streq("pointer_modifier", name)) {
1777                 print_modifier_mask(pointer_modifier, rsp);
1778         } else if (streq("click_to_focus", name)) {
1779                 print_button_index(click_to_focus, rsp);
1780         } else if (streq("pointer_motion_interval", name)) {
1781                 fprintf(rsp, "%u", pointer_motion_interval);
1782         } else if (streq("pointer_action1", name) ||
1783                    streq("pointer_action2", name) ||
1784                    streq("pointer_action3", name)) {
1785                 int index = name[14] - '1';
1786                 print_pointer_action(pointer_actions[index], rsp);
1787 #define GET_COLOR(s) \
1788         } else if (streq(#s, name)) { \
1789                 fprintf(rsp, "%s", s);
1790         GET_COLOR(normal_border_color)
1791         GET_COLOR(active_border_color)
1792         GET_COLOR(focused_border_color)
1793         GET_COLOR(presel_feedback_color)
1794 #undef GET_COLOR
1795 #define GET_BOOL(s) \
1796         } else if (streq(#s, name)) { \
1797                 fprintf(rsp, "%s", BOOL_STR(s));
1798         GET_BOOL(presel_feedback)
1799         GET_BOOL(borderless_monocle)
1800         GET_BOOL(gapless_monocle)
1801         GET_BOOL(single_monocle)
1802         GET_BOOL(swallow_first_click)
1803         GET_BOOL(focus_follows_pointer)
1804         GET_BOOL(pointer_follows_focus)
1805         GET_BOOL(pointer_follows_monitor)
1806         GET_BOOL(ignore_ewmh_focus)
1807         GET_BOOL(ignore_ewmh_struts)
1808         GET_BOOL(center_pseudo_tiled)
1809         GET_BOOL(honor_size_hints)
1810         GET_BOOL(removal_adjustment)
1811         GET_BOOL(remove_disabled_monitors)
1812         GET_BOOL(remove_unplugged_monitors)
1813         GET_BOOL(merge_overlapping_monitors)
1814 #undef GET_BOOL
1815         } else {
1816                 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1817                 return;
1818         }
1819         fprintf(rsp, "\n");
1820 }
1821
1822 void handle_failure(int code, char *src, char *val, FILE *rsp)
1823 {
1824         switch (code) {
1825                 case SELECTOR_BAD_DESCRIPTOR:
1826                         fail(rsp, "%s: Invalid descriptor found in '%s'.\n", src, val);
1827                         break;
1828                 case SELECTOR_BAD_MODIFIERS:
1829                         fail(rsp, "%s: Invalid modifier found in '%s'.\n", src, val);
1830                         break;
1831                 case SELECTOR_INVALID:
1832                         fail(rsp, "");
1833                         break;
1834         }
1835 }
1836
1837 void fail(FILE *rsp, char *fmt, ...)
1838 {
1839         fprintf(rsp, FAILURE_MESSAGE);
1840         va_list ap;
1841         va_start(ap, fmt);
1842         vfprintf(rsp, fmt, ap);
1843         va_end(ap);
1844 }