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>
28 int current_workspace = 0;
29 Workspace workspaces[10];
30 /* Convenience pointer to the current workspace */
31 Workspace *c_ws = &workspaces[0];
40 memset(workspaces, 0, sizeof(workspaces));
42 for (int i = 0; i < 10; i++) {
43 workspaces[i].screen = NULL;
44 workspaces[i].num = i;
45 SLIST_INIT(&(workspaces[i].dock_clients));
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 for (int c = 0; c < workspace->cols; c++) {
70 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
71 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
76 * Adds one row at the head of the table
79 void expand_table_rows_at_head(Workspace *workspace) {
82 for (int cols = 0; cols < workspace->cols; cols++)
83 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
85 /* Move the other rows */
86 for (int cols = 0; cols < workspace->cols; cols++)
87 for (int rows = workspace->rows - 1; rows > 0; rows--) {
88 LOG("Moving row %d to %d\n", rows-1, rows);
89 workspace->table[cols][rows] = workspace->table[cols][rows-1];
90 workspace->table[cols][rows]->row = rows;
92 for (int cols = 0; cols < workspace->cols; cols++)
93 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
97 * Add one column to the table
100 void expand_table_cols(Workspace *workspace) {
103 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
104 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
105 for (int c = 0; c < workspace->rows; c++)
106 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
110 * Inserts one column at the table’s head
113 void expand_table_cols_at_head(Workspace *workspace) {
116 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
117 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
119 /* Move the other columns */
120 for (int rows = 0; rows < workspace->rows; rows++)
121 for (int cols = workspace->cols - 1; cols > 0; cols--) {
122 LOG("Moving col %d to %d\n", cols-1, cols);
123 workspace->table[cols][rows] = workspace->table[cols-1][rows];
124 workspace->table[cols][rows]->col = cols;
127 for (int rows = 0; rows < workspace->rows; rows++)
128 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
132 * Shrinks the table by one column.
134 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
135 * function may only be called from move_*() or after making sure that the containers are freed
139 static void shrink_table_cols(Workspace *workspace) {
142 /* Free the container-pointers */
143 free(workspace->table[workspace->cols]);
145 /* Re-allocate the table */
146 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
150 * See shrink_table_cols()
153 static void shrink_table_rows(Workspace *workspace) {
155 for (int cols = 0; cols < workspace->cols; cols++)
156 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
161 * Performs simple bounds checking for the given column/row
164 bool cell_exists(int col, int row) {
165 return (col >= 0 && col < c_ws->cols) &&
166 (row >= 0 && row < c_ws->rows);
169 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
170 Container *old_container = workspace->table[col][row];
172 if (old_container->mode == MODE_STACK)
173 leave_stack_mode(conn, old_container);
178 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
179 LOG("firstly freeing \n");
181 /* Free the columns which are cleaned up */
182 for (int rows = 0; rows < workspace->rows; rows++)
183 free_container(conn, workspace, cols-1, rows);
185 for (; cols < workspace->cols; cols++)
186 for (int rows = 0; rows < workspace->rows; rows++) {
187 LOG("at col = %d, row = %d\n", cols, rows);
188 Container *new_container = workspace->table[cols][rows];
190 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
191 workspace->table[cols-1][rows] = new_container;
193 new_container->row = rows;
194 new_container->col = cols-1;
198 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
199 for (int cols = 0; cols < workspace->cols; cols++)
200 free_container(conn, workspace, cols, rows-1);
202 for (; rows < workspace->rows; rows++)
203 for (int cols = 0; cols < workspace->cols; cols++) {
204 Container *new_container = workspace->table[cols][rows];
206 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
207 workspace->table[cols][rows-1] = new_container;
209 new_container->row = rows-1;
210 new_container->col = cols;
214 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
215 LOG("dump_table()\n");
216 FOR_TABLE(workspace) {
217 Container *con = workspace->table[cols][rows];
219 LOG("at col=%d, row=%d\n", cols, rows);
220 LOG("currently_focused = %p\n", con->currently_focused);
222 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
223 LOG("got client %08x / %s\n", loop->child, loop->name);
231 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
234 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
235 LOG("cleanup_table()\n");
237 /* Check for empty columns if we got more than one column */
238 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
239 bool completely_empty = true;
240 for (int rows = 0; rows < workspace->rows; rows++)
241 if (workspace->table[cols][rows]->currently_focused != NULL) {
242 completely_empty = false;
245 if (completely_empty) {
246 LOG("Removing completely empty column %d\n", cols);
247 if (cols < (workspace->cols - 1))
248 move_columns_from(conn, workspace, cols+1);
250 for (int rows = 0; rows < workspace->rows; rows++)
251 free_container(conn, workspace, cols, rows);
253 shrink_table_cols(workspace);
255 if (workspace->current_col >= workspace->cols)
256 workspace->current_col = workspace->cols - 1;
260 /* Check for empty rows if we got more than one row */
261 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
262 bool completely_empty = true;
263 LOG("Checking row %d\n", rows);
264 for (int cols = 0; cols < workspace->cols; cols++)
265 if (workspace->table[cols][rows]->currently_focused != NULL) {
266 completely_empty = false;
269 if (completely_empty) {
270 LOG("Removing completely empty row %d\n", rows);
271 if (rows < (workspace->rows - 1))
272 move_rows_from(conn, workspace, rows+1);
274 for (int cols = 0; cols < workspace->cols; cols++)
275 free_container(conn, workspace, cols, rows);
277 shrink_table_rows(workspace);
279 if (workspace->current_row >= workspace->rows)
280 workspace->current_row = workspace->rows - 1;
284 /* Boundary checking for current_col and current_row */
285 if (current_col >= c_ws->cols)
286 current_col = c_ws->cols-1;
288 if (current_row >= c_ws->rows)
289 current_row = c_ws->rows-1;
291 if (CUR_CELL->currently_focused != NULL)
292 set_focus(conn, CUR_CELL->currently_focused);
296 * Fixes col/rowspan (makes sure there are no overlapping windows)
299 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
300 LOG("Fixing col/rowspan\n");
302 FOR_TABLE(workspace) {
303 Container *con = workspace->table[cols][rows];
304 if (con->colspan > 1) {
305 LOG("gots one with colspan %d\n", con->colspan);
306 while (con->colspan > 1 &&
307 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
309 LOG("fixed it to %d\n", con->colspan);
311 if (con->rowspan > 1) {
312 LOG("gots one with rowspan %d\n", con->rowspan);
313 while (con->rowspan > 1 &&
314 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
316 LOG("fixed it to %d\n", con->rowspan);