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