forked from zevv/bucklespring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-libinput.c
120 lines (87 loc) · 2.18 KB
/
scan-libinput.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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/poll.h>
#include <string.h>
#include <errno.h>
#include <libinput.h>
#include "buckle.h"
static int open_restricted(const char *path, int flags, void *user_data)
{
int fd = open(path, flags);
if(fd < 0) {
fprintf(stderr, "Failed to open %s (%s)\n", path, strerror(errno));
}
return fd < 0 ? -errno : fd;
}
static void close_restricted(int fd, void *user_data)
{
close(fd);
}
static const struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
static void handle_key(struct libinput_event *ev)
{
struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
enum libinput_key_state state = libinput_event_keyboard_get_key_state(k);
uint32_t key = libinput_event_keyboard_get_key(k);
play(key, state == LIBINPUT_KEY_STATE_PRESSED);
}
static void handle_events(struct libinput *li)
{
struct libinput_event *ev;
libinput_dispatch(li);
while((ev = libinput_get_event(li))) {
switch(libinput_event_get_type(ev)) {
case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_key(ev);
break;
default:
break;
}
libinput_event_destroy(ev);
libinput_dispatch(li);
}
}
static void log_handler(struct libinput *li, enum libinput_log_priority priority,
const char *format, va_list args)
{
vprintf(format, args);
}
int scan(int verbose)
{
struct udev *udev;
struct libinput *li;
udev = udev_new();
if (!udev) {
fprintf(stderr, "Failed to initialize udev\n");
return -1;
}
li = libinput_udev_create_context(&interface, NULL, udev);
if(!li) {
fprintf(stderr, "Failed to initialize context\n");
return -1;
}
if(verbose) {
libinput_log_set_handler(li, log_handler);
libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
}
if (libinput_udev_assign_seat(li, "seat0")) {
fprintf(stderr, "Failed to set seat\n");
return -1;
}
libinput_dispatch(li);
struct pollfd fds;
fds.fd = libinput_get_fd(li);
fds.events = POLLIN;
fds.revents = 0;
while(poll(&fds, 1, -1) > -1) {
handle_events(li);
}
return 0;
}
void open_console(void)
{
}