]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Refactor workspaces to be stored in a TAILQ instead of an array
[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 #include "xcb.h"
29 #include "config.h"
30 #include "workspace.h"
31 #include "commands.h"
32 #include "resize.h"
33
34 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
35         /* If this container is empty, we’re done */
36         if (container->currently_focused == NULL)
37                 return false;
38
39         /* Get the previous/next client or wrap around */
40         Client *candidate = NULL;
41         if (direction == D_UP) {
42                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43                         candidate = CIRCLEQ_LAST(&(container->clients));
44         }
45         else if (direction == D_DOWN) {
46                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
47                         candidate = CIRCLEQ_FIRST(&(container->clients));
48         } else LOG("Direction not implemented!\n");
49
50         /* If we could not switch, the container contains exactly one client. We return false */
51         if (candidate == container->currently_focused)
52                 return false;
53
54         /* Set focus */
55         set_focus(conn, candidate, true);
56
57         return true;
58 }
59
60 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
61
62 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
63         Client *current;
64         LOG("Jumping to \"%s\"\n", mark);
65
66         Workspace *ws;
67         TAILQ_FOREACH(ws, workspaces, workspaces)
68                 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
69                         if (current->mark == NULL || strcmp(current->mark, mark) != 0)
70                                 continue;
71
72                         set_focus(conn, current, true);
73                         return;
74                 }
75
76         LOG("No window with this mark found\n");
77 }
78
79 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
80         LOG("focusing direction %d\n", direction);
81
82         int new_row = current_row,
83             new_col = current_col;
84         Container *container = CUR_CELL;
85         Workspace *t_ws = c_ws;
86
87         /* Makes sure new_col and new_row are within bounds of the new workspace */
88         void check_colrow_boundaries() {
89                 if (new_col >= t_ws->cols)
90                         new_col = (t_ws->cols - 1);
91                 if (new_row >= t_ws->rows)
92                         new_row = (t_ws->rows - 1);
93         }
94
95         /* There always is a container. If not, current_col or current_row is wrong */
96         assert(container != NULL);
97
98         if (container->workspace->fullscreen_client != NULL) {
99                 LOG("You're in fullscreen mode. Won't switch focus\n");
100                 return;
101         }
102
103         /* For focusing screens, situation is different: we get the rect
104          * of the current screen, then get the screen which is on its
105          * right/left/bottom/top and just switch to the workspace on
106          * the target screen. */
107         if (thing == THING_SCREEN) {
108                 i3Screen *cs = c_ws->screen;
109                 assert(cs != NULL);
110                 Rect bounds = cs->rect;
111
112                 if (direction == D_RIGHT)
113                         bounds.x += bounds.width;
114                 else if (direction == D_LEFT)
115                         bounds.x -= bounds.width;
116                 else if (direction == D_UP)
117                         bounds.y -= bounds.height;
118                 else bounds.y += bounds.height;
119
120                 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
121                 if (target == NULL) {
122                         LOG("Target screen NULL\n");
123                         /* Wrap around if the target screen is out of bounds */
124                         if (direction == D_RIGHT)
125                                 target = get_screen_most(D_LEFT, cs);
126                         else if (direction == D_LEFT)
127                                 target = get_screen_most(D_RIGHT, cs);
128                         else if (direction == D_UP)
129                                 target = get_screen_most(D_DOWN, cs);
130                         else target = get_screen_most(D_UP, cs);
131                 }
132
133                 LOG("Switching to ws %d\n", target->current_workspace + 1);
134                 workspace_show(conn, target->current_workspace->num + 1);
135                 return;
136         }
137
138         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
139         if (direction == D_UP || direction == D_DOWN) {
140                 if (thing == THING_WINDOW)
141                         /* Let’s see if we can perform up/down focus in the current container */
142                         if (focus_window_in_container(conn, container, direction))
143                                 return;
144
145                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
146                         new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
147                 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
148                         /* Set new_row as a sane default, but it may get overwritten in a second */
149                         new_row--;
150
151                         /* Search from the top to correctly handle rowspanned containers */
152                         for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
153                                 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
154                                         continue;
155
156                                 new_row = rows;
157                                 break;
158                         }
159                 } else {
160                         /* Let’s see if there is a screen down/up there to which we can switch */
161                         LOG("container is at %d with height %d\n", container->y, container->height);
162                         i3Screen *screen;
163                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
164                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
165                                 LOG("Wrapping screen around vertically\n");
166                                 /* No screen found? Then wrap */
167                                 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
168                         }
169                         t_ws = screen->current_workspace;
170                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
171                 }
172
173                 check_colrow_boundaries();
174
175                 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
176                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
177                         LOG("Cell empty, checking for colspanned client above...\n");
178                         for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
179                                 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
180                                         continue;
181
182                                 new_col = cols;
183                                 break;
184                         }
185                         LOG("Fixed it to new col %d\n", new_col);
186                 }
187         } else if (direction == D_LEFT || direction == D_RIGHT) {
188                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
189                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
190                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
191                         /* Set new_col as a sane default, but it may get overwritten in a second */
192                         new_col--;
193
194                         /* Search from the left to correctly handle colspanned containers */
195                         for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
196                                 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
197                                         continue;
198
199                                 new_col = cols;
200                                 break;
201                         }
202                 } else {
203                         /* Let’s see if there is a screen left/right here to which we can switch */
204                         LOG("container is at %d with width %d\n", container->x, container->width);
205                         i3Screen *screen;
206                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
207                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
208                                 LOG("Wrapping screen around horizontally\n");
209                                 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
210                         }
211                         t_ws = screen->current_workspace;
212                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
213                 }
214
215                 check_colrow_boundaries();
216
217                 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
218                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
219                         LOG("Cell empty, checking for rowspanned client above...\n");
220                         for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
221                                 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
222                                         continue;
223
224                                 new_row = rows;
225                                 break;
226                         }
227                         LOG("Fixed it to new row %d\n", new_row);
228                 }
229         } else {
230                 LOG("direction unhandled\n");
231                 return;
232         }
233
234         check_colrow_boundaries();
235
236         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
237                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
238 }
239
240 /*
241  * Tries to move the window inside its current container.
242  *
243  * Returns true if the window could be moved, false otherwise.
244  *
245  */
246 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
247                 direction_t direction) {
248         assert(client->container != NULL);
249
250         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
251                                              CIRCLEQ_NEXT(client, clients));
252
253         if (other == CIRCLEQ_END(&(client->container->clients)))
254                 return false;
255
256         LOG("i can do that\n");
257         /* We can move the client inside its current container */
258         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
259         if (direction == D_UP)
260                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
261         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
262         render_layout(conn);
263         return true;
264 }
265
266 /*
267  * Moves the current window or whole container to the given direction, creating a column/row if
268  * necessary.
269  *
270  */
271 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
272         LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
273                                             (direction == D_LEFT ? "left" : "right"))));
274         /* Get current window */
275         Container *container = CUR_CELL,
276                   *new = NULL;
277
278         /* There has to be a container, see focus_window() */
279         assert(container != NULL);
280
281         /* If there is no window or the dock window is focused, we’re done */
282         if (container->currently_focused == NULL ||
283             container->currently_focused->dock)
284                 return;
285
286         /* As soon as the client is moved away, the last focused client in the old
287          * container needs to get focus, if any. Therefore, we save it here. */
288         Client *current_client = container->currently_focused;
289         Client *to_focus = get_last_focused_client(conn, container, current_client);
290
291         if (to_focus == NULL) {
292                 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
293                 if (to_focus == NULL)
294                         to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
295         }
296
297         switch (direction) {
298                 case D_LEFT:
299                         /* If we’re at the left-most position, move the rest of the table right */
300                         if (current_col == 0) {
301                                 expand_table_cols_at_head(c_ws);
302                                 new = CUR_CELL;
303                         } else
304                                 new = CUR_TABLE[--current_col][current_row];
305                         break;
306                 case D_RIGHT:
307                         if (current_col == (c_ws->cols-1))
308                                 expand_table_cols(c_ws);
309
310                         new = CUR_TABLE[++current_col][current_row];
311                         break;
312                 case D_UP:
313                         if (move_current_window_in_container(conn, current_client, D_UP))
314                                 return;
315
316                         /* if we’re at the up-most position, move the rest of the table down */
317                         if (current_row == 0) {
318                                 expand_table_rows_at_head(c_ws);
319                                 new = CUR_CELL;
320                         } else
321                                 new = CUR_TABLE[current_col][--current_row];
322                         break;
323                 case D_DOWN:
324                         if (move_current_window_in_container(conn, current_client, D_DOWN))
325                                 return;
326
327                         if (current_row == (c_ws->rows-1))
328                                 expand_table_rows(c_ws);
329
330                         new = CUR_TABLE[current_col][++current_row];
331                         break;
332                 /* To make static analyzers happy: */
333                 default:
334                         return;
335         }
336
337         /* Remove it from the old container and put it into the new one */
338         client_remove_from_container(conn, current_client, container, true);
339
340         if (new->currently_focused != NULL)
341                 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
342         else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
343         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
344
345         /* Update data structures */
346         current_client->container = new;
347         current_client->workspace = new->workspace;
348         container->currently_focused = to_focus;
349         new->currently_focused = current_client;
350
351         Workspace *workspace = container->workspace;
352
353         /* delete all empty columns/rows */
354         cleanup_table(conn, workspace);
355
356         /* Fix colspan/rowspan if it’d overlap */
357         fix_colrowspan(conn, workspace);
358
359         render_workspace(conn, workspace->screen, workspace);
360         xcb_flush(conn);
361
362         set_focus(conn, current_client, true);
363 }
364
365 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
366         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
367                                             (direction == D_LEFT ? "left" : "right"))));
368         /* Get current window */
369         Container *container = CUR_CELL,
370                   *new = NULL;
371
372         Container **old = &CUR_CELL;
373
374         /* There has to be a container, see focus_window() */
375         assert(container != NULL);
376
377         switch (direction) {
378                 case D_LEFT:
379                         /* If we’re at the left-most position, move the rest of the table right */
380                         if (current_col == 0) {
381                                 expand_table_cols_at_head(c_ws);
382                                 new = CUR_CELL;
383                                 old = &CUR_TABLE[current_col+1][current_row];
384                         } else
385                                 new = CUR_TABLE[--current_col][current_row];
386                         break;
387                 case D_RIGHT:
388                         if (current_col == (c_ws->cols-1))
389                                 expand_table_cols(c_ws);
390
391                         new = CUR_TABLE[++current_col][current_row];
392                         break;
393                 case D_UP:
394                         /* if we’re at the up-most position, move the rest of the table down */
395                         if (current_row == 0) {
396                                 expand_table_rows_at_head(c_ws);
397                                 new = CUR_CELL;
398                                 old = &CUR_TABLE[current_col][current_row+1];
399                         } else
400                                 new = CUR_TABLE[current_col][--current_row];
401                         break;
402                 case D_DOWN:
403                         if (current_row == (c_ws->rows-1))
404                                 expand_table_rows(c_ws);
405
406                         new = CUR_TABLE[current_col][++current_row];
407                         break;
408                 /* To make static analyzers happy: */
409                 default:
410                         return;
411         }
412
413         LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
414
415         /* Swap the containers */
416         int col = new->col;
417         int row = new->row;
418
419         *old = new;
420         new->col = container->col;
421         new->row = container->row;
422
423         CUR_CELL = container;
424         container->col = col;
425         container->row = row;
426
427         Workspace *workspace = container->workspace;
428
429         /* delete all empty columns/rows */
430         cleanup_table(conn, workspace);
431
432         /* Fix colspan/rowspan if it’d overlap */
433         fix_colrowspan(conn, workspace);
434
435         render_layout(conn);
436 }
437
438 /*
439  * "Snaps" the current container (not possible for windows, because it works at table base)
440  * to the given direction, that is, adjusts cellspan/rowspan
441  *
442  */
443 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
444         LOG("snapping container to direction %d\n", direction);
445
446         Container *container = CUR_CELL;
447
448         assert(container != NULL);
449
450         switch (direction) {
451                 case D_LEFT:
452                         /* Snap to the left is actually a move to the left and then a snap right */
453                         if (!cell_exists(container->col - 1, container->row) ||
454                             CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
455                                 LOG("cannot snap to left - the cell is already used\n");
456                                 return;
457                         }
458
459                         move_current_window(conn, D_LEFT);
460                         snap_current_container(conn, D_RIGHT);
461                         return;
462                 case D_RIGHT: {
463                         /* Check if the cell is used */
464                         int new_col = container->col + container->colspan;
465                         for (int i = 0; i < container->rowspan; i++)
466                                 if (!cell_exists(new_col, container->row + i) ||
467                                     CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
468                                         LOG("cannot snap to right - the cell is already used\n");
469                                         return;
470                                 }
471
472                         /* Check if there are other cells with rowspan, which are in our way.
473                          * If so, reduce their rowspan. */
474                         for (int i = container->row-1; i >= 0; i--) {
475                                 LOG("we got cell %d, %d with rowspan %d\n",
476                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
477                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
478                                         CUR_TABLE[new_col][i]->rowspan--;
479                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
480                         }
481
482                         container->colspan++;
483                         break;
484                 }
485                 case D_UP:
486                         if (!cell_exists(container->col, container->row - 1) ||
487                             CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
488                                 LOG("cannot snap to top - the cell is already used\n");
489                                 return;
490                         }
491
492                         move_current_window(conn, D_UP);
493                         snap_current_container(conn, D_DOWN);
494                         return;
495                 case D_DOWN: {
496                         LOG("snapping down\n");
497                         int new_row = container->row + container->rowspan;
498                         for (int i = 0; i < container->colspan; i++)
499                                 if (!cell_exists(container->col + i, new_row) ||
500                                     CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
501                                         LOG("cannot snap down - the cell is already used\n");
502                                         return;
503                                 }
504
505                         for (int i = container->col-1; i >= 0; i--) {
506                                 LOG("we got cell %d, %d with colspan %d\n",
507                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
508                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
509                                         CUR_TABLE[i][new_row]->colspan--;
510                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
511
512                         }
513
514                         container->rowspan++;
515                         break;
516                 }
517                 /* To make static analyzers happy: */
518                 default:
519                         return;
520         }
521
522         render_layout(conn);
523 }
524
525 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
526         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
527         Workspace *t_ws = workspace_get(workspace-1),
528                   *old_ws = client->workspace;
529
530         LOG("moving floating\n");
531
532         workspace_initialize(t_ws, c_ws->screen);
533
534         /* Check if there is already a fullscreen client on the destination workspace and
535          * stop moving if so. */
536         if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
537                 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
538                 return;
539         }
540
541         floating_assign_to_workspace(client, t_ws);
542
543         /* If we’re moving it to an invisible screen, we need to unmap it */
544         if (!workspace_is_visible(t_ws)) {
545                 LOG("This workspace is not visible, unmapping\n");
546                 client_unmap(conn, client);
547         } else {
548                 /* If this is not the case, we move the window to a workspace
549                  * which is on another screen, so we also need to adjust its
550                  * coordinates. */
551                 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
552                 uint32_t relative_x = client->rect.x - old_ws->rect.x,
553                          relative_y = client->rect.y - old_ws->rect.y;
554                 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
555                 client->rect.x = t_ws->rect.x + relative_x;
556                 client->rect.y = t_ws->rect.y + relative_y;
557                 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
558                 reposition_client(conn, client);
559                 xcb_flush(conn);
560         }
561
562         LOG("done\n");
563
564         render_layout(conn);
565
566         if (workspace_is_visible(t_ws))
567                 set_focus(conn, client, true);
568 }
569
570 /*
571  * Moves the currently selected window to the given workspace
572  *
573  */
574 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
575         LOG("Moving current window to workspace %d\n", workspace);
576
577         Container *container = CUR_CELL;
578
579         assert(container != NULL);
580
581         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
582         Workspace *t_ws = workspace_get(workspace-1);
583
584         Client *current_client = container->currently_focused;
585         if (current_client == NULL) {
586                 LOG("No currently focused client in current container.\n");
587                 return;
588         }
589         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
590         if (to_focus == NULL)
591                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
592
593         workspace_initialize(t_ws, container->workspace->screen);
594         /* Check if there is already a fullscreen client on the destination workspace and
595          * stop moving if so. */
596         if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
597                 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
598                 return;
599         }
600
601         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
602
603         assert(to_container != NULL);
604
605         client_remove_from_container(conn, current_client, container, true);
606         if (container->workspace->fullscreen_client == current_client)
607                 container->workspace->fullscreen_client = NULL;
608
609         /* TODO: insert it to the correct position */
610         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
611
612         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
613         LOG("Moved.\n");
614
615         current_client->container = to_container;
616         current_client->workspace = to_container->workspace;
617         container->currently_focused = to_focus;
618         to_container->currently_focused = current_client;
619
620         /* If we’re moving it to an invisible screen, we need to unmap it */
621         if (!workspace_is_visible(to_container->workspace)) {
622                 LOG("This workspace is not visible, unmapping\n");
623                 client_unmap(conn, current_client);
624         } else {
625                 if (current_client->fullscreen) {
626                         LOG("Calling client_enter_fullscreen again\n");
627                         client_enter_fullscreen(conn, current_client);
628                 }
629         }
630
631         /* delete all empty columns/rows */
632         cleanup_table(conn, container->workspace);
633
634         render_layout(conn);
635
636         if (workspace_is_visible(to_container->workspace))
637                 set_focus(conn, current_client, true);
638 }
639
640 /*
641  * Jumps to the given window class / title.
642  * Title is matched using strstr, that is, matches if it appears anywhere
643  * in the string. Regular expressions seem to be a bit overkill here. However,
644  * if we need them for something else somewhen, we may introduce them here, too.
645  *
646  */
647 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
648         char *classtitle;
649         Client *client;
650
651         /* The first character is a quote, this was checked before */
652         classtitle = sstrdup(arguments+1);
653         /* The last character is a quote, we just set it to NULL */
654         classtitle[strlen(classtitle)-1] = '\0';
655
656         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
657                 free(classtitle);
658                 LOG("No matching client found.\n");
659                 return;
660         }
661
662         free(classtitle);
663         set_focus(conn, client, true);
664 }
665
666 /*
667  * Jump directly to the specified workspace, row and col.
668  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
669  *
670  */
671 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
672         int ws, row, col;
673         int result;
674
675         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
676         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
677
678         /* No match? Either no arguments were specified, or no numbers */
679         if (result < 1) {
680                 LOG("At least one valid argument required\n");
681                 return;
682         }
683
684         /* Move to the target workspace */
685         workspace_show(conn, ws);
686
687         if (result < 3)
688                 return;
689
690         LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
691
692         /* Move to row/col */
693         if (row >= c_ws->rows)
694                 row = c_ws->rows - 1;
695         if (col >= c_ws->cols)
696                 col = c_ws->cols - 1;
697
698         LOG("Jumping to col %d, row %d\n", col, row);
699         if (c_ws->table[col][row]->currently_focused != NULL)
700                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
701 }
702
703 /*
704  * Travels the focus stack by the given number of times (or once, if no argument
705  * was specified). That is, selects the window you were in before you focused
706  * the current window.
707  *
708  * The special values 'floating' (select the next floating window), 'tiling'
709  * (select the next tiling window), 'ft' (if the current window is floating,
710  * select the next tiling window and vice-versa) are also valid
711  *
712  */
713 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
714         /* Start count at -1 to always skip the first element */
715         int times, count = -1;
716         Client *current;
717         bool floating_criteria;
718
719         /* Either it’s one of the special values… */
720         if (strcasecmp(arguments, "floating") == 0) {
721                 floating_criteria = true;
722         } else if (strcasecmp(arguments, "tiling") == 0) {
723                 floating_criteria = false;
724         } else if (strcasecmp(arguments, "ft") == 0) {
725                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
726                 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
727                         LOG("Cannot select the next floating/tiling client because there is no client at all\n");
728                         return;
729                 }
730
731                 floating_criteria = !client_is_floating(last_focused);
732         } else {
733                 /* …or a number was specified */
734                 if (sscanf(arguments, "%u", &times) != 1) {
735                         LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
736                         times = 1;
737                 }
738
739                 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
740                         if (++count < times) {
741                                 LOG("Skipping\n");
742                                 continue;
743                         }
744
745                         LOG("Focussing\n");
746                         set_focus(conn, current, true);
747                         break;
748                 }
749                 return;
750         }
751
752         /* Select the next client matching the criteria parsed above */
753         SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
754                 if (client_is_floating(current) == floating_criteria) {
755                         set_focus(conn, current, true);
756                         break;
757                 }
758 }
759
760 /*
761  * Goes through the list of arguments (for exec()) and checks if the given argument
762  * is present. If not, it copies the arguments (because we cannot realloc it) and
763  * appends the given argument.
764  *
765  */
766 static char **append_argument(char **original, char *argument) {
767         int num_args;
768         for (num_args = 0; original[num_args] != NULL; num_args++) {
769                 LOG("original argument: \"%s\"\n", original[num_args]);
770                 /* If the argument is already present we return the original pointer */
771                 if (strcmp(original[num_args], argument) == 0)
772                         return original;
773         }
774         /* Copy the original array */
775         char **result = smalloc((num_args+2) * sizeof(char*));
776         memcpy(result, original, num_args * sizeof(char*));
777         result[num_args] = argument;
778         result[num_args+1] = NULL;
779
780         return result;
781 }
782
783 /* 
784  * Switch to next or previous existing workspace
785  *
786  */
787 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
788         Workspace *ws = c_ws;
789
790         if (direction == 'n') {
791                 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
792                         if (ws->screen == NULL)
793                                 continue;
794
795                         workspace_show(conn, ws->num + 1);
796                         return;
797                 }
798         } else if (direction == 'p') {
799                 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
800                         if (ws->screen == NULL)
801                                 continue;
802
803                         workspace_show(conn, ws->num + 1);
804                         return;
805                 }
806         }
807 }
808
809 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
810         int first, second;
811         resize_orientation_t orientation = O_VERTICAL;
812         Container *con = last_focused->container;
813         Workspace *ws = con->workspace;
814
815         if (STARTS_WITH(command, "left")) {
816                 if (con->col == 0)
817                         return;
818                 first = con->col - 1;
819                 second = con->col;
820                 command += strlen("left");
821         } else if (STARTS_WITH(command, "right")) {
822                 first = con->col + (con->colspan - 1);
823                 LOG("column %d\n", first);
824
825                 if (!cell_exists(first, con->row) ||
826                     (first == (ws->cols-1)))
827                         return;
828
829                 second = first + 1;
830                 command += strlen("right");
831         } else if (STARTS_WITH(command, "top")) {
832                 if (con->row == 0)
833                         return;
834                 first = con->row - 1;
835                 second = con->row;
836                 orientation = O_HORIZONTAL;
837                 command += strlen("top");
838         } else if (STARTS_WITH(command, "bottom")) {
839                 first = con->row + (con->rowspan - 1);
840                 if (!cell_exists(con->col, first) ||
841                     (first == (ws->rows-1)))
842                         return;
843
844                 second = first + 1;
845                 orientation = O_HORIZONTAL;
846                 command += strlen("bottom");
847         } else {
848                 LOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
849                 return;
850         }
851
852         int pixels = atoi(command);
853         if (pixels == 0)
854                 return;
855
856         resize_container(conn, ws, first, second, orientation, pixels);
857 }
858
859 /*
860  * Parses a command, see file CMDMODE for more information
861  *
862  */
863 void parse_command(xcb_connection_t *conn, const char *command) {
864         LOG("--- parsing command \"%s\" ---\n", command);
865         /* Get the first client from focus stack because floating clients are not
866          * in any container, therefore CUR_CELL is not appropriate. */
867         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
868         if (last_focused == SLIST_END(&(c_ws->focus_stack)))
869                 last_focused = NULL;
870
871         /* Hmm, just to be sure */
872         if (command[0] == '\0')
873                 return;
874
875         /* Is it an <exec>? Then execute the given command. */
876         if (STARTS_WITH(command, "exec ")) {
877                 LOG("starting \"%s\"\n", command + strlen("exec "));
878                 start_application(command+strlen("exec "));
879                 return;
880         }
881
882         if (STARTS_WITH(command, "mark")) {
883                 if (last_focused == NULL) {
884                         LOG("There is no window to mark\n");
885                         return;
886                 }
887                 const char *rest = command + strlen("mark");
888                 while (*rest == ' ')
889                         rest++;
890                 if (*rest == '\0') {
891                         LOG("interactive mark starting\n");
892                         start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
893                 } else {
894                         LOG("mark with \"%s\"\n", rest);
895                         client_mark(conn, last_focused, rest);
896                 }
897                 return;
898         }
899
900         if (STARTS_WITH(command, "goto")) {
901                 const char *rest = command + strlen("goto");
902                 while (*rest == ' ')
903                         rest++;
904                 if (*rest == '\0') {
905                         LOG("interactive go to mark starting\n");
906                         start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
907                 } else {
908                         LOG("go to \"%s\"\n", rest);
909                         jump_to_mark(conn, rest);
910                 }
911                 return;
912         }
913
914         if (STARTS_WITH(command, "stack-limit ")) {
915                 if (last_focused == NULL || client_is_floating(last_focused)) {
916                         LOG("No container focused\n");
917                         return;
918                 }
919                 const char *rest = command + strlen("stack-limit ");
920                 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
921                         last_focused->container->stack_limit = STACK_LIMIT_ROWS;
922                         rest += strlen("rows ");
923                 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
924                         last_focused->container->stack_limit = STACK_LIMIT_COLS;
925                         rest += strlen("cols ");
926                 } else {
927                         LOG("Syntax: stack-limit <cols|rows> <limit>\n");
928                         return;
929                 }
930
931                 last_focused->container->stack_limit_value = atoi(rest);
932                 if (last_focused->container->stack_limit_value == 0)
933                         last_focused->container->stack_limit = STACK_LIMIT_NONE;
934
935                 return;
936         }
937
938         if (STARTS_WITH(command, "resize ")) {
939                 if (last_focused == NULL)
940                         return;
941                 const char *rest = command + strlen("resize ");
942                 parse_resize_command(conn, last_focused, rest);
943                 return;
944         }
945
946         if (STARTS_WITH(command, "mode ")) {
947                 const char *rest = command + strlen("mode ");
948                 switch_mode(conn, rest);
949                 return;
950         }
951
952         /* Is it an <exit>? */
953         if (STARTS_WITH(command, "exit")) {
954                 LOG("User issued exit-command, exiting without error.\n");
955                 exit(EXIT_SUCCESS);
956         }
957
958         /* Is it a <reload>? */
959         if (STARTS_WITH(command, "reload")) {
960                 load_configuration(conn, NULL, true);
961                 return;
962         }
963
964         /* Is it <restart>? Then restart in place. */
965         if (STARTS_WITH(command, "restart")) {
966                 LOG("restarting \"%s\"...\n", start_argv[0]);
967                 /* make sure -a is in the argument list or append it */
968                 start_argv = append_argument(start_argv, "-a");
969
970                 execvp(start_argv[0], start_argv);
971                 /* not reached */
972         }
973
974         if (STARTS_WITH(command, "kill")) {
975                 if (last_focused == NULL) {
976                         LOG("There is no window to kill\n");
977                         return;
978                 }
979
980                 LOG("Killing current window\n");
981                 client_kill(conn, last_focused);
982                 return;
983         }
984
985         /* Is it a jump to a specified workspace, row, col? */
986         if (STARTS_WITH(command, "jump ")) {
987                 const char *arguments = command + strlen("jump ");
988                 if (arguments[0] == '"')
989                         jump_to_window(conn, arguments);
990                 else jump_to_container(conn, arguments);
991                 return;
992         }
993
994         /* Should we travel the focus stack? */
995         if (STARTS_WITH(command, "focus")) {
996                 const char *arguments = command + strlen("focus ");
997                 travel_focus_stack(conn, arguments);
998                 return;
999         }
1000
1001         /* Is it 'f' for fullscreen? */
1002         if (command[0] == 'f') {
1003                 if (last_focused == NULL)
1004                         return;
1005                 client_toggle_fullscreen(conn, last_focused);
1006                 return;
1007         }
1008
1009         /* Is it just 's' for stacking or 'd' for default? */
1010         if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1011                 if (last_focused != NULL && client_is_floating(last_focused)) {
1012                         LOG("not switching, this is a floating client\n");
1013                         return;
1014                 }
1015                 LOG("Switching mode for current container\n");
1016                 int new_mode = MODE_DEFAULT;
1017                 if (command[0] == 's')
1018                         new_mode = MODE_STACK;
1019                 if (command[0] == 'T')
1020                         new_mode = MODE_TABBED;
1021                 switch_layout_mode(conn, CUR_CELL, new_mode);
1022                 return;
1023         }
1024
1025         /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1026         /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1027         if (command[0] == 'b') {
1028                 if (last_focused == NULL) {
1029                         LOG("No window focused, cannot change border type\n");
1030                         return;
1031                 }
1032
1033                 char com = command[1];
1034                 if (command[1] == 't') {
1035                         if (last_focused->titlebar_position == TITLEBAR_TOP &&
1036                             !last_focused->borderless)
1037                             com = 'p';
1038                         else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1039                                  !last_focused->borderless)
1040                             com = 'b';
1041                         else com = 'n';
1042                 }
1043
1044                 client_change_border(conn, last_focused, com);
1045                 return;
1046         }
1047
1048         if (command[0] == 'H') {
1049                 LOG("Hiding all floating windows\n");
1050                 floating_toggle_hide(conn, c_ws);
1051                 return;
1052         }
1053
1054         enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1055
1056         /* Is it a <with>? */
1057         if (command[0] == 'w') {
1058                 command++;
1059                 /* TODO: implement */
1060                 if (command[0] == 'c') {
1061                         with = WITH_CONTAINER;
1062                         command++;
1063                 } else if (command[0] == 'w') {
1064                         with = WITH_WORKSPACE;
1065                         command++;
1066                 } else if (command[0] == 's') {
1067                         with = WITH_SCREEN;
1068                         command++;
1069                 } else {
1070                         LOG("not yet implemented.\n");
1071                         return;
1072                 }
1073         }
1074
1075         /* Is it 't' for toggle tiling/floating? */
1076         if (command[0] == 't') {
1077                 if (with == WITH_WORKSPACE) {
1078                         c_ws->auto_float = !c_ws->auto_float;
1079                         LOG("autofloat is now %d\n", c_ws->auto_float);
1080                         return;
1081                 }
1082                 if (last_focused == NULL) {
1083                         LOG("Cannot toggle tiling/floating: workspace empty\n");
1084                         return;
1085                 }
1086
1087                 Workspace *ws = last_focused->workspace;
1088
1089                 toggle_floating_mode(conn, last_focused, false);
1090                 /* delete all empty columns/rows */
1091                 cleanup_table(conn, ws);
1092
1093                 /* Fix colspan/rowspan if it’d overlap */
1094                 fix_colrowspan(conn, ws);
1095
1096                 render_workspace(conn, ws->screen, ws);
1097
1098                 /* Re-focus the client because cleanup_table sets the focus to the last
1099                  * focused client inside a container only. */
1100                 set_focus(conn, last_focused, true);
1101
1102                 return;
1103         }
1104
1105         /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1106         if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1107                next_previous_workspace(conn, command[0]);
1108                return;
1109         }
1110
1111         /* It’s a normal <cmd> */
1112         char *rest = NULL;
1113         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1114         direction_t direction;
1115         int times = strtol(command, &rest, 10);
1116         if (rest == NULL) {
1117                 LOG("Invalid command (\"%s\")\n", command);
1118                 return;
1119         }
1120
1121         if (*rest == '\0') {
1122                 /* No rest? This was a workspace number, not a times specification */
1123                 workspace_show(conn, times);
1124                 return;
1125         }
1126
1127         if (*rest == 'm' || *rest == 's') {
1128                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1129                 rest++;
1130         }
1131
1132         int workspace = strtol(rest, &rest, 10);
1133
1134         if (rest == NULL) {
1135                 LOG("Invalid command (\"%s\")\n", command);
1136                 return;
1137         }
1138
1139         if (*rest == '\0') {
1140                 if (last_focused != NULL && client_is_floating(last_focused))
1141                         move_floating_window_to_workspace(conn, last_focused, workspace);
1142                 else move_current_window_to_workspace(conn, workspace);
1143                 return;
1144         }
1145
1146         if (last_focused == NULL) {
1147                 LOG("Not performing (no window found)\n");
1148                 return;
1149         }
1150
1151         if (client_is_floating(last_focused) &&
1152             (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1153                 LOG("Not performing (floating)\n");
1154                 return;
1155         }
1156
1157         /* Now perform action to <where> */
1158         while (*rest != '\0') {
1159                 if (*rest == 'h')
1160                         direction = D_LEFT;
1161                 else if (*rest == 'j')
1162                         direction = D_DOWN;
1163                 else if (*rest == 'k')
1164                         direction = D_UP;
1165                 else if (*rest == 'l')
1166                         direction = D_RIGHT;
1167                 else {
1168                         LOG("unknown direction: %c\n", *rest);
1169                         return;
1170                 }
1171                 rest++;
1172
1173                 if (action == ACTION_FOCUS) {
1174                         if (with == WITH_SCREEN) {
1175                                 focus_thing(conn, direction, THING_SCREEN);
1176                                 continue;
1177                         }
1178                         if (client_is_floating(last_focused)) {
1179                                 floating_focus_direction(conn, last_focused, direction);
1180                                 continue;
1181                         }
1182                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1183                         continue;
1184                 }
1185
1186                 if (action == ACTION_MOVE) {
1187                         if (with == WITH_SCREEN) {
1188                                 /* TODO: this should swap the screen’s contents
1189                                  * (e.g. all workspaces) with the next/previous/…
1190                                  * screen */
1191                                 LOG("Not yet implemented\n");
1192                                 continue;
1193                         }
1194                         if (client_is_floating(last_focused)) {
1195                                 floating_move(conn, last_focused, direction);
1196                                 continue;
1197                         }
1198                         if (with == WITH_WINDOW)
1199                                 move_current_window(conn, direction);
1200                         else move_current_container(conn, direction);
1201                         continue;
1202                 }
1203
1204                 if (action == ACTION_SNAP) {
1205                         if (with == WITH_SCREEN) {
1206                                 LOG("You cannot snap a screen (it makes no sense).\n");
1207                                 continue;
1208                         }
1209                         snap_current_container(conn, direction);
1210                         continue;
1211                 }
1212         }
1213 }