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