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