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