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