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