-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.c
208 lines (172 loc) · 4.62 KB
/
workspace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "workspace.h"
#include "window.h"
workspace stack[NUM_WS] = { { { { NULL, NULL, 0 } }, NULL } };
int curws = 0;
void print_stack(int ws, int type) {
printf("workspace %x [fwin: %x]: ", ws, stack[ws].fwin ? stack[ws].fwin->windows[WIN_CHILD] : 0);
for (window *list = stack[ws].lists[type].first; list; list = list->next[ws][type]) {
printf("win %x -> ", list->windows[WIN_CHILD]);
}
printf("NULL\n");
}
void print_all_stacks(int ws) {
for (int i = 0; i < TYPE_COUNT; i++) {
print_stack(ws, i);
}
}
void traverse(int ws, int type, void (*func)(window *)) {
for (window *list = stack[ws].lists[type].first; list; list = list->next[ws][type]) {
func(list);
}
}
void safe_traverse(int ws, int type, void (*func)(window *)) {
for (window *list = stack[ws].lists[type].first; list;) {
window *temp = list->next[ws][type];
func(list);
list = temp;
}
}
static void insert_into_helper(int ws, int type, window *win) {
win->next[ws][type] = stack[ws].lists[type].first;
win->prev[ws][type] = NULL;
if (stack[ws].lists[type].first) {
stack[ws].lists[type].first->prev[ws][type] = win;
} else {
stack[ws].lists[type].last = win;
}
stack[ws].lists[type].first = win;
stack[ws].lists[type].count++;
}
void insert_into(int ws, window *win) {
insert_into_helper(ws, TYPE_ALL, win);
if (win->normal) {
stack_above(win);
insert_into_helper(ws, TYPE_NORMAL, win);
} else {
stack_above_abnormal(win);
}
if (win->above) {
insert_into_helper(ws, TYPE_ABOVE, win);
} else if (!win->is_i_full && !win->is_e_full) {
safe_traverse(ws, TYPE_ABOVE, mywm_raise);
}
}
static void append_to_helper(int ws, int type, window *win) {
win->prev[ws][type] = stack[ws].lists[type].last;
win->next[ws][type] = NULL;
if (stack[ws].lists[type].last) {
stack[ws].lists[type].last->next[ws][type] = win;
} else {
stack[ws].lists[type].first = win;
}
stack[ws].lists[type].last = win;
stack[ws].lists[type].count++;
}
void append_to(int ws, window *win) {
append_to_helper(ws, TYPE_ALL, win);
if (win->normal) {
stack_below(win);
append_to_helper(ws, TYPE_NORMAL, win);
} else {
stack_below_abnormal(win);
}
if (win->above) {
fprintf(stderr, "mywm: can't append always-above windows.\n");
return;
} else {
//handle always-below :^)
}
}
static void excise_from_helper(int ws, int type, window *subj) {
if (subj->next[ws][type]) {
subj->next[ws][type]->prev[ws][type] = subj->prev[ws][type];
} else {
stack[ws].lists[type].last = subj->prev[ws][type];
}
if (subj->prev[ws][type]) {
subj->prev[ws][type]->next[ws][type] = subj->next[ws][type];
} else {
stack[ws].lists[type].first = subj->next[ws][type];
}
stack[ws].lists[type].count--;
}
void excise_from(int ws, window *win) {
if (win->normal) {
excise_from_helper(ws, TYPE_NORMAL, win);
}
if (win->above) {
excise_from_helper(ws, TYPE_ABOVE, win);
}
excise_from_helper(ws, TYPE_ALL, win);
}
static void all_but_helper(int ws, window *win, void (*func)(int, window *)) {
int i;
for (i = 0; i < ws; i++) {
func(i, win);
}
for (i++; i < NUM_WS; i++) {
func(i, win);
}
}
void insert_into_all_but(int ws, window *win) {
all_but_helper(ws, win, insert_into);
}
void excise_from_all_but(int ws, window *win) {
all_but_helper(ws, win, excise_from);
}
search_data search_helper(int ws, int type, xcb_window_t id, int (*func)(window *, xcb_window_t)) {
search_data ret;
for (ret.win = stack[ws].lists[type].first; ret.win; ret.win = ret.win->next[ws][type]) {
ret.index = func(ret.win, id);
if (-1 < ret.index) {
return ret;
}
}
return ret;
}
search_data search_range(int ws, int type, int start_index, int end_index, xcb_window_t id) {
search_data ret;
for (ret.win = stack[ws].lists[type].first; ret.win; ret.win = ret.win->next[ws][type]) {
for (int i = start_index; i < end_index; i++) {
if (ret.win->windows[i] == id) {
ret.index = i;
return ret;
}
}
}
return ret;
}
window *search_ws(int ws, int type, int window_index, xcb_window_t id) {
window *cur;
for (cur = stack[ws].lists[type].first; cur; cur = cur->next[ws][type]) {
if (cur->windows[window_index] == id) {
break;
}
}
return cur;
}
window *search_all(int *ws, int type, int window_index, xcb_window_t id) {
window *ret;
for (int i = 0; i < NUM_WS; i++) {
ret = search_ws(i, type, window_index, id);
if (ret) {
if (ws) {
*ws = i;
}
break;
}
}
return ret;
}
void refocus(int ws) {
window *win = stack[ws].lists[TYPE_NORMAL].first;
if (!win) {
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
scr->root, XCB_CURRENT_TIME);
return;
}
/*if (win->is_i_full || win->is_e_full) {
mywm_raise(win);
}*/
focus(win);
}