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