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