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>
27 int current_workspace = 0;
28 Workspace workspaces[10];
29 /* Convenience pointer to the current workspace */
30 Workspace *c_ws = &workspaces[0];
39 memset(workspaces, 0, sizeof(workspaces));
41 for (int i = 0; i < 10; i++) {
42 workspaces[i].screen = NULL;
43 workspaces[i].num = i;
44 SLIST_INIT(&(workspaces[i].dock_clients));
45 expand_table_cols(&(workspaces[i]));
46 expand_table_rows(&(workspaces[i]));
50 static void new_container(Workspace *workspace, Container **container, int col, int row) {
52 new = *container = calloc(sizeof(Container), 1);
53 CIRCLEQ_INIT(&(new->clients));
58 new->workspace = workspace;
62 * Add one row to the table
65 void expand_table_rows(Workspace *workspace) {
68 for (int c = 0; c < workspace->cols; c++) {
69 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
70 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
75 * Add one column to the table
78 void expand_table_cols(Workspace *workspace) {
81 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
82 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
83 for (int c = 0; c < workspace->rows; c++)
84 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
87 static void shrink_table_cols(Workspace *workspace) {
89 free(workspace->table[workspace->cols]);
90 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
93 static void shrink_table_rows(Workspace *workspace) {
95 for (int cols = 0; cols < workspace->cols; cols++)
96 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
101 * Performs simple bounds checking for the given column/row
104 bool cell_exists(int col, int row) {
105 return (col >= 0 && col < c_ws->cols) &&
106 (row >= 0 && row < c_ws->rows);
109 static void move_columns_from(Workspace *workspace, int cols) {
110 for (; cols < workspace->cols; cols++)
111 for (int rows = 0; rows < workspace->rows; rows++) {
112 Container *old_container = workspace->table[cols-1][rows],
113 *new_container = workspace->table[cols][rows];
115 /* Fix the container backpointer for all clients */
117 CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
118 client->container = new_container;
122 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
123 workspace->table[cols-1][rows] = new_container;
125 new_container->row = rows;
126 new_container->col = cols-1;
128 workspace->table[cols][rows] = NULL;
132 static void move_rows_from(Workspace *workspace, int rows) {
133 for (; rows < workspace->rows; rows++)
134 for (int cols = 0; cols < workspace->cols; cols++) {
135 Container *old_container = workspace->table[cols][rows-1],
136 *new_container = workspace->table[cols][rows];
138 /* Fix the container backpointer for all clients */
140 CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
141 client->container = new_container;
145 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
146 workspace->table[cols][rows-1] = new_container;
147 new_container->row = rows-1;
148 new_container->col = cols;
149 workspace->table[cols][rows] = NULL;
154 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
157 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
158 printf("cleanup_table()\n");
160 /* Check for empty columns if we got more than one column */
161 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
162 bool completely_empty = true;
163 for (int rows = 0; rows < workspace->rows; rows++)
164 if (workspace->table[cols][rows]->currently_focused != NULL) {
165 completely_empty = false;
168 if (completely_empty) {
169 printf("Removing completely empty column %d\n", cols);
170 if (cols < (workspace->cols - 1))
171 move_columns_from(workspace, cols+1);
172 shrink_table_cols(workspace);
174 if (workspace->current_col >= workspace->cols)
175 workspace->current_col = workspace->cols - 1;
179 /* Check for empty rows if we got more than one row*/
180 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
181 bool completely_empty = true;
182 for (int cols = 0; cols < workspace->cols; cols++)
183 if (workspace->table[cols][rows]->currently_focused != NULL) {
184 completely_empty = false;
187 if (completely_empty) {
188 printf("Removing completely empty row %d\n", rows);
189 if (rows < (workspace->rows - 1))
190 move_rows_from(workspace, rows+1);
191 shrink_table_rows(workspace);
193 if (workspace->current_row >= workspace->rows)
194 workspace->current_row = workspace->rows - 1;
198 /* Boundary checking for current_col and current_row */
199 if (current_col >= c_ws->cols)
200 current_col = c_ws->cols-1;
202 if (current_row >= c_ws->rows)
203 current_row = c_ws->rows-1;
205 if (CUR_CELL->currently_focused != NULL)
206 set_focus(conn, CUR_CELL->currently_focused);