-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
571 lines (496 loc) · 21.9 KB
/
main.go
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* Copyright (C) 2021 Evuraan, <evuraan@gmail.com> */
package main
/*
#cgo CFLAGS: -Wall -Werror
#include <stdio.h>
#include <sys/types.h> // open
#include <sys/stat.h> // open
#include <fcntl.h> // open
#include <unistd.h> // read
#include <sys/epoll.h> // epoll
#include <stdlib.h> // EXIT_FAILURE
#include <time.h>
#include <stdint.h> // need int32
#include <linux/input.h>
#include <stdarg.h> // vaprintf
#include <string.h> // stncpy
#define ARRAY_MAX 128 // arrayLen in go
#define BUF 8192
#define END 65535
int fd = 0;
unsigned debug = 0;
unsigned enableDebug(){
debug = 1;
return debug;
}
char *get_currentTime() {
static char currentTime[2048] = {0};
time_t t;
time(&t);
strncpy(currentTime, ctime(&t), 1000);
currentTime[strcspn(currentTime, "\r\n")] = 0; // works for LF, CR, CRLF, LFCR, ...
return currentTime;
}
void print(char *format, ...) {
if (!debug){
return;
}
va_list arguments;
va_start(arguments, format);
char temp[BUF] = {0};
snprintf(temp, BUF, "%s [C] %s", get_currentTime(), format);
vfprintf(stdout, temp, arguments);
va_end(arguments);
}
unsigned emit(int fd, int type, int code, int val) {
struct input_event ie = { 0 };
ie.type = type;
ie.code = code;
ie.value = val;
size_t wrote = write(fd, &ie, sizeof(ie));
if (wrote > 0) {
print("[%s] emitted %ld bytes type %d code %d\n", __func__, wrote, type , code);
return wrote;
} else {
return 0;
}
}
int getFd(char *device) {
if (fd) {
return fd;
}
int fda = open(device, O_WRONLY | O_NONBLOCK);
if (fda < 1) {
fprintf(stderr, "err opening %s\n", device);
perror("open event c dev\n");
return -1;
}
fd = fda;
print("[%s] fd opened: %d\n", __func__, fd);
return fd;
}
void closeFd() {
if (!fd) {
close(fd);
fd = -1;
}
}
void printInt(int someInt){
print("[%s] int: %d\n", __func__, someInt);
}
void handleEvents(int32_t *events){
unsigned x = 0;
// Agenda: Key press, report the event, send key release, and report again
// do key press
for (int i = 0; i < ARRAY_MAX; i++){
if (events[i] == END){
break;
}
x++;
emit(fd, EV_KEY, events[i], 1);
}
if (!x){
// we got nothing.
return;
}
// report the keys
emit(fd, EV_SYN, SYN_REPORT, 0);
// release keys
for (int i = 0; i < ARRAY_MAX; i++){
if (events[i] == END){
break;
}
emit(fd, EV_KEY, events[i], 0);
}
// report the release
emit(fd, EV_SYN, SYN_REPORT, 0);
print("[%s] Handled %d events\n", __func__, x);
}
void handleComboEvents(int32_t *events){
unsigned x = 0;
// Agenda: Key press, report the event, send key release, and report again
// do key press
for (int i = 0; i < ARRAY_MAX; i++){
if (events[i] == END){
break;
}
x++;
emit(fd, EV_KEY, events[i], 1);
emit(fd, EV_SYN, SYN_REPORT, 0);
emit(fd, EV_KEY, events[i], 0);
emit(fd, EV_SYN, SYN_REPORT, 0);
}
if (!x){
// we got nothing.
return;
}
print("[%s] Handled %d events\n", __func__, x);
}
*/
import "C"
import (
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"sync"
"time"
"unsafe"
)
var (
stdout io.ReadCloser
eventDelay time.Duration
mustHave = []string{"/usr/libexec/libinput/libinput-debug-events", "libinput-debug-events"}
deBug = false
notifyBool = false
statusIconDisabled = false
directions = []string{up, down, left, right, fastdown, fastup, mediumDown, mediumUp}
workChan chan string
kbd = ""
configFile = ""
conduit = &conduitStruct{}
evt1 = map[string]string{}
// 2 finger touchPad events
evt2 = map[string]string{
right: "KEY_LEFTALT + KEY_LEFT",
left: "KEY_LEFTALT + KEY_RIGHT",
}
evt3 = map[string]string{
right: "KEY_LEFTALT + KEY_LEFT",
left: "KEY_LEFTALT + KEY_RIGHT",
up: "KEY_SPACE",
down: "KEY_LEFTSHIFT + KEY_SPACE",
}
evt4 = map[string]string{
right: "KEY_MUTE",
left: "KEY_MUTE",
down: "KEY_VOLUMEDOWN",
up: "KEY_VOLUMEUP",
}
evt5 = map[string]string{
"touch1up": "KEY_UP",
"touch1down": "KEY_DOWN",
"touch1left": "KEY_LEFTALT + KEY_LEFT",
"touch1right": "KEY_LEFTALT + KEY_RIGHT",
"touch2up": "KEY_UP",
"touch2down": "KEY_DOWN",
"touch2left": "KEY_LEFTALT + KEY_LEFT",
"touch2right": "KEY_LEFTALT + KEY_RIGHT",
"touch3up": "KEY_UP",
"touch3down": "KEY_DOWN",
"touch3left": "KEY_LEFTALT + KEY_LEFT",
"touch3right": "KEY_LEFTALT + KEY_RIGHT",
"touch4up": "KEY_UP",
"touch4down": "KEY_DOWN",
"touch4left": "KEY_LEFTALT + KEY_LEFT",
"touch4right": "KEY_LEFTALT + KEY_RIGHT",
}
eventLibStuff = &eventLib{eventCodes: map[string]int{"KEY_RESERVED": 0, "KEY_ESC": 1, "KEY_1": 2, "KEY_2": 3, "KEY_3": 4, "KEY_4": 5, "KEY_5": 6, "KEY_6": 7, "KEY_7": 8, "KEY_8": 9, "KEY_9": 10, "KEY_0": 11, "KEY_MINUS": 12, "KEY_EQUAL": 13, "KEY_BACKSPACE": 14, "KEY_TAB": 15, "KEY_Q": 16, "KEY_W": 17, "KEY_E": 18, "KEY_R": 19, "KEY_T": 20, "KEY_Y": 21, "KEY_U": 22, "KEY_I": 23, "KEY_O": 24, "KEY_P": 25, "KEY_LEFTBRACE": 26, "KEY_RIGHTBRACE": 27, "KEY_ENTER": 28, "KEY_LEFTCTRL": 29, "KEY_A": 30, "KEY_S": 31, "KEY_D": 32, "KEY_F": 33, "KEY_G": 34, "KEY_H": 35, "KEY_J": 36, "KEY_K": 37, "KEY_L": 38, "KEY_SEMICOLON": 39, "KEY_APOSTROPHE": 40, "KEY_GRAVE": 41, "KEY_LEFTSHIFT": 42, "KEY_BACKSLASH": 43, "KEY_Z": 44, "KEY_X": 45, "KEY_C": 46, "KEY_V": 47, "KEY_B": 48, "KEY_N": 49, "KEY_M": 50, "KEY_COMMA": 51, "KEY_DOT": 52, "KEY_SLASH": 53, "KEY_RIGHTSHIFT": 54, "KEY_KPASTERISK": 55, "KEY_LEFTALT": 56, "KEY_SPACE": 57, "KEY_CAPSLOCK": 58, "KEY_F1": 59, "KEY_F2": 60, "KEY_F3": 61, "KEY_F4": 62, "KEY_F5": 63, "KEY_F6": 64, "KEY_F7": 65, "KEY_F8": 66, "KEY_F9": 67, "KEY_F10": 68, "KEY_NUMLOCK": 69, "KEY_SCROLLLOCK": 70, "KEY_KP7": 71, "KEY_KP8": 72, "KEY_KP9": 73, "KEY_KPMINUS": 74, "KEY_KP4": 75, "KEY_KP5": 76, "KEY_KP6": 77, "KEY_KPPLUS": 78, "KEY_KP1": 79, "KEY_KP2": 80, "KEY_KP3": 81, "KEY_KP0": 82, "KEY_KPDOT": 83, "KEY_ZENKAKUHANKAKU": 85, "KEY_102ND": 86, "KEY_F11": 87, "KEY_F12": 88, "KEY_RO": 89, "KEY_KATAKANA": 90, "KEY_HIRAGANA": 91, "KEY_HENKAN": 92, "KEY_KATAKANAHIRAGANA": 93, "KEY_MUHENKAN": 94, "KEY_KPJPCOMMA": 95, "KEY_KPENTER": 96, "KEY_RIGHTCTRL": 97, "KEY_KPSLASH": 98, "KEY_SYSRQ": 99, "KEY_RIGHTALT": 100, "KEY_LINEFEED": 101, "KEY_HOME": 102, "KEY_UP": 103, "KEY_PAGEUP": 104, "KEY_LEFT": 105, "KEY_RIGHT": 106, "KEY_END": 107, "KEY_DOWN": 108, "KEY_PAGEDOWN": 109, "KEY_INSERT": 110, "KEY_DELETE": 111, "KEY_MACRO": 112, "KEY_MUTE": 113, "KEY_VOLUMEDOWN": 114, "KEY_VOLUMEUP": 115, "KEY_POWER": 116, "KEY_KPEQUAL": 117, "KEY_KPPLUSMINUS": 118, "KEY_PAUSE": 119, "KEY_SCALE": 120, "KEY_KPCOMMA": 121, "KEY_HANGEUL": 122, "KEY_HANJA": 123, "KEY_YEN": 124, "KEY_LEFTMETA": 125, "KEY_RIGHTMETA": 126, "KEY_COMPOSE": 127, "KEY_STOP": 128, "KEY_AGAIN": 129, "KEY_PROPS": 130, "KEY_UNDO": 131, "KEY_FRONT": 132, "KEY_COPY": 133, "KEY_OPEN": 134, "KEY_PASTE": 135, "KEY_FIND": 136, "KEY_CUT": 137, "KEY_HELP": 138, "KEY_MENU": 139, "KEY_CALC": 140, "KEY_SETUP": 141, "KEY_SLEEP": 142, "KEY_WAKEUP": 143, "KEY_FILE": 144, "KEY_SENDFILE": 145, "KEY_DELETEFILE": 146, "KEY_XFER": 147, "KEY_PROG1": 148, "KEY_PROG2": 149, "KEY_WWW": 150, "KEY_MSDOS": 151, "KEY_COFFEE": 152, "KEY_ROTATE_DISPLAY": 153, "KEY_CYCLEWINDOWS": 154, "KEY_MAIL": 155, "KEY_BOOKMARKS": 156, "KEY_COMPUTER": 157, "KEY_BACK": 158, "KEY_FORWARD": 159, "KEY_CLOSECD": 160, "KEY_EJECTCD": 161, "KEY_EJECTCLOSECD": 162, "KEY_NEXTSONG": 163, "KEY_PLAYPAUSE": 164, "KEY_PREVIOUSSONG": 165, "KEY_STOPCD": 166, "KEY_RECORD": 167, "KEY_REWIND": 168, "KEY_PHONE": 169, "KEY_ISO": 170, "KEY_CONFIG": 171, "KEY_HOMEPAGE": 172, "KEY_REFRESH": 173, "KEY_EXIT": 174, "KEY_MOVE": 175, "KEY_EDIT": 176, "KEY_SCROLLUP": 177, "KEY_SCROLLDOWN": 178, "KEY_KPLEFTPAREN": 179, "KEY_KPRIGHTPAREN": 180, "KEY_NEW": 181, "KEY_REDO": 182, "KEY_F13": 183, "KEY_F14": 184, "KEY_F15": 185, "KEY_F16": 186, "KEY_F17": 187, "KEY_F18": 188, "KEY_F19": 189, "KEY_F20": 190, "KEY_F21": 191, "KEY_F22": 192, "KEY_F23": 193, "KEY_F24": 194, "KEY_PLAYCD": 200, "KEY_PAUSECD": 201, "KEY_PROG3": 202, "KEY_PROG4": 203, "KEY_DASHBOARD": 204, "KEY_SUSPEND": 205, "KEY_CLOSE": 206, "KEY_PLAY": 207, "KEY_FASTFORWARD": 208, "KEY_BASSBOOST": 209, "KEY_PRINT": 210, "KEY_HP": 211, "KEY_CAMERA": 212, "KEY_SOUND": 213, "KEY_QUESTION": 214, "KEY_EMAIL": 215, "KEY_CHAT": 216, "KEY_SEARCH": 217, "KEY_CONNECT": 218, "KEY_FINANCE": 219, "KEY_SPORT": 220, "KEY_SHOP": 221, "KEY_ALTERASE": 222, "KEY_CANCEL": 223, "KEY_BRIGHTNESSDOWN": 224, "KEY_BRIGHTNESSUP": 225, "KEY_MEDIA": 226, "KEY_SWITCHVIDEOMODE": 227, "KEY_KBDILLUMTOGGLE": 228, "KEY_KBDILLUMDOWN": 229, "KEY_KBDILLUMUP": 230, "KEY_SEND": 231, "KEY_REPLY": 232, "KEY_FORWARDMAIL": 233, "KEY_SAVE": 234, "KEY_DOCUMENTS": 235, "KEY_BATTERY": 236, "KEY_BLUETOOTH": 237, "KEY_WLAN": 238, "KEY_UWB": 239, "KEY_UNKNOWN": 240, "KEY_VIDEO_NEXT": 241, "KEY_VIDEO_PREV": 242, "KEY_BRIGHTNESS_CYCLE": 243, "KEY_BRIGHTNESS_AUTO": 244, "KEY_DISPLAY_OFF": 245, "KEY_WWAN": 246, "KEY_RFKILL": 247, "KEY_MICMUTE": 248, "KEY_OK": 0x160, "KEY_SELECT": 0x161, "KEY_GOTO": 0x162, "KEY_CLEAR": 0x163, "KEY_POWER2": 0x164, "KEY_OPTION": 0x165, "KEY_INFO": 0x166, "KEY_TIME": 0x167, "KEY_VENDOR": 0x168, "KEY_ARCHIVE": 0x169, "KEY_PROGRAM": 0x16a, "KEY_CHANNEL": 0x16b, "KEY_FAVORITES": 0x16c, "KEY_EPG": 0x16d, "KEY_PVR": 0x16e, "KEY_MHP": 0x16f, "KEY_LANGUAGE": 0x170, "KEY_TITLE": 0x171, "KEY_SUBTITLE": 0x172, "KEY_ANGLE": 0x173, "KEY_FULL_SCREEN": 0x174, "KEY_MODE": 0x175, "KEY_KEYBOARD": 0x176, "KEY_ASPECT_RATIO": 0x177, "KEY_PC": 0x178, "KEY_TV": 0x179, "KEY_TV2": 0x17a, "KEY_VCR": 0x17b, "KEY_VCR2": 0x17c, "KEY_SAT": 0x17d, "KEY_SAT2": 0x17e, "KEY_CD": 0x17f, "KEY_TAPE": 0x180, "KEY_RADIO": 0x181, "KEY_TUNER": 0x182, "KEY_PLAYER": 0x183, "KEY_TEXT": 0x184, "KEY_DVD": 0x185, "KEY_AUX": 0x186, "KEY_MP3": 0x187, "KEY_AUDIO": 0x188, "KEY_VIDEO": 0x189, "KEY_DIRECTORY": 0x18a, "KEY_LIST": 0x18b, "KEY_MEMO": 0x18c, "KEY_CALENDAR": 0x18d, "KEY_RED": 0x18e, "KEY_GREEN": 0x18f, "KEY_YELLOW": 0x190, "KEY_BLUE": 0x191, "KEY_CHANNELUP": 0x192, "KEY_CHANNELDOWN": 0x193, "KEY_FIRST": 0x194, "KEY_LAST": 0x195, "KEY_AB": 0x196, "KEY_NEXT": 0x197, "KEY_RESTART": 0x198, "KEY_SLOW": 0x199, "KEY_SHUFFLE": 0x19a, "KEY_BREAK": 0x19b, "KEY_PREVIOUS": 0x19c, "KEY_DIGITS": 0x19d, "KEY_TEEN": 0x19e, "KEY_TWEN": 0x19f, "KEY_VIDEOPHONE": 0x1a0, "KEY_GAMES": 0x1a1, "KEY_ZOOMIN": 0x1a2, "KEY_ZOOMOUT": 0x1a3, "KEY_ZOOMRESET": 0x1a4, "KEY_WORDPROCESSOR": 0x1a5, "KEY_EDITOR": 0x1a6, "KEY_SPREADSHEET": 0x1a7, "KEY_GRAPHICSEDITOR": 0x1a8, "KEY_PRESENTATION": 0x1a9, "KEY_DATABASE": 0x1aa, "KEY_NEWS": 0x1ab, "KEY_VOICEMAIL": 0x1ac, "KEY_ADDRESSBOOK": 0x1ad, "KEY_MESSENGER": 0x1ae, "KEY_DISPLAYTOGGLE": 0x1af, "KEY_SPELLCHECK": 0x1b0, "KEY_LOGOFF": 0x1b1, "KEY_DOLLAR": 0x1b2, "KEY_EURO": 0x1b3, "KEY_FRAMEBACK": 0x1b4, "KEY_FRAMEFORWARD": 0x1b5, "KEY_CONTEXT_MENU": 0x1b6, "KEY_MEDIA_REPEAT": 0x1b7, "KEY_10CHANNELSUP": 0x1b8, "KEY_10CHANNELSDOWN": 0x1b9, "KEY_IMAGES": 0x1ba, "KEY_DEL_EOL": 0x1c0, "KEY_DEL_EOS": 0x1c1, "KEY_INS_LINE": 0x1c2, "KEY_DEL_LINE": 0x1c3, "KEY_FN": 0x1d0, "KEY_FN_ESC": 0x1d1, "KEY_FN_F1": 0x1d2, "KEY_FN_F2": 0x1d3, "KEY_FN_F3": 0x1d4, "KEY_FN_F4": 0x1d5, "KEY_FN_F5": 0x1d6, "KEY_FN_F6": 0x1d7, "KEY_FN_F7": 0x1d8, "KEY_FN_F8": 0x1d9, "KEY_FN_F9": 0x1da, "KEY_FN_F10": 0x1db, "KEY_FN_F11": 0x1dc, "KEY_FN_F12": 0x1dd, "KEY_FN_1": 0x1de, "KEY_FN_2": 0x1df, "KEY_FN_D": 0x1e0, "KEY_FN_E": 0x1e1, "KEY_FN_F": 0x1e2, "KEY_FN_S": 0x1e3, "KEY_FN_B": 0x1e4, "KEY_BRL_DOT1": 0x1f1, "KEY_BRL_DOT2": 0x1f2, "KEY_BRL_DOT3": 0x1f3, "KEY_BRL_DOT4": 0x1f4, "KEY_BRL_DOT5": 0x1f5, "KEY_BRL_DOT6": 0x1f6, "KEY_BRL_DOT7": 0x1f7, "KEY_BRL_DOT8": 0x1f8, "KEY_BRL_DOT9": 0x1f9, "KEY_BRL_DOT10": 0x1fa, "KEY_NUMERIC_0": 0x200, "KEY_NUMERIC_1": 0x201, "KEY_NUMERIC_2": 0x202, "KEY_NUMERIC_3": 0x203, "KEY_NUMERIC_4": 0x204, "KEY_NUMERIC_5": 0x205, "KEY_NUMERIC_6": 0x206, "KEY_NUMERIC_7": 0x207, "KEY_NUMERIC_8": 0x208, "KEY_NUMERIC_9": 0x209, "KEY_NUMERIC_STAR": 0x20a, "KEY_NUMERIC_POUND": 0x20b, "KEY_NUMERIC_A": 0x20c, "KEY_NUMERIC_B": 0x20d, "KEY_NUMERIC_C": 0x20e, "KEY_NUMERIC_D": 0x20f, "KEY_CAMERA_FOCUS": 0x210, "KEY_WPS_BUTTON": 0x211, "KEY_TOUCHPAD_TOGGLE": 0x212, "KEY_TOUCHPAD_ON": 0x213, "KEY_TOUCHPAD_OFF": 0x214, "KEY_CAMERA_ZOOMIN": 0x215, "KEY_CAMERA_ZOOMOUT": 0x216, "KEY_CAMERA_UP": 0x217, "KEY_CAMERA_DOWN": 0x218, "KEY_CAMERA_LEFT": 0x219, "KEY_CAMERA_RIGHT": 0x21a, "KEY_ATTENDANT_ON": 0x21b, "KEY_ATTENDANT_OFF": 0x21c, "KEY_ATTENDANT_TOGGLE": 0x21d, "KEY_LIGHTS_TOGGLE": 0x21e, "KEY_ALS_TOGGLE": 0x230, "KEY_ROTATE_LOCK_TOGGLE": 0x231, "KEY_BUTTONCONFIG": 0x240, "KEY_TASKMANAGER": 0x241, "KEY_JOURNAL": 0x242, "KEY_CONTROLPANEL": 0x243, "KEY_APPSELECT": 0x244, "KEY_SCREENSAVER": 0x245, "KEY_VOICECOMMAND": 0x246, "KEY_ASSISTANT": 0x247, "KEY_KBD_LAYOUT_NEXT": 0x248, "KEY_BRIGHTNESS_MIN": 0x250, "KEY_BRIGHTNESS_MAX": 0x251, "KEY_KBDINPUTASSIST_PREV": 0x260, "KEY_KBDINPUTASSIST_NEXT": 0x261, "KEY_KBDINPUTASSIST_PREVGROUP": 0x262, "KEY_KBDINPUTASSIST_NEXTGROUP": 0x263, "KEY_KBDINPUTASSIST_ACCEPT": 0x264, "KEY_KBDINPUTASSIST_CANCEL": 0x265, "KEY_RIGHT_UP": 0x266, "KEY_RIGHT_DOWN": 0x267, "KEY_LEFT_UP": 0x268, "KEY_LEFT_DOWN": 0x269, "KEY_ROOT_MENU": 0x26a, "KEY_MEDIA_TOP_MENU": 0x26b, "KEY_NUMERIC_11": 0x26c, "KEY_NUMERIC_12": 0x26d, "KEY_AUDIO_DESC": 0x26e, "KEY_3D_MODE": 0x26f, "KEY_NEXT_FAVORITE": 0x270, "KEY_STOP_RECORD": 0x271, "KEY_PAUSE_RECORD": 0x272, "KEY_VOD": 0x273, "KEY_UNMUTE": 0x274, "KEY_FASTREVERSE": 0x275, "KEY_SLOWREVERSE": 0x276, "KEY_DATA": 0x277, "KEY_ONSCREEN_KEYBOARD": 0x278, "KEY_PRIVACY_SCREEN_TOGGLE": 0x279, "KEY_SELECTIVE_SCREENSHOT": 0x27a, "KEY_MACRO1": 0x290, "KEY_MACRO2": 0x291, "KEY_MACRO3": 0x292, "KEY_MACRO4": 0x293, "KEY_MACRO5": 0x294, "KEY_MACRO6": 0x295, "KEY_MACRO7": 0x296, "KEY_MACRO8": 0x297, "KEY_MACRO9": 0x298, "KEY_MACRO10": 0x299, "KEY_MACRO11": 0x29a, "KEY_MACRO12": 0x29b, "KEY_MACRO13": 0x29c, "KEY_MACRO14": 0x29d, "KEY_MACRO15": 0x29e, "KEY_MACRO16": 0x29f, "KEY_MACRO17": 0x2a0, "KEY_MACRO18": 0x2a1, "KEY_MACRO19": 0x2a2, "KEY_MACRO20": 0x2a3, "KEY_MACRO21": 0x2a4, "KEY_MACRO22": 0x2a5, "KEY_MACRO23": 0x2a6, "KEY_MACRO24": 0x2a7, "KEY_MACRO25": 0x2a8, "KEY_MACRO26": 0x2a9, "KEY_MACRO27": 0x2aa, "KEY_MACRO28": 0x2ab, "KEY_MACRO29": 0x2ac, "KEY_MACRO30": 0x2ad, "KEY_MACRO_RECORD_START": 0x2b0, "KEY_MACRO_RECORD_STOP": 0x2b1, "KEY_MACRO_PRESET_CYCLE": 0x2b2, "KEY_MACRO_PRESET1": 0x2b3, "KEY_MACRO_PRESET2": 0x2b4, "KEY_MACRO_PRESET3": 0x2b5, "KEY_KBD_LCD_MENU1": 0x2b8, "KEY_KBD_LCD_MENU2": 0x2b9, "KEY_KBD_LCD_MENU3": 0x2ba, "KEY_KBD_LCD_MENU4": 0x2bb, "KEY_KBD_LCD_MENU5": 0x2bc, "KEY_MAX": 0x2ff}}
)
const (
progName = "Swipe"
ver = "10a.5"
stdBuf = "stdbuf"
swipeStart = "GESTURE_SWIPE_BEGIN"
swipeUpdate = "GESTURE_SWIPE_UPDATE"
swipeEnd = "GESTURE_SWIPE_END"
touchStart = "TOUCH_DOWN"
touchEnd = "TOUCH_UP"
POINTER_AXIS = "POINTER_AXIS"
octoberTwoFin = "POINTER_SCROLL_FINGER"
oct2FinDelay = 250 * time.Millisecond
defaultDelay = 100 * time.Millisecond
touchMin = 1
up = "UP"
down = "DOWN"
left = "LEFT"
right = "RIGHT"
fastup = "FASTUP"
mediumUp = "MEDIUMUP"
mediumDown = "MEDIUMDOWN"
fastdown = "FASTDOWN"
tag = progName + "/" + ver
layout = "Mon Jan 02 15:04:05 2006"
END = 65535
procWidth = 20
notifyCmd = "notify-send " + progName
arrayLen = 128 // max key+key+key events: 8
inpdev = "/dev/input/event3"
sampleConf = `
# 2 button touchpad gestures
2right: "KEY_LEFTALT + KEY_LEFT"
2left: "KEY_LEFTALT + KEY_RIGHT"
# 3 button touchpad gestures
3right: "KEY_LEFTALT + KEY_LEFT"
3left: "KEY_LEFTALT + KEY_RIGHT"
3up: "KEY_SPACE"
3down: "KEY_LEFTSHIFT + KEY_SPACE"
# 4 button touchpad gestures
4right: "KEY_MUTE"
4left: "KEY_MUTE"
4up: "KEY_VOLUMEUP"
4down: "KEY_VOLUMEDOWN"
# Touchscreen gestures
touch1up: "KEY_UP"
touch1down: "KEY_DOWN"
touch1left: "KEY_LEFTALT + KEY_LEFT"
touch1right:"KEY_LEFTALT + KEY_RIGHT"
touch2up: "KEY_UP"
touch2down: "KEY_DOWN"
touch2left: "KEY_LEFTALT + KEY_LEFT"
touch2right:"KEY_LEFTALT + KEY_RIGHT"
touch3up: "KEY_UP"
touch3down: "KEY_DOWN"
touch3left: "KEY_LEFTALT + KEY_LEFT"
touch3right:"KEY_LEFTALT + KEY_RIGHT"
touch4up: "KEY_UP"
touch4down: "KEY_DOWN"
touch4left: "KEY_LEFTALT + KEY_LEFT"
touch4right:"KEY_LEFTALT + KEY_RIGHT"
`
)
type moves struct {
a float64
b float64
c float64
d float64
}
type eventLib struct {
lastFired time.Time
eventCodes map[string]int
sync.RWMutex
}
func checkKbd(kbd string) {
if !strings.Contains(kbd, "/dev/input/event") {
fmt.Fprintf(os.Stderr, "Invalid input device %s\n", kbd)
os.Exit(1)
}
_, err := os.Stat(kbd)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not stat %s: %s\n", kbd, err)
os.Exit(1)
}
}
func main() {
helpBool := false
flag.BoolVar(&helpBool, "help", false, "Show help")
verBool := false
flag.BoolVar(&verBool, "version", false, "Show version")
sampleCfgBool := false
flag.BoolVar(&sampleCfgBool, "sampleCfg", false, "Show sample config")
keysAvailBool := false
flag.BoolVar(&keysAvailBool, "keys", false, "Show available keys")
devsAvailBool := false
flag.BoolVar(&devsAvailBool, "available", false, "Show available devices")
flag.BoolVar(&deBug, "debug", false, "Enable debug")
flag.BoolVar(&statusIconDisabled, "noIndicator", false, "Disable status icon")
// kbd
defaultKbd := getDeviceForPattern(keyboard)
flag.StringVar(&kbd, "i", defaultKbd, fmt.Sprintf("Input device, eg: %s\n", inpdev))
// config file path
flag.StringVar(&configFile, "c", "", "Config file path")
// optional time delay between event processing
flag.DurationVar(&eventDelay, "delay", defaultDelay, "Delay between events")
flag.Parse()
if verBool {
fmt.Println("Version:", tag)
return
}
if sampleCfgBool {
fmt.Printf("\nSample Config: %s\n", sampleConf)
return
}
if keysAvailBool {
showKeys()
return
}
if devsAvailBool {
showDevices()
return
}
fmt.Printf("Copyright © 2021 Evuraan <evuraan@gmail.com>. All rights reserved.\nThis program comes with ABSOLUTELY NO WARRANTY.\n")
// see if a config file was specified
if configFile != "" {
if !checkFile(configFile) {
fmt.Fprintf(os.Stderr, "Could not find config file %s\n", configFile)
os.Exit(1)
}
parseConfig(configFile)
// our config maps now must be present - at least one of them
configMaps := []string{"evt1", "evt2", "evt3", "evt4", "evt5"}
nonEmpty := 0
for _, evtMap := range configMaps {
nonEmpty += len(evtMap)
}
if nonEmpty < 1 {
fmt.Fprint(os.Stderr, "Config maps are empty. Fatal!")
os.Exit(1)
}
}
workChan = make(chan string, 20)
go func() {
for cmdString := range workChan {
if len(cmdString) > 0 {
go func(cmdString string) {
_ = doRun(cmdString)
}(cmdString)
}
}
}()
print("Howdy!")
checkKbd(kbd) // we die if this fails
print("%s device: %s", keyboard, kbd)
cKbd := C.CString(kbd)
C.getFd(cKbd)
go setupPanelConduit()
libinput()
fmt.Println("Bye bye!")
}
func libinput() {
launcher := ""
stdBufBool := checkExec(stdBuf)
notifyBool = checkExec("notify-send")
for i := range mustHave {
if checkExec(mustHave[i]) {
launcher = mustHave[i]
if stdBufBool {
launcher = fmt.Sprintf("%s -oL %s", stdBuf, mustHave[i])
}
break
}
}
if len(launcher) < 1 {
fmt.Fprint(os.Stderr, "Error: Cannot find libinput debug command\n")
os.Exit(1)
}
err := runThis(launcher)
print("err :%s", err)
}
func showKeys() {
fmt.Println("Available keys:")
eventLibStuff.showKeys()
}
func showDevices() {
fmap := parseFileToMap(devices)
if len(fmap) < 1 {
return
}
devToName := make(map[string]string)
pat := regexp.MustCompile(`event\d*`)
for i := range fmap {
someLine := fmap[i]
if strings.HasPrefix(someLine, "N: Name=") {
name := someLine[8:]
if len(name) > 1 {
name = strings.ReplaceAll(name, "\"", "")
if len(name) <= 1 {
continue
}
pos := i + 4
if len(fmap) < pos {
continue
}
eventLine := fmap[pos]
s := pat.FindString(eventLine)
if len(s) > 0 {
devicePath := fmt.Sprintf("/dev/input/%s", s)
devToName[devicePath] = name
}
}
}
}
if len(devToName) < 1 {
fmt.Fprintf(os.Stderr, "Error: Could not find any input devices.\n")
os.Exit(1)
} else {
fmt.Printf("Available: %d devices\n", len(devToName))
}
for i := 0; i < len(devToName); i++ {
key := fmt.Sprintf("/dev/input/event%d", i)
rhs, ok := devToName[key]
if ok {
fmt.Printf("%s:\t%s\n", key, rhs)
}
}
}
func (eventLibPtr *eventLib) showKeys() {
self := eventLibPtr
self.RLock()
defer self.RUnlock()
for i := range self.eventCodes {
fmt.Println("key --> ", i)
}
fmt.Printf("%d keys available\n", len(self.eventCodes))
}
func (eventLibPtr *eventLib) handleEvent(event string, evtType int) bool {
if len(event) < 1 {
return false
}
self := eventLibPtr
useThisMap := evt3
switch evtType {
case 4:
useThisMap = evt4
case 5:
useThisMap = evt5
case 1:
useThisMap = evt1
case 2:
useThisMap = evt2
}
self.RLock()
defer self.RUnlock()
if len(useThisMap) < 1 {
print("dropping event, map len 0")
}
// check if last fired time is within the delay
if time.Since(self.lastFired) < eventDelay {
print("Dropping event %s, last fired %s", event, self.lastFired)
return false
}
cmd, ok := useThisMap[event]
if !ok {
print("Dropping event %s", event)
return false
}
comboBool := strings.Contains(cmd, "combo")
print("combo: %v", comboBool)
splat := strings.Split(cmd, "+")
if len(splat) < 1 {
return false
}
k := 0
var eventArray [arrayLen]int32
for i := range splat {
keyEvent := splat[i]
keyEvent = strings.TrimSpace(keyEvent)
keyIntVal, ok := self.eventCodes[keyEvent]
if !ok {
print("Missing keycode for %s", keyEvent)
continue
}
eventArray[k] = int32(keyIntVal)
k++
}
if k < 1 {
return false
}
eventArray[k] = END
// C.handleEvents((*C.int)(unsafe.Pointer(&eventArray[0])))
if comboBool {
C.handleComboEvents((*C.int)(unsafe.Pointer(&eventArray[0])))
} else {
C.handleEvents((*C.int)(unsafe.Pointer(&eventArray[0])))
}
go conduit.notifyFifo()
// update the last fired time
go func() {
self.Lock()
defer self.Unlock()
self.lastFired = time.Now()
}()
print("Gesture type %d, intent: %s, cmd: %s", evtType, event, cmd)
return true
}