forked from agraef/ShuttlePRO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shuttlepro.c
422 lines (375 loc) · 10.5 KB
/
shuttlepro.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Contour ShuttlePro v2 interface
Copyright 2013 Eric Messick (FixedImagePhoto.com/Contact)
Copyright 2018 Albert Graef <aggraef@gmail.com>, various improvements
Copyright 2019 Ben Blain <servc.eu/contact>, major overhaul and personalisations
Based on a version (c) 2006 Trammell Hudson <hudson@osresearch.net>
which was in turn
Based heavily on code by Arendt David <admin@prnet.org>
*/
#include "shuttle.h"
#define ISSET(var, pos) ((var)&(1u<<(pos)))
typedef struct input_event EV[MAX_EV_NUM];
unsigned short jogvalue = 0;
short shuttlevalue = 0;
uint16_t buttonsPressed = 0;
unsigned int kbd_interval = 200;
#define ISPRESSED(btn) ISSET(buttonsPressed, btn)
#define NUM_KEYMAP_COMMANDS 4
typedef struct keymapCommand {
void (*f) (char *, uint);
char *arg;
} keymapCommand;
void _printf (char *a, uint v)
{ printf ("%s, Value: %i", a, v); }
keymapCommand keymapCommands[NUM_KEYMAP_COMMANDS] = {{&_printf, "Command 1"},
{&_printf, "Command 2"}/*,
{&_printf, "Command 3"},
{&_printf, "Command 4"}*/};
typedef struct _keymapvalue {
uint value:29;
uint code:2;
} keymapValue; // __attribute__((packed))
#define NUM_KEYMAPS 3
#define KMV_CODE_KEY = 0
#define KMV_CODE_BUTTON_CURRENT_WINDOW = 1
#define KMV_CODE_BUTTON_POINTER_WINDOW = 2
#define KMV_CODE_COMMAND = 3
const uint16_t keymapsMask[NUM_KEYMAPS - 1] = {1u << 10u, 1u << 9u};
// NOTE: Must keep the very first element zeroed (see getKeymap).
const keymapValue keymap[NUM_KEYMAPS][NUM_KEYS] = {
{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {1, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
{{8, 1}, {1, 1}, {3, 1}, {9, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {37, 0}, {0, 0}, {0, 0}}};
Display *display;
void
initdisplay (void)
{
int event, opcode, error, major, minor;
unsigned int kbd_delay;
display = XOpenDisplay (0);
if (!display)
{
errf ("unable to open X display");
exit (1);
}
if (!XTestQueryExtension (display, &event, &error, &major, &minor))
{
errf ("Xtest extensions not supported.");
XCloseDisplay (display);
exit (1);
}
if (XkbQueryExtension (display, &opcode, &event, &error, &major, &minor))
XkbGetAutoRepeatRate (display, XkbUseCoreKbd, &kbd_delay, &kbd_interval);
else
{
errf ("Xkb extensions not supported. Continuing with default values.");
}
}
const keymapValue *getKeymap (uint16_t code)
{
int i = 0;
for (; i < NUM_KEYMAPS - 1; ++i)
{
// We can see the first element is statically all zero, so compare with it
if (unlikely(
buttonsPressed & keymapsMask[i] && memcmp (&keymap[0][0], &keymap[i][code], sizeof (keymapValue)) != 0))
return &keymap[i][code];
}
return &keymap[i][code];
}
void
send_button (unsigned int button, unsigned int press)
{
prnf ("send_button (%d, %d)", button, press);
XTestFakeButtonEvent (display, button, press, DELAY);
}
void
send_button_to_active (unsigned int button, unsigned int press)
{
// TODO get active window and use XSendEvent
XTestFakeButtonEvent (display, button, press, DELAY);
}
void
send_key (unsigned int keycode, unsigned int press)
{
prnf ("send_key (%d, %d)", keycode, press);
XTestFakeKeyEvent (display, keycode, press, DELAY);
}
void
key (unsigned short code, unsigned int value)
{
code -= EVENT_CODE_KEY1;
if (likely(code <= NUM_KEYS))
{
const keymapValue *km;
prnf ("Key: %d %d", code, value);
if (value)
buttonsPressed |= (1u << code);
else
buttonsPressed ^= (1u << code);
km = getKeymap (code);
switch (km->code)
{
case 0b00:
if (!km->value)
{
errf ("key(%d) unbound", code);
return;
}
send_key (km->value, value);
break;
case 0b01:
send_button (km->value, value);
break;
case 0b10:
send_button_to_active (km->value, value);
break;
case 0b11:
{
keymapCommand kc = keymapCommands[km->value];
kc.f (kc.arg, value);
return; // Commands must flush the server themselves
}
}
// Switch to XSync instead?
XFlush (display);
}
else
{
errf ("key(%d, %d) out of range", code + EVENT_CODE_KEY1, value);
}
}
void
shuttle (int value)
{
prnf ("Shuttle: %d, last: %d", value, shuttlevalue);
// if value = 0 stop timer
// [if last value = 0,] start timer with delay = kbd_interval * (1-|value|/7)
// For now, just use shuttle to scroll quickly
if (value == 0)
return;
if (ISPRESSED (11u))
{
uint8_t btn = (value > 0) ? 116 : 111;
send_key (btn, 1);
send_key (btn, 0);
}
else
{
// TODO This temp variable should be optimised out somehow. I don't like it here
uint8_t btn = (value > 0) ? 5 : 4;
for (int i = 0; i < abs (value); ++i)
{
send_button (btn, 1);
send_button (btn, 0);
}
}
XFlush (display);
}
void
jog (bool direction)
{
prnf ("Jog: %d, %i", direction, jogvalue);
if (ISPRESSED (10u))
{
if (!direction)
send_key (50, 1);
send_key (23, 1);
send_key (23, 0);
if (!direction)
send_key (50, 0);
}
else if (ISPRESSED (11u))
{
uint8_t button = direction ? 114 : 113;
send_key (button, 1);
send_key (button, 0);
}
else if (ISPRESSED (12u))
{
uint8_t button = direction ? 117 : 112;
send_key (button, 1);
send_key (button, 0);
}
else
{
uint8_t button = ISPRESSED (9u) ? (direction ? 7 : 6) : (direction ? 5 : 4);
send_button (button, 1);
send_button (button, 0);
}
XFlush (display);
}
void
handle_event (EV ev, int count)
{
bool hasWheelEV = false;
for (int i = 0; i < count; ++i)
{
switch (ev[i].type)
{
case EV_SYN:
return;
case EV_REL:
if (ev[i].code == REL_WHEEL) // Shuttle
{
hasWheelEV = true;
if (shuttlevalue != ev[i].value)
{
shuttle (ev[i].value);
shuttlevalue = ev[i].value;
}
break;
}
if (ev[i].code == REL_DIAL) // Jog
{
if (ev[i].value == jogvalue)
{
if (!hasWheelEV && (uint) shuttlevalue & 1u && count < 3)
{
shuttle (0);
shuttlevalue = 0;
}
break;
}
if (((uint) ev[i].value ^ jogvalue) & 1u)
jog (ev[i].value > jogvalue);
else if ((jogvalue ^ (uint) ev[i].value) == 254u)
// Quick fix for skipping zero jumps:
// 255 -> 1, 1 -> 255
jog (ev[i].value < jogvalue);
jogvalue = ev[i].value;
}
break;
case EV_KEY:
key (ev[i].code, ev[i].value);
break;
}
}
}
void help (char *progname)
{
errf ("Usage: %s [-h] [-p] [device]", progname);
errf ("-h print this message");
errf ("-p enable hot-plugging");
errf ("device, if specified, is the name of the shuttle device to open.");
errf ("Otherwise the program will try to find a suitable device on its own.");
}
#include <glob.h>
uint8_t quit = 0;
void quit_callback ()
{
quit = 1;
}
int
main (int argc, char **argv)
{
EV ev;
const int evsize = sizeof (struct input_event);
char *dev_name;
// Put these single bit fields in a structure to save memory
struct mainopts o = {
.first_time = 1,
.hotplug = 0
};
int opt;
while ((opt = getopt (argc, argv, "hp::")) != -1)
{
switch (opt)
{
case 'h':
help (argv[0]);
exit (0);
case 'p':
o.hotplug = 1;
break;
default:
errf ("Try -h for help.");
exit (1);
}
}
if (optind + 1 < argc)
{
help (argv[0]);
exit (1);
}
if (optind >= argc)
{
// Try to find a suitable device. The following glob pattern should
// hopefully cover all existing versions.
glob_t globbuf;
if (glob ("/dev/input/by-id/usb-*Shuttle*-event-*",
0, NULL, &globbuf))
{
errf ("%s: found no suitable shuttle device", argv[0]);
errf ("Please make sure that your shuttle device is connected.");
errf ("You can also specify the device name on the command line.");
errf ("Try -h for help.");
exit (1);
}
else
{
dev_name = globbuf.gl_pathv[0];
errf ("%s: found shuttle device:\n%s", argv[0], dev_name);
}
}
else
{
dev_name = argv[optind];
}
initdisplay ();
struct sigaction int_handler = {.sa_handler = quit_callback};
sigaction (SIGINT, &int_handler, 0);
while (!quit)
{
o.fd = open (dev_name, O_RDONLY);
if (o.fd < 0)
{
perror (dev_name);
if (o.first_time)
{
exit (1);
}
}
else
{
// Flag it as exclusive access
if (ioctl (o.fd, EVIOCGRAB, 1) < 0)
{
perror ("evgrab ioctl");
}
else
{
o.first_time = 0;
while (!quit)
{
o.nread = read (o.fd, &ev, sizeof (ev));
if (o.nread < 0 && (o.hotplug || quit))
{
quit = 1;
break;
}
if (o.nread < evsize)
{
if (o.nread < 0)
{
perror ("read event");
break;
}
else
{
errf ("short read: %d", o.nread);
break;
}
}
else
{
handle_event (ev, o.nread / evsize);
}
}
}
}
close (o.fd);
if (!quit) sleep (1);
}
}