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