]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Support _NET_WM_VISIBLE_NAME. As per specification this is necessary since we can...
[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);
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);
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);
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);
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     if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
603         strcmp(direction, "height") == 0) {
604         if (px < 0)
605             px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
606         else
607             px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
608     } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
609         if (px < 0)
610             px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
611         else
612             px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
613     }
614
615     if (strcmp(direction, "up") == 0) {
616         floating_con->rect.height += px;
617     } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
618         floating_con->rect.height += px;
619     } else if (strcmp(direction, "left") == 0) {
620         floating_con->rect.width += px;
621     } else {
622         floating_con->rect.width += px;
623     }
624
625     floating_check_size(floating_con);
626
627     /* Did we actually resize anything or did the size constraints prevent us?
628      * If we could not resize, exit now to not move the window. */
629     if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
630         return;
631
632     if (strcmp(direction, "up") == 0) {
633         floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
634     } else if (strcmp(direction, "left") == 0) {
635         floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
636     }
637
638     /* If this is a scratchpad window, don't auto center it from now on. */
639     if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
640         floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
641 }
642
643 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
644     LOG("tiling resize\n");
645     Con *second = NULL;
646     Con *first = current;
647     direction_t search_direction;
648     if (!strcmp(direction, "left"))
649         search_direction = D_LEFT;
650     else if (!strcmp(direction, "right"))
651         search_direction = D_RIGHT;
652     else if (!strcmp(direction, "up"))
653         search_direction = D_UP;
654     else
655         search_direction = D_DOWN;
656
657     bool res = resize_find_tiling_participants(&first, &second, search_direction);
658     if (!res) {
659         LOG("No second container in this direction found.\n");
660         ysuccess(false);
661         return false;
662     }
663
664     /* get the default percentage */
665     int children = con_num_children(first->parent);
666     LOG("ins. %d children\n", children);
667     double percentage = 1.0 / children;
668     LOG("default percentage = %f\n", percentage);
669
670     /* resize */
671     LOG("second->percent = %f\n", second->percent);
672     LOG("first->percent before = %f\n", first->percent);
673     if (first->percent == 0.0)
674         first->percent = percentage;
675     if (second->percent == 0.0)
676         second->percent = percentage;
677     double new_first_percent = first->percent + ((double)ppt / 100.0);
678     double new_second_percent = second->percent - ((double)ppt / 100.0);
679     LOG("new_first_percent = %f\n", new_first_percent);
680     LOG("new_second_percent = %f\n", new_second_percent);
681     /* Ensure that the new percentages are positive and greater than
682      * 0.05 to have a reasonable minimum size. */
683     if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
684         definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
685         first->percent += ((double)ppt / 100.0);
686         second->percent -= ((double)ppt / 100.0);
687         LOG("first->percent after = %f\n", first->percent);
688         LOG("second->percent after = %f\n", second->percent);
689     } else {
690         LOG("Not resizing, already at minimum size\n");
691     }
692
693     return true;
694 }
695
696 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
697     LOG("width/height resize\n");
698     /* get the appropriate current container (skip stacked/tabbed cons) */
699     while (current->parent->layout == L_STACKED ||
700            current->parent->layout == L_TABBED)
701         current = current->parent;
702
703     /* Then further go up until we find one with the matching orientation. */
704     orientation_t search_orientation =
705         (strcmp(direction, "width") == 0 ? HORIZ : VERT);
706
707     while (current->type != CT_WORKSPACE &&
708            current->type != CT_FLOATING_CON &&
709            con_orientation(current->parent) != search_orientation)
710         current = current->parent;
711
712     /* get the default percentage */
713     int children = con_num_children(current->parent);
714     LOG("ins. %d children\n", children);
715     double percentage = 1.0 / children;
716     LOG("default percentage = %f\n", percentage);
717
718     orientation_t orientation = con_orientation(current->parent);
719
720     if ((orientation == HORIZ &&
721          strcmp(direction, "height") == 0) ||
722         (orientation == VERT &&
723          strcmp(direction, "width") == 0)) {
724         LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
725             (orientation == HORIZ ? "horizontal" : "vertical"));
726         ysuccess(false);
727         return false;
728     }
729
730     if (children == 1) {
731         LOG("This is the only container, cannot resize.\n");
732         ysuccess(false);
733         return false;
734     }
735
736     /* Ensure all the other children have a percentage set. */
737     Con *child;
738     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
739         LOG("child->percent = %f (child %p)\n", child->percent, child);
740         if (child->percent == 0.0)
741             child->percent = percentage;
742     }
743
744     double new_current_percent = current->percent + ((double)ppt / 100.0);
745     double subtract_percent = ((double)ppt / 100.0) / (children - 1);
746     LOG("new_current_percent = %f\n", new_current_percent);
747     LOG("subtract_percent = %f\n", subtract_percent);
748     /* Ensure that the new percentages are positive and greater than
749      * 0.05 to have a reasonable minimum size. */
750     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
751         if (child == current)
752             continue;
753         if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
754             LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
755             ysuccess(false);
756             return false;
757         }
758     }
759     if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
760         LOG("Not resizing, already at minimum size\n");
761         ysuccess(false);
762         return false;
763     }
764
765     current->percent += ((double)ppt / 100.0);
766     LOG("current->percent after = %f\n", current->percent);
767
768     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
769         if (child == current)
770             continue;
771         child->percent -= subtract_percent;
772         LOG("child->percent after (%p) = %f\n", child, child->percent);
773     }
774
775     return true;
776 }
777
778 /*
779  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
780  *
781  */
782 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
783     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
784     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
785     // 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
786     int px = atoi(resize_px);
787     int ppt = atoi(resize_ppt);
788     if (strcmp(way, "shrink") == 0) {
789         px *= -1;
790         ppt *= -1;
791     }
792
793     HANDLE_EMPTY_MATCH;
794
795     owindow *current;
796     TAILQ_FOREACH(current, &owindows, owindows) {
797         /* Don't handle dock windows (issue #1201) */
798         if (current->con->window && current->con->window->dock) {
799             DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
800             continue;
801         }
802
803         Con *floating_con;
804         if ((floating_con = con_inside_floating(current->con))) {
805             cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
806         } else {
807             if (strcmp(direction, "width") == 0 ||
808                 strcmp(direction, "height") == 0) {
809                 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
810                     return;
811             } else {
812                 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
813                     return;
814             }
815         }
816     }
817
818     cmd_output->needs_tree_render = true;
819     // XXX: default reply for now, make this a better reply
820     ysuccess(true);
821 }
822
823 /*
824  * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
825  *
826  */
827 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
828     DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
829     owindow *current;
830
831     HANDLE_EMPTY_MATCH;
832
833     TAILQ_FOREACH(current, &owindows, owindows) {
834         DLOG("matching: %p / %s\n", current->con, current->con->name);
835         int border_style = current->con->border_style;
836         char *end;
837         int tmp_border_width = -1;
838         tmp_border_width = strtol(border_width, &end, 10);
839         if (end == border_width) {
840             /* no valid digits found */
841             tmp_border_width = -1;
842         }
843         if (strcmp(border_style_str, "toggle") == 0) {
844             border_style++;
845             border_style %= 3;
846             if (border_style == BS_NORMAL)
847                 tmp_border_width = 2;
848             else if (border_style == BS_NONE)
849                 tmp_border_width = 0;
850             else if (border_style == BS_PIXEL)
851                 tmp_border_width = 1;
852         } else {
853             if (strcmp(border_style_str, "normal") == 0)
854                 border_style = BS_NORMAL;
855             else if (strcmp(border_style_str, "pixel") == 0)
856                 border_style = BS_PIXEL;
857             else if (strcmp(border_style_str, "1pixel") == 0) {
858                 border_style = BS_PIXEL;
859                 tmp_border_width = 1;
860             } else if (strcmp(border_style_str, "none") == 0)
861                 border_style = BS_NONE;
862             else {
863                 ELOG("BUG: called with border_style=%s\n", border_style_str);
864                 ysuccess(false);
865                 return;
866             }
867         }
868         con_set_border_style(current->con, border_style, tmp_border_width);
869     }
870
871     cmd_output->needs_tree_render = true;
872     // XXX: default reply for now, make this a better reply
873     ysuccess(true);
874 }
875
876 /*
877  * Implementation of 'nop <comment>'.
878  *
879  */
880 void cmd_nop(I3_CMD, char *comment) {
881     LOG("-------------------------------------------------\n");
882     LOG("  NOP: %s\n", comment);
883     LOG("-------------------------------------------------\n");
884 }
885
886 /*
887  * Implementation of 'append_layout <path>'.
888  *
889  */
890 void cmd_append_layout(I3_CMD, char *path) {
891     LOG("Appending layout \"%s\"\n", path);
892
893     /* Make sure we allow paths like '~/.i3/layout.json' */
894     path = resolve_tilde(path);
895
896     json_content_t content = json_determine_content(path);
897     LOG("JSON content = %d\n", content);
898     if (content == JSON_CONTENT_UNKNOWN) {
899         ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
900         yerror("Could not determine the contents of \"%s\".", path);
901         free(path);
902         return;
903     }
904
905     Con *parent = focused;
906     if (content == JSON_CONTENT_WORKSPACE) {
907         parent = output_get_content(con_get_output(parent));
908     } else {
909         /* We need to append the layout to a split container, since a leaf
910          * container must not have any children (by definition).
911          * Note that we explicitly check for workspaces, since they are okay for
912          * this purpose, but con_accepts_window() returns false for workspaces. */
913         while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
914             parent = parent->parent;
915     }
916     DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
917     char *errormsg = NULL;
918     tree_append_json(parent, path, &errormsg);
919     if (errormsg != NULL) {
920         yerror(errormsg);
921         free(errormsg);
922         /* Note that we continue executing since tree_append_json() has
923          * side-effects — user-provided layouts can be partly valid, partly
924          * invalid, leading to half of the placeholder containers being
925          * created. */
926     } else {
927         ysuccess(true);
928     }
929
930     // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
931     // false); should be enough, but when sending 'workspace 4; append_layout
932     // /tmp/foo.json', the needs_tree_render == true of the workspace command
933     // is not executed yet and will be batched with append_layout’s
934     // needs_tree_render after the parser finished. We should check if that is
935     // necessary at all.
936     render_con(croot, false);
937
938     restore_open_placeholder_windows(parent);
939
940     if (content == JSON_CONTENT_WORKSPACE)
941         ipc_send_workspace_event("restored", parent, NULL);
942
943     free(path);
944     cmd_output->needs_tree_render = true;
945 }
946
947 /*
948  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
949  *
950  */
951 void cmd_workspace(I3_CMD, char *which) {
952     Con *ws;
953
954     DLOG("which=%s\n", which);
955
956     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
957         LOG("Cannot switch workspace while in global fullscreen\n");
958         ysuccess(false);
959         return;
960     }
961
962     if (strcmp(which, "next") == 0)
963         ws = workspace_next();
964     else if (strcmp(which, "prev") == 0)
965         ws = workspace_prev();
966     else if (strcmp(which, "next_on_output") == 0)
967         ws = workspace_next_on_output();
968     else if (strcmp(which, "prev_on_output") == 0)
969         ws = workspace_prev_on_output();
970     else {
971         ELOG("BUG: called with which=%s\n", which);
972         ysuccess(false);
973         return;
974     }
975
976     workspace_show(ws);
977
978     cmd_output->needs_tree_render = true;
979     // XXX: default reply for now, make this a better reply
980     ysuccess(true);
981 }
982
983 /*
984  * Implementation of 'workspace number <name>'
985  *
986  */
987 void cmd_workspace_number(I3_CMD, char *which) {
988     Con *output, *workspace = NULL;
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     long parsed_num = ws_name_to_number(which);
997
998     if (parsed_num == -1) {
999         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1000         yerror("Could not parse number \"%s\"", which);
1001         return;
1002     }
1003
1004     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1005     GREP_FIRST(workspace, output_get_content(output),
1006                child->num == parsed_num);
1007
1008     if (!workspace) {
1009         LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1010         ysuccess(true);
1011         workspace_show_by_name(which);
1012         cmd_output->needs_tree_render = true;
1013         return;
1014     }
1015     if (maybe_back_and_forth(cmd_output, workspace->name))
1016         return;
1017     workspace_show(workspace);
1018
1019     cmd_output->needs_tree_render = true;
1020     // XXX: default reply for now, make this a better reply
1021     ysuccess(true);
1022 }
1023
1024 /*
1025  * Implementation of 'workspace back_and_forth'.
1026  *
1027  */
1028 void cmd_workspace_back_and_forth(I3_CMD) {
1029     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1030         LOG("Cannot switch workspace while in global fullscreen\n");
1031         ysuccess(false);
1032         return;
1033     }
1034
1035     workspace_back_and_forth();
1036
1037     cmd_output->needs_tree_render = true;
1038     // XXX: default reply for now, make this a better reply
1039     ysuccess(true);
1040 }
1041
1042 /*
1043  * Implementation of 'workspace <name>'
1044  *
1045  */
1046 void cmd_workspace_name(I3_CMD, char *name) {
1047     if (strncasecmp(name, "__", strlen("__")) == 0) {
1048         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1049         ysuccess(false);
1050         return;
1051     }
1052
1053     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1054         LOG("Cannot switch workspace while in global fullscreen\n");
1055         ysuccess(false);
1056         return;
1057     }
1058
1059     DLOG("should switch to workspace %s\n", name);
1060     if (maybe_back_and_forth(cmd_output, name))
1061         return;
1062     workspace_show_by_name(name);
1063
1064     cmd_output->needs_tree_render = true;
1065     // XXX: default reply for now, make this a better reply
1066     ysuccess(true);
1067 }
1068
1069 /*
1070  * Implementation of 'mark [--toggle] <mark>'
1071  *
1072  */
1073 void cmd_mark(I3_CMD, char *mark, char *toggle) {
1074     HANDLE_EMPTY_MATCH;
1075
1076     owindow *current = TAILQ_FIRST(&owindows);
1077     if (current == NULL) {
1078         ysuccess(false);
1079         return;
1080     }
1081
1082     /* Marks must be unique, i.e., no two windows must have the same mark. */
1083     if (current != TAILQ_LAST(&owindows, owindows_head)) {
1084         yerror("A mark must not be put onto more than one window");
1085         return;
1086     }
1087
1088     DLOG("matching: %p / %s\n", current->con, current->con->name);
1089     current->con->mark_changed = true;
1090     if (toggle != NULL && current->con->mark && strcmp(current->con->mark, mark) == 0) {
1091         DLOG("removing window mark %s\n", mark);
1092         FREE(current->con->mark);
1093     } else {
1094         DLOG("marking window with str %s\n", mark);
1095         FREE(current->con->mark);
1096         current->con->mark = sstrdup(mark);
1097     }
1098
1099     DLOG("Clearing all non-matched windows with this mark\n");
1100     Con *con;
1101     TAILQ_FOREACH(con, &all_cons, all_cons) {
1102         /* Skip matched window, we took care of it already. */
1103         if (current->con == con)
1104             continue;
1105
1106         if (con->mark && strcmp(con->mark, mark) == 0) {
1107             FREE(con->mark);
1108             con->mark_changed = true;
1109         }
1110     }
1111
1112     cmd_output->needs_tree_render = true;
1113     // XXX: default reply for now, make this a better reply
1114     ysuccess(true);
1115 }
1116
1117 /*
1118  * Implementation of 'unmark [mark]'
1119  *
1120  */
1121 void cmd_unmark(I3_CMD, char *mark) {
1122     if (mark == NULL) {
1123         Con *con;
1124         TAILQ_FOREACH(con, &all_cons, all_cons) {
1125             if (con->mark == NULL)
1126                 continue;
1127
1128             FREE(con->mark);
1129             con->mark_changed = true;
1130         }
1131         DLOG("Removed all window marks.\n");
1132     } else {
1133         Con *con = con_by_mark(mark);
1134         if (con != NULL) {
1135             FREE(con->mark);
1136             con->mark_changed = true;
1137         }
1138         DLOG("Removed window mark \"%s\".\n", mark);
1139     }
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     owindow *current;
1164
1165     DLOG("should move window to output %s\n", name);
1166
1167     HANDLE_EMPTY_MATCH;
1168
1169     Output *current_output = NULL;
1170     // TODO: fix the handling of criteria
1171     TAILQ_FOREACH(current, &owindows, owindows)
1172     current_output = get_output_of_con(current->con);
1173     assert(current_output != NULL);
1174
1175     Output *output = get_output_from_string(current_output, name);
1176     if (!output) {
1177         LOG("No such output found.\n");
1178         ysuccess(false);
1179         return;
1180     }
1181
1182     /* get visible workspace on output */
1183     Con *ws = NULL;
1184     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1185     if (!ws) {
1186         ysuccess(false);
1187         return;
1188     }
1189
1190     TAILQ_FOREACH(current, &owindows, owindows) {
1191         DLOG("matching: %p / %s\n", current->con, current->con->name);
1192         con_move_to_workspace(current->con, ws, true, false);
1193     }
1194
1195     cmd_output->needs_tree_render = true;
1196     // XXX: default reply for now, make this a better reply
1197     ysuccess(true);
1198 }
1199
1200 /*
1201  * Implementation of 'move [container|window] [to] mark <str>'.
1202  *
1203  */
1204 void cmd_move_con_to_mark(I3_CMD, char *mark) {
1205     DLOG("moving window to mark \"%s\"\n", mark);
1206
1207     HANDLE_EMPTY_MATCH;
1208
1209     bool result = true;
1210     owindow *current;
1211     TAILQ_FOREACH(current, &owindows, owindows) {
1212         DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1213         result &= con_move_to_mark(current->con, mark);
1214     }
1215
1216     cmd_output->needs_tree_render = true;
1217     ysuccess(result);
1218 }
1219
1220 /*
1221  * Implementation of 'floating enable|disable|toggle'
1222  *
1223  */
1224 void cmd_floating(I3_CMD, char *floating_mode) {
1225     owindow *current;
1226
1227     DLOG("floating_mode=%s\n", floating_mode);
1228
1229     HANDLE_EMPTY_MATCH;
1230
1231     TAILQ_FOREACH(current, &owindows, owindows) {
1232         DLOG("matching: %p / %s\n", current->con, current->con->name);
1233         if (strcmp(floating_mode, "toggle") == 0) {
1234             DLOG("should toggle mode\n");
1235             toggle_floating_mode(current->con, false);
1236         } else {
1237             DLOG("should switch mode to %s\n", floating_mode);
1238             if (strcmp(floating_mode, "enable") == 0) {
1239                 floating_enable(current->con, false);
1240             } else {
1241                 floating_disable(current->con, false);
1242             }
1243         }
1244     }
1245
1246     cmd_output->needs_tree_render = true;
1247     // XXX: default reply for now, make this a better reply
1248     ysuccess(true);
1249 }
1250
1251 /*
1252  * Implementation of 'move workspace to [output] <str>'.
1253  *
1254  */
1255 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1256     DLOG("should move workspace to output %s\n", name);
1257
1258     HANDLE_EMPTY_MATCH;
1259
1260     owindow *current;
1261     TAILQ_FOREACH(current, &owindows, owindows) {
1262         Con *ws = con_get_workspace(current->con);
1263         bool success = workspace_move_to_output(ws, name);
1264         if (!success) {
1265             ELOG("Failed to move workspace to output.\n");
1266             ysuccess(false);
1267             return;
1268         }
1269     }
1270
1271     cmd_output->needs_tree_render = true;
1272     // XXX: default reply for now, make this a better reply
1273     ysuccess(true);
1274 }
1275
1276 /*
1277  * Implementation of 'split v|h|vertical|horizontal'.
1278  *
1279  */
1280 void cmd_split(I3_CMD, char *direction) {
1281     owindow *current;
1282     /* TODO: use matches */
1283     LOG("splitting in direction %c\n", direction[0]);
1284     if (match_is_empty(current_match))
1285         tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1286     else {
1287         TAILQ_FOREACH(current, &owindows, owindows) {
1288             DLOG("matching: %p / %s\n", current->con, current->con->name);
1289             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1290         }
1291     }
1292
1293     cmd_output->needs_tree_render = true;
1294     // XXX: default reply for now, make this a better reply
1295     ysuccess(true);
1296 }
1297
1298 /*
1299  * Implementation of 'kill [window|client]'.
1300  *
1301  */
1302 void cmd_kill(I3_CMD, char *kill_mode_str) {
1303     if (kill_mode_str == NULL)
1304         kill_mode_str = "window";
1305     owindow *current;
1306
1307     DLOG("kill_mode=%s\n", kill_mode_str);
1308
1309     int kill_mode;
1310     if (strcmp(kill_mode_str, "window") == 0)
1311         kill_mode = KILL_WINDOW;
1312     else if (strcmp(kill_mode_str, "client") == 0)
1313         kill_mode = KILL_CLIENT;
1314     else {
1315         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1316         ysuccess(false);
1317         return;
1318     }
1319
1320     /* check if the match is empty, not if the result is empty */
1321     if (match_is_empty(current_match))
1322         tree_close_con(kill_mode);
1323     else {
1324         TAILQ_FOREACH(current, &owindows, owindows) {
1325             DLOG("matching: %p / %s\n", current->con, current->con->name);
1326             tree_close(current->con, kill_mode, false, false);
1327         }
1328     }
1329
1330     cmd_output->needs_tree_render = true;
1331     // XXX: default reply for now, make this a better reply
1332     ysuccess(true);
1333 }
1334
1335 /*
1336  * Implementation of 'exec [--no-startup-id] <command>'.
1337  *
1338  */
1339 void cmd_exec(I3_CMD, char *nosn, char *command) {
1340     bool no_startup_id = (nosn != NULL);
1341
1342     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1343     start_application(command, no_startup_id);
1344
1345     // XXX: default reply for now, make this a better reply
1346     ysuccess(true);
1347 }
1348
1349 /*
1350  * Implementation of 'focus left|right|up|down'.
1351  *
1352  */
1353 void cmd_focus_direction(I3_CMD, char *direction) {
1354     DLOG("direction = *%s*\n", direction);
1355
1356     if (strcmp(direction, "left") == 0)
1357         tree_next('p', HORIZ);
1358     else if (strcmp(direction, "right") == 0)
1359         tree_next('n', HORIZ);
1360     else if (strcmp(direction, "up") == 0)
1361         tree_next('p', VERT);
1362     else if (strcmp(direction, "down") == 0)
1363         tree_next('n', VERT);
1364     else {
1365         ELOG("Invalid focus direction (%s)\n", direction);
1366         ysuccess(false);
1367         return;
1368     }
1369
1370     cmd_output->needs_tree_render = true;
1371     // XXX: default reply for now, make this a better reply
1372     ysuccess(true);
1373 }
1374
1375 /*
1376  * Implementation of 'focus tiling|floating|mode_toggle'.
1377  *
1378  */
1379 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1380     DLOG("window_mode = %s\n", window_mode);
1381
1382     Con *ws = con_get_workspace(focused);
1383     Con *current;
1384     if (ws != NULL) {
1385         if (strcmp(window_mode, "mode_toggle") == 0) {
1386             current = TAILQ_FIRST(&(ws->focus_head));
1387             if (current != NULL && current->type == CT_FLOATING_CON)
1388                 window_mode = "tiling";
1389             else
1390                 window_mode = "floating";
1391         }
1392         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1393             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1394                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1395                 continue;
1396
1397             con_focus(con_descend_focused(current));
1398             break;
1399         }
1400     }
1401
1402     cmd_output->needs_tree_render = true;
1403     // XXX: default reply for now, make this a better reply
1404     ysuccess(true);
1405 }
1406
1407 /*
1408  * Implementation of 'focus parent|child'.
1409  *
1410  */
1411 void cmd_focus_level(I3_CMD, char *level) {
1412     DLOG("level = %s\n", level);
1413     bool success = false;
1414
1415     /* Focusing the parent can only be allowed if the newly
1416      * focused container won't escape the fullscreen container. */
1417     if (strcmp(level, "parent") == 0) {
1418         if (focused && focused->parent) {
1419             if (con_fullscreen_permits_focusing(focused->parent))
1420                 success = level_up();
1421             else
1422                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1423         }
1424     }
1425
1426     /* Focusing a child should always be allowed. */
1427     else
1428         success = level_down();
1429
1430     cmd_output->needs_tree_render = success;
1431     // XXX: default reply for now, make this a better reply
1432     ysuccess(success);
1433 }
1434
1435 /*
1436  * Implementation of 'focus'.
1437  *
1438  */
1439 void cmd_focus(I3_CMD) {
1440     DLOG("current_match = %p\n", current_match);
1441
1442     if (match_is_empty(current_match)) {
1443         ELOG("You have to specify which window/container should be focused.\n");
1444         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1445
1446         yerror("You have to specify which window/container should be focused");
1447
1448         return;
1449     }
1450
1451     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1452     int count = 0;
1453     owindow *current;
1454     TAILQ_FOREACH(current, &owindows, owindows) {
1455         Con *ws = con_get_workspace(current->con);
1456         /* If no workspace could be found, this was a dock window.
1457          * Just skip it, you cannot focus dock windows. */
1458         if (!ws)
1459             continue;
1460
1461         /* Check the fullscreen focus constraints. */
1462         if (!con_fullscreen_permits_focusing(current->con)) {
1463             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1464             ysuccess(false);
1465             return;
1466         }
1467
1468         /* In case this is a scratchpad window, call scratchpad_show(). */
1469         if (ws == __i3_scratch) {
1470             scratchpad_show(current->con);
1471             count++;
1472             /* While for the normal focus case we can change focus multiple
1473              * times and only a single window ends up focused, we could show
1474              * multiple scratchpad windows. So, rather break here. */
1475             break;
1476         }
1477
1478         /* If the container is not on the current workspace,
1479          * workspace_show() will switch to a different workspace and (if
1480          * enabled) trigger a mouse pointer warp to the currently focused
1481          * container (!) on the target workspace.
1482          *
1483          * Therefore, before calling workspace_show(), we make sure that
1484          * 'current' will be focused on the workspace. However, we cannot
1485          * just con_focus(current) because then the pointer will not be
1486          * warped at all (the code thinks we are already there).
1487          *
1488          * So we focus 'current' to make it the currently focused window of
1489          * the target workspace, then revert focus. */
1490         Con *currently_focused = focused;
1491         con_focus(current->con);
1492         con_focus(currently_focused);
1493
1494         /* Now switch to the workspace, then focus */
1495         workspace_show(ws);
1496         LOG("focusing %p / %s\n", current->con, current->con->name);
1497         con_focus(current->con);
1498         count++;
1499     }
1500
1501     if (count > 1)
1502         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1503             "while only exactly one container can be focused at a time.\n",
1504             count);
1505
1506     cmd_output->needs_tree_render = true;
1507     // XXX: default reply for now, make this a better reply
1508     ysuccess(true);
1509 }
1510
1511 /*
1512  * Implementation of 'fullscreen enable|toggle [global]' and
1513  *                   'fullscreen disable'
1514  *
1515  */
1516 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1517     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1518     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1519     owindow *current;
1520
1521     HANDLE_EMPTY_MATCH;
1522
1523     TAILQ_FOREACH(current, &owindows, owindows) {
1524         DLOG("matching: %p / %s\n", current->con, current->con->name);
1525         if (strcmp(action, "toggle") == 0) {
1526             con_toggle_fullscreen(current->con, mode);
1527         } else if (strcmp(action, "enable") == 0) {
1528             con_enable_fullscreen(current->con, mode);
1529         } else if (strcmp(action, "disable") == 0) {
1530             con_disable_fullscreen(current->con);
1531         }
1532     }
1533
1534     cmd_output->needs_tree_render = true;
1535     // XXX: default reply for now, make this a better reply
1536     ysuccess(true);
1537 }
1538
1539 /*
1540  * Implementation of 'move <direction> [<pixels> [px]]'.
1541  *
1542  */
1543 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1544     // 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
1545     int px = atoi(move_px);
1546
1547     owindow *current;
1548     HANDLE_EMPTY_MATCH;
1549
1550     Con *initially_focused = focused;
1551
1552     TAILQ_FOREACH(current, &owindows, owindows) {
1553         DLOG("moving in direction %s, px %s\n", direction, move_px);
1554         if (con_is_floating(current->con)) {
1555             DLOG("floating move with %d pixels\n", px);
1556             Rect newrect = current->con->parent->rect;
1557             if (strcmp(direction, "left") == 0) {
1558                 newrect.x -= px;
1559             } else if (strcmp(direction, "right") == 0) {
1560                 newrect.x += px;
1561             } else if (strcmp(direction, "up") == 0) {
1562                 newrect.y -= px;
1563             } else if (strcmp(direction, "down") == 0) {
1564                 newrect.y += px;
1565             }
1566             floating_reposition(current->con->parent, newrect);
1567         } else {
1568             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1569             cmd_output->needs_tree_render = true;
1570         }
1571     }
1572
1573     /* the move command should not disturb focus */
1574     if (focused != initially_focused)
1575         con_focus(initially_focused);
1576
1577     // XXX: default reply for now, make this a better reply
1578     ysuccess(true);
1579 }
1580
1581 /*
1582  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1583  *
1584  */
1585 void cmd_layout(I3_CMD, char *layout_str) {
1586     if (strcmp(layout_str, "stacking") == 0)
1587         layout_str = "stacked";
1588     owindow *current;
1589     layout_t layout;
1590     /* default is a special case which will be handled in con_set_layout(). */
1591     if (strcmp(layout_str, "default") == 0)
1592         layout = L_DEFAULT;
1593     else if (strcmp(layout_str, "stacked") == 0)
1594         layout = L_STACKED;
1595     else if (strcmp(layout_str, "tabbed") == 0)
1596         layout = L_TABBED;
1597     else if (strcmp(layout_str, "splitv") == 0)
1598         layout = L_SPLITV;
1599     else if (strcmp(layout_str, "splith") == 0)
1600         layout = L_SPLITH;
1601     else {
1602         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1603         return;
1604     }
1605
1606     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1607
1608     /* check if the match is empty, not if the result is empty */
1609     if (match_is_empty(current_match))
1610         con_set_layout(focused, layout);
1611     else {
1612         TAILQ_FOREACH(current, &owindows, owindows) {
1613             DLOG("matching: %p / %s\n", current->con, current->con->name);
1614             con_set_layout(current->con, layout);
1615         }
1616     }
1617
1618     cmd_output->needs_tree_render = true;
1619     // XXX: default reply for now, make this a better reply
1620     ysuccess(true);
1621 }
1622
1623 /*
1624  * Implementation of 'layout toggle [all|split]'.
1625  *
1626  */
1627 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1628     owindow *current;
1629
1630     if (toggle_mode == NULL)
1631         toggle_mode = "default";
1632
1633     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1634
1635     /* check if the match is empty, not if the result is empty */
1636     if (match_is_empty(current_match))
1637         con_toggle_layout(focused, toggle_mode);
1638     else {
1639         TAILQ_FOREACH(current, &owindows, owindows) {
1640             DLOG("matching: %p / %s\n", current->con, current->con->name);
1641             con_toggle_layout(current->con, toggle_mode);
1642         }
1643     }
1644
1645     cmd_output->needs_tree_render = true;
1646     // XXX: default reply for now, make this a better reply
1647     ysuccess(true);
1648 }
1649
1650 /*
1651  * Implementation of 'exit'.
1652  *
1653  */
1654 void cmd_exit(I3_CMD) {
1655     LOG("Exiting due to user command.\n");
1656     ipc_shutdown();
1657     unlink(config.ipc_socket_path);
1658     xcb_disconnect(conn);
1659     exit(0);
1660
1661     /* unreached */
1662 }
1663
1664 /*
1665  * Implementation of 'reload'.
1666  *
1667  */
1668 void cmd_reload(I3_CMD) {
1669     LOG("reloading\n");
1670     kill_nagbar(&config_error_nagbar_pid, false);
1671     kill_nagbar(&command_error_nagbar_pid, false);
1672     load_configuration(conn, NULL, true);
1673     x_set_i3_atoms();
1674     /* Send an IPC event just in case the ws names have changed */
1675     ipc_send_workspace_event("reload", NULL, NULL);
1676     /* Send an update event for the barconfig just in case it has changed */
1677     update_barconfig();
1678
1679     // XXX: default reply for now, make this a better reply
1680     ysuccess(true);
1681 }
1682
1683 /*
1684  * Implementation of 'restart'.
1685  *
1686  */
1687 void cmd_restart(I3_CMD) {
1688     LOG("restarting i3\n");
1689     ipc_shutdown();
1690     unlink(config.ipc_socket_path);
1691     /* We need to call this manually since atexit handlers don’t get called
1692      * when exec()ing */
1693     purge_zerobyte_logfile();
1694     i3_restart(false);
1695
1696     // XXX: default reply for now, make this a better reply
1697     ysuccess(true);
1698 }
1699
1700 /*
1701  * Implementation of 'open'.
1702  *
1703  */
1704 void cmd_open(I3_CMD) {
1705     LOG("opening new container\n");
1706     Con *con = tree_open_con(NULL, NULL);
1707     con->layout = L_SPLITH;
1708     con_focus(con);
1709
1710     y(map_open);
1711     ystr("success");
1712     y(bool, true);
1713     ystr("id");
1714     y(integer, (long int)con);
1715     y(map_close);
1716
1717     cmd_output->needs_tree_render = true;
1718 }
1719
1720 /*
1721  * Implementation of 'focus output <output>'.
1722  *
1723  */
1724 void cmd_focus_output(I3_CMD, char *name) {
1725     owindow *current;
1726
1727     DLOG("name = %s\n", name);
1728
1729     HANDLE_EMPTY_MATCH;
1730
1731     /* get the output */
1732     Output *current_output = NULL;
1733     Output *output;
1734
1735     TAILQ_FOREACH(current, &owindows, owindows)
1736     current_output = get_output_of_con(current->con);
1737     assert(current_output != NULL);
1738
1739     output = get_output_from_string(current_output, name);
1740
1741     if (!output) {
1742         LOG("No such output found.\n");
1743         ysuccess(false);
1744         return;
1745     }
1746
1747     /* get visible workspace on output */
1748     Con *ws = NULL;
1749     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1750     if (!ws) {
1751         ysuccess(false);
1752         return;
1753     }
1754
1755     workspace_show(ws);
1756
1757     cmd_output->needs_tree_render = true;
1758     // XXX: default reply for now, make this a better reply
1759     ysuccess(true);
1760 }
1761
1762 /*
1763  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1764  *
1765  */
1766 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1767     int x = atoi(cx);
1768     int y = atoi(cy);
1769     bool has_error = false;
1770
1771     owindow *current;
1772     HANDLE_EMPTY_MATCH;
1773
1774     TAILQ_FOREACH(current, &owindows, owindows) {
1775         if (!con_is_floating(current->con)) {
1776             ELOG("Cannot change position. The window/container is not floating\n");
1777
1778             if (!has_error) {
1779                 yerror("Cannot change position of a window/container because it is not floating.");
1780                 has_error = true;
1781             }
1782
1783             continue;
1784         }
1785
1786         if (strcmp(method, "absolute") == 0) {
1787             current->con->parent->rect.x = x;
1788             current->con->parent->rect.y = y;
1789
1790             DLOG("moving to absolute position %d %d\n", x, y);
1791             floating_maybe_reassign_ws(current->con->parent);
1792             cmd_output->needs_tree_render = true;
1793         }
1794
1795         if (strcmp(method, "position") == 0) {
1796             Rect newrect = current->con->parent->rect;
1797
1798             DLOG("moving to position %d %d\n", x, y);
1799             newrect.x = x;
1800             newrect.y = y;
1801
1802             floating_reposition(current->con->parent, newrect);
1803         }
1804     }
1805
1806     // XXX: default reply for now, make this a better reply
1807     if (!has_error)
1808         ysuccess(true);
1809 }
1810
1811 /*
1812  * Implementation of 'move [window|container] [to] [absolute] position center
1813  *
1814  */
1815 void cmd_move_window_to_center(I3_CMD, char *method) {
1816     if (!con_is_floating(focused)) {
1817         ELOG("Cannot change position. The window/container is not floating\n");
1818         yerror("Cannot change position. The window/container is not floating.");
1819         return;
1820     }
1821
1822     if (strcmp(method, "absolute") == 0) {
1823         DLOG("moving to absolute center\n");
1824         floating_center(focused->parent, croot->rect);
1825
1826         floating_maybe_reassign_ws(focused->parent);
1827         cmd_output->needs_tree_render = true;
1828     }
1829
1830     if (strcmp(method, "position") == 0) {
1831         DLOG("moving to center\n");
1832         floating_center(focused->parent, con_get_workspace(focused)->rect);
1833
1834         cmd_output->needs_tree_render = true;
1835     }
1836
1837     // XXX: default reply for now, make this a better reply
1838     ysuccess(true);
1839 }
1840
1841 /*
1842  * Implementation of 'move [window|container] [to] position mouse'
1843  *
1844  */
1845 void cmd_move_window_to_mouse(I3_CMD) {
1846     HANDLE_EMPTY_MATCH;
1847
1848     owindow *current;
1849     TAILQ_FOREACH(current, &owindows, owindows) {
1850         Con *floating_con = con_inside_floating(current->con);
1851         if (floating_con == NULL) {
1852             DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1853                  current->con, current->con->name);
1854             continue;
1855         }
1856
1857         DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1858         floating_move_to_pointer(floating_con);
1859     }
1860
1861     cmd_output->needs_tree_render = true;
1862     ysuccess(true);
1863 }
1864
1865 /*
1866  * Implementation of 'move scratchpad'.
1867  *
1868  */
1869 void cmd_move_scratchpad(I3_CMD) {
1870     DLOG("should move window to scratchpad\n");
1871     owindow *current;
1872
1873     HANDLE_EMPTY_MATCH;
1874
1875     TAILQ_FOREACH(current, &owindows, owindows) {
1876         DLOG("matching: %p / %s\n", current->con, current->con->name);
1877         scratchpad_move(current->con);
1878     }
1879
1880     cmd_output->needs_tree_render = true;
1881     // XXX: default reply for now, make this a better reply
1882     ysuccess(true);
1883 }
1884
1885 /*
1886  * Implementation of 'scratchpad show'.
1887  *
1888  */
1889 void cmd_scratchpad_show(I3_CMD) {
1890     DLOG("should show scratchpad window\n");
1891     owindow *current;
1892
1893     if (match_is_empty(current_match)) {
1894         scratchpad_show(NULL);
1895     } else {
1896         TAILQ_FOREACH(current, &owindows, owindows) {
1897             DLOG("matching: %p / %s\n", current->con, current->con->name);
1898             scratchpad_show(current->con);
1899         }
1900     }
1901
1902     cmd_output->needs_tree_render = true;
1903     // XXX: default reply for now, make this a better reply
1904     ysuccess(true);
1905 }
1906
1907 /*
1908  * Implementation of 'title_format <format>'
1909  *
1910  */
1911 void cmd_title_format(I3_CMD, char *format) {
1912     DLOG("setting title_format to \"%s\"\n", format);
1913     HANDLE_EMPTY_MATCH;
1914
1915     owindow *current;
1916     TAILQ_FOREACH(current, &owindows, owindows) {
1917         if (current->con->window == NULL)
1918             continue;
1919
1920         DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1921         FREE(current->con->window->title_format);
1922
1923         /* If we only display the title without anything else, we can skip the parsing step,
1924          * so we remove the title format altogether. */
1925         if (strcasecmp(format, "%title") != 0) {
1926             current->con->window->title_format = sstrdup(format);
1927
1928             i3String *formatted_title = window_parse_title_format(current->con->window);
1929             ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1930             I3STRING_FREE(formatted_title);
1931         } else {
1932             /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1933             ewmh_update_visible_name(current->con->window->id, NULL);
1934         }
1935
1936         /* Make sure the window title is redrawn immediately. */
1937         current->con->window->name_x_changed = true;
1938     }
1939
1940     cmd_output->needs_tree_render = true;
1941     ysuccess(true);
1942 }
1943
1944 /*
1945  * Implementation of 'rename workspace [<name>] to <name>'
1946  *
1947  */
1948 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1949     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1950         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1951         ysuccess(false);
1952         return;
1953     }
1954     if (old_name) {
1955         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1956     } else {
1957         LOG("Renaming current workspace to \"%s\"\n", new_name);
1958     }
1959
1960     Con *output, *workspace = NULL;
1961     if (old_name) {
1962         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1963         GREP_FIRST(workspace, output_get_content(output),
1964                    !strcasecmp(child->name, old_name));
1965     } else {
1966         workspace = con_get_workspace(focused);
1967         old_name = workspace->name;
1968     }
1969
1970     if (!workspace) {
1971         yerror("Old workspace \"%s\" not found", old_name);
1972         return;
1973     }
1974
1975     Con *check_dest = NULL;
1976     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1977     GREP_FIRST(check_dest, output_get_content(output),
1978                !strcasecmp(child->name, new_name));
1979
1980     if (check_dest != NULL) {
1981         yerror("New workspace \"%s\" already exists", new_name);
1982         return;
1983     }
1984
1985     /* Change the name and try to parse it as a number. */
1986     FREE(workspace->name);
1987     workspace->name = sstrdup(new_name);
1988
1989     workspace->num = ws_name_to_number(new_name);
1990     LOG("num = %d\n", workspace->num);
1991
1992     /* By re-attaching, the sort order will be correct afterwards. */
1993     Con *previously_focused = focused;
1994     Con *parent = workspace->parent;
1995     con_detach(workspace);
1996     con_attach(workspace, parent, false);
1997
1998     /* Move the workspace to the correct output if it has an assignment */
1999     struct Workspace_Assignment *assignment = NULL;
2000     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2001         if (assignment->output == NULL)
2002             continue;
2003         if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2004             continue;
2005         }
2006
2007         workspace_move_to_output(workspace, assignment->output);
2008
2009         if (previously_focused)
2010             workspace_show(con_get_workspace(previously_focused));
2011
2012         break;
2013     }
2014
2015     /* Restore the previous focus since con_attach messes with the focus. */
2016     con_focus(previously_focused);
2017
2018     cmd_output->needs_tree_render = true;
2019     ysuccess(true);
2020
2021     ipc_send_workspace_event("rename", workspace, NULL);
2022     ewmh_update_desktop_names();
2023     ewmh_update_desktop_viewport();
2024     ewmh_update_current_desktop();
2025
2026     startup_sequence_rename_workspace(old_name, new_name);
2027 }
2028
2029 /*
2030  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2031  *
2032  */
2033 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2034     int mode = M_DOCK;
2035     bool toggle = false;
2036     if (strcmp(bar_mode, "dock") == 0)
2037         mode = M_DOCK;
2038     else if (strcmp(bar_mode, "hide") == 0)
2039         mode = M_HIDE;
2040     else if (strcmp(bar_mode, "invisible") == 0)
2041         mode = M_INVISIBLE;
2042     else if (strcmp(bar_mode, "toggle") == 0)
2043         toggle = true;
2044     else {
2045         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2046         return false;
2047     }
2048
2049     bool changed_sth = false;
2050     Barconfig *current = NULL;
2051     TAILQ_FOREACH(current, &barconfigs, configs) {
2052         if (bar_id && strcmp(current->id, bar_id) != 0)
2053             continue;
2054
2055         if (toggle)
2056             mode = (current->mode + 1) % 2;
2057
2058         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2059         current->mode = mode;
2060         changed_sth = true;
2061
2062         if (bar_id)
2063             break;
2064     }
2065
2066     if (bar_id && !changed_sth) {
2067         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2068         return false;
2069     }
2070
2071     return true;
2072 }
2073
2074 /*
2075  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2076  *
2077  */
2078 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2079     int hidden_state = S_SHOW;
2080     bool toggle = false;
2081     if (strcmp(bar_hidden_state, "hide") == 0)
2082         hidden_state = S_HIDE;
2083     else if (strcmp(bar_hidden_state, "show") == 0)
2084         hidden_state = S_SHOW;
2085     else if (strcmp(bar_hidden_state, "toggle") == 0)
2086         toggle = true;
2087     else {
2088         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2089         return false;
2090     }
2091
2092     bool changed_sth = false;
2093     Barconfig *current = NULL;
2094     TAILQ_FOREACH(current, &barconfigs, configs) {
2095         if (bar_id && strcmp(current->id, bar_id) != 0)
2096             continue;
2097
2098         if (toggle)
2099             hidden_state = (current->hidden_state + 1) % 2;
2100
2101         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2102         current->hidden_state = hidden_state;
2103         changed_sth = true;
2104
2105         if (bar_id)
2106             break;
2107     }
2108
2109     if (bar_id && !changed_sth) {
2110         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2111         return false;
2112     }
2113
2114     return true;
2115 }
2116
2117 /*
2118  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2119  *
2120  */
2121 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2122     bool ret;
2123     if (strcmp(bar_type, "mode") == 0)
2124         ret = cmd_bar_mode(bar_value, bar_id);
2125     else if (strcmp(bar_type, "hidden_state") == 0)
2126         ret = cmd_bar_hidden_state(bar_value, bar_id);
2127     else {
2128         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2129         ret = false;
2130     }
2131
2132     ysuccess(ret);
2133     if (!ret)
2134         return;
2135
2136     update_barconfig();
2137 }
2138
2139 /*
2140  * Implementation of 'shmlog <size>|toggle|on|off'
2141  *
2142  */
2143 void cmd_shmlog(I3_CMD, char *argument) {
2144     if (!strcmp(argument, "toggle"))
2145         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2146         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2147     else if (!strcmp(argument, "on"))
2148         shmlog_size = default_shmlog_size;
2149     else if (!strcmp(argument, "off"))
2150         shmlog_size = 0;
2151     else {
2152         /* If shm logging now, restart logging with the new size. */
2153         if (shmlog_size > 0) {
2154             shmlog_size = 0;
2155             LOG("Restarting shm logging...\n");
2156             init_logging();
2157         }
2158         shmlog_size = atoi(argument);
2159         /* Make a weakly attempt at ensuring the argument is valid. */
2160         if (shmlog_size <= 0)
2161             shmlog_size = default_shmlog_size;
2162     }
2163     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2164     init_logging();
2165     update_shmlog_atom();
2166     // XXX: default reply for now, make this a better reply
2167     ysuccess(true);
2168 }
2169
2170 /*
2171  * Implementation of 'debuglog toggle|on|off'
2172  *
2173  */
2174 void cmd_debuglog(I3_CMD, char *argument) {
2175     bool logging = get_debug_logging();
2176     if (!strcmp(argument, "toggle")) {
2177         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2178         set_debug_logging(!logging);
2179     } else if (!strcmp(argument, "on") && !logging) {
2180         LOG("Enabling debug logging\n");
2181         set_debug_logging(true);
2182     } else if (!strcmp(argument, "off") && logging) {
2183         LOG("Disabling debug logging\n");
2184         set_debug_logging(false);
2185     }
2186     // XXX: default reply for now, make this a better reply
2187     ysuccess(true);
2188 }