]> git.sur5r.net Git - i3/i3/blob - src/table.c
Implement modes. Modes allow you to use different keybindings and switch between...
[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 int num_workspaces = 1;
31 Workspace *workspaces;
32 /* Convenience pointer to the current workspace */
33 Workspace *c_ws;
34 int current_col = 0;
35 int current_row = 0;
36
37 /*
38  * Initialize table
39  *
40  */
41 void init_table() {
42         workspaces = scalloc(sizeof(Workspace));
43         c_ws = workspaces;
44
45         workspaces[0].screen = NULL;
46         workspaces[0].num = 0;
47         TAILQ_INIT(&(workspaces[0].floating_clients));
48         expand_table_cols(&(workspaces[0]));
49         expand_table_rows(&(workspaces[0]));
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         float free_space = workspace->height_factor[workspace->rows-1];
202
203         workspace->rows--;
204         for (int cols = 0; cols < workspace->cols; cols++)
205                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
206
207         /* Shrink the height_factor array */
208         workspace->height_factor = realloc(workspace->height_factor, sizeof(float) * workspace->rows);
209
210         /* Distribute the free space */
211         if (free_space == 0)
212                 return;
213
214         for (int rows = (workspace->rows-1); rows >= 0; rows--) {
215                 if (workspace->height_factor[rows] == 0)
216                         continue;
217
218                 LOG("Added free space (%f) to %d (had %f)\n", free_space, rows,
219                                 workspace->height_factor[rows]);
220                 workspace->height_factor[rows] += free_space;
221                 break;
222         }
223 }
224
225 /*
226  * Performs simple bounds checking for the given column/row
227  *
228  */
229 bool cell_exists(int col, int row) {
230         return (col >= 0 && col < c_ws->cols) &&
231                (row >= 0 && row < c_ws->rows);
232 }
233
234 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
235         Container *old_container = workspace->table[col][row];
236
237         if (old_container->mode == MODE_STACK)
238                 leave_stack_mode(conn, old_container);
239
240         free(old_container);
241 }
242
243 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
244         LOG("firstly freeing \n");
245
246         /* Free the columns which are cleaned up */
247         for (int rows = 0; rows < workspace->rows; rows++)
248                 free_container(conn, workspace, cols-1, rows);
249
250         for (; cols < workspace->cols; cols++)
251                 for (int rows = 0; rows < workspace->rows; rows++) {
252                         LOG("at col = %d, row = %d\n", cols, rows);
253                         Container *new_container = workspace->table[cols][rows];
254
255                         LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
256                         workspace->table[cols-1][rows] = new_container;
257
258                         new_container->row = rows;
259                         new_container->col = cols-1;
260                 }
261 }
262
263 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
264         for (int cols = 0; cols < workspace->cols; cols++)
265                 free_container(conn, workspace, cols, rows-1);
266
267         for (; rows < workspace->rows; rows++)
268                 for (int cols = 0; cols < workspace->cols; cols++) {
269                         Container *new_container = workspace->table[cols][rows];
270
271                         LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
272                         workspace->table[cols][rows-1] = new_container;
273
274                         new_container->row = rows-1;
275                         new_container->col = cols;
276                 }
277 }
278
279 /*
280  * Prints the table’s contents in human-readable form for debugging
281  *
282  */
283 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
284         LOG("dump_table()\n");
285         FOR_TABLE(workspace) {
286                 Container *con = workspace->table[cols][rows];
287                 LOG("----\n");
288                 LOG("at col=%d, row=%d\n", cols, rows);
289                 LOG("currently_focused = %p\n", con->currently_focused);
290                 Client *loop;
291                 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
292                         LOG("got client %08x / %s\n", loop->child, loop->name);
293                 }
294                 LOG("----\n");
295         }
296         LOG("done\n");
297 }
298
299 /*
300  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
301  *
302  */
303 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
304         LOG("cleanup_table()\n");
305
306         /* Check for empty columns if we got more than one column */
307         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
308                 bool completely_empty = true;
309                 for (int rows = 0; rows < workspace->rows; rows++)
310                         if (workspace->table[cols][rows]->currently_focused != NULL) {
311                                 completely_empty = false;
312                                 break;
313                         }
314                 if (completely_empty) {
315                         LOG("Removing completely empty column %d\n", cols);
316                         if (cols < (workspace->cols - 1))
317                                 move_columns_from(conn, workspace, cols+1);
318                         else {
319                                 for (int rows = 0; rows < workspace->rows; rows++)
320                                         free_container(conn, workspace, cols, rows);
321                         }
322                         shrink_table_cols(workspace);
323
324                         if (workspace->current_col >= workspace->cols)
325                                 workspace->current_col = workspace->cols - 1;
326                 } else cols++;
327         }
328
329         /* Check for empty rows if we got more than one row */
330         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
331                 bool completely_empty = true;
332                 LOG("Checking row %d\n", rows);
333                 for (int cols = 0; cols < workspace->cols; cols++)
334                         if (workspace->table[cols][rows]->currently_focused != NULL) {
335                                 completely_empty = false;
336                                 break;
337                         }
338                 if (completely_empty) {
339                         LOG("Removing completely empty row %d\n", rows);
340                         if (rows < (workspace->rows - 1))
341                                 move_rows_from(conn, workspace, rows+1);
342                         else {
343                                 for (int cols = 0; cols < workspace->cols; cols++)
344                                         free_container(conn, workspace, cols, rows);
345                         }
346                         shrink_table_rows(workspace);
347
348                         if (workspace->current_row >= workspace->rows)
349                                 workspace->current_row = workspace->rows - 1;
350                 } else rows++;
351         }
352
353         /* Boundary checking for current_col and current_row */
354         if (current_col >= c_ws->cols)
355                 current_col = c_ws->cols-1;
356
357         if (current_row >= c_ws->rows)
358                 current_row = c_ws->rows-1;
359
360         if (CUR_CELL->currently_focused != NULL)
361                 set_focus(conn, CUR_CELL->currently_focused, true);
362 }
363
364 /*
365  * Fixes col/rowspan (makes sure there are no overlapping windows, obeys borders).
366  *
367  */
368 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
369         LOG("Fixing col/rowspan\n");
370
371         FOR_TABLE(workspace) {
372                 Container *con = workspace->table[cols][rows];
373                 if (con->colspan > 1) {
374                         LOG("gots one with colspan %d (at %d c, %d r)\n", con->colspan, cols, rows);
375                         while (con->colspan > 1 &&
376                                (!cell_exists(cols + (con->colspan-1), rows) ||
377                                 workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL))
378                                 con->colspan--;
379                         LOG("fixed it to %d\n", con->colspan);
380                 }
381                 if (con->rowspan > 1) {
382                         LOG("gots one with rowspan %d (at %d c, %d r)\n", con->rowspan, cols, rows);
383                         while (con->rowspan > 1 &&
384                                (!cell_exists(cols, rows + (con->rowspan - 1)) ||
385                                 workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL))
386                                 con->rowspan--;
387                         LOG("fixed it to %d\n", con->rowspan);
388                 }
389         }
390 }