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 SLIST_INIT(&(workspaces[i].dock_clients));
44 expand_table_cols(&(workspaces[i]));
45 expand_table_rows(&(workspaces[i]));
49 static void new_container(Workspace *workspace, Container **container) {
51 new = *container = calloc(sizeof(Container), 1);
52 CIRCLEQ_INIT(&(new->clients));
55 new->workspace = workspace;
59 * Add one row to the table
62 void expand_table_rows(Workspace *workspace) {
65 for (int c = 0; c < workspace->cols; c++) {
66 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
67 new_container(workspace, &(workspace->table[c][workspace->rows-1]));
72 * Add one column to the table
75 void expand_table_cols(Workspace *workspace) {
78 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
79 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
80 for (int c = 0; c < workspace->rows; c++)
81 new_container(workspace, &(workspace->table[workspace->cols-1][c]));
84 static void shrink_table_cols(Workspace *workspace) {
86 free(workspace->table[workspace->cols]);
87 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
90 static void shrink_table_rows(Workspace *workspace) {
92 for (int cols = 0; cols < workspace->cols; cols++)
93 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
98 * Performs simple bounds checking for the given column/row
101 bool cell_exists(int col, int row) {
102 return (col >= 0 && col < c_ws->cols) &&
103 (row >= 0 && row < c_ws->rows);
106 static void move_columns_from(Workspace *workspace, int cols) {
107 for (; cols < workspace->cols; cols++)
108 for (int rows = 0; rows < workspace->rows; rows++) {
109 free(workspace->table[cols-1][rows]);
111 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
112 workspace->table[cols-1][rows] = workspace->table[cols][rows];
113 workspace->table[cols-1][rows]->col--;
114 workspace->table[cols][rows] = NULL;
118 static void move_rows_from(Workspace *workspace, int rows) {
119 for (; rows < workspace->rows; rows++)
120 for (int cols = 0; cols < workspace->cols; cols++) {
121 free(workspace->table[cols][rows-1]);
123 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
124 workspace->table[cols][rows-1] = workspace->table[cols][rows];
125 workspace->table[cols][rows-1]->row--;
126 workspace->table[cols][rows] = NULL;
131 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
134 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
135 printf("cleanup_table()\n");
137 /* Check for empty columns if we got more than one column */
138 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
139 bool completely_empty = true;
140 for (int rows = 0; rows < workspace->rows; rows++)
141 if (workspace->table[cols][rows]->currently_focused != NULL) {
142 completely_empty = false;
145 if (completely_empty) {
146 printf("Removing completely empty column %d\n", cols);
147 if (cols < (workspace->cols - 1))
148 move_columns_from(workspace, cols+1);
149 shrink_table_cols(workspace);
151 if (workspace->current_col >= workspace->cols)
152 workspace->current_col = workspace->cols - 1;
156 /* Check for empty rows if we got more than one row*/
157 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
158 bool completely_empty = true;
159 for (int cols = 0; cols < workspace->cols; cols++)
160 if (workspace->table[cols][rows]->currently_focused != NULL) {
161 completely_empty = false;
164 if (completely_empty) {
165 printf("Removing completely empty row %d\n", rows);
166 if (rows < (workspace->rows - 1))
167 move_rows_from(workspace, rows+1);
168 shrink_table_rows(workspace);
170 if (workspace->current_row >= workspace->rows)
171 workspace->current_row = workspace->rows - 1;
175 /* Boundary checking for current_col and current_row */
176 if (current_col >= c_ws->cols)
177 current_col = c_ws->cols-1;
179 if (current_row >= c_ws->rows)
180 current_row = c_ws->rows-1;
182 if (CUR_CELL->currently_focused != NULL)
183 set_focus(conn, CUR_CELL->currently_focused);