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>
30 int current_workspace = 0;
31 int num_workspaces = 1;
32 Workspace *workspaces;
33 /* Convenience pointer to the current workspace */
43 workspaces = scalloc(sizeof(Workspace));
46 workspaces[0].screen = NULL;
47 workspaces[0].num = 0;
48 TAILQ_INIT(&(workspaces[0].floating_clients));
51 static void new_container(Workspace *workspace, Container **container, int col, int row) {
53 new = *container = calloc(sizeof(Container), 1);
54 CIRCLEQ_INIT(&(new->clients));
59 new->workspace = workspace;
60 switch_layout_mode(global_conn, new, config.container_mode);
61 new->stack_limit = config.container_stack_limit;
62 new->stack_limit_value = config.container_stack_limit_value;
66 * Add one row to the table
69 void expand_table_rows(Workspace *workspace) {
72 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
73 workspace->height_factor[workspace->rows-1] = 0;
75 for (int c = 0; c < workspace->cols; c++) {
76 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
77 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
82 * Adds one row at the head of the table
85 void expand_table_rows_at_head(Workspace *workspace) {
88 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
90 LOG("rows = %d\n", workspace->rows);
91 for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
92 LOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
93 workspace->height_factor[rows] = workspace->height_factor[rows-1];
96 workspace->height_factor[0] = 0;
98 for (int cols = 0; cols < workspace->cols; cols++)
99 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
101 /* Move the other rows */
102 for (int cols = 0; cols < workspace->cols; cols++)
103 for (int rows = workspace->rows - 1; rows > 0; rows--) {
104 LOG("Moving row %d to %d\n", rows-1, rows);
105 workspace->table[cols][rows] = workspace->table[cols][rows-1];
106 workspace->table[cols][rows]->row = rows;
109 for (int cols = 0; cols < workspace->cols; cols++)
110 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
114 * Add one column to the table
117 void expand_table_cols(Workspace *workspace) {
120 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
121 workspace->width_factor[workspace->cols-1] = 0;
123 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
124 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
125 for (int c = 0; c < workspace->rows; c++)
126 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
130 * Inserts one column at the table’s head
133 void expand_table_cols_at_head(Workspace *workspace) {
136 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
138 LOG("cols = %d\n", workspace->cols);
139 for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
140 LOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
141 workspace->width_factor[cols] = workspace->width_factor[cols-1];
144 workspace->width_factor[0] = 0;
146 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
147 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
149 /* Move the other columns */
150 for (int rows = 0; rows < workspace->rows; rows++)
151 for (int cols = workspace->cols - 1; cols > 0; cols--) {
152 LOG("Moving col %d to %d\n", cols-1, cols);
153 workspace->table[cols][rows] = workspace->table[cols-1][rows];
154 workspace->table[cols][rows]->col = cols;
157 for (int rows = 0; rows < workspace->rows; rows++)
158 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
162 * Shrinks the table by one column.
164 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
165 * function may only be called from move_*() or after making sure that the containers are freed
169 static void shrink_table_cols(Workspace *workspace) {
170 float free_space = workspace->width_factor[workspace->cols-1];
174 /* Shrink the width_factor array */
175 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
177 /* Free the container-pointers */
178 free(workspace->table[workspace->cols]);
180 /* Re-allocate the table */
181 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
183 /* Distribute the free space */
187 for (int cols = (workspace->cols-1); cols >= 0; cols--) {
188 if (workspace->width_factor[cols] == 0)
191 LOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
192 workspace->width_factor[cols]);
193 workspace->width_factor[cols] += free_space;
199 * See shrink_table_cols()
202 static void shrink_table_rows(Workspace *workspace) {
203 float free_space = workspace->height_factor[workspace->rows-1];
206 for (int cols = 0; cols < workspace->cols; cols++)
207 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
209 /* Shrink the height_factor array */
210 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
212 /* Distribute the free space */
216 for (int rows = (workspace->rows-1); rows >= 0; rows--) {
217 if (workspace->height_factor[rows] == 0)
220 LOG("Added free space (%f) to %d (had %f)\n", free_space, rows,
221 workspace->height_factor[rows]);
222 workspace->height_factor[rows] += free_space;
228 * Performs simple bounds checking for the given column/row
231 bool cell_exists(int col, int row) {
232 return (col >= 0 && col < c_ws->cols) &&
233 (row >= 0 && row < c_ws->rows);
236 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
237 Container *old_container = workspace->table[col][row];
239 if (old_container->mode == MODE_STACK)
240 leave_stack_mode(conn, old_container);
245 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
246 LOG("firstly freeing \n");
248 /* Free the columns which are cleaned up */
249 for (int rows = 0; rows < workspace->rows; rows++)
250 free_container(conn, workspace, cols-1, rows);
252 for (; cols < workspace->cols; cols++)
253 for (int rows = 0; rows < workspace->rows; rows++) {
254 LOG("at col = %d, row = %d\n", cols, rows);
255 Container *new_container = workspace->table[cols][rows];
257 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
258 workspace->table[cols-1][rows] = new_container;
260 new_container->row = rows;
261 new_container->col = cols-1;
265 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
266 for (int cols = 0; cols < workspace->cols; cols++)
267 free_container(conn, workspace, cols, rows-1);
269 for (; rows < workspace->rows; rows++)
270 for (int cols = 0; cols < workspace->cols; cols++) {
271 Container *new_container = workspace->table[cols][rows];
273 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
274 workspace->table[cols][rows-1] = new_container;
276 new_container->row = rows-1;
277 new_container->col = cols;
282 * Prints the table’s contents in human-readable form for debugging
285 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
286 LOG("dump_table()\n");
287 FOR_TABLE(workspace) {
288 Container *con = workspace->table[cols][rows];
290 LOG("at col=%d, row=%d\n", cols, rows);
291 LOG("currently_focused = %p\n", con->currently_focused);
293 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
294 LOG("got client %08x / %s\n", loop->child, loop->name);
302 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
305 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
306 LOG("cleanup_table()\n");
308 /* Check for empty columns if we got more than one column */
309 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
310 bool completely_empty = true;
311 for (int rows = 0; rows < workspace->rows; rows++)
312 if (workspace->table[cols][rows]->currently_focused != NULL) {
313 completely_empty = false;
316 if (completely_empty) {
317 LOG("Removing completely empty column %d\n", cols);
318 if (cols < (workspace->cols - 1))
319 move_columns_from(conn, workspace, cols+1);
321 for (int rows = 0; rows < workspace->rows; rows++)
322 free_container(conn, workspace, cols, rows);
324 shrink_table_cols(workspace);
326 if (workspace->current_col >= workspace->cols)
327 workspace->current_col = workspace->cols - 1;
331 /* Check for empty rows if we got more than one row */
332 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
333 bool completely_empty = true;
334 LOG("Checking row %d\n", rows);
335 for (int cols = 0; cols < workspace->cols; cols++)
336 if (workspace->table[cols][rows]->currently_focused != NULL) {
337 completely_empty = false;
340 if (completely_empty) {
341 LOG("Removing completely empty row %d\n", rows);
342 if (rows < (workspace->rows - 1))
343 move_rows_from(conn, workspace, rows+1);
345 for (int cols = 0; cols < workspace->cols; cols++)
346 free_container(conn, workspace, cols, rows);
348 shrink_table_rows(workspace);
350 if (workspace->current_row >= workspace->rows)
351 workspace->current_row = workspace->rows - 1;
355 /* Boundary checking for current_col and current_row */
356 if (current_col >= c_ws->cols)
357 current_col = c_ws->cols-1;
359 if (current_row >= c_ws->rows)
360 current_row = c_ws->rows-1;
362 if (CUR_CELL->currently_focused != NULL)
363 set_focus(conn, CUR_CELL->currently_focused, true);
367 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
370 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
371 LOG("Fixing col/rowspan\n");
373 FOR_TABLE(workspace) {
374 Container *con = workspace->table[cols][rows];
375 if (con->colspan > 1) {
376 LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
377 while (con->colspan > 1 &&
378 (!cell_exists(cols + (con->colspan-1), rows) ||
379 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
381 LOG("fixed it to %d\n", con->colspan);
383 if (con->rowspan > 1) {
384 LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
385 while (con->rowspan > 1 &&
386 (!cell_exists(cols, rows + (con->rowspan - 1)) ||
387 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
389 LOG("fixed it to %d\n", con->rowspan);