[client] evdev: fix input capture and device handling

Classify configured devices by keyboard and pointer capability so
automatic keyboard capture leaves relative pointer devices with the
display server.

Run descriptor, grab, physical-state, and LED operations on one worker.
Marshal normalized events through each display server's event loop and
serialize ownership changes with per-lane barriers.

Fall back whole lanes to native input when an evdev cohort cannot be
owned, and retry missing or removed devices without disabling the rest.

Handle report framing, SYN_DROPPED, high-resolution wheels, queue
overflow, held-state cleanup, and guest-driven keyboard LEDs. Restore
host LEDs and kernel grabs during teardown.

Re-evaluate capture when the active input transport changes.
This commit is contained in:
Geoffrey McRae
2026-08-21 13:10:34 +10:00
parent 8b38546049
commit 423269802f
12 changed files with 2359 additions and 301 deletions

View File

@@ -36,22 +36,31 @@ bool evdev_start(void);
void evdev_stop(void);
/**
* restore the display server hooks and free the device state; only
* call once display server callbacks have stopped
* free the device state; only call once display server callbacks have stopped
*/
void evdev_free(void);
/**
* grab the keyboard for exclusive access
* update the keyboard and pointer capture requirements
*/
void evdev_grabKeyboard(void);
void evdev_setGrab(bool keyboard, bool pointer);
/**
* ungrab the keyboard
* return the event fd used to dispatch captured input
*/
void evdev_ungrabKeyboard(void);
int evdev_getEventFD(void);
/**
* returns true if input should only be processed by evdev
* dispatch captured input on the display server's event thread
*/
void evdev_dispatch(void * opaque);
/**
* return true if keyboard input should only be processed by evdev
*/
bool evdev_isExclusive(void);
/**
* return true if pointer input should only be processed by evdev
*/
bool evdev_isPointerExclusive(void);

View File

@@ -25,6 +25,7 @@
typedef void (*LGInputKeyboardLEDsFn)(void * opaque,
bool valid, uint8_t leds);
typedef void (*LGInputAvailabilityFn)(void * opaque, bool available);
void lgInput_init(void);
void lgInput_free(void);
@@ -43,11 +44,20 @@ bool lgInput_supports(LG_InputSupport support);
* call back into the input layer. */
void lgInput_setKeyboardLEDsListener(
LGInputKeyboardLEDsFn callback, void * opaque);
/* The listener is called synchronously with the current input availability
* and later from transport threads whenever it changes. The callback must
* not call back into the input layer. */
void lgInput_setAvailabilityListener(
LGInputAvailabilityFn callback, void * opaque);
bool lgInput_keyDown(int key);
bool lgInput_keyUp(int key);
bool lgInput_keyboardLEDs(bool numLock, bool capsLock, bool scrollLock);
void lgInput_releaseKeys(void);
void lgInput_releaseButtons(void);
/* Release all tracked keyboard and mouse button state under one active-input
* transaction. */
void lgInput_releaseAll(void);
bool lgInput_mouseMotion(int32_t x, int32_t y);
bool lgInput_mousePosition(uint32_t x, uint32_t y, uint32_t width,

View File

@@ -174,6 +174,9 @@ bool app_isOverlayMode(void)
void app_updateCursorPos(double x, double y)
{
if (evdev_isPointerExclusive())
return;
const bool overlay = app_isOverlayMode();
if (overlay)
{
@@ -375,7 +378,7 @@ static int mapInputToImGuiButton(uint32_t button)
return -1;
}
void app_handleButtonPress(int button)
void app_handleButtonPressInternal(int button)
{
g_cursor.buttons |= (1U << button);
app_updateMouseButtons();
@@ -399,7 +402,13 @@ void app_handleButtonPress(int button)
DEBUG_ERROR("app_handleButtonPress: failed to send message");
}
void app_handleButtonRelease(int button)
void app_handleButtonPress(int button)
{
if (!evdev_isPointerExclusive())
app_handleButtonPressInternal(button);
}
void app_handleButtonReleaseInternal(int button)
{
g_cursor.buttons &= ~(1U << button);
app_updateMouseButtons();
@@ -422,8 +431,17 @@ void app_handleButtonRelease(int button)
DEBUG_ERROR("app_handleButtonRelease: failed to send message");
}
void app_handleButtonRelease(int button)
{
if (!evdev_isPointerExclusive())
app_handleButtonReleaseInternal(button);
}
void app_handleWheelMotion(double motion)
{
if (evdev_isPointerExclusive())
return;
if (app_isOverlayMode())
g_state.io->MouseWheel -= motion;
}
@@ -570,11 +588,15 @@ void app_handleKeyRelease(int sc)
void app_handleKeyboardTyped(const char * typed)
{
if (evdev_isExclusive())
return;
app_registerImGuiText(typed);
ImGuiIO_AddInputCharactersUTF8(g_state.io, typed);
}
void app_handleKeyboardModifiers(bool ctrl, bool shift, bool alt, bool super)
void app_handleKeyboardModifiersInternal(
bool ctrl, bool shift, bool alt, bool super)
{
g_state.modCtrl = ctrl;
g_state.modShift = shift;
@@ -582,8 +604,57 @@ void app_handleKeyboardModifiers(bool ctrl, bool shift, bool alt, bool super)
g_state.modSuper = super;
}
void app_handleKeyboardModifiers(bool ctrl, bool shift, bool alt, bool super)
{
if (!evdev_isExclusive())
app_handleKeyboardModifiersInternal(ctrl, shift, alt, super);
}
void app_handleInputResetInternal(bool keyboard, bool pointer)
{
if (pointer)
{
g_cursor.buttons = 0;
app_updateMouseButtons();
g_state.io->MouseDown[ImGuiMouseButton_Left ] = false;
g_state.io->MouseDown[ImGuiMouseButton_Right ] = false;
g_state.io->MouseDown[ImGuiMouseButton_Middle] = false;
}
if (keyboard)
{
for(int key = 0; key < KEY_MAX; ++key)
if (linux_to_imgui[key])
ImGuiIO_AddKeyEvent(g_state.io, linux_to_imgui[key], false);
g_state.modCtrl = false;
g_state.modShift = false;
g_state.modAlt = false;
g_state.modSuper = false;
g_state.escapeActive = false;
g_state.escapeTime = 0;
g_state.escapeAction = -1;
memset(g_state.escapeKeys, 0, sizeof(g_state.escapeKeys));
memset(g_state.escapeShiftKeys, 0,
sizeof(g_state.escapeShiftKeys));
atomic_store_explicit(
&g_state.keyModifiers, 0, memory_order_release);
}
if (keyboard && pointer)
lgInput_releaseAll();
else if (keyboard)
lgInput_releaseKeys();
else if (pointer)
lgInput_releaseButtons();
app_invalidateWindow(false);
}
void app_handleKeyboardLEDs(bool numLock, bool capsLock, bool scrollLock)
{
if (evdev_isExclusive())
return;
if (!core_inputEnabled())
return;
@@ -594,6 +665,9 @@ void app_handleKeyboardLEDs(bool numLock, bool capsLock, bool scrollLock)
void app_handleMouseRelative(double normx, double normy,
double rawx, double rawy)
{
if (evdev_isPointerExclusive())
return;
MTRACE("rel delta=%.3f,%.3f raw=%.3f,%.3f inWin=%d inView=%d "
"grab=%d", normx, normy, rawx, rawy, g_cursor.inWindow,
g_cursor.inView, g_cursor.grab);

View File

@@ -25,5 +25,10 @@
void app_handleKeyPressInternal(int scancode);
void app_handleKeyReleaseInternal(int scancode);
void app_handleButtonPressInternal(int button);
void app_handleButtonReleaseInternal(int button);
void app_handleKeyboardModifiersInternal(
bool ctrl, bool shift, bool alt, bool super);
void app_handleInputResetInternal(bool keyboard, bool pointer);
#endif

View File

@@ -25,6 +25,7 @@
#include "kb.h"
#include "message.h"
#include "input.h"
#include "evdev.h"
#include "common/time.h"
#include "common/debug.h"
@@ -140,6 +141,8 @@ bool core_inputEnabled(void)
void core_updateKeyboardGrab(void)
{
const bool inputEnabled = core_inputEnabled();
const bool localActive = g_state.focused && !app_isOverlayMode() &&
!g_state.ignoreInput;
/* Explicit capture is local window state and must survive an input provider
* becoming unavailable or changing during startup. */
const bool capture = g_cursor.grab && g_params.grabKeyboard &&
@@ -150,6 +153,7 @@ void core_updateKeyboardGrab(void)
const bool active = g_state.focused && !app_isOverlayMode() &&
(capture || (inputEnabled && (view || automatic)));
evdev_setGrab(active, localActive && g_cursor.grab);
if (active)
g_state.ds->grabKeyboard();
else

File diff suppressed because it is too large Load Diff

View File

@@ -42,15 +42,19 @@ static struct
LG_Lock bindingLock;
LG_RWLock activeLock;
LG_Lock keyboardLEDsDispatch;
LG_Lock availabilityDispatch;
uint32_t nextBindingEpoch;
LGInputKeyboardLEDsFn keyboardLEDsCallback;
void * keyboardLEDsOpaque;
LGInputAvailabilityFn availabilityCallback;
void * availabilityOpaque;
struct InputBinding fallback;
struct InputBinding transport;
struct InputBinding active;
bool useTransport;
bool notifiedAvailable;
_Atomic(bool) keys[KEY_MAX];
_Atomic(uint32_t) buttons;
@@ -114,11 +118,11 @@ static void dispatchKeyboardLEDs(void)
LG_LOCK(l_input.keyboardLEDsDispatch);
LG_LOCK_SHARED(l_input.activeLock);
LGInputKeyboardLEDsFn callback = l_input.keyboardLEDsCallback;
void * opaque = l_input.keyboardLEDsOpaque;
const bool valid = l_input.active.ops &&
void * opaque = l_input.keyboardLEDsOpaque;
const bool valid = l_input.active.ops &&
l_input.active.available &&
l_input.active.keyboardLEDsValid;
const uint8_t leds = l_input.active.keyboardLEDs;
const uint8_t leds = l_input.active.keyboardLEDs;
LG_UNLOCK_SHARED(l_input.activeLock);
if (callback)
@@ -126,6 +130,30 @@ static void dispatchKeyboardLEDs(void)
LG_UNLOCK(l_input.keyboardLEDsDispatch);
}
static bool updateAvailabilityNL(void)
{
const bool available = l_input.active.ops != NULL;
if (l_input.notifiedAvailable == available)
return false;
l_input.notifiedAvailable = available;
return true;
}
static void dispatchAvailability(void)
{
LG_LOCK(l_input.availabilityDispatch);
LG_LOCK_SHARED(l_input.activeLock);
LGInputAvailabilityFn callback = l_input.availabilityCallback;
void * opaque = l_input.availabilityOpaque;
const bool available = l_input.active.ops != NULL;
LG_UNLOCK_SHARED(l_input.activeLock);
if (callback)
callback(opaque, available);
LG_UNLOCK(l_input.availabilityDispatch);
}
static void releaseKeysNL(void)
{
for (int key = 0; key < KEY_MAX; ++key)
@@ -234,9 +262,12 @@ static void fallbackStatusChanged(void * opaque,
if (l_input.fallback.ops &&
l_input.fallback.epoch == (uint32_t)(uintptr_t)opaque)
keyboardLEDsChanged = updateStatusNL(&l_input.fallback, status);
const bool availabilityChanged = updateAvailabilityNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
if (keyboardLEDsChanged)
dispatchKeyboardLEDs();
if (availabilityChanged)
dispatchAvailability();
}
static void transportStatusChanged(void * opaque,
@@ -250,17 +281,21 @@ static void transportStatusChanged(void * opaque,
if (l_input.transport.ops &&
l_input.transport.epoch == (uint32_t)(uintptr_t)opaque)
keyboardLEDsChanged = updateStatusNL(&l_input.transport, status);
const bool availabilityChanged = updateAvailabilityNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
if (keyboardLEDsChanged)
dispatchKeyboardLEDs();
if (availabilityChanged)
dispatchAvailability();
}
void lgInput_init(void)
{
l_input.fallback = (struct InputBinding) { 0 };
l_input.transport = (struct InputBinding) { 0 };
l_input.active = (struct InputBinding) { 0 };
l_input.useTransport = true;
l_input.fallback = (struct InputBinding) { 0 };
l_input.transport = (struct InputBinding) { 0 };
l_input.active = (struct InputBinding) { 0 };
l_input.useTransport = true;
l_input.notifiedAvailable = false;
for (int key = 0; key < KEY_MAX; ++key)
atomic_init(&l_input.keys[key], false);
@@ -269,6 +304,7 @@ void lgInput_init(void)
LG_LOCK_INIT(l_input.bindingLock);
LG_RWLOCK_INIT(l_input.activeLock);
LG_LOCK_INIT(l_input.keyboardLEDsDispatch);
LG_LOCK_INIT(l_input.availabilityDispatch);
}
void lgInput_free(void)
@@ -278,16 +314,20 @@ void lgInput_free(void)
LG_LOCK(l_input.bindingLock);
LG_LOCK(l_input.keyboardLEDsDispatch);
LG_LOCK(l_input.availabilityDispatch);
LG_LOCK_EXCLUSIVE(l_input.activeLock);
resetActiveNL();
fallback = l_input.fallback;
transport = l_input.transport;
l_input.active = (struct InputBinding) { 0 };
l_input.fallback = (struct InputBinding) { 0 };
l_input.transport = (struct InputBinding) { 0 };
l_input.keyboardLEDsCallback = NULL;
l_input.keyboardLEDsOpaque = NULL;
fallback = l_input.fallback;
transport = l_input.transport;
l_input.active = (struct InputBinding) { 0 };
l_input.fallback = (struct InputBinding) { 0 };
l_input.transport = (struct InputBinding) { 0 };
l_input.keyboardLEDsCallback = NULL;
l_input.keyboardLEDsOpaque = NULL;
l_input.availabilityCallback = NULL;
l_input.availabilityOpaque = NULL;
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
LG_UNLOCK(l_input.availabilityDispatch);
LG_UNLOCK(l_input.keyboardLEDsDispatch);
if (fallback.ops && fallback.ops->setStatusListener)
@@ -297,6 +337,7 @@ void lgInput_free(void)
LG_UNLOCK(l_input.bindingLock);
LG_RWLOCK_FREE(l_input.activeLock);
LG_LOCK_FREE(l_input.availabilityDispatch);
LG_LOCK_FREE(l_input.keyboardLEDsDispatch);
LG_LOCK_FREE(l_input.bindingLock);
}
@@ -325,10 +366,13 @@ static void setBinding(struct InputBinding * target,
LG_LOCK_EXCLUSIVE(l_input.activeLock);
*target = next;
const bool keyboardLEDsChanged = updateActiveNL(false);
const bool availabilityChanged = updateAvailabilityNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
if (keyboardLEDsChanged)
dispatchKeyboardLEDs();
if (availabilityChanged)
dispatchAvailability();
if (next.ops && next.ops->setStatusListener)
next.ops->setStatusListener(next.opaque, statusFn,
@@ -343,9 +387,12 @@ static void dropBinding(struct InputBinding * target)
const bool wasActive = bindingEqual(&l_input.active, target);
*target = (struct InputBinding) { 0 };
const bool keyboardLEDsChanged = updateActiveNL(wasActive);
const bool availabilityChanged = updateAvailabilityNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
if (keyboardLEDsChanged)
dispatchKeyboardLEDs();
if (availabilityChanged)
dispatchAvailability();
LG_UNLOCK(l_input.bindingLock);
}
@@ -374,9 +421,12 @@ void lgInput_useTransport(bool enable)
LG_LOCK_EXCLUSIVE(l_input.activeLock);
l_input.useTransport = enable;
const bool keyboardLEDsChanged = updateActiveNL(false);
const bool availabilityChanged = updateAvailabilityNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
if (keyboardLEDsChanged)
dispatchKeyboardLEDs();
if (availabilityChanged)
dispatchAvailability();
}
bool lgInput_available(void)
@@ -423,6 +473,21 @@ void lgInput_setKeyboardLEDsListener(
LG_UNLOCK(l_input.keyboardLEDsDispatch);
}
void lgInput_setAvailabilityListener(
LGInputAvailabilityFn callback, void * opaque)
{
LG_LOCK(l_input.availabilityDispatch);
LG_LOCK_SHARED(l_input.activeLock);
l_input.availabilityCallback = callback;
l_input.availabilityOpaque = opaque;
const bool available = l_input.active.ops != NULL;
LG_UNLOCK_SHARED(l_input.activeLock);
if (callback)
callback(opaque, available);
LG_UNLOCK(l_input.availabilityDispatch);
}
bool lgInput_keyDown(int key)
{
if (key < 0 || key >= KEY_MAX)
@@ -483,6 +548,21 @@ void lgInput_releaseKeys(void)
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
}
void lgInput_releaseButtons(void)
{
LG_LOCK_EXCLUSIVE(l_input.activeLock);
releaseButtonsNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
}
void lgInput_releaseAll(void)
{
LG_LOCK_EXCLUSIVE(l_input.activeLock);
releaseKeysNL();
releaseButtonsNL();
LG_UNLOCK_EXCLUSIVE(l_input.activeLock);
}
bool lgInput_mouseMotion(int32_t x, int32_t y)
{
LG_LOCK_SHARED(l_input.activeLock);

View File

@@ -3603,7 +3603,7 @@ static int lg_run(void)
}
if (evdev_start())
DEBUG_INFO("Using evdev for keyboard capture");
DEBUG_INFO("Using evdev for input capture");
// override the SIGINIT handler so that we can tell the difference between
// SIGINT and the user sending a close event, such as ALT+F4
@@ -3739,7 +3739,13 @@ static int lg_run(void)
.largeCursorDot = g_params.largeCursorDot,
.allowNoInput = strcmp(g_params.transport, "test") == 0,
.opengl = needsOpenGL,
.jitRender = g_params.jitRender
.jitRender = g_params.jitRender,
.eventSource =
{
.fd = evdev_getEventFD(),
.callback = evdev_dispatch,
.opaque = NULL,
},
};
g_state.dsInitialized = g_state.ds->init(params);

View File

@@ -163,6 +163,10 @@ bool core_inputEnabled(void)
return t.input;
}
void core_updateKeyboardGrab(void)
{
}
void core_setGrab(bool enable)
{
++t.grabN;

View File

@@ -224,6 +224,10 @@ void core_updatePositionInfo(void)
++t.posN;
}
void core_updateKeyboardGrab(void)
{
}
void core_setCursorInView(bool enable)
{
++t.cursorN;

View File

@@ -282,6 +282,12 @@ void app_mouseTrace(const char * file, unsigned int line,
(void)format;
}
void evdev_setGrab(bool keyboard, bool pointer)
{
(void)keyboard;
(void)pointer;
}
static bool inputSupports(void * opaque, LG_InputSupport support)
{
return support == LG_INPUT_SUPPORT_MOUSE_ABSOLUTE &&

View File

@@ -95,7 +95,10 @@ Evdev capture
-------------
Advanced users may list Linux evdev devices in ``input:evdev``. They become
active whenever Looking Glass grabs the keyboard, including capture mode and
automatic keyboard capture. The client user must be allowed to read those
devices. Keep ``input:evdevExclusive=yes`` unless duplicate input from the
window system is specifically required.
active when Looking Glass captures their keyboard or relative-pointer role.
Automatic capture takes keyboard-only devices without taking mouse input from
the window system, while full capture takes both roles. Guest keyboard lock
LEDs are mirrored while a writable keyboard is captured. The client user must
be allowed to read the configured devices and write them for LED mirroring.
Keep ``input:evdevExclusive=yes`` unless input from other window-system
devices is also required.