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