]> git.sur5r.net Git - i3/i3/blob - src/table.c
Bugfixes: Various fixes when cleaning up the table/rendering
[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                 SLIST_INIT(&(workspaces[i].dock_clients));
44                 expand_table_cols(&(workspaces[i]));
45                 expand_table_rows(&(workspaces[i]));
46         }
47 }
48
49 static void new_container(Workspace *workspace, Container **container) {
50         Container *new;
51         new = *container = calloc(sizeof(Container), 1);
52         CIRCLEQ_INIT(&(new->clients));
53         new->colspan = 1;
54         new->rowspan = 1;
55         new->workspace = workspace;
56 }
57
58 /*
59  * Add one row to the table
60  *
61  */
62 void expand_table_rows(Workspace *workspace) {
63         workspace->rows++;
64
65         for (int c = 0; c < workspace->cols; c++) {
66                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
67                 new_container(workspace, &(workspace->table[c][workspace->rows-1]));
68         }
69 }
70
71 /*
72  * Add one column to the table
73  *
74  */
75 void expand_table_cols(Workspace *workspace) {
76         workspace->cols++;
77
78         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
79         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
80         for (int c = 0; c < workspace->rows; c++)
81                 new_container(workspace, &(workspace->table[workspace->cols-1][c]));
82 }
83
84 static void shrink_table_cols(Workspace *workspace) {
85         workspace->cols--;
86         free(workspace->table[workspace->cols]);
87         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
88 }
89
90 static void shrink_table_rows(Workspace *workspace) {
91         workspace->rows--;
92         for (int cols = 0; cols < workspace->cols; cols++)
93                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
94 }
95
96
97 /*
98  * Performs simple bounds checking for the given column/row
99  *
100  */
101 bool cell_exists(int col, int row) {
102         return (col >= 0 && col < c_ws->cols) &&
103                (row >= 0 && row < c_ws->rows);
104 }
105
106 static void move_columns_from(Workspace *workspace, int cols) {
107         for (; cols < workspace->cols; cols++)
108                 for (int rows = 0; rows < workspace->rows; rows++) {
109                         free(workspace->table[cols-1][rows]);
110
111                         printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
112                         workspace->table[cols-1][rows] = workspace->table[cols][rows];
113                         workspace->table[cols-1][rows]->col--;
114                         workspace->table[cols][rows] = NULL;
115                 }
116 }
117
118 static void move_rows_from(Workspace *workspace, int rows) {
119         for (; rows < workspace->rows; rows++)
120                 for (int cols = 0; cols < workspace->cols; cols++) {
121                         free(workspace->table[cols][rows-1]);
122
123                         printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
124                         workspace->table[cols][rows-1] = workspace->table[cols][rows];
125                         workspace->table[cols][rows-1]->row--;
126                         workspace->table[cols][rows] = NULL;
127                 }
128 }
129
130 /*
131  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
132  *
133  */
134 void cleanup_table(xcb_connection_t *conn, Workspace *workspace) {
135         printf("cleanup_table()\n");
136
137         /* Check for empty columns if we got more than one column */
138         for (int cols = 0; (workspace->cols > 1) && (cols < workspace->cols);) {
139                 bool completely_empty = true;
140                 for (int rows = 0; rows < workspace->rows; rows++)
141                         if (workspace->table[cols][rows]->currently_focused != NULL) {
142                                 completely_empty = false;
143                                 break;
144                         }
145                 if (completely_empty) {
146                         printf("Removing completely empty column %d\n", cols);
147                         if (cols < (workspace->cols - 1))
148                                 move_columns_from(workspace, cols+1);
149                         shrink_table_cols(workspace);
150
151                         if (workspace->current_col >= workspace->cols)
152                                 workspace->current_col = workspace->cols - 1;
153                 } else cols++;
154         }
155
156         /* Check for empty rows if we got more than one row*/
157         for (int rows = 0; (workspace->rows > 1) && (rows < workspace->rows);) {
158                 bool completely_empty = true;
159                 for (int cols = 0; cols < workspace->cols; cols++)
160                         if (workspace->table[cols][rows]->currently_focused != NULL) {
161                                 completely_empty = false;
162                                 break;
163                         }
164                 if (completely_empty) {
165                         printf("Removing completely empty row %d\n", rows);
166                         if (rows < (workspace->rows - 1))
167                                 move_rows_from(workspace, rows+1);
168                         shrink_table_rows(workspace);
169
170                         if (workspace->current_row >= workspace->rows)
171                                 workspace->current_row = workspace->rows - 1;
172                 } else rows++;
173         }
174
175         /* Boundary checking for current_col and current_row */
176         if (current_col >= c_ws->cols)
177                 current_col = c_ws->cols-1;
178
179         if (current_row >= c_ws->rows)
180                 current_row = c_ws->rows-1;
181
182         if (CUR_CELL->currently_focused != NULL)
183                 set_focus(conn, CUR_CELL->currently_focused);
184 }