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