4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * table.c: Functions/macros for easy modifying/accessing of _the_ table (defining our
18 #include <sys/types.h>
29 int current_workspace = 0;
30 int num_workspaces = 1;
31 Workspace *workspaces;
32 /* Convenience pointer to the current workspace */
42 workspaces = scalloc(sizeof(Workspace));
45 workspaces[0].screen = NULL;
46 workspaces[0].num = 0;
47 TAILQ_INIT(&(workspaces[0].floating_clients));
48 expand_table_cols(&(workspaces[0]));
49 expand_table_rows(&(workspaces[0]));
52 static void new_container(Workspace *workspace, Container **container, int col, int row) {
54 new = *container = calloc(sizeof(Container), 1);
55 CIRCLEQ_INIT(&(new->clients));
60 new->workspace = workspace;
64 * Add one row to the table
67 void expand_table_rows(Workspace *workspace) {
70 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
71 workspace->height_factor[workspace->rows-1] = 0;
73 for (int c = 0; c < workspace->cols; c++) {
74 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
75 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
80 * Adds one row at the head of the table
83 void expand_table_rows_at_head(Workspace *workspace) {
86 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
88 LOG("rows = %d\n", workspace->rows);
89 for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
90 LOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
91 workspace->height_factor[rows] = workspace->height_factor[rows-1];
94 workspace->height_factor[0] = 0;
96 for (int cols = 0; cols < workspace->cols; cols++)
97 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
99 /* Move the other rows */
100 for (int cols = 0; cols < workspace->cols; cols++)
101 for (int rows = workspace->rows - 1; rows > 0; rows--) {
102 LOG("Moving row %d to %d\n", rows-1, rows);
103 workspace->table[cols][rows] = workspace->table[cols][rows-1];
104 workspace->table[cols][rows]->row = rows;
107 for (int cols = 0; cols < workspace->cols; cols++)
108 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
112 * Add one column to the table
115 void expand_table_cols(Workspace *workspace) {
118 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
119 workspace->width_factor[workspace->cols-1] = 0;
121 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
122 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
123 for (int c = 0; c < workspace->rows; c++)
124 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
128 * Inserts one column at the table’s head
131 void expand_table_cols_at_head(Workspace *workspace) {
134 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
136 LOG("cols = %d\n", workspace->cols);
137 for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
138 LOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
139 workspace->width_factor[cols] = workspace->width_factor[cols-1];
142 workspace->width_factor[0] = 0;
144 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
145 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
147 /* Move the other columns */
148 for (int rows = 0; rows < workspace->rows; rows++)
149 for (int cols = workspace->cols - 1; cols > 0; cols--) {
150 LOG("Moving col %d to %d\n", cols-1, cols);
151 workspace->table[cols][rows] = workspace->table[cols-1][rows];
152 workspace->table[cols][rows]->col = cols;
155 for (int rows = 0; rows < workspace->rows; rows++)
156 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
160 * Shrinks the table by one column.
162 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
163 * function may only be called from move_*() or after making sure that the containers are freed
167 static void shrink_table_cols(Workspace *workspace) {
168 float free_space = workspace->width_factor[workspace->cols-1];
172 /* Shrink the width_factor array */
173 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
175 /* Free the container-pointers */
176 free(workspace->table[workspace->cols]);
178 /* Re-allocate the table */
179 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
181 /* Distribute the free space */
185 for (int cols = (workspace->cols-1); cols >= 0; cols--) {
186 if (workspace->width_factor[cols] == 0)
189 LOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
190 workspace->width_factor[cols]);
191 workspace->width_factor[cols] += free_space;
197 * See shrink_table_cols()
200 static void shrink_table_rows(Workspace *workspace) {
201 float free_space = workspace->height_factor[workspace->rows-1];
204 for (int cols = 0; cols < workspace->cols; cols++)
205 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
207 /* Shrink the height_factor array */
208 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
210 /* Distribute the free space */
214 for (int rows = (workspace->rows-1); rows >= 0; rows--) {
215 if (workspace->height_factor[rows] == 0)
218 LOG("Added free space (%f) to %d (had %f)\n", free_space, rows,
219 workspace->height_factor[rows]);
220 workspace->height_factor[rows] += free_space;
226 * Performs simple bounds checking for the given column/row
229 bool cell_exists(int col, int row) {
230 return (col >= 0 && col < c_ws->cols) &&
231 (row >= 0 && row < c_ws->rows);
234 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
235 Container *old_container = workspace->table[col][row];
237 if (old_container->mode == MODE_STACK)
238 leave_stack_mode(conn, old_container);
243 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
244 LOG("firstly freeing \n");
246 /* Free the columns which are cleaned up */
247 for (int rows = 0; rows < workspace->rows; rows++)
248 free_container(conn, workspace, cols-1, rows);
250 for (; cols < workspace->cols; cols++)
251 for (int rows = 0; rows < workspace->rows; rows++) {
252 LOG("at col = %d, row = %d\n", cols, rows);
253 Container *new_container = workspace->table[cols][rows];
255 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
256 workspace->table[cols-1][rows] = new_container;
258 new_container->row = rows;
259 new_container->col = cols-1;
263 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
264 for (int cols = 0; cols < workspace->cols; cols++)
265 free_container(conn, workspace, cols, rows-1);
267 for (; rows < workspace->rows; rows++)
268 for (int cols = 0; cols < workspace->cols; cols++) {
269 Container *new_container = workspace->table[cols][rows];
271 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
272 workspace->table[cols][rows-1] = new_container;
274 new_container->row = rows-1;
275 new_container->col = cols;
280 * Prints the table’s contents in human-readable form for debugging
283 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
284 LOG("dump_table()\n");
285 FOR_TABLE(workspace) {
286 Container *con = workspace->table[cols][rows];
288 LOG("at col=%d, row=%d\n", cols, rows);
289 LOG("currently_focused = %p\n", con->currently_focused);
291 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
292 LOG("got client %08x / %s\n", loop->child, loop->name);
300 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
303 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
304 LOG("cleanup_table()\n");
306 /* Check for empty columns if we got more than one column */
307 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
308 bool completely_empty = true;
309 for (int rows = 0; rows < workspace->rows; rows++)
310 if (workspace->table[cols][rows]->currently_focused != NULL) {
311 completely_empty = false;
314 if (completely_empty) {
315 LOG("Removing completely empty column %d\n", cols);
316 if (cols < (workspace->cols - 1))
317 move_columns_from(conn, workspace, cols+1);
319 for (int rows = 0; rows < workspace->rows; rows++)
320 free_container(conn, workspace, cols, rows);
322 shrink_table_cols(workspace);
324 if (workspace->current_col >= workspace->cols)
325 workspace->current_col = workspace->cols - 1;
329 /* Check for empty rows if we got more than one row */
330 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
331 bool completely_empty = true;
332 LOG("Checking row %d\n", rows);
333 for (int cols = 0; cols < workspace->cols; cols++)
334 if (workspace->table[cols][rows]->currently_focused != NULL) {
335 completely_empty = false;
338 if (completely_empty) {
339 LOG("Removing completely empty row %d\n", rows);
340 if (rows < (workspace->rows - 1))
341 move_rows_from(conn, workspace, rows+1);
343 for (int cols = 0; cols < workspace->cols; cols++)
344 free_container(conn, workspace, cols, rows);
346 shrink_table_rows(workspace);
348 if (workspace->current_row >= workspace->rows)
349 workspace->current_row = workspace->rows - 1;
353 /* Boundary checking for current_col and current_row */
354 if (current_col >= c_ws->cols)
355 current_col = c_ws->cols-1;
357 if (current_row >= c_ws->rows)
358 current_row = c_ws->rows-1;
360 if (CUR_CELL->currently_focused != NULL)
361 set_focus(conn, CUR_CELL->currently_focused, true);
365 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
368 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
369 LOG("Fixing col/rowspan\n");
371 FOR_TABLE(workspace) {
372 Container *con = workspace->table[cols][rows];
373 if (con->colspan > 1) {
374 LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
375 while (con->colspan > 1 &&
376 (!cell_exists(cols + (con->colspan-1), rows) ||
377 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
379 LOG("fixed it to %d\n", con->colspan);
381 if (con->rowspan > 1) {
382 LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
383 while (con->rowspan > 1 &&
384 (!cell_exists(cols, rows + (con->rowspan - 1)) ||
385 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
387 LOG("fixed it to %d\n", con->rowspan);