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