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