]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Glob filepath when calling append_layout
[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
903     /* Make sure we allow paths like '~/.i3/layout.json' */
904     path = resolve_tilde(path);
905
906     json_content_t content = json_determine_content(path);
907     LOG("JSON content = %d\n", content);
908     if (content == JSON_CONTENT_UNKNOWN) {
909         ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
910         ysuccess(false);
911         free(path);
912         return;
913     }
914
915     Con *parent = focused;
916     if (content == JSON_CONTENT_WORKSPACE) {
917         parent = output_get_content(con_get_output(parent));
918     } else {
919         /* We need to append the layout to a split container, since a leaf
920          * container must not have any children (by definition).
921          * Note that we explicitly check for workspaces, since they are okay for
922          * this purpose, but con_accepts_window() returns false for workspaces. */
923         while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
924             parent = parent->parent;
925     }
926     DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
927     char *errormsg = NULL;
928     tree_append_json(parent, path, &errormsg);
929     if (errormsg != NULL) {
930         yerror(errormsg);
931         free(errormsg);
932         /* Note that we continue executing since tree_append_json() has
933          * side-effects — user-provided layouts can be partly valid, partly
934          * invalid, leading to half of the placeholder containers being
935          * created. */
936     } else {
937         ysuccess(true);
938     }
939
940     // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
941     // false); should be enough, but when sending 'workspace 4; append_layout
942     // /tmp/foo.json', the needs_tree_render == true of the workspace command
943     // is not executed yet and will be batched with append_layout’s
944     // needs_tree_render after the parser finished. We should check if that is
945     // necessary at all.
946     render_con(croot, false);
947
948     restore_open_placeholder_windows(parent);
949
950     if (content == JSON_CONTENT_WORKSPACE)
951         ipc_send_workspace_event("restored", parent, NULL);
952
953     free(path);
954     cmd_output->needs_tree_render = true;
955 }
956
957 /*
958  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
959  *
960  */
961 void cmd_workspace(I3_CMD, char *which) {
962     Con *ws;
963
964     DLOG("which=%s\n", which);
965
966     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
967         LOG("Cannot switch workspace while in global fullscreen\n");
968         ysuccess(false);
969         return;
970     }
971
972     if (strcmp(which, "next") == 0)
973         ws = workspace_next();
974     else if (strcmp(which, "prev") == 0)
975         ws = workspace_prev();
976     else if (strcmp(which, "next_on_output") == 0)
977         ws = workspace_next_on_output();
978     else if (strcmp(which, "prev_on_output") == 0)
979         ws = workspace_prev_on_output();
980     else {
981         ELOG("BUG: called with which=%s\n", which);
982         ysuccess(false);
983         return;
984     }
985
986     workspace_show(ws);
987
988     cmd_output->needs_tree_render = true;
989     // XXX: default reply for now, make this a better reply
990     ysuccess(true);
991 }
992
993 /*
994  * Implementation of 'workspace number <name>'
995  *
996  */
997 void cmd_workspace_number(I3_CMD, char *which) {
998     Con *output, *workspace = NULL;
999
1000     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1001         LOG("Cannot switch workspace while in global fullscreen\n");
1002         ysuccess(false);
1003         return;
1004     }
1005
1006     long parsed_num = ws_name_to_number(which);
1007
1008     if (parsed_num == -1) {
1009         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1010         // TODO: better error message
1011         yerror("Could not parse number");
1012         return;
1013     }
1014
1015     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1016     GREP_FIRST(workspace, output_get_content(output),
1017                child->num == parsed_num);
1018
1019     if (!workspace) {
1020         LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1021         ysuccess(true);
1022         workspace_show_by_name(which);
1023         cmd_output->needs_tree_render = true;
1024         return;
1025     }
1026     if (maybe_back_and_forth(cmd_output, workspace->name))
1027         return;
1028     workspace_show(workspace);
1029
1030     cmd_output->needs_tree_render = true;
1031     // XXX: default reply for now, make this a better reply
1032     ysuccess(true);
1033 }
1034
1035 /*
1036  * Implementation of 'workspace back_and_forth'.
1037  *
1038  */
1039 void cmd_workspace_back_and_forth(I3_CMD) {
1040     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1041         LOG("Cannot switch workspace while in global fullscreen\n");
1042         ysuccess(false);
1043         return;
1044     }
1045
1046     workspace_back_and_forth();
1047
1048     cmd_output->needs_tree_render = true;
1049     // XXX: default reply for now, make this a better reply
1050     ysuccess(true);
1051 }
1052
1053 /*
1054  * Implementation of 'workspace <name>'
1055  *
1056  */
1057 void cmd_workspace_name(I3_CMD, char *name) {
1058     if (strncasecmp(name, "__", strlen("__")) == 0) {
1059         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1060         ysuccess(false);
1061         return;
1062     }
1063
1064     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1065         LOG("Cannot switch workspace while in global fullscreen\n");
1066         ysuccess(false);
1067         return;
1068     }
1069
1070     DLOG("should switch to workspace %s\n", name);
1071     if (maybe_back_and_forth(cmd_output, name))
1072         return;
1073
1074     Con *ws = NULL;
1075     Con *output = NULL;
1076
1077     /* first look for a workspace with this name */
1078     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1079         GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1080     }
1081
1082     /* if the name is only digits, we interpret this as a "workspace number"
1083      * command */
1084     if (!ws && name_is_digits(name)) {
1085         long parsed_num = ws_name_to_number(name);
1086         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1087             GREP_FIRST(ws, output_get_content(output),
1088                        child->num == parsed_num);
1089         }
1090     }
1091
1092     /* if no workspace was found, make a new one */
1093     if (!ws)
1094         ws = workspace_get(name, NULL);
1095
1096     workspace_show(ws);
1097
1098     cmd_output->needs_tree_render = true;
1099     // XXX: default reply for now, make this a better reply
1100     ysuccess(true);
1101 }
1102
1103 /*
1104  * Implementation of 'mark <mark>'
1105  *
1106  */
1107 void cmd_mark(I3_CMD, char *mark) {
1108     DLOG("Clearing all windows which have that mark first\n");
1109
1110     Con *con;
1111     TAILQ_FOREACH(con, &all_cons, all_cons) {
1112         if (con->mark && strcmp(con->mark, mark) == 0)
1113             FREE(con->mark);
1114     }
1115
1116     DLOG("marking window with str %s\n", mark);
1117     owindow *current;
1118
1119     HANDLE_EMPTY_MATCH;
1120
1121     TAILQ_FOREACH(current, &owindows, owindows) {
1122         DLOG("matching: %p / %s\n", current->con, current->con->name);
1123         current->con->mark = sstrdup(mark);
1124     }
1125
1126     cmd_output->needs_tree_render = true;
1127     // XXX: default reply for now, make this a better reply
1128     ysuccess(true);
1129 }
1130
1131 /*
1132  * Implementation of 'unmark [mark]'
1133  *
1134  */
1135 void cmd_unmark(I3_CMD, char *mark) {
1136     if (mark == NULL) {
1137         Con *con;
1138         TAILQ_FOREACH(con, &all_cons, all_cons) {
1139             FREE(con->mark);
1140         }
1141         DLOG("removed all window marks");
1142     } else {
1143         Con *con;
1144         TAILQ_FOREACH(con, &all_cons, all_cons) {
1145             if (con->mark && strcmp(con->mark, mark) == 0)
1146                 FREE(con->mark);
1147         }
1148         DLOG("removed window mark %s\n", mark);
1149     }
1150
1151     cmd_output->needs_tree_render = true;
1152     // XXX: default reply for now, make this a better reply
1153     ysuccess(true);
1154 }
1155
1156 /*
1157  * Implementation of 'mode <string>'.
1158  *
1159  */
1160 void cmd_mode(I3_CMD, char *mode) {
1161     DLOG("mode=%s\n", mode);
1162     switch_mode(mode);
1163
1164     // XXX: default reply for now, make this a better reply
1165     ysuccess(true);
1166 }
1167
1168 /*
1169  * Implementation of 'move [window|container] [to] output <str>'.
1170  *
1171  */
1172 void cmd_move_con_to_output(I3_CMD, char *name) {
1173     owindow *current;
1174
1175     DLOG("should move window to output %s\n", name);
1176
1177     HANDLE_EMPTY_MATCH;
1178
1179     /* get the output */
1180     Output *current_output = NULL;
1181     Output *output;
1182
1183     // TODO: fix the handling of criteria
1184     TAILQ_FOREACH(current, &owindows, owindows)
1185     current_output = get_output_of_con(current->con);
1186
1187     assert(current_output != NULL);
1188
1189     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1190     if (strcasecmp(name, "up") == 0)
1191         output = get_output_next_wrap(D_UP, current_output);
1192     else if (strcasecmp(name, "down") == 0)
1193         output = get_output_next_wrap(D_DOWN, current_output);
1194     else if (strcasecmp(name, "left") == 0)
1195         output = get_output_next_wrap(D_LEFT, current_output);
1196     else if (strcasecmp(name, "right") == 0)
1197         output = get_output_next_wrap(D_RIGHT, current_output);
1198     else
1199         output = get_output_by_name(name);
1200
1201     if (!output) {
1202         LOG("No such output found.\n");
1203         ysuccess(false);
1204         return;
1205     }
1206
1207     /* get visible workspace on output */
1208     Con *ws = NULL;
1209     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1210     if (!ws) {
1211         ysuccess(false);
1212         return;
1213     }
1214
1215     TAILQ_FOREACH(current, &owindows, owindows) {
1216         DLOG("matching: %p / %s\n", current->con, current->con->name);
1217         con_move_to_workspace(current->con, ws, true, false);
1218     }
1219
1220     cmd_output->needs_tree_render = true;
1221     // XXX: default reply for now, make this a better reply
1222     ysuccess(true);
1223 }
1224
1225 /*
1226  * Implementation of 'floating enable|disable|toggle'
1227  *
1228  */
1229 void cmd_floating(I3_CMD, char *floating_mode) {
1230     owindow *current;
1231
1232     DLOG("floating_mode=%s\n", floating_mode);
1233
1234     HANDLE_EMPTY_MATCH;
1235
1236     TAILQ_FOREACH(current, &owindows, owindows) {
1237         DLOG("matching: %p / %s\n", current->con, current->con->name);
1238         if (strcmp(floating_mode, "toggle") == 0) {
1239             DLOG("should toggle mode\n");
1240             toggle_floating_mode(current->con, false);
1241         } else {
1242             DLOG("should switch mode to %s\n", floating_mode);
1243             if (strcmp(floating_mode, "enable") == 0) {
1244                 floating_enable(current->con, false);
1245             } else {
1246                 floating_disable(current->con, false);
1247             }
1248         }
1249     }
1250
1251     cmd_output->needs_tree_render = true;
1252     // XXX: default reply for now, make this a better reply
1253     ysuccess(true);
1254 }
1255
1256 /*
1257  * Implementation of 'move workspace to [output] <str>'.
1258  *
1259  */
1260 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1261     DLOG("should move workspace to output %s\n", name);
1262
1263     HANDLE_EMPTY_MATCH;
1264
1265     owindow *current;
1266     TAILQ_FOREACH(current, &owindows, owindows) {
1267         Output *current_output = get_output_of_con(current->con);
1268         if (!current_output) {
1269             ELOG("Cannot get current output. This is a bug in i3.\n");
1270             ysuccess(false);
1271             return;
1272         }
1273         Output *output = get_output_from_string(current_output, name);
1274         if (!output) {
1275             ELOG("Could not get output from string \"%s\"\n", name);
1276             ysuccess(false);
1277             return;
1278         }
1279
1280         Con *content = output_get_content(output->con);
1281         LOG("got output %p with content %p\n", output, content);
1282
1283         Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1284         LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1285
1286         Con *ws = con_get_workspace(current->con);
1287         LOG("should move workspace %p / %s\n", ws, ws->name);
1288         bool workspace_was_visible = workspace_is_visible(ws);
1289
1290         if (con_num_children(ws->parent) == 1) {
1291             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1292
1293             /* check if we can find a workspace assigned to this output */
1294             bool used_assignment = false;
1295             struct Workspace_Assignment *assignment;
1296             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1297                 if (strcmp(assignment->output, current_output->name) != 0)
1298                     continue;
1299
1300                 /* check if this workspace is already attached to the tree */
1301                 Con *workspace = NULL, *out;
1302                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1303                 GREP_FIRST(workspace, output_get_content(out),
1304                            !strcasecmp(child->name, assignment->name));
1305                 if (workspace != NULL)
1306                     continue;
1307
1308                 /* so create the workspace referenced to by this assignment */
1309                 LOG("Creating workspace from assignment %s.\n", assignment->name);
1310                 workspace_get(assignment->name, NULL);
1311                 used_assignment = true;
1312                 break;
1313             }
1314
1315             /* if we couldn't create the workspace using an assignment, create
1316              * it on the output */
1317             if (!used_assignment)
1318                 create_workspace_on_output(current_output, ws->parent);
1319
1320             /* notify the IPC listeners */
1321             ipc_send_workspace_event("init", ws, NULL);
1322         }
1323         DLOG("Detaching\n");
1324
1325         /* detach from the old output and attach to the new output */
1326         Con *old_content = ws->parent;
1327         con_detach(ws);
1328         if (workspace_was_visible) {
1329             /* The workspace which we just detached was visible, so focus
1330              * the next one in the focus-stack. */
1331             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1332             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1333             workspace_show(focus_ws);
1334         }
1335         con_attach(ws, content, false);
1336
1337         /* fix the coordinates of the floating containers */
1338         Con *floating_con;
1339         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1340         floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1341
1342         ipc_send_workspace_event("move", ws, NULL);
1343         if (workspace_was_visible) {
1344             /* Focus the moved workspace on the destination output. */
1345             workspace_show(ws);
1346         }
1347
1348         /* NB: We cannot simply work with previously_visible_ws since it might
1349          * have been cleaned up by workspace_show() already, depending on the
1350          * focus order/number of other workspaces on the output.
1351          * Instead, we loop through the available workspaces and only work with
1352          * previously_visible_ws if we still find it. */
1353         TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1354             if (ws != previously_visible_ws)
1355                 continue;
1356
1357             /* Call the on_remove_child callback of the workspace which previously
1358              * was visible on the destination output. Since it is no longer
1359              * visible, it might need to get cleaned up. */
1360             CALL(previously_visible_ws, on_remove_child);
1361             break;
1362         }
1363     }
1364
1365     cmd_output->needs_tree_render = true;
1366     // XXX: default reply for now, make this a better reply
1367     ysuccess(true);
1368 }
1369
1370 /*
1371  * Implementation of 'split v|h|vertical|horizontal'.
1372  *
1373  */
1374 void cmd_split(I3_CMD, char *direction) {
1375     owindow *current;
1376     /* TODO: use matches */
1377     LOG("splitting in direction %c\n", direction[0]);
1378     if (match_is_empty(current_match))
1379         tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1380     else {
1381         TAILQ_FOREACH(current, &owindows, owindows) {
1382             DLOG("matching: %p / %s\n", current->con, current->con->name);
1383             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1384         }
1385     }
1386
1387     cmd_output->needs_tree_render = true;
1388     // XXX: default reply for now, make this a better reply
1389     ysuccess(true);
1390 }
1391
1392 /*
1393  * Implementation of 'kill [window|client]'.
1394  *
1395  */
1396 void cmd_kill(I3_CMD, char *kill_mode_str) {
1397     if (kill_mode_str == NULL)
1398         kill_mode_str = "window";
1399     owindow *current;
1400
1401     DLOG("kill_mode=%s\n", kill_mode_str);
1402
1403     int kill_mode;
1404     if (strcmp(kill_mode_str, "window") == 0)
1405         kill_mode = KILL_WINDOW;
1406     else if (strcmp(kill_mode_str, "client") == 0)
1407         kill_mode = KILL_CLIENT;
1408     else {
1409         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1410         ysuccess(false);
1411         return;
1412     }
1413
1414     /* check if the match is empty, not if the result is empty */
1415     if (match_is_empty(current_match))
1416         tree_close_con(kill_mode);
1417     else {
1418         TAILQ_FOREACH(current, &owindows, owindows) {
1419             DLOG("matching: %p / %s\n", current->con, current->con->name);
1420             tree_close(current->con, kill_mode, false, false);
1421         }
1422     }
1423
1424     cmd_output->needs_tree_render = true;
1425     // XXX: default reply for now, make this a better reply
1426     ysuccess(true);
1427 }
1428
1429 /*
1430  * Implementation of 'exec [--no-startup-id] <command>'.
1431  *
1432  */
1433 void cmd_exec(I3_CMD, char *nosn, char *command) {
1434     bool no_startup_id = (nosn != NULL);
1435
1436     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1437     start_application(command, no_startup_id);
1438
1439     // XXX: default reply for now, make this a better reply
1440     ysuccess(true);
1441 }
1442
1443 /*
1444  * Implementation of 'focus left|right|up|down'.
1445  *
1446  */
1447 void cmd_focus_direction(I3_CMD, char *direction) {
1448     DLOG("direction = *%s*\n", direction);
1449
1450     if (strcmp(direction, "left") == 0)
1451         tree_next('p', HORIZ);
1452     else if (strcmp(direction, "right") == 0)
1453         tree_next('n', HORIZ);
1454     else if (strcmp(direction, "up") == 0)
1455         tree_next('p', VERT);
1456     else if (strcmp(direction, "down") == 0)
1457         tree_next('n', VERT);
1458     else {
1459         ELOG("Invalid focus direction (%s)\n", direction);
1460         ysuccess(false);
1461         return;
1462     }
1463
1464     cmd_output->needs_tree_render = true;
1465     // XXX: default reply for now, make this a better reply
1466     ysuccess(true);
1467 }
1468
1469 /*
1470  * Implementation of 'focus tiling|floating|mode_toggle'.
1471  *
1472  */
1473 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1474     DLOG("window_mode = %s\n", window_mode);
1475
1476     Con *ws = con_get_workspace(focused);
1477     Con *current;
1478     if (ws != NULL) {
1479         if (strcmp(window_mode, "mode_toggle") == 0) {
1480             current = TAILQ_FIRST(&(ws->focus_head));
1481             if (current != NULL && current->type == CT_FLOATING_CON)
1482                 window_mode = "tiling";
1483             else
1484                 window_mode = "floating";
1485         }
1486         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1487             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1488                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1489                 continue;
1490
1491             con_focus(con_descend_focused(current));
1492             break;
1493         }
1494     }
1495
1496     cmd_output->needs_tree_render = true;
1497     // XXX: default reply for now, make this a better reply
1498     ysuccess(true);
1499 }
1500
1501 /*
1502  * Implementation of 'focus parent|child'.
1503  *
1504  */
1505 void cmd_focus_level(I3_CMD, char *level) {
1506     DLOG("level = %s\n", level);
1507     bool success = false;
1508
1509     /* Focusing the parent can only be allowed if the newly
1510      * focused container won't escape the fullscreen container. */
1511     if (strcmp(level, "parent") == 0) {
1512         if (focused && focused->parent) {
1513             if (con_fullscreen_permits_focusing(focused->parent))
1514                 success = level_up();
1515             else
1516                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1517         }
1518     }
1519
1520     /* Focusing a child should always be allowed. */
1521     else
1522         success = level_down();
1523
1524     cmd_output->needs_tree_render = success;
1525     // XXX: default reply for now, make this a better reply
1526     ysuccess(success);
1527 }
1528
1529 /*
1530  * Implementation of 'focus'.
1531  *
1532  */
1533 void cmd_focus(I3_CMD) {
1534     DLOG("current_match = %p\n", current_match);
1535
1536     if (match_is_empty(current_match)) {
1537         ELOG("You have to specify which window/container should be focused.\n");
1538         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1539
1540         yerror("You have to specify which window/container should be focused");
1541
1542         return;
1543     }
1544
1545     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1546     int count = 0;
1547     owindow *current;
1548     TAILQ_FOREACH(current, &owindows, owindows) {
1549         Con *ws = con_get_workspace(current->con);
1550         /* If no workspace could be found, this was a dock window.
1551          * Just skip it, you cannot focus dock windows. */
1552         if (!ws)
1553             continue;
1554
1555         /* Check the fullscreen focus constraints. */
1556         if (!con_fullscreen_permits_focusing(current->con)) {
1557             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1558             ysuccess(false);
1559             return;
1560         }
1561
1562         /* In case this is a scratchpad window, call scratchpad_show(). */
1563         if (ws == __i3_scratch) {
1564             scratchpad_show(current->con);
1565             count++;
1566             /* While for the normal focus case we can change focus multiple
1567              * times and only a single window ends up focused, we could show
1568              * multiple scratchpad windows. So, rather break here. */
1569             break;
1570         }
1571
1572         /* If the container is not on the current workspace,
1573          * workspace_show() will switch to a different workspace and (if
1574          * enabled) trigger a mouse pointer warp to the currently focused
1575          * container (!) on the target workspace.
1576          *
1577          * Therefore, before calling workspace_show(), we make sure that
1578          * 'current' will be focused on the workspace. However, we cannot
1579          * just con_focus(current) because then the pointer will not be
1580          * warped at all (the code thinks we are already there).
1581          *
1582          * So we focus 'current' to make it the currently focused window of
1583          * the target workspace, then revert focus. */
1584         Con *currently_focused = focused;
1585         con_focus(current->con);
1586         con_focus(currently_focused);
1587
1588         /* Now switch to the workspace, then focus */
1589         workspace_show(ws);
1590         LOG("focusing %p / %s\n", current->con, current->con->name);
1591         con_focus(current->con);
1592         count++;
1593     }
1594
1595     if (count > 1)
1596         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1597             "while only exactly one container can be focused at a time.\n",
1598             count);
1599
1600     cmd_output->needs_tree_render = true;
1601     // XXX: default reply for now, make this a better reply
1602     ysuccess(true);
1603 }
1604
1605 /*
1606  * Implementation of 'fullscreen enable|toggle [global]' and
1607  *                   'fullscreen disable'
1608  *
1609  */
1610 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1611     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1612     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1613     owindow *current;
1614
1615     HANDLE_EMPTY_MATCH;
1616
1617     TAILQ_FOREACH(current, &owindows, owindows) {
1618         DLOG("matching: %p / %s\n", current->con, current->con->name);
1619         if (strcmp(action, "toggle") == 0) {
1620             con_toggle_fullscreen(current->con, mode);
1621         } else if (strcmp(action, "enable") == 0) {
1622             con_enable_fullscreen(current->con, mode);
1623         } else if (strcmp(action, "disable") == 0) {
1624             con_disable_fullscreen(current->con);
1625         }
1626     }
1627
1628     cmd_output->needs_tree_render = true;
1629     // XXX: default reply for now, make this a better reply
1630     ysuccess(true);
1631 }
1632
1633 /*
1634  * Implementation of 'move <direction> [<pixels> [px]]'.
1635  *
1636  */
1637 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1638     // 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
1639     int px = atoi(move_px);
1640
1641     owindow *current;
1642     HANDLE_EMPTY_MATCH;
1643
1644     Con *initially_focused = focused;
1645
1646     TAILQ_FOREACH(current, &owindows, owindows) {
1647         DLOG("moving in direction %s, px %s\n", direction, move_px);
1648         if (con_is_floating(current->con)) {
1649             DLOG("floating move with %d pixels\n", px);
1650             Rect newrect = current->con->parent->rect;
1651             if (strcmp(direction, "left") == 0) {
1652                 newrect.x -= px;
1653             } else if (strcmp(direction, "right") == 0) {
1654                 newrect.x += px;
1655             } else if (strcmp(direction, "up") == 0) {
1656                 newrect.y -= px;
1657             } else if (strcmp(direction, "down") == 0) {
1658                 newrect.y += px;
1659             }
1660             floating_reposition(current->con->parent, newrect);
1661         } else {
1662             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1663             cmd_output->needs_tree_render = true;
1664         }
1665     }
1666
1667     /* the move command should not disturb focus */
1668     if (focused != initially_focused)
1669         con_focus(initially_focused);
1670
1671     // XXX: default reply for now, make this a better reply
1672     ysuccess(true);
1673 }
1674
1675 /*
1676  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1677  *
1678  */
1679 void cmd_layout(I3_CMD, char *layout_str) {
1680     if (strcmp(layout_str, "stacking") == 0)
1681         layout_str = "stacked";
1682     owindow *current;
1683     layout_t layout;
1684     /* default is a special case which will be handled in con_set_layout(). */
1685     if (strcmp(layout_str, "default") == 0)
1686         layout = L_DEFAULT;
1687     else if (strcmp(layout_str, "stacked") == 0)
1688         layout = L_STACKED;
1689     else if (strcmp(layout_str, "tabbed") == 0)
1690         layout = L_TABBED;
1691     else if (strcmp(layout_str, "splitv") == 0)
1692         layout = L_SPLITV;
1693     else if (strcmp(layout_str, "splith") == 0)
1694         layout = L_SPLITH;
1695     else {
1696         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1697         return;
1698     }
1699
1700     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1701
1702     /* check if the match is empty, not if the result is empty */
1703     if (match_is_empty(current_match))
1704         con_set_layout(focused, layout);
1705     else {
1706         TAILQ_FOREACH(current, &owindows, owindows) {
1707             DLOG("matching: %p / %s\n", current->con, current->con->name);
1708             con_set_layout(current->con, layout);
1709         }
1710     }
1711
1712     cmd_output->needs_tree_render = true;
1713     // XXX: default reply for now, make this a better reply
1714     ysuccess(true);
1715 }
1716
1717 /*
1718  * Implementation of 'layout toggle [all|split]'.
1719  *
1720  */
1721 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1722     owindow *current;
1723
1724     if (toggle_mode == NULL)
1725         toggle_mode = "default";
1726
1727     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1728
1729     /* check if the match is empty, not if the result is empty */
1730     if (match_is_empty(current_match))
1731         con_toggle_layout(focused, toggle_mode);
1732     else {
1733         TAILQ_FOREACH(current, &owindows, owindows) {
1734             DLOG("matching: %p / %s\n", current->con, current->con->name);
1735             con_toggle_layout(current->con, toggle_mode);
1736         }
1737     }
1738
1739     cmd_output->needs_tree_render = true;
1740     // XXX: default reply for now, make this a better reply
1741     ysuccess(true);
1742 }
1743
1744 /*
1745  * Implementation of 'exit'.
1746  *
1747  */
1748 void cmd_exit(I3_CMD) {
1749     LOG("Exiting due to user command.\n");
1750     ipc_shutdown();
1751     unlink(config.ipc_socket_path);
1752     xcb_disconnect(conn);
1753     exit(0);
1754
1755     /* unreached */
1756 }
1757
1758 /*
1759  * Implementation of 'reload'.
1760  *
1761  */
1762 void cmd_reload(I3_CMD) {
1763     LOG("reloading\n");
1764     kill_nagbar(&config_error_nagbar_pid, false);
1765     kill_nagbar(&command_error_nagbar_pid, false);
1766     load_configuration(conn, NULL, true);
1767     x_set_i3_atoms();
1768     /* Send an IPC event just in case the ws names have changed */
1769     ipc_send_workspace_event("reload", NULL, NULL);
1770     /* Send an update event for the barconfig just in case it has changed */
1771     update_barconfig();
1772
1773     // XXX: default reply for now, make this a better reply
1774     ysuccess(true);
1775 }
1776
1777 /*
1778  * Implementation of 'restart'.
1779  *
1780  */
1781 void cmd_restart(I3_CMD) {
1782     LOG("restarting i3\n");
1783     ipc_shutdown();
1784     unlink(config.ipc_socket_path);
1785     /* We need to call this manually since atexit handlers don’t get called
1786      * when exec()ing */
1787     purge_zerobyte_logfile();
1788     i3_restart(false);
1789
1790     // XXX: default reply for now, make this a better reply
1791     ysuccess(true);
1792 }
1793
1794 /*
1795  * Implementation of 'open'.
1796  *
1797  */
1798 void cmd_open(I3_CMD) {
1799     LOG("opening new container\n");
1800     Con *con = tree_open_con(NULL, NULL);
1801     con->layout = L_SPLITH;
1802     con_focus(con);
1803
1804     y(map_open);
1805     ystr("success");
1806     y(bool, true);
1807     ystr("id");
1808     y(integer, (long int)con);
1809     y(map_close);
1810
1811     cmd_output->needs_tree_render = true;
1812 }
1813
1814 /*
1815  * Implementation of 'focus output <output>'.
1816  *
1817  */
1818 void cmd_focus_output(I3_CMD, char *name) {
1819     owindow *current;
1820
1821     DLOG("name = %s\n", name);
1822
1823     HANDLE_EMPTY_MATCH;
1824
1825     /* get the output */
1826     Output *current_output = NULL;
1827     Output *output;
1828
1829     TAILQ_FOREACH(current, &owindows, owindows)
1830     current_output = get_output_of_con(current->con);
1831     assert(current_output != NULL);
1832
1833     output = get_output_from_string(current_output, name);
1834
1835     if (!output) {
1836         LOG("No such output found.\n");
1837         ysuccess(false);
1838         return;
1839     }
1840
1841     /* get visible workspace on output */
1842     Con *ws = NULL;
1843     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1844     if (!ws) {
1845         ysuccess(false);
1846         return;
1847     }
1848
1849     workspace_show(ws);
1850
1851     cmd_output->needs_tree_render = true;
1852     // XXX: default reply for now, make this a better reply
1853     ysuccess(true);
1854 }
1855
1856 /*
1857  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1858  *
1859  */
1860 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1861     int x = atoi(cx);
1862     int y = atoi(cy);
1863     bool has_error = false;
1864
1865     owindow *current;
1866     HANDLE_EMPTY_MATCH;
1867
1868     TAILQ_FOREACH(current, &owindows, owindows) {
1869         if (!con_is_floating(current->con)) {
1870             ELOG("Cannot change position. The window/container is not floating\n");
1871
1872             if (!has_error) {
1873                 yerror("Cannot change position of a window/container because it is not floating.");
1874                 has_error = true;
1875             }
1876
1877             continue;
1878         }
1879
1880         if (strcmp(method, "absolute") == 0) {
1881             current->con->parent->rect.x = x;
1882             current->con->parent->rect.y = y;
1883
1884             DLOG("moving to absolute position %d %d\n", x, y);
1885             floating_maybe_reassign_ws(current->con->parent);
1886             cmd_output->needs_tree_render = true;
1887         }
1888
1889         if (strcmp(method, "position") == 0) {
1890             Rect newrect = current->con->parent->rect;
1891
1892             DLOG("moving to position %d %d\n", x, y);
1893             newrect.x = x;
1894             newrect.y = y;
1895
1896             floating_reposition(current->con->parent, newrect);
1897         }
1898     }
1899
1900     // XXX: default reply for now, make this a better reply
1901     if (!has_error)
1902         ysuccess(true);
1903 }
1904
1905 /*
1906  * Implementation of 'move [window|container] [to] [absolute] position center
1907  *
1908  */
1909 void cmd_move_window_to_center(I3_CMD, char *method) {
1910     if (!con_is_floating(focused)) {
1911         ELOG("Cannot change position. The window/container is not floating\n");
1912         yerror("Cannot change position. The window/container is not floating.");
1913         return;
1914     }
1915
1916     if (strcmp(method, "absolute") == 0) {
1917         Rect *rect = &focused->parent->rect;
1918
1919         DLOG("moving to absolute center\n");
1920         rect->x = croot->rect.width / 2 - rect->width / 2;
1921         rect->y = croot->rect.height / 2 - rect->height / 2;
1922
1923         floating_maybe_reassign_ws(focused->parent);
1924         cmd_output->needs_tree_render = true;
1925     }
1926
1927     if (strcmp(method, "position") == 0) {
1928         Rect *wsrect = &con_get_workspace(focused)->rect;
1929         Rect newrect = focused->parent->rect;
1930
1931         DLOG("moving to center\n");
1932         newrect.x = wsrect->width / 2 - newrect.width / 2;
1933         newrect.y = wsrect->height / 2 - newrect.height / 2;
1934
1935         floating_reposition(focused->parent, newrect);
1936     }
1937
1938     // XXX: default reply for now, make this a better reply
1939     ysuccess(true);
1940 }
1941
1942 /*
1943  * Implementation of 'move scratchpad'.
1944  *
1945  */
1946 void cmd_move_scratchpad(I3_CMD) {
1947     DLOG("should move window to scratchpad\n");
1948     owindow *current;
1949
1950     HANDLE_EMPTY_MATCH;
1951
1952     TAILQ_FOREACH(current, &owindows, owindows) {
1953         DLOG("matching: %p / %s\n", current->con, current->con->name);
1954         scratchpad_move(current->con);
1955     }
1956
1957     cmd_output->needs_tree_render = true;
1958     // XXX: default reply for now, make this a better reply
1959     ysuccess(true);
1960 }
1961
1962 /*
1963  * Implementation of 'scratchpad show'.
1964  *
1965  */
1966 void cmd_scratchpad_show(I3_CMD) {
1967     DLOG("should show scratchpad window\n");
1968     owindow *current;
1969
1970     if (match_is_empty(current_match)) {
1971         scratchpad_show(NULL);
1972     } else {
1973         TAILQ_FOREACH(current, &owindows, owindows) {
1974             DLOG("matching: %p / %s\n", current->con, current->con->name);
1975             scratchpad_show(current->con);
1976         }
1977     }
1978
1979     cmd_output->needs_tree_render = true;
1980     // XXX: default reply for now, make this a better reply
1981     ysuccess(true);
1982 }
1983
1984 /*
1985  * Implementation of 'rename workspace [<name>] to <name>'
1986  *
1987  */
1988 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1989     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1990         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1991         ysuccess(false);
1992         return;
1993     }
1994     if (old_name) {
1995         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1996     } else {
1997         LOG("Renaming current workspace to \"%s\"\n", new_name);
1998     }
1999
2000     Con *output, *workspace = NULL;
2001     if (old_name) {
2002         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2003         GREP_FIRST(workspace, output_get_content(output),
2004                    !strcasecmp(child->name, old_name));
2005     } else {
2006         workspace = con_get_workspace(focused);
2007     }
2008
2009     if (!workspace) {
2010         // TODO: we should include the old workspace name here and use yajl for
2011         // generating the reply.
2012         // TODO: better error message
2013         yerror("Old workspace not found");
2014         return;
2015     }
2016
2017     Con *check_dest = NULL;
2018     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2019     GREP_FIRST(check_dest, output_get_content(output),
2020                !strcasecmp(child->name, new_name));
2021
2022     if (check_dest != NULL) {
2023         // TODO: we should include the new workspace name here and use yajl for
2024         // generating the reply.
2025         // TODO: better error message
2026         yerror("New workspace already exists");
2027         return;
2028     }
2029
2030     /* Change the name and try to parse it as a number. */
2031     FREE(workspace->name);
2032     workspace->name = sstrdup(new_name);
2033
2034     workspace->num = ws_name_to_number(new_name);
2035     LOG("num = %d\n", workspace->num);
2036
2037     /* By re-attaching, the sort order will be correct afterwards. */
2038     Con *previously_focused = focused;
2039     Con *parent = workspace->parent;
2040     con_detach(workspace);
2041     con_attach(workspace, parent, false);
2042     /* Restore the previous focus since con_attach messes with the focus. */
2043     con_focus(previously_focused);
2044
2045     cmd_output->needs_tree_render = true;
2046     ysuccess(true);
2047
2048     ipc_send_workspace_event("rename", workspace, NULL);
2049     ewmh_update_desktop_names();
2050     ewmh_update_desktop_viewport();
2051     ewmh_update_current_desktop();
2052 }
2053
2054 /*
2055  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2056  *
2057  */
2058 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2059     int mode = M_DOCK;
2060     bool toggle = false;
2061     if (strcmp(bar_mode, "dock") == 0)
2062         mode = M_DOCK;
2063     else if (strcmp(bar_mode, "hide") == 0)
2064         mode = M_HIDE;
2065     else if (strcmp(bar_mode, "invisible") == 0)
2066         mode = M_INVISIBLE;
2067     else if (strcmp(bar_mode, "toggle") == 0)
2068         toggle = true;
2069     else {
2070         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2071         return false;
2072     }
2073
2074     bool changed_sth = false;
2075     Barconfig *current = NULL;
2076     TAILQ_FOREACH(current, &barconfigs, configs) {
2077         if (bar_id && strcmp(current->id, bar_id) != 0)
2078             continue;
2079
2080         if (toggle)
2081             mode = (current->mode + 1) % 2;
2082
2083         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2084         current->mode = mode;
2085         changed_sth = true;
2086
2087         if (bar_id)
2088             break;
2089     }
2090
2091     if (bar_id && !changed_sth) {
2092         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2093         return false;
2094     }
2095
2096     return true;
2097 }
2098
2099 /*
2100  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2101  *
2102  */
2103 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2104     int hidden_state = S_SHOW;
2105     bool toggle = false;
2106     if (strcmp(bar_hidden_state, "hide") == 0)
2107         hidden_state = S_HIDE;
2108     else if (strcmp(bar_hidden_state, "show") == 0)
2109         hidden_state = S_SHOW;
2110     else if (strcmp(bar_hidden_state, "toggle") == 0)
2111         toggle = true;
2112     else {
2113         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2114         return false;
2115     }
2116
2117     bool changed_sth = false;
2118     Barconfig *current = NULL;
2119     TAILQ_FOREACH(current, &barconfigs, configs) {
2120         if (bar_id && strcmp(current->id, bar_id) != 0)
2121             continue;
2122
2123         if (toggle)
2124             hidden_state = (current->hidden_state + 1) % 2;
2125
2126         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2127         current->hidden_state = hidden_state;
2128         changed_sth = true;
2129
2130         if (bar_id)
2131             break;
2132     }
2133
2134     if (bar_id && !changed_sth) {
2135         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2136         return false;
2137     }
2138
2139     return true;
2140 }
2141
2142 /*
2143  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2144  *
2145  */
2146 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2147     bool ret;
2148     if (strcmp(bar_type, "mode") == 0)
2149         ret = cmd_bar_mode(bar_value, bar_id);
2150     else if (strcmp(bar_type, "hidden_state") == 0)
2151         ret = cmd_bar_hidden_state(bar_value, bar_id);
2152     else {
2153         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2154         ret = false;
2155     }
2156
2157     ysuccess(ret);
2158     if (!ret)
2159         return;
2160
2161     update_barconfig();
2162 }
2163
2164 /*
2165  * Implementation of 'shmlog <size>|toggle|on|off'
2166  *
2167  */
2168 void cmd_shmlog(I3_CMD, char *argument) {
2169     if (!strcmp(argument, "toggle"))
2170         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2171         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2172     else if (!strcmp(argument, "on"))
2173         shmlog_size = default_shmlog_size;
2174     else if (!strcmp(argument, "off"))
2175         shmlog_size = 0;
2176     else {
2177         /* If shm logging now, restart logging with the new size. */
2178         if (shmlog_size > 0) {
2179             shmlog_size = 0;
2180             LOG("Restarting shm logging...\n");
2181             init_logging();
2182         }
2183         shmlog_size = atoi(argument);
2184         /* Make a weakly attempt at ensuring the argument is valid. */
2185         if (shmlog_size <= 0)
2186             shmlog_size = default_shmlog_size;
2187     }
2188     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2189     init_logging();
2190     update_shmlog_atom();
2191     // XXX: default reply for now, make this a better reply
2192     ysuccess(true);
2193 }
2194
2195 /*
2196  * Implementation of 'debuglog toggle|on|off'
2197  *
2198  */
2199 void cmd_debuglog(I3_CMD, char *argument) {
2200     bool logging = get_debug_logging();
2201     if (!strcmp(argument, "toggle")) {
2202         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2203         set_debug_logging(!logging);
2204     } else if (!strcmp(argument, "on") && !logging) {
2205         LOG("Enabling debug logging\n");
2206         set_debug_logging(true);
2207     } else if (!strcmp(argument, "off") && logging) {
2208         LOG("Disabling debug logging\n");
2209         set_debug_logging(false);
2210     }
2211     // XXX: default reply for now, make this a better reply
2212     ysuccess(true);
2213 }