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