4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
27 static bool focus_window_in_container(xcb_connection_t *conn, Container *container,
28 direction_t direction) {
29 /* If this container is empty, we’re done */
30 if (container->currently_focused == NULL)
33 /* Get the previous/next client or wrap around */
34 Client *candidate = NULL;
35 if (direction == D_UP) {
36 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
37 candidate = CIRCLEQ_LAST(&(container->clients));
39 else if (direction == D_DOWN) {
40 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
41 candidate = CIRCLEQ_FIRST(&(container->clients));
42 } else printf("Direction not implemented!\n");
45 set_focus(conn, candidate);
50 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
52 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
53 printf("focusing direction %d\n", direction);
55 int new_row = current_row,
56 new_col = current_col;
58 Container *container = CUR_CELL;
59 Workspace *t_ws = c_ws;
61 /* There always is a container. If not, current_col or current_row is wrong */
62 assert(container != NULL);
64 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
65 if (direction == D_UP || direction == D_DOWN) {
66 if (thing == THING_WINDOW)
67 /* Let’s see if we can perform up/down focus in the current container */
68 if (focus_window_in_container(conn, container, direction))
71 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
73 else if (direction == D_UP && cell_exists(current_col, current_row-1))
76 /* Let’s see if there is a screen down/up there to which we can switch */
77 printf("container is at %d with height %d\n", container->y, container->height);
79 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
80 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
81 printf("Not possible, no screen found.\n");
84 t_ws = &(workspaces[screen->current_workspace]);
85 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
87 if (new_col >= t_ws->cols)
88 new_col = (t_ws->cols - 1);
89 if (new_row >= t_ws->rows)
90 new_row = (t_ws->rows - 1);
92 } else if (direction == D_LEFT || direction == D_RIGHT) {
93 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
95 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
98 /* Let’s see if there is a screen left/right here to which we can switch */
99 printf("container is at %d with width %d\n", container->x, container->width);
101 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
102 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
103 printf("Not possible, no screen found.\n");
106 t_ws = &(workspaces[screen->current_workspace]);
107 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
108 /* Bounds checking */
109 if (new_col >= t_ws->cols)
110 new_col = (t_ws->cols - 1);
111 if (new_row >= t_ws->rows)
112 new_row = (t_ws->rows - 1);
115 printf("direction unhandled\n");
119 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
120 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused);
124 * Tries to move the window inside its current container.
126 * Returns true if the window could be moved, false otherwise.
129 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
130 direction_t direction) {
131 assert(client->container != NULL);
133 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
134 CIRCLEQ_NEXT(client, clients));
136 if (other == CIRCLEQ_END(&(client->container->clients)))
139 printf("i can do that\n");
140 /* We can move the client inside its current container */
141 /* TODO: needs wrapping */
142 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
143 if (direction == D_UP)
144 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
145 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
151 * Moves the current window to the given direction, creating a column/row if
155 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
156 printf("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
157 (direction == D_LEFT ? "left" : "right"))));
158 /* Get current window */
159 Container *container = CUR_CELL,
162 /* There has to be a container, see focus_window() */
163 assert(container != NULL);
165 /* If there is no window or the dock window is focused, we’re done */
166 if (container->currently_focused == NULL ||
167 container->currently_focused->dock)
170 /* As soon as the client is moved away, the next client in the old
171 * container needs to get focus, if any. Therefore, we save it here. */
172 Client *current_client = container->currently_focused;
173 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
174 if (to_focus == NULL)
175 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
179 /* TODO: If we’re at the left-most position, move the rest of the table right */
180 if (current_col == 0)
183 new = CUR_TABLE[--current_col][current_row];
186 if (current_col == (c_ws->cols-1))
187 expand_table_cols(c_ws);
189 new = CUR_TABLE[++current_col][current_row];
192 /* TODO: if we’re at the up-most position, move the rest of the table down */
193 if (move_current_window_in_container(conn, current_client, D_UP) ||
197 new = CUR_TABLE[current_col][--current_row];
200 if (move_current_window_in_container(conn, current_client, D_DOWN))
203 if (current_row == (c_ws->rows-1))
204 expand_table_rows(c_ws);
206 new = CUR_TABLE[current_col][++current_row];
210 /* Remove it from the old container and put it into the new one */
211 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
212 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
214 /* Update data structures */
215 current_client->container = new;
216 container->currently_focused = to_focus;
217 new->currently_focused = current_client;
219 /* delete all empty columns/rows */
220 cleanup_table(conn, container->workspace);
222 /* Fix colspan/rowspan if it’d overlap */
223 fix_colrowspan(conn, container->workspace);
227 set_focus(conn, current_client);
231 * "Snaps" the current container (not possible for windows, because it works at table base)
232 * to the given direction, that is, adjusts cellspan/rowspan
235 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
236 printf("snapping container to direction %d\n", direction);
238 Container *container = CUR_CELL;
240 assert(container != NULL);
244 /* Snap to the left is actually a move to the left and then a snap right */
245 if (!cell_exists(container->col - 1, container->row) ||
246 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
247 printf("cannot snap to right - the cell is already used\n");
251 move_current_window(conn, D_LEFT);
252 snap_current_container(conn, D_RIGHT);
255 /* Check if the cell is used */
256 if (!cell_exists(container->col + 1, container->row) ||
257 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
258 printf("cannot snap to right - the cell is already used\n");
262 /* Check if there are other cells with rowspan, which are in our way.
263 * If so, reduce their rowspan. */
264 for (int i = container->row-1; i >= 0; i--) {
265 printf("we got cell %d, %d with rowspan %d\n",
266 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
267 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
268 CUR_TABLE[container->col+1][i]->rowspan--;
269 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
272 container->colspan++;
275 move_current_window(conn, D_UP);
276 snap_current_container(conn, D_DOWN);
279 printf("snapping down\n");
280 if (!cell_exists(container->col, container->row+1) ||
281 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
282 printf("cannot snap down - the cell is already used\n");
286 for (int i = container->col-1; i >= 0; i--) {
287 printf("we got cell %d, %d with colspan %d\n",
288 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
289 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
290 CUR_TABLE[i][container->row+1]->colspan--;
291 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
295 container->rowspan++;
303 * Moves the currently selected window to the given workspace
306 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
307 printf("Moving current window to workspace %d\n", workspace);
309 Container *container = CUR_CELL;
311 assert(container != NULL);
313 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
314 Workspace *t_ws = &(workspaces[workspace-1]);
316 Client *current_client = container->currently_focused;
317 if (current_client == NULL) {
318 printf("No currently focused client in current container.\n");
321 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
322 if (to_focus == NULL)
323 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
325 if (t_ws->screen == NULL) {
326 printf("initializing new workspace, setting num to %d\n", workspace-1);
327 t_ws->screen = container->workspace->screen;
328 /* Copy the dimensions from the virtual screen */
329 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
332 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
334 assert(to_container != NULL);
336 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
337 SLIST_REMOVE(&(container->workspace->focus_stack), current_client, Client, focus_clients);
339 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
340 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
343 current_client->container = to_container;
344 container->currently_focused = to_focus;
345 to_container->currently_focused = current_client;
347 /* If we’re moving it to an invisible screen, we need to unmap it */
348 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
349 printf("This workspace is not visible, unmapping\n");
350 xcb_unmap_window(conn, current_client->frame);
353 /* delete all empty columns/rows */
354 cleanup_table(conn, container->workspace);
359 static void show_workspace(xcb_connection_t *conn, int workspace) {
361 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
362 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
363 Workspace *t_ws = &(workspaces[workspace-1]);
365 printf("show_workspace(%d)\n", workspace);
367 /* Store current_row/current_col */
368 c_ws->current_row = current_row;
369 c_ws->current_col = current_col;
371 /* Check if the workspace has not been used yet */
372 if (t_ws->screen == NULL) {
373 printf("initializing new workspace, setting num to %d\n", workspace);
374 t_ws->screen = c_ws->screen;
375 /* Copy the dimensions from the virtual screen */
376 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
379 if (c_ws->screen != t_ws->screen) {
380 /* We need to switch to the other screen first */
381 printf("moving over to other screen.\n");
382 c_ws = &(workspaces[t_ws->screen->current_workspace]);
383 current_col = c_ws->current_col;
384 current_row = c_ws->current_row;
385 if (CUR_CELL->currently_focused != NULL)
386 warp_pointer_into(conn, CUR_CELL->currently_focused);
388 Rect *dims = &(c_ws->screen->rect);
389 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
390 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
394 /* Check if we need to change something or if we’re already there */
395 if (c_ws->screen->current_workspace == (workspace-1))
398 t_ws->screen->current_workspace = workspace-1;
400 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
401 //xcb_grab_server(conn);
403 /* Unmap all clients of the current workspace */
404 int unmapped_clients = 0;
405 for (int cols = 0; cols < c_ws->cols; cols++)
406 for (int rows = 0; rows < c_ws->rows; rows++)
407 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
408 xcb_unmap_window(conn, client->frame);
412 /* If we did not unmap any clients, the workspace is empty and we can destroy it */
413 if (unmapped_clients == 0)
416 /* Unmap the stack windows on the current workspace, if any */
417 struct Stack_Window *stack_win;
418 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
419 if (stack_win->container->workspace == c_ws)
420 xcb_unmap_window(conn, stack_win->window);
422 c_ws = &workspaces[workspace-1];
423 current_row = c_ws->current_row;
424 current_col = c_ws->current_col;
425 printf("new current row = %d, current col = %d\n", current_row, current_col);
427 /* Map all clients on the new workspace */
428 for (int cols = 0; cols < c_ws->cols; cols++)
429 for (int rows = 0; rows < c_ws->rows; rows++)
430 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
431 xcb_map_window(conn, client->frame);
433 /* Map all stack windows, if any */
434 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
435 if (stack_win->container->workspace == c_ws)
436 xcb_map_window(conn, stack_win->window);
438 /* Restore focus on the new workspace */
439 if (CUR_CELL->currently_focused != NULL)
440 set_focus(conn, CUR_CELL->currently_focused);
441 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
443 //xcb_ungrab_server(conn);
449 * Parses a command, see file CMDMODE for more information
452 void parse_command(xcb_connection_t *conn, const char *command) {
453 printf("--- parsing command \"%s\" ---\n", command);
454 /* Hmm, just to be sure */
455 if (command[0] == '\0')
458 /* Is it an <exec>? */
459 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
460 printf("starting \"%s\"\n", command + strlen("exec "));
461 start_application(command+strlen("exec "));
465 /* Is it <restart>? */
466 if (strncmp(command, "restart", strlen("restart")) == 0) {
467 printf("restarting \"%s\"...\n", application_path);
468 execl(application_path, application_path, NULL);
472 /* Is it 'f' for fullscreen? */
473 if (command[0] == 'f') {
474 if (CUR_CELL->currently_focused == NULL)
476 toggle_fullscreen(conn, CUR_CELL->currently_focused);
480 /* Is it just 's' for stacking or 'd' for default? */
481 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
482 printf("Switching mode for current container\n");
483 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
487 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
489 /* Is it a <with>? */
490 if (command[0] == 'w') {
492 /* TODO: implement */
493 if (command[0] == 'c') {
494 with = WITH_CONTAINER;
497 printf("not yet implemented.\n");
502 /* It's a normal <cmd> */
504 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
505 direction_t direction;
506 int times = strtol(command, &rest, 10);
508 printf("Invalid command (\"%s\")\n", command);
513 /* No rest? This was a tag number, not a times specification */
514 show_workspace(conn, times);
518 if (*rest == 'm' || *rest == 's') {
519 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
523 int workspace = strtol(rest, &rest, 10);
526 printf("Invalid command (\"%s\")\n", command);
531 move_current_window_to_workspace(conn, workspace);
535 /* Now perform action to <where> */
536 while (*rest != '\0') {
539 else if (*rest == 'j')
541 else if (*rest == 'k')
543 else if (*rest == 'l')
546 printf("unknown direction: %c\n", *rest);
550 if (action == ACTION_FOCUS) {
551 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
553 else if (action == ACTION_MOVE)
554 move_current_window(conn, direction);
555 else if (action == ACTION_SNAP)
556 snap_current_container(conn, direction);
561 printf("--- done ---\n");