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
|
#include "keyboard_event.h"
/* C stdlib */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
/* POSIX */
#include <unistd.h>
#include <dlfcn.h>
typedef struct {
uint32_t key;
uint32_t modifiers; /* keyboard_modkeys bitmask */
const char *action;
} global_hotkey;
#define NUM_GLOBAL_HOTKEYS 6
static global_hotkey global_hotkeys[NUM_GLOBAL_HOTKEYS] = {
{ .key = KEY_Z, .modifiers = KEYBOARD_MODKEY_LALT, .action = "show_hide" },
{ .key = KEY_F9, .modifiers = KEYBOARD_MODKEY_LALT, .action = "record" },
{ .key = KEY_F7, .modifiers = KEYBOARD_MODKEY_LALT, .action = "pause" },
{ .key = KEY_F8, .modifiers = KEYBOARD_MODKEY_LALT, .action = "stream" },
{ .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT | KEYBOARD_MODKEY_SHIFT, .action = "replay_start" },
{ .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT, .action = "replay_save" }
};
static bool on_key_callback(uint32_t key, uint32_t modifiers, int press_status, void *userdata) {
(void)userdata;
for(int i = 0; i < NUM_GLOBAL_HOTKEYS; ++i) {
if(key == global_hotkeys[i].key && modifiers == global_hotkeys[i].modifiers) {
if(press_status == 1) { /* 1 == Pressed */
puts(global_hotkeys[i].action);
fflush(stdout);
}
return false;
}
}
return true;
}
static void usage(void) {
fprintf(stderr, "usage: gsr-global-hotkeys [--all|--virtual]\n");
fprintf(stderr, "OPTIONS:\n");
fprintf(stderr, " --all Grab all devices.\n");
fprintf(stderr, " --virtual Grab all virtual devices only.\n");
}
typedef void* (*XOpenDisplay_FUNC)(const char*);
typedef int (*XErrorHandler_FUNC)(void *display, void* error_event);
typedef XErrorHandler_FUNC (*XSetErrorHandler_FUNC)(XErrorHandler_FUNC handler);
static int x_ignore_error(void *display, void *ee) {
(void)display;
(void)ee;
return 0;
}
static x11_context setup_x11_context(void) {
x11_context x_context = {0};
XSetErrorHandler_FUNC XSetErrorHandler = NULL;
void *x11_lib = dlopen("libX11.so.6", RTLD_LAZY);
if(!x11_lib) {
fprintf(stderr, "Warning: dlopen libX11.so.6 failed\n");
return x_context;
}
XOpenDisplay_FUNC XOpenDisplay = dlsym(x11_lib, "XOpenDisplay");
if(!XOpenDisplay) {
fprintf(stderr, "Warning: dlsym XOpenDisplay failed\n");
goto fail;
}
x_context.XKeycodeToKeysym = dlsym(x11_lib, "XKeycodeToKeysym");
if(!x_context.XKeycodeToKeysym) {
fprintf(stderr, "Warning: dlsym XKeycodeToKeysym failed\n");
goto fail;
}
x_context.XPending = dlsym(x11_lib, "XPending");
if(!x_context.XPending) {
fprintf(stderr, "Warning: dlsym XPending failed\n");
goto fail;
}
x_context.XNextEvent = dlsym(x11_lib, "XNextEvent");
if(!x_context.XNextEvent) {
fprintf(stderr, "Warning: dlsym XNextEvent failed\n");
goto fail;
}
x_context.XRefreshKeyboardMapping = dlsym(x11_lib, "XRefreshKeyboardMapping");
if(!x_context.XRefreshKeyboardMapping) {
fprintf(stderr, "Warning: dlsym XRefreshKeyboardMapping failed\n");
goto fail;
}
x_context.display = XOpenDisplay(NULL);
if(!x_context.display) {
fprintf(stderr, "Warning: XOpenDisplay failed\n");
goto fail;
}
XSetErrorHandler = dlsym(x11_lib, "XSetErrorHandler");
if(XSetErrorHandler)
XSetErrorHandler(x_ignore_error);
else
fprintf(stderr, "Warning: dlsym XSetErrorHandler failed\n");
return x_context;
fail:
memset(&x_context, 0, sizeof(x_context));
dlclose(x11_lib);
return x_context;
}
int main(int argc, char **argv) {
keyboard_grab_type grab_type = KEYBOARD_GRAB_TYPE_ALL;
if(argc == 2) {
const char *grab_type_arg = argv[1];
if(strcmp(grab_type_arg, "--all") == 0) {
grab_type = KEYBOARD_GRAB_TYPE_ALL;
} else if(strcmp(grab_type_arg, "--virtual") == 0) {
grab_type = KEYBOARD_GRAB_TYPE_VIRTUAL;
} else {
fprintf(stderr, "Error: expected --all or --virtual, got %s\n", grab_type_arg);
usage();
return 1;
}
} else if(argc != 1) {
fprintf(stderr, "Error: expected 0 or 1 arguments, got %d argument(s)\n", argc);
usage();
return 1;
}
x11_context x_context = setup_x11_context();
const uid_t user_id = getuid();
if(geteuid() != 0) {
if(setuid(0) == -1) {
fprintf(stderr, "Error: failed to change user to root\n");
return 1;
}
}
keyboard_event keyboard_ev;
if(!keyboard_event_init(&keyboard_ev, true, true, grab_type, x_context)) {
fprintf(stderr, "Error: failed to setup hotplugging and no keyboard input devices were found\n");
setuid(user_id);
return 1;
}
fprintf(stderr, "Info: global hotkeys setup, waiting for hotkeys to be pressed\n");
for(;;) {
keyboard_event_poll_events(&keyboard_ev, -1, on_key_callback, NULL);
if(keyboard_event_stdout_has_failed(&keyboard_ev)) {
fprintf(stderr, "Info: stdout closed (parent process likely closed this process), exiting...\n");
break;
}
}
keyboard_event_deinit(&keyboard_ev);
setuid(user_id);
return 0;
}
|