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