4 * i3 - an improved dynamic tiling window manager
6 * © 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)
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");
39 /* If we don’t have anything to select, we’re done */
40 if (candidad == CIRCLEQ_END(&(container->clients)))
43 /* Set focus if we could successfully move */
44 set_focus(connection, candidad);
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;
56 /* There always is a container. If not, current_col or current_row is wrong */
57 assert(container != NULL);
59 if (focus_window_in_container(connection, container, direction))
62 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
64 else if (direction == D_UP && cell_exists(current_col, current_row-1))
66 } else if (direction == D_LEFT || direction == D_RIGHT) {
67 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
69 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
72 printf("nah, not possible\n");
76 printf("direction unhandled\n");
80 if (CUR_CELL->currently_focused != NULL)
81 set_focus(connection, CUR_CELL->currently_focused);
85 * Tries to move the window inside its current container.
87 * Returns true if the window could be moved, false otherwise.
90 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
91 direction_t direction) {
92 assert(client->container != NULL);
94 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
95 CIRCLEQ_NEXT(client, clients));
97 if (other == CIRCLEQ_END(&(client->container->clients)))
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);
111 * Moves the current window to the given direction, creating a column/row if
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,
122 /* There has to be a container, see focus_window() */
123 assert(container != NULL);
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)
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);
139 if (current_col == 0)
142 new = CUR_TABLE[--current_col][current_row];
145 if (current_col == (c_ws->cols-1))
146 expand_table_cols(c_ws);
148 new = CUR_TABLE[++current_col][current_row];
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) ||
156 new = CUR_TABLE[current_col][--current_row];
159 if (move_current_window_in_container(connection, current_client, D_DOWN))
162 if (current_row == (c_ws->rows-1))
163 expand_table_rows(c_ws);
165 new = CUR_TABLE[current_col][++current_row];
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);
173 /* Update data structures */
174 current_client->container = new;
175 container->currently_focused = to_focus;
176 new->currently_focused = current_client;
178 /* delete all empty columns/rows */
179 cleanup_table(connection, container->workspace);
180 render_layout(connection);
182 set_focus(connection, current_client);
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
190 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
191 printf("snapping container to direction %d\n", direction);
193 Container *container = CUR_CELL;
195 assert(container != NULL);
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);
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");
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);
221 container->colspan++;
224 move_current_window(connection, D_UP);
225 snap_current_container(connection, 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");
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);
244 container->rowspan++;
248 render_layout(connection);
251 static void show_workspace(xcb_connection_t *conn, int workspace) {
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]);
257 printf("show_workspace(%d)\n", workspace);
259 /* Store current_row/current_col */
260 c_ws->current_row = current_row;
261 c_ws->current_col = current_col;
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));
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);
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));
286 /* Check if we need to change something or if we’re already there */
287 if (c_ws->screen->current_workspace == (workspace-1))
290 t_ws->screen->current_workspace = workspace-1;
292 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
293 //xcb_grab_server(conn);
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);
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);
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);
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);
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);
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);
328 //xcb_ungrab_server(conn);
334 * Parses a command, see file CMDMODE for more information
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')
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 "));
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);
357 /* Is it 'f' for fullscreen? */
358 if (command[0] == 'f') {
359 if (CUR_CELL->currently_focused == NULL)
361 toggle_fullscreen(conn, CUR_CELL->currently_focused);
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));
372 /* Is it a <with>? */
373 if (command[0] == 'w') {
374 /* TODO: implement */
375 printf("not yet implemented.\n");
379 /* It's a normal <cmd> */
382 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
383 direction_t direction;
384 times = strtol(command, &rest, 10);
386 printf("Invalid command (\"%s\")\n", command);
389 if (*rest == 'm' || *rest == 's') {
390 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
395 /* No rest? This was a tag number, not a times specification */
396 show_workspace(conn, times);
400 /* Now perform action to <where> */
401 while (*rest != '\0') {
404 else if (*rest == 'j')
406 else if (*rest == 'k')
408 else if (*rest == 'l')
411 printf("unknown direction: %c\n", *rest);
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);
425 printf("--- done ---\n");