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