]> git.sur5r.net Git - i3/i3/blob - src/table.c
Document binding on Mode_switch in userguide, be more verbose in config
[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 #include "layout.h"
28
29 int current_workspace = 0;
30 Workspace workspaces[10];
31 /* Convenience pointer to the current workspace */
32 Workspace *c_ws = &workspaces[0];
33 int current_col = 0;
34 int current_row = 0;
35
36 /*
37  * Initialize table
38  *
39  */
40 void init_table() {
41         memset(workspaces, 0, sizeof(workspaces));
42
43         for (int i = 0; i < 10; i++) {
44                 workspaces[i].screen = NULL;
45                 workspaces[i].num = i;
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         workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
70         workspace->height_factor[workspace->rows-1] = 0;
71
72         for (int c = 0; c < workspace->cols; c++) {
73                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
74                 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
75         }
76 }
77
78 /*
79  * Adds one row at the head of the table
80  *
81  */
82 void expand_table_rows_at_head(Workspace *workspace) {
83         workspace->rows++;
84
85         workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
86
87         LOG("rows = %d\n", workspace->rows);
88         for (int rows = (workspace->rows - 1); rows >= 1; rows--) {
89                 LOG("Moving height_factor %d (%f) to %d\n", rows-1, workspace->height_factor[rows-1], rows);
90                 workspace->height_factor[rows] = workspace->height_factor[rows-1];
91         }
92
93         workspace->height_factor[0] = 0;
94
95         for (int cols = 0; cols < workspace->cols; cols++)
96                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
97
98         /* Move the other rows */
99         for (int cols = 0; cols < workspace->cols; cols++)
100                 for (int rows = workspace->rows - 1; rows > 0; rows--) {
101                         LOG("Moving row %d to %d\n", rows-1, rows);
102                         workspace->table[cols][rows] = workspace->table[cols][rows-1];
103                         workspace->table[cols][rows]->row = rows;
104                 }
105
106         for (int cols = 0; cols < workspace->cols; cols++)
107                 new_container(workspace, &(workspace->table[cols][0]), cols, 0);
108 }
109
110 /*
111  * Add one column to the table
112  *
113  */
114 void expand_table_cols(Workspace *workspace) {
115         workspace->cols++;
116
117         workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
118         workspace->width_factor[workspace->cols-1] = 0;
119
120         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
121         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
122         for (int c = 0; c < workspace->rows; c++)
123                 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
124 }
125
126 /*
127  * Inserts one column at the table’s head
128  *
129  */
130 void expand_table_cols_at_head(Workspace *workspace) {
131         workspace->cols++;
132
133         workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
134
135         LOG("cols = %d\n", workspace->cols);
136         for (int cols = (workspace->cols - 1); cols >= 1; cols--) {
137                 LOG("Moving width_factor %d (%f) to %d\n", cols-1, workspace->width_factor[cols-1], cols);
138                 workspace->width_factor[cols] = workspace->width_factor[cols-1];
139         }
140
141         workspace->width_factor[0] = 0;
142
143         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
144         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
145
146         /* Move the other columns */
147         for (int rows = 0; rows < workspace->rows; rows++)
148                 for (int cols = workspace->cols - 1; cols > 0; cols--) {
149                         LOG("Moving col %d to %d\n", cols-1, cols);
150                         workspace->table[cols][rows] = workspace->table[cols-1][rows];
151                         workspace->table[cols][rows]->col = cols;
152                 }
153
154         for (int rows = 0; rows < workspace->rows; rows++)
155                 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
156 }
157
158 /*
159  * Shrinks the table by one column.
160  *
161  * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
162  * function may only be called from move_*() or after making sure that the containers are freed
163  * properly.
164  *
165  */
166 static void shrink_table_cols(Workspace *workspace) {
167         float free_space = workspace->width_factor[workspace->cols-1];
168
169         workspace->cols--;
170
171         /* Shrink the width_factor array */
172         workspace->width_factor = realloc(workspace->width_factor, sizeof(float) * workspace->cols);
173
174         /* Free the container-pointers */
175         free(workspace->table[workspace->cols]);
176
177         /* Re-allocate the table */
178         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
179
180         /* Distribute the free space */
181         if (free_space == 0)
182                 return;
183
184         for (int cols = (workspace->cols-1); cols >= 0; cols--) {
185                 if (workspace->width_factor[cols] == 0)
186                         continue;
187
188                 LOG("Added free space (%f) to %d (had %f)\n", free_space, cols,
189                                 workspace->width_factor[cols]);
190                 workspace->width_factor[cols] += free_space;
191                 break;
192         }
193 }
194
195 /*
196  * See shrink_table_cols()
197  *
198  */
199 static void shrink_table_rows(Workspace *workspace) {
200         workspace->rows--;
201         for (int cols = 0; cols < workspace->cols; cols++)
202                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
203 }
204
205
206 /*
207  * Performs simple bounds checking for the given column/row
208  *
209  */
210 bool cell_exists(int col, int row) {
211         return (col >= 0 && col < c_ws->cols) &&
212                (row >= 0 && row < c_ws->rows);
213 }
214
215 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
216         Container *old_container = workspace->table[col][row];
217
218         if (old_container->mode == MODE_STACK)
219                 leave_stack_mode(conn, old_container);
220
221         free(old_container);
222 }
223
224 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
225         LOG("firstly freeing \n");
226
227         /* Free the columns which are cleaned up */
228         for (int rows = 0; rows < workspace->rows; rows++)
229                 free_container(conn, workspace, cols-1, rows);
230
231         for (; cols < workspace->cols; cols++)
232                 for (int rows = 0; rows < workspace->rows; rows++) {
233                         LOG("at col = %d, row = %d\n", cols, rows);
234                         Container *new_container = workspace->table[cols][rows];
235
236                         LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
237                         workspace->table[cols-1][rows] = new_container;
238
239                         new_container->row = rows;
240                         new_container->col = cols-1;
241                 }
242 }
243
244 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
245         for (int cols = 0; cols < workspace->cols; cols++)
246                 free_container(conn, workspace, cols, rows-1);
247
248         for (; rows < workspace->rows; rows++)
249                 for (int cols = 0; cols < workspace->cols; cols++) {
250                         Container *new_container = workspace->table[cols][rows];
251
252                         LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
253                         workspace->table[cols][rows-1] = new_container;
254
255                         new_container->row = rows-1;
256                         new_container->col = cols;
257                 }
258 }
259
260 /*
261  * Prints the table’s contents in human-readable form for debugging
262  *
263  */
264 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
265         LOG("dump_table()\n");
266         FOR_TABLE(workspace) {
267                 Container *con = workspace->table[cols][rows];
268                 LOG("----\n");
269                 LOG("at col=%d, row=%d\n", cols, rows);
270                 LOG("currently_focused = %p\n", con->currently_focused);
271                 Client *loop;
272                 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
273                         LOG("got client %08x / %s\n", loop->child, loop->name);
274                 }
275                 LOG("----\n");
276         }
277         LOG("done\n");
278 }
279
280 /*
281  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
282  *
283  */
284 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
285         LOG("cleanup_table()\n");
286
287         /* Check for empty columns if we got more than one column */
288         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
289                 bool completely_empty = true;
290                 for (int rows = 0; rows < workspace->rows; rows++)
291                         if (workspace->table[cols][rows]->currently_focused != NULL) {
292                                 completely_empty = false;
293                                 break;
294                         }
295                 if (completely_empty) {
296                         LOG("Removing completely empty column %d\n", cols);
297                         if (cols < (workspace->cols - 1))
298                                 move_columns_from(conn, workspace, cols+1);
299                         else {
300                                 for (int rows = 0; rows < workspace->rows; rows++)
301                                         free_container(conn, workspace, cols, rows);
302                         }
303                         shrink_table_cols(workspace);
304
305                         if (workspace->current_col >= workspace->cols)
306                                 workspace->current_col = workspace->cols - 1;
307                 } else cols++;
308         }
309
310         /* Check for empty rows if we got more than one row */
311         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
312                 bool completely_empty = true;
313                 LOG("Checking row %d\n", rows);
314                 for (int cols = 0; cols < workspace->cols; cols++)
315                         if (workspace->table[cols][rows]->currently_focused != NULL) {
316                                 completely_empty = false;
317                                 break;
318                         }
319                 if (completely_empty) {
320                         LOG("Removing completely empty row %d\n", rows);
321                         if (rows < (workspace->rows - 1))
322                                 move_rows_from(conn, workspace, rows+1);
323                         else {
324                                 for (int cols = 0; cols < workspace->cols; cols++)
325                                         free_container(conn, workspace, cols, rows);
326                         }
327                         shrink_table_rows(workspace);
328
329                         if (workspace->current_row >= workspace->rows)
330                                 workspace->current_row = workspace->rows - 1;
331                 } else rows++;
332         }
333
334         /* Boundary checking for current_col and current_row */
335         if (current_col >= c_ws->cols)
336                 current_col = c_ws->cols-1;
337
338         if (current_row >= c_ws->rows)
339                 current_row = c_ws->rows-1;
340
341         if (CUR_CELL->currently_focused != NULL)
342                 set_focus(conn, CUR_CELL->currently_focused, true);
343 }
344
345 /*
346  * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
347  *
348  */
349 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
350         LOG("Fixing col/rowspan\n");
351
352         FOR_TABLE(workspace) {
353                 Container *con = workspace->table[cols][rows];
354                 if (con->colspan > 1) {
355                         LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
356                         while (con->colspan > 1 &&
357                                (!cell_exists(cols + (con->colspan-1), rows) ||
358                                 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
359                                 con->colspan--;
360                         LOG("fixed it to %d\n", con->colspan);
361                 }
362                 if (con->rowspan > 1) {
363                         LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
364                         while (con->rowspan > 1 &&
365                                (!cell_exists(cols, rows + (con->rowspan - 1)) ||
366                                 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
367                                 con->rowspan--;
368                         LOG("fixed it to %d\n", con->rowspan);
369                 }
370         }
371 }