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