]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Use the already existing function to get rid of some code duplication
[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     /* Make sure we allow paths like '~/.i3/layout.json' */
906     path = resolve_tilde(path);
907
908     json_content_t content = json_determine_content(path);
909     LOG("JSON content = %d\n", content);
910     if (content == JSON_CONTENT_UNKNOWN) {
911         ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
912         ysuccess(false);
913         free(path);
914         return;
915     }
916
917     Con *parent = focused;
918     if (content == JSON_CONTENT_WORKSPACE) {
919         parent = output_get_content(con_get_output(parent));
920     } else {
921         /* We need to append the layout to a split container, since a leaf
922          * container must not have any children (by definition).
923          * Note that we explicitly check for workspaces, since they are okay for
924          * this purpose, but con_accepts_window() returns false for workspaces. */
925         while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
926             parent = parent->parent;
927     }
928     DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
929     char *errormsg = NULL;
930     tree_append_json(parent, path, &errormsg);
931     if (errormsg != NULL) {
932         yerror(errormsg);
933         free(errormsg);
934         /* Note that we continue executing since tree_append_json() has
935          * side-effects — user-provided layouts can be partly valid, partly
936          * invalid, leading to half of the placeholder containers being
937          * created. */
938     } else {
939         ysuccess(true);
940     }
941
942     // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
943     // false); should be enough, but when sending 'workspace 4; append_layout
944     // /tmp/foo.json', the needs_tree_render == true of the workspace command
945     // is not executed yet and will be batched with append_layout’s
946     // needs_tree_render after the parser finished. We should check if that is
947     // necessary at all.
948     render_con(croot, false);
949
950     restore_open_placeholder_windows(parent);
951
952     if (content == JSON_CONTENT_WORKSPACE)
953         ipc_send_workspace_event("restored", parent, NULL);
954
955     free(path);
956     cmd_output->needs_tree_render = true;
957 }
958
959 /*
960  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
961  *
962  */
963 void cmd_workspace(I3_CMD, char *which) {
964     Con *ws;
965
966     DLOG("which=%s\n", which);
967
968     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
969         LOG("Cannot switch workspace while in global fullscreen\n");
970         ysuccess(false);
971         return;
972     }
973
974     if (strcmp(which, "next") == 0)
975         ws = workspace_next();
976     else if (strcmp(which, "prev") == 0)
977         ws = workspace_prev();
978     else if (strcmp(which, "next_on_output") == 0)
979         ws = workspace_next_on_output();
980     else if (strcmp(which, "prev_on_output") == 0)
981         ws = workspace_prev_on_output();
982     else {
983         ELOG("BUG: called with which=%s\n", which);
984         ysuccess(false);
985         return;
986     }
987
988     workspace_show(ws);
989
990     cmd_output->needs_tree_render = true;
991     // XXX: default reply for now, make this a better reply
992     ysuccess(true);
993 }
994
995 /*
996  * Implementation of 'workspace number <name>'
997  *
998  */
999 void cmd_workspace_number(I3_CMD, char *which) {
1000     Con *output, *workspace = NULL;
1001
1002     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1003         LOG("Cannot switch workspace while in global fullscreen\n");
1004         ysuccess(false);
1005         return;
1006     }
1007
1008     long parsed_num = ws_name_to_number(which);
1009
1010     if (parsed_num == -1) {
1011         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1012         yerror("Could not parse number \"%s\"", which);
1013         return;
1014     }
1015
1016     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1017     GREP_FIRST(workspace, output_get_content(output),
1018                child->num == parsed_num);
1019
1020     if (!workspace) {
1021         LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1022         ysuccess(true);
1023         workspace_show_by_name(which);
1024         cmd_output->needs_tree_render = true;
1025         return;
1026     }
1027     if (maybe_back_and_forth(cmd_output, workspace->name))
1028         return;
1029     workspace_show(workspace);
1030
1031     cmd_output->needs_tree_render = true;
1032     // XXX: default reply for now, make this a better reply
1033     ysuccess(true);
1034 }
1035
1036 /*
1037  * Implementation of 'workspace back_and_forth'.
1038  *
1039  */
1040 void cmd_workspace_back_and_forth(I3_CMD) {
1041     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1042         LOG("Cannot switch workspace while in global fullscreen\n");
1043         ysuccess(false);
1044         return;
1045     }
1046
1047     workspace_back_and_forth();
1048
1049     cmd_output->needs_tree_render = true;
1050     // XXX: default reply for now, make this a better reply
1051     ysuccess(true);
1052 }
1053
1054 /*
1055  * Implementation of 'workspace <name>'
1056  *
1057  */
1058 void cmd_workspace_name(I3_CMD, char *name) {
1059     if (strncasecmp(name, "__", strlen("__")) == 0) {
1060         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1061         ysuccess(false);
1062         return;
1063     }
1064
1065     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1066         LOG("Cannot switch workspace while in global fullscreen\n");
1067         ysuccess(false);
1068         return;
1069     }
1070
1071     DLOG("should switch to workspace %s\n", name);
1072     if (maybe_back_and_forth(cmd_output, name))
1073         return;
1074
1075     Con *ws = NULL;
1076     Con *output = NULL;
1077
1078     /* first look for a workspace with this name */
1079     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1080         GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1081     }
1082
1083     /* if the name is only digits, we interpret this as a "workspace number"
1084      * command */
1085     if (!ws && name_is_digits(name)) {
1086         long parsed_num = ws_name_to_number(name);
1087         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1088             GREP_FIRST(ws, output_get_content(output),
1089                        child->num == parsed_num);
1090         }
1091     }
1092
1093     /* if no workspace was found, make a new one */
1094     if (!ws)
1095         ws = workspace_get(name, NULL);
1096
1097     workspace_show(ws);
1098
1099     cmd_output->needs_tree_render = true;
1100     // XXX: default reply for now, make this a better reply
1101     ysuccess(true);
1102 }
1103
1104 /*
1105  * Implementation of 'mark <mark>'
1106  *
1107  */
1108 void cmd_mark(I3_CMD, char *mark) {
1109     DLOG("Clearing all windows which have that mark first\n");
1110
1111     Con *con;
1112     TAILQ_FOREACH(con, &all_cons, all_cons) {
1113         if (con->mark && strcmp(con->mark, mark) == 0)
1114             FREE(con->mark);
1115     }
1116
1117     DLOG("marking window with str %s\n", mark);
1118     owindow *current;
1119
1120     HANDLE_EMPTY_MATCH;
1121
1122     TAILQ_FOREACH(current, &owindows, owindows) {
1123         DLOG("matching: %p / %s\n", current->con, current->con->name);
1124         current->con->mark = sstrdup(mark);
1125     }
1126
1127     cmd_output->needs_tree_render = true;
1128     // XXX: default reply for now, make this a better reply
1129     ysuccess(true);
1130 }
1131
1132 /*
1133  * Implementation of 'unmark [mark]'
1134  *
1135  */
1136 void cmd_unmark(I3_CMD, char *mark) {
1137     if (mark == NULL) {
1138         Con *con;
1139         TAILQ_FOREACH(con, &all_cons, all_cons) {
1140             FREE(con->mark);
1141         }
1142         DLOG("removed all window marks");
1143     } else {
1144         Con *con;
1145         TAILQ_FOREACH(con, &all_cons, all_cons) {
1146             if (con->mark && strcmp(con->mark, mark) == 0)
1147                 FREE(con->mark);
1148         }
1149         DLOG("removed window mark %s\n", mark);
1150     }
1151
1152     cmd_output->needs_tree_render = true;
1153     // XXX: default reply for now, make this a better reply
1154     ysuccess(true);
1155 }
1156
1157 /*
1158  * Implementation of 'mode <string>'.
1159  *
1160  */
1161 void cmd_mode(I3_CMD, char *mode) {
1162     DLOG("mode=%s\n", mode);
1163     switch_mode(mode);
1164
1165     // XXX: default reply for now, make this a better reply
1166     ysuccess(true);
1167 }
1168
1169 /*
1170  * Implementation of 'move [window|container] [to] output <str>'.
1171  *
1172  */
1173 void cmd_move_con_to_output(I3_CMD, char *name) {
1174     owindow *current;
1175
1176     DLOG("should move window to output %s\n", name);
1177
1178     HANDLE_EMPTY_MATCH;
1179
1180     Output *current_output = NULL;
1181     // TODO: fix the handling of criteria
1182     TAILQ_FOREACH(current, &owindows, owindows)
1183     current_output = get_output_of_con(current->con);
1184     assert(current_output != NULL);
1185
1186     Output *output = get_output_from_string(current_output, name);
1187     if (!output) {
1188         LOG("No such output found.\n");
1189         ysuccess(false);
1190         return;
1191     }
1192
1193     /* get visible workspace on output */
1194     Con *ws = NULL;
1195     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1196     if (!ws) {
1197         ysuccess(false);
1198         return;
1199     }
1200
1201     TAILQ_FOREACH(current, &owindows, owindows) {
1202         DLOG("matching: %p / %s\n", current->con, current->con->name);
1203         con_move_to_workspace(current->con, ws, true, false);
1204     }
1205
1206     cmd_output->needs_tree_render = true;
1207     // XXX: default reply for now, make this a better reply
1208     ysuccess(true);
1209 }
1210
1211 /*
1212  * Implementation of 'floating enable|disable|toggle'
1213  *
1214  */
1215 void cmd_floating(I3_CMD, char *floating_mode) {
1216     owindow *current;
1217
1218     DLOG("floating_mode=%s\n", floating_mode);
1219
1220     HANDLE_EMPTY_MATCH;
1221
1222     TAILQ_FOREACH(current, &owindows, owindows) {
1223         DLOG("matching: %p / %s\n", current->con, current->con->name);
1224         if (strcmp(floating_mode, "toggle") == 0) {
1225             DLOG("should toggle mode\n");
1226             toggle_floating_mode(current->con, false);
1227         } else {
1228             DLOG("should switch mode to %s\n", floating_mode);
1229             if (strcmp(floating_mode, "enable") == 0) {
1230                 floating_enable(current->con, false);
1231             } else {
1232                 floating_disable(current->con, false);
1233             }
1234         }
1235     }
1236
1237     cmd_output->needs_tree_render = true;
1238     // XXX: default reply for now, make this a better reply
1239     ysuccess(true);
1240 }
1241
1242 /*
1243  * Implementation of 'move workspace to [output] <str>'.
1244  *
1245  */
1246 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1247     DLOG("should move workspace to output %s\n", name);
1248
1249     HANDLE_EMPTY_MATCH;
1250
1251     owindow *current;
1252     TAILQ_FOREACH(current, &owindows, owindows) {
1253         Output *current_output = get_output_of_con(current->con);
1254         if (!current_output) {
1255             ELOG("Cannot get current output. This is a bug in i3.\n");
1256             ysuccess(false);
1257             return;
1258         }
1259         Output *output = get_output_from_string(current_output, name);
1260         if (!output) {
1261             ELOG("Could not get output from string \"%s\"\n", name);
1262             ysuccess(false);
1263             return;
1264         }
1265
1266         Con *content = output_get_content(output->con);
1267         LOG("got output %p with content %p\n", output, content);
1268
1269         Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1270         LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1271
1272         Con *ws = con_get_workspace(current->con);
1273         LOG("should move workspace %p / %s\n", ws, ws->name);
1274         bool workspace_was_visible = workspace_is_visible(ws);
1275
1276         if (con_num_children(ws->parent) == 1) {
1277             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1278
1279             /* check if we can find a workspace assigned to this output */
1280             bool used_assignment = false;
1281             struct Workspace_Assignment *assignment;
1282             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1283                 if (strcmp(assignment->output, current_output->name) != 0)
1284                     continue;
1285
1286                 /* check if this workspace is already attached to the tree */
1287                 Con *workspace = NULL, *out;
1288                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1289                 GREP_FIRST(workspace, output_get_content(out),
1290                            !strcasecmp(child->name, assignment->name));
1291                 if (workspace != NULL)
1292                     continue;
1293
1294                 /* so create the workspace referenced to by this assignment */
1295                 LOG("Creating workspace from assignment %s.\n", assignment->name);
1296                 workspace_get(assignment->name, NULL);
1297                 used_assignment = true;
1298                 break;
1299             }
1300
1301             /* if we couldn't create the workspace using an assignment, create
1302              * it on the output */
1303             if (!used_assignment)
1304                 create_workspace_on_output(current_output, ws->parent);
1305
1306             /* notify the IPC listeners */
1307             ipc_send_workspace_event("init", ws, NULL);
1308         }
1309         DLOG("Detaching\n");
1310
1311         /* detach from the old output and attach to the new output */
1312         Con *old_content = ws->parent;
1313         con_detach(ws);
1314         if (workspace_was_visible) {
1315             /* The workspace which we just detached was visible, so focus
1316              * the next one in the focus-stack. */
1317             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1318             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1319             workspace_show(focus_ws);
1320         }
1321         con_attach(ws, content, false);
1322
1323         /* fix the coordinates of the floating containers */
1324         Con *floating_con;
1325         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1326         floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1327
1328         ipc_send_workspace_event("move", ws, NULL);
1329         if (workspace_was_visible) {
1330             /* Focus the moved workspace on the destination output. */
1331             workspace_show(ws);
1332         }
1333
1334         /* NB: We cannot simply work with previously_visible_ws since it might
1335          * have been cleaned up by workspace_show() already, depending on the
1336          * focus order/number of other workspaces on the output.
1337          * Instead, we loop through the available workspaces and only work with
1338          * previously_visible_ws if we still find it. */
1339         TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1340             if (ws != previously_visible_ws)
1341                 continue;
1342
1343             /* Call the on_remove_child callback of the workspace which previously
1344              * was visible on the destination output. Since it is no longer
1345              * visible, it might need to get cleaned up. */
1346             CALL(previously_visible_ws, on_remove_child);
1347             break;
1348         }
1349     }
1350
1351     cmd_output->needs_tree_render = true;
1352     // XXX: default reply for now, make this a better reply
1353     ysuccess(true);
1354 }
1355
1356 /*
1357  * Implementation of 'split v|h|vertical|horizontal'.
1358  *
1359  */
1360 void cmd_split(I3_CMD, char *direction) {
1361     owindow *current;
1362     /* TODO: use matches */
1363     LOG("splitting in direction %c\n", direction[0]);
1364     if (match_is_empty(current_match))
1365         tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1366     else {
1367         TAILQ_FOREACH(current, &owindows, owindows) {
1368             DLOG("matching: %p / %s\n", current->con, current->con->name);
1369             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1370         }
1371     }
1372
1373     cmd_output->needs_tree_render = true;
1374     // XXX: default reply for now, make this a better reply
1375     ysuccess(true);
1376 }
1377
1378 /*
1379  * Implementation of 'kill [window|client]'.
1380  *
1381  */
1382 void cmd_kill(I3_CMD, char *kill_mode_str) {
1383     if (kill_mode_str == NULL)
1384         kill_mode_str = "window";
1385     owindow *current;
1386
1387     DLOG("kill_mode=%s\n", kill_mode_str);
1388
1389     int kill_mode;
1390     if (strcmp(kill_mode_str, "window") == 0)
1391         kill_mode = KILL_WINDOW;
1392     else if (strcmp(kill_mode_str, "client") == 0)
1393         kill_mode = KILL_CLIENT;
1394     else {
1395         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1396         ysuccess(false);
1397         return;
1398     }
1399
1400     /* check if the match is empty, not if the result is empty */
1401     if (match_is_empty(current_match))
1402         tree_close_con(kill_mode);
1403     else {
1404         TAILQ_FOREACH(current, &owindows, owindows) {
1405             DLOG("matching: %p / %s\n", current->con, current->con->name);
1406             tree_close(current->con, kill_mode, false, false);
1407         }
1408     }
1409
1410     cmd_output->needs_tree_render = true;
1411     // XXX: default reply for now, make this a better reply
1412     ysuccess(true);
1413 }
1414
1415 /*
1416  * Implementation of 'exec [--no-startup-id] <command>'.
1417  *
1418  */
1419 void cmd_exec(I3_CMD, char *nosn, char *command) {
1420     bool no_startup_id = (nosn != NULL);
1421
1422     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1423     start_application(command, no_startup_id);
1424
1425     // XXX: default reply for now, make this a better reply
1426     ysuccess(true);
1427 }
1428
1429 /*
1430  * Implementation of 'focus left|right|up|down'.
1431  *
1432  */
1433 void cmd_focus_direction(I3_CMD, char *direction) {
1434     DLOG("direction = *%s*\n", direction);
1435
1436     if (strcmp(direction, "left") == 0)
1437         tree_next('p', HORIZ);
1438     else if (strcmp(direction, "right") == 0)
1439         tree_next('n', HORIZ);
1440     else if (strcmp(direction, "up") == 0)
1441         tree_next('p', VERT);
1442     else if (strcmp(direction, "down") == 0)
1443         tree_next('n', VERT);
1444     else {
1445         ELOG("Invalid focus direction (%s)\n", direction);
1446         ysuccess(false);
1447         return;
1448     }
1449
1450     cmd_output->needs_tree_render = true;
1451     // XXX: default reply for now, make this a better reply
1452     ysuccess(true);
1453 }
1454
1455 /*
1456  * Implementation of 'focus tiling|floating|mode_toggle'.
1457  *
1458  */
1459 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1460     DLOG("window_mode = %s\n", window_mode);
1461
1462     Con *ws = con_get_workspace(focused);
1463     Con *current;
1464     if (ws != NULL) {
1465         if (strcmp(window_mode, "mode_toggle") == 0) {
1466             current = TAILQ_FIRST(&(ws->focus_head));
1467             if (current != NULL && current->type == CT_FLOATING_CON)
1468                 window_mode = "tiling";
1469             else
1470                 window_mode = "floating";
1471         }
1472         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1473             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1474                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1475                 continue;
1476
1477             con_focus(con_descend_focused(current));
1478             break;
1479         }
1480     }
1481
1482     cmd_output->needs_tree_render = true;
1483     // XXX: default reply for now, make this a better reply
1484     ysuccess(true);
1485 }
1486
1487 /*
1488  * Implementation of 'focus parent|child'.
1489  *
1490  */
1491 void cmd_focus_level(I3_CMD, char *level) {
1492     DLOG("level = %s\n", level);
1493     bool success = false;
1494
1495     /* Focusing the parent can only be allowed if the newly
1496      * focused container won't escape the fullscreen container. */
1497     if (strcmp(level, "parent") == 0) {
1498         if (focused && focused->parent) {
1499             if (con_fullscreen_permits_focusing(focused->parent))
1500                 success = level_up();
1501             else
1502                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1503         }
1504     }
1505
1506     /* Focusing a child should always be allowed. */
1507     else
1508         success = level_down();
1509
1510     cmd_output->needs_tree_render = success;
1511     // XXX: default reply for now, make this a better reply
1512     ysuccess(success);
1513 }
1514
1515 /*
1516  * Implementation of 'focus'.
1517  *
1518  */
1519 void cmd_focus(I3_CMD) {
1520     DLOG("current_match = %p\n", current_match);
1521
1522     if (match_is_empty(current_match)) {
1523         ELOG("You have to specify which window/container should be focused.\n");
1524         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1525
1526         yerror("You have to specify which window/container should be focused");
1527
1528         return;
1529     }
1530
1531     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1532     int count = 0;
1533     owindow *current;
1534     TAILQ_FOREACH(current, &owindows, owindows) {
1535         Con *ws = con_get_workspace(current->con);
1536         /* If no workspace could be found, this was a dock window.
1537          * Just skip it, you cannot focus dock windows. */
1538         if (!ws)
1539             continue;
1540
1541         /* Check the fullscreen focus constraints. */
1542         if (!con_fullscreen_permits_focusing(current->con)) {
1543             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1544             ysuccess(false);
1545             return;
1546         }
1547
1548         /* In case this is a scratchpad window, call scratchpad_show(). */
1549         if (ws == __i3_scratch) {
1550             scratchpad_show(current->con);
1551             count++;
1552             /* While for the normal focus case we can change focus multiple
1553              * times and only a single window ends up focused, we could show
1554              * multiple scratchpad windows. So, rather break here. */
1555             break;
1556         }
1557
1558         /* If the container is not on the current workspace,
1559          * workspace_show() will switch to a different workspace and (if
1560          * enabled) trigger a mouse pointer warp to the currently focused
1561          * container (!) on the target workspace.
1562          *
1563          * Therefore, before calling workspace_show(), we make sure that
1564          * 'current' will be focused on the workspace. However, we cannot
1565          * just con_focus(current) because then the pointer will not be
1566          * warped at all (the code thinks we are already there).
1567          *
1568          * So we focus 'current' to make it the currently focused window of
1569          * the target workspace, then revert focus. */
1570         Con *currently_focused = focused;
1571         con_focus(current->con);
1572         con_focus(currently_focused);
1573
1574         /* Now switch to the workspace, then focus */
1575         workspace_show(ws);
1576         LOG("focusing %p / %s\n", current->con, current->con->name);
1577         con_focus(current->con);
1578         count++;
1579     }
1580
1581     if (count > 1)
1582         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1583             "while only exactly one container can be focused at a time.\n",
1584             count);
1585
1586     cmd_output->needs_tree_render = true;
1587     // XXX: default reply for now, make this a better reply
1588     ysuccess(true);
1589 }
1590
1591 /*
1592  * Implementation of 'fullscreen enable|toggle [global]' and
1593  *                   'fullscreen disable'
1594  *
1595  */
1596 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1597     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1598     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1599     owindow *current;
1600
1601     HANDLE_EMPTY_MATCH;
1602
1603     TAILQ_FOREACH(current, &owindows, owindows) {
1604         DLOG("matching: %p / %s\n", current->con, current->con->name);
1605         if (strcmp(action, "toggle") == 0) {
1606             con_toggle_fullscreen(current->con, mode);
1607         } else if (strcmp(action, "enable") == 0) {
1608             con_enable_fullscreen(current->con, mode);
1609         } else if (strcmp(action, "disable") == 0) {
1610             con_disable_fullscreen(current->con);
1611         }
1612     }
1613
1614     cmd_output->needs_tree_render = true;
1615     // XXX: default reply for now, make this a better reply
1616     ysuccess(true);
1617 }
1618
1619 /*
1620  * Implementation of 'move <direction> [<pixels> [px]]'.
1621  *
1622  */
1623 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1624     // 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
1625     int px = atoi(move_px);
1626
1627     owindow *current;
1628     HANDLE_EMPTY_MATCH;
1629
1630     Con *initially_focused = focused;
1631
1632     TAILQ_FOREACH(current, &owindows, owindows) {
1633         DLOG("moving in direction %s, px %s\n", direction, move_px);
1634         if (con_is_floating(current->con)) {
1635             DLOG("floating move with %d pixels\n", px);
1636             Rect newrect = current->con->parent->rect;
1637             if (strcmp(direction, "left") == 0) {
1638                 newrect.x -= px;
1639             } else if (strcmp(direction, "right") == 0) {
1640                 newrect.x += px;
1641             } else if (strcmp(direction, "up") == 0) {
1642                 newrect.y -= px;
1643             } else if (strcmp(direction, "down") == 0) {
1644                 newrect.y += px;
1645             }
1646             floating_reposition(current->con->parent, newrect);
1647         } else {
1648             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1649             cmd_output->needs_tree_render = true;
1650         }
1651     }
1652
1653     /* the move command should not disturb focus */
1654     if (focused != initially_focused)
1655         con_focus(initially_focused);
1656
1657     // XXX: default reply for now, make this a better reply
1658     ysuccess(true);
1659 }
1660
1661 /*
1662  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1663  *
1664  */
1665 void cmd_layout(I3_CMD, char *layout_str) {
1666     if (strcmp(layout_str, "stacking") == 0)
1667         layout_str = "stacked";
1668     owindow *current;
1669     layout_t layout;
1670     /* default is a special case which will be handled in con_set_layout(). */
1671     if (strcmp(layout_str, "default") == 0)
1672         layout = L_DEFAULT;
1673     else if (strcmp(layout_str, "stacked") == 0)
1674         layout = L_STACKED;
1675     else if (strcmp(layout_str, "tabbed") == 0)
1676         layout = L_TABBED;
1677     else if (strcmp(layout_str, "splitv") == 0)
1678         layout = L_SPLITV;
1679     else if (strcmp(layout_str, "splith") == 0)
1680         layout = L_SPLITH;
1681     else {
1682         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1683         return;
1684     }
1685
1686     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1687
1688     /* check if the match is empty, not if the result is empty */
1689     if (match_is_empty(current_match))
1690         con_set_layout(focused, layout);
1691     else {
1692         TAILQ_FOREACH(current, &owindows, owindows) {
1693             DLOG("matching: %p / %s\n", current->con, current->con->name);
1694             con_set_layout(current->con, layout);
1695         }
1696     }
1697
1698     cmd_output->needs_tree_render = true;
1699     // XXX: default reply for now, make this a better reply
1700     ysuccess(true);
1701 }
1702
1703 /*
1704  * Implementation of 'layout toggle [all|split]'.
1705  *
1706  */
1707 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1708     owindow *current;
1709
1710     if (toggle_mode == NULL)
1711         toggle_mode = "default";
1712
1713     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1714
1715     /* check if the match is empty, not if the result is empty */
1716     if (match_is_empty(current_match))
1717         con_toggle_layout(focused, toggle_mode);
1718     else {
1719         TAILQ_FOREACH(current, &owindows, owindows) {
1720             DLOG("matching: %p / %s\n", current->con, current->con->name);
1721             con_toggle_layout(current->con, toggle_mode);
1722         }
1723     }
1724
1725     cmd_output->needs_tree_render = true;
1726     // XXX: default reply for now, make this a better reply
1727     ysuccess(true);
1728 }
1729
1730 /*
1731  * Implementation of 'exit'.
1732  *
1733  */
1734 void cmd_exit(I3_CMD) {
1735     LOG("Exiting due to user command.\n");
1736     ipc_shutdown();
1737     unlink(config.ipc_socket_path);
1738     xcb_disconnect(conn);
1739     exit(0);
1740
1741     /* unreached */
1742 }
1743
1744 /*
1745  * Implementation of 'reload'.
1746  *
1747  */
1748 void cmd_reload(I3_CMD) {
1749     LOG("reloading\n");
1750     kill_nagbar(&config_error_nagbar_pid, false);
1751     kill_nagbar(&command_error_nagbar_pid, false);
1752     load_configuration(conn, NULL, true);
1753     x_set_i3_atoms();
1754     /* Send an IPC event just in case the ws names have changed */
1755     ipc_send_workspace_event("reload", NULL, NULL);
1756     /* Send an update event for the barconfig just in case it has changed */
1757     update_barconfig();
1758
1759     // XXX: default reply for now, make this a better reply
1760     ysuccess(true);
1761 }
1762
1763 /*
1764  * Implementation of 'restart'.
1765  *
1766  */
1767 void cmd_restart(I3_CMD) {
1768     LOG("restarting i3\n");
1769     ipc_shutdown();
1770     unlink(config.ipc_socket_path);
1771     /* We need to call this manually since atexit handlers don’t get called
1772      * when exec()ing */
1773     purge_zerobyte_logfile();
1774     i3_restart(false);
1775
1776     // XXX: default reply for now, make this a better reply
1777     ysuccess(true);
1778 }
1779
1780 /*
1781  * Implementation of 'open'.
1782  *
1783  */
1784 void cmd_open(I3_CMD) {
1785     LOG("opening new container\n");
1786     Con *con = tree_open_con(NULL, NULL);
1787     con->layout = L_SPLITH;
1788     con_focus(con);
1789
1790     y(map_open);
1791     ystr("success");
1792     y(bool, true);
1793     ystr("id");
1794     y(integer, (long int)con);
1795     y(map_close);
1796
1797     cmd_output->needs_tree_render = true;
1798 }
1799
1800 /*
1801  * Implementation of 'focus output <output>'.
1802  *
1803  */
1804 void cmd_focus_output(I3_CMD, char *name) {
1805     owindow *current;
1806
1807     DLOG("name = %s\n", name);
1808
1809     HANDLE_EMPTY_MATCH;
1810
1811     /* get the output */
1812     Output *current_output = NULL;
1813     Output *output;
1814
1815     TAILQ_FOREACH(current, &owindows, owindows)
1816     current_output = get_output_of_con(current->con);
1817     assert(current_output != NULL);
1818
1819     output = get_output_from_string(current_output, name);
1820
1821     if (!output) {
1822         LOG("No such output found.\n");
1823         ysuccess(false);
1824         return;
1825     }
1826
1827     /* get visible workspace on output */
1828     Con *ws = NULL;
1829     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1830     if (!ws) {
1831         ysuccess(false);
1832         return;
1833     }
1834
1835     workspace_show(ws);
1836
1837     cmd_output->needs_tree_render = true;
1838     // XXX: default reply for now, make this a better reply
1839     ysuccess(true);
1840 }
1841
1842 /*
1843  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1844  *
1845  */
1846 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1847     int x = atoi(cx);
1848     int y = atoi(cy);
1849     bool has_error = false;
1850
1851     owindow *current;
1852     HANDLE_EMPTY_MATCH;
1853
1854     TAILQ_FOREACH(current, &owindows, owindows) {
1855         if (!con_is_floating(current->con)) {
1856             ELOG("Cannot change position. The window/container is not floating\n");
1857
1858             if (!has_error) {
1859                 yerror("Cannot change position of a window/container because it is not floating.");
1860                 has_error = true;
1861             }
1862
1863             continue;
1864         }
1865
1866         if (strcmp(method, "absolute") == 0) {
1867             current->con->parent->rect.x = x;
1868             current->con->parent->rect.y = y;
1869
1870             DLOG("moving to absolute position %d %d\n", x, y);
1871             floating_maybe_reassign_ws(current->con->parent);
1872             cmd_output->needs_tree_render = true;
1873         }
1874
1875         if (strcmp(method, "position") == 0) {
1876             Rect newrect = current->con->parent->rect;
1877
1878             DLOG("moving to position %d %d\n", x, y);
1879             newrect.x = x;
1880             newrect.y = y;
1881
1882             floating_reposition(current->con->parent, newrect);
1883         }
1884     }
1885
1886     // XXX: default reply for now, make this a better reply
1887     if (!has_error)
1888         ysuccess(true);
1889 }
1890
1891 /*
1892  * Implementation of 'move [window|container] [to] [absolute] position center
1893  *
1894  */
1895 void cmd_move_window_to_center(I3_CMD, char *method) {
1896     if (!con_is_floating(focused)) {
1897         ELOG("Cannot change position. The window/container is not floating\n");
1898         yerror("Cannot change position. The window/container is not floating.");
1899         return;
1900     }
1901
1902     if (strcmp(method, "absolute") == 0) {
1903         Rect *rect = &focused->parent->rect;
1904
1905         DLOG("moving to absolute center\n");
1906         rect->x = croot->rect.width / 2 - rect->width / 2;
1907         rect->y = croot->rect.height / 2 - rect->height / 2;
1908
1909         floating_maybe_reassign_ws(focused->parent);
1910         cmd_output->needs_tree_render = true;
1911     }
1912
1913     if (strcmp(method, "position") == 0) {
1914         Rect *wsrect = &con_get_workspace(focused)->rect;
1915         Rect newrect = focused->parent->rect;
1916
1917         DLOG("moving to center\n");
1918         newrect.x = wsrect->width / 2 - newrect.width / 2;
1919         newrect.y = wsrect->height / 2 - newrect.height / 2;
1920
1921         floating_reposition(focused->parent, newrect);
1922     }
1923
1924     // XXX: default reply for now, make this a better reply
1925     ysuccess(true);
1926 }
1927
1928 /*
1929  * Implementation of 'move scratchpad'.
1930  *
1931  */
1932 void cmd_move_scratchpad(I3_CMD) {
1933     DLOG("should move window to scratchpad\n");
1934     owindow *current;
1935
1936     HANDLE_EMPTY_MATCH;
1937
1938     TAILQ_FOREACH(current, &owindows, owindows) {
1939         DLOG("matching: %p / %s\n", current->con, current->con->name);
1940         scratchpad_move(current->con);
1941     }
1942
1943     cmd_output->needs_tree_render = true;
1944     // XXX: default reply for now, make this a better reply
1945     ysuccess(true);
1946 }
1947
1948 /*
1949  * Implementation of 'scratchpad show'.
1950  *
1951  */
1952 void cmd_scratchpad_show(I3_CMD) {
1953     DLOG("should show scratchpad window\n");
1954     owindow *current;
1955
1956     if (match_is_empty(current_match)) {
1957         scratchpad_show(NULL);
1958     } else {
1959         TAILQ_FOREACH(current, &owindows, owindows) {
1960             DLOG("matching: %p / %s\n", current->con, current->con->name);
1961             scratchpad_show(current->con);
1962         }
1963     }
1964
1965     cmd_output->needs_tree_render = true;
1966     // XXX: default reply for now, make this a better reply
1967     ysuccess(true);
1968 }
1969
1970 /*
1971  * Implementation of 'rename workspace [<name>] to <name>'
1972  *
1973  */
1974 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1975     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1976         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1977         ysuccess(false);
1978         return;
1979     }
1980     if (old_name) {
1981         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1982     } else {
1983         LOG("Renaming current workspace to \"%s\"\n", new_name);
1984     }
1985
1986     Con *output, *workspace = NULL;
1987     if (old_name) {
1988         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1989         GREP_FIRST(workspace, output_get_content(output),
1990                    !strcasecmp(child->name, old_name));
1991     } else {
1992         workspace = con_get_workspace(focused);
1993     }
1994
1995     if (!workspace) {
1996         yerror("Old workspace \"%s\" not found", old_name);
1997         return;
1998     }
1999
2000     Con *check_dest = NULL;
2001     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2002     GREP_FIRST(check_dest, output_get_content(output),
2003                !strcasecmp(child->name, new_name));
2004
2005     if (check_dest != NULL) {
2006         yerror("New workspace \"%s\" already exists", new_name);
2007         return;
2008     }
2009
2010     /* Change the name and try to parse it as a number. */
2011     FREE(workspace->name);
2012     workspace->name = sstrdup(new_name);
2013
2014     workspace->num = ws_name_to_number(new_name);
2015     LOG("num = %d\n", workspace->num);
2016
2017     /* By re-attaching, the sort order will be correct afterwards. */
2018     Con *previously_focused = focused;
2019     Con *parent = workspace->parent;
2020     con_detach(workspace);
2021     con_attach(workspace, parent, false);
2022     /* Restore the previous focus since con_attach messes with the focus. */
2023     con_focus(previously_focused);
2024
2025     cmd_output->needs_tree_render = true;
2026     ysuccess(true);
2027
2028     ipc_send_workspace_event("rename", workspace, NULL);
2029     ewmh_update_desktop_names();
2030     ewmh_update_desktop_viewport();
2031     ewmh_update_current_desktop();
2032 }
2033
2034 /*
2035  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2036  *
2037  */
2038 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2039     int mode = M_DOCK;
2040     bool toggle = false;
2041     if (strcmp(bar_mode, "dock") == 0)
2042         mode = M_DOCK;
2043     else if (strcmp(bar_mode, "hide") == 0)
2044         mode = M_HIDE;
2045     else if (strcmp(bar_mode, "invisible") == 0)
2046         mode = M_INVISIBLE;
2047     else if (strcmp(bar_mode, "toggle") == 0)
2048         toggle = true;
2049     else {
2050         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2051         return false;
2052     }
2053
2054     bool changed_sth = false;
2055     Barconfig *current = NULL;
2056     TAILQ_FOREACH(current, &barconfigs, configs) {
2057         if (bar_id && strcmp(current->id, bar_id) != 0)
2058             continue;
2059
2060         if (toggle)
2061             mode = (current->mode + 1) % 2;
2062
2063         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2064         current->mode = mode;
2065         changed_sth = true;
2066
2067         if (bar_id)
2068             break;
2069     }
2070
2071     if (bar_id && !changed_sth) {
2072         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2073         return false;
2074     }
2075
2076     return true;
2077 }
2078
2079 /*
2080  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2081  *
2082  */
2083 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2084     int hidden_state = S_SHOW;
2085     bool toggle = false;
2086     if (strcmp(bar_hidden_state, "hide") == 0)
2087         hidden_state = S_HIDE;
2088     else if (strcmp(bar_hidden_state, "show") == 0)
2089         hidden_state = S_SHOW;
2090     else if (strcmp(bar_hidden_state, "toggle") == 0)
2091         toggle = true;
2092     else {
2093         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2094         return false;
2095     }
2096
2097     bool changed_sth = false;
2098     Barconfig *current = NULL;
2099     TAILQ_FOREACH(current, &barconfigs, configs) {
2100         if (bar_id && strcmp(current->id, bar_id) != 0)
2101             continue;
2102
2103         if (toggle)
2104             hidden_state = (current->hidden_state + 1) % 2;
2105
2106         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2107         current->hidden_state = hidden_state;
2108         changed_sth = true;
2109
2110         if (bar_id)
2111             break;
2112     }
2113
2114     if (bar_id && !changed_sth) {
2115         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2116         return false;
2117     }
2118
2119     return true;
2120 }
2121
2122 /*
2123  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2124  *
2125  */
2126 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2127     bool ret;
2128     if (strcmp(bar_type, "mode") == 0)
2129         ret = cmd_bar_mode(bar_value, bar_id);
2130     else if (strcmp(bar_type, "hidden_state") == 0)
2131         ret = cmd_bar_hidden_state(bar_value, bar_id);
2132     else {
2133         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2134         ret = false;
2135     }
2136
2137     ysuccess(ret);
2138     if (!ret)
2139         return;
2140
2141     update_barconfig();
2142 }
2143
2144 /*
2145  * Implementation of 'shmlog <size>|toggle|on|off'
2146  *
2147  */
2148 void cmd_shmlog(I3_CMD, char *argument) {
2149     if (!strcmp(argument, "toggle"))
2150         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2151         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2152     else if (!strcmp(argument, "on"))
2153         shmlog_size = default_shmlog_size;
2154     else if (!strcmp(argument, "off"))
2155         shmlog_size = 0;
2156     else {
2157         /* If shm logging now, restart logging with the new size. */
2158         if (shmlog_size > 0) {
2159             shmlog_size = 0;
2160             LOG("Restarting shm logging...\n");
2161             init_logging();
2162         }
2163         shmlog_size = atoi(argument);
2164         /* Make a weakly attempt at ensuring the argument is valid. */
2165         if (shmlog_size <= 0)
2166             shmlog_size = default_shmlog_size;
2167     }
2168     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2169     init_logging();
2170     update_shmlog_atom();
2171     // XXX: default reply for now, make this a better reply
2172     ysuccess(true);
2173 }
2174
2175 /*
2176  * Implementation of 'debuglog toggle|on|off'
2177  *
2178  */
2179 void cmd_debuglog(I3_CMD, char *argument) {
2180     bool logging = get_debug_logging();
2181     if (!strcmp(argument, "toggle")) {
2182         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2183         set_debug_logging(!logging);
2184     } else if (!strcmp(argument, "on") && !logging) {
2185         LOG("Enabling debug logging\n");
2186         set_debug_logging(true);
2187     } else if (!strcmp(argument, "off") && logging) {
2188         LOG("Disabling debug logging\n");
2189         set_debug_logging(false);
2190     }
2191     // XXX: default reply for now, make this a better reply
2192     ysuccess(true);
2193 }