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