]> git.sur5r.net Git - i3/i3/blob - src/table.c
Bugfix: Correctly initialize new containers
[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
27 int current_workspace = 0;
28 Workspace workspaces[10];
29 /* Convenience pointer to the current workspace */
30 Workspace *c_ws = &workspaces[0];
31 int current_col = 0;
32 int current_row = 0;
33
34 /*
35  * Initialize table
36  *
37  */
38 void init_table() {
39         memset(workspaces, 0, sizeof(workspaces));
40
41         for (int i = 0; i < 10; i++) {
42                 workspaces[i].screen = NULL;
43                 workspaces[i].num = i;
44                 SLIST_INIT(&(workspaces[i].dock_clients));
45                 expand_table_cols(&(workspaces[i]));
46                 expand_table_rows(&(workspaces[i]));
47         }
48 }
49
50 static void new_container(Workspace *workspace, Container **container, int col, int row) {
51         Container *new;
52         new = *container = calloc(sizeof(Container), 1);
53         CIRCLEQ_INIT(&(new->clients));
54         new->colspan = 1;
55         new->rowspan = 1;
56         new->col = col;
57         new->row = row;
58         new->workspace = workspace;
59 }
60
61 /*
62  * Add one row to the table
63  *
64  */
65 void expand_table_rows(Workspace *workspace) {
66         workspace->rows++;
67
68         for (int c = 0; c < workspace->cols; c++) {
69                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
70                 new_container(workspace, &(workspace->table[c][workspace->rows-1]), c, workspace->rows-1);
71         }
72 }
73
74 /*
75  * Add one column to the table
76  *
77  */
78 void expand_table_cols(Workspace *workspace) {
79         workspace->cols++;
80
81         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
82         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
83         for (int c = 0; c < workspace->rows; c++)
84                 new_container(workspace, &(workspace->table[workspace->cols-1][c]), workspace->cols-1, c);
85 }
86
87 static void shrink_table_cols(Workspace *workspace) {
88         workspace->cols--;
89         free(workspace->table[workspace->cols]);
90         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
91 }
92
93 static void shrink_table_rows(Workspace *workspace) {
94         workspace->rows--;
95         for (int cols = 0; cols < workspace->cols; cols++)
96                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
97 }
98
99
100 /*
101  * Performs simple bounds checking for the given column/row
102  *
103  */
104 bool cell_exists(int col, int row) {
105         return (col >= 0 && col < c_ws->cols) &&
106                (row >= 0 && row < c_ws->rows);
107 }
108
109 static void move_columns_from(Workspace *workspace, int cols) {
110         for (; cols < workspace->cols; cols++)
111                 for (int rows = 0; rows < workspace->rows; rows++) {
112                         Container *old_container = workspace->table[cols-1][rows],
113                                   *new_container = workspace->table[cols][rows];
114
115                         /* Fix the container backpointer for all clients */
116                         Client *client;
117                         CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
118                                 client->container = new_container;
119
120                         free(old_container);
121
122                         printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
123                         workspace->table[cols-1][rows] = new_container;
124
125                         new_container->row = rows;
126                         new_container->col = cols-1;
127
128                         workspace->table[cols][rows] = NULL;
129                 }
130 }
131
132 static void move_rows_from(Workspace *workspace, int rows) {
133         for (; rows < workspace->rows; rows++)
134                 for (int cols = 0; cols < workspace->cols; cols++) {
135                         Container *old_container = workspace->table[cols][rows-1],
136                                   *new_container = workspace->table[cols][rows];
137
138                         /* Fix the container backpointer for all clients */
139                         Client *client;
140                         CIRCLEQ_FOREACH(client, &(old_container->clients), clients)
141                                 client->container = new_container;
142
143                         free(old_container);
144
145                         printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
146                         workspace->table[cols][rows-1] = new_container;
147                         new_container->row = rows-1;
148                         new_container->col = cols;
149                         workspace->table[cols][rows] = NULL;
150                 }
151 }
152
153 /*
154  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
155  *
156  */
157 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
158         printf("cleanup_table()\n");
159
160         /* Check for empty columns if we got more than one column */
161         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
162                 bool completely_empty = true;
163                 for (int rows = 0; rows < workspace->rows; rows++)
164                         if (workspace->table[cols][rows]->currently_focused != NULL) {
165                                 completely_empty = false;
166                                 break;
167                         }
168                 if (completely_empty) {
169                         printf("Removing completely empty column %d\n", cols);
170                         if (cols < (workspace->cols - 1))
171                                 move_columns_from(workspace, cols+1);
172                         shrink_table_cols(workspace);
173
174                         if (workspace->current_col >= workspace->cols)
175                                 workspace->current_col = workspace->cols - 1;
176                 } else cols++;
177         }
178
179         /* Check for empty rows if we got more than one row*/
180         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
181                 bool completely_empty = true;
182                 for (int cols = 0; cols < workspace->cols; cols++)
183                         if (workspace->table[cols][rows]->currently_focused != NULL) {
184                                 completely_empty = false;
185                                 break;
186                         }
187                 if (completely_empty) {
188                         printf("Removing completely empty row %d\n", rows);
189                         if (rows < (workspace->rows - 1))
190                                 move_rows_from(workspace, rows+1);
191                         shrink_table_rows(workspace);
192
193                         if (workspace->current_row >= workspace->rows)
194                                 workspace->current_row = workspace->rows - 1;
195                 } else rows++;
196         }
197
198         /* Boundary checking for current_col and current_row */
199         if (current_col >= c_ws->cols)
200                 current_col = c_ws->cols-1;
201
202         if (current_row >= c_ws->rows)
203                 current_row = c_ws->rows-1;
204
205         if (CUR_CELL->currently_focused != NULL)
206                 set_focus(conn, CUR_CELL->currently_focused);
207 }