]> git.sur5r.net Git - i3/i3/blob - src/commands.c
add comments to src/commands.c
[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() {
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 char *cmd_criteria_init(Match *current_match) {
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     /* This command is internal and does not generate a JSON reply. */
193     return NULL;
194 }
195
196 /*
197  * A match specification just finished (the closing square bracket was found),
198  * so we filter the list of owindows.
199  *
200  */
201 char *cmd_criteria_match_windows(Match *current_match) {
202     owindow *next, *current;
203
204     DLOG("match specification finished, matching...\n");
205     /* copy the old list head to iterate through it and start with a fresh
206      * list which will contain only matching windows */
207     struct owindows_head old = owindows;
208     TAILQ_INIT(&owindows);
209     for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
210         /* make a copy of the next pointer and advance the pointer to the
211          * next element as we are going to invalidate the element’s
212          * next/prev pointers by calling TAILQ_INSERT_TAIL later */
213         current = next;
214         next = TAILQ_NEXT(next, owindows);
215
216         DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
217         if (current_match->con_id != NULL) {
218             if (current_match->con_id == current->con) {
219                 DLOG("matches container!\n");
220                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
221             }
222         } else if (current_match->mark != NULL && current->con->mark != NULL &&
223                    regex_matches(current_match->mark, current->con->mark)) {
224             DLOG("match by mark\n");
225             TAILQ_INSERT_TAIL(&owindows, current, owindows);
226         } else {
227             if (current->con->window == NULL)
228                 continue;
229             if (match_matches_window(current_match, current->con->window)) {
230                 DLOG("matches window!\n");
231                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
232             } else {
233                 DLOG("doesnt match\n");
234                 free(current);
235             }
236         }
237     }
238
239     TAILQ_FOREACH(current, &owindows, owindows) {
240         DLOG("matching: %p / %s\n", current->con, current->con->name);
241     }
242
243     /* This command is internal and does not generate a JSON reply. */
244     return NULL;
245 }
246
247 /*
248  * Interprets a ctype=cvalue pair and adds it to the current match
249  * specification.
250  *
251  */
252 char *cmd_criteria_add(Match *current_match, char *ctype, char *cvalue) {
253     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
254
255     if (strcmp(ctype, "class") == 0) {
256         current_match->class = regex_new(cvalue);
257         return NULL;
258     }
259
260     if (strcmp(ctype, "instance") == 0) {
261         current_match->instance = regex_new(cvalue);
262         return NULL;
263     }
264
265     if (strcmp(ctype, "window_role") == 0) {
266         current_match->role = regex_new(cvalue);
267         return NULL;
268     }
269
270     if (strcmp(ctype, "con_id") == 0) {
271         char *end;
272         long parsed = strtol(cvalue, &end, 10);
273         if (parsed == LONG_MIN ||
274             parsed == LONG_MAX ||
275             parsed < 0 ||
276             (end && *end != '\0')) {
277             ELOG("Could not parse con id \"%s\"\n", cvalue);
278         } else {
279             current_match->con_id = (Con*)parsed;
280             printf("id as int = %p\n", current_match->con_id);
281         }
282         return NULL;
283     }
284
285     if (strcmp(ctype, "id") == 0) {
286         char *end;
287         long parsed = strtol(cvalue, &end, 10);
288         if (parsed == LONG_MIN ||
289             parsed == LONG_MAX ||
290             parsed < 0 ||
291             (end && *end != '\0')) {
292             ELOG("Could not parse window id \"%s\"\n", cvalue);
293         } else {
294             current_match->id = parsed;
295             printf("window id as int = %d\n", current_match->id);
296         }
297         return NULL;
298     }
299
300     if (strcmp(ctype, "con_mark") == 0) {
301         current_match->mark = regex_new(cvalue);
302         return NULL;
303     }
304
305     if (strcmp(ctype, "title") == 0) {
306         current_match->title = regex_new(cvalue);
307         return NULL;
308     }
309
310     ELOG("Unknown criterion: %s\n", ctype);
311
312     /* This command is internal and does not generate a JSON reply. */
313     return NULL;
314 }
315
316 /*
317  * Implementation of 'move [window|container] [to] workspace
318  * next|prev|next_on_output|prev_on_output'.
319  *
320  */
321 char *cmd_move_con_to_workspace(Match *current_match, char *which) {
322     owindow *current;
323
324     DLOG("which=%s\n", which);
325
326     HANDLE_EMPTY_MATCH;
327
328     /* get the workspace */
329     Con *ws;
330     if (strcmp(which, "next") == 0)
331         ws = workspace_next();
332     else if (strcmp(which, "prev") == 0)
333         ws = workspace_prev();
334     else if (strcmp(which, "next_on_output") == 0)
335         ws = workspace_next_on_output();
336     else if (strcmp(which, "prev_on_output") == 0)
337         ws = workspace_prev_on_output();
338     else {
339         ELOG("BUG: called with which=%s\n", which);
340         return sstrdup("{\"sucess\": false}");
341     }
342
343     TAILQ_FOREACH(current, &owindows, owindows) {
344         DLOG("matching: %p / %s\n", current->con, current->con->name);
345         con_move_to_workspace(current->con, ws, true, false);
346     }
347
348     tree_render();
349
350     // XXX: default reply for now, make this a better reply
351     return sstrdup("{\"success\": true}");
352 }
353
354 /*
355  * Implementation of 'move [window|container] [to] workspace <name>'.
356  *
357  */
358 char *cmd_move_con_to_workspace_name(Match *current_match, char *name) {
359     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
360         LOG("You cannot switch to the i3 internal workspaces.\n");
361         return sstrdup("{\"sucess\": false}");
362     }
363
364     owindow *current;
365
366     /* Error out early to not create a non-existing workspace (in
367      * workspace_get()) if we are not actually able to move anything. */
368     if (match_is_empty(current_match) && focused->type == CT_WORKSPACE)
369         return sstrdup("{\"sucess\": false}");
370
371     LOG("should move window to workspace %s\n", name);
372     /* get the workspace */
373     Con *ws = workspace_get(name, NULL);
374
375     HANDLE_EMPTY_MATCH;
376
377     TAILQ_FOREACH(current, &owindows, owindows) {
378         DLOG("matching: %p / %s\n", current->con, current->con->name);
379         con_move_to_workspace(current->con, ws, true, false);
380     }
381
382     tree_render();
383
384     // XXX: default reply for now, make this a better reply
385     return sstrdup("{\"success\": true}");
386 }
387
388 /*
389  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
390  *
391  */
392 char *cmd_resize(Match *current_match, char *way, char *direction, char *resize_px, char *resize_ppt) {
393     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
394     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
395     // 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
396     int px = atoi(resize_px);
397     int ppt = atoi(resize_ppt);
398     if (strcmp(way, "shrink") == 0) {
399         px *= -1;
400         ppt *= -1;
401     }
402
403     Con *floating_con;
404     if ((floating_con = con_inside_floating(focused))) {
405         printf("floating resize\n");
406         if (strcmp(direction, "up") == 0) {
407             floating_con->rect.y -= px;
408             floating_con->rect.height += px;
409         } else if (strcmp(direction, "down") == 0) {
410             floating_con->rect.height += px;
411         } else if (strcmp(direction, "left") == 0) {
412             floating_con->rect.x -= px;
413             floating_con->rect.width += px;
414         } else {
415             floating_con->rect.width += px;
416         }
417     } else {
418         LOG("tiling resize\n");
419         /* get the appropriate current container (skip stacked/tabbed cons) */
420         Con *current = focused;
421         while (current->parent->layout == L_STACKED ||
422                current->parent->layout == L_TABBED)
423             current = current->parent;
424
425         /* Then further go up until we find one with the matching orientation. */
426         orientation_t search_orientation =
427             (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
428
429         while (current->type != CT_WORKSPACE &&
430                current->type != CT_FLOATING_CON &&
431                current->parent->orientation != search_orientation)
432             current = current->parent;
433
434         /* get the default percentage */
435         int children = con_num_children(current->parent);
436         Con *other;
437         LOG("ins. %d children\n", children);
438         double percentage = 1.0 / children;
439         LOG("default percentage = %f\n", percentage);
440
441         orientation_t orientation = current->parent->orientation;
442
443         if ((orientation == HORIZ &&
444              (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
445             (orientation == VERT &&
446              (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
447             LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
448                 (orientation == HORIZ ? "horizontal" : "vertical"));
449             return sstrdup("{\"sucess\": false}");
450         }
451
452         if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
453             other = TAILQ_PREV(current, nodes_head, nodes);
454         } else {
455             other = TAILQ_NEXT(current, nodes);
456         }
457         if (other == TAILQ_END(workspaces)) {
458             LOG("No other container in this direction found, cannot resize.\n");
459             return sstrdup("{\"sucess\": false}");
460         }
461         LOG("other->percent = %f\n", other->percent);
462         LOG("current->percent before = %f\n", current->percent);
463         if (current->percent == 0.0)
464             current->percent = percentage;
465         if (other->percent == 0.0)
466             other->percent = percentage;
467         double new_current_percent = current->percent + ((double)ppt / 100.0);
468         double new_other_percent = other->percent - ((double)ppt / 100.0);
469         LOG("new_current_percent = %f\n", new_current_percent);
470         LOG("new_other_percent = %f\n", new_other_percent);
471         /* Ensure that the new percentages are positive and greater than
472          * 0.05 to have a reasonable minimum size. */
473         if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
474             definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
475             current->percent += ((double)ppt / 100.0);
476             other->percent -= ((double)ppt / 100.0);
477             LOG("current->percent after = %f\n", current->percent);
478             LOG("other->percent after = %f\n", other->percent);
479         } else {
480             LOG("Not resizing, already at minimum size\n");
481         }
482     }
483
484     tree_render();
485
486     // XXX: default reply for now, make this a better reply
487     return sstrdup("{\"success\": true}");
488 }
489
490 /*
491  * Implementation of 'border normal|none|1pixel|toggle'.
492  *
493  */
494 char *cmd_border(Match *current_match, char *border_style_str) {
495     DLOG("border style should be changed to %s\n", border_style_str);
496     owindow *current;
497
498     HANDLE_EMPTY_MATCH;
499
500     TAILQ_FOREACH(current, &owindows, owindows) {
501         DLOG("matching: %p / %s\n", current->con, current->con->name);
502         int border_style = current->con->border_style;
503         if (strcmp(border_style_str, "toggle") == 0) {
504             border_style++;
505             border_style %= 3;
506         } else {
507             if (strcmp(border_style_str, "normal") == 0)
508                 border_style = BS_NORMAL;
509             else if (strcmp(border_style_str, "none") == 0)
510                 border_style = BS_NONE;
511             else if (strcmp(border_style_str, "1pixel") == 0)
512                 border_style = BS_1PIXEL;
513             else {
514                 ELOG("BUG: called with border_style=%s\n", border_style_str);
515                 return sstrdup("{\"sucess\": false}");
516             }
517         }
518         con_set_border_style(current->con, border_style);
519     }
520
521     tree_render();
522
523     // XXX: default reply for now, make this a better reply
524     return sstrdup("{\"success\": true}");
525 }
526
527 /*
528  * Implementation of 'nop <comment>'.
529  *
530  */
531 char *cmd_nop(Match *current_match, char *comment) {
532     LOG("-------------------------------------------------\n");
533     LOG("  NOP: %s\n", comment);
534     LOG("-------------------------------------------------\n");
535
536     return NULL;
537 }
538
539 /*
540  * Implementation of 'append_layout <path>'.
541  *
542  */
543 char *cmd_append_layout(Match *current_match, char *path) {
544     LOG("Appending layout \"%s\"\n", path);
545     tree_append_json(path);
546     tree_render();
547
548     // XXX: default reply for now, make this a better reply
549     return sstrdup("{\"success\": true}");
550 }
551
552 /*
553  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
554  *
555  */
556 char *cmd_workspace(Match *current_match, char *which) {
557     Con *ws;
558
559     DLOG("which=%s\n", which);
560
561     if (strcmp(which, "next") == 0)
562         ws = workspace_next();
563     else if (strcmp(which, "prev") == 0)
564         ws = workspace_prev();
565     else if (strcmp(which, "next_on_output") == 0)
566         ws = workspace_next_on_output();
567     else if (strcmp(which, "prev_on_output") == 0)
568         ws = workspace_prev_on_output();
569     else {
570         ELOG("BUG: called with which=%s\n", which);
571         return sstrdup("{\"sucess\": false}");
572     }
573
574     workspace_show(ws);
575     tree_render();
576
577     // XXX: default reply for now, make this a better reply
578     return sstrdup("{\"success\": true}");
579 }
580
581 /*
582  * Implementation of 'workspace back_and_forth'.
583  *
584  */
585 char *cmd_workspace_back_and_forth(Match *current_match) {
586     workspace_back_and_forth();
587     tree_render();
588
589     // XXX: default reply for now, make this a better reply
590     return sstrdup("{\"success\": true}");
591 }
592
593 /*
594  * Implementation of 'workspace <name>'
595  *
596  */
597 char *cmd_workspace_name(Match *current_match, char *name) {
598     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
599         LOG("You cannot switch to the i3 internal workspaces.\n");
600         return sstrdup("{\"sucess\": false}");
601     }
602
603     DLOG("should switch to workspace %s\n", name);
604
605     Con *ws = con_get_workspace(focused);
606
607     /* Check if the command wants to switch to the current workspace */
608     if (strcmp(ws->name, name) == 0) {
609         DLOG("This workspace is already focused.\n");
610         if (config.workspace_auto_back_and_forth) {
611             workspace_back_and_forth();
612             tree_render();
613         }
614         return sstrdup("{\"sucess\": false}");
615     }
616
617     workspace_show_by_name(name);
618
619     tree_render();
620
621     // XXX: default reply for now, make this a better reply
622     return sstrdup("{\"success\": true}");
623 }
624
625 /*
626  * Implementation of 'mark <mark>'
627  *
628  */
629 char *cmd_mark(Match *current_match, char *mark) {
630     DLOG("Clearing all windows which have that mark first\n");
631
632     Con *con;
633     TAILQ_FOREACH(con, &all_cons, all_cons) {
634         if (con->mark && strcmp(con->mark, mark) == 0)
635             FREE(con->mark);
636     }
637
638     DLOG("marking window with str %s\n", mark);
639     owindow *current;
640
641     HANDLE_EMPTY_MATCH;
642
643     TAILQ_FOREACH(current, &owindows, owindows) {
644         DLOG("matching: %p / %s\n", current->con, current->con->name);
645         current->con->mark = sstrdup(mark);
646     }
647
648     tree_render();
649
650     // XXX: default reply for now, make this a better reply
651     return sstrdup("{\"success\": true}");
652 }
653
654 /*
655  * Implementation of 'mode <string>'.
656  *
657  */
658 char *cmd_mode(Match *current_match, char *mode) {
659     DLOG("mode=%s\n", mode);
660     switch_mode(mode);
661
662     // XXX: default reply for now, make this a better reply
663     return sstrdup("{\"success\": true}");
664 }
665
666 /*
667  * Implementation of 'move [window|container] [to] output <str>'.
668  *
669  */
670 char *cmd_move_con_to_output(Match *current_match, char *name) {
671     owindow *current;
672
673     DLOG("should move window to output %s\n", name);
674
675     HANDLE_EMPTY_MATCH;
676
677     /* get the output */
678     Output *current_output = NULL;
679     Output *output;
680
681     // TODO: fix the handling of criteria
682     TAILQ_FOREACH(current, &owindows, owindows)
683         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
684
685     assert(current_output != NULL);
686
687     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
688     if (strcasecmp(name, "up") == 0)
689         output = get_output_next(D_UP, current_output);
690     else if (strcasecmp(name, "down") == 0)
691         output = get_output_next(D_DOWN, current_output);
692     else if (strcasecmp(name, "left") == 0)
693         output = get_output_next(D_LEFT, current_output);
694     else if (strcasecmp(name, "right") == 0)
695         output = get_output_next(D_RIGHT, current_output);
696     else
697         output = get_output_by_name(name);
698
699     if (!output) {
700         LOG("No such output found.\n");
701         return sstrdup("{\"sucess\": false}");
702     }
703
704     /* get visible workspace on output */
705     Con *ws = NULL;
706     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
707     if (!ws)
708         return sstrdup("{\"sucess\": false}");
709
710     TAILQ_FOREACH(current, &owindows, owindows) {
711         DLOG("matching: %p / %s\n", current->con, current->con->name);
712         con_move_to_workspace(current->con, ws, true, false);
713     }
714
715     tree_render();
716
717     // XXX: default reply for now, make this a better reply
718     return sstrdup("{\"success\": true}");
719 }
720
721 /*
722  * Implementation of 'floating enable|disable|toggle'
723  *
724  */
725 char *cmd_floating(Match *current_match, char *floating_mode) {
726     owindow *current;
727
728     DLOG("floating_mode=%s\n", floating_mode);
729
730     HANDLE_EMPTY_MATCH;
731
732     TAILQ_FOREACH(current, &owindows, owindows) {
733         DLOG("matching: %p / %s\n", current->con, current->con->name);
734         if (strcmp(floating_mode, "toggle") == 0) {
735             DLOG("should toggle mode\n");
736             toggle_floating_mode(current->con, false);
737         } else {
738             DLOG("should switch mode to %s\n", floating_mode);
739             if (strcmp(floating_mode, "enable") == 0) {
740                 floating_enable(current->con, false);
741             } else {
742                 floating_disable(current->con, false);
743             }
744         }
745     }
746
747     tree_render();
748
749     // XXX: default reply for now, make this a better reply
750     return sstrdup("{\"success\": true}");
751 }
752
753 /*
754  * Implementation of 'move workspace to [output] <str>'.
755  *
756  */
757 char *cmd_move_workspace_to_output(Match *current_match, char *name) {
758     DLOG("should move workspace to output %s\n", name);
759
760     HANDLE_EMPTY_MATCH;
761
762     owindow *current;
763     TAILQ_FOREACH(current, &owindows, owindows) {
764         Output *current_output = get_output_containing(current->con->rect.x,
765                                                        current->con->rect.y);
766         Output *output = get_output_from_string(current_output, name);
767         if (!output) {
768             LOG("No such output\n");
769             return sstrdup("{\"sucess\": false}");
770         }
771
772         Con *content = output_get_content(output->con);
773         LOG("got output %p with content %p\n", output, content);
774
775         Con *ws = con_get_workspace(current->con);
776         LOG("should move workspace %p / %s\n", ws, ws->name);
777         if (con_num_children(ws->parent) == 1) {
778             LOG("Not moving workspace \"%s\", it is the only workspace on its output.\n", ws->name);
779             continue;
780         }
781         bool workspace_was_visible = workspace_is_visible(ws);
782         Con *old_content = ws->parent;
783         con_detach(ws);
784         if (workspace_was_visible) {
785             /* The workspace which we just detached was visible, so focus
786              * the next one in the focus-stack. */
787             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
788             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
789             workspace_show(focus_ws);
790         }
791         con_attach(ws, content, false);
792         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
793         if (workspace_was_visible) {
794             /* Focus the moved workspace on the destination output. */
795             workspace_show(ws);
796         }
797     }
798
799     tree_render();
800
801     // XXX: default reply for now, make this a better reply
802     return sstrdup("{\"success\": true}");
803 }
804
805 /*
806  * Implementation of 'split v|h|vertical|horizontal'.
807  *
808  */
809 char *cmd_split(Match *current_match, char *direction) {
810     /* TODO: use matches */
811     LOG("splitting in direction %c\n", direction[0]);
812     tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
813
814     tree_render();
815
816     // XXX: default reply for now, make this a better reply
817     return sstrdup("{\"success\": true}");
818 }
819
820 /*
821  * Implementaiton of 'kill [window|client]'.
822  *
823  */
824 char *cmd_kill(Match *current_match, char *kill_mode_str) {
825     if (kill_mode_str == NULL)
826         kill_mode_str = "window";
827     owindow *current;
828
829     DLOG("kill_mode=%s\n", kill_mode_str);
830
831     int kill_mode;
832     if (strcmp(kill_mode_str, "window") == 0)
833         kill_mode = KILL_WINDOW;
834     else if (strcmp(kill_mode_str, "client") == 0)
835         kill_mode = KILL_CLIENT;
836     else {
837         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
838         return sstrdup("{\"sucess\": false}");
839     }
840
841     /* check if the match is empty, not if the result is empty */
842     if (match_is_empty(current_match))
843         tree_close_con(kill_mode);
844     else {
845         TAILQ_FOREACH(current, &owindows, owindows) {
846             DLOG("matching: %p / %s\n", current->con, current->con->name);
847             tree_close(current->con, kill_mode, false, false);
848         }
849     }
850
851     tree_render();
852
853     // XXX: default reply for now, make this a better reply
854     return sstrdup("{\"success\": true}");
855 }
856
857 /*
858  * Implementation of 'exec [--no-startup-id] <command>'.
859  *
860  */
861 char *cmd_exec(Match *current_match, char *nosn, char *command) {
862     bool no_startup_id = (nosn != NULL);
863
864     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
865     start_application(command, no_startup_id);
866
867     // XXX: default reply for now, make this a better reply
868     return sstrdup("{\"success\": true}");
869 }
870
871 /*
872  * Implementation of 'focus left|right|up|down'.
873  *
874  */
875 char *cmd_focus_direction(Match *current_match, char *direction) {
876     if (focused &&
877         focused->type != CT_WORKSPACE &&
878         focused->fullscreen_mode != CF_NONE) {
879         LOG("Cannot change focus while in fullscreen mode.\n");
880         return sstrdup("{\"sucess\": false}");
881     }
882
883     DLOG("direction = *%s*\n", direction);
884
885     if (strcmp(direction, "left") == 0)
886         tree_next('p', HORIZ);
887     else if (strcmp(direction, "right") == 0)
888         tree_next('n', HORIZ);
889     else if (strcmp(direction, "up") == 0)
890         tree_next('p', VERT);
891     else if (strcmp(direction, "down") == 0)
892         tree_next('n', VERT);
893     else {
894         ELOG("Invalid focus direction (%s)\n", direction);
895         return sstrdup("{\"sucess\": false}");
896     }
897
898     tree_render();
899
900     // XXX: default reply for now, make this a better reply
901     return sstrdup("{\"success\": true}");
902 }
903
904 /*
905  * Implementation of 'focus tiling|floating|mode_toggle'.
906  *
907  */
908 char *cmd_focus_window_mode(Match *current_match, char *window_mode) {
909     if (focused &&
910         focused->type != CT_WORKSPACE &&
911         focused->fullscreen_mode != CF_NONE) {
912         LOG("Cannot change focus while in fullscreen mode.\n");
913         return sstrdup("{\"sucess\": false}");
914     }
915
916     DLOG("window_mode = %s\n", window_mode);
917
918     Con *ws = con_get_workspace(focused);
919     Con *current;
920     if (ws != NULL) {
921         if (strcmp(window_mode, "mode_toggle") == 0) {
922             current = TAILQ_FIRST(&(ws->focus_head));
923             if (current != NULL && current->type == CT_FLOATING_CON)
924                 window_mode = "tiling";
925             else window_mode = "floating";
926         }
927         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
928             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
929                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
930                 continue;
931
932             con_focus(con_descend_focused(current));
933             break;
934         }
935     }
936
937     tree_render();
938
939     // XXX: default reply for now, make this a better reply
940     return sstrdup("{\"success\": true}");
941 }
942
943 /*
944  * Implementation of 'focus parent|child'.
945  *
946  */
947 char *cmd_focus_level(Match *current_match, char *level) {
948     if (focused &&
949         focused->type != CT_WORKSPACE &&
950         focused->fullscreen_mode != CF_NONE) {
951         LOG("Cannot change focus while in fullscreen mode.\n");
952         return sstrdup("{\"sucess\": false}");
953     }
954
955     DLOG("level = %s\n", level);
956
957     if (strcmp(level, "parent") == 0)
958         level_up();
959     else level_down();
960
961     tree_render();
962
963     // XXX: default reply for now, make this a better reply
964     return sstrdup("{\"success\": true}");
965 }
966
967 /*
968  * Implementation of 'focus'.
969  *
970  */
971 char *cmd_focus(Match *current_match) {
972     DLOG("current_match = %p\n", current_match);
973     if (focused &&
974         focused->type != CT_WORKSPACE &&
975         focused->fullscreen_mode != CF_NONE) {
976         LOG("Cannot change focus while in fullscreen mode.\n");
977         return sstrdup("{\"sucess\": false}");
978     }
979
980     owindow *current;
981
982     if (match_is_empty(current_match)) {
983         ELOG("You have to specify which window/container should be focused.\n");
984         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
985
986         char *json_output;
987         sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
988                   "specify which window/container should be focused\"}");
989         return json_output;
990     }
991
992     int count = 0;
993     TAILQ_FOREACH(current, &owindows, owindows) {
994         Con *ws = con_get_workspace(current->con);
995         /* If no workspace could be found, this was a dock window.
996          * Just skip it, you cannot focus dock windows. */
997         if (!ws)
998             continue;
999
1000         /* If the container is not on the current workspace,
1001          * workspace_show() will switch to a different workspace and (if
1002          * enabled) trigger a mouse pointer warp to the currently focused
1003          * container (!) on the target workspace.
1004          *
1005          * Therefore, before calling workspace_show(), we make sure that
1006          * 'current' will be focused on the workspace. However, we cannot
1007          * just con_focus(current) because then the pointer will not be
1008          * warped at all (the code thinks we are already there).
1009          *
1010          * So we focus 'current' to make it the currently focused window of
1011          * the target workspace, then revert focus. */
1012         Con *currently_focused = focused;
1013         con_focus(current->con);
1014         con_focus(currently_focused);
1015
1016         /* Now switch to the workspace, then focus */
1017         workspace_show(ws);
1018         LOG("focusing %p / %s\n", current->con, current->con->name);
1019         con_focus(current->con);
1020         count++;
1021     }
1022
1023     if (count > 1)
1024         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1025             "while only exactly one container can be focused at a time.\n", count);
1026
1027     tree_render();
1028
1029     // XXX: default reply for now, make this a better reply
1030     return sstrdup("{\"success\": true}");
1031 }
1032
1033 /*
1034  * Implementation of 'fullscreen [global]'.
1035  *
1036  */
1037 char *cmd_fullscreen(Match *current_match, char *fullscreen_mode) {
1038     if (fullscreen_mode == NULL)
1039         fullscreen_mode = "output";
1040     DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1041     owindow *current;
1042
1043     HANDLE_EMPTY_MATCH;
1044
1045     TAILQ_FOREACH(current, &owindows, owindows) {
1046         printf("matching: %p / %s\n", current->con, current->con->name);
1047         con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1048     }
1049
1050     tree_render();
1051
1052     // XXX: default reply for now, make this a better reply
1053     return sstrdup("{\"success\": true}");
1054 }
1055
1056 /*
1057  * Implementation of 'move <direction> [<pixels> [px]]'.
1058  *
1059  */
1060 char *cmd_move_direction(Match *current_match, char *direction, char *move_px) {
1061     // 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
1062     int px = atoi(move_px);
1063
1064     /* TODO: make 'move' work with criteria. */
1065     DLOG("moving in direction %s, px %s\n", direction, move_px);
1066     if (con_is_floating(focused)) {
1067         DLOG("floating move with %d pixels\n", px);
1068         Rect newrect = focused->parent->rect;
1069         if (strcmp(direction, "left") == 0) {
1070             newrect.x -= px;
1071         } else if (strcmp(direction, "right") == 0) {
1072             newrect.x += px;
1073         } else if (strcmp(direction, "up") == 0) {
1074             newrect.y -= px;
1075         } else if (strcmp(direction, "down") == 0) {
1076             newrect.y += px;
1077         }
1078         floating_reposition(focused->parent, newrect);
1079     } else {
1080         tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1081                    (strcmp(direction, "left") == 0 ? D_LEFT :
1082                     (strcmp(direction, "up") == 0 ? D_UP :
1083                      D_DOWN))));
1084         tree_render();
1085     }
1086
1087
1088     // XXX: default reply for now, make this a better reply
1089     return sstrdup("{\"success\": true}");
1090 }
1091
1092 /*
1093  * Implementation of 'layout default|stacked|stacking|tabbed'.
1094  *
1095  */
1096 char *cmd_layout(Match *current_match, char *layout_str) {
1097     if (strcmp(layout_str, "stacking") == 0)
1098         layout_str = "stacked";
1099     DLOG("changing layout to %s\n", layout_str);
1100     owindow *current;
1101     int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1102                   (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1103                    L_TABBED));
1104
1105     /* check if the match is empty, not if the result is empty */
1106     if (match_is_empty(current_match))
1107         con_set_layout(focused->parent, layout);
1108     else {
1109         TAILQ_FOREACH(current, &owindows, owindows) {
1110             DLOG("matching: %p / %s\n", current->con, current->con->name);
1111             con_set_layout(current->con, layout);
1112         }
1113     }
1114
1115     tree_render();
1116
1117     // XXX: default reply for now, make this a better reply
1118     return sstrdup("{\"success\": true}");
1119 }
1120
1121 /*
1122  * Implementaiton of 'exit'.
1123  *
1124  */
1125 char *cmd_exit(Match *current_match) {
1126     LOG("Exiting due to user command.\n");
1127     exit(0);
1128
1129     /* unreached */
1130 }
1131
1132 /*
1133  * Implementaiton of 'reload'.
1134  *
1135  */
1136 char *cmd_reload(Match *current_match) {
1137     LOG("reloading\n");
1138     kill_configerror_nagbar(false);
1139     load_configuration(conn, NULL, true);
1140     x_set_i3_atoms();
1141     /* Send an IPC event just in case the ws names have changed */
1142     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1143
1144     // XXX: default reply for now, make this a better reply
1145     return sstrdup("{\"success\": true}");
1146 }
1147
1148 /*
1149  * Implementaiton of 'restart'.
1150  *
1151  */
1152 char *cmd_restart(Match *current_match) {
1153     LOG("restarting i3\n");
1154     i3_restart(false);
1155
1156     // XXX: default reply for now, make this a better reply
1157     return sstrdup("{\"success\": true}");
1158 }
1159
1160 /*
1161  * Implementaiton of 'open'.
1162  *
1163  */
1164 char *cmd_open(Match *current_match) {
1165     LOG("opening new container\n");
1166     Con *con = tree_open_con(NULL, NULL);
1167     con_focus(con);
1168     char *json_output;
1169     sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
1170
1171     tree_render();
1172
1173     return json_output;
1174 }
1175
1176 /*
1177  * Implementation of 'focus output <output>'.
1178  *
1179  */
1180 char *cmd_focus_output(Match *current_match, char *name) {
1181     owindow *current;
1182
1183     DLOG("name = %s\n", name);
1184
1185     HANDLE_EMPTY_MATCH;
1186
1187     /* get the output */
1188     Output *current_output = NULL;
1189     Output *output;
1190
1191     TAILQ_FOREACH(current, &owindows, owindows)
1192         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1193     assert(current_output != NULL);
1194
1195     output = get_output_from_string(current_output, name);
1196
1197     if (!output) {
1198         LOG("No such output found.\n");
1199         return sstrdup("{\"sucess\": false}");
1200     }
1201
1202     /* get visible workspace on output */
1203     Con *ws = NULL;
1204     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1205     if (!ws)
1206         return sstrdup("{\"sucess\": false}");
1207
1208     workspace_show(ws);
1209     tree_render();
1210
1211     // XXX: default reply for now, make this a better reply
1212     return sstrdup("{\"success\": true}");
1213 }
1214
1215 /*
1216  * Implementation of 'move scratchpad'.
1217  *
1218  */
1219 char *cmd_move_scratchpad(Match *current_match) {
1220     DLOG("should move window to scratchpad\n");
1221     owindow *current;
1222
1223     HANDLE_EMPTY_MATCH;
1224
1225     TAILQ_FOREACH(current, &owindows, owindows) {
1226         DLOG("matching: %p / %s\n", current->con, current->con->name);
1227         scratchpad_move(current->con);
1228     }
1229
1230     tree_render();
1231
1232     // XXX: default reply for now, make this a better reply
1233     return sstrdup("{\"success\": true}");
1234 }
1235
1236 /*
1237  * Implementation of 'scratchpad show'.
1238  *
1239  */
1240 char *cmd_scratchpad_show(Match *current_match) {
1241     DLOG("should show scratchpad window\n");
1242     owindow *current;
1243
1244     if (match_is_empty(current_match)) {
1245         scratchpad_show(NULL);
1246     } else {
1247         TAILQ_FOREACH(current, &owindows, owindows) {
1248             DLOG("matching: %p / %s\n", current->con, current->con->name);
1249             scratchpad_show(current->con);
1250         }
1251     }
1252
1253     tree_render();
1254
1255     // XXX: default reply for now, make this a better reply
1256     return sstrdup("{\"success\": true}");
1257 }