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