-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathswm.c
341 lines (280 loc) · 8.23 KB
/
swm.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Copyright (c) 2014, Broseph <dcat (at) iotek (dot) org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#define CLEANMASK(m) ((m & ~0x80))
/* sane modifier names */
#define SUPER XCB_MOD_MASK_4
#define ALT XCB_MOD_MASK_1
#define CTRL XCB_MOD_MASK_CONTROL
#define SHIFT XCB_MOD_MASK_SHIFT
#include <xcb/xcb.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
enum { INACTIVE, ACTIVE };
/* global variables */
static xcb_connection_t *conn;
static xcb_screen_t *scr;
static xcb_window_t focuswin;
/* proto */
static void subscribe(xcb_window_t);
static void cleanup(void);
static int deploy(void);
static void focus(xcb_window_t, int);
#include "config.h"
static void
cleanup(void)
{
/* graceful exit */
if (conn != NULL)
xcb_disconnect(conn);
}
static int
deploy(void)
{
/* init xcb and grab events */
uint32_t values[2];
int mask;
if (xcb_connection_has_error(conn = xcb_connect(NULL, NULL)))
return -1;
scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
focuswin = scr->root;
#ifdef ENABLE_MOUSE
xcb_grab_button(conn, 0, scr->root, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, scr->root, XCB_NONE, 1, MOD);
xcb_grab_button(conn, 0, scr->root, XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, scr->root, XCB_NONE, 3, MOD);
#endif
mask = XCB_CW_EVENT_MASK;
values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
xcb_change_window_attributes_checked(conn, scr->root, mask, values);
xcb_flush(conn);
return 0;
}
static void
focus(xcb_window_t win, int mode)
{
uint32_t values[1];
#ifdef DOUBLE_BORDER
short w, h, b, o;
if (!win)
return;
xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(conn,
xcb_get_geometry(conn, win), NULL);
if (geom == NULL)
return;
w = (short)geom->width;
h = (short)geom->height;
b = (unsigned short)geom->border_width;
o = (unsigned short)OUTER;
xcb_rectangle_t inner[] = {
/* you're not supposed to understand this. */
{ w,0,b-o ,h+b- o },
{ w+b +o, 0, b -o, h+ b - o},
{ 0,h ,w+b -o,b- o },
{ 0,h +b+ o, w+ b- o, b -o},
{ w+b+o,b +h +o,b,b}
};
xcb_rectangle_t outer[] = {
{w + b - o, 0, o, h + b * 2},
{w + b, 0, o, h + b * 2},
{0, h + b - o, w + b * 2, o},
{0, h + b, w + b * 2, o},
{1, 1, 1, 1}
};
xcb_pixmap_t pmap = xcb_generate_id(conn);
xcb_create_pixmap(conn, scr->root_depth, pmap, win, geom->width
+ (geom->border_width * 2),
geom->height + (geom->border_width * 2));
xcb_gcontext_t gc = xcb_generate_id(conn);
xcb_create_gc(conn, gc, pmap, 0, NULL);
values[0] = OUTERCOL;
xcb_change_gc(conn, gc, XCB_GC_FOREGROUND, values);
xcb_poly_fill_rectangle(conn, pmap, gc, 5, outer);
values[0] = mode ? FOCUSCOL : UNFOCUSCOL;
xcb_change_gc(conn, gc, XCB_GC_FOREGROUND, values);
xcb_poly_fill_rectangle(conn, pmap, gc, 5, inner);
values[0] = pmap;
xcb_change_window_attributes(conn, win, XCB_CW_BORDER_PIXMAP, values);
xcb_free_pixmap(conn, pmap);
xcb_free_gc(conn, gc);
#else
values[0] = mode ? FOCUSCOL : UNFOCUSCOL;
xcb_change_window_attributes(conn, win, XCB_CW_BORDER_PIXEL, values);
#endif
if (mode == ACTIVE) {
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
win, XCB_CURRENT_TIME);
if (win != focuswin) {
focus(focuswin, INACTIVE);
focuswin = win;
}
}
}
static void
subscribe(xcb_window_t win)
{
uint32_t values[2];
/* subscribe to events */
values[0] = XCB_EVENT_MASK_ENTER_WINDOW;
values[1] = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
xcb_change_window_attributes(conn, win, XCB_CW_EVENT_MASK, values);
/* border width */
values[0] = BORDERWIDTH;
xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
}
static void
events_loop(void)
{
xcb_generic_event_t *ev;
#ifdef ENABLE_MOUSE
uint32_t values[3];
xcb_get_geometry_reply_t *geom;
xcb_window_t win = 0;
#endif
/* loop */
for (;;) {
ev = xcb_wait_for_event(conn);
if (!ev)
errx(1, "xcb connection broken");
switch (CLEANMASK(ev->response_type)) {
case XCB_CREATE_NOTIFY: {
xcb_create_notify_event_t *e;
e = (xcb_create_notify_event_t *)ev;
if (!e->override_redirect) {
subscribe(e->window);
focus(e->window, ACTIVE);
}
} break;
case XCB_DESTROY_NOTIFY: {
xcb_destroy_notify_event_t *e;
e = (xcb_destroy_notify_event_t *)ev;
xcb_kill_client(conn, e->window);
} break;
#ifdef ENABLE_SLOPPY
case XCB_ENTER_NOTIFY: {
xcb_enter_notify_event_t *e;
e = (xcb_enter_notify_event_t *)ev;
focus(e->event, ACTIVE);
} break;
#endif
case XCB_MAP_NOTIFY: {
xcb_map_notify_event_t *e;
e = (xcb_map_notify_event_t *)ev;
if (!e->override_redirect) {
xcb_map_window(conn, e->window);
focus(e->window, ACTIVE);
}
} break;
#ifdef ENABLE_MOUSE
case XCB_BUTTON_PRESS: {
xcb_button_press_event_t *e;
e = ( xcb_button_press_event_t *)ev;
win = e->child;
if (!win || win == scr->root)
break;
values[0] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(conn, win,
XCB_CONFIG_WINDOW_STACK_MODE, values);
geom = xcb_get_geometry_reply(conn,
xcb_get_geometry(conn, win), NULL);
if (e->detail == 1) {
values[2] = 1;
xcb_warp_pointer(conn, XCB_NONE, win, 0, 0, 0,
0, geom->width/2, geom->height/2);
} else {
values[2] = 3;
xcb_warp_pointer(conn, XCB_NONE, win, 0, 0, 0,
0, geom->width, geom->height);
}
xcb_grab_pointer(conn, 0, scr->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,
scr->root, XCB_NONE, XCB_CURRENT_TIME);
xcb_flush(conn);
} break;
case XCB_MOTION_NOTIFY: {
xcb_query_pointer_reply_t *pointer;
pointer = xcb_query_pointer_reply(conn,
xcb_query_pointer(conn, scr->root), 0);
if (values[2] == 1) {
geom = xcb_get_geometry_reply(conn,
xcb_get_geometry(conn, win), NULL);
if (!geom)
break;
values[0] = (pointer->root_x + geom->width / 2
> scr->width_in_pixels
- (BORDERWIDTH*2))
? scr->width_in_pixels - geom->width
- (BORDERWIDTH*2)
: pointer->root_x - geom->width / 2;
values[1] = (pointer->root_y + geom->height / 2
> scr->height_in_pixels
- (BORDERWIDTH*2))
? (scr->height_in_pixels - geom->height
- (BORDERWIDTH*2))
: pointer->root_y - geom->height / 2;
if (pointer->root_x < geom->width/2)
values[0] = 0;
if (pointer->root_y < geom->height/2)
values[1] = 0;
xcb_configure_window(conn, win,
XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y, values);
xcb_flush(conn);
} else if (values[2] == 3) {
geom = xcb_get_geometry_reply(conn,
xcb_get_geometry(conn, win), NULL);
values[0] = pointer->root_x - geom->x;
values[1] = pointer->root_y - geom->y;
xcb_configure_window(conn, win,
XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT, values);
xcb_flush(conn);
}
} break;
case XCB_BUTTON_RELEASE:
focus(win, ACTIVE);
xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
break;
#endif
case XCB_CONFIGURE_NOTIFY: {
xcb_configure_notify_event_t *e;
e = (xcb_configure_notify_event_t *)ev;
if (e->window != focuswin)
focus(e->window, INACTIVE);
focus(focuswin, ACTIVE);
} break;
}
xcb_flush(conn);
free(ev);
}
}
int
main(void)
{
/* graceful exit */
atexit(cleanup);
if (deploy() < 0)
errx(EXIT_FAILURE, "error connecting to X");
for (;;)
events_loop();
return EXIT_FAILURE;
}
/* vim: set noet sw=8 sts=8: */