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