]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Bugfix: Rendering of colspan/rowspan was wrong
[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
27 static bool focus_window_in_container(xcb_connection_t *conn, Container *container,
28                 direction_t direction) {
29         /* If this container is empty, we’re done */
30         if (container->currently_focused == NULL)
31                 return false;
32
33         /* Get the previous/next client or wrap around */
34         Client *candidate = NULL;
35         if (direction == D_UP) {
36                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
37                         candidate = CIRCLEQ_LAST(&(container->clients));
38         }
39         else if (direction == D_DOWN) {
40                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
41                         candidate = CIRCLEQ_FIRST(&(container->clients));
42         } else printf("Direction not implemented!\n");
43
44         /* Set focus */
45         set_focus(conn, candidate);
46
47         return true;
48 }
49
50 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
51
52 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
53         printf("focusing direction %d\n", direction);
54
55         int new_row = current_row,
56             new_col = current_col;
57
58         Container *container = CUR_CELL;
59         Workspace *t_ws = c_ws;
60
61         /* There always is a container. If not, current_col or current_row is wrong */
62         assert(container != NULL);
63
64         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
65         if (direction == D_UP || direction == D_DOWN) {
66                 if (thing == THING_WINDOW)
67                         /* Let’s see if we can perform up/down focus in the current container */
68                         if (focus_window_in_container(conn, container, direction))
69                                 return;
70
71                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
72                         new_row++;
73                 else if (direction == D_UP && cell_exists(current_col, current_row-1))
74                         new_row--;
75                 else {
76                         /* Let’s see if there is a screen down/up there to which we can switch */
77                         printf("container is at %d with height %d\n", container->y, container->height);
78                         i3Screen *screen;
79                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
80                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
81                                 printf("Not possible, no screen found.\n");
82                                 return;
83                         }
84                         t_ws = &(workspaces[screen->current_workspace]);
85                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
86                         /* Bounds checking */
87                         if (new_col >= t_ws->cols)
88                                 new_col = (t_ws->cols - 1);
89                         if (new_row >= t_ws->rows)
90                                 new_row = (t_ws->rows - 1);
91                 }
92         } else if (direction == D_LEFT || direction == D_RIGHT) {
93                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
94                         new_col++;
95                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
96                         new_col--;
97                 else {
98                         /* Let’s see if there is a screen left/right here to which we can switch */
99                         printf("container is at %d with width %d\n", container->x, container->width);
100                         i3Screen *screen;
101                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
102                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
103                                 printf("Not possible, no screen found.\n");
104                                 return;
105                         }
106                         t_ws = &(workspaces[screen->current_workspace]);
107                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
108                         /* Bounds checking */
109                         if (new_col >= t_ws->cols)
110                                 new_col = (t_ws->cols - 1);
111                         if (new_row >= t_ws->rows)
112                                 new_row = (t_ws->rows - 1);
113                 }
114         } else {
115                 printf("direction unhandled\n");
116                 return;
117         }
118
119         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
120                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused);
121 }
122
123 /*
124  * Tries to move the window inside its current container.
125  *
126  * Returns true if the window could be moved, false otherwise.
127  *
128  */
129 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
130                 direction_t direction) {
131         assert(client->container != NULL);
132
133         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
134                                              CIRCLEQ_NEXT(client, clients));
135
136         if (other == CIRCLEQ_END(&(client->container->clients)))
137                 return false;
138
139         printf("i can do that\n");
140         /* We can move the client inside its current container */
141         /* TODO: needs wrapping */
142         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
143         if (direction == D_UP)
144                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
145         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
146         render_layout(conn);
147         return true;
148 }
149
150 /*
151  * Moves the current window to the given direction, creating a column/row if
152  * necessary
153  *
154  */
155 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
156         printf("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
157                                         (direction == D_LEFT ? "left" : "right"))));
158         /* Get current window */
159         Container *container = CUR_CELL,
160                   *new = NULL;
161
162         /* There has to be a container, see focus_window() */
163         assert(container != NULL);
164
165         /* If there is no window or the dock window is focused, we’re done */
166         if (container->currently_focused == NULL ||
167             container->currently_focused->dock)
168                 return;
169
170         /* As soon as the client is moved away, the next client in the old
171          * container needs to get focus, if any. Therefore, we save it here. */
172         Client *current_client = container->currently_focused;
173         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
174         if (to_focus == NULL)
175                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
176
177         switch (direction) {
178                 case D_LEFT:
179                         /* TODO: If we’re at the left-most position, move the rest of the table right */
180                         if (current_col == 0)
181                                 return;
182
183                         new = CUR_TABLE[--current_col][current_row];
184                         break;
185                 case D_RIGHT:
186                         if (current_col == (c_ws->cols-1))
187                                 expand_table_cols(c_ws);
188
189                         new = CUR_TABLE[++current_col][current_row];
190                         break;
191                 case D_UP:
192                         /* TODO: if we’re at the up-most position, move the rest of the table down */
193                         if (move_current_window_in_container(conn, current_client, D_UP) ||
194                                 current_row == 0)
195                                 return;
196
197                         new = CUR_TABLE[current_col][--current_row];
198                         break;
199                 case D_DOWN:
200                         if (move_current_window_in_container(conn, current_client, D_DOWN))
201                                 return;
202
203                         if (current_row == (c_ws->rows-1))
204                                 expand_table_rows(c_ws);
205
206                         new = CUR_TABLE[current_col][++current_row];
207                         break;
208         }
209
210         /* Remove it from the old container and put it into the new one */
211         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
212         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
213
214         /* Update data structures */
215         current_client->container = new;
216         container->currently_focused = to_focus;
217         new->currently_focused = current_client;
218
219         /* delete all empty columns/rows */
220         cleanup_table(conn, container->workspace);
221
222         /* Fix colspan/rowspan if it’d overlap */
223         fix_colrowspan(conn, container->workspace);
224
225         render_layout(conn);
226
227         set_focus(conn, current_client);
228 }
229
230 /*
231  * "Snaps" the current container (not possible for windows, because it works at table base)
232  * to the given direction, that is, adjusts cellspan/rowspan
233  *
234  */
235 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
236         printf("snapping container to direction %d\n", direction);
237
238         Container *container = CUR_CELL;
239
240         assert(container != NULL);
241
242         switch (direction) {
243                 case D_LEFT:
244                         /* Snap to the left is actually a move to the left and then a snap right */
245                         if (!cell_exists(container->col - 1, container->row) ||
246                                 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
247                                 printf("cannot snap to right - the cell is already used\n");
248                                 return;
249                         }
250
251                         move_current_window(conn, D_LEFT);
252                         snap_current_container(conn, D_RIGHT);
253                         return;
254                 case D_RIGHT:
255                         /* Check if the cell is used */
256                         if (!cell_exists(container->col + 1, container->row) ||
257                                 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
258                                 printf("cannot snap to right - the cell is already used\n");
259                                 return;
260                         }
261
262                         /* Check if there are other cells with rowspan, which are in our way.
263                          * If so, reduce their rowspan. */
264                         for (int i = container->row-1; i >= 0; i--) {
265                                 printf("we got cell %d, %d with rowspan %d\n",
266                                                 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
267                                 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
268                                         CUR_TABLE[container->col+1][i]->rowspan--;
269                                 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
270                         }
271
272                         container->colspan++;
273                         break;
274                 case D_UP:
275                         move_current_window(conn, D_UP);
276                         snap_current_container(conn, D_DOWN);
277                         return;
278                 case D_DOWN:
279                         printf("snapping down\n");
280                         if (!cell_exists(container->col, container->row+1) ||
281                                 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
282                                 printf("cannot snap down - the cell is already used\n");
283                                 return;
284                         }
285
286                         for (int i = container->col-1; i >= 0; i--) {
287                                 printf("we got cell %d, %d with colspan %d\n",
288                                                 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
289                                 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
290                                         CUR_TABLE[i][container->row+1]->colspan--;
291                                 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
292
293                         }
294
295                         container->rowspan++;
296                         break;
297         }
298
299         render_layout(conn);
300 }
301
302 /*
303  * Moves the currently selected window to the given workspace
304  *
305  */
306 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
307         printf("Moving current window to workspace %d\n", workspace);
308
309         Container *container = CUR_CELL;
310
311         assert(container != NULL);
312
313         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
314         Workspace *t_ws = &(workspaces[workspace-1]);
315
316         Client *current_client = container->currently_focused;
317         if (current_client == NULL) {
318                 printf("No currently focused client in current container.\n");
319                 return;
320         }
321         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
322         if (to_focus == NULL)
323                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
324
325         if (t_ws->screen == NULL) {
326                 printf("initializing new workspace, setting num to %d\n", workspace-1);
327                 t_ws->screen = container->workspace->screen;
328                 /* Copy the dimensions from the virtual screen */
329                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
330         }
331
332         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
333
334         assert(to_container != NULL);
335
336         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
337         SLIST_REMOVE(&(container->workspace->focus_stack), current_client, Client, focus_clients);
338
339         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
340         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
341         printf("Moved.\n");
342
343         current_client->container = to_container;
344         container->currently_focused = to_focus;
345         to_container->currently_focused = current_client;
346
347         /* If we’re moving it to an invisible screen, we need to unmap it */
348         if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
349                 printf("This workspace is not visible, unmapping\n");
350                 xcb_unmap_window(conn, current_client->frame);
351         }
352
353         /* delete all empty columns/rows */
354         cleanup_table(conn, container->workspace);
355
356         render_layout(conn);
357 }
358
359 static void show_workspace(xcb_connection_t *conn, int workspace) {
360         Client *client;
361         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
362         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
363         Workspace *t_ws = &(workspaces[workspace-1]);
364
365         printf("show_workspace(%d)\n", workspace);
366
367         /* Store current_row/current_col */
368         c_ws->current_row = current_row;
369         c_ws->current_col = current_col;
370
371         /* Check if the workspace has not been used yet */
372         if (t_ws->screen == NULL) {
373                 printf("initializing new workspace, setting num to %d\n", workspace);
374                 t_ws->screen = c_ws->screen;
375                 /* Copy the dimensions from the virtual screen */
376                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
377         }
378
379         if (c_ws->screen != t_ws->screen) {
380                 /* We need to switch to the other screen first */
381                 printf("moving over to other screen.\n");
382                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
383                 current_col = c_ws->current_col;
384                 current_row = c_ws->current_row;
385                 if (CUR_CELL->currently_focused != NULL)
386                         warp_pointer_into(conn, CUR_CELL->currently_focused);
387                 else {
388                         Rect *dims = &(c_ws->screen->rect);
389                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
390                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
391                 }
392         }
393
394         /* Check if we need to change something or if we’re already there */
395         if (c_ws->screen->current_workspace == (workspace-1))
396                 return;
397
398         t_ws->screen->current_workspace = workspace-1;
399
400         /* TODO: does grabbing the server actually bring us any (speed)advantages? */
401         //xcb_grab_server(conn);
402
403         /* Unmap all clients of the current workspace */
404         int unmapped_clients = 0;
405         for (int cols = 0; cols < c_ws->cols; cols++)
406                 for (int rows = 0; rows < c_ws->rows; rows++)
407                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
408                                 xcb_unmap_window(conn, client->frame);
409                                 unmapped_clients++;
410                         }
411
412         /* If we did not unmap any clients, the workspace is empty and we can destroy it */
413         if (unmapped_clients == 0)
414                 c_ws->screen = NULL;
415
416         /* Unmap the stack windows on the current workspace, if any */
417         struct Stack_Window *stack_win;
418         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
419                 if (stack_win->container->workspace == c_ws)
420                         xcb_unmap_window(conn, stack_win->window);
421
422         c_ws = &workspaces[workspace-1];
423         current_row = c_ws->current_row;
424         current_col = c_ws->current_col;
425         printf("new current row = %d, current col = %d\n", current_row, current_col);
426
427         /* Map all clients on the new workspace */
428         for (int cols = 0; cols < c_ws->cols; cols++)
429                 for (int rows = 0; rows < c_ws->rows; rows++)
430                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
431                                 xcb_map_window(conn, client->frame);
432
433         /* Map all stack windows, if any */
434         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
435                 if (stack_win->container->workspace == c_ws)
436                         xcb_map_window(conn, stack_win->window);
437
438         /* Restore focus on the new workspace */
439         if (CUR_CELL->currently_focused != NULL)
440                 set_focus(conn, CUR_CELL->currently_focused);
441         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
442
443         //xcb_ungrab_server(conn);
444
445         render_layout(conn);
446 }
447
448 /*
449  * Parses a command, see file CMDMODE for more information
450  *
451  */
452 void parse_command(xcb_connection_t *conn, const char *command) {
453         printf("--- parsing command \"%s\" ---\n", command);
454         /* Hmm, just to be sure */
455         if (command[0] == '\0')
456                 return;
457
458         /* Is it an <exec>? */
459         if (strncmp(command, "exec ", strlen("exec ")) == 0) {
460                 printf("starting \"%s\"\n", command + strlen("exec "));
461                 start_application(command+strlen("exec "));
462                 return;
463         }
464
465         /* Is it <restart>? */
466         if (strncmp(command, "restart", strlen("restart")) == 0) {
467                 printf("restarting \"%s\"...\n", application_path);
468                 execl(application_path, application_path, NULL);
469                 /* not reached */
470         }
471
472         /* Is it 'f' for fullscreen? */
473         if (command[0] == 'f') {
474                 if (CUR_CELL->currently_focused == NULL)
475                         return;
476                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
477                 return;
478         }
479
480         /* Is it just 's' for stacking or 'd' for default? */
481         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
482                 printf("Switching mode for current container\n");
483                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
484                 return;
485         }
486
487         enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
488
489         /* Is it a <with>? */
490         if (command[0] == 'w') {
491                 command++;
492                 /* TODO: implement */
493                 if (command[0] == 'c') {
494                         with = WITH_CONTAINER;
495                         command++;
496                 } else {
497                         printf("not yet implemented.\n");
498                         return;
499                 }
500         }
501
502         /* It's a normal <cmd> */
503         char *rest = NULL;
504         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
505         direction_t direction;
506         int times = strtol(command, &rest, 10);
507         if (rest == NULL) {
508                 printf("Invalid command (\"%s\")\n", command);
509                 return;
510         }
511
512         if (*rest == '\0') {
513                 /* No rest? This was a tag number, not a times specification */
514                 show_workspace(conn, times);
515                 return;
516         }
517
518         if (*rest == 'm' || *rest == 's') {
519                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
520                 rest++;
521         }
522
523         int workspace = strtol(rest, &rest, 10);
524
525         if (rest == NULL) {
526                 printf("Invalid command (\"%s\")\n", command);
527                 return;
528         }
529
530         if (*rest == '\0') {
531                 move_current_window_to_workspace(conn, workspace);
532                 return;
533         }
534
535         /* Now perform action to <where> */
536         while (*rest != '\0') {
537                 if (*rest == 'h')
538                         direction = D_LEFT;
539                 else if (*rest == 'j')
540                         direction = D_DOWN;
541                 else if (*rest == 'k')
542                         direction = D_UP;
543                 else if (*rest == 'l')
544                         direction = D_RIGHT;
545                 else {
546                         printf("unknown direction: %c\n", *rest);
547                         return;
548                 }
549
550                 if (action == ACTION_FOCUS) {
551                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
552                 }
553                 else if (action == ACTION_MOVE)
554                         move_current_window(conn, direction);
555                 else if (action == ACTION_SNAP)
556                         snap_current_container(conn, direction);
557
558                 rest++;
559         }
560
561         printf("--- done ---\n");
562 }