]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Add vim hints, copyright notice to each file, add LICENSE, retab! everything
[i3/i3] / src / commands.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * (c) 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;
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
38         /* If we don’t have anything to select, we’re done */
39         if (candidad == CIRCLEQ_END(&(container->clients)))
40                 return false;
41
42         /* Set focus if we could successfully move */
43         container->currently_focused = candidad;
44         xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE, candidad->child, XCB_CURRENT_TIME);
45         render_layout(connection);
46
47         return true;
48 }
49
50 static void focus_window(xcb_connection_t *connection, direction_t direction) {
51         printf("focusing direction %d\n", direction);
52         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
53         if (direction == D_UP || direction == D_DOWN) {
54                 /* Let’s see if we can perform up/down focus in the current container */
55                 Container *container = CUR_CELL;
56
57                 /* There always is a container. If not, current_col or current_row is wrong */
58                 assert(container != NULL);
59
60                 if (focus_window_in_container(connection, container, direction))
61                         return;
62         } else if (direction == D_LEFT || direction == D_RIGHT) {
63                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
64                         current_col++;
65                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
66                         current_col--;
67                 else {
68                         printf("nah, not possible\n");
69                         return;
70                 }
71                 if (CUR_CELL->currently_focused != NULL) {
72                         xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE,
73                                         CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
74                         render_layout(connection);
75                 }
76
77         } else {
78                 printf("direction unhandled\n");
79         }
80 }
81
82 /*
83  * Tries to move the window inside its current container.
84  *
85  * Returns true if the window could be moved, false otherwise.
86  *
87  */
88 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
89                 direction_t direction) {
90         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
91                                                 CIRCLEQ_NEXT(client, clients));
92
93         if (other == CIRCLEQ_END(&(client->container->clients)))
94                 return false;
95
96         printf("i can do that\n");
97         /* We can move the client inside its current container */
98         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
99         if (direction == D_UP)
100                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
101         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
102         render_layout(connection);
103         return true;
104 }
105
106 /*
107  * Moves the current window to the given direction, creating a column/row if
108  * necessary
109  *
110  */
111 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
112         printf("moving window to direction %d\n", direction);
113         /* Get current window */
114         Container *container = CUR_CELL,
115                   *new;
116
117         /* There has to be a container, see focus_window() */
118         assert(container != NULL);
119
120         /* If there is no window, we’re done */
121         if (container->currently_focused == NULL)
122                 return;
123
124         /* As soon as the client is moved away, the next client in the old
125          * container needs to get focus, if any. Therefore, we save it here. */
126         Client *current_client = container->currently_focused;
127         Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
128         if (to_focus == CIRCLEQ_END(&(container->clients)))
129                 to_focus = NULL;
130
131         switch (direction) {
132                 case D_LEFT:
133                         if (current_col == 0)
134                                 return;
135
136                         new = CUR_TABLE[--current_col][current_row];
137                         break;
138                 case D_RIGHT:
139                         if (current_col == (c_ws->cols-1))
140                                 expand_table_cols(c_ws);
141
142                         new = CUR_TABLE[++current_col][current_row];
143                         break;
144                 case D_UP:
145                         /* TODO: if we’re at the up-most position, move the rest of the table down */
146                         if (move_current_window_in_container(connection, current_client, D_UP) ||
147                                 current_row == 0)
148                                 return;
149
150                         new = CUR_TABLE[current_col][--current_row];
151                         break;
152                 case D_DOWN:
153                         if (move_current_window_in_container(connection, current_client, D_DOWN))
154                                 return;
155
156                         if (current_row == (c_ws->rows-1))
157                                 expand_table_rows(c_ws);
158
159                         new = CUR_TABLE[current_col][++current_row];
160                         break;
161         }
162
163         /* Remove it from the old container and put it into the new one */
164         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
165         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
166
167         /* Update data structures */
168         current_client->container = new;
169         container->currently_focused = to_focus;
170         new->currently_focused = current_client;
171
172         /* TODO: delete all empty columns/rows */
173
174         render_layout(connection);
175 }
176
177 /*
178  * "Snaps" the current container (not possible for windows, because it works at table base)
179  * to the given direction, that is, adjusts cellspan/rowspan
180  *
181  */
182 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
183         printf("snapping container to direction %d\n", direction);
184
185         Container *container = CUR_CELL;
186         int i;
187
188         assert(container != NULL);
189
190         switch (direction) {
191                 case D_LEFT:
192                         /* Snap to the left is actually a move to the left and then a snap right */
193                         move_current_window(connection, D_LEFT);
194                         snap_current_container(connection, D_RIGHT);
195                         return;
196                 case D_RIGHT:
197                         /* Check if the cell is used */
198                         if (!cell_exists(container->col + 1, container->row) ||
199                                 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
200                                 printf("cannot snap to right - the cell is already used\n");
201                                 return;
202                         }
203
204                         /* Check if there are other cells with rowspan, which are in our way.
205                          * If so, reduce their rowspan. */
206                         for (i = container->row-1; i >= 0; i--) {
207                                 printf("we got cell %d, %d with rowspan %d\n",
208                                                 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
209                                 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
210                                         CUR_TABLE[container->col+1][i]->rowspan--;
211                                 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
212                         }
213
214                         container->colspan++;
215                         break;
216                 case D_UP:
217                         move_current_window(connection, D_UP);
218                         snap_current_container(connection, D_DOWN);
219                         return;
220                 case D_DOWN:
221                         printf("snapping down\n");
222                         if (!cell_exists(container->col, container->row+1) ||
223                                 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
224                                 printf("cannot snap down - the cell is already used\n");
225                                 return;
226                         }
227
228                         for (i = container->col-1; i >= 0; i--) {
229                                 printf("we got cell %d, %d with colspan %d\n",
230                                                 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
231                                 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
232                                         CUR_TABLE[i][container->row+1]->colspan--;
233                                 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
234
235                         }
236
237                         container->rowspan++;
238                         break;
239         }
240
241         render_layout(connection);
242 }
243
244 static void show_workspace(xcb_connection_t *conn, int workspace) {
245         int cols, rows;
246         Client *client;
247         printf("show_workspace(%d)\n", workspace);
248
249         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
250
251         /* Store current_row/current_col */
252         c_ws->current_row = current_row;
253         c_ws->current_col = current_col;
254
255         /* TODO: does grabbing the server actually bring us any (speed)advantages? */
256         //xcb_grab_server(conn);
257
258         /* Unmap all clients */
259         for (cols = 0; cols < c_ws->cols; cols++)
260                 for (rows = 0; rows < c_ws->rows; rows++) {
261                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
262                                 xcb_unmap_window(conn, client->frame);
263                 }
264
265         c_ws = &workspaces[workspace-1];
266         current_row = c_ws->current_row;
267         current_col = c_ws->current_col;
268         printf("new current row = %d, current col = %d\n", current_row, current_col);
269
270         /* Map all clients on the new workspace */
271         for (cols = 0; cols < c_ws->cols; cols++)
272                 for (rows = 0; rows < c_ws->rows; rows++) {
273                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
274                                 xcb_map_window(conn, client->frame);
275                 }
276
277         /* Restore focus on the new workspace */
278         if (CUR_CELL->currently_focused != NULL)
279                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
280         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, root, XCB_CURRENT_TIME);
281
282         //xcb_ungrab_server(conn);
283
284         render_layout(conn);
285 }
286
287 /*
288  * Parses a command, see file CMDMODE for more information
289  *
290  */
291 void parse_command(xcb_connection_t *conn, const char *command) {
292         printf("--- parsing command \"%s\" ---\n", command);
293         /* Hmm, just to be sure */
294         if (command[0] == '\0')
295                 return;
296
297         /* Is it an <exec>? */
298         if (strncmp(command, "exec ", strlen("exec ")) == 0) {
299                 printf("starting \"%s\"\n", command + strlen("exec "));
300                 start_application(command+strlen("exec "));
301                 return;
302         }
303
304         /* Is it a <with>? */
305         if (command[0] == 'w') {
306                 /* TODO: implement */
307                 printf("not yet implemented.\n");
308                 return;
309         }
310
311         /* It's a normal <cmd> */
312         int times;
313         char *rest = NULL;
314         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
315         direction_t direction;
316         times = strtol(command, &rest, 10);
317         if (rest == NULL) {
318                 printf("Invalid command: Consists only of a movement\n");
319                 return;
320         }
321         if (*rest == 'm' || *rest == 's') {
322                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
323                 rest++;
324         }
325
326         if (*rest == '\0') {
327                 /* No rest? This was a tag number, not a times specification */
328                 show_workspace(conn, times);
329                 return;
330         }
331
332         /* Now perform action to <where> */
333         while (*rest != '\0') {
334                 if (*rest == 'h')
335                         direction = D_LEFT;
336                 else if (*rest == 'j')
337                         direction = D_DOWN;
338                 else if (*rest == 'k')
339                         direction = D_UP;
340                 else if (*rest == 'l')
341                         direction = D_RIGHT;
342                 else {
343                         printf("unknown direction: %c\n", *rest);
344                         return;
345                 }
346
347                 if (action == ACTION_FOCUS)
348                         focus_window(conn, direction);
349                 else if (action == ACTION_MOVE)
350                         move_current_window(conn, direction);
351                 else if (action == ACTION_SNAP)
352                         snap_current_container(conn, direction);
353
354                 rest++;
355         }
356
357         printf("--- done ---\n");
358 }