]> git.sur5r.net Git - i3/i3/blob - src/commands.c
e289e018f163e71e5e7432cbb00fc8dd69ffa6fd
[i3/i3] / src / commands.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include <xcb/xcb.h>
19
20 #include "util.h"
21 #include "data.h"
22 #include "table.h"
23 #include "layout.h"
24 #include "i3.h"
25 #include "xinerama.h"
26 #include "client.h"
27
28 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
29         /* If this container is empty, we’re done */
30         if (container->currently_focused == NULL)
31                 return false;
32
33         /* Get the previous/next client or wrap around */
34         Client *candidate = NULL;
35         if (direction == D_UP) {
36                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
37                         candidate = CIRCLEQ_LAST(&(container->clients));
38         }
39         else if (direction == D_DOWN) {
40                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
41                         candidate = CIRCLEQ_FIRST(&(container->clients));
42         } else LOG("Direction not implemented!\n");
43
44         /* If we could not switch, the container contains exactly one client. We return false */
45         if (candidate == container->currently_focused)
46                 return false;
47
48         /* Set focus */
49         set_focus(conn, candidate, true);
50
51         return true;
52 }
53
54 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
55
56 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
57         LOG("focusing direction %d\n", direction);
58
59         int new_row = current_row,
60             new_col = current_col;
61
62         Container *container = CUR_CELL;
63         Workspace *t_ws = c_ws;
64
65         /* There always is a container. If not, current_col or current_row is wrong */
66         assert(container != NULL);
67
68         if (container->workspace->fullscreen_client != NULL) {
69                 LOG("You're in fullscreen mode. Won't switch focus\n");
70                 return;
71         }
72
73         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
74         if (direction == D_UP || direction == D_DOWN) {
75                 if (thing == THING_WINDOW)
76                         /* Let’s see if we can perform up/down focus in the current container */
77                         if (focus_window_in_container(conn, container, direction))
78                                 return;
79
80                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
81                         new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
82                 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
83                         /* Set new_row as a sane default, but it may get overwritten in a second */
84                         new_row--;
85
86                         /* Search from the top to correctly handle rowspanned containers */
87                         for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
88                                 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
89                                         continue;
90
91                                 new_row = rows;
92                                 break;
93                         }
94                 } else {
95                         /* Let’s see if there is a screen down/up there to which we can switch */
96                         LOG("container is at %d with height %d\n", container->y, container->height);
97                         i3Screen *screen;
98                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
99                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
100                                 LOG("Wrapping screen around vertically\n");
101                                 /* No screen found? Then wrap */
102                                 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
103                         }
104                         t_ws = &(workspaces[screen->current_workspace]);
105                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
106                 }
107         } else if (direction == D_LEFT || direction == D_RIGHT) {
108                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
109                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
110                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
111                         /* Set new_col as a sane default, but it may get overwritten in a second */
112                         new_col--;
113
114                         /* Search from the left to correctly handle colspanned containers */
115                         for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
116                                 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
117                                         continue;
118
119                                 new_col = cols;
120                                 break;
121                         }
122                 } else {
123                         /* Let’s see if there is a screen left/right here to which we can switch */
124                         LOG("container is at %d with width %d\n", container->x, container->width);
125                         i3Screen *screen;
126                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
127                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
128                                 LOG("Wrapping screen around horizontally\n");
129                                 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
130                         }
131                         t_ws = &(workspaces[screen->current_workspace]);
132                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
133                 }
134         } else {
135                 LOG("direction unhandled\n");
136                 return;
137         }
138
139         /* Bounds checking */
140         if (new_col >= t_ws->cols)
141                 new_col = (t_ws->cols - 1);
142         if (new_row >= t_ws->rows)
143                 new_row = (t_ws->rows - 1);
144
145         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
146                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
147 }
148
149 /*
150  * Tries to move the window inside its current container.
151  *
152  * Returns true if the window could be moved, false otherwise.
153  *
154  */
155 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
156                 direction_t direction) {
157         assert(client->container != NULL);
158
159         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
160                                              CIRCLEQ_NEXT(client, clients));
161
162         if (other == CIRCLEQ_END(&(client->container->clients)))
163                 return false;
164
165         LOG("i can do that\n");
166         /* We can move the client inside its current container */
167         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
168         if (direction == D_UP)
169                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
170         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
171         render_layout(conn);
172         return true;
173 }
174
175 /*
176  * Moves the current window or whole container to the given direction, creating a column/row if
177  * necessary.
178  *
179  */
180 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
181         LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
182                                             (direction == D_LEFT ? "left" : "right"))));
183         /* Get current window */
184         Container *container = CUR_CELL,
185                   *new = NULL;
186
187         /* There has to be a container, see focus_window() */
188         assert(container != NULL);
189
190         /* If there is no window or the dock window is focused, we’re done */
191         if (container->currently_focused == NULL ||
192             container->currently_focused->dock)
193                 return;
194
195         /* As soon as the client is moved away, the last focused client in the old
196          * container needs to get focus, if any. Therefore, we save it here. */
197         Client *current_client = container->currently_focused;
198         Client *to_focus = get_last_focused_client(conn, container, current_client);
199
200         if (to_focus == NULL) {
201                 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
202                 if (to_focus == NULL)
203                         to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
204         }
205
206         switch (direction) {
207                 case D_LEFT:
208                         /* If we’re at the left-most position, move the rest of the table right */
209                         if (current_col == 0) {
210                                 expand_table_cols_at_head(c_ws);
211                                 new = CUR_CELL;
212                         } else
213                                 new = CUR_TABLE[--current_col][current_row];
214                         break;
215                 case D_RIGHT:
216                         if (current_col == (c_ws->cols-1))
217                                 expand_table_cols(c_ws);
218
219                         new = CUR_TABLE[++current_col][current_row];
220                         break;
221                 case D_UP:
222                         if (move_current_window_in_container(conn, current_client, D_UP))
223                                 return;
224
225                         /* if we’re at the up-most position, move the rest of the table down */
226                         if (current_row == 0) {
227                                 expand_table_rows_at_head(c_ws);
228                                 new = CUR_CELL;
229                         } else
230                                 new = CUR_TABLE[current_col][--current_row];
231                         break;
232                 case D_DOWN:
233                         if (move_current_window_in_container(conn, current_client, D_DOWN))
234                                 return;
235
236                         if (current_row == (c_ws->rows-1))
237                                 expand_table_rows(c_ws);
238
239                         new = CUR_TABLE[current_col][++current_row];
240                         break;
241         }
242
243         /* Remove it from the old container and put it into the new one */
244         client_remove_from_container(conn, current_client, container);
245
246         if (new->currently_focused != NULL)
247                 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
248         else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
249         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
250
251         /* Update data structures */
252         current_client->container = new;
253         container->currently_focused = to_focus;
254         new->currently_focused = current_client;
255
256         Workspace *workspace = container->workspace;
257
258         /* delete all empty columns/rows */
259         cleanup_table(conn, workspace);
260
261         /* Fix colspan/rowspan if it’d overlap */
262         fix_colrowspan(conn, workspace);
263
264         render_layout(conn);
265
266         set_focus(conn, current_client, true);
267 }
268
269 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
270         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
271                                             (direction == D_LEFT ? "left" : "right"))));
272         /* Get current window */
273         Container *container = CUR_CELL,
274                   *new = NULL;
275
276         Container **old = &CUR_CELL;
277
278         /* There has to be a container, see focus_window() */
279         assert(container != NULL);
280
281         switch (direction) {
282                 case D_LEFT:
283                         /* If we’re at the left-most position, move the rest of the table right */
284                         if (current_col == 0) {
285                                 expand_table_cols_at_head(c_ws);
286                                 new = CUR_CELL;
287                                 old = &CUR_TABLE[current_col+1][current_row];
288                         } else
289                                 new = CUR_TABLE[--current_col][current_row];
290                         break;
291                 case D_RIGHT:
292                         if (current_col == (c_ws->cols-1))
293                                 expand_table_cols(c_ws);
294
295                         new = CUR_TABLE[++current_col][current_row];
296                         break;
297                 case D_UP:
298                         /* if we’re at the up-most position, move the rest of the table down */
299                         if (current_row == 0) {
300                                 expand_table_rows_at_head(c_ws);
301                                 new = CUR_CELL;
302                                 old = &CUR_TABLE[current_col][current_row+1];
303                         } else
304                                 new = CUR_TABLE[current_col][--current_row];
305                         break;
306                 case D_DOWN:
307                         if (current_row == (c_ws->rows-1))
308                                 expand_table_rows(c_ws);
309
310                         new = CUR_TABLE[current_col][++current_row];
311                         break;
312         }
313
314         LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
315
316         /* Swap the containers */
317         int col = new->col;
318         int row = new->row;
319
320         *old = new;
321         new->col = container->col;
322         new->row = container->row;
323
324         CUR_CELL = container;
325         container->col = col;
326         container->row = row;
327
328         Workspace *workspace = container->workspace;
329
330         /* delete all empty columns/rows */
331         cleanup_table(conn, workspace);
332
333         /* Fix colspan/rowspan if it’d overlap */
334         fix_colrowspan(conn, workspace);
335
336         render_layout(conn);
337 }
338
339 /*
340  * "Snaps" the current container (not possible for windows, because it works at table base)
341  * to the given direction, that is, adjusts cellspan/rowspan
342  *
343  */
344 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
345         LOG("snapping container to direction %d\n", direction);
346
347         Container *container = CUR_CELL;
348
349         assert(container != NULL);
350
351         switch (direction) {
352                 case D_LEFT:
353                         /* Snap to the left is actually a move to the left and then a snap right */
354                         if (!cell_exists(container->col - 1, container->row) ||
355                                 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
356                                 LOG("cannot snap to left - the cell is already used\n");
357                                 return;
358                         }
359
360                         move_current_window(conn, D_LEFT);
361                         snap_current_container(conn, D_RIGHT);
362                         return;
363                 case D_RIGHT: {
364                         /* Check if the cell is used */
365                         int new_col = container->col + container->colspan;
366                         if (!cell_exists(new_col, container->row) ||
367                                 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
368                                 LOG("cannot snap to right - the cell is already used\n");
369                                 return;
370                         }
371
372                         /* Check if there are other cells with rowspan, which are in our way.
373                          * If so, reduce their rowspan. */
374                         for (int i = container->row-1; i >= 0; i--) {
375                                 LOG("we got cell %d, %d with rowspan %d\n",
376                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
377                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
378                                         CUR_TABLE[new_col][i]->rowspan--;
379                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
380                         }
381
382                         container->colspan++;
383                         break;
384                 }
385                 case D_UP:
386                         if (!cell_exists(container->col, container->row - 1) ||
387                                 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
388                                 LOG("cannot snap to top - the cell is already used\n");
389                                 return;
390                         }
391
392                         move_current_window(conn, D_UP);
393                         snap_current_container(conn, D_DOWN);
394                         return;
395                 case D_DOWN: {
396                         LOG("snapping down\n");
397                         int new_row = container->row + container->rowspan;
398                         if (!cell_exists(container->col, new_row) ||
399                                 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
400                                 LOG("cannot snap down - the cell is already used\n");
401                                 return;
402                         }
403
404                         for (int i = container->col-1; i >= 0; i--) {
405                                 LOG("we got cell %d, %d with colspan %d\n",
406                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
407                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
408                                         CUR_TABLE[i][new_row]->colspan--;
409                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
410
411                         }
412
413                         container->rowspan++;
414                         break;
415                 }
416         }
417
418         render_layout(conn);
419 }
420
421 /*
422  * Moves the currently selected window to the given workspace
423  *
424  */
425 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
426         LOG("Moving current window to workspace %d\n", workspace);
427
428         Container *container = CUR_CELL;
429
430         assert(container != NULL);
431
432         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
433         Workspace *t_ws = &(workspaces[workspace-1]);
434
435         Client *current_client = container->currently_focused;
436         if (current_client == NULL) {
437                 LOG("No currently focused client in current container.\n");
438                 return;
439         }
440         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
441         if (to_focus == NULL)
442                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
443
444         if (t_ws->screen == NULL) {
445                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
446                 t_ws->screen = container->workspace->screen;
447                 /* Copy the dimensions from the virtual screen */
448                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
449         } else {
450                 /* Check if there is already a fullscreen client on the destination workspace and
451                  * stop moving if so. */
452                 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
453                         LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
454                         return;
455                 }
456         }
457
458         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
459
460         assert(to_container != NULL);
461
462         client_remove_from_container(conn, current_client, container);
463         if (container->workspace->fullscreen_client == current_client)
464                 container->workspace->fullscreen_client = NULL;
465
466         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
467         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
468         if (current_client->fullscreen)
469                 t_ws->fullscreen_client = current_client;
470         LOG("Moved.\n");
471
472         current_client->container = to_container;
473         container->currently_focused = to_focus;
474         to_container->currently_focused = current_client;
475
476         /* If we’re moving it to an invisible screen, we need to unmap it */
477         if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
478                 LOG("This workspace is not visible, unmapping\n");
479                 xcb_unmap_window(conn, current_client->frame);
480         }
481
482         /* delete all empty columns/rows */
483         cleanup_table(conn, container->workspace);
484
485         render_layout(conn);
486 }
487
488 /*
489  * Switches to the given workspace
490  *
491  */
492 void show_workspace(xcb_connection_t *conn, int workspace) {
493         Client *client;
494         bool need_warp = false;
495         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
496         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
497         Workspace *t_ws = &(workspaces[workspace-1]);
498
499         LOG("show_workspace(%d)\n", workspace);
500
501         /* Store current_row/current_col */
502         c_ws->current_row = current_row;
503         c_ws->current_col = current_col;
504
505         /* Check if the workspace has not been used yet */
506         if (t_ws->screen == NULL) {
507                 LOG("initializing new workspace, setting num to %d\n", workspace);
508                 t_ws->screen = c_ws->screen;
509                 /* Copy the dimensions from the virtual screen */
510                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
511         }
512
513         if (c_ws->screen != t_ws->screen) {
514                 /* We need to switch to the other screen first */
515                 LOG("moving over to other screen.\n");
516
517                 /* Store the old client */
518                 Client *old_client = CUR_CELL->currently_focused;
519
520                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
521                 current_col = c_ws->current_col;
522                 current_row = c_ws->current_row;
523                 if (CUR_CELL->currently_focused != NULL)
524                         need_warp = true;
525                 else {
526                         Rect *dims = &(c_ws->screen->rect);
527                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
528                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
529                 }
530
531                 /* Re-decorate the old client, it’s not focused anymore */
532                 if ((old_client != NULL) && !old_client->dock)
533                         redecorate_window(conn, old_client);
534                 else xcb_flush(conn);
535         }
536
537         /* Check if we need to change something or if we’re already there */
538         if (c_ws->screen->current_workspace == (workspace-1)) {
539                 if (CUR_CELL->currently_focused != NULL) {
540                         set_focus(conn, CUR_CELL->currently_focused, true);
541                         if (need_warp) {
542                                 client_warp_pointer_into(conn, CUR_CELL->currently_focused);
543                                 xcb_flush(conn);
544                         }
545                 }
546                 return;
547         }
548
549         t_ws->screen->current_workspace = workspace-1;
550         Workspace *old_workspace = c_ws;
551         c_ws = &workspaces[workspace-1];
552
553         /* Unmap all clients of the old workspace */
554         unmap_workspace(conn, old_workspace);
555
556         current_row = c_ws->current_row;
557         current_col = c_ws->current_col;
558         LOG("new current row = %d, current col = %d\n", current_row, current_col);
559
560         ignore_enter_notify_forall(conn, c_ws, true);
561
562         /* Map all clients on the new workspace */
563         FOR_TABLE(c_ws)
564                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
565                         xcb_map_window(conn, client->frame);
566
567         /* Map all stack windows, if any */
568         struct Stack_Window *stack_win;
569         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
570                 if (stack_win->container->workspace == c_ws)
571                         xcb_map_window(conn, stack_win->window);
572
573         ignore_enter_notify_forall(conn, c_ws, false);
574
575         /* Restore focus on the new workspace */
576         if (CUR_CELL->currently_focused != NULL) {
577                 set_focus(conn, CUR_CELL->currently_focused, true);
578                 if (need_warp) {
579                         client_warp_pointer_into(conn, CUR_CELL->currently_focused);
580                         xcb_flush(conn);
581                 }
582         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
583
584         render_layout(conn);
585 }
586
587 /*
588  * Jumps to the given window class / title.
589  * Title is matched using strstr, that is, matches if it appears anywhere
590  * in the string. Regular expressions seem to be a bit overkill here. However,
591  * if we need them for something else somewhen, we may introduce them here, too.
592  *
593  */
594 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
595         char *classtitle;
596         Client *client;
597
598         /* The first character is a quote, this was checked before */
599         classtitle = sstrdup(arguments+1);
600         /* The last character is a quote, we just set it to NULL */
601         classtitle[strlen(classtitle)-1] = '\0';
602
603         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
604                 free(classtitle);
605                 LOG("No matching client found.\n");
606                 return;
607         }
608
609         free(classtitle);
610         set_focus(conn, client, true);
611 }
612
613 /*
614  * Jump directly to the specified workspace, row and col.
615  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
616  *
617  */
618 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
619         int ws, row, col;
620         int result;
621
622         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
623         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
624
625         /* No match? Either no arguments were specified, or no numbers */
626         if (result < 1) {
627                 LOG("At least one valid argument required\n");
628                 return;
629         }
630
631         /* Move to the target workspace */
632         show_workspace(conn, ws);
633
634         if (result < 3)
635                 return;
636
637         LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
638
639         /* Move to row/col */
640         if (row >= c_ws->rows)
641                 row = c_ws->rows - 1;
642         if (col >= c_ws->cols)
643                 col = c_ws->cols - 1;
644
645         LOG("Jumping to col %d, row %d\n", col, row);
646         if (c_ws->table[col][row]->currently_focused != NULL)
647                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
648 }
649
650 /*
651  * Travels the focus stack by the given number of times (or once, if no argument
652  * was specified). That is, selects the window you were in before you focused
653  * the current window.
654  *
655  */
656 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
657         /* Start count at -1 to always skip the first element */
658         int times, count = -1;
659         Client *current;
660
661         if (sscanf(arguments, "%u", &times) != 1) {
662                 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
663                 times = 1;
664         }
665
666         Workspace *ws = CUR_CELL->workspace;
667
668         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
669                 if (++count < times) {
670                         LOG("Skipping\n");
671                         continue;
672                 }
673
674                 LOG("Focussing\n");
675                 set_focus(conn, current, true);
676                 break;
677         }
678 }
679
680 /*
681  * Parses a command, see file CMDMODE for more information
682  *
683  */
684 void parse_command(xcb_connection_t *conn, const char *command) {
685         LOG("--- parsing command \"%s\" ---\n", command);
686         /* Hmm, just to be sure */
687         if (command[0] == '\0')
688                 return;
689
690         /* Is it an <exec>? Then execute the given command. */
691         if (STARTS_WITH(command, "exec ")) {
692                 LOG("starting \"%s\"\n", command + strlen("exec "));
693                 start_application(command+strlen("exec "));
694                 return;
695         }
696
697         /* Is it an <exit>? */
698         if (STARTS_WITH(command, "exit")) {
699                 LOG("User issued exit-command, exiting without error.\n");
700                 exit(EXIT_SUCCESS);
701         }
702
703         /* Is it <restart>? Then restart in place. */
704         if (STARTS_WITH(command, "restart")) {
705                 LOG("restarting \"%s\"...\n", start_argv[0]);
706                 execvp(start_argv[0], start_argv);
707                 /* not reached */
708         }
709
710         if (STARTS_WITH(command, "kill")) {
711                 if (CUR_CELL->currently_focused == NULL) {
712                         LOG("There is no window to kill\n");
713                         return;
714                 }
715
716                 LOG("Killing current window\n");
717                 client_kill(conn, CUR_CELL->currently_focused);
718                 return;
719         }
720
721         /* Is it a jump to a specified workspae, row, col? */
722         if (STARTS_WITH(command, "jump ")) {
723                 const char *arguments = command + strlen("jump ");
724                 if (arguments[0] == '"')
725                         jump_to_window(conn, arguments);
726                 else jump_to_container(conn, arguments);
727                 return;
728         }
729
730         /* Should we travel the focus stack? */
731         if (STARTS_WITH(command, "focus")) {
732                 const char *arguments = command + strlen("focus");
733                 travel_focus_stack(conn, arguments);
734                 return;
735         }
736
737         /* Is it 'f' for fullscreen? */
738         if (command[0] == 'f') {
739                 if (CUR_CELL->currently_focused == NULL)
740                         return;
741                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
742                 return;
743         }
744
745         /* Is it just 's' for stacking or 'd' for default? */
746         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
747                 LOG("Switching mode for current container\n");
748                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
749                 return;
750         }
751
752         enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
753
754         /* Is it a <with>? */
755         if (command[0] == 'w') {
756                 command++;
757                 /* TODO: implement */
758                 if (command[0] == 'c') {
759                         with = WITH_CONTAINER;
760                         command++;
761                 } else {
762                         LOG("not yet implemented.\n");
763                         return;
764                 }
765         }
766
767         /* It's a normal <cmd> */
768         char *rest = NULL;
769         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
770         direction_t direction;
771         int times = strtol(command, &rest, 10);
772         if (rest == NULL) {
773                 LOG("Invalid command (\"%s\")\n", command);
774                 return;
775         }
776
777         if (*rest == '\0') {
778                 /* No rest? This was a tag number, not a times specification */
779                 show_workspace(conn, times);
780                 return;
781         }
782
783         if (*rest == 'm' || *rest == 's') {
784                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
785                 rest++;
786         }
787
788         int workspace = strtol(rest, &rest, 10);
789
790         if (rest == NULL) {
791                 LOG("Invalid command (\"%s\")\n", command);
792                 return;
793         }
794
795         if (*rest == '\0') {
796                 move_current_window_to_workspace(conn, workspace);
797                 return;
798         }
799
800         /* Now perform action to <where> */
801         while (*rest != '\0') {
802                 if (*rest == 'h')
803                         direction = D_LEFT;
804                 else if (*rest == 'j')
805                         direction = D_DOWN;
806                 else if (*rest == 'k')
807                         direction = D_UP;
808                 else if (*rest == 'l')
809                         direction = D_RIGHT;
810                 else {
811                         LOG("unknown direction: %c\n", *rest);
812                         return;
813                 }
814
815                 if (action == ACTION_FOCUS)
816                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
817                 else if (action == ACTION_MOVE) {
818                         if (with == WITH_WINDOW)
819                                 move_current_window(conn, direction);
820                         else move_current_container(conn, direction);
821                 }
822                 else if (action == ACTION_SNAP)
823                         snap_current_container(conn, direction);
824
825                 rest++;
826         }
827
828         LOG("--- done ---\n");
829 }