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) {
52 new = *container = calloc(sizeof(Container), 1);
53 CIRCLEQ_INIT(&(new->clients));
56 new->workspace = workspace;
60 * Add one row to the table
63 void expand_table_rows(Workspace *workspace) {
66 for (int c = 0; c < workspace->cols; c++) {
67 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
68 new_container(workspace, &(workspace->table[c][workspace->rows-1]));
73 * Add one column to the table
76 void expand_table_cols(Workspace *workspace) {
79 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
80 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
81 for (int c = 0; c < workspace->rows; c++)
82 new_container(workspace, &(workspace->table[workspace->cols-1][c]));
85 static void shrink_table_cols(Workspace *workspace) {
87 free(workspace->table[workspace->cols]);
88 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
91 static void shrink_table_rows(Workspace *workspace) {
93 for (int cols = 0; cols < workspace->cols; cols++)
94 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
99 * Performs simple bounds checking for the given column/row
102 bool cell_exists(int col, int row) {
103 return (col >= 0 && col < c_ws->cols) &&
104 (row >= 0 && row < c_ws->rows);
107 static void move_columns_from(Workspace *workspace, int cols) {
108 for (; cols < workspace->cols; cols++)
109 for (int rows = 0; rows < workspace->rows; rows++) {
110 free(workspace->table[cols-1][rows]);
112 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
113 workspace->table[cols-1][rows] = workspace->table[cols][rows];
114 workspace->table[cols-1][rows]->col--;
115 workspace->table[cols][rows] = NULL;
119 static void move_rows_from(Workspace *workspace, int rows) {
120 for (; rows < workspace->rows; rows++)
121 for (int cols = 0; cols < workspace->cols; cols++) {
122 free(workspace->table[cols][rows-1]);
124 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
125 workspace->table[cols][rows-1] = workspace->table[cols][rows];
126 workspace->table[cols][rows-1]->row--;
127 workspace->table[cols][rows] = NULL;
132 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
135 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
136 printf("cleanup_table()\n");
138 /* Check for empty columns if we got more than one column */
139 for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
140 bool completely_empty = true;
141 for (int rows = 0; rows < workspace->rows; rows++)
142 if (workspace->table[cols][rows]->currently_focused != NULL) {
143 completely_empty = false;
146 if (completely_empty) {
147 printf("Removing completely empty column %d\n", cols);
148 if (cols < (workspace->cols - 1))
149 move_columns_from(workspace, cols+1);
150 shrink_table_cols(workspace);
152 if (workspace->current_col >= workspace->cols)
153 workspace->current_col = workspace->cols - 1;
157 /* Check for empty rows if we got more than one row*/
158 for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
159 bool completely_empty = true;
160 for (int cols = 0; cols < workspace->cols; cols++)
161 if (workspace->table[cols][rows]->currently_focused != NULL) {
162 completely_empty = false;
165 if (completely_empty) {
166 printf("Removing completely empty row %d\n", rows);
167 if (rows < (workspace->rows - 1))
168 move_rows_from(workspace, rows+1);
169 shrink_table_rows(workspace);
171 if (workspace->current_row >= workspace->rows)
172 workspace->current_row = workspace->rows - 1;
176 /* Boundary checking for current_col and current_row */
177 if (current_col >= c_ws->cols)
178 current_col = c_ws->cols-1;
180 if (current_row >= c_ws->rows)
181 current_row = c_ws->rows-1;
183 if (CUR_CELL->currently_focused != NULL)
184 set_focus(conn, CUR_CELL->currently_focused);