]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Merge branch 'master' into next
[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 container. 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|current'.
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     /* We have nothing to move:
363      *  when criteria was specified but didn't match any window or
364      *  when criteria wasn't specified and we don't have any window focused. */
365     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
366         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
367         ysuccess(false);
368         return;
369     }
370
371     HANDLE_EMPTY_MATCH;
372
373     /* get the workspace */
374     Con *ws;
375     if (strcmp(which, "next") == 0)
376         ws = workspace_next();
377     else if (strcmp(which, "prev") == 0)
378         ws = workspace_prev();
379     else if (strcmp(which, "next_on_output") == 0)
380         ws = workspace_next_on_output();
381     else if (strcmp(which, "prev_on_output") == 0)
382         ws = workspace_prev_on_output();
383     else if (strcmp(which, "current") == 0)
384         ws = con_get_workspace(focused);
385     else {
386         ELOG("BUG: called with which=%s\n", which);
387         ysuccess(false);
388         return;
389     }
390
391     TAILQ_FOREACH(current, &owindows, owindows) {
392         DLOG("matching: %p / %s\n", current->con, current->con->name);
393         con_move_to_workspace(current->con, ws, true, false);
394     }
395
396     cmd_output->needs_tree_render = true;
397     // XXX: default reply for now, make this a better reply
398     ysuccess(true);
399 }
400
401 /*
402  * Implementation of 'move [window|container] [to] workspace <name>'.
403  *
404  */
405 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
406     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
407         LOG("You cannot switch to the i3 internal workspaces.\n");
408         ysuccess(false);
409         return;
410     }
411
412     owindow *current;
413
414     /* We have nothing to move:
415      *  when criteria was specified but didn't match any window or
416      *  when criteria wasn't specified and we don't have any window focused. */
417     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
418         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
419         ysuccess(false);
420         return;
421     }
422
423     LOG("should move window to workspace %s\n", name);
424     /* get the workspace */
425     Con *ws = workspace_get(name, NULL);
426
427     HANDLE_EMPTY_MATCH;
428
429     TAILQ_FOREACH(current, &owindows, owindows) {
430         DLOG("matching: %p / %s\n", current->con, current->con->name);
431         con_move_to_workspace(current->con, ws, true, false);
432     }
433
434     cmd_output->needs_tree_render = true;
435     // XXX: default reply for now, make this a better reply
436     ysuccess(true);
437 }
438
439 /*
440  * Implementation of 'move [window|container] [to] workspace number <number>'.
441  *
442  */
443 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
444     owindow *current;
445
446     /* We have nothing to move:
447      *  when criteria was specified but didn't match any window or
448      *  when criteria wasn't specified and we don't have any window focused. */
449     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
450         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
451         ysuccess(false);
452         return;
453     }
454
455     LOG("should move window to workspace with number %d\n", which);
456     /* get the workspace */
457     Con *output, *workspace = NULL;
458
459     char *endptr = NULL;
460     long parsed_num = strtol(which, &endptr, 10);
461     if (parsed_num == LONG_MIN ||
462         parsed_num == LONG_MAX ||
463         parsed_num < 0 ||
464         *endptr != '\0') {
465         LOG("Could not parse \"%s\" as a number.\n", which);
466         y(map_open);
467         ystr("success");
468         y(bool, false);
469         ystr("error");
470         // TODO: better error message
471         ystr("Could not parse number");
472         y(map_close);
473         return;
474     }
475
476     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
477         GREP_FIRST(workspace, output_get_content(output),
478             child->num == parsed_num);
479
480     if (!workspace) {
481         y(map_open);
482         ystr("success");
483         y(bool, false);
484         ystr("error");
485         // TODO: better error message
486         ystr("No such workspace");
487         y(map_close);
488         return;
489     }
490
491     HANDLE_EMPTY_MATCH;
492
493     TAILQ_FOREACH(current, &owindows, owindows) {
494         DLOG("matching: %p / %s\n", current->con, current->con->name);
495         con_move_to_workspace(current->con, workspace, true, false);
496     }
497
498     cmd_output->needs_tree_render = true;
499     // XXX: default reply for now, make this a better reply
500     ysuccess(true);
501 }
502
503 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
504     LOG("floating resize\n");
505     if (strcmp(direction, "up") == 0) {
506         floating_con->rect.y -= px;
507         floating_con->rect.height += px;
508     } else if (strcmp(direction, "down") == 0) {
509         floating_con->rect.height += px;
510     } else if (strcmp(direction, "left") == 0) {
511         floating_con->rect.x -= px;
512         floating_con->rect.width += px;
513     } else {
514         floating_con->rect.width += px;
515     }
516 }
517
518 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
519     LOG("tiling resize\n");
520     /* get the appropriate current container (skip stacked/tabbed cons) */
521     Con *current = focused;
522     while (current->parent->layout == L_STACKED ||
523            current->parent->layout == L_TABBED)
524         current = current->parent;
525
526     /* Then further go up until we find one with the matching orientation. */
527     orientation_t search_orientation =
528         (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
529
530     while (current->type != CT_WORKSPACE &&
531            current->type != CT_FLOATING_CON &&
532            current->parent->orientation != search_orientation)
533         current = current->parent;
534
535     /* get the default percentage */
536     int children = con_num_children(current->parent);
537     Con *other;
538     LOG("ins. %d children\n", children);
539     double percentage = 1.0 / children;
540     LOG("default percentage = %f\n", percentage);
541
542     orientation_t orientation = current->parent->orientation;
543
544     if ((orientation == HORIZ &&
545          (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
546         (orientation == VERT &&
547          (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
548         LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
549             (orientation == HORIZ ? "horizontal" : "vertical"));
550         ysuccess(false);
551         return;
552     }
553
554     if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
555         other = TAILQ_PREV(current, nodes_head, nodes);
556     } else {
557         other = TAILQ_NEXT(current, nodes);
558     }
559     if (other == TAILQ_END(workspaces)) {
560         LOG("No other container in this direction found, cannot resize.\n");
561         ysuccess(false);
562         return;
563     }
564     LOG("other->percent = %f\n", other->percent);
565     LOG("current->percent before = %f\n", current->percent);
566     if (current->percent == 0.0)
567         current->percent = percentage;
568     if (other->percent == 0.0)
569         other->percent = percentage;
570     double new_current_percent = current->percent + ((double)ppt / 100.0);
571     double new_other_percent = other->percent - ((double)ppt / 100.0);
572     LOG("new_current_percent = %f\n", new_current_percent);
573     LOG("new_other_percent = %f\n", new_other_percent);
574     /* Ensure that the new percentages are positive and greater than
575      * 0.05 to have a reasonable minimum size. */
576     if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
577         definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
578         current->percent += ((double)ppt / 100.0);
579         other->percent -= ((double)ppt / 100.0);
580         LOG("current->percent after = %f\n", current->percent);
581         LOG("other->percent after = %f\n", other->percent);
582     } else {
583         LOG("Not resizing, already at minimum size\n");
584     }
585 }
586
587 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
588     LOG("width/height resize\n");
589     /* get the appropriate current container (skip stacked/tabbed cons) */
590     Con *current = focused;
591     while (current->parent->layout == L_STACKED ||
592            current->parent->layout == L_TABBED)
593         current = current->parent;
594
595     /* Then further go up until we find one with the matching orientation. */
596     orientation_t search_orientation =
597         (strcmp(direction, "width") == 0 ? HORIZ : VERT);
598
599     while (current->type != CT_WORKSPACE &&
600            current->type != CT_FLOATING_CON &&
601            current->parent->orientation != search_orientation)
602         current = current->parent;
603
604     /* get the default percentage */
605     int children = con_num_children(current->parent);
606     LOG("ins. %d children\n", children);
607     double percentage = 1.0 / children;
608     LOG("default percentage = %f\n", percentage);
609
610     orientation_t orientation = current->parent->orientation;
611
612     if ((orientation == HORIZ &&
613          strcmp(direction, "height") == 0) ||
614         (orientation == VERT &&
615          strcmp(direction, "width") == 0)) {
616         LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
617             (orientation == HORIZ ? "horizontal" : "vertical"));
618         ysuccess(false);
619         return;
620     }
621
622     if (children == 1) {
623         LOG("This is the only container, cannot resize.\n");
624         ysuccess(false);
625         return;
626     }
627
628     /* Ensure all the other children have a percentage set. */
629     Con *child;
630     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
631         LOG("child->percent = %f (child %p)\n", child->percent, child);
632         if (child->percent == 0.0)
633             child->percent = percentage;
634     }
635
636     double new_current_percent = current->percent + ((double)ppt / 100.0);
637     double subtract_percent = ((double)ppt / 100.0) / (children - 1);
638     LOG("new_current_percent = %f\n", new_current_percent);
639     LOG("subtract_percent = %f\n", subtract_percent);
640     /* Ensure that the new percentages are positive and greater than
641      * 0.05 to have a reasonable minimum size. */
642     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
643         if (child == current)
644             continue;
645         if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
646             LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
647             ysuccess(false);
648             return;
649         }
650     }
651     if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
652         LOG("Not resizing, already at minimum size\n");
653         ysuccess(false);
654         return;
655     }
656
657     current->percent += ((double)ppt / 100.0);
658     LOG("current->percent after = %f\n", current->percent);
659
660     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
661         if (child == current)
662             continue;
663         child->percent -= subtract_percent;
664         LOG("child->percent after (%p) = %f\n", child, child->percent);
665     }
666 }
667
668 /*
669  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
670  *
671  */
672 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
673     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
674     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
675     // 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
676     int px = atoi(resize_px);
677     int ppt = atoi(resize_ppt);
678     if (strcmp(way, "shrink") == 0) {
679         px *= -1;
680         ppt *= -1;
681     }
682
683     Con *floating_con;
684     if ((floating_con = con_inside_floating(focused))) {
685         cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
686     } else {
687         if (strcmp(direction, "width") == 0 ||
688             strcmp(direction, "height") == 0)
689             cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
690         else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
691     }
692
693     cmd_output->needs_tree_render = true;
694     // XXX: default reply for now, make this a better reply
695     ysuccess(true);
696 }
697
698 /*
699  * Implementation of 'border normal|none|1pixel|toggle'.
700  *
701  */
702 void cmd_border(I3_CMD, char *border_style_str) {
703     DLOG("border style should be changed to %s\n", border_style_str);
704     owindow *current;
705
706     HANDLE_EMPTY_MATCH;
707
708     TAILQ_FOREACH(current, &owindows, owindows) {
709         DLOG("matching: %p / %s\n", current->con, current->con->name);
710         int border_style = current->con->border_style;
711         if (strcmp(border_style_str, "toggle") == 0) {
712             border_style++;
713             border_style %= 3;
714         } else {
715             if (strcmp(border_style_str, "normal") == 0)
716                 border_style = BS_NORMAL;
717             else if (strcmp(border_style_str, "none") == 0)
718                 border_style = BS_NONE;
719             else if (strcmp(border_style_str, "1pixel") == 0)
720                 border_style = BS_1PIXEL;
721             else {
722                 ELOG("BUG: called with border_style=%s\n", border_style_str);
723                 ysuccess(false);
724                 return;
725             }
726         }
727         con_set_border_style(current->con, border_style);
728     }
729
730     cmd_output->needs_tree_render = true;
731     // XXX: default reply for now, make this a better reply
732     ysuccess(true);
733 }
734
735 /*
736  * Implementation of 'nop <comment>'.
737  *
738  */
739 void cmd_nop(I3_CMD, char *comment) {
740     LOG("-------------------------------------------------\n");
741     LOG("  NOP: %s\n", comment);
742     LOG("-------------------------------------------------\n");
743 }
744
745 /*
746  * Implementation of 'append_layout <path>'.
747  *
748  */
749 void cmd_append_layout(I3_CMD, char *path) {
750     LOG("Appending layout \"%s\"\n", path);
751     tree_append_json(path);
752
753     cmd_output->needs_tree_render = true;
754     // XXX: default reply for now, make this a better reply
755     ysuccess(true);
756 }
757
758 /*
759  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
760  *
761  */
762 void cmd_workspace(I3_CMD, char *which) {
763     Con *ws;
764
765     DLOG("which=%s\n", which);
766
767     if (strcmp(which, "next") == 0)
768         ws = workspace_next();
769     else if (strcmp(which, "prev") == 0)
770         ws = workspace_prev();
771     else if (strcmp(which, "next_on_output") == 0)
772         ws = workspace_next_on_output();
773     else if (strcmp(which, "prev_on_output") == 0)
774         ws = workspace_prev_on_output();
775     else {
776         ELOG("BUG: called with which=%s\n", which);
777         ysuccess(false);
778         return;
779     }
780
781     workspace_show(ws);
782
783     cmd_output->needs_tree_render = true;
784     // XXX: default reply for now, make this a better reply
785     ysuccess(true);
786 }
787
788 /*
789  * Implementation of 'workspace number <number>'
790  *
791  */
792 void cmd_workspace_number(I3_CMD, char *which) {
793     Con *output, *workspace = NULL;
794
795     char *endptr = NULL;
796     long parsed_num = strtol(which, &endptr, 10);
797     if (parsed_num == LONG_MIN ||
798         parsed_num == LONG_MAX ||
799         parsed_num < 0 ||
800         *endptr != '\0') {
801         LOG("Could not parse \"%s\" as a number.\n", which);
802         y(map_open);
803         ystr("success");
804         y(bool, false);
805         ystr("error");
806         // TODO: better error message
807         ystr("Could not parse number");
808         y(map_close);
809
810         return;
811     }
812
813     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
814         GREP_FIRST(workspace, output_get_content(output),
815             child->num == parsed_num);
816
817     if (!workspace) {
818         LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
819         ysuccess(true);
820         /* terminate the which string after the endposition of the number */
821         *endptr = '\0';
822         if (maybe_back_and_forth(cmd_output, which))
823             return;
824         workspace_show_by_name(which);
825         cmd_output->needs_tree_render = true;
826         return;
827     }
828     if (maybe_back_and_forth(cmd_output, which))
829         return;
830     workspace_show(workspace);
831
832     cmd_output->needs_tree_render = true;
833     // XXX: default reply for now, make this a better reply
834     ysuccess(true);
835 }
836
837 /*
838  * Implementation of 'workspace back_and_forth'.
839  *
840  */
841 void cmd_workspace_back_and_forth(I3_CMD) {
842     workspace_back_and_forth();
843
844     cmd_output->needs_tree_render = true;
845     // XXX: default reply for now, make this a better reply
846     ysuccess(true);
847 }
848
849 /*
850  * Implementation of 'workspace <name>'
851  *
852  */
853 void cmd_workspace_name(I3_CMD, char *name) {
854     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
855         LOG("You cannot switch to the i3 internal workspaces.\n");
856         ysuccess(false);
857         return;
858     }
859
860     DLOG("should switch to workspace %s\n", name);
861     if (maybe_back_and_forth(cmd_output, name))
862        return;
863     workspace_show_by_name(name);
864
865     cmd_output->needs_tree_render = true;
866     // XXX: default reply for now, make this a better reply
867     ysuccess(true);
868 }
869
870 /*
871  * Implementation of 'mark <mark>'
872  *
873  */
874 void cmd_mark(I3_CMD, char *mark) {
875     DLOG("Clearing all windows which have that mark first\n");
876
877     Con *con;
878     TAILQ_FOREACH(con, &all_cons, all_cons) {
879         if (con->mark && strcmp(con->mark, mark) == 0)
880             FREE(con->mark);
881     }
882
883     DLOG("marking window with str %s\n", mark);
884     owindow *current;
885
886     HANDLE_EMPTY_MATCH;
887
888     TAILQ_FOREACH(current, &owindows, owindows) {
889         DLOG("matching: %p / %s\n", current->con, current->con->name);
890         current->con->mark = sstrdup(mark);
891     }
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 'mode <string>'.
900  *
901  */
902 void cmd_mode(I3_CMD, char *mode) {
903     DLOG("mode=%s\n", mode);
904     switch_mode(mode);
905
906     // XXX: default reply for now, make this a better reply
907     ysuccess(true);
908 }
909
910 /*
911  * Implementation of 'move [window|container] [to] output <str>'.
912  *
913  */
914 void cmd_move_con_to_output(I3_CMD, char *name) {
915     owindow *current;
916
917     DLOG("should move window to output %s\n", name);
918
919     HANDLE_EMPTY_MATCH;
920
921     /* get the output */
922     Output *current_output = NULL;
923     Output *output;
924
925     // TODO: fix the handling of criteria
926     TAILQ_FOREACH(current, &owindows, owindows)
927         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
928
929     assert(current_output != NULL);
930
931     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
932     if (strcasecmp(name, "up") == 0)
933         output = get_output_next(D_UP, current_output);
934     else if (strcasecmp(name, "down") == 0)
935         output = get_output_next(D_DOWN, current_output);
936     else if (strcasecmp(name, "left") == 0)
937         output = get_output_next(D_LEFT, current_output);
938     else if (strcasecmp(name, "right") == 0)
939         output = get_output_next(D_RIGHT, current_output);
940     else
941         output = get_output_by_name(name);
942
943     if (!output) {
944         LOG("No such output found.\n");
945         ysuccess(false);
946         return;
947     }
948
949     /* get visible workspace on output */
950     Con *ws = NULL;
951     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
952     if (!ws) {
953         ysuccess(false);
954         return;
955     }
956
957     TAILQ_FOREACH(current, &owindows, owindows) {
958         DLOG("matching: %p / %s\n", current->con, current->con->name);
959         con_move_to_workspace(current->con, ws, true, false);
960     }
961
962     cmd_output->needs_tree_render = true;
963     // XXX: default reply for now, make this a better reply
964     ysuccess(true);
965 }
966
967 /*
968  * Implementation of 'floating enable|disable|toggle'
969  *
970  */
971 void cmd_floating(I3_CMD, char *floating_mode) {
972     owindow *current;
973
974     DLOG("floating_mode=%s\n", floating_mode);
975
976     HANDLE_EMPTY_MATCH;
977
978     TAILQ_FOREACH(current, &owindows, owindows) {
979         DLOG("matching: %p / %s\n", current->con, current->con->name);
980         if (strcmp(floating_mode, "toggle") == 0) {
981             DLOG("should toggle mode\n");
982             toggle_floating_mode(current->con, false);
983         } else {
984             DLOG("should switch mode to %s\n", floating_mode);
985             if (strcmp(floating_mode, "enable") == 0) {
986                 floating_enable(current->con, false);
987             } else {
988                 floating_disable(current->con, false);
989             }
990         }
991     }
992
993     cmd_output->needs_tree_render = true;
994     // XXX: default reply for now, make this a better reply
995     ysuccess(true);
996 }
997
998 /*
999  * Implementation of 'move workspace to [output] <str>'.
1000  *
1001  */
1002 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1003     DLOG("should move workspace to output %s\n", name);
1004
1005     HANDLE_EMPTY_MATCH;
1006
1007     owindow *current;
1008     TAILQ_FOREACH(current, &owindows, owindows) {
1009         Output *current_output = get_output_containing(current->con->rect.x,
1010                                                        current->con->rect.y);
1011         Output *output = get_output_from_string(current_output, name);
1012         if (!output) {
1013             LOG("No such output\n");
1014             ysuccess(false);
1015             return;
1016         }
1017
1018         Con *content = output_get_content(output->con);
1019         LOG("got output %p with content %p\n", output, content);
1020
1021         Con *ws = con_get_workspace(current->con);
1022         LOG("should move workspace %p / %s\n", ws, ws->name);
1023
1024         if (con_num_children(ws->parent) == 1) {
1025             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1026
1027             /* check if we can find a workspace assigned to this output */
1028             bool used_assignment = false;
1029             struct Workspace_Assignment *assignment;
1030             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1031                 if (strcmp(assignment->output, current_output->name) != 0)
1032                     continue;
1033
1034                 /* check if this workspace is already attached to the tree */
1035                 Con *workspace = NULL, *out;
1036                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1037                     GREP_FIRST(workspace, output_get_content(out),
1038                                !strcasecmp(child->name, assignment->name));
1039                 if (workspace != NULL)
1040                     continue;
1041
1042                 /* so create the workspace referenced to by this assignment */
1043                 LOG("Creating workspace from assignment %s.\n", assignment->name);
1044                 workspace_get(assignment->name, NULL);
1045                 used_assignment = true;
1046                 break;
1047             }
1048
1049             /* if we couldn't create the workspace using an assignment, create
1050              * it on the output */
1051             if (!used_assignment)
1052                 create_workspace_on_output(current_output, ws->parent);
1053
1054             /* notify the IPC listeners */
1055             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1056         }
1057
1058         /* detach from the old output and attach to the new output */
1059         bool workspace_was_visible = workspace_is_visible(ws);
1060         Con *old_content = ws->parent;
1061         con_detach(ws);
1062         if (workspace_was_visible) {
1063             /* The workspace which we just detached was visible, so focus
1064              * the next one in the focus-stack. */
1065             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1066             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1067             workspace_show(focus_ws);
1068         }
1069         con_attach(ws, content, false);
1070
1071         /* fix the coordinates of the floating containers */
1072         Con *floating_con;
1073         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1074             floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1075
1076         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1077         if (workspace_was_visible) {
1078             /* Focus the moved workspace on the destination output. */
1079             workspace_show(ws);
1080         }
1081     }
1082
1083     cmd_output->needs_tree_render = true;
1084     // XXX: default reply for now, make this a better reply
1085     ysuccess(true);
1086 }
1087
1088 /*
1089  * Implementation of 'split v|h|vertical|horizontal'.
1090  *
1091  */
1092 void cmd_split(I3_CMD, char *direction) {
1093     /* TODO: use matches */
1094     LOG("splitting in direction %c\n", direction[0]);
1095     tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1096
1097     cmd_output->needs_tree_render = true;
1098     // XXX: default reply for now, make this a better reply
1099     ysuccess(true);
1100 }
1101
1102 /*
1103  * Implementaiton of 'kill [window|client]'.
1104  *
1105  */
1106 void cmd_kill(I3_CMD, char *kill_mode_str) {
1107     if (kill_mode_str == NULL)
1108         kill_mode_str = "window";
1109     owindow *current;
1110
1111     DLOG("kill_mode=%s\n", kill_mode_str);
1112
1113     int kill_mode;
1114     if (strcmp(kill_mode_str, "window") == 0)
1115         kill_mode = KILL_WINDOW;
1116     else if (strcmp(kill_mode_str, "client") == 0)
1117         kill_mode = KILL_CLIENT;
1118     else {
1119         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1120         ysuccess(false);
1121         return;
1122     }
1123
1124     /* check if the match is empty, not if the result is empty */
1125     if (match_is_empty(current_match))
1126         tree_close_con(kill_mode);
1127     else {
1128         TAILQ_FOREACH(current, &owindows, owindows) {
1129             DLOG("matching: %p / %s\n", current->con, current->con->name);
1130             tree_close(current->con, kill_mode, false, false);
1131         }
1132     }
1133
1134     cmd_output->needs_tree_render = true;
1135     // XXX: default reply for now, make this a better reply
1136     ysuccess(true);
1137 }
1138
1139 /*
1140  * Implementation of 'exec [--no-startup-id] <command>'.
1141  *
1142  */
1143 void cmd_exec(I3_CMD, char *nosn, char *command) {
1144     bool no_startup_id = (nosn != NULL);
1145
1146     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1147     start_application(command, no_startup_id);
1148
1149     // XXX: default reply for now, make this a better reply
1150     ysuccess(true);
1151 }
1152
1153 /*
1154  * Implementation of 'focus left|right|up|down'.
1155  *
1156  */
1157 void cmd_focus_direction(I3_CMD, char *direction) {
1158     if (focused &&
1159         focused->type != CT_WORKSPACE &&
1160         focused->fullscreen_mode != CF_NONE) {
1161         LOG("Cannot change focus while in fullscreen mode.\n");
1162         ysuccess(false);
1163         return;
1164     }
1165
1166     DLOG("direction = *%s*\n", direction);
1167
1168     if (strcmp(direction, "left") == 0)
1169         tree_next('p', HORIZ);
1170     else if (strcmp(direction, "right") == 0)
1171         tree_next('n', HORIZ);
1172     else if (strcmp(direction, "up") == 0)
1173         tree_next('p', VERT);
1174     else if (strcmp(direction, "down") == 0)
1175         tree_next('n', VERT);
1176     else {
1177         ELOG("Invalid focus direction (%s)\n", direction);
1178         ysuccess(false);
1179         return;
1180     }
1181
1182     cmd_output->needs_tree_render = true;
1183     // XXX: default reply for now, make this a better reply
1184     ysuccess(true);
1185 }
1186
1187 /*
1188  * Implementation of 'focus tiling|floating|mode_toggle'.
1189  *
1190  */
1191 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1192     if (focused &&
1193         focused->type != CT_WORKSPACE &&
1194         focused->fullscreen_mode != CF_NONE) {
1195         LOG("Cannot change focus while in fullscreen mode.\n");
1196         ysuccess(false);
1197         return;
1198     }
1199
1200     DLOG("window_mode = %s\n", window_mode);
1201
1202     Con *ws = con_get_workspace(focused);
1203     Con *current;
1204     if (ws != NULL) {
1205         if (strcmp(window_mode, "mode_toggle") == 0) {
1206             current = TAILQ_FIRST(&(ws->focus_head));
1207             if (current != NULL && current->type == CT_FLOATING_CON)
1208                 window_mode = "tiling";
1209             else window_mode = "floating";
1210         }
1211         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1212             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1213                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1214                 continue;
1215
1216             con_focus(con_descend_focused(current));
1217             break;
1218         }
1219     }
1220
1221     cmd_output->needs_tree_render = true;
1222     // XXX: default reply for now, make this a better reply
1223     ysuccess(true);
1224 }
1225
1226 /*
1227  * Implementation of 'focus parent|child'.
1228  *
1229  */
1230 void cmd_focus_level(I3_CMD, char *level) {
1231     DLOG("level = %s\n", level);
1232     bool success = false;
1233
1234     /* Focusing the parent can only be allowed if the newly
1235      * focused container won't escape the fullscreen container. */
1236     if (strcmp(level, "parent") == 0) {
1237         if (focused && focused->parent) {
1238             if (con_fullscreen_permits_focusing(focused->parent))
1239                 success = level_up();
1240             else
1241                 LOG("Currently in fullscreen, not going up\n");
1242         }
1243     }
1244
1245     /* Focusing a child should always be allowed. */
1246     else success = level_down();
1247
1248     cmd_output->needs_tree_render = success;
1249     // XXX: default reply for now, make this a better reply
1250     ysuccess(success);
1251 }
1252
1253 /*
1254  * Implementation of 'focus'.
1255  *
1256  */
1257 void cmd_focus(I3_CMD) {
1258     DLOG("current_match = %p\n", current_match);
1259
1260     if (match_is_empty(current_match)) {
1261         ELOG("You have to specify which window/container should be focused.\n");
1262         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1263
1264         y(map_open);
1265         ystr("success");
1266         y(bool, false);
1267         ystr("error");
1268         ystr("You have to specify which window/container should be focused");
1269         y(map_close);
1270
1271         return;
1272     }
1273
1274     int count = 0;
1275     owindow *current;
1276     TAILQ_FOREACH(current, &owindows, owindows) {
1277         Con *ws = con_get_workspace(current->con);
1278         /* If no workspace could be found, this was a dock window.
1279          * Just skip it, you cannot focus dock windows. */
1280         if (!ws)
1281             continue;
1282
1283         /* Check the fullscreen focus constraints. */
1284         if (!con_fullscreen_permits_focusing(current->con)) {
1285             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1286             ysuccess(false);
1287             return;
1288         }
1289
1290         /* If the container is not on the current workspace,
1291          * workspace_show() will switch to a different workspace and (if
1292          * enabled) trigger a mouse pointer warp to the currently focused
1293          * container (!) on the target workspace.
1294          *
1295          * Therefore, before calling workspace_show(), we make sure that
1296          * 'current' will be focused on the workspace. However, we cannot
1297          * just con_focus(current) because then the pointer will not be
1298          * warped at all (the code thinks we are already there).
1299          *
1300          * So we focus 'current' to make it the currently focused window of
1301          * the target workspace, then revert focus. */
1302         Con *currently_focused = focused;
1303         con_focus(current->con);
1304         con_focus(currently_focused);
1305
1306         /* Now switch to the workspace, then focus */
1307         workspace_show(ws);
1308         LOG("focusing %p / %s\n", current->con, current->con->name);
1309         con_focus(current->con);
1310         count++;
1311     }
1312
1313     if (count > 1)
1314         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1315             "while only exactly one container can be focused at a time.\n", count);
1316
1317     cmd_output->needs_tree_render = true;
1318     // XXX: default reply for now, make this a better reply
1319     ysuccess(true);
1320 }
1321
1322 /*
1323  * Implementation of 'fullscreen [global]'.
1324  *
1325  */
1326 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1327     if (fullscreen_mode == NULL)
1328         fullscreen_mode = "output";
1329     DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1330     owindow *current;
1331
1332     HANDLE_EMPTY_MATCH;
1333
1334     TAILQ_FOREACH(current, &owindows, owindows) {
1335         printf("matching: %p / %s\n", current->con, current->con->name);
1336         con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1337     }
1338
1339     cmd_output->needs_tree_render = true;
1340     // XXX: default reply for now, make this a better reply
1341     ysuccess(true);
1342 }
1343
1344 /*
1345  * Implementation of 'move <direction> [<pixels> [px]]'.
1346  *
1347  */
1348 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1349     // 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
1350     int px = atoi(move_px);
1351
1352     /* TODO: make 'move' work with criteria. */
1353     DLOG("moving in direction %s, px %s\n", direction, move_px);
1354     if (con_is_floating(focused)) {
1355         DLOG("floating move with %d pixels\n", px);
1356         Rect newrect = focused->parent->rect;
1357         if (strcmp(direction, "left") == 0) {
1358             newrect.x -= px;
1359         } else if (strcmp(direction, "right") == 0) {
1360             newrect.x += px;
1361         } else if (strcmp(direction, "up") == 0) {
1362             newrect.y -= px;
1363         } else if (strcmp(direction, "down") == 0) {
1364             newrect.y += px;
1365         }
1366         floating_reposition(focused->parent, newrect);
1367     } else {
1368         tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1369                    (strcmp(direction, "left") == 0 ? D_LEFT :
1370                     (strcmp(direction, "up") == 0 ? D_UP :
1371                      D_DOWN))));
1372         cmd_output->needs_tree_render = true;
1373     }
1374
1375     // XXX: default reply for now, make this a better reply
1376     ysuccess(true);
1377 }
1378
1379 /*
1380  * Implementation of 'layout default|stacked|stacking|tabbed'.
1381  *
1382  */
1383 void cmd_layout(I3_CMD, char *layout_str) {
1384     if (strcmp(layout_str, "stacking") == 0)
1385         layout_str = "stacked";
1386     DLOG("changing layout to %s\n", layout_str);
1387     owindow *current;
1388     int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1389                   (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1390                    L_TABBED));
1391
1392     /* check if the match is empty, not if the result is empty */
1393     if (match_is_empty(current_match))
1394         con_set_layout(focused->parent, layout);
1395     else {
1396         TAILQ_FOREACH(current, &owindows, owindows) {
1397             DLOG("matching: %p / %s\n", current->con, current->con->name);
1398             con_set_layout(current->con, layout);
1399         }
1400     }
1401
1402     cmd_output->needs_tree_render = true;
1403     // XXX: default reply for now, make this a better reply
1404     ysuccess(true);
1405 }
1406
1407 /*
1408  * Implementaiton of 'exit'.
1409  *
1410  */
1411 void cmd_exit(I3_CMD) {
1412     LOG("Exiting due to user command.\n");
1413     exit(0);
1414
1415     /* unreached */
1416 }
1417
1418 /*
1419  * Implementaiton of 'reload'.
1420  *
1421  */
1422 void cmd_reload(I3_CMD) {
1423     LOG("reloading\n");
1424     kill_configerror_nagbar(false);
1425     load_configuration(conn, NULL, true);
1426     x_set_i3_atoms();
1427     /* Send an IPC event just in case the ws names have changed */
1428     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1429
1430     // XXX: default reply for now, make this a better reply
1431     ysuccess(true);
1432 }
1433
1434 /*
1435  * Implementaiton of 'restart'.
1436  *
1437  */
1438 void cmd_restart(I3_CMD) {
1439     LOG("restarting i3\n");
1440     i3_restart(false);
1441
1442     // XXX: default reply for now, make this a better reply
1443     ysuccess(true);
1444 }
1445
1446 /*
1447  * Implementaiton of 'open'.
1448  *
1449  */
1450 void cmd_open(I3_CMD) {
1451     LOG("opening new container\n");
1452     Con *con = tree_open_con(NULL, NULL);
1453     con_focus(con);
1454
1455     y(map_open);
1456     ystr("success");
1457     y(bool, true);
1458     ystr("id");
1459     y(integer, (long int)con);
1460     y(map_close);
1461
1462     cmd_output->needs_tree_render = true;
1463 }
1464
1465 /*
1466  * Implementation of 'focus output <output>'.
1467  *
1468  */
1469 void cmd_focus_output(I3_CMD, char *name) {
1470     owindow *current;
1471
1472     DLOG("name = %s\n", name);
1473
1474     HANDLE_EMPTY_MATCH;
1475
1476     /* get the output */
1477     Output *current_output = NULL;
1478     Output *output;
1479
1480     TAILQ_FOREACH(current, &owindows, owindows)
1481         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1482     assert(current_output != NULL);
1483
1484     output = get_output_from_string(current_output, name);
1485
1486     if (!output) {
1487         LOG("No such output found.\n");
1488         ysuccess(false);
1489         return;
1490     }
1491
1492     /* get visible workspace on output */
1493     Con *ws = NULL;
1494     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1495     if (!ws) {
1496         ysuccess(false);
1497         return;
1498     }
1499
1500     workspace_show(ws);
1501
1502     cmd_output->needs_tree_render = true;
1503     // XXX: default reply for now, make this a better reply
1504     ysuccess(true);
1505 }
1506
1507 /*
1508  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1509  *
1510  */
1511 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1512
1513     int x = atoi(cx);
1514     int y = atoi(cy);
1515
1516     if (!con_is_floating(focused)) {
1517         ELOG("Cannot change position. The window/container is not floating\n");
1518         y(map_open);
1519         ystr("success");
1520         y(bool, false);
1521         ystr("error");
1522         ystr("Cannot change position. The window/container is not floating.");
1523         y(map_close);
1524         return;
1525     }
1526
1527     if (strcmp(method, "absolute") == 0) {
1528         focused->parent->rect.x = x;
1529         focused->parent->rect.y = y;
1530
1531         DLOG("moving to absolute position %d %d\n", x, y);
1532         floating_maybe_reassign_ws(focused->parent);
1533         cmd_output->needs_tree_render = true;
1534     }
1535
1536     if (strcmp(method, "position") == 0) {
1537         Rect newrect = focused->parent->rect;
1538
1539         DLOG("moving to position %d %d\n", x, y);
1540         newrect.x = x;
1541         newrect.y = y;
1542
1543         floating_reposition(focused->parent, newrect);
1544     }
1545
1546     // XXX: default reply for now, make this a better reply
1547     ysuccess(true);
1548 }
1549
1550 /*
1551  * Implementation of 'move [window|container] [to] [absolute] position center
1552  *
1553  */
1554 void cmd_move_window_to_center(I3_CMD, char *method) {
1555
1556     if (!con_is_floating(focused)) {
1557         ELOG("Cannot change position. The window/container is not floating\n");
1558         y(map_open);
1559         ystr("success");
1560         y(bool, false);
1561         ystr("error");
1562         ystr("Cannot change position. The window/container is not floating.");
1563         y(map_close);
1564     }
1565
1566     if (strcmp(method, "absolute") == 0) {
1567         Rect *rect = &focused->parent->rect;
1568
1569         DLOG("moving to absolute center\n");
1570         rect->x = croot->rect.width/2 - rect->width/2;
1571         rect->y = croot->rect.height/2 - rect->height/2;
1572
1573         floating_maybe_reassign_ws(focused->parent);
1574         cmd_output->needs_tree_render = true;
1575     }
1576
1577     if (strcmp(method, "position") == 0) {
1578         Rect *wsrect = &con_get_workspace(focused)->rect;
1579         Rect newrect = focused->parent->rect;
1580
1581         DLOG("moving to center\n");
1582         newrect.x = wsrect->width/2 - newrect.width/2;
1583         newrect.y = wsrect->height/2 - newrect.height/2;
1584
1585         floating_reposition(focused->parent, newrect);
1586     }
1587
1588     // XXX: default reply for now, make this a better reply
1589     ysuccess(true);
1590 }
1591
1592 /*
1593  * Implementation of 'move scratchpad'.
1594  *
1595  */
1596 void cmd_move_scratchpad(I3_CMD) {
1597     DLOG("should move window to scratchpad\n");
1598     owindow *current;
1599
1600     HANDLE_EMPTY_MATCH;
1601
1602     TAILQ_FOREACH(current, &owindows, owindows) {
1603         DLOG("matching: %p / %s\n", current->con, current->con->name);
1604         scratchpad_move(current->con);
1605     }
1606
1607     cmd_output->needs_tree_render = true;
1608     // XXX: default reply for now, make this a better reply
1609     ysuccess(true);
1610 }
1611
1612 /*
1613  * Implementation of 'scratchpad show'.
1614  *
1615  */
1616 void cmd_scratchpad_show(I3_CMD) {
1617     DLOG("should show scratchpad window\n");
1618     owindow *current;
1619
1620     if (match_is_empty(current_match)) {
1621         scratchpad_show(NULL);
1622     } else {
1623         TAILQ_FOREACH(current, &owindows, owindows) {
1624             DLOG("matching: %p / %s\n", current->con, current->con->name);
1625             scratchpad_show(current->con);
1626         }
1627     }
1628
1629     cmd_output->needs_tree_render = true;
1630     // XXX: default reply for now, make this a better reply
1631     ysuccess(true);
1632 }
1633
1634 /*
1635  * Implementation of 'rename workspace <name> to <name>'
1636  *
1637  */
1638 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1639     LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1640
1641     Con *output, *workspace = NULL;
1642     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1643         GREP_FIRST(workspace, output_get_content(output),
1644             !strcasecmp(child->name, old_name));
1645
1646     if (!workspace) {
1647         // TODO: we should include the old workspace name here and use yajl for
1648         // generating the reply.
1649         y(map_open);
1650         ystr("success");
1651         y(bool, false);
1652         ystr("error");
1653         // TODO: better error message
1654         ystr("Old workspace not found");
1655         y(map_close);
1656         return;
1657     }
1658
1659     Con *check_dest = NULL;
1660     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1661         GREP_FIRST(check_dest, output_get_content(output),
1662             !strcasecmp(child->name, new_name));
1663
1664     if (check_dest != NULL) {
1665         // TODO: we should include the new workspace name here and use yajl for
1666         // generating the reply.
1667         y(map_open);
1668         ystr("success");
1669         y(bool, false);
1670         ystr("error");
1671         // TODO: better error message
1672         ystr("New workspace already exists");
1673         y(map_close);
1674         return;
1675     }
1676
1677     /* Change the name and try to parse it as a number. */
1678     FREE(workspace->name);
1679     workspace->name = sstrdup(new_name);
1680     char *endptr = NULL;
1681     long parsed_num = strtol(new_name, &endptr, 10);
1682     if (parsed_num == LONG_MIN ||
1683         parsed_num == LONG_MAX ||
1684         parsed_num < 0 ||
1685         endptr == new_name)
1686         workspace->num = -1;
1687     else workspace->num = parsed_num;
1688     LOG("num = %d\n", workspace->num);
1689
1690     /* By re-attaching, the sort order will be correct afterwards. */
1691     Con *previously_focused = focused;
1692     Con *parent = workspace->parent;
1693     con_detach(workspace);
1694     con_attach(workspace, parent, false);
1695     /* Restore the previous focus since con_attach messes with the focus. */
1696     con_focus(previously_focused);
1697
1698     cmd_output->needs_tree_render = true;
1699     ysuccess(true);
1700
1701     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1702 }