]> git.sur5r.net Git - i3/i3/blob - src/table.c
Put documentation for each function in the header files, doxygen-compatible
[i3/i3] / src / table.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * table.c: Functions/macros for easy modifying/accessing of _the_ table (defining our
11  *          layout).
12  *
13  */
14 #include <stdio.h>
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <stdbool.h>
21 #include <assert.h>
22
23 #include "data.h"
24 #include "table.h"
25 #include "util.h"
26 #include "i3.h"
27
28 int current_workspace = 0;
29 Workspace workspaces[10];
30 /* Convenience pointer to the current workspace */
31 Workspace *c_ws = &workspaces[0];
32 int current_col = 0;
33 int current_row = 0;
34
35 /*
36  * Initialize table
37  *
38  */
39 void init_table() {
40         memset(workspaces, 0, sizeof(workspaces));
41
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]));
48         }
49 }
50
51 static void new_container(Workspace *workspace, Container **container, int col, int row) {
52         Container *new;
53         new = *container = calloc(sizeof(Container), 1);
54         CIRCLEQ_INIT(&(new->clients));
55         new->colspan = 1;
56         new->rowspan = 1;
57         new->col = col;
58         new->row = row;
59         new->workspace = workspace;
60 }
61
62 /*
63  * Add one row to the table
64  *
65  */
66 void expand_table_rows(Workspace *workspace) {
67         workspace->rows++;
68
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);
72         }
73 }
74
75 /*
76  * Adds one row at the head of the table
77  *
78  */
79 void expand_table_rows_at_head(Workspace *workspace) {
80         workspace->rows++;
81
82         for (int cols = 0; cols < workspace->cols; cols++)
83                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
84
85         /* Move the other rows */
86         for (int cols = 0; cols < workspace->cols; cols++)
87                 for (int rows = workspace->rows - 1; rows > 0; rows--) {
88                         LOG("Moving row %d to %d\n", rows-1, rows);
89                         workspace->table[cols][rows] = workspace->table[cols][rows-1];
90                         workspace->table[cols][rows]->row = rows;
91                 }
92         for (int cols = 0; cols < workspace->cols; cols++)
93                 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
94 }
95
96 /*
97  * Add one column to the table
98  *
99  */
100 void expand_table_cols(Workspace *workspace) {
101         workspace->cols++;
102
103         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
104         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
105         for (int c = 0; c < workspace->rows; c++)
106                 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
107 }
108
109 /*
110  * Inserts one column at the table’s head
111  *
112  */
113 void expand_table_cols_at_head(Workspace *workspace) {
114         workspace->cols++;
115
116         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
117         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
118
119         /* Move the other columns */
120         for (int rows = 0; rows < workspace->rows; rows++)
121                 for (int cols = workspace->cols - 1; cols > 0; cols--) {
122                         LOG("Moving col %d to %d\n", cols-1, cols);
123                         workspace->table[cols][rows] = workspace->table[cols-1][rows];
124                         workspace->table[cols][rows]->col = cols;
125                 }
126
127         for (int rows = 0; rows < workspace->rows; rows++)
128                 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
129 }
130
131 /*
132  * Shrinks the table by one column.
133  *
134  * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
135  * function may only be called from move_*() or after making sure that the containers are freed
136  * properly.
137  *
138  */
139 static void shrink_table_cols(Workspace *workspace) {
140         workspace->cols--;
141
142         /* Free the container-pointers */
143         free(workspace->table[workspace->cols]);
144
145         /* Re-allocate the table */
146         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
147 }
148
149 /*
150  * See shrink_table_cols()
151  *
152  */
153 static void shrink_table_rows(Workspace *workspace) {
154         workspace->rows--;
155         for (int cols = 0; cols < workspace->cols; cols++)
156                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
157 }
158
159
160 /*
161  * Performs simple bounds checking for the given column/row
162  *
163  */
164 bool cell_exists(int col, int row) {
165         return (col >= 0 && col < c_ws->cols) &&
166                (row >= 0 && row < c_ws->rows);
167 }
168
169 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
170         Container *old_container = workspace->table[col][row];
171
172         if (old_container->mode == MODE_STACK)
173                 leave_stack_mode(conn, old_container);
174
175         free(old_container);
176 }
177
178 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
179         LOG("firstly freeing \n");
180
181         /* Free the columns which are cleaned up */
182         for (int rows = 0; rows < workspace->rows; rows++)
183                 free_container(conn, workspace, cols-1, rows);
184
185         for (; cols < workspace->cols; cols++)
186                 for (int rows = 0; rows < workspace->rows; rows++) {
187                         LOG("at col = %d, row = %d\n", cols, rows);
188                         Container *new_container = workspace->table[cols][rows];
189
190                         LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
191                         workspace->table[cols-1][rows] = new_container;
192
193                         new_container->row = rows;
194                         new_container->col = cols-1;
195                 }
196 }
197
198 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
199         for (int cols = 0; cols < workspace->cols; cols++)
200                 free_container(conn, workspace, cols, rows-1);
201
202         for (; rows < workspace->rows; rows++)
203                 for (int cols = 0; cols < workspace->cols; cols++) {
204                         Container *new_container = workspace->table[cols][rows];
205
206                         LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
207                         workspace->table[cols][rows-1] = new_container;
208
209                         new_container->row = rows-1;
210                         new_container->col = cols;
211                 }
212 }
213
214 /*
215  * Prints the table’s contents in human-readable form for debugging
216  *
217  */
218 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
219         LOG("dump_table()\n");
220         FOR_TABLE(workspace) {
221                 Container *con = workspace->table[cols][rows];
222                 LOG("----\n");
223                 LOG("at col=%d, row=%d\n", cols, rows);
224                 LOG("currently_focused = %p\n", con->currently_focused);
225                 Client *loop;
226                 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
227                         LOG("got client %08x / %s\n", loop->child, loop->name);
228                 }
229                 LOG("----\n");
230         }
231         LOG("done\n");
232 }
233
234 /*
235  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
236  *
237  */
238 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
239         LOG("cleanup_table()\n");
240
241         /* Check for empty columns if we got more than one column */
242         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
243                 bool completely_empty = true;
244                 for (int rows = 0; rows < workspace->rows; rows++)
245                         if (workspace->table[cols][rows]->currently_focused != NULL) {
246                                 completely_empty = false;
247                                 break;
248                         }
249                 if (completely_empty) {
250                         LOG("Removing completely empty column %d\n", cols);
251                         if (cols < (workspace->cols - 1))
252                                 move_columns_from(conn, workspace, cols+1);
253                         else {
254                                 for (int rows = 0; rows < workspace->rows; rows++)
255                                         free_container(conn, workspace, cols, rows);
256                         }
257                         shrink_table_cols(workspace);
258
259                         if (workspace->current_col >= workspace->cols)
260                                 workspace->current_col = workspace->cols - 1;
261                 } else cols++;
262         }
263
264         /* Check for empty rows if we got more than one row */
265         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
266                 bool completely_empty = true;
267                 LOG("Checking row %d\n", rows);
268                 for (int cols = 0; cols < workspace->cols; cols++)
269                         if (workspace->table[cols][rows]->currently_focused != NULL) {
270                                 completely_empty = false;
271                                 break;
272                         }
273                 if (completely_empty) {
274                         LOG("Removing completely empty row %d\n", rows);
275                         if (rows < (workspace->rows - 1))
276                                 move_rows_from(conn, workspace, rows+1);
277                         else {
278                                 for (int cols = 0; cols < workspace->cols; cols++)
279                                         free_container(conn, workspace, cols, rows);
280                         }
281                         shrink_table_rows(workspace);
282
283                         if (workspace->current_row >= workspace->rows)
284                                 workspace->current_row = workspace->rows - 1;
285                 } else rows++;
286         }
287
288         /* Boundary checking for current_col and current_row */
289         if (current_col >= c_ws->cols)
290                 current_col = c_ws->cols-1;
291
292         if (current_row >= c_ws->rows)
293                 current_row = c_ws->rows-1;
294
295         if (CUR_CELL->currently_focused != NULL)
296                 set_focus(conn, CUR_CELL->currently_focused, true);
297 }
298
299 /*
300  * Fixes col/rowspan (makes sure there are no overlapping windows)
301  *
302  */
303 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
304         LOG("Fixing col/rowspan\n");
305
306         FOR_TABLE(workspace) {
307                 Container *con = workspace->table[cols][rows];
308                 if (con->colspan > 1) {
309                         LOG("gots one with colspan %d\n", con->colspan);
310                         while (con->colspan > 1 &&
311                                workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
312                                 con->colspan--;
313                         LOG("fixed it to %d\n", con->colspan);
314                 }
315                 if (con->rowspan > 1) {
316                         LOG("gots one with rowspan %d\n", con->rowspan);
317                         while (con->rowspan > 1 &&
318                                workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
319                                 con->rowspan--;
320                         LOG("fixed it to %d\n", con->rowspan);
321                 }
322         }
323 }