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