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 printf("firstly freeing \n");
113 /* Clean up the column to be freed */
114 for (int rows = 0; rows < workspace->rows; rows++) {
115 Container *old_container = workspace->table[cols-1][rows];
117 if (old_container->mode == MODE_STACK)
118 leave_stack_mode(conn, old_container);
123 for (; cols < workspace->cols; cols++)
124 for (int rows = 0; rows < workspace->rows; rows++) {
125 printf("at col = %d, row = %d\n", cols, rows);
126 Container *new_container = workspace->table[cols][rows];
128 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
129 workspace->table[cols-1][rows] = new_container;
131 new_container->row = rows;
132 new_container->col = cols-1;
136 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
137 for (int cols = 0; cols < workspace->cols; cols++) {
138 Container *old_container = workspace->table[cols][rows-1];
140 if (old_container->mode == MODE_STACK)
141 leave_stack_mode(conn, old_container);
145 for (; rows < workspace->rows; rows++)
146 for (int cols = 0; cols < workspace->cols; cols++) {
147 Container *new_container = workspace->table[cols][rows];
149 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
150 workspace->table[cols][rows-1] = new_container;
152 new_container->row = rows-1;
153 new_container->col = cols;
157 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
158 printf("dump_table()\n");
159 for (int cols = 0; cols < workspace->cols; cols++) {
160 for (int rows = 0; rows < workspace->rows; rows++) {
161 Container *con = workspace->table[cols][rows];
163 printf("at col=%d, row=%d\n", cols, rows);
164 printf("currently_focused = %p\n", con->currently_focused);
166 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
167 printf("got client %08x / %s\n", loop->child, loop->name);
176 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
179 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
180 printf("cleanup_table()\n");
182 /* Check for empty columns if we got more than one column */
183 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
184 bool completely_empty = true;
185 for (int rows = 0; rows < workspace->rows; rows++)
186 if (workspace->table[cols][rows]->currently_focused != NULL) {
187 completely_empty = false;
190 if (completely_empty) {
191 printf("Removing completely empty column %d\n", cols);
192 if (cols < (workspace->cols - 1))
193 move_columns_from(conn, workspace, cols+1);
194 shrink_table_cols(workspace);
196 if (workspace->current_col >= workspace->cols)
197 workspace->current_col = workspace->cols - 1;
201 /* Check for empty rows if we got more than one row*/
202 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
203 bool completely_empty = true;
204 for (int cols = 0; cols < workspace->cols; cols++)
205 if (workspace->table[cols][rows]->currently_focused != NULL) {
206 completely_empty = false;
209 if (completely_empty) {
210 printf("Removing completely empty row %d\n", rows);
211 if (rows < (workspace->rows - 1))
212 move_rows_from(conn, workspace, rows+1);
213 shrink_table_rows(workspace);
215 if (workspace->current_row >= workspace->rows)
216 workspace->current_row = workspace->rows - 1;
220 /* Boundary checking for current_col and current_row */
221 if (current_col >= c_ws->cols)
222 current_col = c_ws->cols-1;
224 if (current_row >= c_ws->rows)
225 current_row = c_ws->rows-1;
227 if (CUR_CELL->currently_focused != NULL)
228 set_focus(conn, CUR_CELL->currently_focused);
232 * Fixes col/rowspan (makes sure there are no overlapping windows)
235 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
236 printf("Fixing col/rowspan\n");
238 for (int cols = 0; cols < workspace->cols; cols++)
239 for (int rows = 0; rows < workspace->rows; rows++) {
240 Container *con = workspace->table[cols][rows];
241 if (con->colspan > 1) {
242 printf("gots one with colspan %d\n", con->colspan);
243 while (con->colspan > 1 &&
244 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
246 printf("fixed it to %d\n", con->colspan);
248 if (con->rowspan > 1) {
249 printf("gots one with rowspan %d\n", con->rowspan);
250 while (con->rowspan > 1 &&
251 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
253 printf("fixed it to %d\n", con->rowspan);