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"
31 int current_workspace = 0;
32 int num_workspaces = 1;
33 struct workspaces_head *workspaces;
34 /* Convenience pointer to the current workspace */
44 workspaces = scalloc(sizeof(struct workspaces_head));
45 TAILQ_INIT(workspaces);
47 c_ws = scalloc(sizeof(Workspace));
48 workspace_set_name(c_ws, NULL);
49 TAILQ_INIT(&(c_ws->floating_clients));
50 TAILQ_INSERT_TAIL(workspaces, c_ws, workspaces);
53 static void new_container(Workspace *workspace, Container **container, int col, int row, bool skip_layout_switch) {
55 new = *container = calloc(sizeof(Container), 1);
56 CIRCLEQ_INIT(&(new->clients));
61 new->workspace = workspace;
62 if (!skip_layout_switch)
63 switch_layout_mode(global_conn, new, config.container_mode);
64 new->stack_limit = config.container_stack_limit;
65 new->stack_limit_value = config.container_stack_limit_value;
69 * Add one row to the table
72 void expand_table_rows(Workspace *workspace) {
75 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
76 workspace->height_factor[workspace->rows-1] = 0;
78 for (int c = 0; c < workspace->cols; c++) {
79 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
80 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1, true);
83 /* We need to switch the layout in a separate step because it could
84 * happen that render_layout() (being called by switch_layout_mode())
85 * would access containers which were not yet initialized. */
86 for (int c = 0; c < workspace->cols; c++)
87 switch_layout_mode(global_conn, workspace->table[c][workspace->rows-1], config.container_mode);
91 * Adds one row at the head of the table
94 void expand_table_rows_at_head(Workspace *workspace) {
97 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
99 LOG("rows = %d\n", workspace->rows);
100 for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
101 LOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
102 workspace->height_factor[rows] = workspace->height_factor[rows-1];
105 workspace->height_factor[0] = 0;
107 for (int cols = 0; cols < workspace->cols; cols++)
108 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
110 /* Move the other rows */
111 for (int cols = 0; cols < workspace->cols; cols++)
112 for (int rows = workspace->rows - 1; rows > 0; rows--) {
113 LOG("Moving row %d to %d\n", rows-1, rows);
114 workspace->table[cols][rows] = workspace->table[cols][rows-1];
115 workspace->table[cols][rows]->row = rows;
118 for (int cols = 0; cols < workspace->cols; cols++)
119 new_container(workspace, &(workspace->table[cols][0]), cols, 0, false);
123 * Add one column to the table
126 void expand_table_cols(Workspace *workspace) {
129 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
130 workspace->width_factor[workspace->cols-1] = 0;
132 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
133 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
135 for (int c = 0; c < workspace->rows; c++)
136 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c, true);
138 for (int c = 0; c < workspace->rows; c++)
139 switch_layout_mode(global_conn, workspace->table[workspace->cols-1][c], config.container_mode);
143 * Inserts one column at the table’s head
146 void expand_table_cols_at_head(Workspace *workspace) {
149 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
151 LOG("cols = %d\n", workspace->cols);
152 for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
153 LOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
154 workspace->width_factor[cols] = workspace->width_factor[cols-1];
157 workspace->width_factor[0] = 0;
159 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
160 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
162 /* Move the other columns */
163 for (int rows = 0; rows < workspace->rows; rows++)
164 for (int cols = workspace->cols - 1; cols > 0; cols--) {
165 LOG("Moving col %d to %d\n", cols-1, cols);
166 workspace->table[cols][rows] = workspace->table[cols-1][rows];
167 workspace->table[cols][rows]->col = cols;
170 for (int rows = 0; rows < workspace->rows; rows++)
171 new_container(workspace, &(workspace->table[0][rows]), 0, rows, false);
175 * Shrinks the table by one column.
177 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
178 * function may only be called from move_*() or after making sure that the containers are freed
182 static void shrink_table_cols(Workspace *workspace) {
183 float free_space = workspace->width_factor[workspace->cols-1];
187 /* Shrink the width_factor array */
188 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
190 /* Free the container-pointers */
191 free(workspace->table[workspace->cols]);
193 /* Re-allocate the table */
194 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
196 /* Distribute the free space */
200 for (int cols = (workspace->cols-1); cols >= 0; cols--) {
201 if (workspace->width_factor[cols] == 0)
204 LOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
205 workspace->width_factor[cols]);
206 workspace->width_factor[cols] += free_space;
212 * See shrink_table_cols()
215 static void shrink_table_rows(Workspace *workspace) {
216 float free_space = workspace->height_factor[workspace->rows-1];
219 for (int cols = 0; cols < workspace->cols; cols++)
220 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
222 /* Shrink the height_factor array */
223 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
225 /* Distribute the free space */
229 for (int rows = (workspace->rows-1); rows >= 0; rows--) {
230 if (workspace->height_factor[rows] == 0)
233 LOG("Added free space (%f) to %d (had %f)\n", free_space, rows,
234 workspace->height_factor[rows]);
235 workspace->height_factor[rows] += free_space;
241 * Performs simple bounds checking for the given column/row
244 bool cell_exists(int col, int row) {
245 return (col >= 0 && col < c_ws->cols) &&
246 (row >= 0 && row < c_ws->rows);
249 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
250 Container *old_container = workspace->table[col][row];
252 if (old_container->mode == MODE_STACK || old_container->mode == MODE_TABBED)
253 leave_stack_mode(conn, old_container);
258 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
259 LOG("firstly freeing \n");
261 /* Free the columns which are cleaned up */
262 for (int rows = 0; rows < workspace->rows; rows++)
263 free_container(conn, workspace, cols-1, rows);
265 for (; cols < workspace->cols; cols++)
266 for (int rows = 0; rows < workspace->rows; rows++) {
267 LOG("at col = %d, row = %d\n", cols, rows);
268 Container *new_container = workspace->table[cols][rows];
270 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
271 workspace->table[cols-1][rows] = new_container;
273 new_container->row = rows;
274 new_container->col = cols-1;
278 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
279 for (int cols = 0; cols < workspace->cols; cols++)
280 free_container(conn, workspace, cols, rows-1);
282 for (; rows < workspace->rows; rows++)
283 for (int cols = 0; cols < workspace->cols; cols++) {
284 Container *new_container = workspace->table[cols][rows];
286 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
287 workspace->table[cols][rows-1] = new_container;
289 new_container->row = rows-1;
290 new_container->col = cols;
295 * Prints the table’s contents in human-readable form for debugging
298 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
299 LOG("dump_table()\n");
300 FOR_TABLE(workspace) {
301 Container *con = workspace->table[cols][rows];
303 LOG("at col=%d, row=%d\n", cols, rows);
304 LOG("currently_focused = %p\n", con->currently_focused);
306 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
307 LOG("got client %08x / %s\n", loop->child, loop->name);
315 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
318 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
319 LOG("cleanup_table()\n");
321 /* Check for empty columns if we got more than one column */
322 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
323 bool completely_empty = true;
324 for (int rows = 0; rows < workspace->rows; rows++)
325 if (workspace->table[cols][rows]->currently_focused != NULL) {
326 completely_empty = false;
329 if (completely_empty) {
330 LOG("Removing completely empty column %d\n", cols);
331 if (cols < (workspace->cols - 1))
332 move_columns_from(conn, workspace, cols+1);
334 for (int rows = 0; rows < workspace->rows; rows++)
335 free_container(conn, workspace, cols, rows);
337 shrink_table_cols(workspace);
339 if (workspace->current_col >= workspace->cols)
340 workspace->current_col = workspace->cols - 1;
344 /* Check for empty rows if we got more than one row */
345 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
346 bool completely_empty = true;
347 LOG("Checking row %d\n", rows);
348 for (int cols = 0; cols < workspace->cols; cols++)
349 if (workspace->table[cols][rows]->currently_focused != NULL) {
350 completely_empty = false;
353 if (completely_empty) {
354 LOG("Removing completely empty row %d\n", rows);
355 if (rows < (workspace->rows - 1))
356 move_rows_from(conn, workspace, rows+1);
358 for (int cols = 0; cols < workspace->cols; cols++)
359 free_container(conn, workspace, cols, rows);
361 shrink_table_rows(workspace);
363 if (workspace->current_row >= workspace->rows)
364 workspace->current_row = workspace->rows - 1;
368 /* Boundary checking for current_col and current_row */
369 if (current_col >= c_ws->cols)
370 current_col = c_ws->cols-1;
372 if (current_row >= c_ws->rows)
373 current_row = c_ws->rows-1;
375 if (CUR_CELL->currently_focused != NULL)
376 set_focus(conn, CUR_CELL->currently_focused, true);
380 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
383 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
384 LOG("Fixing col/rowspan\n");
386 FOR_TABLE(workspace) {
387 Container *con = workspace->table[cols][rows];
388 if (con->colspan > 1) {
389 LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
390 while (con->colspan > 1 &&
391 (!cell_exists(cols + (con->colspan-1), rows) ||
392 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
394 LOG("fixed it to %d\n", con->colspan);
396 if (con->rowspan > 1) {
397 LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
398 while (con->rowspan > 1 &&
399 (!cell_exists(cols, rows + (con->rowspan - 1)) ||
400 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
402 LOG("fixed it to %d\n", con->rowspan);