[client] evdev: read up to 256 events at a time

This commit is contained in:
Geoffrey McRae 2025-03-08 18:19:10 +11:00
parent 35e6a6e81a
commit 4278a10fe1

View File

@ -149,6 +149,8 @@ err:
static int evdev_thread(void * opaque) static int evdev_thread(void * opaque)
{ {
struct epoll_event * events = alloca(sizeof(*events) * state.deviceCount); struct epoll_event * events = alloca(sizeof(*events) * state.deviceCount);
struct input_event msgs[256];
DEBUG_INFO("evdev_thread Started"); DEBUG_INFO("evdev_thread Started");
while(app_isRunning()) while(app_isRunning())
{ {
@ -174,11 +176,9 @@ static int evdev_thread(void * opaque)
int waiting = epoll_wait(state.epoll, events, state.deviceCount, 100); int waiting = epoll_wait(state.epoll, events, state.deviceCount, 100);
for(int i = 0; i < waiting; ++i) for(int i = 0; i < waiting; ++i)
{ {
struct input_event ev;
EvdevDevice * device = (EvdevDevice *)events[i].data.ptr; EvdevDevice * device = (EvdevDevice *)events[i].data.ptr;
int n = read(device->fd, msgs, sizeof(msgs));
size_t n = read(device->fd, &ev, sizeof(ev)); if (n < 0)
if (n != sizeof(ev))
{ {
if (errno == ENODEV) if (errno == ENODEV)
{ {
@ -196,34 +196,42 @@ static int evdev_thread(void * opaque)
continue; continue;
} }
if (ev.type != EV_KEY) if (n % sizeof(*msgs) != 0)
continue; DEBUG_WARN("Incomplete evdev read: %s", device->path);
bool grabbed = state.grabbed; int count = n / sizeof(*msgs);
if (ev.value == 1) struct input_event *ev = msgs;
for(int i = 0; i < count; ++i, ++ev)
{ {
++state.keys[ev.code]; if (ev->type != EV_KEY)
continue;
if (grabbed && state.keys[ev.code] == 1) bool grabbed = state.grabbed;
app_handleKeyPressInternal(ev.code); if (ev->value == 1)
}
else if (ev.value == 0 && --state.keys[ev.code] <= 0)
{
state.keys[ev.code] = 0;
if (state.pending == PENDING_GRAB)
{ {
state.pending = PENDING_NONE; ++state.keys[ev->code];
evdev_grabKeyboard();
}
else if (state.pending == PENDING_UNGRAB)
{
state.pending = PENDING_NONE;
evdev_ungrabKeyboard();
}
if (grabbed) if (grabbed && state.keys[ev->code] == 1)
app_handleKeyReleaseInternal(ev.code); app_handleKeyPressInternal(ev->code);
}
else if (ev->value == 0 && --state.keys[ev->code] <= 0)
{
state.keys[ev->code] = 0;
if (state.pending == PENDING_GRAB)
{
state.pending = PENDING_NONE;
evdev_grabKeyboard();
}
else if (state.pending == PENDING_UNGRAB)
{
state.pending = PENDING_NONE;
evdev_ungrabKeyboard();
}
if (grabbed)
app_handleKeyReleaseInternal(ev->code);
}
} }
} }
} }