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