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);
88 static void shrink_table_cols(Workspace *workspace) {
90 free(workspace->table[workspace->cols]);
91 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
94 static void shrink_table_rows(Workspace *workspace) {
96 for (int cols = 0; cols < workspace->cols; cols++)
97 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
102 * Performs simple bounds checking for the given column/row
105 bool cell_exists(int col, int row) {
106 return (col >= 0 && col < c_ws->cols) &&
107 (row >= 0 && row < c_ws->rows);
110 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
111 for (; cols < workspace->cols; cols++)
112 for (int rows = 0; rows < workspace->rows; rows++) {
113 Container *old_container = workspace->table[cols-1][rows],
114 *new_container = workspace->table[cols][rows];
116 /* Fix the container backpointer for all clients */
118 CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
119 client->container = new_container;
121 if (old_container->mode == MODE_STACK)
122 leave_stack_mode(conn, old_container);
126 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
127 workspace->table[cols-1][rows] = new_container;
129 new_container->row = rows;
130 new_container->col = cols-1;
132 workspace->table[cols][rows] = NULL;
136 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
137 for (; rows < workspace->rows; rows++)
138 for (int cols = 0; cols < workspace->cols; cols++) {
139 Container *old_container = workspace->table[cols][rows-1],
140 *new_container = workspace->table[cols][rows];
142 /* Fix the container backpointer for all clients */
144 CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
145 client->container = new_container;
147 if (old_container->mode == MODE_STACK)
148 leave_stack_mode(conn, old_container);
152 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
153 workspace->table[cols][rows-1] = new_container;
154 new_container->row = rows-1;
155 new_container->col = cols;
156 workspace->table[cols][rows] = NULL;
161 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
164 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
165 printf("cleanup_table()\n");
167 /* Check for empty columns if we got more than one column */
168 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
169 bool completely_empty = true;
170 for (int rows = 0; rows < workspace->rows; rows++)
171 if (workspace->table[cols][rows]->currently_focused != NULL) {
172 completely_empty = false;
175 if (completely_empty) {
176 printf("Removing completely empty column %d\n", cols);
177 if (cols < (workspace->cols - 1))
178 move_columns_from(conn, workspace, cols+1);
179 shrink_table_cols(workspace);
181 if (workspace->current_col >= workspace->cols)
182 workspace->current_col = workspace->cols - 1;
186 /* Check for empty rows if we got more than one row*/
187 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
188 bool completely_empty = true;
189 for (int cols = 0; cols < workspace->cols; cols++)
190 if (workspace->table[cols][rows]->currently_focused != NULL) {
191 completely_empty = false;
194 if (completely_empty) {
195 printf("Removing completely empty row %d\n", rows);
196 if (rows < (workspace->rows - 1))
197 move_rows_from(conn, workspace, rows+1);
198 shrink_table_rows(workspace);
200 if (workspace->current_row >= workspace->rows)
201 workspace->current_row = workspace->rows - 1;
205 /* Boundary checking for current_col and current_row */
206 if (current_col >= c_ws->cols)
207 current_col = c_ws->cols-1;
209 if (current_row >= c_ws->rows)
210 current_row = c_ws->rows-1;
212 if (CUR_CELL->currently_focused != NULL)
213 set_focus(conn, CUR_CELL->currently_focused);