keyboard & mouse forwarding

This commit is contained in:
galister
2025-11-11 21:12:01 +01:00
committed by Patrick Nicolas
parent efde4bf247
commit 2063cae3ae
22 changed files with 924 additions and 2 deletions
+427
View File
@@ -0,0 +1,427 @@
#include "hid.h"
#include "scene.h"
#include <spdlog/spdlog.h>
#define NUM_KEYCODES 226
/// Maps Android KeyCodes to Linux key event codes
static const int KEYCODE_TO_VK[NUM_KEYCODES] = {
0, // KEYCODE_UNKNOWN
0, // KEYCODE_SOFT_LEFT
0, // KEYCODE_SOFT_RIGHT
0, // KEYCODE_HOME
0, // KEYCODE_BACK
0, // KEYCODE_CALL
0, // KEYCODE_ENDCALL
KEY_0, // KEYCODE_0
KEY_1, // KEYCODE_1
KEY_2, // KEYCODE_2
KEY_3, // KEYCODE_3
KEY_4, // KEYCODE_4
KEY_5, // KEYCODE_5
KEY_6, // KEYCODE_6
KEY_7, // KEYCODE_7
KEY_8, // KEYCODE_8
KEY_9, // KEYCODE_9
0, // KEYCODE_STAR
0, // KEYCODE_POUND
KEY_UP, // KEYCODE_DPAD_UP
KEY_DOWN, // KEYCODE_DPAD_DOWN
KEY_LEFT, // KEYCODE_DPAD_LEFT
KEY_RIGHT, // KEYCODE_DPAD_RIGHT
0, // KEYCODE_DPAD_CENTER
// there's no way to discern between BT keyboard & device buttons using NDK,
// so having these mapped consumes all events from the HMD volume rocker.
// alternatively, move the handling to stream.cpp
0, // KEYCODE_VOLUME_UP // KEY_VOLUMEUP
0, // KEYCODE_VOLUME_DOWN // KEY_VOLUMEDOWN
0, // KEYCODE_POWER - device power button, do not consume
0, // KEYCODE_CAMERA
0, // KEYCODE_CLEAR
KEY_A, // KEYCODE_A
KEY_B, // KEYCODE_B
KEY_C, // KEYCODE_C
KEY_D, // KEYCODE_D
KEY_E, // KEYCODE_E
KEY_F, // KEYCODE_F
KEY_G, // KEYCODE_G
KEY_H, // KEYCODE_H
KEY_I, // KEYCODE_I
KEY_J, // KEYCODE_J
KEY_K, // KEYCODE_K
KEY_L, // KEYCODE_L
KEY_M, // KEYCODE_M
KEY_N, // KEYCODE_N
KEY_O, // KEYCODE_O
KEY_P, // KEYCODE_P
KEY_Q, // KEYCODE_Q
KEY_R, // KEYCODE_R
KEY_S, // KEYCODE_S
KEY_T, // KEYCODE_T
KEY_U, // KEYCODE_U
KEY_V, // KEYCODE_V
KEY_W, // KEYCODE_W
KEY_X, // KEYCODE_X
KEY_Y, // KEYCODE_Y
KEY_Z, // KEYCODE_Z
KEY_COMMA, // KEYCODE_COMMA
KEY_DOT, // KEYCODE_PERIOD
KEY_LEFTALT, // KEYCODE_ALT_LEFT
KEY_RIGHTALT, // KEYCODE_ALT_RIGHT
KEY_LEFTSHIFT, // KEYCODE_SHIFT_LEFT
KEY_RIGHTSHIFT, // KEYCODE_SHIFT_RIGHT
KEY_TAB, // KEYCODE_TAB
KEY_SPACE, // KEYCODE_SPACE
0, // KEYCODE_SYM
KEY_WWW, // KEYCODE_EXPLORER
KEY_MAIL, // KEYCODE_ENVELOPE
KEY_ENTER, // KEYCODE_ENTER
KEY_BACKSPACE, // KEYCODE_DEL
KEY_GRAVE, // KEYCODE_GRAVE
KEY_MINUS, // KEYCODE_MINUS
KEY_EQUAL, // KEYCODE_EQUALS
KEY_LEFTBRACE, // KEYCODE_LEFT_BRACKET
KEY_RIGHTBRACE, // KEYCODE_RIGHT_BRACKET
KEY_BACKSLASH, // KEYCODE_BACKSLASH
KEY_SEMICOLON, // KEYCODE_SEMICOLON
KEY_APOSTROPHE, // KEYCODE_APOSTROPHE
KEY_SLASH, // KEYCODE_SLASH
0, // KEYCODE_AT
0, // KEYCODE_NUM
0, // KEYCODE_HEADSETHOOK
0, // KEYCODE_FOCUS
KEY_EQUAL, // KEYCODE_PLUS
KEY_MENU, // KEYCODE_MENU
0, // KEYCODE_NOTIFICATION
KEY_SEARCH, // KEYCODE_SEARCH
KEY_PLAYPAUSE, // KEYCODE_MEDIA_PLAY_PAUSE
KEY_STOPCD, // KEYCODE_MEDIA_STOP
KEY_NEXTSONG, // KEYCODE_MEDIA_NEXT
KEY_PREVIOUSSONG, // KEYCODE_MEDIA_PREVIOUS
KEY_REWIND, // KEYCODE_MEDIA_REWIND
KEY_FASTFORWARD, // KEYCODE_MEDIA_FAST_FORWARD
KEY_MUTE, // KEYCODE_MUTE
KEY_PAGEUP, // KEYCODE_PAGE_UP
KEY_PAGEDOWN, // KEYCODE_PAGE_DOWN
0, // KEYCODE_PICTSYMBOLS
0, // KEYCODE_SWITCH_CHARSET
0, // KEYCODE_BUTTON_A
0, // KEYCODE_BUTTON_B
0, // KEYCODE_BUTTON_C
0, // KEYCODE_BUTTON_X
0, // KEYCODE_BUTTON_Y
0, // KEYCODE_BUTTON_Z
0, // KEYCODE_BUTTON_L1
0, // KEYCODE_BUTTON_R1
0, // KEYCODE_BUTTON_L2
0, // KEYCODE_BUTTON_R2
0, // KEYCODE_BUTTON_THUMBL
0, // KEYCODE_BUTTON_THUMBR
0, // KEYCODE_BUTTON_START
0, // KEYCODE_BUTTON_SELECT
0, // KEYCODE_BUTTON_MODE
KEY_ESC, // KEYCODE_ESCAPE
KEY_DELETE, // KEYCODE_FORWARD_DEL
KEY_LEFTCTRL, // KEYCODE_CTRL_LEFT
KEY_RIGHTCTRL, // KEYCODE_CTRL_RIGHT
KEY_CAPSLOCK, // KEYCODE_CAPS_LOCK
KEY_SCROLLLOCK, // KEYCODE_SCROLL_LOCK
KEY_LEFTMETA, // KEYCODE_META_LEFT
KEY_RIGHTMETA, // KEYCODE_META_RIGHT
0, // KEYCODE_FUNCTION
KEY_SYSRQ, // KEYCODE_SYSRQ
KEY_PAUSE, // KEYCODE_BREAK
KEY_HOME, // KEYCODE_MOVE_HOME
KEY_END, // KEYCODE_MOVE_END
KEY_INSERT, // KEYCODE_INSERT
KEY_FORWARD, // KEYCODE_FORWARD
KEY_PLAYCD, // KEYCODE_MEDIA_PLAY
KEY_PAUSECD, // KEYCODE_MEDIA_PAUSE
KEY_CLOSECD, // KEYCODE_MEDIA_CLOSE
KEY_EJECTCD, // KEYCODE_MEDIA_EJECT
KEY_RECORD, // KEYCODE_MEDIA_RECORD
KEY_F1, // KEYCODE_F1
KEY_F2, // KEYCODE_F2
KEY_F3, // KEYCODE_F3
KEY_F4, // KEYCODE_F4
KEY_F5, // KEYCODE_F5
KEY_F6, // KEYCODE_F6
KEY_F7, // KEYCODE_F7
KEY_F8, // KEYCODE_F8
KEY_F9, // KEYCODE_F9
KEY_F10, // KEYCODE_F10
KEY_F11, // KEYCODE_F11
KEY_F12, // KEYCODE_F12
KEY_NUMLOCK, // KEYCODE_NUM_LOCK
KEY_KP0, // KEYCODE_NUMPAD_0
KEY_KP1, // KEYCODE_NUMPAD_1
KEY_KP2, // KEYCODE_NUMPAD_2
KEY_KP3, // KEYCODE_NUMPAD_3
KEY_KP4, // KEYCODE_NUMPAD_4
KEY_KP5, // KEYCODE_NUMPAD_5
KEY_KP6, // KEYCODE_NUMPAD_6
KEY_KP7, // KEYCODE_NUMPAD_7
KEY_KP8, // KEYCODE_NUMPAD_8
KEY_KP9, // KEYCODE_NUMPAD_9
KEY_KPSLASH, // KEYCODE_NUMPAD_DIVIDE
KEY_KPASTERISK, // KEYCODE_NUMPAD_MULTIPLY
KEY_KPMINUS, // KEYCODE_NUMPAD_SUBTRACT
KEY_KPPLUS, // KEYCODE_NUMPAD_ADD
KEY_KPDOT, // KEYCODE_NUMPAD_DOT
KEY_KPCOMMA, // KEYCODE_NUMPAD_COMMA
KEY_KPENTER, // KEYCODE_NUMPAD_ENTER
KEY_KPEQUAL, // KEYCODE_NUMPAD_EQUALS
KEY_KPLEFTPAREN, // KEYCODE_NUMPAD_LEFT_PAREN
KEY_KPRIGHTPAREN, // KEYCODE_NUMPAD_RIGHT_PAREN
KEY_MUTE, // KEYCODE_VOLUME_MUTE
0, // KEYCODE_INFO
0, // KEYCODE_CHANNEL_UP
0, // KEYCODE_CHANNEL_DOWN
0, // KEYCODE_ZOOM_IN
0, // KEYCODE_ZOOM_OUT
0, // KEYCODE_TV
0, // KEYCODE_WINDOW
0, // KEYCODE_GUIDE
0, // KEYCODE_DVR
KEY_BOOKMARKS, // KEYCODE_BOOKMARK
0, // KEYCODE_CAPTIONS
0, // KEYCODE_SETTINGS
0, // KEYCODE_TV_POWER
0, // KEYCODE_TV_INPUT
0, // KEYCODE_STB_POWER
0, // KEYCODE_STB_INPUT
0, // KEYCODE_AVR_POWER
0, // KEYCODE_AVR_INPUT
0, // KEYCODE_PROG_RED
0, // KEYCODE_PROG_GREEN
0, // KEYCODE_PROG_YELLOW
0, // KEYCODE_PROG_BLUE
0, // KEYCODE_APP_SWITCH
0, // KEYCODE_BUTTON_1
0, // KEYCODE_BUTTON_2
0, // KEYCODE_BUTTON_3
0, // KEYCODE_BUTTON_4
0, // KEYCODE_BUTTON_5
0, // KEYCODE_BUTTON_6
0, // KEYCODE_BUTTON_7
0, // KEYCODE_BUTTON_8
0, // KEYCODE_BUTTON_9
0, // KEYCODE_BUTTON_10
0, // KEYCODE_BUTTON_11
0, // KEYCODE_BUTTON_12
0, // KEYCODE_BUTTON_13
0, // KEYCODE_BUTTON_14
0, // KEYCODE_BUTTON_15
0, // KEYCODE_BUTTON_16
KEY_LANGUAGE, // KEYCODE_LANGUAGE_SWITCH
0, // KEYCODE_MANNER_MODE
0, // KEYCODE_3D_MODE
0, // KEYCODE_CONTACTS
KEY_CALENDAR, // KEYCODE_CALENDAR
0, // KEYCODE_MUSIC
KEY_CALC, // KEYCODE_CALCULATOR
KEY_ZENKAKUHANKAKU, // KEYCODE_ZENKAKU_HANKAKU
0, // KEYCODE_EISU
KEY_MUHENKAN, // KEYCODE_MUHENKAN
KEY_HENKAN, // KEYCODE_HENKAN
KEY_KATAKANAHIRAGANA, // KEYCODE_KATAKANA_HIRAGANA
KEY_YEN, // KEYCODE_YEN
KEY_RO, // KEYCODE_RO
KEY_KATAKANAHIRAGANA, // KEYCODE_KANA
KEY_ASSISTANT, // KEYCODE_ASSIST
KEY_BRIGHTNESSDOWN, // KEYCODE_BRIGHTNESS_DOWN
KEY_BRIGHTNESSUP, // KEYCODE_BRIGHTNESS_UP
0, // KEYCODE_MEDIA_AUDIO_TRACK
KEY_SLEEP, // KEYCODE_SLEEP
KEY_WAKEUP, // KEYCODE_WAKEUP
};
uint8_t key_code_to_vk(int16_t key_code)
{
if (key_code < 0 || key_code >= NUM_KEYCODES)
return 0;
return KEYCODE_TO_VK[key_code];
}
bool android_hid::input_handler::handle_input(scene * current_scene, AInputEvent * event)
{
const int32_t type = AInputEvent_getType(event);
const int32_t src = AInputEvent_getSource(event);
const int32_t dev = AInputEvent_getDeviceId(event);
if (type == AINPUT_EVENT_TYPE_KEY)
{
int32_t flags = AKeyEvent_getFlags(event);
if ((flags & AKEY_EVENT_FLAG_SOFT_KEYBOARD) != 0)
return false; // ignore soft keyboards
if ((src & AINPUT_SOURCE_KEYBOARD) == 0)
return false; // event is not from keyboard
int32_t keycode = AKeyEvent_getKeyCode(event);
int32_t action = AKeyEvent_getAction(event);
bool pressed;
switch (action)
{
case AKEY_EVENT_ACTION_DOWN:
pressed = true;
break;
case AKEY_EVENT_ACTION_UP:
pressed = false;
break;
default:
return false;
}
uint8_t vk = key_code_to_vk(keycode);
if (vk == 0)
return false;
if (pressed)
return current_scene->on_input_key_down(vk);
else
return current_scene->on_input_key_up(vk);
}
if (type == AINPUT_EVENT_TYPE_MOTION)
{
// don't care about absolute mouse due to mapping difficulties
if ((src & AINPUT_SOURCE_MOUSE_RELATIVE) == 0)
return false;
int32_t actionMasked = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
switch (actionMasked)
{
case AMOTION_EVENT_ACTION_HOVER_MOVE: // mouse move with no buttons pressed
case AMOTION_EVENT_ACTION_MOVE: { // mouse move with buttons pressed
float x = AMotionEvent_getX(event, 0);
float y = AMotionEvent_getY(event, 0);
return current_scene->on_input_mouse_move(x, y);
}
case AMOTION_EVENT_ACTION_BUTTON_PRESS:
case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
uint32_t buttons = (uint32_t)AMotionEvent_getButtonState(event);
auto button_diff = buttons ^ buttons_before;
bool ret_val = false;
for (uint8_t i = 0; i < 3; ++i)
{
auto i_shifted = (1u << i);
if ((button_diff & i_shifted) != 0)
{
if ((buttons & i_shifted) != 0)
ret_val = current_scene->on_input_button_down(i);
else
ret_val = current_scene->on_input_button_up(i);
}
}
buttons_before = buttons;
return ret_val;
}
case AMOTION_EVENT_ACTION_SCROLL: {
float h = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_HSCROLL, 0);
float v = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_VSCROLL, 0);
return current_scene->on_input_scroll(h, v);
}
default:
return false;
}
}
return false;
}
bool try_clear_exception(JNIEnv * env, const char * where)
{
if (env->ExceptionCheck())
{
spdlog::error("[pointer_capture] Exception at {}", where);
env->ExceptionDescribe(); // logs to logcat
env->ExceptionClear();
return true;
}
return false;
}
void make_decor_view_call(ANativeActivity * activity, const char * method_name)
{
if (!activity || !activity->vm || !activity->env || !activity->clazz)
{
spdlog::error("[pointer_capture] Invalid ANativeActivity* or missing VM/Activity");
return;
}
JNIEnv * env = activity->env;
jobject act = activity->clazz;
jclass clsActivity = env->GetObjectClass(act);
jclass clsWindow = 0;
jobject window = 0;
jclass clsView = 0;
jobject decor = 0;
do
{
if (try_clear_exception(env, "GetObjectClass(Activity)") || not clsActivity)
break; // goto cleanup
jmethodID midGetWindow = env->GetMethodID(clsActivity, "getWindow", "()Landroid/view/Window;");
if (try_clear_exception(env, "Activity.getWindow() method lookup") || not midGetWindow)
break; // goto cleanup
window = env->CallObjectMethod(act, midGetWindow);
if (try_clear_exception(env, "Activity.getWindow() call") || not window)
break; // goto cleanup
clsWindow = env->GetObjectClass(window);
if (try_clear_exception(env, "GetObjectClass(Window)") || not clsWindow)
break; // goto cleanup
jmethodID midGetDecor = env->GetMethodID(clsWindow, "getDecorView", "()Landroid/view/View;");
if (try_clear_exception(env, "Window.getDecorView() method lookup") || not midGetDecor)
break; // goto cleanup
decor = env->CallObjectMethod(window, midGetDecor);
if (try_clear_exception(env, "Window.getDecorView() call") || not decor)
break; // goto cleanup
clsView = env->GetObjectClass(decor);
if (try_clear_exception(env, "GetObjectClass(DecorView)") || not clsView)
break; // goto cleanup
jmethodID midAction = env->GetMethodID(clsView, method_name, "()V");
if (try_clear_exception(env, "View method lookup") || not midAction)
break; // goto cleanup
env->CallVoidMethod(decor, midAction);
if (try_clear_exception(env, "View method call"))
break; // goto cleanup
spdlog::info("[pointer_capture] {}() invoked successfully", method_name);
} while (false);
// cleanup
if (clsView)
env->DeleteLocalRef(clsView);
if (decor)
env->DeleteLocalRef(decor);
if (clsWindow)
env->DeleteLocalRef(clsWindow);
if (window)
env->DeleteLocalRef(window);
if (clsActivity)
env->DeleteLocalRef(clsActivity);
}
void android_hid::request_pointer_capture(ANativeActivity * activity)
{
make_decor_view_call(activity, "requestPointerCapture");
}
void android_hid::release_pointer_capture(ANativeActivity * activity)
{
make_decor_view_call(activity, "releasePointerCapture");
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include "scene.h"
#include <android_native_app_glue.h>
#include <cstdint>
#include <linux/input-event-codes.h>
#include <mutex>
#include <vector>
namespace android_hid
{
void request_pointer_capture(ANativeActivity * activity);
void release_pointer_capture(ANativeActivity * activity);
class input_handler
{
uint32_t buttons_before = 0;
public:
bool handle_input(scene * current_scene, AInputEvent * event);
};
} // namespace android_hid
+20
View File
@@ -1504,6 +1504,26 @@ application::application(application_info info) :
}
};
// capture pointer to receive relative mouse events
app_info.native_app->activity->callbacks->onWindowFocusChanged = [](ANativeActivity * activity, int has_focus) {
if (has_focus)
android_hid::request_pointer_capture(activity);
else
android_hid::release_pointer_capture(activity);
};
app_info.native_app->onInputEvent = [](android_app * app, AInputEvent * event) {
auto app_instance = static_cast<application *>(app->userData);
std::unique_lock _{app_instance->scene_stack_lock};
if (!app_instance->scene_stack.empty())
{
auto scene = app_instance->scene_stack.back();
return app_instance->input_handler.handle_input(scene.get(), event) ? 1 : 0;
}
return 0;
};
wifi = wifi_lock::make_wifi_lock(app_info.native_app->activity->clazz);
// Initialize the loader for this platform
+3
View File
@@ -20,6 +20,7 @@
#pragma once
#ifdef __ANDROID__
#include "android/hid.h"
#include <android_native_app_glue.h>
#endif
@@ -81,6 +82,8 @@ class application : public singleton<application>
#ifdef __ANDROID__
ANativeWindow * native_window = nullptr;
bool resumed = false;
android_hid::input_handler input_handler{};
#endif
static inline const char engine_name[] = "No engine";
+26
View File
@@ -18,6 +18,7 @@
*/
#include "scene.h"
#include "application.h"
#include "render/scene_components.h"
#include "utils/contains.h"
@@ -611,3 +612,28 @@ void scene::remove(entt::entity entity)
void scene::on_unfocused() {}
void scene::on_focused() {}
void scene::on_xr_event(const xr::event &) {}
bool scene::on_input_key_down(uint8_t key_code)
{
return false;
}
bool scene::on_input_key_up(uint8_t key_code)
{
return false;
}
bool scene::on_input_mouse_move(float x, float y)
{
return false;
}
bool scene::on_input_button_down(uint8_t button)
{
return false;
}
bool scene::on_input_button_up(uint8_t button)
{
return false;
}
bool scene::on_input_scroll(float h, float v)
{
return false;
}
+7
View File
@@ -213,6 +213,13 @@ public:
virtual void render(const XrFrameState &) = 0;
virtual void on_xr_event(const xr::event &);
virtual bool on_input_key_down(uint8_t key_code);
virtual bool on_input_key_up(uint8_t key_code);
virtual bool on_input_mouse_move(float x, float y);
virtual bool on_input_button_down(uint8_t button);
virtual bool on_input_button_up(uint8_t button);
virtual bool on_input_scroll(float h, float v);
entt::registry world;
std::shared_ptr<entt::registry> load_gltf(const std::filesystem::path & path, std::function<void(float)> progress_cb = {});
+33
View File
@@ -1302,3 +1302,36 @@ void scenes::stream::on_xr_event(const xr::event & event)
break;
}
}
bool scenes::stream::forward_hid_input(from_headset::hid::input_t packet)
{
if (not hid_forwarding)
return false;
network_session->send_control(from_headset::hid::input{packet});
return true;
}
bool scenes::stream::on_input_key_down(uint8_t key_code)
{
return forward_hid_input(from_headset::hid::key_down{key_code});
}
bool scenes::stream::on_input_key_up(uint8_t key_code)
{
return forward_hid_input(from_headset::hid::key_up{key_code});
}
bool scenes::stream::on_input_mouse_move(float x, float y)
{
return forward_hid_input(from_headset::hid::mouse_move{x, y});
}
bool scenes::stream::on_input_button_down(uint8_t button)
{
return forward_hid_input(from_headset::hid::button_down{button});
}
bool scenes::stream::on_input_button_up(uint8_t button)
{
return forward_hid_input(from_headset::hid::button_up{button});
}
bool scenes::stream::on_input_scroll(float h, float v)
{
return forward_hid_input(from_headset::hid::mouse_scroll{h, v});
}
+10
View File
@@ -81,6 +81,7 @@ private:
std::array<std::atomic<interaction_profile>, 2> interaction_profiles; // left and right hand
std::atomic<bool> interaction_profile_changed = false;
std::atomic<bool> recenter_requested = false;
std::atomic<bool> hid_forwarding = false;
std::atomic<XrDuration> display_time_phase = 0;
std::atomic<XrDuration> display_time_period = 0;
XrTime last_display_time = 0;
@@ -173,6 +174,8 @@ private:
stream(std::string server_name, scene & parent_scene);
bool forward_hid_input(from_headset::hid::input_t);
public:
~stream();
@@ -187,6 +190,13 @@ public:
void on_unfocused() override;
void on_xr_event(const xr::event &) override;
bool on_input_key_down(uint8_t key_code) override;
bool on_input_key_up(uint8_t key_code) override;
bool on_input_mouse_move(float x, float y) override;
bool on_input_button_down(uint8_t button) override;
bool on_input_button_up(uint8_t button) override;
bool on_input_scroll(float h, float v) override;
void operator()(to_headset::crypto_handshake &&) {};
void operator()(to_headset::pin_check_2 &&) {};
void operator()(to_headset::pin_check_4 &&) {};
+4 -1
View File
@@ -556,7 +556,7 @@ void scenes::stream::tracking()
e.code().value() != XR_ERROR_TIME_INVALID)
throw;
}
}
} // end prediction loop
XrDuration busy_time = t.count();
// Target: polling between 1 and 5ms, with 20% busy time
@@ -658,6 +658,9 @@ void scenes::stream::operator()(to_headset::tracking_control && packet)
if (audio_handle)
audio_handle->set_mic_state(packet.enabled[m]);
auto n = size_t(wivrn::to_headset::tracking_control::id::hid_input);
hid_forwarding = packet.enabled[n];
*locked = packet;
locked->min_offset = std::min(locked->min_offset, locked->max_offset);
}
+44
View File
@@ -391,6 +391,47 @@ struct inputs
std::vector<input_value> values;
};
struct hid
{
struct button_down
{
uint8_t button;
};
struct button_up
{
uint8_t button;
};
struct mouse_move
{
float x;
float y;
};
struct mouse_scroll
{
float h;
float v;
};
struct key_down
{
uint8_t key;
};
struct key_up
{
uint8_t key;
};
using input_t = std::variant<button_down, button_up, mouse_move, mouse_scroll, key_down, key_up>;
struct input
{
input_t input_data;
};
};
struct timesync_response
{
XrTime query;
@@ -472,6 +513,7 @@ struct stop_application
uint32_t id;
};
// when changing this, also make sure there are handlers in wivrn_session, etc. or compilation will fail
using packets = std::variant<
crypto_handshake,
pin_check_1,
@@ -497,6 +539,7 @@ using packets = std::variant<
start_app,
get_running_applications,
set_active_application,
hid::input,
stop_application>;
} // namespace from_headset
@@ -671,6 +714,7 @@ struct tracking_control
right_hand,
face,
generic_tracker,
hid_input,
battery,
microphone,
+5
View File
@@ -86,6 +86,11 @@ public:
{
return fd != -1;
}
operator int() const
{
return fd;
}
};
class UDP : public fd_base
+12
View File
@@ -230,6 +230,16 @@ Kirigami.ScrollablePage {
id: show_system_checks
text: i18n("Check system configuration on start")
}
RowLayout {
visible: config.hid_forwarding_supported
Controls.CheckBox {
id: hid_forwarding
text: i18n("Forward keyboard & mouse from headset")
}
Kirigami.ContextualHelpButton {
toolTipText: i18n("Keyboard and mouse connected to the client will act as if connected to the server. Client OS may reserve specific keys and combinations, which cannot be forwarded.")
}
}
Controls.CheckBox {
id: debug_gui
text: i18n("Enable debug window")
@@ -389,6 +399,7 @@ Kirigami.ScrollablePage {
config.debugGui = debug_gui.checked;
config.steamVrLh = steamvr_lh.checked;
config.hidForwarding = hid_forwarding.checked;
}
function load() {
@@ -400,6 +411,7 @@ Kirigami.ScrollablePage {
debug_gui.checked = config.debugGui;
steamvr_lh.checked = config.steamVrLh;
hid_forwarding.checked = config.hidForwarding;
openvr_combobox.load()
+40
View File
@@ -25,6 +25,7 @@
#include <QList>
#include <QObject>
#include <cassert>
#include <fcntl.h>
#include <nlohmann/json.hpp>
#include <qqmlintegration.h>
@@ -120,6 +121,7 @@ void Settings::emitAllChanged()
openvrChanged();
debugGuiChanged();
steamVrLhChanged();
hidForwardingChanged();
}
void Settings::load(const wivrn_server * server)
@@ -384,6 +386,22 @@ void Settings::set_application(const QString & value)
applicationChanged();
}
bool Settings::hidForwarding() const
{
auto it = m_jsonSettings.find("hid-forwarding");
if (it != m_jsonSettings.end() and it->is_boolean())
return *it;
return false;
}
void Settings::set_hidForwarding(const bool & value)
{
auto old = hidForwarding();
m_jsonSettings["hid-forwarding"] = value;
if (old != value)
hidForwardingChanged();
}
bool Settings::debugGui() const
{
// Advanced options (debug window, steamvr_lh)
@@ -504,6 +522,7 @@ void Settings::restore_defaults()
m_jsonSettings.erase("scale");
m_jsonSettings.erase("bitrate");
m_jsonSettings.erase("application");
m_jsonSettings.erase("hid-forwarding");
m_jsonSettings.erase("debug-gui");
m_jsonSettings.erase("use-steamvr-lh");
m_jsonSettings.erase("tcp-only");
@@ -532,5 +551,26 @@ bool Settings::steamvr_lh() const
return false;
#endif
}
bool Settings::hid_forwarding() const
{
// only called from the UI thread → no locking needed
static std::optional<bool> can_open_uinput;
if (can_open_uinput.has_value())
return can_open_uinput.value();
can_open_uinput = false;
constexpr std::array paths = {"/dev/uinput", "/dev/input/uinput"};
for (const char * p: paths)
{
int fd = ::open(p, O_WRONLY | O_NONBLOCK);
if (fd >= 0)
{
::close(fd);
can_open_uinput = true;
break;
}
}
return can_open_uinput.value();
}
#include "moc_settings.cpp"
+4
View File
@@ -72,10 +72,12 @@ public:
Q_PROPERTY(QString application READ application WRITE set_application NOTIFY applicationChanged)
Q_PROPERTY(QString openvr READ openvr WRITE set_openvr NOTIFY openvrChanged)
Q_PROPERTY(bool hidForwarding READ hidForwarding WRITE set_hidForwarding NOTIFY hidForwardingChanged)
Q_PROPERTY(bool debugGui READ debugGui WRITE set_debugGui NOTIFY debugGuiChanged)
Q_PROPERTY(bool steamVrLh READ steamVrLh WRITE set_steamVrLh NOTIFY steamVrLhChanged)
Q_PROPERTY(bool flatpak READ flatpak CONSTANT)
Q_PROPERTY(bool hid_forwarding_supported READ hid_forwarding CONSTANT)
Q_PROPERTY(bool debug_gui_supported READ debug_gui CONSTANT)
Q_PROPERTY(bool steamvr_lh_supported READ steamvr_lh CONSTANT)
@@ -86,6 +88,7 @@ public:
SETTER_GETTER_NOTIFY(int, bitrate)
SETTER_GETTER_NOTIFY(float, scale)
SETTER_GETTER_NOTIFY(QString, application)
SETTER_GETTER_NOTIFY(bool, hidForwarding)
SETTER_GETTER_NOTIFY(bool, debugGui)
SETTER_GETTER_NOTIFY(bool, steamVrLh)
SETTER_GETTER_NOTIFY(bool, tcpOnly)
@@ -108,6 +111,7 @@ public:
bool flatpak() const;
bool debug_gui() const;
bool steamvr_lh() const;
bool hid_forwarding() const;
static encoder_name encoder_id_from_string(std::string_view s);
static video_codec codec_id_from_string(std::string_view s);
+7
View File
@@ -212,6 +212,13 @@ If unset, WiVRn will autodetect the path of such a tool as usual (see [the Steam
If set to an null, WiVRn will not manage the OpenVR configuration.
## `hid-forwarding`
Default value: `false`
Only available when the `uinput` kernel module is loaded and the user has write access.
Enables the forwarding of keyboard and mouse input from the client to the server.
## `debug-gui`
Default value: `false`
+1
View File
@@ -104,6 +104,7 @@ if (WIVRN_BUILD_SERVER)
driver/wivrn_htc_face_tracker.cpp
driver/wivrn_generic_tracker.cpp
driver/wivrn_foveation.cpp
driver/wivrn_uinput.cpp
driver/pose_list.cpp
driver/view_list.cpp
driver/hand_joints_list.cpp
+3
View File
@@ -195,6 +195,9 @@ configuration::configuration()
}
}
if (auto it = json.find("hid-forwarding"); it != json.end())
hid_forwarding = *it;
if (auto it = json.find("debug-gui"); it != json.end())
debug_gui = *it;
+1
View File
@@ -63,6 +63,7 @@ struct configuration
std::vector<std::string> application;
bool debug_gui = false;
bool use_steamvr_lh = false;
bool hid_forwarding = false;
bool tcp_only = false;
service_publication publication = service_publication::avahi;
+37 -1
View File
@@ -21,6 +21,7 @@
#include "accept_connection.h"
#include "application.h"
#include "configuration.h"
#include "driver/app_pacer.h"
#include "main/comp_compositor.h"
#include "main/comp_main_interface.h"
@@ -54,7 +55,6 @@
#include <vulkan/vulkan.h>
#if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE
#include "configuration.h"
#include "steamvr_lh_interface.h"
#endif
@@ -293,6 +293,24 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
system_name += " on WiVRn";
strlcpy(xrt_system.base.properties.name, system_name.c_str(), std::size(xrt_system.base.properties.name));
}
if (configuration().hid_forwarding)
{
try
{
uinput_handler.emplace();
tracking_control.set_enabled(to_headset::tracking_control::id::hid_input, true);
}
catch (...)
{
U_LOG_W("Could not initialize keyboard & mouse forwarding");
U_LOG_W("Ensure that the uinput kernel module is loaded and your user is in the input group.");
wivrn_ipc_socket_monado->send(from_monado::server_error{
.where = "Could not initialize keyboard & mouse forwarding",
.message = "Ensure that the uinput kernel module is loaded and your user is in the input group.",
});
}
}
}
wivrn_session::~wivrn_session()
@@ -582,6 +600,24 @@ void wivrn_session::operator()(from_headset::inputs && inputs)
right_controller.set_inputs(inputs, offset);
}
void wivrn_session::operator()(from_headset::hid::input && e)
{
try
{
if (uinput_handler)
uinput_handler->handle_input(e);
}
catch (const std::exception & e)
{
wivrn_ipc_socket_monado->send(from_monado::server_error{
.where = "HID forwarding error",
.message = e.what(),
});
U_LOG_E("HID forwarding error: %s", e.what());
uinput_handler.reset();
}
}
void wivrn_session::operator()(from_headset::timesync_response && timesync)
{
offset_est.add_sample(timesync);
+3
View File
@@ -28,6 +28,7 @@
#include "wivrn_hmd.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include "wivrn_uinput.h"
#include "xrt/xrt_results.h"
#include "xrt/xrt_system.h"
#include <atomic>
@@ -125,6 +126,7 @@ class wivrn_session : public xrt_system_devices
std::unique_ptr<wivrn_fb_face2_tracker> fb_face2_tracker;
std::unique_ptr<wivrn_htc_face_tracker> htc_face_tracker;
std::vector<std::unique_ptr<wivrn_generic_tracker>> generic_trackers;
std::optional<wivrn_uinput> uinput_handler;
std::shared_mutex comp_target_mutex;
wivrn_comp_target * comp_target;
@@ -189,6 +191,7 @@ public:
void operator()(from_headset::hand_tracking &&);
void operator()(from_headset::body_tracking &&);
void operator()(from_headset::inputs &&);
void operator()(from_headset::hid::input && e);
void operator()(from_headset::timesync_response &&);
void operator()(from_headset::feedback &&);
void operator()(from_headset::battery &&);
+189
View File
@@ -0,0 +1,189 @@
#include "wivrn_uinput.h"
#include "utils/overloaded.h"
#include <array>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <system_error>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include <linux/uinput.h>
namespace
{
wivrn::fd_base open_uinput_or_throw()
{
constexpr std::array paths = {"/dev/uinput", "/dev/input/uinput"};
for (const char * p: paths)
{
int fd = ::open(p, O_WRONLY | O_NONBLOCK);
if (fd >= 0)
return fd;
if (errno != ENOENT) // exists but inaccessbile
throw std::system_error(errno, std::generic_category(), "error while opening uinput");
}
throw std::runtime_error("no uinput device found");
}
void write_or_throw(int fd, const void * buf, size_t n)
{
auto res = ::write(fd, buf, n);
if (res < 0)
throw std::system_error(errno, std::generic_category(), "error during write");
if (res != n)
throw std::runtime_error("byte count mismatch while writing to uinput");
}
void fill_ids(uinput_user_dev & uidev, uint16_t product) noexcept
{
std::memset(&uidev, 0, sizeof(uidev));
uidev.id.bustype = 0x03;
uidev.id.vendor = 0x4711;
uidev.id.product = product;
uidev.id.version = 5;
}
void emit_ev(int fd, uint16_t type, uint16_t code, int32_t value)
{
struct input_event ev;
std::memset(&ev, 0, sizeof(ev));
gettimeofday(&ev.time, nullptr);
ev.type = type;
ev.code = code;
ev.value = value;
write_or_throw(fd, &ev, sizeof(ev));
}
void syn(int fd)
{
emit_ev(fd, EV_SYN, SYN_REPORT, 0);
}
void ioctl_or_throw(int fd, unsigned long op)
{
auto n = ioctl(fd, op);
if (n < 0)
throw std::system_error(errno, std::generic_category(), "ioctl error");
}
void ioctl_or_throw(int fd, unsigned long op, unsigned long int arg)
{
auto n = ioctl(fd, op, arg);
if (n < 0)
throw std::system_error(errno, std::generic_category(), "ioctl error");
}
} // namespace
wivrn_uinput::wivrn_uinput()
{
init_keyboard();
init_mouse();
};
void wivrn_uinput::handle_input(wivrn::from_headset::hid::input & e)
{
std::visit(utils::overloaded{
[this](const wivrn::from_headset::hid::key_down & e) {
send_key(e.key, true);
},
[this](const wivrn::from_headset::hid::key_up & e) {
send_key(e.key, false);
},
[this](const wivrn::from_headset::hid::button_down & e) {
send_button(e.button, true);
},
[this](const wivrn::from_headset::hid::button_up & e) {
send_button(e.button, false);
},
[this](const wivrn::from_headset::hid::mouse_scroll & e) {
mouse_scroll(e.v, e.h);
},
[this](const wivrn::from_headset::hid::mouse_move & e) {
mouse_move_relative(e.x, e.y);
},
},
e.input_data);
}
void wivrn_uinput::send_key(uint16_t key, bool down)
{
emit_ev(kbd_fd, EV_KEY, key, down ? 1 : 0);
syn(kbd_fd);
}
/// 0: left, 1: right, 2: middle
void wivrn_uinput::send_button(uint16_t mouse_button, bool down)
{
mouse_button += BTN_MOUSE;
emit_ev(mouse_fd, EV_KEY, mouse_button, down ? 1 : 0);
syn(mouse_fd);
}
void wivrn_uinput::mouse_move_relative(int16_t x, int16_t y)
{
if (x)
emit_ev(mouse_fd, EV_REL, REL_X, x);
if (y)
emit_ev(mouse_fd, EV_REL, REL_Y, y);
syn(mouse_fd);
}
void wivrn_uinput::mouse_scroll(int16_t vertical, int16_t horizontal)
{
if (vertical)
emit_ev(mouse_fd, EV_REL, REL_WHEEL, vertical);
if (horizontal)
emit_ev(mouse_fd, EV_REL, REL_HWHEEL, horizontal);
syn(mouse_fd);
}
void wivrn_uinput::init_keyboard()
{
kbd_fd = open_uinput_or_throw();
int fd = kbd_fd.get_fd();
ioctl_or_throw(fd, UI_SET_EVBIT, EV_KEY);
ioctl_or_throw(fd, UI_SET_EVBIT, EV_REP);
// enable all KEY_* to allow arbitrary key codes
for (int code = 1; code <= KEY_MAX; ++code)
ioctl_or_throw(fd, UI_SET_KEYBIT, code);
uinput_user_dev uidev;
fill_ids(uidev, 0x0840);
std::snprintf(uidev.name, sizeof(uidev.name), "WiVRn Keyboard");
write_or_throw(fd, &uidev, sizeof(uidev));
ioctl_or_throw(fd, UI_DEV_CREATE);
}
void wivrn_uinput::init_mouse()
{
mouse_fd = open_uinput_or_throw();
int fd = mouse_fd.get_fd();
ioctl_or_throw(fd, UI_SET_EVBIT, EV_KEY);
ioctl_or_throw(fd, UI_SET_EVBIT, EV_REL);
// buttons
ioctl_or_throw(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl_or_throw(fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl_or_throw(fd, UI_SET_KEYBIT, BTN_MIDDLE);
// relative motion + wheels
ioctl_or_throw(fd, UI_SET_RELBIT, REL_X);
ioctl_or_throw(fd, UI_SET_RELBIT, REL_Y);
ioctl_or_throw(fd, UI_SET_RELBIT, REL_WHEEL);
ioctl_or_throw(fd, UI_SET_RELBIT, REL_HWHEEL);
uinput_user_dev uidev;
fill_ids(uidev, 0x0839);
std::snprintf(uidev.name, sizeof(uidev.name), "WiVRn Mouse");
write_or_throw(fd, &uidev, sizeof(uidev));
ioctl_or_throw(fd, UI_DEV_CREATE);
}
+24
View File
@@ -0,0 +1,24 @@
#include "wivrn_packets.h"
#include "wivrn_sockets.h"
#include <cstdint>
class wivrn_uinput
{
public:
wivrn_uinput();
void handle_input(wivrn::from_headset::hid::input &);
private:
void init_keyboard();
void init_mouse();
void send_key(uint16_t key, bool down);
void send_button(uint16_t mouse_button, bool down);
void mouse_move_relative(int16_t x, int16_t y);
void mouse_scroll(int16_t vertical, int16_t horizontal);
wivrn::fd_base kbd_fd;
wivrn::fd_base mouse_fd;
};