]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Bugfix: Set focus when table was shrinked and CUR_CELL might have been shrinked
[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
26 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
27                 direction_t direction) {
28         /* If this container is empty, we’re done */
29         if (container->currently_focused == NULL)
30                 return false;
31
32         Client *candidad = NULL;
33         if (direction == D_UP)
34                 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
35         else if (direction == D_DOWN)
36                 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
37         else printf("Direction not implemented!\n");
38
39         /* If we don’t have anything to select, we’re done */
40         if (candidad == CIRCLEQ_END(&(container->clients)))
41                 return false;
42
43         /* Set focus if we could successfully move */
44         set_focus(connection, candidad);
45
46         return true;
47 }
48
49 static void focus_window(xcb_connection_t *connection, direction_t direction) {
50         printf("focusing direction %d\n", direction);
51         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
52         if (direction == D_UP || direction == D_DOWN) {
53                 /* Let’s see if we can perform up/down focus in the current container */
54                 Container *container = CUR_CELL;
55
56                 /* There always is a container. If not, current_col or current_row is wrong */
57                 assert(container != NULL);
58
59                 if (focus_window_in_container(connection, container, direction))
60                         return;
61
62                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
63                         current_row++;
64                 else if (direction == D_UP && cell_exists(current_col, current_row-1))
65                         current_row--;
66         } else if (direction == D_LEFT || direction == D_RIGHT) {
67                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
68                         current_col++;
69                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
70                         current_col--;
71                 else {
72                         printf("nah, not possible\n");
73                         return;
74                 }
75         } else {
76                 printf("direction unhandled\n");
77                 return;
78         }
79
80         if (CUR_CELL->currently_focused != NULL)
81                 set_focus(connection, CUR_CELL->currently_focused);
82 }
83
84 /*
85  * Tries to move the window inside its current container.
86  *
87  * Returns true if the window could be moved, false otherwise.
88  *
89  */
90 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
91                 direction_t direction) {
92         assert(client->container != NULL);
93
94         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
95                                                 CIRCLEQ_NEXT(client, clients));
96
97         if (other == CIRCLEQ_END(&(client->container->clients)))
98                 return false;
99
100         printf("i can do that\n");
101         /* We can move the client inside its current container */
102         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
103         if (direction == D_UP)
104                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
105         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
106         render_layout(connection);
107         return true;
108 }
109
110 /*
111  * Moves the current window to the given direction, creating a column/row if
112  * necessary
113  *
114  */
115 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
116         printf("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
117                                         (direction == D_LEFT ? "left" : "right"))));
118         /* Get current window */
119         Container *container = CUR_CELL,
120                   *new = NULL;
121
122         /* There has to be a container, see focus_window() */
123         assert(container != NULL);
124
125         /* If there is no window or the dock window is focused, we’re done */
126         if (container->currently_focused == NULL ||
127             container->currently_focused->dock)
128                 return;
129
130         /* As soon as the client is moved away, the next client in the old
131          * container needs to get focus, if any. Therefore, we save it here. */
132         Client *current_client = container->currently_focused;
133         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
134         if (to_focus == NULL)
135                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
136
137         switch (direction) {
138                 case D_LEFT:
139                         if (current_col == 0)
140                                 return;
141
142                         new = CUR_TABLE[--current_col][current_row];
143                         break;
144                 case D_RIGHT:
145                         if (current_col == (c_ws->cols-1))
146                                 expand_table_cols(c_ws);
147
148                         new = CUR_TABLE[++current_col][current_row];
149                         break;
150                 case D_UP:
151                         /* TODO: if we’re at the up-most position, move the rest of the table down */
152                         if (move_current_window_in_container(connection, current_client, D_UP) ||
153                                 current_row == 0)
154                                 return;
155
156                         new = CUR_TABLE[current_col][--current_row];
157                         break;
158                 case D_DOWN:
159                         if (move_current_window_in_container(connection, current_client, D_DOWN))
160                                 return;
161
162                         if (current_row == (c_ws->rows-1))
163                                 expand_table_rows(c_ws);
164
165                         new = CUR_TABLE[current_col][++current_row];
166                         break;
167         }
168
169         /* Remove it from the old container and put it into the new one */
170         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
171         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
172
173         /* Update data structures */
174         current_client->container = new;
175         container->currently_focused = to_focus;
176         new->currently_focused = current_client;
177
178         /* delete all empty columns/rows */
179         cleanup_table(connection, container->workspace);
180         render_layout(connection);
181
182         set_focus(connection, current_client);
183 }
184
185 /*
186  * "Snaps" the current container (not possible for windows, because it works at table base)
187  * to the given direction, that is, adjusts cellspan/rowspan
188  *
189  */
190 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
191         printf("snapping container to direction %d\n", direction);
192
193         Container *container = CUR_CELL;
194
195         assert(container != NULL);
196
197         switch (direction) {
198                 case D_LEFT:
199                         /* Snap to the left is actually a move to the left and then a snap right */
200                         move_current_window(connection, D_LEFT);
201                         snap_current_container(connection, D_RIGHT);
202                         return;
203                 case D_RIGHT:
204                         /* Check if the cell is used */
205                         if (!cell_exists(container->col + 1, container->row) ||
206                                 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
207                                 printf("cannot snap to right - the cell is already used\n");
208                                 return;
209                         }
210
211                         /* Check if there are other cells with rowspan, which are in our way.
212                          * If so, reduce their rowspan. */
213                         for (int i = container->row-1; i >= 0; i--) {
214                                 printf("we got cell %d, %d with rowspan %d\n",
215                                                 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
216                                 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
217                                         CUR_TABLE[container->col+1][i]->rowspan--;
218                                 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
219                         }
220
221                         container->colspan++;
222                         break;
223                 case D_UP:
224                         move_current_window(connection, D_UP);
225                         snap_current_container(connection, D_DOWN);
226                         return;
227                 case D_DOWN:
228                         printf("snapping down\n");
229                         if (!cell_exists(container->col, container->row+1) ||
230                                 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
231                                 printf("cannot snap down - the cell is already used\n");
232                                 return;
233                         }
234
235                         for (int i = container->col-1; i >= 0; i--) {
236                                 printf("we got cell %d, %d with colspan %d\n",
237                                                 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
238                                 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
239                                         CUR_TABLE[i][container->row+1]->colspan--;
240                                 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
241
242                         }
243
244                         container->rowspan++;
245                         break;
246         }
247
248         render_layout(connection);
249 }
250
251 static void show_workspace(xcb_connection_t *conn, int workspace) {
252         Client *client;
253         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
254         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
255         Workspace *t_ws = &(workspaces[workspace-1]);
256
257         printf("show_workspace(%d)\n", workspace);
258
259         /* Store current_row/current_col */
260         c_ws->current_row = current_row;
261         c_ws->current_col = current_col;
262
263         /* Check if the workspace has not been used yet */
264         if (t_ws->screen == NULL) {
265                 printf("initializing new workspace, setting num to %d\n", workspace);
266                 t_ws->screen = c_ws->screen;
267                 /* Copy the dimensions from the virtual screen */
268                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
269         }
270
271         if (c_ws->screen != t_ws->screen) {
272                 /* We need to switch to the other screen first */
273                 printf("Just moving over to other screen.\n");
274                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
275                 current_col = c_ws->current_col;
276                 current_row = c_ws->current_row;
277                 if (CUR_CELL->currently_focused != NULL)
278                         warp_pointer_into(conn, CUR_CELL->currently_focused);
279                 else {
280                         Rect *dims = &(c_ws->screen->rect);
281                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
282                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
283                 }
284         }
285
286         /* Check if we need to change something or if we’re already there */
287         if (c_ws->screen->current_workspace == (workspace-1))
288                 return;
289
290         t_ws->screen->current_workspace = workspace-1;
291
292         /* TODO: does grabbing the server actually bring us any (speed)advantages? */
293         //xcb_grab_server(conn);
294
295         /* Unmap all clients of the current workspace */
296         for (int cols = 0; cols < c_ws->cols; cols++)
297                 for (int rows = 0; rows < c_ws->rows; rows++)
298                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
299                                 xcb_unmap_window(conn, client->frame);
300
301         /* Unmap the stack windows on the current workspace, if any */
302         struct Stack_Window *stack_win;
303         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
304                 if (stack_win->container->workspace == c_ws)
305                         xcb_unmap_window(conn, stack_win->window);
306
307         c_ws = &workspaces[workspace-1];
308         current_row = c_ws->current_row;
309         current_col = c_ws->current_col;
310         printf("new current row = %d, current col = %d\n", current_row, current_col);
311
312         /* Map all clients on the new workspace */
313         for (int cols = 0; cols < c_ws->cols; cols++)
314                 for (int rows = 0; rows < c_ws->rows; rows++)
315                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
316                                 xcb_map_window(conn, client->frame);
317
318         /* Map all stack windows, if any */
319         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
320                 if (stack_win->container->workspace == c_ws)
321                         xcb_map_window(conn, stack_win->window);
322
323         /* Restore focus on the new workspace */
324         if (CUR_CELL->currently_focused != NULL)
325                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
326         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
327
328         //xcb_ungrab_server(conn);
329
330         render_layout(conn);
331 }
332
333 /*
334  * Parses a command, see file CMDMODE for more information
335  *
336  */
337 void parse_command(xcb_connection_t *conn, const char *command) {
338         printf("--- parsing command \"%s\" ---\n", command);
339         /* Hmm, just to be sure */
340         if (command[0] == '\0')
341                 return;
342
343         /* Is it an <exec>? */
344         if (strncmp(command, "exec ", strlen("exec ")) == 0) {
345                 printf("starting \"%s\"\n", command + strlen("exec "));
346                 start_application(command+strlen("exec "));
347                 return;
348         }
349
350         /* Is it <restart>? */
351         if (strncmp(command, "restart", strlen("restart")) == 0) {
352                 printf("restarting \"%s\"...\n", application_path);
353                 execl(application_path, application_path, NULL);
354                 /* not reached */
355         }
356
357         /* Is it 'f' for fullscreen? */
358         if (command[0] == 'f') {
359                 if (CUR_CELL->currently_focused == NULL)
360                         return;
361                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
362                 return;
363         }
364
365         /* Is it just 's' for stacking or 'd' for default? */
366         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
367                 printf("Switching mode for current container\n");
368                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
369                 return;
370         }
371
372         /* Is it a <with>? */
373         if (command[0] == 'w') {
374                 /* TODO: implement */
375                 printf("not yet implemented.\n");
376                 return;
377         }
378
379         /* It's a normal <cmd> */
380         int times;
381         char *rest = NULL;
382         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
383         direction_t direction;
384         times = strtol(command, &rest, 10);
385         if (rest == NULL) {
386                 printf("Invalid command (\"%s\")\n", command);
387                 return;
388         }
389         if (*rest == 'm' || *rest == 's') {
390                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
391                 rest++;
392         }
393
394         if (*rest == '\0') {
395                 /* No rest? This was a tag number, not a times specification */
396                 show_workspace(conn, times);
397                 return;
398         }
399
400         /* Now perform action to <where> */
401         while (*rest != '\0') {
402                 if (*rest == 'h')
403                         direction = D_LEFT;
404                 else if (*rest == 'j')
405                         direction = D_DOWN;
406                 else if (*rest == 'k')
407                         direction = D_UP;
408                 else if (*rest == 'l')
409                         direction = D_RIGHT;
410                 else {
411                         printf("unknown direction: %c\n", *rest);
412                         return;
413                 }
414
415                 if (action == ACTION_FOCUS)
416                         focus_window(conn, direction);
417                 else if (action == ACTION_MOVE)
418                         move_current_window(conn, direction);
419                 else if (action == ACTION_SNAP)
420                         snap_current_container(conn, direction);
421
422                 rest++;
423         }
424
425         printf("--- done ---\n");
426 }