This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
forked from teichopsia/idrac-kvm-keyboard-fix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycode-hack.c
122 lines (99 loc) · 2.24 KB
/
keycode-hack.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
/*
* Shared library hack to translate evdev keycodes to old style keycodes.
*/
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
static int (*real_XNextEvent)(Display *, XEvent *) = NULL;
static KeyCode (*real_XKeysymToKeycode)(Display *, KeySym) = NULL;
static KeySym (*sym_XKeycodeToKeysym)(Display *, KeyCode, int) = NULL;
static int hack_initialised = 0;
#define DEBUG 0
#ifdef DEBUG
static FILE *fd = NULL;
#endif
static void
hack_init(void)
{
void *h;
h = dlopen("libX11.so", RTLD_LAZY);
if (h == NULL) {
fprintf(stderr, "Unable to open libX11\n");
_exit(1);
}
real_XNextEvent = dlsym(h, "XNextEvent");
if (real_XNextEvent == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
real_XKeysymToKeycode = dlsym(h, "XKeysymToKeycode");
if (real_XKeysymToKeycode == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
sym_XKeycodeToKeysym = dlsym(h, "XKeycodeToKeysym");
if (sym_XKeycodeToKeysym == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
#ifdef DEBUG
if (fd == NULL) {
fd = fopen("/tmp/keycode-log", "a");
if (fd == NULL)
fprintf(stderr, "Unable to open key-log\n");
}
#endif
hack_initialised = 1;
}
int
XNextEvent(Display *display, XEvent *event)
{
int r;
if (!hack_initialised)
hack_init();
r = real_XNextEvent(display, event);
if (event->type == KeyPress || event->type == KeyRelease) {
XKeyEvent *keyevent;
KeySym keysym;
keyevent = (XKeyEvent *)event;
#ifdef DEBUG
fprintf(fd, "KeyEvent: %d\n", keyevent->keycode);
fflush(fd);
#endif
/* mangle keycodes */
keysym = sym_XKeycodeToKeysym(display, keyevent->keycode, 0);
switch (keysym) {
case XK_Up:
keyevent->keycode = 98;
break;
case XK_Down:
keyevent->keycode = 104;
break;
case XK_Left:
keyevent->keycode = 100;
break;
case XK_Right:
keyevent->keycode = 102;
break;
case XK_Print:
keyevent->keycode = 111;
break;
}
}
return r;
}
#ifdef DEBUG
KeyCode
XKeysymToKeycode(Display *display, KeySym keysym)
{
KeyCode keycode;
if (!hack_initialised)
hack_init();
keycode = real_XKeysymToKeycode(display, keysym);
fprintf(fd, "XKeysymToKeycode: %d\n", keycode);
fflush(fd);
return keycode;
}
#endif