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