-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.c
405 lines (368 loc) · 9.45 KB
/
event.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* ISC License
*
* Copyright (c) 2021, Tommi Leino <namhas@gmail.com>
*
* 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.
*/
#include "mxswm.h"
#include <err.h>
#include <X11/Xatom.h>
static Window window;
static Time timestamp;
static struct client *client;
Time
current_event_timestamp()
{
return timestamp;
}
#ifdef TRACE
static XEvent *_current_event;
time_t
start_time()
{
static time_t t;
if (t == 0)
t = time(NULL);
return t;
}
const char *
current_event()
{
return str_event(_current_event);
}
struct client *
event_client()
{
return client;
}
void
set_event_client(struct client *_client)
{
client = _client;
}
Window
current_window()
{
return window;
}
#endif
#ifdef TRACE
const char *
str_event(XEvent *event)
{
static const struct event_str {
int type;
char *name;
} es[] = {
{ KeyPress, "KeyPress" },
{ KeyRelease, "KeyRelease" },
{ ButtonPress, "ButtonPress" },
{ ButtonRelease, "ButtonRelease" },
{ MotionNotify, "MotionNotify" },
{ EnterNotify, "EnterNotify" },
{ LeaveNotify, "LeaveNotify" },
{ FocusIn, "FocusIn" },
{ FocusOut, "FocusOut" },
{ KeymapNotify, "KeymapNotify" },
{ Expose, "Expose" },
{ GraphicsExpose, "GraphicsExpose" },
{ NoExpose, "NoExpose" },
{ VisibilityNotify, "VisibilityNotify" },
{ CreateNotify, "CreateNotify" },
{ DestroyNotify, "DestroyNotify" },
{ UnmapNotify, "UnmapNotify" },
{ MapNotify, "MapNotify" },
{ MapRequest, "MapRequest" },
{ ReparentNotify, "ReparentNotify" },
{ ConfigureNotify, "ConfigureNotify" },
{ ConfigureRequest, "ConfigureRequest" },
{ GravityNotify, "GravityNotify" },
{ ResizeRequest, "ResizeRequest" },
{ CirculateNotify, "CirculateNotify" },
{ CirculateRequest, "CirculateRequest" },
{ PropertyNotify, "PropertyNotify" },
{ SelectionClear, "SelectionClear" },
{ SelectionRequest, "SelectionRequest" },
{ SelectionNotify, "SelectionNotify" },
{ ColormapNotify, "ColormapNotify" },
{ ClientMessage, "ClientMessage" },
{ MappingNotify, "MappingNotify" },
{ -1 }
};
int i;
char *s;
if (event == NULL)
return "";
for (i = 0; es[i].type != -1; i++)
if (es[i].type == event->type)
break;
if (es[i].type != -1)
s = es[i].name;
else
s = "<unknown event>";
return s;
}
#endif
static int disappear_error;
static int
handle_disappear_error(Display *display, XErrorEvent *event)
{
TRACE_LOG("*");
disappear_error = 1;
return 1;
}
static void
pass_configure(XConfigureRequestEvent *event)
{
XWindowChanges wc;
Display *dpy;
TRACE_LOG("*");
dpy = display();
wc.x = event->x;
wc.y = event->y;
wc.width = event->width;
wc.height = event->height;
wc.border_width = event->border_width;
wc.stack_mode = Above;
event->value_mask &= ~CWStackMode;
XConfigureWindow(dpy, event->window, event->value_mask, &wc);
}
static int
is_input_only(Window window)
{
XWindowAttributes wa;
Display *d = display();
int input_only;
input_only = 0;
XSync(display(), False);
XSetErrorHandler(handle_disappear_error);
if (XGetWindowAttributes(d, window, &wa)) {
input_only = (wa.class == InputOnly);
TRACE_LOG("input_only: %d", input_only);
return input_only;
}
if (disappear_error) {
disappear_error = 0;
return 1;
}
XSync(display(), False);
XSetErrorHandler(None);
return 0;
}
int
handle_event(XEvent *event)
{
struct stack *stack;
#ifdef TRACE
_current_event = event;
#endif
timestamp = CurrentTime;
window = 0;
switch (event->type) {
case Expose:
window = event->xexpose.window;
stack = have_stack(window);
if (stack != NULL)
draw_stack(stack);
else if (is_statusbar(window))
draw_statusbar();
break;
case ButtonPress:
window = event->xbutton.window;
timestamp = event->xbutton.time;
stack = find_stack_xy(event->xbutton.x, event->xbutton.y);
/*
* We are in XGrabButton mode, do the correct thing for
* click-to-focus based on the current focus state and
* click location.
*/
if (stack != NULL && current_stack() != stack)
focus_stack(stack);
else
TRACE_LOG("ignore");
XAllowEvents(display(), ReplayPointer, CurrentTime);
break;
case KeyRelease:
case KeyPress:
timestamp = event->xkey.time;
do_keyaction(&(event->xkey));
break;
case PropertyNotify:
window = event->xproperty.window;
client = have_client(window);
if (client != NULL) {
TRACE_LOG("update atom=%lu", event->xproperty.atom);
switch (event->xproperty.atom) {
case XA_WM_NAME:
update_client_name(client);
draw_stack(client->stack);
draw_menu();
break;
default:
if (event->xproperty.atom ==
wmh[WM_PROTOCOLS]) {
read_protocols(client);
draw_stack(client->stack);
draw_menu();
} else if (event->xproperty.atom ==
wmh[_NET_WM_NAME]) {
update_client_name(client);
draw_stack(client->stack);
draw_menu();
} else {
TRACE_LOG("unsupported atom %s",
XGetAtomName(display(),
event->xproperty.atom));
}
break;
}
} else if (window == DefaultRootWindow(display())) {
switch (event->xproperty.atom) {
case XA_WM_NAME:
draw_statusbar();
break;
default:
if (event->xproperty.atom ==
wmh[_NET_WM_NAME])
draw_statusbar();
break;
}
} else
TRACE_LOG("ignore");
break;
case MapNotify:
case UnmapNotify:
window = event->xmap.window;
client = have_client(window);
if (client != NULL) {
TRACE_LOG("mapped was %d", client->mapped);
client->mapped = (event->type == MapNotify) ? 1 : 0;
TRACE_LOG("mapped is now %d", client->mapped);
if (client->mapped &&
client->flags & CF_FOCUS_WHEN_MAPPED) {
client->flags &= ~CF_FOCUS_WHEN_MAPPED;
focus_client(client, client->stack);
} else if (client->mapped &&
client->stack == current_stack()) {
focus_client(client, client->stack);
} else if (client->mapped && client->stack != NULL) {
draw_stack(client->stack);
} else if (client->mapped && client->stack == NULL) {
client->stack = current_stack();
draw_stack(client->stack);
} else if (client->mapped == 0) {
stack = client->stack;
CLIENT_STACK(client) = NULL;
draw_stack(stack);
draw_menu();
}
} else if ((stack = have_stack(window)) != NULL)
stack->mapped = (event->type == MapNotify) ? 1 : 0;
else if (is_statusbar(window))
set_statusbar_mapped_status(
(event->type == MapNotify) ? 1 : 0);
else
TRACE_LOG("ignore");
break;
case DestroyNotify:
if (event->type == UnmapNotify)
window = event->xmap.window;
else
window = event->xdestroywindow.window;
client = have_client(window);
if (client != NULL) {
TRACE_LOG("remove");
stack = client->stack;
remove_client(client);
draw_stack(stack);
draw_menu();
focus_stack(current_stack());
client = NULL;
} else
TRACE_LOG("ignore");
break;
case MapRequest:
window = event->xmaprequest.window;
client = have_client(window);
if (client != NULL) {
TRACE_LOG("mapping");
XMapWindow(display(), window);
XSelectInput(display(), window, PropertyChangeMask);
read_protocols(client);
update_client_name(client);
} else
TRACE_LOG("ignore");
break;
case ConfigureRequest:
window = event->xconfigurerequest.window;
client = have_client(window);
if (client != NULL) {
TRACE_LOG("got %dx%d+%d+%d",
event->xconfigurerequest.width,
event->xconfigurerequest.height,
event->xconfigurerequest.x,
event->xconfigurerequest.y);
/*
* We don't care about the window configuration
* before the window is mapped except for mild
* optimization reasons, but we cannot optimize
* because some clients such as 'xterm' behave
* erratically unless the requested configuration
* is passed as is.
*
* In other words, we intercept only whenever
* clients try to change window configuration of
* mapped windows in which case we simply do
* nothing because windows will always be maximized.
*/
if (!client->mapped)
pass_configure(&event->xconfigurerequest);
} else
TRACE_LOG("ignore");
break;
case ConfigureNotify:
window = event->xconfigure.window;
TRACE_LOG("%dx%d+%d+%d", event->xconfigure.width,
event->xconfigure.height, event->xconfigure.x,
event->xconfigure.y);
break;
case CreateNotify:
window = event->xcreatewindow.window;
TRACE_LOG("%dx%d+%d+%d bw=%d override=%d",
event->xcreatewindow.width, event->xcreatewindow.height,
event->xcreatewindow.x, event->xcreatewindow.y,
event->xcreatewindow.border_width,
event->xcreatewindow.override_redirect);
if (event->xcreatewindow.override_redirect == True ||
is_input_only(window))
TRACE_LOG("ignore");
else {
client = add_client(window, NULL, 0, current_stack(),
0);
if (client == NULL)
warn("add_client");
TRACE_LOG("created");
}
break;
default:
TRACE_LOG("unhandled");
break;
}
#ifdef TRACE
_current_event = 0;
window = 0;
client = NULL;
#endif
return 1;
}