-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.c
296 lines (254 loc) · 6.41 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
#include <limits.h>
#include <strings.h>
#include <cairo-xlib.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XInput2.h>
#include "debug.h"
#include "event.h"
#include "root.h"
#include "window.h"
#include "vtk.h"
static vtk_modifiers _vtk_modifiers(unsigned int state) {
vtk_modifiers vm = 0;
if (state & ShiftMask) vm |= VTK_M_SHIFT;
if (state & LockMask) vm |= VTK_M_CAPS_LOCK;
if (state & ControlMask) vm |= VTK_M_CONTROL;
if (state & Mod1Mask) vm |= VTK_M_ALT;
if (state & Mod4Mask) vm |= VTK_M_SUPER;
if (state & Button1Mask) vm |= VTK_M_LEFT_BTN;
if (state & Button2Mask) vm |= VTK_M_MIDDLE_BTN;
if (state & Button3Mask) vm |= VTK_M_RIGHT_BTN;
return vm;
}
static void _vtk_event_key(vtk_window win, XKeyEvent ev) {
int shiftlvl = (ev.state & ShiftMask) == (ev.state & LockMask) ? 0 : 1;
KeySym xk = XkbKeycodeToKeysym(win->root->dpy, ev.keycode, 0, shiftlvl);
vtk_key vk;
switch (xk) {
case XK_BackSpace:
vk = VTK_K_BACKSPACE;
break;
case XK_Tab:
vk = VTK_K_TAB;
break;
case XK_Return:
vk = VTK_K_RETURN;
break;
case XK_Escape:
vk = VTK_K_ESCAPE;
break;
case XK_Delete:
vk = VTK_K_DELETE;
break;
case XK_Insert:
vk = VTK_K_INSERT;
break;
case XK_Page_Up:
vk = VTK_K_PAGE_UP;
break;
case XK_Page_Down:
vk = VTK_K_PAGE_DOWN;
break;
case XK_Home:
vk = VTK_K_HOME;
break;
case XK_End:
vk = VTK_K_END;
break;
case XK_Up:
vk = VTK_K_UP;
break;
case XK_Down:
vk = VTK_K_DOWN;
break;
case XK_Left:
vk = VTK_K_LEFT;
break;
case XK_Right:
vk = VTK_K_RIGHT;
break;
default:
if (' ' <= xk && xk <= '~') {
vk = xk;
} else {
DEBUG("Unknown KeySym received: %lu", xk);
}
break;
}
vtk_event ve;
ve.key = (struct vtk_key_event){
.type = ev.type == KeyPress ? VTK_EV_KEY_PRESS : VTK_EV_KEY_RELEASE,
.key = vk,
.mods = _vtk_modifiers(ev.state)
};
switch (ev.type) {
case KeyPress:
if (win->event.key_press.h) {
win->event.key_press.h(ve, win->event.key_press.d);
}
break;
case KeyRelease:
if (win->event.key_release.h) {
win->event.key_release.h(ve, win->event.key_release.d);
}
break;
}
}
static void _vtk_event_motion(vtk_window win, XMotionEvent ev) {
if (!win->event.mouse_move.h) return;
vtk_event ve;
ve.mouse_move = (struct vtk_mouse_move_event){
.type = VTK_EV_MOUSE_MOVE,
.mods = _vtk_modifiers(ev.state),
.x = ev.x,
.y = ev.y,
};
win->event.mouse_move.h(ve, win->event.mouse_move.d);
}
static void _vtk_event_send_scroll(vtk_window win, double amount) {
DEBUG("scroll");
if (!win->event.scroll.h) return;
vtk_event ve;
ve.scroll = (struct vtk_scroll_event){
.type = VTK_EV_SCROLL,
.amount = amount,
};
win->event.scroll.h(ve, win->event.scroll.d);
}
static void _vtk_event_button(vtk_window win, XButtonEvent ev) {
vtk_modifiers vb;
switch (ev.button) {
case Button1:
vb = VTK_M_LEFT_BTN;
break;
case Button2:
vb = VTK_M_MIDDLE_BTN;
break;
case Button3:
vb = VTK_M_RIGHT_BTN;
break;
case Button4:
if (!win->root->xi2.enable) {
return _vtk_event_send_scroll(win, 1);
}
case Button5:
if (!win->root->xi2.enable) {
return _vtk_event_send_scroll(win, -1);
}
}
vtk_event ve;
ve.mouse_button = (struct vtk_mouse_button_event){
.type = ev.type == ButtonPress ? VTK_EV_MOUSE_PRESS : VTK_EV_MOUSE_RELEASE,
.btn = vb,
.mods = _vtk_modifiers(ev.state),
.x = ev.x,
.y = ev.y,
};
switch (ev.type) {
case ButtonPress:
if (win->event.mouse_press.h) {
win->event.mouse_press.h(ve, win->event.mouse_press.d);
}
break;
case ButtonRelease:
if (win->event.mouse_release.h) {
win->event.mouse_release.h(ve, win->event.mouse_release.d);
}
break;
}
}
static void _vtk_event_configure(vtk_window win, XConfigureEvent ev) {
int w, h;
vtk_window_get_size(win, &w, &h);
if (ev.width != w || ev.height != h) {
cairo_xlib_surface_set_size(win->csurf, ev.width, ev.height);
if (win->event.resize.h) {
win->event.resize.h((vtk_event){ VTK_EV_RESIZE }, win->event.resize.d);
}
vtk_window_redraw(win);
}
}
static void _vtk_event_scroll(vtk_window win, XGenericEventCookie cookie) {
union {
XIDeviceChangedEvent *devchange;
XIDeviceEvent *dev;
XIEnterEvent *enter;
} ev = { cookie.data };
switch (cookie.evtype) {
case XI_DeviceChanged:
DEBUG("DeviceChanged");
vtk_root_update_xi2_scroll(win->root, ev.devchange->classes, ev.devchange->num_classes);
break;
case XI_Enter:
if (ev.enter->evtype == XI_Enter && ev.enter->mode == XINotifyNormal) {
DEBUG("Enter");
win->root->xi2.scroll_v.reset = true;
}
break;
case XI_Motion:
for (int byte_i = 0, val_i = 0; byte_i < ev.dev->valuators.mask_len; byte_i++) {
int cur_byte = ev.dev->valuators.mask[byte_i], bit_i;
int valuator = CHAR_BIT * byte_i - 1; // - 1 accounts for ffs starting at 1
// Loop through each set bit
while ((bit_i = ffs(cur_byte))) {
valuator += bit_i;
cur_byte >>= bit_i;
if (valuator > win->root->xi2.scroll_v.valuator) goto finish;
if (valuator == win->root->xi2.scroll_v.valuator) {
double value = ev.dev->valuators.values[val_i];
if (win->root->xi2.scroll_v.reset) {
DEBUG("Resetting stored scroll value");
win->root->xi2.scroll_v.reset = false;
} else {
double delta = win->root->xi2.scroll_v.value - value;
_vtk_event_send_scroll(win, delta / win->root->xi2.scroll_v.increment);
}
win->root->xi2.scroll_v.value = value;
}
val_i++;
}
}
finish:
break;
}
}
void vtk_event_handle(vtk_window win, XEvent ev) {
switch (ev.type) {
case ClientMessage:
if (ev.xclient.data.l[0] == win->root->wm_delete_window && win->event.close.h) {
win->event.close.h((vtk_event){ VTK_EV_CLOSE }, win->event.close.d);
}
break;
case Expose:
vtk_window_redraw(win);
break;
case MappingNotify:
// Apparently you need to do this for some reason
switch (ev.xmapping.request) {
case MappingModifier:
case MappingKeyboard:
XRefreshKeyboardMapping(&ev.xmapping);
break;
}
case KeyPress:
case KeyRelease:
_vtk_event_key(win, ev.xkey);
break;
case MotionNotify:
_vtk_event_motion(win, ev.xmotion);
break;
case ButtonPress:
case ButtonRelease:
_vtk_event_button(win, ev.xbutton);
break;
case ConfigureNotify:
_vtk_event_configure(win, ev.xconfigure);
break;
case GenericEvent:
if (ev.xgeneric.extension == win->root->xi2.opcode && XGetEventData(win->root->dpy, &ev.xcookie)) {
_vtk_event_scroll(win, ev.xcookie);
XFreeEventData(win->root->dpy, &ev.xcookie);
}
}
}