16 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
17 direction_t direction) {
18 /* If this container is empty, we’re done */
19 if (container->currently_focused == NULL)
23 if (direction == D_UP)
24 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
25 else if (direction == D_DOWN)
26 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
28 /* If we don’t have anything to select, we’re done */
29 if (candidad == CIRCLEQ_END(&(container->clients)))
32 /* Set focus if we could successfully move */
33 container->currently_focused = candidad;
34 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE, candidad->child, XCB_CURRENT_TIME);
35 render_layout(connection);
40 static void focus_window(xcb_connection_t *connection, direction_t direction) {
41 printf("focusing direction %d\n", direction);
42 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
43 if (direction == D_UP || direction == D_DOWN) {
44 /* Let’s see if we can perform up/down focus in the current container */
45 Container *container = CUR_CELL;
47 /* There always is a container. If not, current_col or current_row is wrong */
48 assert(container != NULL);
50 if (focus_window_in_container(connection, container, direction))
52 } else if (direction == D_LEFT || direction == D_RIGHT) {
53 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
55 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
58 printf("nah, not possible\n");
61 if (CUR_CELL->currently_focused != NULL) {
62 xcb_set_input_focus(connection, XCB_INPUT_FOCUS_NONE,
63 CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
64 render_layout(connection);
68 printf("direction unhandled\n");
73 * Tries to move the window inside its current container.
75 * Returns true if the window could be moved, false otherwise.
78 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
79 direction_t direction) {
80 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
81 CIRCLEQ_NEXT(client, clients));
83 if (other == CIRCLEQ_END(&(client->container->clients)))
86 printf("i can do that\n");
87 /* We can move the client inside its current container */
88 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
89 if (direction == D_UP)
90 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
91 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
92 render_layout(connection);
97 * Moves the current window to the given direction, creating a column/row if
101 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
102 printf("moving window to direction %d\n", direction);
103 /* Get current window */
104 Container *container = CUR_CELL,
107 /* There has to be a container, see focus_window() */
108 assert(container != NULL);
110 /* If there is no window, we’re done */
111 if (container->currently_focused == NULL)
114 /* As soon as the client is moved away, the next client in the old
115 * container needs to get focus, if any. Therefore, we save it here. */
116 Client *current_client = container->currently_focused;
117 Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
118 if (to_focus == CIRCLEQ_END(&(container->clients)))
123 if (current_col == 0)
126 new = CUR_TABLE[--current_col][current_row];
129 if (current_col == (c_ws->cols-1))
130 expand_table_cols(c_ws);
132 new = CUR_TABLE[++current_col][current_row];
135 /* TODO: if we’re at the up-most position, move the rest of the table down */
136 if (move_current_window_in_container(connection, current_client, D_UP) ||
140 new = CUR_TABLE[current_col][--current_row];
143 if (move_current_window_in_container(connection, current_client, D_DOWN))
146 if (current_row == (c_ws->rows-1))
147 expand_table_rows(c_ws);
149 new = CUR_TABLE[current_col][++current_row];
153 /* Remove it from the old container and put it into the new one */
154 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
155 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
157 /* Update data structures */
158 current_client->container = new;
159 container->currently_focused = to_focus;
160 new->currently_focused = current_client;
162 /* TODO: delete all empty columns/rows */
164 render_layout(connection);
168 * "Snaps" the current container (not possible for windows, because it works at table base)
169 * to the given direction, that is, adjusts cellspan/rowspan
172 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
173 printf("snapping container to direction %d\n", direction);
175 Container *container = CUR_CELL;
178 assert(container != NULL);
182 /* Snap to the left is actually a move to the left and then a snap right */
183 move_current_window(connection, D_LEFT);
184 snap_current_container(connection, D_RIGHT);
187 /* Check if the cell is used */
188 if (!cell_exists(container->col + 1, container->row) ||
189 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
190 printf("cannot snap to right - the cell is already used\n");
194 /* Check if there are other cells with rowspan, which are in our way.
195 * If so, reduce their rowspan. */
196 for (i = container->row-1; i >= 0; i--) {
197 printf("we got cell %d, %d with rowspan %d\n",
198 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
199 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
200 CUR_TABLE[container->col+1][i]->rowspan--;
201 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
204 container->colspan++;
207 move_current_window(connection, D_UP);
208 snap_current_container(connection, D_DOWN);
211 printf("snapping down\n");
212 if (!cell_exists(container->col, container->row+1) ||
213 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
214 printf("cannot snap down - the cell is already used\n");
218 for (i = container->col-1; i >= 0; i--) {
219 printf("we got cell %d, %d with colspan %d\n",
220 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
221 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
222 CUR_TABLE[i][container->row+1]->colspan--;
223 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
227 container->rowspan++;
231 render_layout(connection);
234 static void show_workspace(xcb_connection_t *conn, int workspace) {
237 printf("show_workspace(%d)\n", workspace);
239 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
241 /* Store current_row/current_col */
242 c_ws->current_row = current_row;
243 c_ws->current_col = current_col;
245 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
246 //xcb_grab_server(conn);
248 /* Unmap all clients */
249 for (cols = 0; cols < c_ws->cols; cols++)
250 for (rows = 0; rows < c_ws->rows; rows++) {
251 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
252 xcb_unmap_window(conn, client->frame);
255 c_ws = &workspaces[workspace-1];
256 current_row = c_ws->current_row;
257 current_col = c_ws->current_col;
258 printf("new current row = %d, current col = %d\n", current_row, current_col);
260 /* Map all clients on the new workspace */
261 for (cols = 0; cols < c_ws->cols; cols++)
262 for (rows = 0; rows < c_ws->rows; rows++) {
263 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
264 xcb_map_window(conn, client->frame);
267 /* Restore focus on the new workspace */
268 if (CUR_CELL->currently_focused != NULL)
269 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
270 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, root, XCB_CURRENT_TIME);
272 //xcb_ungrab_server(conn);
278 * Parses a command, see file CMDMODE for more information
281 void parse_command(xcb_connection_t *conn, const char *command) {
282 printf("--- parsing command \"%s\" ---\n", command);
283 /* Hmm, just to be sure */
284 if (command[0] == '\0')
287 /* Is it an <exec>? */
288 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
289 printf("starting \"%s\"\n", command + strlen("exec "));
290 start_application(command+strlen("exec "), NULL);
294 /* Is it a <with>? */
295 if (command[0] == 'w') {
296 /* TODO: implement */
297 printf("not yet implemented.\n");
301 /* It's a normal <cmd> */
304 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
305 direction_t direction;
306 times = strtol(command, &rest, 10);
308 printf("Invalid command: Consists only of a movement\n");
311 if (*rest == 'm' || *rest == 's') {
312 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
317 /* No rest? This was a tag number, not a times specification */
318 show_workspace(conn, times);
322 /* Now perform action to <where> */
323 while (*rest != '\0') {
326 else if (*rest == 'j')
328 else if (*rest == 'k')
330 else if (*rest == 'l')
333 printf("unknown direction: %c\n", *rest);
337 if (action == ACTION_FOCUS)
338 focus_window(conn, direction);
339 else if (action == ACTION_MOVE)
340 move_current_window(conn, direction);
341 else if (action == ACTION_SNAP)
342 snap_current_container(conn, direction);
347 printf("--- done ---\n");