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