]> git.sur5r.net Git - i3/i3/blob - src/commands.c
2b4b163e8a5961428e9d1f4d5bb35ccadc99ba31
[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                 /* To make static analyzers happy: */
243                 default:
244                         return;
245         }
246
247         /* Remove it from the old container and put it into the new one */
248         client_remove_from_container(conn, current_client, container, true);
249
250         if (new->currently_focused != NULL)
251                 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
252         else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
253         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
254
255         /* Update data structures */
256         current_client->container = new;
257         current_client->workspace = new->workspace;
258         container->currently_focused = to_focus;
259         new->currently_focused = current_client;
260
261         Workspace *workspace = container->workspace;
262
263         /* delete all empty columns/rows */
264         cleanup_table(conn, workspace);
265
266         /* Fix colspan/rowspan if it’d overlap */
267         fix_colrowspan(conn, workspace);
268
269         render_workspace(conn, workspace->screen, workspace);
270         xcb_flush(conn);
271
272         set_focus(conn, current_client, true);
273 }
274
275 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
276         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
277                                             (direction == D_LEFT ? "left" : "right"))));
278         /* Get current window */
279         Container *container = CUR_CELL,
280                   *new = NULL;
281
282         Container **old = &CUR_CELL;
283
284         /* There has to be a container, see focus_window() */
285         assert(container != NULL);
286
287         switch (direction) {
288                 case D_LEFT:
289                         /* If we’re at the left-most position, move the rest of the table right */
290                         if (current_col == 0) {
291                                 expand_table_cols_at_head(c_ws);
292                                 new = CUR_CELL;
293                                 old = &CUR_TABLE[current_col+1][current_row];
294                         } else
295                                 new = CUR_TABLE[--current_col][current_row];
296                         break;
297                 case D_RIGHT:
298                         if (current_col == (c_ws->cols-1))
299                                 expand_table_cols(c_ws);
300
301                         new = CUR_TABLE[++current_col][current_row];
302                         break;
303                 case D_UP:
304                         /* if we’re at the up-most position, move the rest of the table down */
305                         if (current_row == 0) {
306                                 expand_table_rows_at_head(c_ws);
307                                 new = CUR_CELL;
308                                 old = &CUR_TABLE[current_col][current_row+1];
309                         } else
310                                 new = CUR_TABLE[current_col][--current_row];
311                         break;
312                 case D_DOWN:
313                         if (current_row == (c_ws->rows-1))
314                                 expand_table_rows(c_ws);
315
316                         new = CUR_TABLE[current_col][++current_row];
317                         break;
318                 /* To make static analyzers happy: */
319                 default:
320                         return;
321         }
322
323         LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
324
325         /* Swap the containers */
326         int col = new->col;
327         int row = new->row;
328
329         *old = new;
330         new->col = container->col;
331         new->row = container->row;
332
333         CUR_CELL = container;
334         container->col = col;
335         container->row = row;
336
337         Workspace *workspace = container->workspace;
338
339         /* delete all empty columns/rows */
340         cleanup_table(conn, workspace);
341
342         /* Fix colspan/rowspan if it’d overlap */
343         fix_colrowspan(conn, workspace);
344
345         render_layout(conn);
346 }
347
348 /*
349  * "Snaps" the current container (not possible for windows, because it works at table base)
350  * to the given direction, that is, adjusts cellspan/rowspan
351  *
352  */
353 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
354         LOG("snapping container to direction %d\n", direction);
355
356         Container *container = CUR_CELL;
357
358         assert(container != NULL);
359
360         switch (direction) {
361                 case D_LEFT:
362                         /* Snap to the left is actually a move to the left and then a snap right */
363                         if (!cell_exists(container->col - 1, container->row) ||
364                             CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
365                                 LOG("cannot snap to left - the cell is already used\n");
366                                 return;
367                         }
368
369                         move_current_window(conn, D_LEFT);
370                         snap_current_container(conn, D_RIGHT);
371                         return;
372                 case D_RIGHT: {
373                         /* Check if the cell is used */
374                         int new_col = container->col + container->colspan;
375                         for (int i = 0; i < container->rowspan; i++)
376                                 if (!cell_exists(new_col, container->row + i) ||
377                                     CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
378                                         LOG("cannot snap to right - the cell is already used\n");
379                                         return;
380                                 }
381
382                         /* Check if there are other cells with rowspan, which are in our way.
383                          * If so, reduce their rowspan. */
384                         for (int i = container->row-1; i >= 0; i--) {
385                                 LOG("we got cell %d, %d with rowspan %d\n",
386                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
387                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
388                                         CUR_TABLE[new_col][i]->rowspan--;
389                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
390                         }
391
392                         container->colspan++;
393                         break;
394                 }
395                 case D_UP:
396                         if (!cell_exists(container->col, container->row - 1) ||
397                             CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
398                                 LOG("cannot snap to top - the cell is already used\n");
399                                 return;
400                         }
401
402                         move_current_window(conn, D_UP);
403                         snap_current_container(conn, D_DOWN);
404                         return;
405                 case D_DOWN: {
406                         LOG("snapping down\n");
407                         int new_row = container->row + container->rowspan;
408                         for (int i = 0; i < container->colspan; i++)
409                                 if (!cell_exists(container->col + i, new_row) ||
410                                     CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
411                                         LOG("cannot snap down - the cell is already used\n");
412                                         return;
413                                 }
414
415                         for (int i = container->col-1; i >= 0; i--) {
416                                 LOG("we got cell %d, %d with colspan %d\n",
417                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
418                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
419                                         CUR_TABLE[i][new_row]->colspan--;
420                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
421
422                         }
423
424                         container->rowspan++;
425                         break;
426                 }
427                 /* To make static analyzers happy: */
428                 default:
429                         return;
430         }
431
432         render_layout(conn);
433 }
434
435 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
436         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
437         Workspace *t_ws = &(workspaces[workspace-1]),
438                   *old_ws = client->workspace;
439
440         LOG("moving floating\n");
441
442         if (t_ws->screen == NULL) {
443                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
444                 t_ws->screen = c_ws->screen;
445                 /* Copy the dimensions from the virtual screen */
446                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
447         } else {
448                 /* Check if there is already a fullscreen client on the destination workspace and
449                  * stop moving if so. */
450                 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
451                         LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
452                         return;
453                 }
454         }
455
456         floating_assign_to_workspace(client, t_ws);
457
458         /* If we’re moving it to an invisible screen, we need to unmap it */
459         if (t_ws->screen->current_workspace != t_ws->num) {
460                 LOG("This workspace is not visible, unmapping\n");
461                 xcb_unmap_window(conn, client->frame);
462         } else {
463                 /* If this is not the case, we move the window to a workspace
464                  * which is on another screen, so we also need to adjust its
465                  * coordinates. */
466                 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
467                 uint32_t relative_x = client->rect.x - old_ws->rect.x,
468                          relative_y = client->rect.y - old_ws->rect.y;
469                 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
470                 client->rect.x = t_ws->rect.x + relative_x;
471                 client->rect.y = t_ws->rect.y + relative_y;
472                 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
473                 reposition_client(conn, client);
474                 xcb_flush(conn);
475         }
476
477         LOG("done\n");
478
479         render_layout(conn);
480
481         set_focus(conn, client, true);
482 }
483
484 /*
485  * Moves the currently selected window to the given workspace
486  *
487  */
488 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
489         LOG("Moving current window to workspace %d\n", workspace);
490
491         Container *container = CUR_CELL;
492
493         assert(container != NULL);
494
495         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
496         Workspace *t_ws = &(workspaces[workspace-1]);
497
498         Client *current_client = container->currently_focused;
499         if (current_client == NULL) {
500                 LOG("No currently focused client in current container.\n");
501                 return;
502         }
503         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
504         if (to_focus == NULL)
505                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
506
507         if (t_ws->screen == NULL) {
508                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
509                 t_ws->screen = container->workspace->screen;
510                 /* Copy the dimensions from the virtual screen */
511                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
512         } else {
513                 /* Check if there is already a fullscreen client on the destination workspace and
514                  * stop moving if so. */
515                 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
516                         LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
517                         return;
518                 }
519         }
520
521         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
522
523         assert(to_container != NULL);
524
525         client_remove_from_container(conn, current_client, container, true);
526         if (container->workspace->fullscreen_client == current_client)
527                 container->workspace->fullscreen_client = NULL;
528
529         /* TODO: insert it to the correct position */
530         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
531
532         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
533         if (current_client->fullscreen)
534                 t_ws->fullscreen_client = current_client;
535         LOG("Moved.\n");
536
537         current_client->container = to_container;
538         current_client->workspace = to_container->workspace;
539         container->currently_focused = to_focus;
540         to_container->currently_focused = current_client;
541
542         /* If we’re moving it to an invisible screen, we need to unmap it */
543         if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
544                 LOG("This workspace is not visible, unmapping\n");
545                 xcb_unmap_window(conn, current_client->frame);
546         }
547
548         /* delete all empty columns/rows */
549         cleanup_table(conn, container->workspace);
550
551         render_layout(conn);
552 }
553
554 /*
555  * Switches to the given workspace
556  *
557  */
558 void show_workspace(xcb_connection_t *conn, int workspace) {
559         Client *client;
560         bool need_warp = false;
561         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
562         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
563         Workspace *t_ws = &(workspaces[workspace-1]);
564
565         LOG("show_workspace(%d)\n", workspace);
566
567         /* Store current_row/current_col */
568         c_ws->current_row = current_row;
569         c_ws->current_col = current_col;
570
571         /* Check if the workspace has not been used yet */
572         if (t_ws->screen == NULL) {
573                 LOG("initializing new workspace, setting num to %d\n", workspace);
574                 t_ws->screen = c_ws->screen;
575                 /* Copy the dimensions from the virtual screen */
576                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
577         }
578
579         if (c_ws->screen != t_ws->screen) {
580                 /* We need to switch to the other screen first */
581                 LOG("moving over to other screen.\n");
582
583                 /* Store the old client */
584                 Client *old_client = CUR_CELL->currently_focused;
585
586                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
587                 current_col = c_ws->current_col;
588                 current_row = c_ws->current_row;
589                 if (CUR_CELL->currently_focused != NULL)
590                         need_warp = true;
591                 else {
592                         Rect *dims = &(c_ws->screen->rect);
593                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
594                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
595                 }
596
597                 /* Re-decorate the old client, it’s not focused anymore */
598                 if ((old_client != NULL) && !old_client->dock)
599                         redecorate_window(conn, old_client);
600                 else xcb_flush(conn);
601         }
602
603         /* Check if we need to change something or if we’re already there */
604         if (c_ws->screen->current_workspace == (workspace-1)) {
605                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
606                 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
607                         set_focus(conn, last_focused, true);
608                         if (need_warp) {
609                                 client_warp_pointer_into(conn, last_focused);
610                                 xcb_flush(conn);
611                         }
612                 }
613
614                 return;
615         }
616
617         t_ws->screen->current_workspace = workspace-1;
618         Workspace *old_workspace = c_ws;
619         c_ws = &workspaces[workspace-1];
620
621         /* Unmap all clients of the old workspace */
622         unmap_workspace(conn, old_workspace);
623
624         current_row = c_ws->current_row;
625         current_col = c_ws->current_col;
626         LOG("new current row = %d, current col = %d\n", current_row, current_col);
627
628         ignore_enter_notify_forall(conn, c_ws, true);
629
630         /* Map all clients on the new workspace */
631         FOR_TABLE(c_ws)
632                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
633                         xcb_map_window(conn, client->frame);
634
635         /* Map all floating clients */
636         if (!c_ws->floating_hidden)
637                 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
638                         xcb_map_window(conn, client->frame);
639
640         /* Map all stack windows, if any */
641         struct Stack_Window *stack_win;
642         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
643                 if (stack_win->container->workspace == c_ws)
644                         xcb_map_window(conn, stack_win->window);
645
646         ignore_enter_notify_forall(conn, c_ws, false);
647
648         /* Restore focus on the new workspace */
649         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
650         if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
651                 set_focus(conn, last_focused, true);
652                 if (need_warp) {
653                         client_warp_pointer_into(conn, last_focused);
654                         xcb_flush(conn);
655                 }
656         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
657
658         render_layout(conn);
659 }
660
661 /*
662  * Jumps to the given window class / title.
663  * Title is matched using strstr, that is, matches if it appears anywhere
664  * in the string. Regular expressions seem to be a bit overkill here. However,
665  * if we need them for something else somewhen, we may introduce them here, too.
666  *
667  */
668 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
669         char *classtitle;
670         Client *client;
671
672         /* The first character is a quote, this was checked before */
673         classtitle = sstrdup(arguments+1);
674         /* The last character is a quote, we just set it to NULL */
675         classtitle[strlen(classtitle)-1] = '\0';
676
677         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
678                 free(classtitle);
679                 LOG("No matching client found.\n");
680                 return;
681         }
682
683         free(classtitle);
684         set_focus(conn, client, true);
685 }
686
687 /*
688  * Jump directly to the specified workspace, row and col.
689  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
690  *
691  */
692 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
693         int ws, row, col;
694         int result;
695
696         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
697         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
698
699         /* No match? Either no arguments were specified, or no numbers */
700         if (result < 1) {
701                 LOG("At least one valid argument required\n");
702                 return;
703         }
704
705         /* Move to the target workspace */
706         show_workspace(conn, ws);
707
708         if (result < 3)
709                 return;
710
711         LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
712
713         /* Move to row/col */
714         if (row >= c_ws->rows)
715                 row = c_ws->rows - 1;
716         if (col >= c_ws->cols)
717                 col = c_ws->cols - 1;
718
719         LOG("Jumping to col %d, row %d\n", col, row);
720         if (c_ws->table[col][row]->currently_focused != NULL)
721                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
722 }
723
724 /*
725  * Travels the focus stack by the given number of times (or once, if no argument
726  * was specified). That is, selects the window you were in before you focused
727  * the current window.
728  *
729  * The special values 'floating' (select the next floating window), 'tiling'
730  * (select the next tiling window), 'ft' (if the current window is floating,
731  * select the next tiling window and vice-versa) are also valid
732  *
733  */
734 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
735         /* Start count at -1 to always skip the first element */
736         int times, count = -1;
737         Client *current;
738         bool floating_criteria;
739
740         /* Either it’s one of the special values… */
741         if (strcasecmp(arguments, "floating") == 0) {
742                 floating_criteria = true;
743         } else if (strcasecmp(arguments, "tiling") == 0) {
744                 floating_criteria = false;
745         } else if (strcasecmp(arguments, "ft") == 0) {
746                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
747                 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
748                         LOG("Cannot select the next floating/tiling client because there is no client at all\n");
749                         return;
750                 }
751
752                 floating_criteria = !client_is_floating(last_focused);
753         } else {
754                 /* …or a number was specified */
755                 if (sscanf(arguments, "%u", &times) != 1) {
756                         LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
757                         times = 1;
758                 }
759
760                 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
761                         if (++count < times) {
762                                 LOG("Skipping\n");
763                                 continue;
764                         }
765
766                         LOG("Focussing\n");
767                         set_focus(conn, current, true);
768                         break;
769                 }
770                 return;
771         }
772
773         /* Select the next client matching the criteria parsed above */
774         SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
775                 if (client_is_floating(current) == floating_criteria) {
776                         set_focus(conn, current, true);
777                         break;
778                 }
779 }
780
781 /*
782  * Goes through the list of arguments (for exec()) and checks if the given argument
783  * is present. If not, it copies the arguments (because we cannot realloc it) and
784  * appends the given argument.
785  *
786  */
787 static char **append_argument(char **original, char *argument) {
788         int num_args;
789         for (num_args = 0; original[num_args] != NULL; num_args++) {
790                 LOG("original argument: \"%s\"\n", original[num_args]);
791                 /* If the argument is already present we return the original pointer */
792                 if (strcmp(original[num_args], argument) == 0)
793                         return original;
794         }
795         /* Copy the original array */
796         char **result = smalloc((num_args+2) * sizeof(char*));
797         memcpy(result, original, num_args * sizeof(char*));
798         result[num_args] = argument;
799         result[num_args+1] = NULL;
800
801         return result;
802 }
803
804 /*
805  * Parses a command, see file CMDMODE for more information
806  *
807  */
808 void parse_command(xcb_connection_t *conn, const char *command) {
809         LOG("--- parsing command \"%s\" ---\n", command);
810         /* Get the first client from focus stack because floating clients are not
811          * in any container, therefore CUR_CELL is not appropriate. */
812         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
813         if (last_focused == SLIST_END(&(c_ws->focus_stack)))
814                 last_focused = NULL;
815
816         /* Hmm, just to be sure */
817         if (command[0] == '\0')
818                 return;
819
820         /* Is it an <exec>? Then execute the given command. */
821         if (STARTS_WITH(command, "exec ")) {
822                 LOG("starting \"%s\"\n", command + strlen("exec "));
823                 start_application(command+strlen("exec "));
824                 return;
825         }
826
827         /* Is it an <exit>? */
828         if (STARTS_WITH(command, "exit")) {
829                 LOG("User issued exit-command, exiting without error.\n");
830                 exit(EXIT_SUCCESS);
831         }
832
833         /* Is it <restart>? Then restart in place. */
834         if (STARTS_WITH(command, "restart")) {
835                 LOG("restarting \"%s\"...\n", start_argv[0]);
836                 /* make sure -a is in the argument list or append it */
837                 start_argv = append_argument(start_argv, "-a");
838
839                 execvp(start_argv[0], start_argv);
840                 /* not reached */
841         }
842
843         if (STARTS_WITH(command, "kill")) {
844                 if (last_focused == NULL) {
845                         LOG("There is no window to kill\n");
846                         return;
847                 }
848
849                 LOG("Killing current window\n");
850                 client_kill(conn, last_focused);
851                 return;
852         }
853
854         /* Is it a jump to a specified workspace, row, col? */
855         if (STARTS_WITH(command, "jump ")) {
856                 const char *arguments = command + strlen("jump ");
857                 if (arguments[0] == '"')
858                         jump_to_window(conn, arguments);
859                 else jump_to_container(conn, arguments);
860                 return;
861         }
862
863         /* Should we travel the focus stack? */
864         if (STARTS_WITH(command, "focus")) {
865                 const char *arguments = command + strlen("focus ");
866                 travel_focus_stack(conn, arguments);
867                 return;
868         }
869
870         /* Is it 'f' for fullscreen? */
871         if (command[0] == 'f') {
872                 if (last_focused == NULL)
873                         return;
874                 client_toggle_fullscreen(conn, last_focused);
875                 return;
876         }
877
878         /* Is it just 's' for stacking or 'd' for default? */
879         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
880                 if (last_focused == NULL || client_is_floating(last_focused)) {
881                         LOG("not switching, this is a floating client\n");
882                         return;
883                 }
884                 LOG("Switching mode for current container\n");
885                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
886                 return;
887         }
888
889         if (command[0] == 'H') {
890                 LOG("Hiding all floating windows\n");
891                 floating_toggle_hide(conn, c_ws);
892                 return;
893         }
894
895         enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
896
897         /* Is it a <with>? */
898         if (command[0] == 'w') {
899                 command++;
900                 /* TODO: implement */
901                 if (command[0] == 'c') {
902                         with = WITH_CONTAINER;
903                         command++;
904                 } else if (command[0] == 'w') {
905                         with = WITH_WORKSPACE;
906                         command++;
907                 } else {
908                         LOG("not yet implemented.\n");
909                         return;
910                 }
911         }
912
913         /* Is it 't' for toggle tiling/floating? */
914         if (command[0] == 't') {
915                 if (with == WITH_WORKSPACE) {
916                         c_ws->auto_float = !c_ws->auto_float;
917                         LOG("autofloat is now %d\n", c_ws->auto_float);
918                         return;
919                 }
920                 if (last_focused == NULL) {
921                         LOG("Cannot toggle tiling/floating: workspace empty\n");
922                         return;
923                 }
924
925                 toggle_floating_mode(conn, last_focused, false);
926                 /* delete all empty columns/rows */
927                 cleanup_table(conn, last_focused->workspace);
928
929                 /* Fix colspan/rowspan if it’d overlap */
930                 fix_colrowspan(conn, last_focused->workspace);
931
932                 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
933                 xcb_flush(conn);
934                 return;
935         }
936
937         /* It’s a normal <cmd> */
938         char *rest = NULL;
939         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
940         direction_t direction;
941         int times = strtol(command, &rest, 10);
942         if (rest == NULL) {
943                 LOG("Invalid command (\"%s\")\n", command);
944                 return;
945         }
946
947         if (*rest == '\0') {
948                 /* No rest? This was a workspace number, not a times specification */
949                 show_workspace(conn, times);
950                 return;
951         }
952
953         if (*rest == 'm' || *rest == 's') {
954                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
955                 rest++;
956         }
957
958         int workspace = strtol(rest, &rest, 10);
959
960         if (rest == NULL) {
961                 LOG("Invalid command (\"%s\")\n", command);
962                 return;
963         }
964
965         if (*rest == '\0') {
966                 if (last_focused != NULL && client_is_floating(last_focused))
967                         move_floating_window_to_workspace(conn, last_focused, workspace);
968                 else move_current_window_to_workspace(conn, workspace);
969                 return;
970         }
971
972         if (last_focused == NULL) {
973                 LOG("Not performing (no window found)\n");
974                 return;
975         }
976
977         if (client_is_floating(last_focused) &&
978             (action != ACTION_FOCUS && action != ACTION_MOVE)) {
979                 LOG("Not performing (floating)\n");
980                 return;
981         }
982
983         /* Now perform action to <where> */
984         while (*rest != '\0') {
985                 if (*rest == 'h')
986                         direction = D_LEFT;
987                 else if (*rest == 'j')
988                         direction = D_DOWN;
989                 else if (*rest == 'k')
990                         direction = D_UP;
991                 else if (*rest == 'l')
992                         direction = D_RIGHT;
993                 else {
994                         LOG("unknown direction: %c\n", *rest);
995                         return;
996                 }
997                 rest++;
998
999                 if (action == ACTION_FOCUS) {
1000                         if (client_is_floating(last_focused)) {
1001                                 floating_focus_direction(conn, last_focused, direction);
1002                                 continue;
1003                         }
1004                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1005                         continue;
1006                 }
1007
1008                 if (action == ACTION_MOVE) {
1009                         if (client_is_floating(last_focused)) {
1010                                 floating_move(conn, last_focused, direction);
1011                                 continue;
1012                         }
1013                         if (with == WITH_WINDOW)
1014                                 move_current_window(conn, direction);
1015                         else move_current_container(conn, direction);
1016                         continue;
1017                 }
1018
1019                 if (action == ACTION_SNAP) {
1020                         snap_current_container(conn, direction);
1021                         continue;
1022                 }
1023         }
1024
1025         LOG("--- done ---\n");
1026 }