]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Improve error messages on failing commands
[i3/i3] / src / commands.c
1 #undef I3__FILE__
2 #define I3__FILE__ "commands.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * commands.c: all command functions (see commands_parser.c)
10  *
11  */
12 #include <float.h>
13 #include <stdarg.h>
14
15 #include "all.h"
16 #include "shmlog.h"
17
18 // Macros to make the YAJL API a bit easier to use.
19 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
20 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
21 #define ysuccess(success)                   \
22     do {                                    \
23         if (cmd_output->json_gen != NULL) { \
24             y(map_open);                    \
25             ystr("success");                \
26             y(bool, success);               \
27             y(map_close);                   \
28         }                                   \
29     } while (0)
30 #define yerror(format, ...)                             \
31     do {                                                \
32         if (cmd_output->json_gen != NULL) {             \
33             char *message;                              \
34             sasprintf(&message, format, ##__VA_ARGS__); \
35             y(map_open);                                \
36             ystr("success");                            \
37             y(bool, false);                             \
38             ystr("error");                              \
39             ystr(message);                              \
40             y(map_close);                               \
41             free(message);                              \
42         }                                               \
43     } while (0)
44
45 /** When the command did not include match criteria (!), we use the currently
46  * focused container. Do not confuse this case with a command which included
47  * criteria but which did not match any windows. This macro has to be called in
48  * every command.
49  */
50 #define HANDLE_EMPTY_MATCH                              \
51     do {                                                \
52         if (match_is_empty(current_match)) {            \
53             owindow *ow = smalloc(sizeof(owindow));     \
54             ow->con = focused;                          \
55             TAILQ_INIT(&owindows);                      \
56             TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
57         }                                               \
58     } while (0)
59
60 /*
61  * Returns true if a is definitely greater than b (using the given epsilon)
62  *
63  */
64 static bool definitelyGreaterThan(float a, float b, float epsilon) {
65     return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
66 }
67
68 /*
69  * Returns an 'output' corresponding to one of left/right/down/up or a specific
70  * output name.
71  *
72  */
73 static Output *get_output_from_string(Output *current_output, const char *output_str) {
74     Output *output;
75
76     if (strcasecmp(output_str, "left") == 0)
77         output = get_output_next_wrap(D_LEFT, current_output);
78     else if (strcasecmp(output_str, "right") == 0)
79         output = get_output_next_wrap(D_RIGHT, current_output);
80     else if (strcasecmp(output_str, "up") == 0)
81         output = get_output_next_wrap(D_UP, current_output);
82     else if (strcasecmp(output_str, "down") == 0)
83         output = get_output_next_wrap(D_DOWN, current_output);
84     else
85         output = get_output_by_name(output_str);
86
87     return output;
88 }
89
90 /*
91  * Returns the output containing the given container.
92  */
93 static Output *get_output_of_con(Con *con) {
94     Con *output_con = con_get_output(con);
95     Output *output = get_output_by_name(output_con->name);
96     assert(output != NULL);
97
98     return output;
99 }
100
101 /*
102  * Checks whether we switched to a new workspace and returns false in that case,
103  * signaling that further workspace switching should be done by the calling function
104  * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
105  * and return true, signaling that no further workspace switching should occur in the calling function.
106  *
107  */
108 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
109     Con *ws = con_get_workspace(focused);
110
111     /* If we switched to a different workspace, do nothing */
112     if (strcmp(ws->name, name) != 0)
113         return false;
114
115     DLOG("This workspace is already focused.\n");
116     if (config.workspace_auto_back_and_forth) {
117         workspace_back_and_forth();
118         cmd_output->needs_tree_render = true;
119     }
120     return true;
121 }
122
123 /*
124  * Return the passed workspace unless it is the current one and auto back and
125  * forth is enabled, in which case the back_and_forth workspace is returned.
126  */
127 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
128     Con *current, *baf;
129
130     if (!config.workspace_auto_back_and_forth)
131         return workspace;
132
133     current = con_get_workspace(focused);
134
135     if (current == workspace) {
136         baf = workspace_back_and_forth_get();
137         if (baf != NULL) {
138             DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
139             return baf;
140         }
141     }
142
143     return workspace;
144 }
145
146 // This code is commented out because we might recycle it for popping up error
147 // messages on parser errors.
148 #if 0
149 static pid_t migration_pid = -1;
150
151 /*
152  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
153  * it exited (or could not be started, depending on the exit code).
154  *
155  */
156 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
157     ev_child_stop(EV_A_ watcher);
158     if (!WIFEXITED(watcher->rstatus)) {
159         fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
160         return;
161     }
162
163     int exitcode = WEXITSTATUS(watcher->rstatus);
164     printf("i3-nagbar process exited with status %d\n", exitcode);
165     if (exitcode == 2) {
166         fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
167     }
168
169     migration_pid = -1;
170 }
171
172 /* We need ev >= 4 for the following code. Since it is not *that* important (it
173  * only makes sure that there are no i3-nagbar instances left behind) we still
174  * support old systems with libev 3. */
175 #if EV_VERSION_MAJOR >= 4
176 /*
177  * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
178  * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
179  *
180  */
181 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
182     if (migration_pid != -1) {
183         LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
184         kill(migration_pid, SIGKILL);
185     }
186 }
187 #endif
188
189 void cmd_MIGRATION_start_nagbar(void) {
190     if (migration_pid != -1) {
191         fprintf(stderr, "i3-nagbar already running.\n");
192         return;
193     }
194     fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
195     ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
196     ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
197     ELOG("FYI: Your i3 version is " I3_VERSION "\n");
198     migration_pid = fork();
199     if (migration_pid == -1) {
200         warn("Could not fork()");
201         return;
202     }
203
204     /* child */
205     if (migration_pid == 0) {
206         char *pageraction;
207         sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
208         char *argv[] = {
209             NULL, /* will be replaced by the executable path */
210             "-t",
211             "error",
212             "-m",
213             "You found a parsing error. Please, please, please, report it!",
214             "-b",
215             "show errors",
216             pageraction,
217             NULL
218         };
219         exec_i3_utility("i3-nagbar", argv);
220     }
221
222     /* parent */
223     /* install a child watcher */
224     ev_child *child = smalloc(sizeof(ev_child));
225     ev_child_init(child, &nagbar_exited, migration_pid, 0);
226     ev_child_start(main_loop, child);
227
228 /* We need ev >= 4 for the following code. Since it is not *that* important (it
229  * only makes sure that there are no i3-nagbar instances left behind) we still
230  * support old systems with libev 3. */
231 #if EV_VERSION_MAJOR >= 4
232     /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
233      * still running) */
234     ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
235     ev_cleanup_init(cleanup, nagbar_cleanup);
236     ev_cleanup_start(main_loop, cleanup);
237 #endif
238 }
239
240 #endif
241
242 /*******************************************************************************
243  * Criteria functions.
244  ******************************************************************************/
245
246 /*
247  * Helper data structure for an operation window (window on which the operation
248  * will be performed). Used to build the TAILQ owindows.
249  *
250  */
251 typedef struct owindow {
252     Con *con;
253     TAILQ_ENTRY(owindow) owindows;
254 } owindow;
255
256 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
257
258 static owindows_head owindows;
259
260 /*
261  * Initializes the specified 'Match' data structure and the initial state of
262  * commands.c for matching target windows of a command.
263  *
264  */
265 void cmd_criteria_init(I3_CMD) {
266     Con *con;
267     owindow *ow;
268
269     DLOG("Initializing criteria, current_match = %p\n", current_match);
270     match_free(current_match);
271     match_init(current_match);
272     while (!TAILQ_EMPTY(&owindows)) {
273         ow = TAILQ_FIRST(&owindows);
274         TAILQ_REMOVE(&owindows, ow, owindows);
275         free(ow);
276     }
277     TAILQ_INIT(&owindows);
278     /* copy all_cons */
279     TAILQ_FOREACH(con, &all_cons, all_cons) {
280         ow = smalloc(sizeof(owindow));
281         ow->con = con;
282         TAILQ_INSERT_TAIL(&owindows, ow, owindows);
283     }
284 }
285
286 /*
287  * A match specification just finished (the closing square bracket was found),
288  * so we filter the list of owindows.
289  *
290  */
291 void cmd_criteria_match_windows(I3_CMD) {
292     owindow *next, *current;
293
294     DLOG("match specification finished, matching...\n");
295     /* copy the old list head to iterate through it and start with a fresh
296      * list which will contain only matching windows */
297     struct owindows_head old = owindows;
298     TAILQ_INIT(&owindows);
299     for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
300         /* make a copy of the next pointer and advance the pointer to the
301          * next element as we are going to invalidate the element’s
302          * next/prev pointers by calling TAILQ_INSERT_TAIL later */
303         current = next;
304         next = TAILQ_NEXT(next, owindows);
305
306         DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
307         if (current_match->con_id != NULL) {
308             if (current_match->con_id == current->con) {
309                 DLOG("matches container!\n");
310                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
311             } else {
312                 DLOG("doesnt match\n");
313                 free(current);
314             }
315         } else if (current_match->mark != NULL && current->con->mark != NULL &&
316                    regex_matches(current_match->mark, current->con->mark)) {
317             DLOG("match by mark\n");
318             TAILQ_INSERT_TAIL(&owindows, current, owindows);
319         } else {
320             if (current->con->window && match_matches_window(current_match, current->con->window)) {
321                 DLOG("matches window!\n");
322                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
323             } else {
324                 DLOG("doesnt match\n");
325                 free(current);
326             }
327         }
328     }
329
330     TAILQ_FOREACH(current, &owindows, owindows) {
331         DLOG("matching: %p / %s\n", current->con, current->con->name);
332     }
333 }
334
335 /*
336  * Interprets a ctype=cvalue pair and adds it to the current match
337  * specification.
338  *
339  */
340 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
341     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
342
343     if (strcmp(ctype, "class") == 0) {
344         current_match->class = regex_new(cvalue);
345         return;
346     }
347
348     if (strcmp(ctype, "instance") == 0) {
349         current_match->instance = regex_new(cvalue);
350         return;
351     }
352
353     if (strcmp(ctype, "window_role") == 0) {
354         current_match->window_role = regex_new(cvalue);
355         return;
356     }
357
358     if (strcmp(ctype, "con_id") == 0) {
359         char *end;
360         long parsed = strtol(cvalue, &end, 10);
361         if (parsed == LONG_MIN ||
362             parsed == LONG_MAX ||
363             parsed < 0 ||
364             (end && *end != '\0')) {
365             ELOG("Could not parse con id \"%s\"\n", cvalue);
366         } else {
367             current_match->con_id = (Con *)parsed;
368             DLOG("id as int = %p\n", current_match->con_id);
369         }
370         return;
371     }
372
373     if (strcmp(ctype, "id") == 0) {
374         char *end;
375         long parsed = strtol(cvalue, &end, 10);
376         if (parsed == LONG_MIN ||
377             parsed == LONG_MAX ||
378             parsed < 0 ||
379             (end && *end != '\0')) {
380             ELOG("Could not parse window id \"%s\"\n", cvalue);
381         } else {
382             current_match->id = parsed;
383             DLOG("window id as int = %d\n", current_match->id);
384         }
385         return;
386     }
387
388     if (strcmp(ctype, "con_mark") == 0) {
389         current_match->mark = regex_new(cvalue);
390         return;
391     }
392
393     if (strcmp(ctype, "title") == 0) {
394         current_match->title = regex_new(cvalue);
395         return;
396     }
397
398     if (strcmp(ctype, "urgent") == 0) {
399         if (strcasecmp(cvalue, "latest") == 0 ||
400             strcasecmp(cvalue, "newest") == 0 ||
401             strcasecmp(cvalue, "recent") == 0 ||
402             strcasecmp(cvalue, "last") == 0) {
403             current_match->urgent = U_LATEST;
404         } else if (strcasecmp(cvalue, "oldest") == 0 ||
405                    strcasecmp(cvalue, "first") == 0) {
406             current_match->urgent = U_OLDEST;
407         }
408         return;
409     }
410
411     ELOG("Unknown criterion: %s\n", ctype);
412 }
413
414 /*
415  * Implementation of 'move [window|container] [to] workspace
416  * next|prev|next_on_output|prev_on_output|current'.
417  *
418  */
419 void cmd_move_con_to_workspace(I3_CMD, char *which) {
420     owindow *current;
421
422     DLOG("which=%s\n", which);
423
424     /* We have nothing to move:
425      *  when criteria was specified but didn't match any window or
426      *  when criteria wasn't specified and we don't have any window focused. */
427     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
428         (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
429          !con_has_children(focused))) {
430         ysuccess(false);
431         return;
432     }
433
434     HANDLE_EMPTY_MATCH;
435
436     /* get the workspace */
437     Con *ws;
438     if (strcmp(which, "next") == 0)
439         ws = workspace_next();
440     else if (strcmp(which, "prev") == 0)
441         ws = workspace_prev();
442     else if (strcmp(which, "next_on_output") == 0)
443         ws = workspace_next_on_output();
444     else if (strcmp(which, "prev_on_output") == 0)
445         ws = workspace_prev_on_output();
446     else if (strcmp(which, "current") == 0)
447         ws = con_get_workspace(focused);
448     else {
449         ELOG("BUG: called with which=%s\n", which);
450         ysuccess(false);
451         return;
452     }
453
454     TAILQ_FOREACH(current, &owindows, owindows) {
455         DLOG("matching: %p / %s\n", current->con, current->con->name);
456         con_move_to_workspace(current->con, ws, true, false);
457     }
458
459     cmd_output->needs_tree_render = true;
460     // XXX: default reply for now, make this a better reply
461     ysuccess(true);
462 }
463
464 /**
465  * Implementation of 'move [window|container] [to] workspace back_and_forth'.
466  *
467  */
468 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
469     owindow *current;
470     Con *ws;
471
472     ws = workspace_back_and_forth_get();
473
474     if (ws == NULL) {
475         yerror("No workspace was previously active.");
476         return;
477     }
478
479     HANDLE_EMPTY_MATCH;
480
481     TAILQ_FOREACH(current, &owindows, owindows) {
482         DLOG("matching: %p / %s\n", current->con, current->con->name);
483         con_move_to_workspace(current->con, ws, true, false);
484     }
485
486     cmd_output->needs_tree_render = true;
487     // XXX: default reply for now, make this a better reply
488     ysuccess(true);
489 }
490
491 /*
492  * Implementation of 'move [window|container] [to] workspace <name>'.
493  *
494  */
495 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
496     if (strncasecmp(name, "__", strlen("__")) == 0) {
497         LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
498         ysuccess(false);
499         return;
500     }
501
502     owindow *current;
503
504     /* We have nothing to move:
505      *  when criteria was specified but didn't match any window or
506      *  when criteria wasn't specified and we don't have any window focused. */
507     if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
508         ELOG("No windows match your criteria, cannot move.\n");
509         ysuccess(false);
510         return;
511     } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
512                !con_has_children(focused)) {
513         ysuccess(false);
514         return;
515     }
516
517     LOG("should move window to workspace %s\n", name);
518     /* get the workspace */
519     Con *ws = NULL;
520     Con *output = NULL;
521
522     /* first look for a workspace with this name */
523     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
524         GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
525     }
526
527     /* if the name is plain digits, we interpret this as a "workspace number"
528      * command */
529     if (!ws && name_is_digits(name)) {
530         long parsed_num = ws_name_to_number(name);
531         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
532             GREP_FIRST(ws, output_get_content(output),
533                        child->num == parsed_num);
534         }
535     }
536
537     /* if no workspace was found, make a new one */
538     if (!ws)
539         ws = workspace_get(name, NULL);
540
541     ws = maybe_auto_back_and_forth_workspace(ws);
542
543     HANDLE_EMPTY_MATCH;
544
545     TAILQ_FOREACH(current, &owindows, owindows) {
546         DLOG("matching: %p / %s\n", current->con, current->con->name);
547         con_move_to_workspace(current->con, ws, true, false);
548     }
549
550     cmd_output->needs_tree_render = true;
551     // XXX: default reply for now, make this a better reply
552     ysuccess(true);
553 }
554
555 /*
556  * Implementation of 'move [window|container] [to] workspace number <name>'.
557  *
558  */
559 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
560     owindow *current;
561
562     /* We have nothing to move:
563      *  when criteria was specified but didn't match any window or
564      *  when criteria wasn't specified and we don't have any window focused. */
565     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
566         (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
567          !con_has_children(focused))) {
568         ysuccess(false);
569         return;
570     }
571
572     LOG("should move window to workspace %s\n", which);
573     /* get the workspace */
574     Con *output, *workspace = NULL;
575
576     long parsed_num = ws_name_to_number(which);
577
578     if (parsed_num == -1) {
579         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
580         yerror("Could not parse number \"%s\"", which);
581         return;
582     }
583
584     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
585     GREP_FIRST(workspace, output_get_content(output),
586                child->num == parsed_num);
587
588     if (!workspace) {
589         workspace = workspace_get(which, NULL);
590     }
591
592     workspace = maybe_auto_back_and_forth_workspace(workspace);
593
594     HANDLE_EMPTY_MATCH;
595
596     TAILQ_FOREACH(current, &owindows, owindows) {
597         DLOG("matching: %p / %s\n", current->con, current->con->name);
598         con_move_to_workspace(current->con, workspace, true, false);
599     }
600
601     cmd_output->needs_tree_render = true;
602     // XXX: default reply for now, make this a better reply
603     ysuccess(true);
604 }
605
606 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
607     LOG("floating resize\n");
608     Rect old_rect = floating_con->rect;
609     Con *focused_con = con_descend_focused(floating_con);
610
611     /* ensure that resize will take place even if pixel increment is smaller than
612      * height increment or width increment.
613      * fixes #1011 */
614     if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
615         strcmp(direction, "height") == 0) {
616         if (px < 0)
617             px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
618         else
619             px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
620     } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
621         if (px < 0)
622             px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
623         else
624             px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
625     }
626
627     if (strcmp(direction, "up") == 0) {
628         floating_con->rect.height += px;
629     } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
630         floating_con->rect.height += px;
631     } else if (strcmp(direction, "left") == 0) {
632         floating_con->rect.width += px;
633     } else {
634         floating_con->rect.width += px;
635     }
636
637     floating_check_size(floating_con);
638
639     /* Did we actually resize anything or did the size constraints prevent us?
640      * If we could not resize, exit now to not move the window. */
641     if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
642         return;
643
644     if (strcmp(direction, "up") == 0) {
645         floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
646     } else if (strcmp(direction, "left") == 0) {
647         floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
648     }
649
650     /* If this is a scratchpad window, don't auto center it from now on. */
651     if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
652         floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
653 }
654
655 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
656     LOG("tiling resize\n");
657     Con *second = NULL;
658     Con *first = current;
659     direction_t search_direction;
660     if (!strcmp(direction, "left"))
661         search_direction = D_LEFT;
662     else if (!strcmp(direction, "right"))
663         search_direction = D_RIGHT;
664     else if (!strcmp(direction, "up"))
665         search_direction = D_UP;
666     else
667         search_direction = D_DOWN;
668
669     bool res = resize_find_tiling_participants(&first, &second, search_direction);
670     if (!res) {
671         LOG("No second container in this direction found.\n");
672         ysuccess(false);
673         return false;
674     }
675
676     /* get the default percentage */
677     int children = con_num_children(first->parent);
678     LOG("ins. %d children\n", children);
679     double percentage = 1.0 / children;
680     LOG("default percentage = %f\n", percentage);
681
682     /* resize */
683     LOG("second->percent = %f\n", second->percent);
684     LOG("first->percent before = %f\n", first->percent);
685     if (first->percent == 0.0)
686         first->percent = percentage;
687     if (second->percent == 0.0)
688         second->percent = percentage;
689     double new_first_percent = first->percent + ((double)ppt / 100.0);
690     double new_second_percent = second->percent - ((double)ppt / 100.0);
691     LOG("new_first_percent = %f\n", new_first_percent);
692     LOG("new_second_percent = %f\n", new_second_percent);
693     /* Ensure that the new percentages are positive and greater than
694      * 0.05 to have a reasonable minimum size. */
695     if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
696         definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
697         first->percent += ((double)ppt / 100.0);
698         second->percent -= ((double)ppt / 100.0);
699         LOG("first->percent after = %f\n", first->percent);
700         LOG("second->percent after = %f\n", second->percent);
701     } else {
702         LOG("Not resizing, already at minimum size\n");
703     }
704
705     return true;
706 }
707
708 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
709     LOG("width/height resize\n");
710     /* get the appropriate current container (skip stacked/tabbed cons) */
711     while (current->parent->layout == L_STACKED ||
712            current->parent->layout == L_TABBED)
713         current = current->parent;
714
715     /* Then further go up until we find one with the matching orientation. */
716     orientation_t search_orientation =
717         (strcmp(direction, "width") == 0 ? HORIZ : VERT);
718
719     while (current->type != CT_WORKSPACE &&
720            current->type != CT_FLOATING_CON &&
721            con_orientation(current->parent) != search_orientation)
722         current = current->parent;
723
724     /* get the default percentage */
725     int children = con_num_children(current->parent);
726     LOG("ins. %d children\n", children);
727     double percentage = 1.0 / children;
728     LOG("default percentage = %f\n", percentage);
729
730     orientation_t orientation = con_orientation(current->parent);
731
732     if ((orientation == HORIZ &&
733          strcmp(direction, "height") == 0) ||
734         (orientation == VERT &&
735          strcmp(direction, "width") == 0)) {
736         LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
737             (orientation == HORIZ ? "horizontal" : "vertical"));
738         ysuccess(false);
739         return false;
740     }
741
742     if (children == 1) {
743         LOG("This is the only container, cannot resize.\n");
744         ysuccess(false);
745         return false;
746     }
747
748     /* Ensure all the other children have a percentage set. */
749     Con *child;
750     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
751         LOG("child->percent = %f (child %p)\n", child->percent, child);
752         if (child->percent == 0.0)
753             child->percent = percentage;
754     }
755
756     double new_current_percent = current->percent + ((double)ppt / 100.0);
757     double subtract_percent = ((double)ppt / 100.0) / (children - 1);
758     LOG("new_current_percent = %f\n", new_current_percent);
759     LOG("subtract_percent = %f\n", subtract_percent);
760     /* Ensure that the new percentages are positive and greater than
761      * 0.05 to have a reasonable minimum size. */
762     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
763         if (child == current)
764             continue;
765         if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
766             LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
767             ysuccess(false);
768             return false;
769         }
770     }
771     if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
772         LOG("Not resizing, already at minimum size\n");
773         ysuccess(false);
774         return false;
775     }
776
777     current->percent += ((double)ppt / 100.0);
778     LOG("current->percent after = %f\n", current->percent);
779
780     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
781         if (child == current)
782             continue;
783         child->percent -= subtract_percent;
784         LOG("child->percent after (%p) = %f\n", child, child->percent);
785     }
786
787     return true;
788 }
789
790 /*
791  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
792  *
793  */
794 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
795     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
796     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
797     // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
798     int px = atoi(resize_px);
799     int ppt = atoi(resize_ppt);
800     if (strcmp(way, "shrink") == 0) {
801         px *= -1;
802         ppt *= -1;
803     }
804
805     HANDLE_EMPTY_MATCH;
806
807     owindow *current;
808     TAILQ_FOREACH(current, &owindows, owindows) {
809         /* Don't handle dock windows (issue #1201) */
810         if (current->con->window && current->con->window->dock) {
811             DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
812             continue;
813         }
814
815         Con *floating_con;
816         if ((floating_con = con_inside_floating(current->con))) {
817             cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
818         } else {
819             if (strcmp(direction, "width") == 0 ||
820                 strcmp(direction, "height") == 0) {
821                 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
822                     return;
823             } else {
824                 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
825                     return;
826             }
827         }
828     }
829
830     cmd_output->needs_tree_render = true;
831     // XXX: default reply for now, make this a better reply
832     ysuccess(true);
833 }
834
835 /*
836  * Implementation of 'border normal|none|1pixel|toggle|pixel'.
837  *
838  */
839 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
840     DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
841     owindow *current;
842
843     HANDLE_EMPTY_MATCH;
844
845     TAILQ_FOREACH(current, &owindows, owindows) {
846         DLOG("matching: %p / %s\n", current->con, current->con->name);
847         int border_style = current->con->border_style;
848         char *end;
849         int tmp_border_width = -1;
850         tmp_border_width = strtol(border_width, &end, 10);
851         if (end == border_width) {
852             /* no valid digits found */
853             tmp_border_width = -1;
854         }
855         if (strcmp(border_style_str, "toggle") == 0) {
856             border_style++;
857             border_style %= 3;
858             if (border_style == BS_NORMAL)
859                 tmp_border_width = 2;
860             else if (border_style == BS_NONE)
861                 tmp_border_width = 0;
862             else if (border_style == BS_PIXEL)
863                 tmp_border_width = 1;
864         } else {
865             if (strcmp(border_style_str, "normal") == 0)
866                 border_style = BS_NORMAL;
867             else if (strcmp(border_style_str, "pixel") == 0)
868                 border_style = BS_PIXEL;
869             else if (strcmp(border_style_str, "1pixel") == 0) {
870                 border_style = BS_PIXEL;
871                 tmp_border_width = 1;
872             } else if (strcmp(border_style_str, "none") == 0)
873                 border_style = BS_NONE;
874             else {
875                 ELOG("BUG: called with border_style=%s\n", border_style_str);
876                 ysuccess(false);
877                 return;
878             }
879         }
880         con_set_border_style(current->con, border_style, tmp_border_width);
881     }
882
883     cmd_output->needs_tree_render = true;
884     // XXX: default reply for now, make this a better reply
885     ysuccess(true);
886 }
887
888 /*
889  * Implementation of 'nop <comment>'.
890  *
891  */
892 void cmd_nop(I3_CMD, char *comment) {
893     LOG("-------------------------------------------------\n");
894     LOG("  NOP: %s\n", comment);
895     LOG("-------------------------------------------------\n");
896 }
897
898 /*
899  * Implementation of 'append_layout <path>'.
900  *
901  */
902 void cmd_append_layout(I3_CMD, char *path) {
903     LOG("Appending layout \"%s\"\n", path);
904
905     json_content_t content = json_determine_content(path);
906     LOG("JSON content = %d\n", content);
907     if (content == JSON_CONTENT_UNKNOWN) {
908         ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
909         ysuccess(false);
910         return;
911     }
912
913     Con *parent = focused;
914     if (content == JSON_CONTENT_WORKSPACE) {
915         parent = output_get_content(con_get_output(parent));
916     } else {
917         /* We need to append the layout to a split container, since a leaf
918          * container must not have any children (by definition).
919          * Note that we explicitly check for workspaces, since they are okay for
920          * this purpose, but con_accepts_window() returns false for workspaces. */
921         while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
922             parent = parent->parent;
923     }
924     DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
925     char *errormsg = NULL;
926     tree_append_json(parent, path, &errormsg);
927     if (errormsg != NULL) {
928         yerror(errormsg);
929         free(errormsg);
930         /* Note that we continue executing since tree_append_json() has
931          * side-effects — user-provided layouts can be partly valid, partly
932          * invalid, leading to half of the placeholder containers being
933          * created. */
934     } else {
935         ysuccess(true);
936     }
937
938     // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
939     // false); should be enough, but when sending 'workspace 4; append_layout
940     // /tmp/foo.json', the needs_tree_render == true of the workspace command
941     // is not executed yet and will be batched with append_layout’s
942     // needs_tree_render after the parser finished. We should check if that is
943     // necessary at all.
944     render_con(croot, false);
945
946     restore_open_placeholder_windows(parent);
947
948     if (content == JSON_CONTENT_WORKSPACE)
949         ipc_send_workspace_event("restored", parent, NULL);
950
951     cmd_output->needs_tree_render = true;
952 }
953
954 /*
955  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
956  *
957  */
958 void cmd_workspace(I3_CMD, char *which) {
959     Con *ws;
960
961     DLOG("which=%s\n", which);
962
963     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
964         LOG("Cannot switch workspace while in global fullscreen\n");
965         ysuccess(false);
966         return;
967     }
968
969     if (strcmp(which, "next") == 0)
970         ws = workspace_next();
971     else if (strcmp(which, "prev") == 0)
972         ws = workspace_prev();
973     else if (strcmp(which, "next_on_output") == 0)
974         ws = workspace_next_on_output();
975     else if (strcmp(which, "prev_on_output") == 0)
976         ws = workspace_prev_on_output();
977     else {
978         ELOG("BUG: called with which=%s\n", which);
979         ysuccess(false);
980         return;
981     }
982
983     workspace_show(ws);
984
985     cmd_output->needs_tree_render = true;
986     // XXX: default reply for now, make this a better reply
987     ysuccess(true);
988 }
989
990 /*
991  * Implementation of 'workspace number <name>'
992  *
993  */
994 void cmd_workspace_number(I3_CMD, char *which) {
995     Con *output, *workspace = NULL;
996
997     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
998         LOG("Cannot switch workspace while in global fullscreen\n");
999         ysuccess(false);
1000         return;
1001     }
1002
1003     long parsed_num = ws_name_to_number(which);
1004
1005     if (parsed_num == -1) {
1006         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1007         yerror("Could not parse number \"%s\"", which);
1008         return;
1009     }
1010
1011     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1012     GREP_FIRST(workspace, output_get_content(output),
1013                child->num == parsed_num);
1014
1015     if (!workspace) {
1016         LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1017         ysuccess(true);
1018         workspace_show_by_name(which);
1019         cmd_output->needs_tree_render = true;
1020         return;
1021     }
1022     if (maybe_back_and_forth(cmd_output, workspace->name))
1023         return;
1024     workspace_show(workspace);
1025
1026     cmd_output->needs_tree_render = true;
1027     // XXX: default reply for now, make this a better reply
1028     ysuccess(true);
1029 }
1030
1031 /*
1032  * Implementation of 'workspace back_and_forth'.
1033  *
1034  */
1035 void cmd_workspace_back_and_forth(I3_CMD) {
1036     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1037         LOG("Cannot switch workspace while in global fullscreen\n");
1038         ysuccess(false);
1039         return;
1040     }
1041
1042     workspace_back_and_forth();
1043
1044     cmd_output->needs_tree_render = true;
1045     // XXX: default reply for now, make this a better reply
1046     ysuccess(true);
1047 }
1048
1049 /*
1050  * Implementation of 'workspace <name>'
1051  *
1052  */
1053 void cmd_workspace_name(I3_CMD, char *name) {
1054     if (strncasecmp(name, "__", strlen("__")) == 0) {
1055         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1056         ysuccess(false);
1057         return;
1058     }
1059
1060     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1061         LOG("Cannot switch workspace while in global fullscreen\n");
1062         ysuccess(false);
1063         return;
1064     }
1065
1066     DLOG("should switch to workspace %s\n", name);
1067     if (maybe_back_and_forth(cmd_output, name))
1068         return;
1069
1070     Con *ws = NULL;
1071     Con *output = NULL;
1072
1073     /* first look for a workspace with this name */
1074     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1075         GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1076     }
1077
1078     /* if the name is only digits, we interpret this as a "workspace number"
1079      * command */
1080     if (!ws && name_is_digits(name)) {
1081         long parsed_num = ws_name_to_number(name);
1082         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1083             GREP_FIRST(ws, output_get_content(output),
1084                        child->num == parsed_num);
1085         }
1086     }
1087
1088     /* if no workspace was found, make a new one */
1089     if (!ws)
1090         ws = workspace_get(name, NULL);
1091
1092     workspace_show(ws);
1093
1094     cmd_output->needs_tree_render = true;
1095     // XXX: default reply for now, make this a better reply
1096     ysuccess(true);
1097 }
1098
1099 /*
1100  * Implementation of 'mark <mark>'
1101  *
1102  */
1103 void cmd_mark(I3_CMD, char *mark) {
1104     DLOG("Clearing all windows which have that mark first\n");
1105
1106     Con *con;
1107     TAILQ_FOREACH(con, &all_cons, all_cons) {
1108         if (con->mark && strcmp(con->mark, mark) == 0)
1109             FREE(con->mark);
1110     }
1111
1112     DLOG("marking window with str %s\n", mark);
1113     owindow *current;
1114
1115     HANDLE_EMPTY_MATCH;
1116
1117     TAILQ_FOREACH(current, &owindows, owindows) {
1118         DLOG("matching: %p / %s\n", current->con, current->con->name);
1119         current->con->mark = sstrdup(mark);
1120     }
1121
1122     cmd_output->needs_tree_render = true;
1123     // XXX: default reply for now, make this a better reply
1124     ysuccess(true);
1125 }
1126
1127 /*
1128  * Implementation of 'unmark [mark]'
1129  *
1130  */
1131 void cmd_unmark(I3_CMD, char *mark) {
1132     if (mark == NULL) {
1133         Con *con;
1134         TAILQ_FOREACH(con, &all_cons, all_cons) {
1135             FREE(con->mark);
1136         }
1137         DLOG("removed all window marks");
1138     } else {
1139         Con *con;
1140         TAILQ_FOREACH(con, &all_cons, all_cons) {
1141             if (con->mark && strcmp(con->mark, mark) == 0)
1142                 FREE(con->mark);
1143         }
1144         DLOG("removed window mark %s\n", mark);
1145     }
1146
1147     cmd_output->needs_tree_render = true;
1148     // XXX: default reply for now, make this a better reply
1149     ysuccess(true);
1150 }
1151
1152 /*
1153  * Implementation of 'mode <string>'.
1154  *
1155  */
1156 void cmd_mode(I3_CMD, char *mode) {
1157     DLOG("mode=%s\n", mode);
1158     switch_mode(mode);
1159
1160     // XXX: default reply for now, make this a better reply
1161     ysuccess(true);
1162 }
1163
1164 /*
1165  * Implementation of 'move [window|container] [to] output <str>'.
1166  *
1167  */
1168 void cmd_move_con_to_output(I3_CMD, char *name) {
1169     owindow *current;
1170
1171     DLOG("should move window to output %s\n", name);
1172
1173     HANDLE_EMPTY_MATCH;
1174
1175     /* get the output */
1176     Output *current_output = NULL;
1177     Output *output;
1178
1179     // TODO: fix the handling of criteria
1180     TAILQ_FOREACH(current, &owindows, owindows)
1181     current_output = get_output_of_con(current->con);
1182
1183     assert(current_output != NULL);
1184
1185     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1186     if (strcasecmp(name, "up") == 0)
1187         output = get_output_next_wrap(D_UP, current_output);
1188     else if (strcasecmp(name, "down") == 0)
1189         output = get_output_next_wrap(D_DOWN, current_output);
1190     else if (strcasecmp(name, "left") == 0)
1191         output = get_output_next_wrap(D_LEFT, current_output);
1192     else if (strcasecmp(name, "right") == 0)
1193         output = get_output_next_wrap(D_RIGHT, current_output);
1194     else
1195         output = get_output_by_name(name);
1196
1197     if (!output) {
1198         LOG("No such output found.\n");
1199         ysuccess(false);
1200         return;
1201     }
1202
1203     /* get visible workspace on output */
1204     Con *ws = NULL;
1205     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1206     if (!ws) {
1207         ysuccess(false);
1208         return;
1209     }
1210
1211     TAILQ_FOREACH(current, &owindows, owindows) {
1212         DLOG("matching: %p / %s\n", current->con, current->con->name);
1213         con_move_to_workspace(current->con, ws, true, false);
1214     }
1215
1216     cmd_output->needs_tree_render = true;
1217     // XXX: default reply for now, make this a better reply
1218     ysuccess(true);
1219 }
1220
1221 /*
1222  * Implementation of 'floating enable|disable|toggle'
1223  *
1224  */
1225 void cmd_floating(I3_CMD, char *floating_mode) {
1226     owindow *current;
1227
1228     DLOG("floating_mode=%s\n", floating_mode);
1229
1230     HANDLE_EMPTY_MATCH;
1231
1232     TAILQ_FOREACH(current, &owindows, owindows) {
1233         DLOG("matching: %p / %s\n", current->con, current->con->name);
1234         if (strcmp(floating_mode, "toggle") == 0) {
1235             DLOG("should toggle mode\n");
1236             toggle_floating_mode(current->con, false);
1237         } else {
1238             DLOG("should switch mode to %s\n", floating_mode);
1239             if (strcmp(floating_mode, "enable") == 0) {
1240                 floating_enable(current->con, false);
1241             } else {
1242                 floating_disable(current->con, false);
1243             }
1244         }
1245     }
1246
1247     cmd_output->needs_tree_render = true;
1248     // XXX: default reply for now, make this a better reply
1249     ysuccess(true);
1250 }
1251
1252 /*
1253  * Implementation of 'move workspace to [output] <str>'.
1254  *
1255  */
1256 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1257     DLOG("should move workspace to output %s\n", name);
1258
1259     HANDLE_EMPTY_MATCH;
1260
1261     owindow *current;
1262     TAILQ_FOREACH(current, &owindows, owindows) {
1263         Output *current_output = get_output_of_con(current->con);
1264         if (!current_output) {
1265             ELOG("Cannot get current output. This is a bug in i3.\n");
1266             ysuccess(false);
1267             return;
1268         }
1269         Output *output = get_output_from_string(current_output, name);
1270         if (!output) {
1271             ELOG("Could not get output from string \"%s\"\n", name);
1272             ysuccess(false);
1273             return;
1274         }
1275
1276         Con *content = output_get_content(output->con);
1277         LOG("got output %p with content %p\n", output, content);
1278
1279         Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1280         LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1281
1282         Con *ws = con_get_workspace(current->con);
1283         LOG("should move workspace %p / %s\n", ws, ws->name);
1284         bool workspace_was_visible = workspace_is_visible(ws);
1285
1286         if (con_num_children(ws->parent) == 1) {
1287             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1288
1289             /* check if we can find a workspace assigned to this output */
1290             bool used_assignment = false;
1291             struct Workspace_Assignment *assignment;
1292             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1293                 if (strcmp(assignment->output, current_output->name) != 0)
1294                     continue;
1295
1296                 /* check if this workspace is already attached to the tree */
1297                 Con *workspace = NULL, *out;
1298                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1299                 GREP_FIRST(workspace, output_get_content(out),
1300                            !strcasecmp(child->name, assignment->name));
1301                 if (workspace != NULL)
1302                     continue;
1303
1304                 /* so create the workspace referenced to by this assignment */
1305                 LOG("Creating workspace from assignment %s.\n", assignment->name);
1306                 workspace_get(assignment->name, NULL);
1307                 used_assignment = true;
1308                 break;
1309             }
1310
1311             /* if we couldn't create the workspace using an assignment, create
1312              * it on the output */
1313             if (!used_assignment)
1314                 create_workspace_on_output(current_output, ws->parent);
1315
1316             /* notify the IPC listeners */
1317             ipc_send_workspace_event("init", ws, NULL);
1318         }
1319         DLOG("Detaching\n");
1320
1321         /* detach from the old output and attach to the new output */
1322         Con *old_content = ws->parent;
1323         con_detach(ws);
1324         if (workspace_was_visible) {
1325             /* The workspace which we just detached was visible, so focus
1326              * the next one in the focus-stack. */
1327             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1328             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1329             workspace_show(focus_ws);
1330         }
1331         con_attach(ws, content, false);
1332
1333         /* fix the coordinates of the floating containers */
1334         Con *floating_con;
1335         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1336         floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1337
1338         ipc_send_workspace_event("move", ws, NULL);
1339         if (workspace_was_visible) {
1340             /* Focus the moved workspace on the destination output. */
1341             workspace_show(ws);
1342         }
1343
1344         /* NB: We cannot simply work with previously_visible_ws since it might
1345          * have been cleaned up by workspace_show() already, depending on the
1346          * focus order/number of other workspaces on the output.
1347          * Instead, we loop through the available workspaces and only work with
1348          * previously_visible_ws if we still find it. */
1349         TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1350             if (ws != previously_visible_ws)
1351                 continue;
1352
1353             /* Call the on_remove_child callback of the workspace which previously
1354              * was visible on the destination output. Since it is no longer
1355              * visible, it might need to get cleaned up. */
1356             CALL(previously_visible_ws, on_remove_child);
1357             break;
1358         }
1359     }
1360
1361     cmd_output->needs_tree_render = true;
1362     // XXX: default reply for now, make this a better reply
1363     ysuccess(true);
1364 }
1365
1366 /*
1367  * Implementation of 'split v|h|vertical|horizontal'.
1368  *
1369  */
1370 void cmd_split(I3_CMD, char *direction) {
1371     owindow *current;
1372     /* TODO: use matches */
1373     LOG("splitting in direction %c\n", direction[0]);
1374     if (match_is_empty(current_match))
1375         tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1376     else {
1377         TAILQ_FOREACH(current, &owindows, owindows) {
1378             DLOG("matching: %p / %s\n", current->con, current->con->name);
1379             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1380         }
1381     }
1382
1383     cmd_output->needs_tree_render = true;
1384     // XXX: default reply for now, make this a better reply
1385     ysuccess(true);
1386 }
1387
1388 /*
1389  * Implementation of 'kill [window|client]'.
1390  *
1391  */
1392 void cmd_kill(I3_CMD, char *kill_mode_str) {
1393     if (kill_mode_str == NULL)
1394         kill_mode_str = "window";
1395     owindow *current;
1396
1397     DLOG("kill_mode=%s\n", kill_mode_str);
1398
1399     int kill_mode;
1400     if (strcmp(kill_mode_str, "window") == 0)
1401         kill_mode = KILL_WINDOW;
1402     else if (strcmp(kill_mode_str, "client") == 0)
1403         kill_mode = KILL_CLIENT;
1404     else {
1405         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1406         ysuccess(false);
1407         return;
1408     }
1409
1410     /* check if the match is empty, not if the result is empty */
1411     if (match_is_empty(current_match))
1412         tree_close_con(kill_mode);
1413     else {
1414         TAILQ_FOREACH(current, &owindows, owindows) {
1415             DLOG("matching: %p / %s\n", current->con, current->con->name);
1416             tree_close(current->con, kill_mode, false, false);
1417         }
1418     }
1419
1420     cmd_output->needs_tree_render = true;
1421     // XXX: default reply for now, make this a better reply
1422     ysuccess(true);
1423 }
1424
1425 /*
1426  * Implementation of 'exec [--no-startup-id] <command>'.
1427  *
1428  */
1429 void cmd_exec(I3_CMD, char *nosn, char *command) {
1430     bool no_startup_id = (nosn != NULL);
1431
1432     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1433     start_application(command, no_startup_id);
1434
1435     // XXX: default reply for now, make this a better reply
1436     ysuccess(true);
1437 }
1438
1439 /*
1440  * Implementation of 'focus left|right|up|down'.
1441  *
1442  */
1443 void cmd_focus_direction(I3_CMD, char *direction) {
1444     DLOG("direction = *%s*\n", direction);
1445
1446     if (strcmp(direction, "left") == 0)
1447         tree_next('p', HORIZ);
1448     else if (strcmp(direction, "right") == 0)
1449         tree_next('n', HORIZ);
1450     else if (strcmp(direction, "up") == 0)
1451         tree_next('p', VERT);
1452     else if (strcmp(direction, "down") == 0)
1453         tree_next('n', VERT);
1454     else {
1455         ELOG("Invalid focus direction (%s)\n", direction);
1456         ysuccess(false);
1457         return;
1458     }
1459
1460     cmd_output->needs_tree_render = true;
1461     // XXX: default reply for now, make this a better reply
1462     ysuccess(true);
1463 }
1464
1465 /*
1466  * Implementation of 'focus tiling|floating|mode_toggle'.
1467  *
1468  */
1469 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1470     DLOG("window_mode = %s\n", window_mode);
1471
1472     Con *ws = con_get_workspace(focused);
1473     Con *current;
1474     if (ws != NULL) {
1475         if (strcmp(window_mode, "mode_toggle") == 0) {
1476             current = TAILQ_FIRST(&(ws->focus_head));
1477             if (current != NULL && current->type == CT_FLOATING_CON)
1478                 window_mode = "tiling";
1479             else
1480                 window_mode = "floating";
1481         }
1482         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1483             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1484                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1485                 continue;
1486
1487             con_focus(con_descend_focused(current));
1488             break;
1489         }
1490     }
1491
1492     cmd_output->needs_tree_render = true;
1493     // XXX: default reply for now, make this a better reply
1494     ysuccess(true);
1495 }
1496
1497 /*
1498  * Implementation of 'focus parent|child'.
1499  *
1500  */
1501 void cmd_focus_level(I3_CMD, char *level) {
1502     DLOG("level = %s\n", level);
1503     bool success = false;
1504
1505     /* Focusing the parent can only be allowed if the newly
1506      * focused container won't escape the fullscreen container. */
1507     if (strcmp(level, "parent") == 0) {
1508         if (focused && focused->parent) {
1509             if (con_fullscreen_permits_focusing(focused->parent))
1510                 success = level_up();
1511             else
1512                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1513         }
1514     }
1515
1516     /* Focusing a child should always be allowed. */
1517     else
1518         success = level_down();
1519
1520     cmd_output->needs_tree_render = success;
1521     // XXX: default reply for now, make this a better reply
1522     ysuccess(success);
1523 }
1524
1525 /*
1526  * Implementation of 'focus'.
1527  *
1528  */
1529 void cmd_focus(I3_CMD) {
1530     DLOG("current_match = %p\n", current_match);
1531
1532     if (match_is_empty(current_match)) {
1533         ELOG("You have to specify which window/container should be focused.\n");
1534         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1535
1536         yerror("You have to specify which window/container should be focused");
1537
1538         return;
1539     }
1540
1541     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1542     int count = 0;
1543     owindow *current;
1544     TAILQ_FOREACH(current, &owindows, owindows) {
1545         Con *ws = con_get_workspace(current->con);
1546         /* If no workspace could be found, this was a dock window.
1547          * Just skip it, you cannot focus dock windows. */
1548         if (!ws)
1549             continue;
1550
1551         /* Check the fullscreen focus constraints. */
1552         if (!con_fullscreen_permits_focusing(current->con)) {
1553             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1554             ysuccess(false);
1555             return;
1556         }
1557
1558         /* In case this is a scratchpad window, call scratchpad_show(). */
1559         if (ws == __i3_scratch) {
1560             scratchpad_show(current->con);
1561             count++;
1562             /* While for the normal focus case we can change focus multiple
1563              * times and only a single window ends up focused, we could show
1564              * multiple scratchpad windows. So, rather break here. */
1565             break;
1566         }
1567
1568         /* If the container is not on the current workspace,
1569          * workspace_show() will switch to a different workspace and (if
1570          * enabled) trigger a mouse pointer warp to the currently focused
1571          * container (!) on the target workspace.
1572          *
1573          * Therefore, before calling workspace_show(), we make sure that
1574          * 'current' will be focused on the workspace. However, we cannot
1575          * just con_focus(current) because then the pointer will not be
1576          * warped at all (the code thinks we are already there).
1577          *
1578          * So we focus 'current' to make it the currently focused window of
1579          * the target workspace, then revert focus. */
1580         Con *currently_focused = focused;
1581         con_focus(current->con);
1582         con_focus(currently_focused);
1583
1584         /* Now switch to the workspace, then focus */
1585         workspace_show(ws);
1586         LOG("focusing %p / %s\n", current->con, current->con->name);
1587         con_focus(current->con);
1588         count++;
1589     }
1590
1591     if (count > 1)
1592         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1593             "while only exactly one container can be focused at a time.\n",
1594             count);
1595
1596     cmd_output->needs_tree_render = true;
1597     // XXX: default reply for now, make this a better reply
1598     ysuccess(true);
1599 }
1600
1601 /*
1602  * Implementation of 'fullscreen enable|toggle [global]' and
1603  *                   'fullscreen disable'
1604  *
1605  */
1606 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1607     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1608     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1609     owindow *current;
1610
1611     HANDLE_EMPTY_MATCH;
1612
1613     TAILQ_FOREACH(current, &owindows, owindows) {
1614         DLOG("matching: %p / %s\n", current->con, current->con->name);
1615         if (strcmp(action, "toggle") == 0) {
1616             con_toggle_fullscreen(current->con, mode);
1617         } else if (strcmp(action, "enable") == 0) {
1618             con_enable_fullscreen(current->con, mode);
1619         } else if (strcmp(action, "disable") == 0) {
1620             con_disable_fullscreen(current->con);
1621         }
1622     }
1623
1624     cmd_output->needs_tree_render = true;
1625     // XXX: default reply for now, make this a better reply
1626     ysuccess(true);
1627 }
1628
1629 /*
1630  * Implementation of 'move <direction> [<pixels> [px]]'.
1631  *
1632  */
1633 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1634     // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
1635     int px = atoi(move_px);
1636
1637     owindow *current;
1638     HANDLE_EMPTY_MATCH;
1639
1640     Con *initially_focused = focused;
1641
1642     TAILQ_FOREACH(current, &owindows, owindows) {
1643         DLOG("moving in direction %s, px %s\n", direction, move_px);
1644         if (con_is_floating(current->con)) {
1645             DLOG("floating move with %d pixels\n", px);
1646             Rect newrect = current->con->parent->rect;
1647             if (strcmp(direction, "left") == 0) {
1648                 newrect.x -= px;
1649             } else if (strcmp(direction, "right") == 0) {
1650                 newrect.x += px;
1651             } else if (strcmp(direction, "up") == 0) {
1652                 newrect.y -= px;
1653             } else if (strcmp(direction, "down") == 0) {
1654                 newrect.y += px;
1655             }
1656             floating_reposition(current->con->parent, newrect);
1657         } else {
1658             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1659             cmd_output->needs_tree_render = true;
1660         }
1661     }
1662
1663     /* the move command should not disturb focus */
1664     if (focused != initially_focused)
1665         con_focus(initially_focused);
1666
1667     // XXX: default reply for now, make this a better reply
1668     ysuccess(true);
1669 }
1670
1671 /*
1672  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1673  *
1674  */
1675 void cmd_layout(I3_CMD, char *layout_str) {
1676     if (strcmp(layout_str, "stacking") == 0)
1677         layout_str = "stacked";
1678     owindow *current;
1679     layout_t layout;
1680     /* default is a special case which will be handled in con_set_layout(). */
1681     if (strcmp(layout_str, "default") == 0)
1682         layout = L_DEFAULT;
1683     else if (strcmp(layout_str, "stacked") == 0)
1684         layout = L_STACKED;
1685     else if (strcmp(layout_str, "tabbed") == 0)
1686         layout = L_TABBED;
1687     else if (strcmp(layout_str, "splitv") == 0)
1688         layout = L_SPLITV;
1689     else if (strcmp(layout_str, "splith") == 0)
1690         layout = L_SPLITH;
1691     else {
1692         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1693         return;
1694     }
1695
1696     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1697
1698     /* check if the match is empty, not if the result is empty */
1699     if (match_is_empty(current_match))
1700         con_set_layout(focused, layout);
1701     else {
1702         TAILQ_FOREACH(current, &owindows, owindows) {
1703             DLOG("matching: %p / %s\n", current->con, current->con->name);
1704             con_set_layout(current->con, layout);
1705         }
1706     }
1707
1708     cmd_output->needs_tree_render = true;
1709     // XXX: default reply for now, make this a better reply
1710     ysuccess(true);
1711 }
1712
1713 /*
1714  * Implementation of 'layout toggle [all|split]'.
1715  *
1716  */
1717 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1718     owindow *current;
1719
1720     if (toggle_mode == NULL)
1721         toggle_mode = "default";
1722
1723     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1724
1725     /* check if the match is empty, not if the result is empty */
1726     if (match_is_empty(current_match))
1727         con_toggle_layout(focused, toggle_mode);
1728     else {
1729         TAILQ_FOREACH(current, &owindows, owindows) {
1730             DLOG("matching: %p / %s\n", current->con, current->con->name);
1731             con_toggle_layout(current->con, toggle_mode);
1732         }
1733     }
1734
1735     cmd_output->needs_tree_render = true;
1736     // XXX: default reply for now, make this a better reply
1737     ysuccess(true);
1738 }
1739
1740 /*
1741  * Implementation of 'exit'.
1742  *
1743  */
1744 void cmd_exit(I3_CMD) {
1745     LOG("Exiting due to user command.\n");
1746     ipc_shutdown();
1747     unlink(config.ipc_socket_path);
1748     xcb_disconnect(conn);
1749     exit(0);
1750
1751     /* unreached */
1752 }
1753
1754 /*
1755  * Implementation of 'reload'.
1756  *
1757  */
1758 void cmd_reload(I3_CMD) {
1759     LOG("reloading\n");
1760     kill_nagbar(&config_error_nagbar_pid, false);
1761     kill_nagbar(&command_error_nagbar_pid, false);
1762     load_configuration(conn, NULL, true);
1763     x_set_i3_atoms();
1764     /* Send an IPC event just in case the ws names have changed */
1765     ipc_send_workspace_event("reload", NULL, NULL);
1766     /* Send an update event for the barconfig just in case it has changed */
1767     update_barconfig();
1768
1769     // XXX: default reply for now, make this a better reply
1770     ysuccess(true);
1771 }
1772
1773 /*
1774  * Implementation of 'restart'.
1775  *
1776  */
1777 void cmd_restart(I3_CMD) {
1778     LOG("restarting i3\n");
1779     ipc_shutdown();
1780     unlink(config.ipc_socket_path);
1781     /* We need to call this manually since atexit handlers don’t get called
1782      * when exec()ing */
1783     purge_zerobyte_logfile();
1784     i3_restart(false);
1785
1786     // XXX: default reply for now, make this a better reply
1787     ysuccess(true);
1788 }
1789
1790 /*
1791  * Implementation of 'open'.
1792  *
1793  */
1794 void cmd_open(I3_CMD) {
1795     LOG("opening new container\n");
1796     Con *con = tree_open_con(NULL, NULL);
1797     con->layout = L_SPLITH;
1798     con_focus(con);
1799
1800     y(map_open);
1801     ystr("success");
1802     y(bool, true);
1803     ystr("id");
1804     y(integer, (long int)con);
1805     y(map_close);
1806
1807     cmd_output->needs_tree_render = true;
1808 }
1809
1810 /*
1811  * Implementation of 'focus output <output>'.
1812  *
1813  */
1814 void cmd_focus_output(I3_CMD, char *name) {
1815     owindow *current;
1816
1817     DLOG("name = %s\n", name);
1818
1819     HANDLE_EMPTY_MATCH;
1820
1821     /* get the output */
1822     Output *current_output = NULL;
1823     Output *output;
1824
1825     TAILQ_FOREACH(current, &owindows, owindows)
1826     current_output = get_output_of_con(current->con);
1827     assert(current_output != NULL);
1828
1829     output = get_output_from_string(current_output, name);
1830
1831     if (!output) {
1832         LOG("No such output found.\n");
1833         ysuccess(false);
1834         return;
1835     }
1836
1837     /* get visible workspace on output */
1838     Con *ws = NULL;
1839     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1840     if (!ws) {
1841         ysuccess(false);
1842         return;
1843     }
1844
1845     workspace_show(ws);
1846
1847     cmd_output->needs_tree_render = true;
1848     // XXX: default reply for now, make this a better reply
1849     ysuccess(true);
1850 }
1851
1852 /*
1853  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1854  *
1855  */
1856 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1857     int x = atoi(cx);
1858     int y = atoi(cy);
1859     bool has_error = false;
1860
1861     owindow *current;
1862     HANDLE_EMPTY_MATCH;
1863
1864     TAILQ_FOREACH(current, &owindows, owindows) {
1865         if (!con_is_floating(current->con)) {
1866             ELOG("Cannot change position. The window/container is not floating\n");
1867
1868             if (!has_error) {
1869                 yerror("Cannot change position of a window/container because it is not floating.");
1870                 has_error = true;
1871             }
1872
1873             continue;
1874         }
1875
1876         if (strcmp(method, "absolute") == 0) {
1877             current->con->parent->rect.x = x;
1878             current->con->parent->rect.y = y;
1879
1880             DLOG("moving to absolute position %d %d\n", x, y);
1881             floating_maybe_reassign_ws(current->con->parent);
1882             cmd_output->needs_tree_render = true;
1883         }
1884
1885         if (strcmp(method, "position") == 0) {
1886             Rect newrect = current->con->parent->rect;
1887
1888             DLOG("moving to position %d %d\n", x, y);
1889             newrect.x = x;
1890             newrect.y = y;
1891
1892             floating_reposition(current->con->parent, newrect);
1893         }
1894     }
1895
1896     // XXX: default reply for now, make this a better reply
1897     if (!has_error)
1898         ysuccess(true);
1899 }
1900
1901 /*
1902  * Implementation of 'move [window|container] [to] [absolute] position center
1903  *
1904  */
1905 void cmd_move_window_to_center(I3_CMD, char *method) {
1906     if (!con_is_floating(focused)) {
1907         ELOG("Cannot change position. The window/container is not floating\n");
1908         yerror("Cannot change position. The window/container is not floating.");
1909         return;
1910     }
1911
1912     if (strcmp(method, "absolute") == 0) {
1913         Rect *rect = &focused->parent->rect;
1914
1915         DLOG("moving to absolute center\n");
1916         rect->x = croot->rect.width / 2 - rect->width / 2;
1917         rect->y = croot->rect.height / 2 - rect->height / 2;
1918
1919         floating_maybe_reassign_ws(focused->parent);
1920         cmd_output->needs_tree_render = true;
1921     }
1922
1923     if (strcmp(method, "position") == 0) {
1924         Rect *wsrect = &con_get_workspace(focused)->rect;
1925         Rect newrect = focused->parent->rect;
1926
1927         DLOG("moving to center\n");
1928         newrect.x = wsrect->width / 2 - newrect.width / 2;
1929         newrect.y = wsrect->height / 2 - newrect.height / 2;
1930
1931         floating_reposition(focused->parent, newrect);
1932     }
1933
1934     // XXX: default reply for now, make this a better reply
1935     ysuccess(true);
1936 }
1937
1938 /*
1939  * Implementation of 'move scratchpad'.
1940  *
1941  */
1942 void cmd_move_scratchpad(I3_CMD) {
1943     DLOG("should move window to scratchpad\n");
1944     owindow *current;
1945
1946     HANDLE_EMPTY_MATCH;
1947
1948     TAILQ_FOREACH(current, &owindows, owindows) {
1949         DLOG("matching: %p / %s\n", current->con, current->con->name);
1950         scratchpad_move(current->con);
1951     }
1952
1953     cmd_output->needs_tree_render = true;
1954     // XXX: default reply for now, make this a better reply
1955     ysuccess(true);
1956 }
1957
1958 /*
1959  * Implementation of 'scratchpad show'.
1960  *
1961  */
1962 void cmd_scratchpad_show(I3_CMD) {
1963     DLOG("should show scratchpad window\n");
1964     owindow *current;
1965
1966     if (match_is_empty(current_match)) {
1967         scratchpad_show(NULL);
1968     } else {
1969         TAILQ_FOREACH(current, &owindows, owindows) {
1970             DLOG("matching: %p / %s\n", current->con, current->con->name);
1971             scratchpad_show(current->con);
1972         }
1973     }
1974
1975     cmd_output->needs_tree_render = true;
1976     // XXX: default reply for now, make this a better reply
1977     ysuccess(true);
1978 }
1979
1980 /*
1981  * Implementation of 'rename workspace [<name>] to <name>'
1982  *
1983  */
1984 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1985     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1986         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1987         ysuccess(false);
1988         return;
1989     }
1990     if (old_name) {
1991         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1992     } else {
1993         LOG("Renaming current workspace to \"%s\"\n", new_name);
1994     }
1995
1996     Con *output, *workspace = NULL;
1997     if (old_name) {
1998         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1999         GREP_FIRST(workspace, output_get_content(output),
2000                    !strcasecmp(child->name, old_name));
2001     } else {
2002         workspace = con_get_workspace(focused);
2003     }
2004
2005     if (!workspace) {
2006         yerror("Old workspace \"%s\" not found", old_name);
2007         return;
2008     }
2009
2010     Con *check_dest = NULL;
2011     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2012     GREP_FIRST(check_dest, output_get_content(output),
2013                !strcasecmp(child->name, new_name));
2014
2015     if (check_dest != NULL) {
2016         yerror("New workspace \"%s\" already exists", new_name);
2017         return;
2018     }
2019
2020     /* Change the name and try to parse it as a number. */
2021     FREE(workspace->name);
2022     workspace->name = sstrdup(new_name);
2023
2024     workspace->num = ws_name_to_number(new_name);
2025     LOG("num = %d\n", workspace->num);
2026
2027     /* By re-attaching, the sort order will be correct afterwards. */
2028     Con *previously_focused = focused;
2029     Con *parent = workspace->parent;
2030     con_detach(workspace);
2031     con_attach(workspace, parent, false);
2032     /* Restore the previous focus since con_attach messes with the focus. */
2033     con_focus(previously_focused);
2034
2035     cmd_output->needs_tree_render = true;
2036     ysuccess(true);
2037
2038     ipc_send_workspace_event("rename", workspace, NULL);
2039     ewmh_update_desktop_names();
2040     ewmh_update_desktop_viewport();
2041     ewmh_update_current_desktop();
2042 }
2043
2044 /*
2045  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2046  *
2047  */
2048 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2049     int mode = M_DOCK;
2050     bool toggle = false;
2051     if (strcmp(bar_mode, "dock") == 0)
2052         mode = M_DOCK;
2053     else if (strcmp(bar_mode, "hide") == 0)
2054         mode = M_HIDE;
2055     else if (strcmp(bar_mode, "invisible") == 0)
2056         mode = M_INVISIBLE;
2057     else if (strcmp(bar_mode, "toggle") == 0)
2058         toggle = true;
2059     else {
2060         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2061         return false;
2062     }
2063
2064     bool changed_sth = false;
2065     Barconfig *current = NULL;
2066     TAILQ_FOREACH(current, &barconfigs, configs) {
2067         if (bar_id && strcmp(current->id, bar_id) != 0)
2068             continue;
2069
2070         if (toggle)
2071             mode = (current->mode + 1) % 2;
2072
2073         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2074         current->mode = mode;
2075         changed_sth = true;
2076
2077         if (bar_id)
2078             break;
2079     }
2080
2081     if (bar_id && !changed_sth) {
2082         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2083         return false;
2084     }
2085
2086     return true;
2087 }
2088
2089 /*
2090  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2091  *
2092  */
2093 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2094     int hidden_state = S_SHOW;
2095     bool toggle = false;
2096     if (strcmp(bar_hidden_state, "hide") == 0)
2097         hidden_state = S_HIDE;
2098     else if (strcmp(bar_hidden_state, "show") == 0)
2099         hidden_state = S_SHOW;
2100     else if (strcmp(bar_hidden_state, "toggle") == 0)
2101         toggle = true;
2102     else {
2103         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2104         return false;
2105     }
2106
2107     bool changed_sth = false;
2108     Barconfig *current = NULL;
2109     TAILQ_FOREACH(current, &barconfigs, configs) {
2110         if (bar_id && strcmp(current->id, bar_id) != 0)
2111             continue;
2112
2113         if (toggle)
2114             hidden_state = (current->hidden_state + 1) % 2;
2115
2116         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2117         current->hidden_state = hidden_state;
2118         changed_sth = true;
2119
2120         if (bar_id)
2121             break;
2122     }
2123
2124     if (bar_id && !changed_sth) {
2125         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2126         return false;
2127     }
2128
2129     return true;
2130 }
2131
2132 /*
2133  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2134  *
2135  */
2136 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2137     bool ret;
2138     if (strcmp(bar_type, "mode") == 0)
2139         ret = cmd_bar_mode(bar_value, bar_id);
2140     else if (strcmp(bar_type, "hidden_state") == 0)
2141         ret = cmd_bar_hidden_state(bar_value, bar_id);
2142     else {
2143         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2144         ret = false;
2145     }
2146
2147     ysuccess(ret);
2148     if (!ret)
2149         return;
2150
2151     update_barconfig();
2152 }
2153
2154 /*
2155  * Implementation of 'shmlog <size>|toggle|on|off'
2156  *
2157  */
2158 void cmd_shmlog(I3_CMD, char *argument) {
2159     if (!strcmp(argument, "toggle"))
2160         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2161         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2162     else if (!strcmp(argument, "on"))
2163         shmlog_size = default_shmlog_size;
2164     else if (!strcmp(argument, "off"))
2165         shmlog_size = 0;
2166     else {
2167         /* If shm logging now, restart logging with the new size. */
2168         if (shmlog_size > 0) {
2169             shmlog_size = 0;
2170             LOG("Restarting shm logging...\n");
2171             init_logging();
2172         }
2173         shmlog_size = atoi(argument);
2174         /* Make a weakly attempt at ensuring the argument is valid. */
2175         if (shmlog_size <= 0)
2176             shmlog_size = default_shmlog_size;
2177     }
2178     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2179     init_logging();
2180     update_shmlog_atom();
2181     // XXX: default reply for now, make this a better reply
2182     ysuccess(true);
2183 }
2184
2185 /*
2186  * Implementation of 'debuglog toggle|on|off'
2187  *
2188  */
2189 void cmd_debuglog(I3_CMD, char *argument) {
2190     bool logging = get_debug_logging();
2191     if (!strcmp(argument, "toggle")) {
2192         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2193         set_debug_logging(!logging);
2194     } else if (!strcmp(argument, "on") && !logging) {
2195         LOG("Enabling debug logging\n");
2196         set_debug_logging(true);
2197     } else if (!strcmp(argument, "off") && logging) {
2198         LOG("Disabling debug logging\n");
2199         set_debug_logging(false);
2200     }
2201     // XXX: default reply for now, make this a better reply
2202     ysuccess(true);
2203 }