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