63 lines
1.0 KiB
C
63 lines
1.0 KiB
C
#include "input.h"
|
|
|
|
#include <assert.h>
|
|
#include <fcntl.h>
|
|
#include <linux/input.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
enum {
|
|
RELEASE = 0,
|
|
PRESS = 1,
|
|
REPEAT = 2,
|
|
NCALLBACKS,
|
|
};
|
|
|
|
static int input_fd;
|
|
static input_callback_t callbacks[NCALLBACKS];
|
|
|
|
int input_init()
|
|
{
|
|
memset(callbacks, 0, sizeof(callbacks));
|
|
|
|
input_fd = open("/dev/input/event0", O_RDONLY);
|
|
assert(input_fd != -1);
|
|
|
|
const int res = ioctl(input_fd, EVIOCGRAB, (void *)1);
|
|
assert(res != -1);
|
|
|
|
return input_fd;
|
|
}
|
|
|
|
void input_cleanup()
|
|
{
|
|
ioctl(input_fd, EVIOCGRAB, (void *)0);
|
|
close(input_fd);
|
|
}
|
|
|
|
void input_on_press(input_callback_t cb)
|
|
{
|
|
callbacks[PRESS] = cb;
|
|
}
|
|
|
|
void input_on_release(input_callback_t cb)
|
|
{
|
|
callbacks[RELEASE] = cb;
|
|
}
|
|
|
|
void input_on_repeat(input_callback_t cb)
|
|
{
|
|
callbacks[REPEAT] = cb;
|
|
}
|
|
|
|
void input_handle()
|
|
{
|
|
struct input_event ev;
|
|
const int len = read(input_fd, &ev, sizeof(ev));
|
|
assert(len == sizeof(ev));
|
|
if (ev.type == EV_KEY && 0 <= ev.value && ev.value < NCALLBACKS) {
|
|
if (callbacks[ev.value] != nullptr)
|
|
callbacks[ev.value](ev.code);
|
|
}
|
|
}
|