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