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