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