-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathget_key_presses.c
More file actions
262 lines (229 loc) · 6.72 KB
/
get_key_presses.c
File metadata and controls
262 lines (229 loc) · 6.72 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <pthread.h>
#include <libudev.h>
#include <libinput.h>
#include <libevdev/libevdev.h>
#include "config.h"
#define MAX_BUFFER_LENGTH 512
enum error_code {
NO_ERROR,
UDEV_FAILED,
LIBINPUT_FAILED,
SEAT_FAILED,
PERMISSION_FAILED
};
struct input_handler_data {
struct udev *udev;
struct libinput *libinput;
};
static void *handle_input(void *user_data)
{
struct input_handler_data *input_handler_data = user_data;
char line[MAX_BUFFER_LENGTH];
while (fgets(line, MAX_BUFFER_LENGTH, stdin) != NULL) {
if (strcmp(line, "stop\n") == 0) {
libinput_unref(input_handler_data->libinput);
udev_unref(input_handler_data->udev);
exit(EXIT_SUCCESS);
}
}
return NULL;
}
static int open_restricted(const char *path, int flags, void *user_data)
{
(void)user_data;
int fd = open(path, flags);
if (fd < 0)
fprintf(stderr, "Failed to open %s because of %s.\n", path,
strerror(errno));
return fd < 0 ? -errno : fd;
}
static void close_restricted(int fd, void *user_data)
{
(void)user_data;
close(fd);
}
static const struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
static int print_key_event(struct libinput_event *event)
{
struct libinput_event_keyboard *keyboard =
libinput_event_get_keyboard_event(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
uint32_t time_stamp = libinput_event_keyboard_get_time(keyboard);
uint32_t key_code = libinput_event_keyboard_get_key(keyboard);
const char *key_name = libevdev_event_code_get_name(EV_KEY, key_code);
key_name = key_name ? key_name : "null";
enum libinput_key_state state_code =
libinput_event_keyboard_get_key_state(keyboard);
const char *state_name = state_code == LIBINPUT_KEY_STATE_PRESSED ?
"PRESSED" :
"RELEASED";
return printf("{"
"\"event_name\": \"KEYBOARD_KEY\", "
"\"event_type\": %d, "
"\"time_stamp\": %d, "
"\"key_name\": \"%s\", "
"\"key_code\": %d, "
"\"state_name\": \"%s\", "
"\"state_code\": %d"
"}\n",
event_type, time_stamp, key_name, key_code, state_name,
state_code);
}
static int print_button_event(struct libinput_event *event)
{
struct libinput_event_pointer *pointer =
libinput_event_get_pointer_event(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
uint32_t time_stamp = libinput_event_pointer_get_time(pointer);
uint32_t button_code = libinput_event_pointer_get_button(pointer);
const char *button_name =
libevdev_event_code_get_name(EV_KEY, button_code);
enum libinput_button_state state_code =
libinput_event_pointer_get_button_state(pointer);
const char *state_name = state_code == LIBINPUT_BUTTON_STATE_PRESSED ?
"PRESSED" :
"RELEASED";
return printf("{"
"\"event_name\": \"POINTER_BUTTON\", "
"\"event_type\": %d, "
"\"time_stamp\": %d, "
"\"key_name\": \"%s\", "
"\"key_code\": %d, "
"\"state_name\": \"%s\", "
"\"state_code\": %d"
"}\n",
event_type, time_stamp, button_name, button_code,
state_name, state_code);
}
static int handle_events(struct libinput *libinput)
{
int result = -1;
struct libinput_event *event;
if (libinput_dispatch(libinput) < 0)
return result;
// Please keep printing a line per json.
while ((event = libinput_get_event(libinput)) != NULL) {
switch (libinput_event_get_type(event)) {
// This program only handle key event.
case LIBINPUT_EVENT_KEYBOARD_KEY:
print_key_event(event);
break;
// Sorry, mouse button is also a key.
case LIBINPUT_EVENT_POINTER_BUTTON:
print_button_event(event);
break;
default:
break;
}
// Do a `fflush(stdout)` here, so when we write to pipes,
// the other one can always get a latest result.
// If we don't have `fflush(stdout)` here, pipe will save
// some lines in buffer and pass them together.
fflush(stdout);
libinput_event_destroy(event);
result = 0;
}
return result;
}
static int run_mainloop(struct libinput *libinput)
{
struct pollfd fd;
fd.fd = libinput_get_fd(libinput);
fd.events = POLLIN;
fd.revents = 0;
if (handle_events(libinput) != 0) {
fprintf(stderr,
"Expected device added events on startup but "
"got none. Maybe you don't have the right permissions?"
"\n");
return -1;
}
while (poll(&fd, 1, -1) > -1)
handle_events(libinput);
return 0;
}
void print_help(char *program_name)
{
printf("The backend of Show Me The Key.\n");
printf("Version " PROJECT_VERSION ".\n");
printf("Usage: %s [OPTION…]\n", program_name);
printf("Options:\n");
printf("\t-h, --help\tDisplay help then exit.\n");
printf("\t-v, --version\tDisplay version then exit.\n");
printf("Warning: This is the backend and is not designed to run "
"by users. You should run the frontend of Show Me The Key, "
"and the frontend will run this.\n");
}
int main(int argc, char *argv[])
{
const struct option long_options[] = { { "version", no_argument, 0,
'v' },
{ "help", no_argument, 0, 'h' },
{ NULL, 0, NULL, 0 } };
int option_index = 0;
int opt = 0;
while ((opt = getopt_long(argc, argv, "vh", long_options,
&option_index)) != -1) {
switch (opt) {
case 0:
// We don't use this.
break;
case 'v':
printf(PROJECT_VERSION "\n");
return 0;
case 'h':
print_help(argv[0]);
return 0;
case '?':
// getopt_long already printed an error message.
break;
default:
fprintf(stderr, "%s: Invalid option `-%c`.\n", argv[0],
opt);
break;
}
}
struct udev *udev = udev_new();
if (udev == NULL) {
fprintf(stderr, "Failed to initialize udev.\n");
return UDEV_FAILED;
}
struct libinput *libinput =
libinput_udev_create_context(&interface, NULL, udev);
if (!libinput) {
fprintf(stderr, "Failed to initialize libinput from udev.\n");
return LIBINPUT_FAILED;
}
// TODO: Support custom seat.
if (libinput_udev_assign_seat(libinput, "seat0") != 0) {
fprintf(stderr, "Failed to set seat.\n");
libinput_unref(libinput);
udev_unref(udev);
return SEAT_FAILED;
}
// Typically this will be run with pkexec as a subprocess,
// and the parent cannot kill it because it is privileged,
// so we use another thread to see if it gets "stop\n" from stdin,
// it will exit by itself.
pthread_t input_handler;
struct input_handler_data input_handler_data = { udev, libinput };
pthread_create(&input_handler, NULL, handle_input, &input_handler_data);
if (run_mainloop(libinput) < 0)
return PERMISSION_FAILED;
libinput_unref(libinput);
udev_unref(udev);
return NO_ERROR;
}