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