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 * Add one column to the table
79 void expand_table_cols(Workspace *workspace) {
82 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
83 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
84 for (int c = 0; c < workspace->rows; c++)
85 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
89 * Shrinks the table by one column.
91 * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
92 * function may only be called from move_*() or after making sure that the containers are freed
96 static void shrink_table_cols(Workspace *workspace) {
99 /* Free the container-pointers */
100 free(workspace->table[workspace->cols]);
102 /* Re-allocate the table */
103 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
107 * See shrink_table_cols()
110 static void shrink_table_rows(Workspace *workspace) {
112 for (int cols = 0; cols < workspace->cols; cols++)
113 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
118 * Performs simple bounds checking for the given column/row
121 bool cell_exists(int col, int row) {
122 return (col >= 0 && col < c_ws->cols) &&
123 (row >= 0 && row < c_ws->rows);
126 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
127 Container *old_container = workspace->table[col][row];
129 if (old_container->mode == MODE_STACK)
130 leave_stack_mode(conn, old_container);
135 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
136 LOG("firstly freeing \n");
138 /* Free the columns which are cleaned up */
139 for (int rows = 0; rows < workspace->rows; rows++)
140 free_container(conn, workspace, cols-1, rows);
142 for (; cols < workspace->cols; cols++)
143 for (int rows = 0; rows < workspace->rows; rows++) {
144 LOG("at col = %d, row = %d\n", cols, rows);
145 Container *new_container = workspace->table[cols][rows];
147 LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
148 workspace->table[cols-1][rows] = new_container;
150 new_container->row = rows;
151 new_container->col = cols-1;
155 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
156 for (int cols = 0; cols < workspace->cols; cols++)
157 free_container(conn, workspace, cols, rows-1);
159 for (; rows < workspace->rows; rows++)
160 for (int cols = 0; cols < workspace->cols; cols++) {
161 Container *new_container = workspace->table[cols][rows];
163 LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
164 workspace->table[cols][rows-1] = new_container;
166 new_container->row = rows-1;
167 new_container->col = cols;
171 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
172 LOG("dump_table()\n");
173 FOR_TABLE(workspace) {
174 Container *con = workspace->table[cols][rows];
176 LOG("at col=%d, row=%d\n", cols, rows);
177 LOG("currently_focused = %p\n", con->currently_focused);
179 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
180 LOG("got client %08x / %s\n", loop->child, loop->name);
188 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
191 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
192 LOG("cleanup_table()\n");
194 /* Check for empty columns if we got more than one column */
195 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
196 bool completely_empty = true;
197 for (int rows = 0; rows < workspace->rows; rows++)
198 if (workspace->table[cols][rows]->currently_focused != NULL) {
199 completely_empty = false;
202 if (completely_empty) {
203 LOG("Removing completely empty column %d\n", cols);
204 if (cols < (workspace->cols - 1))
205 move_columns_from(conn, workspace, cols+1);
207 for (int rows = 0; rows < workspace->rows; rows++)
208 free_container(conn, workspace, cols, rows);
210 shrink_table_cols(workspace);
212 if (workspace->current_col >= workspace->cols)
213 workspace->current_col = workspace->cols - 1;
217 /* Check for empty rows if we got more than one row */
218 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
219 bool completely_empty = true;
220 LOG("Checking row %d\n", rows);
221 for (int cols = 0; cols < workspace->cols; cols++)
222 if (workspace->table[cols][rows]->currently_focused != NULL) {
223 completely_empty = false;
226 if (completely_empty) {
227 LOG("Removing completely empty row %d\n", rows);
228 if (rows < (workspace->rows - 1))
229 move_rows_from(conn, workspace, rows+1);
231 for (int cols = 0; cols < workspace->cols; cols++)
232 free_container(conn, workspace, cols, rows);
234 shrink_table_rows(workspace);
236 if (workspace->current_row >= workspace->rows)
237 workspace->current_row = workspace->rows - 1;
241 /* Boundary checking for current_col and current_row */
242 if (current_col >= c_ws->cols)
243 current_col = c_ws->cols-1;
245 if (current_row >= c_ws->rows)
246 current_row = c_ws->rows-1;
248 if (CUR_CELL->currently_focused != NULL)
249 set_focus(conn, CUR_CELL->currently_focused);
253 * Fixes col/rowspan (makes sure there are no overlapping windows)
256 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
257 LOG("Fixing col/rowspan\n");
259 FOR_TABLE(workspace) {
260 Container *con = workspace->table[cols][rows];
261 if (con->colspan > 1) {
262 LOG("gots one with colspan %d\n", con->colspan);
263 while (con->colspan > 1 &&
264 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
266 LOG("fixed it to %d\n", con->colspan);
268 if (con->rowspan > 1) {
269 LOG("gots one with rowspan %d\n", con->rowspan);
270 while (con->rowspan > 1 &&
271 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
273 LOG("fixed it to %d\n", con->rowspan);