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