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