]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Rename tree_close() to tree_close_internal().
[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|vertical|horizontal'.
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         tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1247     }
1248
1249     cmd_output->needs_tree_render = true;
1250     // XXX: default reply for now, make this a better reply
1251     ysuccess(true);
1252 }
1253
1254 /*
1255  * Implementation of 'kill [window|client]'.
1256  *
1257  */
1258 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1259     if (kill_mode_str == NULL)
1260         kill_mode_str = "window";
1261     owindow *current;
1262
1263     DLOG("kill_mode=%s\n", kill_mode_str);
1264
1265     int kill_mode;
1266     if (strcmp(kill_mode_str, "window") == 0)
1267         kill_mode = KILL_WINDOW;
1268     else if (strcmp(kill_mode_str, "client") == 0)
1269         kill_mode = KILL_CLIENT;
1270     else {
1271         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1272         ysuccess(false);
1273         return;
1274     }
1275
1276     HANDLE_INVALID_MATCH;
1277
1278     /* check if the match is empty, not if the result is empty */
1279     if (match_is_empty(current_match))
1280         tree_close_con(kill_mode);
1281     else {
1282         TAILQ_FOREACH(current, &owindows, owindows) {
1283             DLOG("matching: %p / %s\n", current->con, current->con->name);
1284             tree_close_internal(current->con, kill_mode, false, false);
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 'exec [--no-startup-id] <command>'.
1295  *
1296  */
1297 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1298     bool no_startup_id = (nosn != NULL);
1299
1300     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1301     start_application(command, no_startup_id);
1302
1303     // XXX: default reply for now, make this a better reply
1304     ysuccess(true);
1305 }
1306
1307 /*
1308  * Implementation of 'focus left|right|up|down'.
1309  *
1310  */
1311 void cmd_focus_direction(I3_CMD, const char *direction) {
1312     DLOG("direction = *%s*\n", direction);
1313
1314     if (strcmp(direction, "left") == 0)
1315         tree_next('p', HORIZ);
1316     else if (strcmp(direction, "right") == 0)
1317         tree_next('n', HORIZ);
1318     else if (strcmp(direction, "up") == 0)
1319         tree_next('p', VERT);
1320     else if (strcmp(direction, "down") == 0)
1321         tree_next('n', VERT);
1322     else {
1323         ELOG("Invalid focus direction (%s)\n", direction);
1324         ysuccess(false);
1325         return;
1326     }
1327
1328     cmd_output->needs_tree_render = true;
1329     // XXX: default reply for now, make this a better reply
1330     ysuccess(true);
1331 }
1332
1333 /*
1334  * Implementation of 'focus tiling|floating|mode_toggle'.
1335  *
1336  */
1337 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1338     DLOG("window_mode = %s\n", window_mode);
1339
1340     Con *ws = con_get_workspace(focused);
1341     if (ws != NULL) {
1342         if (strcmp(window_mode, "mode_toggle") == 0) {
1343             if (con_inside_floating(focused))
1344                 window_mode = "tiling";
1345             else
1346                 window_mode = "floating";
1347         }
1348         Con *current;
1349         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1350             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1351                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1352                 continue;
1353
1354             con_focus(con_descend_focused(current));
1355             break;
1356         }
1357     }
1358
1359     cmd_output->needs_tree_render = true;
1360     // XXX: default reply for now, make this a better reply
1361     ysuccess(true);
1362 }
1363
1364 /*
1365  * Implementation of 'focus parent|child'.
1366  *
1367  */
1368 void cmd_focus_level(I3_CMD, const char *level) {
1369     DLOG("level = %s\n", level);
1370     bool success = false;
1371
1372     /* Focusing the parent can only be allowed if the newly
1373      * focused container won't escape the fullscreen container. */
1374     if (strcmp(level, "parent") == 0) {
1375         if (focused && focused->parent) {
1376             if (con_fullscreen_permits_focusing(focused->parent))
1377                 success = level_up();
1378             else
1379                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1380         }
1381     }
1382
1383     /* Focusing a child should always be allowed. */
1384     else
1385         success = level_down();
1386
1387     cmd_output->needs_tree_render = success;
1388     // XXX: default reply for now, make this a better reply
1389     ysuccess(success);
1390 }
1391
1392 /*
1393  * Implementation of 'focus'.
1394  *
1395  */
1396 void cmd_focus(I3_CMD) {
1397     DLOG("current_match = %p\n", current_match);
1398
1399     if (match_is_empty(current_match)) {
1400         ELOG("You have to specify which window/container should be focused.\n");
1401         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1402
1403         yerror("You have to specify which window/container should be focused");
1404
1405         return;
1406     }
1407
1408     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1409     int count = 0;
1410     owindow *current;
1411     TAILQ_FOREACH(current, &owindows, owindows) {
1412         Con *ws = con_get_workspace(current->con);
1413         /* If no workspace could be found, this was a dock window.
1414          * Just skip it, you cannot focus dock windows. */
1415         if (!ws)
1416             continue;
1417
1418         /* Check the fullscreen focus constraints. */
1419         if (!con_fullscreen_permits_focusing(current->con)) {
1420             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1421             ysuccess(false);
1422             return;
1423         }
1424
1425         /* In case this is a scratchpad window, call scratchpad_show(). */
1426         if (ws == __i3_scratch) {
1427             scratchpad_show(current->con);
1428             count++;
1429             /* While for the normal focus case we can change focus multiple
1430              * times and only a single window ends up focused, we could show
1431              * multiple scratchpad windows. So, rather break here. */
1432             break;
1433         }
1434
1435         /* If the container is not on the current workspace,
1436          * workspace_show() will switch to a different workspace and (if
1437          * enabled) trigger a mouse pointer warp to the currently focused
1438          * container (!) on the target workspace.
1439          *
1440          * Therefore, before calling workspace_show(), we make sure that
1441          * 'current' will be focused on the workspace. However, we cannot
1442          * just con_focus(current) because then the pointer will not be
1443          * warped at all (the code thinks we are already there).
1444          *
1445          * So we focus 'current' to make it the currently focused window of
1446          * the target workspace, then revert focus. */
1447         Con *currently_focused = focused;
1448         con_focus(current->con);
1449         con_focus(currently_focused);
1450
1451         /* Now switch to the workspace, then focus */
1452         workspace_show(ws);
1453         LOG("focusing %p / %s\n", current->con, current->con->name);
1454         con_focus(current->con);
1455         count++;
1456     }
1457
1458     if (count > 1)
1459         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1460             "while only exactly one container can be focused at a time.\n",
1461             count);
1462
1463     cmd_output->needs_tree_render = true;
1464     ysuccess(count > 0);
1465 }
1466
1467 /*
1468  * Implementation of 'fullscreen enable|toggle [global]' and
1469  *                   'fullscreen disable'
1470  *
1471  */
1472 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1473     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1474     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1475     owindow *current;
1476
1477     HANDLE_EMPTY_MATCH;
1478
1479     TAILQ_FOREACH(current, &owindows, owindows) {
1480         DLOG("matching: %p / %s\n", current->con, current->con->name);
1481         if (strcmp(action, "toggle") == 0) {
1482             con_toggle_fullscreen(current->con, mode);
1483         } else if (strcmp(action, "enable") == 0) {
1484             con_enable_fullscreen(current->con, mode);
1485         } else if (strcmp(action, "disable") == 0) {
1486             con_disable_fullscreen(current->con);
1487         }
1488     }
1489
1490     cmd_output->needs_tree_render = true;
1491     // XXX: default reply for now, make this a better reply
1492     ysuccess(true);
1493 }
1494
1495 /*
1496  * Implementation of 'sticky enable|disable|toggle'.
1497  *
1498  */
1499 void cmd_sticky(I3_CMD, const char *action) {
1500     DLOG("%s sticky on window\n", action);
1501     HANDLE_EMPTY_MATCH;
1502
1503     owindow *current;
1504     TAILQ_FOREACH(current, &owindows, owindows) {
1505         if (current->con->window == NULL) {
1506             ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1507             continue;
1508         }
1509         DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1510
1511         bool sticky = false;
1512         if (strcmp(action, "enable") == 0)
1513             sticky = true;
1514         else if (strcmp(action, "disable") == 0)
1515             sticky = false;
1516         else if (strcmp(action, "toggle") == 0)
1517             sticky = !current->con->sticky;
1518
1519         current->con->sticky = sticky;
1520         ewmh_update_sticky(current->con->window->id, sticky);
1521     }
1522
1523     /* A window we made sticky might not be on a visible workspace right now, so we need to make
1524      * sure it gets pushed to the front now. */
1525     output_push_sticky_windows(focused);
1526
1527     cmd_output->needs_tree_render = true;
1528     ysuccess(true);
1529 }
1530
1531 /*
1532  * Implementation of 'move <direction> [<pixels> [px]]'.
1533  *
1534  */
1535 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1536     owindow *current;
1537     HANDLE_EMPTY_MATCH;
1538
1539     Con *initially_focused = focused;
1540
1541     TAILQ_FOREACH(current, &owindows, owindows) {
1542         DLOG("moving in direction %s, px %ld\n", direction, move_px);
1543         if (con_is_floating(current->con)) {
1544             DLOG("floating move with %ld pixels\n", move_px);
1545             Rect newrect = current->con->parent->rect;
1546             if (strcmp(direction, "left") == 0) {
1547                 newrect.x -= move_px;
1548             } else if (strcmp(direction, "right") == 0) {
1549                 newrect.x += move_px;
1550             } else if (strcmp(direction, "up") == 0) {
1551                 newrect.y -= move_px;
1552             } else if (strcmp(direction, "down") == 0) {
1553                 newrect.y += move_px;
1554             }
1555             floating_reposition(current->con->parent, newrect);
1556         } else {
1557             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1558             cmd_output->needs_tree_render = true;
1559         }
1560     }
1561
1562     /* the move command should not disturb focus */
1563     if (focused != initially_focused)
1564         con_focus(initially_focused);
1565
1566     // XXX: default reply for now, make this a better reply
1567     ysuccess(true);
1568 }
1569
1570 /*
1571  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1572  *
1573  */
1574 void cmd_layout(I3_CMD, const char *layout_str) {
1575     HANDLE_EMPTY_MATCH;
1576
1577     if (strcmp(layout_str, "stacking") == 0)
1578         layout_str = "stacked";
1579     layout_t layout;
1580     /* default is a special case which will be handled in con_set_layout(). */
1581     if (strcmp(layout_str, "default") == 0)
1582         layout = L_DEFAULT;
1583     else if (strcmp(layout_str, "stacked") == 0)
1584         layout = L_STACKED;
1585     else if (strcmp(layout_str, "tabbed") == 0)
1586         layout = L_TABBED;
1587     else if (strcmp(layout_str, "splitv") == 0)
1588         layout = L_SPLITV;
1589     else if (strcmp(layout_str, "splith") == 0)
1590         layout = L_SPLITH;
1591     else {
1592         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1593         return;
1594     }
1595
1596     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1597
1598     owindow *current;
1599     TAILQ_FOREACH(current, &owindows, owindows) {
1600         if (con_is_docked(current->con)) {
1601             ELOG("cannot change layout of a docked container, skipping it.\n");
1602             continue;
1603         }
1604
1605         DLOG("matching: %p / %s\n", current->con, current->con->name);
1606         con_set_layout(current->con, layout);
1607     }
1608
1609     cmd_output->needs_tree_render = true;
1610     // XXX: default reply for now, make this a better reply
1611     ysuccess(true);
1612 }
1613
1614 /*
1615  * Implementation of 'layout toggle [all|split]'.
1616  *
1617  */
1618 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1619     owindow *current;
1620
1621     if (toggle_mode == NULL)
1622         toggle_mode = "default";
1623
1624     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1625
1626     /* check if the match is empty, not if the result is empty */
1627     if (match_is_empty(current_match))
1628         con_toggle_layout(focused, toggle_mode);
1629     else {
1630         TAILQ_FOREACH(current, &owindows, owindows) {
1631             DLOG("matching: %p / %s\n", current->con, current->con->name);
1632             con_toggle_layout(current->con, toggle_mode);
1633         }
1634     }
1635
1636     cmd_output->needs_tree_render = true;
1637     // XXX: default reply for now, make this a better reply
1638     ysuccess(true);
1639 }
1640
1641 /*
1642  * Implementation of 'exit'.
1643  *
1644  */
1645 void cmd_exit(I3_CMD) {
1646     LOG("Exiting due to user command.\n");
1647     ipc_shutdown();
1648     unlink(config.ipc_socket_path);
1649     xcb_disconnect(conn);
1650     exit(0);
1651
1652     /* unreached */
1653 }
1654
1655 /*
1656  * Implementation of 'reload'.
1657  *
1658  */
1659 void cmd_reload(I3_CMD) {
1660     LOG("reloading\n");
1661     kill_nagbar(&config_error_nagbar_pid, false);
1662     kill_nagbar(&command_error_nagbar_pid, false);
1663     load_configuration(conn, NULL, true);
1664     x_set_i3_atoms();
1665     /* Send an IPC event just in case the ws names have changed */
1666     ipc_send_workspace_event("reload", NULL, NULL);
1667     /* Send an update event for the barconfig just in case it has changed */
1668     update_barconfig();
1669
1670     // XXX: default reply for now, make this a better reply
1671     ysuccess(true);
1672 }
1673
1674 /*
1675  * Implementation of 'restart'.
1676  *
1677  */
1678 void cmd_restart(I3_CMD) {
1679     LOG("restarting i3\n");
1680     ipc_shutdown();
1681     unlink(config.ipc_socket_path);
1682     /* We need to call this manually since atexit handlers don’t get called
1683      * when exec()ing */
1684     purge_zerobyte_logfile();
1685     i3_restart(false);
1686
1687     // XXX: default reply for now, make this a better reply
1688     ysuccess(true);
1689 }
1690
1691 /*
1692  * Implementation of 'open'.
1693  *
1694  */
1695 void cmd_open(I3_CMD) {
1696     LOG("opening new container\n");
1697     Con *con = tree_open_con(NULL, NULL);
1698     con->layout = L_SPLITH;
1699     con_focus(con);
1700
1701     y(map_open);
1702     ystr("success");
1703     y(bool, true);
1704     ystr("id");
1705     y(integer, (long int)con);
1706     y(map_close);
1707
1708     cmd_output->needs_tree_render = true;
1709 }
1710
1711 /*
1712  * Implementation of 'focus output <output>'.
1713  *
1714  */
1715 void cmd_focus_output(I3_CMD, const char *name) {
1716     owindow *current;
1717
1718     DLOG("name = %s\n", name);
1719
1720     HANDLE_EMPTY_MATCH;
1721
1722     /* get the output */
1723     Output *current_output = NULL;
1724     Output *output;
1725
1726     TAILQ_FOREACH(current, &owindows, owindows)
1727     current_output = get_output_of_con(current->con);
1728     assert(current_output != NULL);
1729
1730     output = get_output_from_string(current_output, name);
1731
1732     if (!output) {
1733         LOG("No such output found.\n");
1734         ysuccess(false);
1735         return;
1736     }
1737
1738     /* get visible workspace on output */
1739     Con *ws = NULL;
1740     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1741     if (!ws) {
1742         ysuccess(false);
1743         return;
1744     }
1745
1746     workspace_show(ws);
1747
1748     cmd_output->needs_tree_render = true;
1749     // XXX: default reply for now, make this a better reply
1750     ysuccess(true);
1751 }
1752
1753 /*
1754  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1755  *
1756  */
1757 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1758     bool has_error = false;
1759
1760     owindow *current;
1761     HANDLE_EMPTY_MATCH;
1762
1763     TAILQ_FOREACH(current, &owindows, owindows) {
1764         if (!con_is_floating(current->con)) {
1765             ELOG("Cannot change position. The window/container is not floating\n");
1766
1767             if (!has_error) {
1768                 yerror("Cannot change position of a window/container because it is not floating.");
1769                 has_error = true;
1770             }
1771
1772             continue;
1773         }
1774
1775         if (strcmp(method, "absolute") == 0) {
1776             current->con->parent->rect.x = x;
1777             current->con->parent->rect.y = y;
1778
1779             DLOG("moving to absolute position %ld %ld\n", x, y);
1780             floating_maybe_reassign_ws(current->con->parent);
1781             cmd_output->needs_tree_render = true;
1782         }
1783
1784         if (strcmp(method, "position") == 0) {
1785             Rect newrect = current->con->parent->rect;
1786
1787             DLOG("moving to position %ld %ld\n", x, y);
1788             newrect.x = x;
1789             newrect.y = y;
1790
1791             floating_reposition(current->con->parent, newrect);
1792         }
1793     }
1794
1795     // XXX: default reply for now, make this a better reply
1796     if (!has_error)
1797         ysuccess(true);
1798 }
1799
1800 /*
1801  * Implementation of 'move [window|container] [to] [absolute] position center
1802  *
1803  */
1804 void cmd_move_window_to_center(I3_CMD, const char *method) {
1805     bool has_error = false;
1806     HANDLE_EMPTY_MATCH;
1807
1808     owindow *current;
1809     TAILQ_FOREACH(current, &owindows, owindows) {
1810         Con *floating_con = con_inside_floating(current->con);
1811         if (floating_con == NULL) {
1812             ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1813                  current->con, current->con->name);
1814
1815             if (!has_error) {
1816                 yerror("Cannot change position of a window/container because it is not floating.");
1817                 has_error = true;
1818             }
1819
1820             continue;
1821         }
1822
1823         if (strcmp(method, "absolute") == 0) {
1824             DLOG("moving to absolute center\n");
1825             floating_center(floating_con, croot->rect);
1826
1827             floating_maybe_reassign_ws(floating_con);
1828             cmd_output->needs_tree_render = true;
1829         }
1830
1831         if (strcmp(method, "position") == 0) {
1832             DLOG("moving to center\n");
1833             floating_center(floating_con, con_get_workspace(floating_con)->rect);
1834
1835             cmd_output->needs_tree_render = true;
1836         }
1837     }
1838
1839     // XXX: default reply for now, make this a better reply
1840     if (!has_error)
1841         ysuccess(true);
1842 }
1843
1844 /*
1845  * Implementation of 'move [window|container] [to] position mouse'
1846  *
1847  */
1848 void cmd_move_window_to_mouse(I3_CMD) {
1849     HANDLE_EMPTY_MATCH;
1850
1851     owindow *current;
1852     TAILQ_FOREACH(current, &owindows, owindows) {
1853         Con *floating_con = con_inside_floating(current->con);
1854         if (floating_con == NULL) {
1855             DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1856                  current->con, current->con->name);
1857             continue;
1858         }
1859
1860         DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1861         floating_move_to_pointer(floating_con);
1862     }
1863
1864     cmd_output->needs_tree_render = true;
1865     ysuccess(true);
1866 }
1867
1868 /*
1869  * Implementation of 'move scratchpad'.
1870  *
1871  */
1872 void cmd_move_scratchpad(I3_CMD) {
1873     DLOG("should move window to scratchpad\n");
1874     owindow *current;
1875
1876     HANDLE_EMPTY_MATCH;
1877
1878     TAILQ_FOREACH(current, &owindows, owindows) {
1879         DLOG("matching: %p / %s\n", current->con, current->con->name);
1880         scratchpad_move(current->con);
1881     }
1882
1883     cmd_output->needs_tree_render = true;
1884     // XXX: default reply for now, make this a better reply
1885     ysuccess(true);
1886 }
1887
1888 /*
1889  * Implementation of 'scratchpad show'.
1890  *
1891  */
1892 void cmd_scratchpad_show(I3_CMD) {
1893     DLOG("should show scratchpad window\n");
1894     owindow *current;
1895
1896     if (match_is_empty(current_match)) {
1897         scratchpad_show(NULL);
1898     } else {
1899         TAILQ_FOREACH(current, &owindows, owindows) {
1900             DLOG("matching: %p / %s\n", current->con, current->con->name);
1901             scratchpad_show(current->con);
1902         }
1903     }
1904
1905     cmd_output->needs_tree_render = true;
1906     // XXX: default reply for now, make this a better reply
1907     ysuccess(true);
1908 }
1909
1910 /*
1911  * Implementation of 'title_format <format>'
1912  *
1913  */
1914 void cmd_title_format(I3_CMD, const char *format) {
1915     DLOG("setting title_format to \"%s\"\n", format);
1916     HANDLE_EMPTY_MATCH;
1917
1918     owindow *current;
1919     TAILQ_FOREACH(current, &owindows, owindows) {
1920         if (current->con->window == NULL)
1921             continue;
1922
1923         DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1924         FREE(current->con->window->title_format);
1925
1926         /* If we only display the title without anything else, we can skip the parsing step,
1927          * so we remove the title format altogether. */
1928         if (strcasecmp(format, "%title") != 0) {
1929             current->con->window->title_format = sstrdup(format);
1930
1931             i3String *formatted_title = window_parse_title_format(current->con->window);
1932             ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1933             I3STRING_FREE(formatted_title);
1934         } else {
1935             /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1936             ewmh_update_visible_name(current->con->window->id, NULL);
1937         }
1938
1939         /* Make sure the window title is redrawn immediately. */
1940         current->con->window->name_x_changed = true;
1941     }
1942
1943     cmd_output->needs_tree_render = true;
1944     ysuccess(true);
1945 }
1946
1947 /*
1948  * Implementation of 'rename workspace [<name>] to <name>'
1949  *
1950  */
1951 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1952     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1953         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1954         ysuccess(false);
1955         return;
1956     }
1957     if (old_name) {
1958         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1959     } else {
1960         LOG("Renaming current workspace to \"%s\"\n", new_name);
1961     }
1962
1963     Con *output, *workspace = NULL;
1964     if (old_name) {
1965         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1966         GREP_FIRST(workspace, output_get_content(output),
1967                    !strcasecmp(child->name, old_name));
1968     } else {
1969         workspace = con_get_workspace(focused);
1970         old_name = workspace->name;
1971     }
1972
1973     if (!workspace) {
1974         yerror("Old workspace \"%s\" not found", old_name);
1975         return;
1976     }
1977
1978     Con *check_dest = NULL;
1979     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1980     GREP_FIRST(check_dest, output_get_content(output),
1981                !strcasecmp(child->name, new_name));
1982
1983     if (check_dest != NULL) {
1984         yerror("New workspace \"%s\" already exists", new_name);
1985         return;
1986     }
1987
1988     /* Change the name and try to parse it as a number. */
1989     FREE(workspace->name);
1990     workspace->name = sstrdup(new_name);
1991
1992     workspace->num = ws_name_to_number(new_name);
1993     LOG("num = %d\n", workspace->num);
1994
1995     /* By re-attaching, the sort order will be correct afterwards. */
1996     Con *previously_focused = focused;
1997     Con *parent = workspace->parent;
1998     con_detach(workspace);
1999     con_attach(workspace, parent, false);
2000
2001     /* Move the workspace to the correct output if it has an assignment */
2002     struct Workspace_Assignment *assignment = NULL;
2003     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2004         if (assignment->output == NULL)
2005             continue;
2006         if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2007             continue;
2008         }
2009
2010         workspace_move_to_output(workspace, assignment->output);
2011
2012         if (previously_focused)
2013             workspace_show(con_get_workspace(previously_focused));
2014
2015         break;
2016     }
2017
2018     /* Restore the previous focus since con_attach messes with the focus. */
2019     con_focus(previously_focused);
2020
2021     cmd_output->needs_tree_render = true;
2022     ysuccess(true);
2023
2024     ipc_send_workspace_event("rename", workspace, NULL);
2025     ewmh_update_desktop_names();
2026     ewmh_update_desktop_viewport();
2027     ewmh_update_current_desktop();
2028
2029     startup_sequence_rename_workspace(old_name, new_name);
2030 }
2031
2032 /*
2033  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2034  *
2035  */
2036 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2037     int mode = M_DOCK;
2038     bool toggle = false;
2039     if (strcmp(bar_mode, "dock") == 0)
2040         mode = M_DOCK;
2041     else if (strcmp(bar_mode, "hide") == 0)
2042         mode = M_HIDE;
2043     else if (strcmp(bar_mode, "invisible") == 0)
2044         mode = M_INVISIBLE;
2045     else if (strcmp(bar_mode, "toggle") == 0)
2046         toggle = true;
2047     else {
2048         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2049         return false;
2050     }
2051
2052     bool changed_sth = false;
2053     Barconfig *current = NULL;
2054     TAILQ_FOREACH(current, &barconfigs, configs) {
2055         if (bar_id && strcmp(current->id, bar_id) != 0)
2056             continue;
2057
2058         if (toggle)
2059             mode = (current->mode + 1) % 2;
2060
2061         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2062         current->mode = mode;
2063         changed_sth = true;
2064
2065         if (bar_id)
2066             break;
2067     }
2068
2069     if (bar_id && !changed_sth) {
2070         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2071         return false;
2072     }
2073
2074     return true;
2075 }
2076
2077 /*
2078  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2079  *
2080  */
2081 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2082     int hidden_state = S_SHOW;
2083     bool toggle = false;
2084     if (strcmp(bar_hidden_state, "hide") == 0)
2085         hidden_state = S_HIDE;
2086     else if (strcmp(bar_hidden_state, "show") == 0)
2087         hidden_state = S_SHOW;
2088     else if (strcmp(bar_hidden_state, "toggle") == 0)
2089         toggle = true;
2090     else {
2091         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2092         return false;
2093     }
2094
2095     bool changed_sth = false;
2096     Barconfig *current = NULL;
2097     TAILQ_FOREACH(current, &barconfigs, configs) {
2098         if (bar_id && strcmp(current->id, bar_id) != 0)
2099             continue;
2100
2101         if (toggle)
2102             hidden_state = (current->hidden_state + 1) % 2;
2103
2104         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2105         current->hidden_state = hidden_state;
2106         changed_sth = true;
2107
2108         if (bar_id)
2109             break;
2110     }
2111
2112     if (bar_id && !changed_sth) {
2113         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2114         return false;
2115     }
2116
2117     return true;
2118 }
2119
2120 /*
2121  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2122  *
2123  */
2124 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2125     bool ret;
2126     if (strcmp(bar_type, "mode") == 0)
2127         ret = cmd_bar_mode(bar_value, bar_id);
2128     else if (strcmp(bar_type, "hidden_state") == 0)
2129         ret = cmd_bar_hidden_state(bar_value, bar_id);
2130     else {
2131         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2132         ret = false;
2133     }
2134
2135     ysuccess(ret);
2136     if (!ret)
2137         return;
2138
2139     update_barconfig();
2140 }
2141
2142 /*
2143  * Implementation of 'shmlog <size>|toggle|on|off'
2144  *
2145  */
2146 void cmd_shmlog(I3_CMD, const char *argument) {
2147     if (!strcmp(argument, "toggle"))
2148         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2149         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2150     else if (!strcmp(argument, "on"))
2151         shmlog_size = default_shmlog_size;
2152     else if (!strcmp(argument, "off"))
2153         shmlog_size = 0;
2154     else {
2155         /* If shm logging now, restart logging with the new size. */
2156         if (shmlog_size > 0) {
2157             shmlog_size = 0;
2158             LOG("Restarting shm logging...\n");
2159             init_logging();
2160         }
2161         shmlog_size = atoi(argument);
2162         /* Make a weakly attempt at ensuring the argument is valid. */
2163         if (shmlog_size <= 0)
2164             shmlog_size = default_shmlog_size;
2165     }
2166     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2167     init_logging();
2168     update_shmlog_atom();
2169     // XXX: default reply for now, make this a better reply
2170     ysuccess(true);
2171 }
2172
2173 /*
2174  * Implementation of 'debuglog toggle|on|off'
2175  *
2176  */
2177 void cmd_debuglog(I3_CMD, const char *argument) {
2178     bool logging = get_debug_logging();
2179     if (!strcmp(argument, "toggle")) {
2180         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2181         set_debug_logging(!logging);
2182     } else if (!strcmp(argument, "on") && !logging) {
2183         LOG("Enabling debug logging\n");
2184         set_debug_logging(true);
2185     } else if (!strcmp(argument, "off") && logging) {
2186         LOG("Disabling debug logging\n");
2187         set_debug_logging(false);
2188     }
2189     // XXX: default reply for now, make this a better reply
2190     ysuccess(true);
2191 }