]> git.sur5r.net Git - i3/i3/blob - src/commands.c
b84cb858d00f111e50486b21a4a87e90dd743e01
[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
551         /* Unmap all clients of the current workspace */
552         unmap_workspace(conn, c_ws);
553
554         c_ws = &workspaces[workspace-1];
555         current_row = c_ws->current_row;
556         current_col = c_ws->current_col;
557         LOG("new current row = %d, current col = %d\n", current_row, current_col);
558
559         ignore_enter_notify_forall(conn, c_ws, true);
560
561         /* Map all clients on the new workspace */
562         FOR_TABLE(c_ws)
563                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
564                         xcb_map_window(conn, client->frame);
565
566         /* Map all stack windows, if any */
567         struct Stack_Window *stack_win;
568         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
569                 if (stack_win->container->workspace == c_ws)
570                         xcb_map_window(conn, stack_win->window);
571
572         ignore_enter_notify_forall(conn, c_ws, false);
573
574         /* Restore focus on the new workspace */
575         if (CUR_CELL->currently_focused != NULL) {
576                 set_focus(conn, CUR_CELL->currently_focused, true);
577                 if (need_warp) {
578                         client_warp_pointer_into(conn, CUR_CELL->currently_focused);
579                         xcb_flush(conn);
580                 }
581         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
582
583         render_layout(conn);
584 }
585
586 /*
587  * Jumps to the given window class / title.
588  * Title is matched using strstr, that is, matches if it appears anywhere
589  * in the string. Regular expressions seem to be a bit overkill here. However,
590  * if we need them for something else somewhen, we may introduce them here, too.
591  *
592  */
593 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
594         char *classtitle;
595         Client *client;
596
597         /* The first character is a quote, this was checked before */
598         classtitle = sstrdup(arguments+1);
599         /* The last character is a quote, we just set it to NULL */
600         classtitle[strlen(classtitle)-1] = '\0';
601
602         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
603                 free(classtitle);
604                 LOG("No matching client found.\n");
605                 return;
606         }
607
608         free(classtitle);
609         set_focus(conn, client, true);
610 }
611
612 /*
613  * Jump directly to the specified workspace, row and col.
614  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
615  *
616  */
617 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
618         int ws, row, col;
619         int result;
620
621         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
622         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
623
624         /* No match? Either no arguments were specified, or no numbers */
625         if (result < 1) {
626                 LOG("At least one valid argument required\n");
627                 return;
628         }
629
630         /* Move to the target workspace */
631         show_workspace(conn, ws);
632
633         if (result < 3)
634                 return;
635
636         LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
637
638         /* Move to row/col */
639         if (row >= c_ws->rows)
640                 row = c_ws->rows - 1;
641         if (col >= c_ws->cols)
642                 col = c_ws->cols - 1;
643
644         LOG("Jumping to col %d, row %d\n", col, row);
645         if (c_ws->table[col][row]->currently_focused != NULL)
646                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
647 }
648
649 /*
650  * Travels the focus stack by the given number of times (or once, if no argument
651  * was specified). That is, selects the window you were in before you focused
652  * the current window.
653  *
654  */
655 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
656         /* Start count at -1 to always skip the first element */
657         int times, count = -1;
658         Client *current;
659
660         if (sscanf(arguments, "%u", &times) != 1) {
661                 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
662                 times = 1;
663         }
664
665         Workspace *ws = CUR_CELL->workspace;
666
667         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
668                 if (++count < times) {
669                         LOG("Skipping\n");
670                         continue;
671                 }
672
673                 LOG("Focussing\n");
674                 set_focus(conn, current, true);
675                 break;
676         }
677 }
678
679 /*
680  * Parses a command, see file CMDMODE for more information
681  *
682  */
683 void parse_command(xcb_connection_t *conn, const char *command) {
684         LOG("--- parsing command \"%s\" ---\n", command);
685         /* Hmm, just to be sure */
686         if (command[0] == '\0')
687                 return;
688
689         /* Is it an <exec>? Then execute the given command. */
690         if (STARTS_WITH(command, "exec ")) {
691                 LOG("starting \"%s\"\n", command + strlen("exec "));
692                 start_application(command+strlen("exec "));
693                 return;
694         }
695
696         /* Is it an <exit>? */
697         if (STARTS_WITH(command, "exit")) {
698                 LOG("User issued exit-command, exiting without error.\n");
699                 exit(EXIT_SUCCESS);
700         }
701
702         /* Is it <restart>? Then restart in place. */
703         if (STARTS_WITH(command, "restart")) {
704                 LOG("restarting \"%s\"...\n", start_argv[0]);
705                 execvp(start_argv[0], start_argv);
706                 /* not reached */
707         }
708
709         if (STARTS_WITH(command, "kill")) {
710                 if (CUR_CELL->currently_focused == NULL) {
711                         LOG("There is no window to kill\n");
712                         return;
713                 }
714
715                 LOG("Killing current window\n");
716                 client_kill(conn, CUR_CELL->currently_focused);
717                 return;
718         }
719
720         /* Is it a jump to a specified workspae, row, col? */
721         if (STARTS_WITH(command, "jump ")) {
722                 const char *arguments = command + strlen("jump ");
723                 if (arguments[0] == '"')
724                         jump_to_window(conn, arguments);
725                 else jump_to_container(conn, arguments);
726                 return;
727         }
728
729         /* Should we travel the focus stack? */
730         if (STARTS_WITH(command, "focus")) {
731                 const char *arguments = command + strlen("focus");
732                 travel_focus_stack(conn, arguments);
733                 return;
734         }
735
736         /* Is it 'f' for fullscreen? */
737         if (command[0] == 'f') {
738                 if (CUR_CELL->currently_focused == NULL)
739                         return;
740                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
741                 return;
742         }
743
744         /* Is it just 's' for stacking or 'd' for default? */
745         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
746                 LOG("Switching mode for current container\n");
747                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
748                 return;
749         }
750
751         enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
752
753         /* Is it a <with>? */
754         if (command[0] == 'w') {
755                 command++;
756                 /* TODO: implement */
757                 if (command[0] == 'c') {
758                         with = WITH_CONTAINER;
759                         command++;
760                 } else {
761                         LOG("not yet implemented.\n");
762                         return;
763                 }
764         }
765
766         /* It's a normal <cmd> */
767         char *rest = NULL;
768         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
769         direction_t direction;
770         int times = strtol(command, &rest, 10);
771         if (rest == NULL) {
772                 LOG("Invalid command (\"%s\")\n", command);
773                 return;
774         }
775
776         if (*rest == '\0') {
777                 /* No rest? This was a tag number, not a times specification */
778                 show_workspace(conn, times);
779                 return;
780         }
781
782         if (*rest == 'm' || *rest == 's') {
783                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
784                 rest++;
785         }
786
787         int workspace = strtol(rest, &rest, 10);
788
789         if (rest == NULL) {
790                 LOG("Invalid command (\"%s\")\n", command);
791                 return;
792         }
793
794         if (*rest == '\0') {
795                 move_current_window_to_workspace(conn, workspace);
796                 return;
797         }
798
799         /* Now perform action to <where> */
800         while (*rest != '\0') {
801                 if (*rest == 'h')
802                         direction = D_LEFT;
803                 else if (*rest == 'j')
804                         direction = D_DOWN;
805                 else if (*rest == 'k')
806                         direction = D_UP;
807                 else if (*rest == 'l')
808                         direction = D_RIGHT;
809                 else {
810                         LOG("unknown direction: %c\n", *rest);
811                         return;
812                 }
813
814                 if (action == ACTION_FOCUS)
815                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
816                 else if (action == ACTION_MOVE) {
817                         if (with == WITH_WINDOW)
818                                 move_current_window(conn, direction);
819                         else move_current_container(conn, direction);
820                 }
821                 else if (action == ACTION_SNAP)
822                         snap_current_container(conn, direction);
823
824                 rest++;
825         }
826
827         LOG("--- done ---\n");
828 }