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 Workspace workspaces[10];
31 /* Convenience pointer to the current workspace */
32 Workspace *c_ws = &workspaces[0];
41 memset(workspaces, 0, sizeof(workspaces));
43 for (int i = 0; i < 10; i++) {
44 workspaces[i].screen = NULL;
45 workspaces[i].num = i;
46 TAILQ_INIT(&(workspaces[i].floating_clients));
47 expand_table_cols(&(workspaces[i]));
48 expand_table_rows(&(workspaces[i]));
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) {
202 for (int cols = 0; cols < workspace->cols; cols++)
203 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
208 * Performs simple bounds checking for the given column/row
211 bool cell_exists(int col, int row) {
212 return (col >= 0 && col < c_ws->cols) &&
213 (row >= 0 && row < c_ws->rows);
216 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
217 Container *old_container = workspace->table[col][row];
219 if (old_container->mode == MODE_STACK)
220 leave_stack_mode(conn, old_container);
225 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
226 LOG("firstly freeing \n");
228 /* Free the columns which are cleaned up */
229 for (int rows = 0; rows < workspace->rows; rows++)
230 free_container(conn, workspace, cols-1, rows);
232 for (; cols < workspace->cols; cols++)
233 for (int rows = 0; rows < workspace->rows; rows++) {
234 LOG("at col = %d, row = %d\n", cols, rows);
235 Container *new_container = workspace->table[cols][rows];
237 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
238 workspace->table[cols-1][rows] = new_container;
240 new_container->row = rows;
241 new_container->col = cols-1;
245 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
246 for (int cols = 0; cols < workspace->cols; cols++)
247 free_container(conn, workspace, cols, rows-1);
249 for (; rows < workspace->rows; rows++)
250 for (int cols = 0; cols < workspace->cols; cols++) {
251 Container *new_container = workspace->table[cols][rows];
253 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
254 workspace->table[cols][rows-1] = new_container;
256 new_container->row = rows-1;
257 new_container->col = cols;
262 * Prints the table’s contents in human-readable form for debugging
265 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
266 LOG("dump_table()\n");
267 FOR_TABLE(workspace) {
268 Container *con = workspace->table[cols][rows];
270 LOG("at col=%d, row=%d\n", cols, rows);
271 LOG("currently_focused = %p\n", con->currently_focused);
273 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
274 LOG("got client %08x / %s\n", loop->child, loop->name);
282 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
285 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
286 LOG("cleanup_table()\n");
288 /* Check for empty columns if we got more than one column */
289 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
290 bool completely_empty = true;
291 for (int rows = 0; rows < workspace->rows; rows++)
292 if (workspace->table[cols][rows]->currently_focused != NULL) {
293 completely_empty = false;
296 if (completely_empty) {
297 LOG("Removing completely empty column %d\n", cols);
298 if (cols < (workspace->cols - 1))
299 move_columns_from(conn, workspace, cols+1);
301 for (int rows = 0; rows < workspace->rows; rows++)
302 free_container(conn, workspace, cols, rows);
304 shrink_table_cols(workspace);
306 if (workspace->current_col >= workspace->cols)
307 workspace->current_col = workspace->cols - 1;
311 /* Check for empty rows if we got more than one row */
312 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
313 bool completely_empty = true;
314 LOG("Checking row %d\n", rows);
315 for (int cols = 0; cols < workspace->cols; cols++)
316 if (workspace->table[cols][rows]->currently_focused != NULL) {
317 completely_empty = false;
320 if (completely_empty) {
321 LOG("Removing completely empty row %d\n", rows);
322 if (rows < (workspace->rows - 1))
323 move_rows_from(conn, workspace, rows+1);
325 for (int cols = 0; cols < workspace->cols; cols++)
326 free_container(conn, workspace, cols, rows);
328 shrink_table_rows(workspace);
330 if (workspace->current_row >= workspace->rows)
331 workspace->current_row = workspace->rows - 1;
335 /* Boundary checking for current_col and current_row */
336 if (current_col >= c_ws->cols)
337 current_col = c_ws->cols-1;
339 if (current_row >= c_ws->rows)
340 current_row = c_ws->rows-1;
342 if (CUR_CELL->currently_focused != NULL)
343 set_focus(conn, CUR_CELL->currently_focused, true);
347 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
350 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
351 LOG("Fixing col/rowspan\n");
353 FOR_TABLE(workspace) {
354 Container *con = workspace->table[cols][rows];
355 if (con->colspan > 1) {
356 LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
357 while (con->colspan > 1 &&
358 (!cell_exists(cols + (con->colspan-1), rows) ||
359 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
361 LOG("fixed it to %d\n", con->colspan);
363 if (con->rowspan > 1) {
364 LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
365 while (con->rowspan > 1 &&
366 (!cell_exists(cols, rows + (con->rowspan - 1)) ||
367 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
369 LOG("fixed it to %d\n", con->rowspan);