]> git.sur5r.net Git - i3/i3/blob - src/table.c
Implement moving clients to the left if they are leftmost
[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
28 int current_workspace = 0;
29 Workspace workspaces[10];
30 /* Convenience pointer to the current workspace */
31 Workspace *c_ws = &workspaces[0];
32 int current_col = 0;
33 int current_row = 0;
34
35 /*
36  * Initialize table
37  *
38  */
39 void init_table() {
40         memset(workspaces, 0, sizeof(workspaces));
41
42         for (int i = 0; i < 10; i++) {
43                 workspaces[i].screen = NULL;
44                 workspaces[i].num = i;
45                 SLIST_INIT(&(workspaces[i].dock_clients));
46                 expand_table_cols(&(workspaces[i]));
47                 expand_table_rows(&(workspaces[i]));
48         }
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 }
61
62 /*
63  * Add one row to the table
64  *
65  */
66 void expand_table_rows(Workspace *workspace) {
67         workspace->rows++;
68
69         for (int c = 0; c < workspace->cols; c++) {
70                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
71                 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
72         }
73 }
74
75 /*
76  * Add one column to the table
77  *
78  */
79 void expand_table_cols(Workspace *workspace) {
80         workspace->cols++;
81
82         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
83         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
84         for (int c = 0; c < workspace->rows; c++)
85                 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
86 }
87
88 /*
89  * Inserts one column at the table’s head
90  *
91  */
92 void expand_table_cols_at_head(Workspace *workspace) {
93         workspace->cols++;
94
95         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
96         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
97
98         /* Move the other columns */
99         for (int rows = 0; rows < workspace->rows; rows++)
100                 for (int cols = workspace->cols - 1; cols > 0; cols--) {
101                         LOG("Moving col %d to %d\n", cols-1, cols);
102                         workspace->table[cols][rows] = workspace->table[cols-1][rows];
103                         workspace->table[cols][rows]->col = cols;
104                 }
105
106         for (int rows = 0; rows < workspace->rows; rows++)
107                 new_container(workspace, &(workspace->table[0][rows]), 0, rows);
108 }
109
110 /*
111  * Shrinks the table by one column.
112  *
113  * The containers themselves are freed in move_columns_from() or move_rows_from(). Therefore, this
114  * function may only be called from move_*() or after making sure that the containers are freed
115  * properly.
116  *
117  */
118 static void shrink_table_cols(Workspace *workspace) {
119         workspace->cols--;
120
121         /* Free the container-pointers */
122         free(workspace->table[workspace->cols]);
123
124         /* Re-allocate the table */
125         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
126 }
127
128 /*
129  * See shrink_table_cols()
130  *
131  */
132 static void shrink_table_rows(Workspace *workspace) {
133         workspace->rows--;
134         for (int cols = 0; cols < workspace->cols; cols++)
135                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
136 }
137
138
139 /*
140  * Performs simple bounds checking for the given column/row
141  *
142  */
143 bool cell_exists(int col, int row) {
144         return (col >= 0 && col < c_ws->cols) &&
145                (row >= 0 && row < c_ws->rows);
146 }
147
148 static void free_container(xcb_connection_t *conn, Workspace *workspace, int col, int row) {
149         Container *old_container = workspace->table[col][row];
150
151         if (old_container->mode == MODE_STACK)
152                 leave_stack_mode(conn, old_container);
153
154         free(old_container);
155 }
156
157 static void move_columns_from(xcb_connection_t *conn, Workspace *workspace, int cols) {
158         LOG("firstly freeing \n");
159
160         /* Free the columns which are cleaned up */
161         for (int rows = 0; rows < workspace->rows; rows++)
162                 free_container(conn, workspace, cols-1, rows);
163
164         for (; cols < workspace->cols; cols++)
165                 for (int rows = 0; rows < workspace->rows; rows++) {
166                         LOG("at col = %d, row = %d\n", cols, rows);
167                         Container *new_container = workspace->table[cols][rows];
168
169                         LOG("moving cols = %d to cols -1 = %d\n", cols, cols-1);
170                         workspace->table[cols-1][rows] = new_container;
171
172                         new_container->row = rows;
173                         new_container->col = cols-1;
174                 }
175 }
176
177 static void move_rows_from(xcb_connection_t *conn, Workspace *workspace, int rows) {
178         for (int cols = 0; cols < workspace->cols; cols++)
179                 free_container(conn, workspace, cols, rows-1);
180
181         for (; rows < workspace->rows; rows++)
182                 for (int cols = 0; cols < workspace->cols; cols++) {
183                         Container *new_container = workspace->table[cols][rows];
184
185                         LOG("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
186                         workspace->table[cols][rows-1] = new_container;
187
188                         new_container->row = rows-1;
189                         new_container->col = cols;
190                 }
191 }
192
193 void dump_table(xcb_connection_t *conn, Workspace *workspace) {
194         LOG("dump_table()\n");
195         FOR_TABLE(workspace) {
196                 Container *con = workspace->table[cols][rows];
197                 LOG("----\n");
198                 LOG("at col=%d, row=%d\n", cols, rows);
199                 LOG("currently_focused = %p\n", con->currently_focused);
200                 Client *loop;
201                 CIRCLEQ_FOREACH(loop, &(con->clients), clients) {
202                         LOG("got client %08x / %s\n", loop->child, loop->name);
203                 }
204                 LOG("----\n");
205         }
206         LOG("done\n");
207 }
208
209 /*
210  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
211  *
212  */
213 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
214         LOG("cleanup_table()\n");
215
216         /* Check for empty columns if we got more than one column */
217         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
218                 bool completely_empty = true;
219                 for (int rows = 0; rows < workspace->rows; rows++)
220                         if (workspace->table[cols][rows]->currently_focused != NULL) {
221                                 completely_empty = false;
222                                 break;
223                         }
224                 if (completely_empty) {
225                         LOG("Removing completely empty column %d\n", cols);
226                         if (cols < (workspace->cols - 1))
227                                 move_columns_from(conn, workspace, cols+1);
228                         else {
229                                 for (int rows = 0; rows < workspace->rows; rows++)
230                                         free_container(conn, workspace, cols, rows);
231                         }
232                         shrink_table_cols(workspace);
233
234                         if (workspace->current_col >= workspace->cols)
235                                 workspace->current_col = workspace->cols - 1;
236                 } else cols++;
237         }
238
239         /* Check for empty rows if we got more than one row */
240         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
241                 bool completely_empty = true;
242                 LOG("Checking row %d\n", rows);
243                 for (int cols = 0; cols < workspace->cols; cols++)
244                         if (workspace->table[cols][rows]->currently_focused != NULL) {
245                                 completely_empty = false;
246                                 break;
247                         }
248                 if (completely_empty) {
249                         LOG("Removing completely empty row %d\n", rows);
250                         if (rows < (workspace->rows - 1))
251                                 move_rows_from(conn, workspace, rows+1);
252                         else {
253                                 for (int cols = 0; cols < workspace->cols; cols++)
254                                         free_container(conn, workspace, cols, rows);
255                         }
256                         shrink_table_rows(workspace);
257
258                         if (workspace->current_row >= workspace->rows)
259                                 workspace->current_row = workspace->rows - 1;
260                 } else rows++;
261         }
262
263         /* Boundary checking for current_col and current_row */
264         if (current_col >= c_ws->cols)
265                 current_col = c_ws->cols-1;
266
267         if (current_row >= c_ws->rows)
268                 current_row = c_ws->rows-1;
269
270         if (CUR_CELL->currently_focused != NULL)
271                 set_focus(conn, CUR_CELL->currently_focused);
272 }
273
274 /*
275  * Fixes col/rowspan (makes sure there are no overlapping windows)
276  *
277  */
278 void fix_colrowspan(xcb_connection_t *conn, Workspace *workspace) {
279         LOG("Fixing col/rowspan\n");
280
281         FOR_TABLE(workspace) {
282                 Container *con = workspace->table[cols][rows];
283                 if (con->colspan > 1) {
284                         LOG("gots one with colspan %d\n", con->colspan);
285                         while (con->colspan > 1 &&
286                                workspace->table[cols + (con->colspan - 1)][rows]->currently_focused != NULL)
287                                 con->colspan--;
288                         LOG("fixed it to %d\n", con->colspan);
289                 }
290                 if (con->rowspan > 1) {
291                         LOG("gots one with rowspan %d\n", con->rowspan);
292                         while (con->rowspan > 1 &&
293                                workspace->table[cols][rows + (con->rowspan - 1)]->currently_focused != NULL)
294                                 con->rowspan--;
295                         LOG("fixed it to %d\n", con->rowspan);
296                 }
297         }
298 }