]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Bugfix: Fix warping of cursor when only switching screen, not workspace
[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 = get_last_focused_client(conn, container, current_client);
178
179         if (to_focus == NULL) {
180                 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
181                 if (to_focus == NULL)
182                         to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
183         }
184
185         switch (direction) {
186                 case D_LEFT:
187                         /* If we’re at the left-most position, move the rest of the table right */
188                         if (current_col == 0) {
189                                 expand_table_cols_at_head(c_ws);
190                                 new = CUR_CELL;
191                         } else
192                                 new = CUR_TABLE[--current_col][current_row];
193                         break;
194                 case D_RIGHT:
195                         if (current_col == (c_ws->cols-1))
196                                 expand_table_cols(c_ws);
197
198                         new = CUR_TABLE[++current_col][current_row];
199                         break;
200                 case D_UP:
201                         if (move_current_window_in_container(conn, current_client, D_UP))
202                                 return;
203
204                         /* if we’re at the up-most position, move the rest of the table down */
205                         if (current_row == 0) {
206                                 expand_table_rows_at_head(c_ws);
207                                 new = CUR_CELL;
208                         } else
209                                 new = CUR_TABLE[current_col][--current_row];
210                         break;
211                 case D_DOWN:
212                         if (move_current_window_in_container(conn, current_client, D_DOWN))
213                                 return;
214
215                         if (current_row == (c_ws->rows-1))
216                                 expand_table_rows(c_ws);
217
218                         new = CUR_TABLE[current_col][++current_row];
219                         break;
220         }
221
222         /* Remove it from the old container and put it into the new one */
223         remove_client_from_container(conn, current_client, container);
224         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
225         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
226
227         /* Update data structures */
228         current_client->container = new;
229         container->currently_focused = to_focus;
230         new->currently_focused = current_client;
231
232         Workspace *workspace = container->workspace;
233
234         /* delete all empty columns/rows */
235         cleanup_table(conn, workspace);
236
237         /* Fix colspan/rowspan if it’d overlap */
238         fix_colrowspan(conn, workspace);
239
240         render_layout(conn);
241
242         set_focus(conn, current_client, true);
243 }
244
245 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
246         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
247                                             (direction == D_LEFT ? "left" : "right"))));
248         /* Get current window */
249         Container *container = CUR_CELL,
250                   *new = NULL;
251
252         Container **old = &CUR_CELL;
253
254         /* There has to be a container, see focus_window() */
255         assert(container != NULL);
256
257         switch (direction) {
258                 case D_LEFT:
259                         /* If we’re at the left-most position, move the rest of the table right */
260                         if (current_col == 0) {
261                                 expand_table_cols_at_head(c_ws);
262                                 new = CUR_CELL;
263                                 old = &CUR_TABLE[current_col+1][current_row];
264                         } else
265                                 new = CUR_TABLE[--current_col][current_row];
266                         break;
267                 case D_RIGHT:
268                         if (current_col == (c_ws->cols-1))
269                                 expand_table_cols(c_ws);
270
271                         new = CUR_TABLE[++current_col][current_row];
272                         break;
273                 case D_UP:
274                         /* if we’re at the up-most position, move the rest of the table down */
275                         if (current_row == 0) {
276                                 expand_table_rows_at_head(c_ws);
277                                 new = CUR_CELL;
278                                 old = &CUR_TABLE[current_col][current_row+1];
279                         } else
280                                 new = CUR_TABLE[current_col][--current_row];
281                         break;
282                 case D_DOWN:
283                         if (current_row == (c_ws->rows-1))
284                                 expand_table_rows(c_ws);
285
286                         new = CUR_TABLE[current_col][++current_row];
287                         break;
288         }
289
290         LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
291
292         /* Swap the containers */
293         int col = new->col;
294         int row = new->row;
295
296         *old = new;
297         new->col = container->col;
298         new->row = container->row;
299
300         CUR_CELL = container;
301         container->col = col;
302         container->row = row;
303
304         Workspace *workspace = container->workspace;
305
306         /* delete all empty columns/rows */
307         cleanup_table(conn, workspace);
308
309         /* Fix colspan/rowspan if it’d overlap */
310         fix_colrowspan(conn, workspace);
311
312         render_layout(conn);
313 }
314
315 /*
316  * "Snaps" the current container (not possible for windows, because it works at table base)
317  * to the given direction, that is, adjusts cellspan/rowspan
318  *
319  */
320 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
321         LOG("snapping container to direction %d\n", direction);
322
323         Container *container = CUR_CELL;
324
325         assert(container != NULL);
326
327         switch (direction) {
328                 case D_LEFT:
329                         /* Snap to the left is actually a move to the left and then a snap right */
330                         if (!cell_exists(container->col - 1, container->row) ||
331                                 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
332                                 LOG("cannot snap to left - the cell is already used\n");
333                                 return;
334                         }
335
336                         move_current_window(conn, D_LEFT);
337                         snap_current_container(conn, D_RIGHT);
338                         return;
339                 case D_RIGHT: {
340                         /* Check if the cell is used */
341                         int new_col = container->col + container->colspan;
342                         if (!cell_exists(new_col, container->row) ||
343                                 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
344                                 LOG("cannot snap to right - the cell is already used\n");
345                                 return;
346                         }
347
348                         /* Check if there are other cells with rowspan, which are in our way.
349                          * If so, reduce their rowspan. */
350                         for (int i = container->row-1; i >= 0; i--) {
351                                 LOG("we got cell %d, %d with rowspan %d\n",
352                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
353                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
354                                         CUR_TABLE[new_col][i]->rowspan--;
355                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
356                         }
357
358                         container->colspan++;
359                         break;
360                 }
361                 case D_UP:
362                         if (!cell_exists(container->col, container->row - 1) ||
363                                 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
364                                 LOG("cannot snap to top - the cell is already used\n");
365                                 return;
366                         }
367
368                         move_current_window(conn, D_UP);
369                         snap_current_container(conn, D_DOWN);
370                         return;
371                 case D_DOWN: {
372                         LOG("snapping down\n");
373                         int new_row = container->row + container->rowspan;
374                         if (!cell_exists(container->col, new_row) ||
375                                 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
376                                 LOG("cannot snap down - the cell is already used\n");
377                                 return;
378                         }
379
380                         for (int i = container->col-1; i >= 0; i--) {
381                                 LOG("we got cell %d, %d with colspan %d\n",
382                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
383                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
384                                         CUR_TABLE[i][new_row]->colspan--;
385                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
386
387                         }
388
389                         container->rowspan++;
390                         break;
391                 }
392         }
393
394         render_layout(conn);
395 }
396
397 /*
398  * Moves the currently selected window to the given workspace
399  *
400  */
401 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
402         LOG("Moving current window to workspace %d\n", workspace);
403
404         Container *container = CUR_CELL;
405
406         assert(container != NULL);
407
408         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
409         Workspace *t_ws = &(workspaces[workspace-1]);
410
411         Client *current_client = container->currently_focused;
412         if (current_client == NULL) {
413                 LOG("No currently focused client in current container.\n");
414                 return;
415         }
416         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
417         if (to_focus == NULL)
418                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
419
420         if (t_ws->screen == NULL) {
421                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
422                 t_ws->screen = container->workspace->screen;
423                 /* Copy the dimensions from the virtual screen */
424                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
425         }
426
427         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
428
429         assert(to_container != NULL);
430
431         remove_client_from_container(conn, current_client, container);
432
433         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
434         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
435         LOG("Moved.\n");
436
437         current_client->container = to_container;
438         container->currently_focused = to_focus;
439         to_container->currently_focused = current_client;
440
441         /* If we’re moving it to an invisible screen, we need to unmap it */
442         if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
443                 LOG("This workspace is not visible, unmapping\n");
444                 xcb_unmap_window(conn, current_client->frame);
445         }
446
447         /* delete all empty columns/rows */
448         cleanup_table(conn, container->workspace);
449
450         render_layout(conn);
451 }
452
453 /*
454  * Switches to the given workspace
455  *
456  */
457 void show_workspace(xcb_connection_t *conn, int workspace) {
458         Client *client;
459         bool need_warp = false;
460         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
461         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
462         Workspace *t_ws = &(workspaces[workspace-1]);
463
464         LOG("show_workspace(%d)\n", workspace);
465
466         /* Store current_row/current_col */
467         c_ws->current_row = current_row;
468         c_ws->current_col = current_col;
469
470         /* Check if the workspace has not been used yet */
471         if (t_ws->screen == NULL) {
472                 LOG("initializing new workspace, setting num to %d\n", workspace);
473                 t_ws->screen = c_ws->screen;
474                 /* Copy the dimensions from the virtual screen */
475                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
476         }
477
478         if (c_ws->screen != t_ws->screen) {
479                 /* We need to switch to the other screen first */
480                 LOG("moving over to other screen.\n");
481
482                 /* Store the old client */
483                 Client *old_client = CUR_CELL->currently_focused;
484
485                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
486                 current_col = c_ws->current_col;
487                 current_row = c_ws->current_row;
488                 if (CUR_CELL->currently_focused != NULL)
489                         need_warp = true;
490                 else {
491                         Rect *dims = &(c_ws->screen->rect);
492                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
493                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
494                 }
495
496                 /* Re-decorate the old client, it’s not focused anymore */
497                 if ((old_client != NULL) && !old_client->dock)
498                         redecorate_window(conn, old_client);
499                 else xcb_flush(conn);
500         }
501
502         /* Check if we need to change something or if we’re already there */
503         if (c_ws->screen->current_workspace == (workspace-1)) {
504                 if (CUR_CELL->currently_focused != NULL) {
505                         set_focus(conn, CUR_CELL->currently_focused, true);
506                         if (need_warp) {
507                                 warp_pointer_into(conn, CUR_CELL->currently_focused);
508                                 xcb_flush(conn);
509                         }
510                 }
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                 if (need_warp) {
564                         warp_pointer_into(conn, CUR_CELL->currently_focused);
565                         xcb_flush(conn);
566                 }
567         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
568
569         //xcb_ungrab_server(conn);
570
571         render_layout(conn);
572 }
573
574 /*
575  * Parses a command, see file CMDMODE for more information
576  *
577  */
578 void parse_command(xcb_connection_t *conn, const char *command) {
579         LOG("--- parsing command \"%s\" ---\n", command);
580         /* Hmm, just to be sure */
581         if (command[0] == '\0')
582                 return;
583
584         /* Is it an <exec>? Then execute the given command. */
585         if (STARTS_WITH(command, "exec ")) {
586                 LOG("starting \"%s\"\n", command + strlen("exec "));
587                 start_application(command+strlen("exec "));
588                 return;
589         }
590
591         /* Is it an <exit>? */
592         if (STARTS_WITH(command, "exit")) {
593                 LOG("User issued exit-command, exiting without error.\n");
594                 exit(EXIT_SUCCESS);
595         }
596
597         /* Is it <restart>? Then restart in place. */
598         if (STARTS_WITH(command, "restart")) {
599                 LOG("restarting \"%s\"...\n", application_path);
600                 execl(application_path, application_path, NULL);
601                 /* not reached */
602         }
603
604         if (STARTS_WITH(command, "kill")) {
605                 if (CUR_CELL->currently_focused == NULL) {
606                         LOG("There is no window to kill\n");
607                         return;
608                 }
609
610                 LOG("Killing current window\n");
611                 kill_window(conn, CUR_CELL->currently_focused);
612                 return;
613         }
614
615         /* Is it 'f' for fullscreen? */
616         if (command[0] == 'f') {
617                 if (CUR_CELL->currently_focused == NULL)
618                         return;
619                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
620                 return;
621         }
622
623         /* Is it just 's' for stacking or 'd' for default? */
624         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
625                 LOG("Switching mode for current container\n");
626                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
627                 return;
628         }
629
630         enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
631
632         /* Is it a <with>? */
633         if (command[0] == 'w') {
634                 command++;
635                 /* TODO: implement */
636                 if (command[0] == 'c') {
637                         with = WITH_CONTAINER;
638                         command++;
639                 } else {
640                         LOG("not yet implemented.\n");
641                         return;
642                 }
643         }
644
645         /* It's a normal <cmd> */
646         char *rest = NULL;
647         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
648         direction_t direction;
649         int times = strtol(command, &rest, 10);
650         if (rest == NULL) {
651                 LOG("Invalid command (\"%s\")\n", command);
652                 return;
653         }
654
655         if (*rest == '\0') {
656                 /* No rest? This was a tag number, not a times specification */
657                 show_workspace(conn, times);
658                 return;
659         }
660
661         if (*rest == 'm' || *rest == 's') {
662                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
663                 rest++;
664         }
665
666         int workspace = strtol(rest, &rest, 10);
667
668         if (rest == NULL) {
669                 LOG("Invalid command (\"%s\")\n", command);
670                 return;
671         }
672
673         if (*rest == '\0') {
674                 move_current_window_to_workspace(conn, workspace);
675                 return;
676         }
677
678         /* Now perform action to <where> */
679         while (*rest != '\0') {
680                 if (*rest == 'h')
681                         direction = D_LEFT;
682                 else if (*rest == 'j')
683                         direction = D_DOWN;
684                 else if (*rest == 'k')
685                         direction = D_UP;
686                 else if (*rest == 'l')
687                         direction = D_RIGHT;
688                 else {
689                         LOG("unknown direction: %c\n", *rest);
690                         return;
691                 }
692
693                 if (action == ACTION_FOCUS)
694                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
695                 else if (action == ACTION_MOVE) {
696                         if (with == WITH_WINDOW)
697                                 move_current_window(conn, direction);
698                         else move_current_container(conn, direction);
699                 }
700                 else if (action == ACTION_SNAP)
701                         snap_current_container(conn, direction);
702
703                 rest++;
704         }
705
706         LOG("--- done ---\n");
707 }