4 * i3 - an improved dynamic tiling window manager
6 * (c) 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
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)
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);
38 /* If we don’t have anything to select, we’re done */
39 if (candidad == CIRCLEQ_END(&(container->clients)))
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);
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;
57 /* There always is a container. If not, current_col or current_row is wrong */
58 assert(container != NULL);
60 if (focus_window_in_container(connection, container, direction))
62 } else if (direction == D_LEFT || direction == D_RIGHT) {
63 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
65 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
68 printf("nah, not possible\n");
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);
78 printf("direction unhandled\n");
83 * Tries to move the window inside its current container.
85 * Returns true if the window could be moved, false otherwise.
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));
93 if (other == CIRCLEQ_END(&(client->container->clients)))
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);
107 * Moves the current window to the given direction, creating a column/row if
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,
117 /* There has to be a container, see focus_window() */
118 assert(container != NULL);
120 /* If there is no window, we’re done */
121 if (container->currently_focused == NULL)
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)))
133 if (current_col == 0)
136 new = CUR_TABLE[--current_col][current_row];
139 if (current_col == (c_ws->cols-1))
140 expand_table_cols(c_ws);
142 new = CUR_TABLE[++current_col][current_row];
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) ||
150 new = CUR_TABLE[current_col][--current_row];
153 if (move_current_window_in_container(connection, current_client, D_DOWN))
156 if (current_row == (c_ws->rows-1))
157 expand_table_rows(c_ws);
159 new = CUR_TABLE[current_col][++current_row];
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);
167 /* Update data structures */
168 current_client->container = new;
169 container->currently_focused = to_focus;
170 new->currently_focused = current_client;
172 /* TODO: delete all empty columns/rows */
174 render_layout(connection);
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
182 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
183 printf("snapping container to direction %d\n", direction);
185 Container *container = CUR_CELL;
188 assert(container != NULL);
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);
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");
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);
214 container->colspan++;
217 move_current_window(connection, D_UP);
218 snap_current_container(connection, 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");
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);
237 container->rowspan++;
241 render_layout(connection);
244 static void show_workspace(xcb_connection_t *conn, int workspace) {
247 printf("show_workspace(%d)\n", workspace);
249 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
251 /* Store current_row/current_col */
252 c_ws->current_row = current_row;
253 c_ws->current_col = current_col;
255 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
256 //xcb_grab_server(conn);
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);
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);
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);
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);
282 //xcb_ungrab_server(conn);
288 * Parses a command, see file CMDMODE for more information
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')
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 "));
304 /* Is it a <with>? */
305 if (command[0] == 'w') {
306 /* TODO: implement */
307 printf("not yet implemented.\n");
311 /* It's a normal <cmd> */
314 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
315 direction_t direction;
316 times = strtol(command, &rest, 10);
318 printf("Invalid command: Consists only of a movement\n");
321 if (*rest == 'm' || *rest == 's') {
322 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
327 /* No rest? This was a tag number, not a times specification */
328 show_workspace(conn, times);
332 /* Now perform action to <where> */
333 while (*rest != '\0') {
336 else if (*rest == 'j')
338 else if (*rest == 'k')
340 else if (*rest == 'l')
343 printf("unknown direction: %c\n", *rest);
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);
357 printf("--- done ---\n");