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)
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))
61 } else if (direction == D_LEFT || direction == D_RIGHT) {
62 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
64 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
67 printf("nah, not possible\n");
70 if (CUR_CELL->currently_focused != NULL) {
71 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_POINTER_ROOT,
72 CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
73 render_layout(connection);
77 printf("direction unhandled\n");
82 * Tries to move the window inside its current container.
84 * Returns true if the window could be moved, false otherwise.
87 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
88 direction_t direction) {
89 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
90 CIRCLEQ_NEXT(client, clients));
92 if (other == CIRCLEQ_END(&(client->container->clients)))
95 printf("i can do that\n");
96 /* We can move the client inside its current container */
97 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
98 if (direction == D_UP)
99 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
100 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
101 render_layout(connection);
106 * Moves the current window to the given direction, creating a column/row if
110 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
111 printf("moving window to direction %d\n", direction);
112 /* Get current window */
113 Container *container = CUR_CELL,
116 /* There has to be a container, see focus_window() */
117 assert(container != NULL);
119 /* If there is no window, we’re done */
120 if (container->currently_focused == NULL)
123 /* As soon as the client is moved away, the next client in the old
124 * container needs to get focus, if any. Therefore, we save it here. */
125 Client *current_client = container->currently_focused;
126 Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
127 if (to_focus == CIRCLEQ_END(&(container->clients)))
132 if (current_col == 0)
135 new = CUR_TABLE[--current_col][current_row];
138 if (current_col == (c_ws->cols-1))
139 expand_table_cols(c_ws);
141 new = CUR_TABLE[++current_col][current_row];
144 /* TODO: if we’re at the up-most position, move the rest of the table down */
145 if (move_current_window_in_container(connection, current_client, D_UP) ||
149 new = CUR_TABLE[current_col][--current_row];
152 if (move_current_window_in_container(connection, current_client, D_DOWN))
155 if (current_row == (c_ws->rows-1))
156 expand_table_rows(c_ws);
158 new = CUR_TABLE[current_col][++current_row];
162 /* Remove it from the old container and put it into the new one */
163 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
164 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
166 /* Update data structures */
167 current_client->container = new;
168 container->currently_focused = to_focus;
169 new->currently_focused = current_client;
171 /* TODO: delete all empty columns/rows */
173 render_layout(connection);
177 * "Snaps" the current container (not possible for windows, because it works at table base)
178 * to the given direction, that is, adjusts cellspan/rowspan
181 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
182 printf("snapping container to direction %d\n", direction);
184 Container *container = CUR_CELL;
187 assert(container != NULL);
191 /* Snap to the left is actually a move to the left and then a snap right */
192 move_current_window(connection, D_LEFT);
193 snap_current_container(connection, D_RIGHT);
196 /* Check if the cell is used */
197 if (!cell_exists(container->col + 1, container->row) ||
198 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
199 printf("cannot snap to right - the cell is already used\n");
203 /* Check if there are other cells with rowspan, which are in our way.
204 * If so, reduce their rowspan. */
205 for (i = container->row-1; i >= 0; i--) {
206 printf("we got cell %d, %d with rowspan %d\n",
207 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
208 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
209 CUR_TABLE[container->col+1][i]->rowspan--;
210 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
213 container->colspan++;
216 move_current_window(connection, D_UP);
217 snap_current_container(connection, D_DOWN);
220 printf("snapping down\n");
221 if (!cell_exists(container->col, container->row+1) ||
222 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
223 printf("cannot snap down - the cell is already used\n");
227 for (i = container->col-1; i >= 0; i--) {
228 printf("we got cell %d, %d with colspan %d\n",
229 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
230 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
231 CUR_TABLE[i][container->row+1]->colspan--;
232 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
236 container->rowspan++;
240 render_layout(connection);
243 static void show_workspace(xcb_connection_t *conn, int workspace) {
246 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
247 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
248 Workspace *t_ws = &(workspaces[workspace-1]);
250 printf("show_workspace(%d)\n", workspace);
252 /* Store current_row/current_col */
253 c_ws->current_row = current_row;
254 c_ws->current_col = current_col;
256 /* Check if the workspace has not been used yet */
257 if (t_ws->screen == NULL) {
258 printf("initializing new workspace, setting num to %d\n", workspace);
259 t_ws->screen = c_ws->screen;
260 /* Copy the dimensions from the virtual screen */
261 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
264 if (c_ws->screen != t_ws->screen) {
265 /* We need to switch to the other screen first */
266 printf("Just moving over to other screen.\n");
267 c_ws = &(workspaces[t_ws->screen->current_workspace]);
268 current_col = c_ws->current_col;
269 current_row = c_ws->current_row;
270 if (CUR_CELL->currently_focused != NULL)
271 warp_pointer_into(conn, CUR_CELL->currently_focused);
273 Rect *dims = &(c_ws->screen->rect);
274 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
275 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
279 /* Check if we need to change something or if we’re already there */
280 if (c_ws->screen->current_workspace == (workspace-1))
283 t_ws->screen->current_workspace = workspace-1;
285 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
286 //xcb_grab_server(conn);
288 /* Unmap all clients of the current workspace */
289 for (cols = 0; cols < c_ws->cols; cols++)
290 for (rows = 0; rows < c_ws->rows; rows++) {
291 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
292 xcb_unmap_window(conn, client->frame);
295 c_ws = &workspaces[workspace-1];
296 current_row = c_ws->current_row;
297 current_col = c_ws->current_col;
298 printf("new current row = %d, current col = %d\n", current_row, current_col);
300 /* Map all clients on the new workspace */
301 for (cols = 0; cols < c_ws->cols; cols++)
302 for (rows = 0; rows < c_ws->rows; rows++) {
303 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
304 xcb_map_window(conn, client->frame);
307 /* Restore focus on the new workspace */
308 if (CUR_CELL->currently_focused != NULL)
309 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
310 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
312 //xcb_ungrab_server(conn);
318 * Parses a command, see file CMDMODE for more information
321 void parse_command(xcb_connection_t *conn, const char *command) {
322 printf("--- parsing command \"%s\" ---\n", command);
323 /* Hmm, just to be sure */
324 if (command[0] == '\0')
327 /* Is it an <exec>? */
328 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
329 printf("starting \"%s\"\n", command + strlen("exec "));
330 start_application(command+strlen("exec "));
334 /* Is it 'f' for fullscreen? */
335 if (command[0] == 'f') {
336 if (CUR_CELL->currently_focused == NULL)
338 toggle_fullscreen(conn, CUR_CELL->currently_focused);
342 /* Is it a <with>? */
343 if (command[0] == 'w') {
344 /* TODO: implement */
345 printf("not yet implemented.\n");
349 /* It's a normal <cmd> */
352 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
353 direction_t direction;
354 times = strtol(command, &rest, 10);
356 printf("Invalid command (\"%s\")\n", command);
359 if (*rest == 'm' || *rest == 's') {
360 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
365 /* No rest? This was a tag number, not a times specification */
366 show_workspace(conn, times);
370 /* Now perform action to <where> */
371 while (*rest != '\0') {
374 else if (*rest == 'j')
376 else if (*rest == 'k')
378 else if (*rest == 'l')
381 printf("unknown direction: %c\n", *rest);
385 if (action == ACTION_FOCUS)
386 focus_window(conn, direction);
387 else if (action == ACTION_MOVE)
388 move_current_window(conn, direction);
389 else if (action == ACTION_SNAP)
390 snap_current_container(conn, direction);
395 printf("--- done ---\n");