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