-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
xwm.c
276 lines (252 loc) · 8.12 KB
/
xwm.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* See LICENSE file for license details. */
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include "xwm.h"
#include "config.h"
#define UNUSED(x) (void)(x)
static xcb_connection_t * dpy;
static xcb_screen_t * scre;
static xcb_drawable_t win;
static uint32_t values[3];
static void killclient(char **com) {
UNUSED(com);
xcb_kill_client(dpy, win);
}
static void closewm(char **com) {
UNUSED(com);
if (dpy != NULL) {
xcb_disconnect(dpy);
}
}
static void spawn(char **com) {
if (fork() == 0) {
setsid();
if (fork() != 0) {
_exit(0);
}
execvp((char*)com[0], (char**)com);
_exit(0);
}
wait(NULL);
}
static void fullclient(char **com) {
UNUSED(com);
uint32_t vals[4];
vals[0] = 0 - BORDER_WIDTH;
vals[1] = 0 - BORDER_WIDTH;
vals[2] = scre->width_in_pixels;
vals[3] = scre->height_in_pixels;
xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X |
XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT, vals);
xcb_flush(dpy);
}
static void handleButtonPress(xcb_generic_event_t *ev) {
xcb_button_press_event_t *e = (xcb_button_press_event_t *) ev;
win = e->child;
values[0] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
values[2] = ((1 == e->detail) ? 1 : ((win != 0) ? 3 : 0 ));
xcb_grab_pointer(dpy, 0, scre->root, XCB_EVENT_MASK_BUTTON_RELEASE
| XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_POINTER_MOTION_HINT,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
scre->root, XCB_NONE, XCB_CURRENT_TIME);
}
static void handleMotionNotify(xcb_generic_event_t *ev) {
UNUSED(ev);
xcb_query_pointer_cookie_t coord = xcb_query_pointer(dpy, scre->root);
xcb_query_pointer_reply_t *poin = xcb_query_pointer_reply(dpy, coord, 0);
if ((values[2] == (uint32_t)(1)) && (win != 0)) {
xcb_get_geometry_cookie_t geom_now = xcb_get_geometry(dpy, win);
xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, geom_now, NULL);
uint16_t geom_x = geom->width + (2 * BORDER_WIDTH);
uint16_t geom_y = geom->height + (2 * BORDER_WIDTH);
values[0] = ((poin->root_x + geom_x) > scre->width_in_pixels) ?
(scre->width_in_pixels - geom_x) : poin->root_x;
values[1] = ((poin->root_y + geom_y) > scre->height_in_pixels) ?
(scre->height_in_pixels - geom_y) : poin->root_y;
xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y, values);
} else if ((values[2] == (uint32_t)(3)) && (win != 0)) {
xcb_get_geometry_cookie_t geom_now = xcb_get_geometry(dpy, win);
xcb_get_geometry_reply_t* geom = xcb_get_geometry_reply(dpy, geom_now, NULL);
if (!((poin->root_x <= geom->x) || (poin->root_y <= geom->y))) {
values[0] = poin->root_x - geom->x - BORDER_WIDTH;
values[1] = poin->root_y - geom->y - BORDER_WIDTH;
if ((values[0] >= (uint32_t)(WINDOW_MIN_WIDTH)) &&
(values[1] >= (uint32_t)(WINDOW_MIN_HEIGHT))) {
xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT, values);
}
}
} else {}
}
static xcb_keycode_t *xcb_get_keycodes(xcb_keysym_t keysym) {
xcb_key_symbols_t *keysyms = xcb_key_symbols_alloc(dpy);
xcb_keycode_t *keycode;
keycode = (!(keysyms) ? NULL : xcb_key_symbols_get_keycode(keysyms, keysym));
xcb_key_symbols_free(keysyms);
return keycode;
}
static xcb_keysym_t xcb_get_keysym(xcb_keycode_t keycode) {
xcb_key_symbols_t *keysyms = xcb_key_symbols_alloc(dpy);
xcb_keysym_t keysym;
keysym = (!(keysyms) ? 0 : xcb_key_symbols_get_keysym(keysyms, keycode, 0));
xcb_key_symbols_free(keysyms);
return keysym;
}
static void setFocus(xcb_drawable_t window) {
if ((window != 0) && (window != scre->root)) {
xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, window,
XCB_CURRENT_TIME);
}
}
static void setFocusColor(xcb_window_t window, int focus) {
if ((BORDER_WIDTH > 0) && (scre->root != window) && (0 != window)) {
uint32_t vals[1];
vals[0] = focus ? BORDER_COLOR_FOCUSED : BORDER_COLOR_UNFOCUSED;
xcb_change_window_attributes(dpy, window, XCB_CW_BORDER_PIXEL, vals);
xcb_flush(dpy);
}
}
static void handleKeyPress(xcb_generic_event_t *ev) {
xcb_key_press_event_t *e = ( xcb_key_press_event_t *) ev;
xcb_keysym_t keysym = xcb_get_keysym(e->detail);
win = e->child;
int key_table_size = sizeof(keys) / sizeof(*keys);
for (int i = 0; i < key_table_size; ++i) {
if ((keys[i].keysym == keysym) && (keys[i].mod == e->state)) {
keys[i].func(keys[i].com);
}
}
}
static void handleEnterNotify(xcb_generic_event_t *ev) {
xcb_enter_notify_event_t *e = ( xcb_enter_notify_event_t *) ev;
setFocus(e->event);
}
static void handleButtonRelease(xcb_generic_event_t *ev) {
UNUSED(ev);
xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
}
static void handleDestroyNotify(xcb_generic_event_t *ev) {
xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) ev;
xcb_kill_client(dpy, e->window);
}
static void handleFocusIn(xcb_generic_event_t *ev) {
xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) ev;
setFocusColor(e->event, 1);
}
static void handleFocusOut(xcb_generic_event_t *ev) {
xcb_focus_out_event_t *e = (xcb_focus_out_event_t *) ev;
setFocusColor(e->event, 0);
}
static void handleMapRequest(xcb_generic_event_t *ev) {
xcb_map_request_event_t *e = (xcb_map_request_event_t *) ev;
xcb_map_window(dpy, e->window);
uint32_t vals[5];
vals[0] = (scre->width_in_pixels / 2) - (WINDOW_WIDTH / 2);
vals[1] = (scre->height_in_pixels / 2) - (WINDOW_HEIGHT / 2);
vals[2] = WINDOW_WIDTH;
vals[3] = WINDOW_HEIGHT;
vals[4] = BORDER_WIDTH;
xcb_configure_window(dpy, e->window, XCB_CONFIG_WINDOW_X |
XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH, vals);
xcb_flush(dpy);
values[0] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
xcb_change_window_attributes_checked(dpy, e->window,
XCB_CW_EVENT_MASK, values);
setFocus(e->window);
}
static int eventHandler(void) {
int ret = xcb_connection_has_error(dpy);
if (ret == 0) {
xcb_generic_event_t *ev = xcb_wait_for_event(dpy);
if (ev != NULL) {
handler_func_t *handler;
for (handler = handler_funs; handler->func != NULL; handler++) {
if ((ev->response_type & ~0x80) == handler->request) {
handler->func(ev);
}
}
free(ev);
}
}
xcb_flush(dpy);
return ret;
}
static void setup(void) {
values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes_checked(dpy, scre->root,
XCB_CW_EVENT_MASK, values);
xcb_ungrab_key(dpy, XCB_GRAB_ANY, scre->root, XCB_MOD_MASK_ANY);
int key_table_size = sizeof(keys) / sizeof(*keys);
for (int i = 0; i < key_table_size; ++i) {
xcb_keycode_t *keycode = xcb_get_keycodes(keys[i].keysym);
if (keycode != NULL) {
xcb_grab_key(dpy, 1, scre->root, keys[i].mod, *keycode,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC );
}
}
xcb_flush(dpy);
xcb_grab_button(dpy, 0, scre->root, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, scre->root, XCB_NONE, 1, MOD1);
xcb_grab_button(dpy, 0, scre->root, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, scre->root, XCB_NONE, 3, MOD1);
xcb_flush(dpy);
}
static int die(char *errstr) {
size_t n = 0;
char *p = errstr;
while ((* (p++)) != 0) {
++n;
}
ssize_t o = write(STDERR_FILENO, errstr, n);
int ret = 1;
if (o < 0) {
ret = -1;
}
return ret;
}
static int strcmp_c(char *str1, char *str2) {
char *c1 = str1;
char *c2 = str2;
while ((* c1) && ((* c1) == (* c2))) {
++c1;
++c2;
}
int n = (* c1) - (* c2);
return n;
}
int main(int argc, char *argv[]) {
int ret = 0;
if ((argc == 2) && (strcmp_c("-v", argv[1]) == 0)) {
ret = die("xwm-0.1.9, Copyright © 2021-2022 Michael Czigler, MIT License\n");
}
if ((ret == 0) && (argc != 1)) {
ret = die("usage: xwm [-v]\n");
}
if (ret == 0) {
dpy = xcb_connect(NULL, NULL);
ret = xcb_connection_has_error(dpy);
if (ret > 0) {
ret = die("xcb_connection_has_error\n");
}
}
if (ret == 0) {
scre = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
setup();
}
while (ret == 0) {
ret = eventHandler();
}
return ret;
}