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>
26 int current_workspace = 0;
27 Workspace workspaces[10];
28 /* Convenience pointer to the current workspace */
29 Workspace *c_ws = &workspaces[0];
38 memset(workspaces, 0, sizeof(workspaces));
40 for (int i = 0; i < 10; i++) {
41 workspaces[i].screen = NULL;
42 SLIST_INIT(&(workspaces[i].dock_clients));
43 expand_table_cols(&(workspaces[i]));
44 expand_table_rows(&(workspaces[i]));
48 static void new_container(Workspace *workspace, Container **container) {
50 new = *container = calloc(sizeof(Container), 1);
51 CIRCLEQ_INIT(&(new->clients));
54 new->workspace = workspace;
58 * Add one row to the table
61 void expand_table_rows(Workspace *workspace) {
64 for (int c = 0; c < workspace->cols; c++) {
65 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
66 new_container(workspace, &(workspace->table[c][workspace->rows-1]));
71 * Add one column to the table
74 void expand_table_cols(Workspace *workspace) {
77 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
78 workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
79 for (int c = 0; c < workspace->rows; c++)
80 new_container(workspace, &(workspace->table[workspace->cols-1][c]));
83 static void shrink_table_cols(Workspace *workspace) {
85 free(workspace->table[workspace->cols]);
86 workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
89 static void shrink_table_rows(Workspace *workspace) {
91 for (int cols = 0; cols < workspace->cols; cols++)
92 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
97 * Performs simple bounds checking for the given column/row
100 bool cell_exists(int col, int row) {
101 return (col >= 0 && col < c_ws->cols) &&
102 (row >= 0 && row < c_ws->rows);
105 static void move_columns_from(Workspace *workspace, int cols) {
106 for (; cols < workspace->cols; cols++)
107 for (int rows = 0; rows < workspace->rows; rows++) {
108 free(workspace->table[cols-1][rows]);
110 printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
111 workspace->table[cols-1][rows] = workspace->table[cols][rows];
112 workspace->table[cols][rows] = NULL;
116 static void move_rows_from(Workspace *workspace, int rows) {
117 for (; rows < workspace->rows; rows++)
118 for (int cols = 0; cols < workspace->cols; cols++) {
119 free(workspace->table[cols][rows-1]);
121 printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
122 workspace->table[cols][rows-1] = workspace->table[cols][rows];
123 workspace->table[cols][rows] = NULL;
128 * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
131 void cleanup_table(Workspace *workspace) {
132 /* Check for empty columns */
133 for (int cols = 0; cols < workspace->cols;) {
134 bool completely_empty = true;
135 for (int rows = 0; rows < workspace->rows; rows++)
136 if (workspace->table[cols][rows]->currently_focused != NULL) {
137 completely_empty = false;
140 if (completely_empty && cols > 0) {
141 printf("Removing completely empty column %d\n", cols);
142 if (cols < (workspace->cols - 1))
143 move_columns_from(workspace, cols+1);
144 shrink_table_cols(workspace);
148 /* Check for empty rows */
149 for (int rows = 0; rows < workspace->rows;) {
150 bool completely_empty = true;
151 for (int cols = 0; cols < workspace->cols; cols++)
152 if (workspace->table[cols][rows]->currently_focused != NULL) {
153 completely_empty = false;
156 if (completely_empty && rows > 0) {
157 printf("Removing completely empty row %d\n", rows);
158 if (rows < (workspace->rows - 1))
159 move_rows_from(workspace, rows+1);
160 shrink_table_rows(workspace);