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