]> git.sur5r.net Git - i3/i3/blob - src/table.c
Grab XCB_GRAB_SYNC and replay the event so it doesn’t get lost
[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
26 int current_workspace = 0;
27 Workspace workspaces[10];
28 /* Convenience pointer to the current workspace */
29 Workspace *c_ws = &workspaces[0];
30 int current_col = 0;
31 int current_row = 0;
32
33 /*
34  * Initialize table
35  *
36  */
37 void init_table() {
38         memset(workspaces, 0, sizeof(workspaces));
39
40         for (int i = 0; i < 10; i++) {
41                 workspaces[i].screen = NULL;
42                 SLIST_INIT(&(workspaces[i].dock_clients));
43                 expand_table_cols(&(workspaces[i]));
44                 expand_table_rows(&(workspaces[i]));
45         }
46 }
47
48 static void new_container(Workspace *workspace, Container **container) {
49         Container *new;
50         new = *container = calloc(sizeof(Container), 1);
51         CIRCLEQ_INIT(&(new->clients));
52         new->colspan = 1;
53         new->rowspan = 1;
54         new->workspace = workspace;
55 }
56
57 /*
58  * Add one row to the table
59  *
60  */
61 void expand_table_rows(Workspace *workspace) {
62         workspace->rows++;
63
64         for (int c = 0; c < workspace->cols; c++) {
65                 workspace->table[c] = realloc(workspace->table[c], sizeof(Container*) * workspace->rows);
66                 new_container(workspace, &(workspace->table[c][workspace->rows-1]));
67         }
68 }
69
70 /*
71  * Add one column to the table
72  *
73  */
74 void expand_table_cols(Workspace *workspace) {
75         workspace->cols++;
76
77         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
78         workspace->table[workspace->cols-1] = calloc(sizeof(Container*) * workspace->rows, 1);
79         for (int c = 0; c < workspace->rows; c++)
80                 new_container(workspace, &(workspace->table[workspace->cols-1][c]));
81 }
82
83 static void shrink_table_cols(Workspace *workspace) {
84         workspace->cols--;
85         free(workspace->table[workspace->cols]);
86         workspace->table = realloc(workspace->table, sizeof(Container**) * workspace->cols);
87 }
88
89 static void shrink_table_rows(Workspace *workspace) {
90         workspace->rows--;
91         for (int cols = 0; cols < workspace->cols; cols++)
92                 workspace->table[cols] = realloc(workspace->table[cols], sizeof(Container*) * workspace->rows);
93 }
94
95
96 /*
97  * Performs simple bounds checking for the given column/row
98  *
99  */
100 bool cell_exists(int col, int row) {
101         return (col >= 0 && col < c_ws->cols) &&
102                (row >= 0 && row < c_ws->rows);
103 }
104
105 static void move_columns_from(Workspace *workspace, int cols) {
106         for (; cols < workspace->cols; cols++)
107                 for (int rows = 0; rows < workspace->rows; rows++) {
108                         free(workspace->table[cols-1][rows]);
109
110                         printf("moving cols = %d to cols -1 = %d\n", cols, cols-1);
111                         workspace->table[cols-1][rows] = workspace->table[cols][rows];
112                         workspace->table[cols][rows] = NULL;
113                 }
114 }
115
116 static void move_rows_from(Workspace *workspace, int rows) {
117         for (; rows < workspace->rows; rows++)
118                 for (int cols = 0; cols < workspace->cols; cols++) {
119                         free(workspace->table[cols][rows-1]);
120
121                         printf("moving rows = %d to rows -1 = %d\n", rows, rows - 1);
122                         workspace->table[cols][rows-1] = workspace->table[cols][rows];
123                         workspace->table[cols][rows] = NULL;
124                 }
125 }
126
127 /*
128  * Shrinks the table by "compacting" it, that is, removing completely empty rows/columns
129  *
130  */
131 void cleanup_table(Workspace *workspace) {
132         /* Check for empty columns */
133         for (int cols = 0; cols < workspace->cols;) {
134                 bool completely_empty = true;
135                 for (int rows = 0; rows < workspace->rows; rows++)
136                         if (workspace->table[cols][rows]->currently_focused != NULL) {
137                                 completely_empty = false;
138                                 break;
139                         }
140                 if (completely_empty && cols > 0) {
141                         printf("Removing completely empty column %d\n", cols);
142                         if (cols < (workspace->cols - 1))
143                                 move_columns_from(workspace, cols+1);
144                         shrink_table_cols(workspace);
145                 } else cols++;
146         }
147
148         /* Check for empty rows */
149         for (int rows = 0; rows < workspace->rows;) {
150                 bool completely_empty = true;
151                 for (int cols = 0; cols < workspace->cols; cols++)
152                         if (workspace->table[cols][rows]->currently_focused != NULL) {
153                                 completely_empty = false;
154                                 break;
155                         }
156                 if (completely_empty && rows > 0) {
157                         printf("Removing completely empty row %d\n", rows);
158                         if (rows < (workspace->rows - 1))
159                                 move_rows_from(workspace, rows+1);
160                         shrink_table_rows(workspace);
161                 } else rows++;
162         }
163 }