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 expand_table_cols(&(workspaces[i]));
47 expand_table_rows(&(workspaces[i]));
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;
63 * Add one row to the table
66 void expand_table_rows(Workspace *workspace) {
69 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
70 workspace->height_factor[workspace->rows-1] = 0;
72 for (int c = 0; c < workspace->cols; c++) {
73 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
74 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
79 * Adds one row at the head of the table
82 void expand_table_rows_at_head(Workspace *workspace) {
85 workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
87 LOG("rows = %d\n", workspace->rows);
88 for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
89 LOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
90 workspace->height_factor[rows] = workspace->height_factor[rows-1];
93 workspace->height_factor[0] = 0;
95 for (int cols = 0; cols < workspace->cols; cols++)
96 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
98 /* Move the other rows */
99 for (int cols = 0; cols < workspace->cols; cols++)
100 for (int rows = workspace->rows - 1; rows > 0; rows--) {
101 LOG("Moving row %d to %d\n", rows-1, rows);
102 workspace->table[cols][rows] = workspace->table[cols][rows-1];
103 workspace->table[cols][rows]->row = rows;
106 for (int cols = 0; cols < workspace->cols; cols++)
107 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
111 * Add one column to the table
114 void expand_table_cols(Workspace *workspace) {
117 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
118 workspace->width_factor[workspace->cols-1] = 0;
120 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
121 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
122 for (int c = 0; c < workspace->rows; c++)
123 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
127 * Inserts one column at the table’s head
130 void expand_table_cols_at_head(Workspace *workspace) {
133 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
135 LOG("cols = %d\n", workspace->cols);
136 for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
137 LOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
138 workspace->width_factor[cols] = workspace->width_factor[cols-1];
141 workspace->width_factor[0] = 0;
143 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
144 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
146 /* Move the other columns */
147 for (int rows = 0; rows < workspace->rows; rows++)
148 for (int cols = workspace->cols - 1; cols > 0; cols--) {
149 LOG("Moving col %d to %d\n", cols-1, cols);
150 workspace->table[cols][rows] = workspace->table[cols-1][rows];
151 workspace->table[cols][rows]->col = cols;
154 for (int rows = 0; rows < workspace->rows; rows++)
155 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
159 * Shrinks the table by one column.
161 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
162 * function may only be called from move_*() or after making sure that the containers are freed
166 static void shrink_table_cols(Workspace *workspace) {
167 float free_space = workspace->width_factor[workspace->cols-1];
171 /* Shrink the width_factor array */
172 workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
174 /* Free the container-pointers */
175 free(workspace->table[workspace->cols]);
177 /* Re-allocate the table */
178 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
180 /* Distribute the free space */
184 for (int cols = (workspace->cols-1); cols >= 0; cols--) {
185 if (workspace->width_factor[cols] == 0)
188 LOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
189 workspace->width_factor[cols]);
190 workspace->width_factor[cols] += free_space;
196 * See shrink_table_cols()
199 static void shrink_table_rows(Workspace *workspace) {
201 for (int cols = 0; cols < workspace->cols; cols++)
202 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
207 * Performs simple bounds checking for the given column/row
210 bool cell_exists(int col, int row) {
211 return (col >= 0 && col < c_ws->cols) &&
212 (row >= 0 && row < c_ws->rows);
215 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
216 Container *old_container = workspace->table[col][row];
218 if (old_container->mode == MODE_STACK)
219 leave_stack_mode(conn, old_container);
224 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
225 LOG("firstly freeing \n");
227 /* Free the columns which are cleaned up */
228 for (int rows = 0; rows < workspace->rows; rows++)
229 free_container(conn, workspace, cols-1, rows);
231 for (; cols < workspace->cols; cols++)
232 for (int rows = 0; rows < workspace->rows; rows++) {
233 LOG("at col = %d, row = %d\n", cols, rows);
234 Container *new_container = workspace->table[cols][rows];
236 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
237 workspace->table[cols-1][rows] = new_container;
239 new_container->row = rows;
240 new_container->col = cols-1;
244 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
245 for (int cols = 0; cols < workspace->cols; cols++)
246 free_container(conn, workspace, cols, rows-1);
248 for (; rows < workspace->rows; rows++)
249 for (int cols = 0; cols < workspace->cols; cols++) {
250 Container *new_container = workspace->table[cols][rows];
252 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
253 workspace->table[cols][rows-1] = new_container;
255 new_container->row = rows-1;
256 new_container->col = cols;
261 * Prints the table’s contents in human-readable form for debugging
264 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
265 LOG("dump_table()\n");
266 FOR_TABLE(workspace) {
267 Container *con = workspace->table[cols][rows];
269 LOG("at col=%d, row=%d\n", cols, rows);
270 LOG("currently_focused = %p\n", con->currently_focused);
272 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
273 LOG("got client %08x / %s\n", loop->child, loop->name);
281 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
284 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
285 LOG("cleanup_table()\n");
287 /* Check for empty columns if we got more than one column */
288 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
289 bool completely_empty = true;
290 for (int rows = 0; rows < workspace->rows; rows++)
291 if (workspace->table[cols][rows]->currently_focused != NULL) {
292 completely_empty = false;
295 if (completely_empty) {
296 LOG("Removing completely empty column %d\n", cols);
297 if (cols < (workspace->cols - 1))
298 move_columns_from(conn, workspace, cols+1);
300 for (int rows = 0; rows < workspace->rows; rows++)
301 free_container(conn, workspace, cols, rows);
303 shrink_table_cols(workspace);
305 if (workspace->current_col >= workspace->cols)
306 workspace->current_col = workspace->cols - 1;
310 /* Check for empty rows if we got more than one row */
311 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
312 bool completely_empty = true;
313 LOG("Checking row %d\n", rows);
314 for (int cols = 0; cols < workspace->cols; cols++)
315 if (workspace->table[cols][rows]->currently_focused != NULL) {
316 completely_empty = false;
319 if (completely_empty) {
320 LOG("Removing completely empty row %d\n", rows);
321 if (rows < (workspace->rows - 1))
322 move_rows_from(conn, workspace, rows+1);
324 for (int cols = 0; cols < workspace->cols; cols++)
325 free_container(conn, workspace, cols, rows);
327 shrink_table_rows(workspace);
329 if (workspace->current_row >= workspace->rows)
330 workspace->current_row = workspace->rows - 1;
334 /* Boundary checking for current_col and current_row */
335 if (current_col >= c_ws->cols)
336 current_col = c_ws->cols-1;
338 if (current_row >= c_ws->rows)
339 current_row = c_ws->rows-1;
341 if (CUR_CELL->currently_focused != NULL)
342 set_focus(conn, CUR_CELL->currently_focused, true);
346 * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
349 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
350 LOG("Fixing col/rowspan\n");
352 FOR_TABLE(workspace) {
353 Container *con = workspace->table[cols][rows];
354 if (con->colspan > 1) {
355 LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
356 while (con->colspan > 1 &&
357 (!cell_exists(cols + (con->colspan-1), rows) ||
358 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
360 LOG("fixed it to %d\n", con->colspan);
362 if (con->rowspan > 1) {
363 LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
364 while (con->rowspan > 1 &&
365 (!cell_exists(cols, rows + (con->rowspan - 1)) ||
366 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
368 LOG("fixed it to %d\n", con->rowspan);