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 #include "workspace.h"
32 int current_workspace = 0;
33 int num_workspaces = 1;
34 struct workspaces_head *workspaces;
35 /* Convenience pointer to the current workspace */
45 workspaces = scalloc(sizeof(struct workspaces_head));
46 TAILQ_INIT(workspaces);
48 c_ws = scalloc(sizeof(Workspace));
49 workspace_set_name(c_ws, NULL);
50 TAILQ_INIT(&(c_ws->floating_clients));
51 TAILQ_INSERT_TAIL(workspaces, c_ws, workspaces);
54 static void new_container(Workspace *workspace, Container **container, int col, int row, bool skip_layout_switch) {
56 new = *container = scalloc(sizeof(Container));
57 CIRCLEQ_INIT(&(new->clients));
62 new->workspace = workspace;
63 if (!skip_layout_switch)
64 switch_layout_mode(global_conn, new, config.container_mode);
65 new->stack_limit = config.container_stack_limit;
66 new->stack_limit_value = config.container_stack_limit_value;
70 * Add one row to the table
73 void expand_table_rows(Workspace *workspace) {
76 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
77 workspace->height_factor[workspace->rows-1] = 0;
79 for (int c = 0; c < workspace->cols; c++) {
80 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
81 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1, true);
84 /* We need to switch the layout in a separate step because it could
85 * happen that render_layout() (being called by switch_layout_mode())
86 * would access containers which were not yet initialized. */
87 for (int c = 0; c < workspace->cols; c++)
88 switch_layout_mode(global_conn, workspace->table[c][workspace->rows-1], config.container_mode);
92 * Adds one row at the head of the table
95 void expand_table_rows_at_head(Workspace *workspace) {
98 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
100 DLOG("rows = %d\n", workspace->rows);
101 for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
102 DLOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
103 workspace->height_factor[rows] = workspace->height_factor[rows-1];
106 workspace->height_factor[0] = 0;
108 for (int cols = 0; cols < workspace->cols; cols++)
109 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
111 /* Move the other rows */
112 for (int cols = 0; cols < workspace->cols; cols++)
113 for (int rows = workspace->rows - 1; rows > 0; rows--) {
114 DLOG("Moving row %d to %d\n", rows-1, rows);
115 workspace->table[cols][rows] = workspace->table[cols][rows-1];
116 workspace->table[cols][rows]->row = rows;
119 for (int cols = 0; cols < workspace->cols; cols++)
120 new_container(workspace, &(workspace->table[cols][0]), cols, 0, false);
124 * Add one column to the table
127 void expand_table_cols(Workspace *workspace) {
130 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
131 workspace->width_factor[workspace->cols-1] = 0;
133 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
134 workspace->table[workspace->cols-1] = scalloc(sizeof(Container*) * workspace->rows);
136 for (int c = 0; c < workspace->rows; c++)
137 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c, true);
139 for (int c = 0; c < workspace->rows; c++)
140 switch_layout_mode(global_conn, workspace->table[workspace->cols-1][c], config.container_mode);
144 * Inserts one column at the table’s head
147 void expand_table_cols_at_head(Workspace *workspace) {
150 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
152 DLOG("cols = %d\n", workspace->cols);
153 for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
154 DLOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
155 workspace->width_factor[cols] = workspace->width_factor[cols-1];
158 workspace->width_factor[0] = 0;
160 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
161 workspace->table[workspace->cols-1] = scalloc(sizeof(Container*) * workspace->rows);
163 /* Move the other columns */
164 for (int rows = 0; rows < workspace->rows; rows++)
165 for (int cols = workspace->cols - 1; cols > 0; cols--) {
166 DLOG("Moving col %d to %d\n", cols-1, cols);
167 workspace->table[cols][rows] = workspace->table[cols-1][rows];
168 workspace->table[cols][rows]->col = cols;
171 for (int rows = 0; rows < workspace->rows; rows++)
172 new_container(workspace, &(workspace->table[0][rows]), 0, rows, false);
176 * Shrinks the table by one column.
178 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
179 * function may only be called from move_*() or after making sure that the containers are freed
183 static void shrink_table_cols(Workspace *workspace) {
184 float free_space = workspace->width_factor[workspace->cols-1];
188 /* Shrink the width_factor array */
189 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
191 /* Free the container-pointers */
192 free(workspace->table[workspace->cols]);
194 /* Re-allocate the table */
195 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
197 /* Distribute the free space */
201 for (int cols = (workspace->cols-1); cols >= 0; cols--) {
202 if (workspace->width_factor[cols] == 0)
205 DLOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
206 workspace->width_factor[cols]);
207 workspace->width_factor[cols] += free_space;
213 * See shrink_table_cols()
216 static void shrink_table_rows(Workspace *workspace) {
217 float free_space = workspace->height_factor[workspace->rows-1];
220 for (int cols = 0; cols < workspace->cols; cols++)
221 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
223 /* Shrink the height_factor array */
224 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
226 /* Distribute the free space */
230 for (int rows = (workspace->rows-1); rows >= 0; rows--) {
231 if (workspace->height_factor[rows] == 0)
234 DLOG("Added free space (%f) to %d (had %f)\n", free_space, rows,
235 workspace->height_factor[rows]);
236 workspace->height_factor[rows] += free_space;
242 * Performs simple bounds checking for the given column/row
245 bool cell_exists(Workspace *ws, int col, int row) {
246 return (col >= 0 && col < ws->cols) &&
247 (row >= 0 && row < ws->rows);
250 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
251 Container *old_container = workspace->table[col][row];
253 if (old_container->mode == MODE_STACK || old_container->mode == MODE_TABBED)
254 leave_stack_mode(conn, old_container);
259 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
260 DLOG("firstly freeing \n");
262 /* Free the columns which are cleaned up */
263 for (int rows = 0; rows < workspace->rows; rows++)
264 free_container(conn, workspace, cols-1, rows);
266 for (; cols < workspace->cols; cols++)
267 for (int rows = 0; rows < workspace->rows; rows++) {
268 DLOG("at col = %d, row = %d\n", cols, rows);
269 Container *new_container = workspace->table[cols][rows];
271 DLOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
272 workspace->table[cols-1][rows] = new_container;
274 new_container->row = rows;
275 new_container->col = cols-1;
279 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
280 for (int cols = 0; cols < workspace->cols; cols++)
281 free_container(conn, workspace, cols, rows-1);
283 for (; rows < workspace->rows; rows++)
284 for (int cols = 0; cols < workspace->cols; cols++) {
285 Container *new_container = workspace->table[cols][rows];
287 DLOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
288 workspace->table[cols][rows-1] = new_container;
290 new_container->row = rows-1;
291 new_container->col = cols;
296 * Prints the table’s contents in human-readable form for debugging
299 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
300 DLOG("dump_table()\n");
301 FOR_TABLE(workspace) {
302 Container *con = workspace->table[cols][rows];
304 DLOG("at col=%d, row=%d\n", cols, rows);
305 DLOG("currently_focused = %p\n", con->currently_focused);
307 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
308 DLOG("got client %08x / %s\n", loop->child, loop->name);
316 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
319 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
320 DLOG("cleanup_table()\n");
322 /* Check for empty columns if we got more than one column */
323 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
324 bool completely_empty = true;
325 for (int rows = 0; rows < workspace->rows; rows++)
326 if (workspace->table[cols][rows]->currently_focused != NULL) {
327 completely_empty = false;
330 if (completely_empty) {
331 DLOG("Removing completely empty column %d\n", cols);
332 if (cols < (workspace->cols - 1))
333 move_columns_from(conn, workspace, cols+1);
335 for (int rows = 0; rows < workspace->rows; rows++)
336 free_container(conn, workspace, cols, rows);
338 shrink_table_cols(workspace);
340 if (workspace->current_col >= workspace->cols)
341 workspace->current_col = workspace->cols - 1;
345 /* Check for empty rows if we got more than one row */
346 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
347 bool completely_empty = true;
348 DLOG("Checking row %d\n", rows);
349 for (int cols = 0; cols < workspace->cols; cols++)
350 if (workspace->table[cols][rows]->currently_focused != NULL) {
351 completely_empty = false;
354 if (completely_empty) {
355 DLOG("Removing completely empty row %d\n", rows);
356 if (rows < (workspace->rows - 1))
357 move_rows_from(conn, workspace, rows+1);
359 for (int cols = 0; cols < workspace->cols; cols++)
360 free_container(conn, workspace, cols, rows);
362 shrink_table_rows(workspace);
364 if (workspace->current_row >= workspace->rows)
365 workspace->current_row = workspace->rows - 1;
369 /* Boundary checking for current_col and current_row */
370 if (current_col >= c_ws->cols)
371 current_col = c_ws->cols-1;
373 if (current_row >= c_ws->rows)
374 current_row = c_ws->rows-1;
376 if (CUR_CELL->currently_focused != NULL)
377 set_focus(conn, CUR_CELL->currently_focused, true);
381 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
384 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
385 DLOG("Fixing col/rowspan\n");
387 FOR_TABLE(workspace) {
388 Container *con = workspace->table[cols][rows];
389 if (con->colspan > 1) {
390 DLOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
391 while (con->colspan > 1 &&
392 (!cell_exists(workspace, cols + (con->colspan-1), rows) &&
393 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
395 DLOG("fixed it to %d\n", con->colspan);
397 if (con->rowspan > 1) {
398 DLOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
399 while (con->rowspan > 1 &&
400 (!cell_exists(workspace, cols, rows + (con->rowspan - 1)) &&
401 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
403 DLOG("fixed it to %d\n", con->rowspan);