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