Merge remote-tracking branch 'upstream/master'
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ buildscript {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:8.12.1'
|
||||
classpath 'com.android.tools.build:gradle:8.13.0'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 = {});
|
||||
|
||||
@@ -232,11 +232,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
|
||||
if (config.preferred_refresh_rate and (config.preferred_refresh_rate == 0 or utils::contains(info.available_refresh_rates, *config.preferred_refresh_rate)))
|
||||
{
|
||||
info.preferred_refresh_rate = *config.preferred_refresh_rate;
|
||||
if (info.preferred_refresh_rate == 0)
|
||||
info.available_refresh_rates = {
|
||||
std::ranges::lower_bound(info.available_refresh_rates, config.minimum_refresh_rate),
|
||||
info.available_refresh_rates.end(),
|
||||
};
|
||||
info.minimum_refresh_rate = config.minimum_refresh_rate.value_or(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1302,3 +1298,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});
|
||||
}
|
||||
|
||||
+11
-1
@@ -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 &&) {};
|
||||
@@ -301,7 +311,7 @@ private:
|
||||
void accumulate_metrics(XrTime predicted_display_time, const std::vector<std::shared_ptr<wivrn::shard_accumulator::blit_handle>> & blit_handles, const gpu_timestamps & timestamps);
|
||||
void gui_performance_metrics();
|
||||
void gui_compact_view();
|
||||
void gui_settings();
|
||||
void gui_settings(float predicted_display_period);
|
||||
void gui_foveation_settings(float predicted_display_period);
|
||||
void gui_applications();
|
||||
void draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period);
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "implot.h"
|
||||
#include "utils/contains.h"
|
||||
#include "utils/i18n.h"
|
||||
#include "utils/ranges.h"
|
||||
#include <IconsFontAwesome6.h>
|
||||
@@ -382,13 +383,81 @@ void scenes::stream::gui_compact_view()
|
||||
}
|
||||
}
|
||||
|
||||
static void send_settings_changed_packet(xr::session & session, wivrn_session * network, const configuration & config, float predicted_display_period)
|
||||
{
|
||||
const auto & refresh_rates = session.get_refresh_rates();
|
||||
from_headset::settings_changed packet{};
|
||||
if (config.preferred_refresh_rate and (config.preferred_refresh_rate == 0 or utils::contains(refresh_rates, *config.preferred_refresh_rate)))
|
||||
{
|
||||
packet.preferred_refresh_rate = *config.preferred_refresh_rate;
|
||||
packet.minimum_refresh_rate = config.minimum_refresh_rate.value_or(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
packet.preferred_refresh_rate = predicted_display_period;
|
||||
}
|
||||
network->send_control(std::move(packet));
|
||||
}
|
||||
|
||||
std::string openxr_post_processing_flag_name(XrCompositionLayerSettingsFlagsFB flag); // TODO declaration in a .h file
|
||||
void scenes::stream::gui_settings()
|
||||
void scenes::stream::gui_settings(float predicted_display_period)
|
||||
{
|
||||
auto & config = application::get_config();
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(20, 20));
|
||||
|
||||
if (instance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME))
|
||||
{
|
||||
const auto & refresh_rates = session.get_refresh_rates();
|
||||
if (not refresh_rates.empty())
|
||||
{
|
||||
float active_rate = config.preferred_refresh_rate.value_or(refresh_rates.back());
|
||||
if (ImGui::BeginCombo(_S("Refresh rate"), active_rate ? fmt::format("{}", active_rate).c_str() : _cS("automatic refresh rate", "Automatic")))
|
||||
{
|
||||
if (ImGui::Selectable(_cS("automatic refresh rate", "Automatic"), config.preferred_refresh_rate == 0, ImGuiSelectableFlags_SelectOnRelease))
|
||||
{
|
||||
session.set_refresh_rate(0);
|
||||
config.preferred_refresh_rate = 0;
|
||||
send_settings_changed_packet(session, network_session.get(), config, predicted_display_period);
|
||||
config.save();
|
||||
}
|
||||
if (ImGui::IsItemHovered())
|
||||
imgui_ctx->tooltip(_("Select refresh rate based on measured application performance.\nMay cause flicker when a change happens."));
|
||||
for (float rate: refresh_rates)
|
||||
{
|
||||
if (ImGui::Selectable(fmt::format("{}", rate).c_str(), rate == config.preferred_refresh_rate, ImGuiSelectableFlags_SelectOnRelease))
|
||||
{
|
||||
session.set_refresh_rate(rate);
|
||||
config.preferred_refresh_rate = rate;
|
||||
send_settings_changed_packet(session, network_session.get(), config, predicted_display_period);
|
||||
config.save();
|
||||
}
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
imgui_ctx->vibrate_on_hover();
|
||||
|
||||
if (config.preferred_refresh_rate == 0 and refresh_rates.size() > 2)
|
||||
{
|
||||
float min_rate = config.minimum_refresh_rate.value_or(refresh_rates.front());
|
||||
if (ImGui::BeginCombo(_S("Minimum refresh rate"), fmt::format("{}", min_rate).c_str()))
|
||||
{
|
||||
for (float rate: refresh_rates | std::views::take(refresh_rates.size() - 1))
|
||||
{
|
||||
if (ImGui::Selectable(fmt::format("{}", rate).c_str(), rate == config.minimum_refresh_rate, ImGuiSelectableFlags_SelectOnRelease))
|
||||
{
|
||||
config.minimum_refresh_rate = rate;
|
||||
send_settings_changed_packet(session, network_session.get(), config, predicted_display_period);
|
||||
config.save();
|
||||
}
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
imgui_ctx->vibrate_on_hover();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (application::get_openxr_post_processing_supported())
|
||||
{
|
||||
ImGui::Text("%s", _S("OpenXR post-processing"));
|
||||
@@ -845,7 +914,7 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte
|
||||
case gui_status::settings:
|
||||
ImGui::SetCursorPos({tab_width + 20, 20});
|
||||
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0));
|
||||
gui_settings();
|
||||
gui_settings(predicted_display_period * 1.e-9f);
|
||||
ImGui::EndChild();
|
||||
break;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+14
-9
@@ -46,6 +46,9 @@ private:
|
||||
control_socket_t control;
|
||||
stream_socket_t stream;
|
||||
|
||||
std::atomic<uint64_t> bytes_sent_ = 0;
|
||||
std::atomic<uint64_t> bytes_received_ = 0;
|
||||
|
||||
template <typename T>
|
||||
void handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
|
||||
|
||||
@@ -60,16 +63,16 @@ public:
|
||||
template <typename T>
|
||||
void send_control(T && packet)
|
||||
{
|
||||
control.send(std::forward<T>(packet));
|
||||
bytes_sent_ += control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
{
|
||||
if (stream)
|
||||
stream.send(std::forward<T>(packet));
|
||||
bytes_sent_ += stream.send(std::forward<T>(packet));
|
||||
else
|
||||
control.send(std::forward<T>(packet));
|
||||
bytes_sent_ += control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -81,9 +84,9 @@ public:
|
||||
fds[1].events = POLLIN;
|
||||
fds[1].fd = control.get_fd();
|
||||
|
||||
while (auto packet = stream.receive_pending())
|
||||
while (auto packet = stream.receive_pending(&bytes_received_))
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
while (auto packet = control.receive_pending())
|
||||
while (auto packet = control.receive_pending(&bytes_received_))
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
|
||||
int r = ::poll(fds, std::size(fds), timeout.count());
|
||||
@@ -98,14 +101,16 @@ public:
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
{
|
||||
auto packet = stream.receive();
|
||||
auto packet = stream.receive(&bytes_received_);
|
||||
if (packet)
|
||||
{
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
}
|
||||
}
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
auto packet = control.receive();
|
||||
auto packet = control.receive(&bytes_received_);
|
||||
if (packet)
|
||||
std::visit(std::forward<T>(visitor), std::move(*packet));
|
||||
}
|
||||
@@ -115,11 +120,11 @@ public:
|
||||
|
||||
uint64_t bytes_received() const
|
||||
{
|
||||
return control.bytes_received() + stream.bytes_received();
|
||||
return bytes_received_;
|
||||
}
|
||||
|
||||
uint64_t bytes_sent() const
|
||||
{
|
||||
return control.bytes_sent() + stream.bytes_sent();
|
||||
return bytes_sent_;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -221,6 +221,8 @@ struct headset_info_packet
|
||||
uint32_t recommended_eye_height;
|
||||
std::vector<float> available_refresh_rates;
|
||||
float preferred_refresh_rate;
|
||||
// for automatic
|
||||
float minimum_refresh_rate;
|
||||
struct audio_description
|
||||
{
|
||||
uint8_t num_channels;
|
||||
@@ -245,6 +247,13 @@ struct headset_info_packet
|
||||
std::string variant;
|
||||
};
|
||||
|
||||
struct settings_changed
|
||||
{
|
||||
float preferred_refresh_rate;
|
||||
// for automatic
|
||||
float minimum_refresh_rate;
|
||||
};
|
||||
|
||||
struct handshake
|
||||
{
|
||||
// Sending this on TCP means connection will be TCP only
|
||||
@@ -391,6 +400,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,11 +522,13 @@ 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,
|
||||
pin_check_3,
|
||||
headset_info_packet,
|
||||
settings_changed,
|
||||
feedback,
|
||||
audio_data,
|
||||
handshake,
|
||||
@@ -497,6 +549,7 @@ using packets = std::variant<
|
||||
start_app,
|
||||
get_running_applications,
|
||||
set_active_application,
|
||||
hid::input,
|
||||
stop_application>;
|
||||
} // namespace from_headset
|
||||
|
||||
@@ -671,6 +724,7 @@ struct tracking_control
|
||||
right_hand,
|
||||
face,
|
||||
generic_tracker,
|
||||
hid_input,
|
||||
battery,
|
||||
microphone,
|
||||
|
||||
|
||||
@@ -288,6 +288,11 @@ public:
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t wire_size()
|
||||
{
|
||||
return initial_buffer.size_bytes();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(T & v)
|
||||
{
|
||||
|
||||
+18
-22
@@ -266,8 +266,6 @@ std::pair<wivrn::deserialization_packet, sockaddr_in6> wivrn::UDP::receive_from_
|
||||
if (received < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
bytes_received_ += received;
|
||||
|
||||
std::span message{buffer.get(), (size_t)received};
|
||||
|
||||
if (encrypted)
|
||||
@@ -346,8 +344,6 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
|
||||
|
||||
for (int i = received - 1; i >= 0; --i)
|
||||
{
|
||||
bytes_received_ += mmsgs[i].msg_len;
|
||||
|
||||
std::span<uint8_t> message{(uint8_t *)iovecs[i].iov_base, mmsgs[i].msg_len};
|
||||
assert(message.data() != nullptr);
|
||||
|
||||
@@ -376,7 +372,7 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
void wivrn::UDP::send_raw(serialization_packet && packet)
|
||||
size_t wivrn::UDP::send_raw(serialization_packet && packet)
|
||||
{
|
||||
thread_local std::vector<iovec> iovecs;
|
||||
iovecs.clear();
|
||||
@@ -399,23 +395,21 @@ void wivrn::UDP::send_raw(serialization_packet && packet)
|
||||
}
|
||||
|
||||
for (const auto & span: data)
|
||||
{
|
||||
iovecs.emplace_back(span.data(), span.size());
|
||||
bytes_sent_ += span.size();
|
||||
}
|
||||
|
||||
if (::writev(fd, iovecs.data(), iovecs.size()) < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
if (ssize_t sent = ::writev(fd, iovecs.data(), iovecs.size()); sent >= 0)
|
||||
return sent;
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
}
|
||||
|
||||
void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
|
||||
size_t wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
|
||||
{
|
||||
thread_local std::vector<iovec> iovecs;
|
||||
thread_local std::vector<mmsghdr> mmsgs;
|
||||
thread_local std::vector<uint64_t> iv_counters;
|
||||
|
||||
if (packets.empty())
|
||||
return;
|
||||
return 0;
|
||||
|
||||
iovecs.clear();
|
||||
mmsgs.clear();
|
||||
@@ -423,6 +417,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
|
||||
|
||||
iv_counters.reserve(packets.size());
|
||||
|
||||
size_t sent = 0;
|
||||
for (serialization_packet & packet: packets)
|
||||
{
|
||||
std::vector<std::span<uint8_t>> & data = packet;
|
||||
@@ -444,7 +439,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
|
||||
for (const auto & span: data)
|
||||
{
|
||||
iovecs.emplace_back(span.data(), span.size_bytes());
|
||||
bytes_sent_ += span.size();
|
||||
sent += span.size();
|
||||
}
|
||||
|
||||
if (encrypted)
|
||||
@@ -462,6 +457,7 @@ void wivrn::UDP::send_many_raw(std::span<serialization_packet> packets)
|
||||
// sendmmsg may not send all messages, just consider them as lost for UDP
|
||||
if (sendmmsg(fd, mmsgs.data(), mmsgs.size(), 0) < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
return sent;
|
||||
}
|
||||
|
||||
wivrn::deserialization_packet wivrn::TCP::receive_raw()
|
||||
@@ -503,8 +499,6 @@ wivrn::deserialization_packet wivrn::TCP::receive_raw()
|
||||
if (received_size == 0)
|
||||
throw socket_shutdown{};
|
||||
|
||||
bytes_received_ += received_size;
|
||||
|
||||
if (decrypter)
|
||||
{
|
||||
std::span<uint8_t> received_data{&*data.end(), (size_t)received_size};
|
||||
@@ -547,8 +541,9 @@ wivrn::deserialization_packet wivrn::TCP::receive_pending()
|
||||
return deserialization_packet{buffer, span};
|
||||
}
|
||||
|
||||
void wivrn::TCP::send_raw(serialization_packet && packet)
|
||||
size_t wivrn::TCP::send_raw(serialization_packet && packet)
|
||||
{
|
||||
size_t total_sent = 0;
|
||||
thread_local std::vector<iovec> iovecs;
|
||||
iovecs.clear();
|
||||
|
||||
@@ -589,7 +584,7 @@ void wivrn::TCP::send_raw(serialization_packet && packet)
|
||||
if (sent < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
bytes_sent_ += sent;
|
||||
total_sent += sent;
|
||||
|
||||
// iov fully consumed
|
||||
while (hdr.msg_iovlen > 0 and sent >= hdr.msg_iov[0].iov_len)
|
||||
@@ -599,20 +594,20 @@ void wivrn::TCP::send_raw(serialization_packet && packet)
|
||||
--hdr.msg_iovlen;
|
||||
}
|
||||
if (hdr.msg_iovlen == 0)
|
||||
return;
|
||||
return total_sent;
|
||||
hdr.msg_iov[0].iov_base = (void *)((uintptr_t)hdr.msg_iov[0].iov_base + sent);
|
||||
hdr.msg_iov[0].iov_len -= sent;
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
|
||||
size_t wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
|
||||
{
|
||||
thread_local std::vector<iovec> iovecs;
|
||||
thread_local std::vector<uint32_t> sizes;
|
||||
thread_local std::vector<std::span<uint8_t>> spans;
|
||||
|
||||
if (packets.empty())
|
||||
return;
|
||||
return 0;
|
||||
|
||||
iovecs.clear();
|
||||
sizes.clear();
|
||||
@@ -652,6 +647,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
|
||||
encrypter.encrypt_in_place(spans);
|
||||
}
|
||||
|
||||
size_t total_sent = 0;
|
||||
while (true)
|
||||
{
|
||||
ssize_t sent = ::sendmsg(fd, &hdr, MSG_NOSIGNAL);
|
||||
@@ -662,7 +658,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
|
||||
if (sent < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
bytes_sent_ += sent;
|
||||
total_sent += sent;
|
||||
|
||||
// iov fully consumed
|
||||
while (hdr.msg_iovlen > 0 and sent >= hdr.msg_iov[0].iov_len)
|
||||
@@ -672,7 +668,7 @@ void wivrn::TCP::send_many_raw(std::span<serialization_packet> packets)
|
||||
--hdr.msg_iovlen;
|
||||
}
|
||||
if (hdr.msg_iovlen == 0)
|
||||
return;
|
||||
return total_sent;
|
||||
hdr.msg_iov[0].iov_base = (void *)((uintptr_t)hdr.msg_iov[0].iov_base + sent);
|
||||
hdr.msg_iov[0].iov_len -= sent;
|
||||
}
|
||||
|
||||
+16
-19
@@ -68,8 +68,6 @@ protected:
|
||||
int fd = -1;
|
||||
|
||||
fd_base(const fd_base &) = delete;
|
||||
std::atomic<uint64_t> bytes_sent_ = 0;
|
||||
std::atomic<uint64_t> bytes_received_ = 0;
|
||||
|
||||
public:
|
||||
fd_base() = default;
|
||||
@@ -89,14 +87,9 @@ public:
|
||||
return fd != -1;
|
||||
}
|
||||
|
||||
uint64_t bytes_sent() const
|
||||
operator int() const
|
||||
{
|
||||
return bytes_sent_;
|
||||
}
|
||||
|
||||
uint64_t bytes_received() const
|
||||
{
|
||||
return bytes_received_;
|
||||
return fd;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -122,8 +115,8 @@ public:
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
std::pair<wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::span<serialization_packet> packets);
|
||||
size_t send_raw(serialization_packet && packet);
|
||||
size_t send_many_raw(std::span<serialization_packet> packets);
|
||||
|
||||
void connect(in6_addr address, int port);
|
||||
void connect(in_addr address, int port);
|
||||
@@ -157,8 +150,8 @@ public:
|
||||
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::span<serialization_packet> packets);
|
||||
size_t send_raw(serialization_packet && packet);
|
||||
size_t send_many_raw(std::span<serialization_packet> packets);
|
||||
|
||||
void set_aes_key_and_ivs(std::span<std::uint8_t, 16> key, std::span<std::uint8_t, 16> recv_iv, std::span<std::uint8_t, 16> send_iv);
|
||||
};
|
||||
@@ -220,20 +213,24 @@ public:
|
||||
Socket(std::forward<Args>(args)...)
|
||||
{}
|
||||
|
||||
std::optional<ReceivedType> receive_pending()
|
||||
std::optional<ReceivedType> receive_pending(std::atomic<uint64_t> * size = nullptr)
|
||||
{
|
||||
deserialization_packet packet = ((Socket *)this)->receive_pending();
|
||||
if (packet.empty())
|
||||
return {};
|
||||
if (size)
|
||||
size->fetch_add(packet.wire_size());
|
||||
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
|
||||
std::optional<ReceivedType> receive()
|
||||
std::optional<ReceivedType> receive(std::atomic<uint64_t> * size = nullptr)
|
||||
{
|
||||
deserialization_packet packet = this->receive_raw();
|
||||
if (packet.empty())
|
||||
return {};
|
||||
if (size)
|
||||
size->fetch_add(packet.wire_size());
|
||||
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
@@ -249,16 +246,16 @@ public:
|
||||
}
|
||||
|
||||
template <details::not_lvalue_reference T>
|
||||
void send(T && data)
|
||||
size_t send(T && data)
|
||||
{
|
||||
thread_local serialization_packet p;
|
||||
serialize(p, data);
|
||||
this->send_raw(std::move(p));
|
||||
return this->send_raw(std::move(p));
|
||||
}
|
||||
|
||||
void send(std::span<serialization_packet> packets)
|
||||
size_t send(std::span<serialization_packet> packets)
|
||||
{
|
||||
this->send_many_raw(packets);
|
||||
return this->send_many_raw(packets);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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`
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ public:
|
||||
|
||||
std::optional<from_headset::packets> poll_control(int timeout);
|
||||
|
||||
const wivrn::from_headset::headset_info_packet & info() const
|
||||
wivrn::from_headset::headset_info_packet & info()
|
||||
{
|
||||
return info_packet;
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
@@ -389,6 +407,12 @@ void wivrn_session::operator()(from_headset::headset_info_packet &&)
|
||||
U_LOG_W("unexpected headset info packet, ignoring");
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(from_headset::settings_changed && settings)
|
||||
{
|
||||
connection->info().preferred_refresh_rate = settings.preferred_refresh_rate;
|
||||
connection->info().minimum_refresh_rate = settings.minimum_refresh_rate;
|
||||
}
|
||||
|
||||
static xrt_device_name get_name(interaction_profile profile)
|
||||
{
|
||||
switch (profile)
|
||||
@@ -582,6 +606,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);
|
||||
@@ -866,23 +908,17 @@ struct refresh_rate_adjuster
|
||||
{
|
||||
std::chrono::seconds period{10};
|
||||
std::chrono::steady_clock::time_point next = std::chrono::steady_clock::now() + period;
|
||||
bool enabled;
|
||||
pacing_app_factory & pacers;
|
||||
const from_headset::headset_info_packet & info;
|
||||
from_headset::headset_info_packet & info;
|
||||
float last = 0;
|
||||
|
||||
refresh_rate_adjuster(const from_headset::headset_info_packet & info, pacing_app_factory & pacers) :
|
||||
enabled(info.preferred_refresh_rate == 0 and info.available_refresh_rates.size() > 1),
|
||||
refresh_rate_adjuster(from_headset::headset_info_packet & info, pacing_app_factory & pacers) :
|
||||
pacers(pacers),
|
||||
info(info)
|
||||
{
|
||||
if (enabled)
|
||||
U_LOG_I("Automatic refresh rate adjustment enabled");
|
||||
}
|
||||
info(info) {}
|
||||
|
||||
void adjust(wivrn_connection & cnx)
|
||||
{
|
||||
if (not enabled or std::chrono::steady_clock::now() < next)
|
||||
if (info.preferred_refresh_rate != 0 or info.available_refresh_rates.size() < 2 or std::chrono::steady_clock::now() < next)
|
||||
return;
|
||||
|
||||
// Maximum refresh rate the application can reach
|
||||
@@ -892,7 +928,7 @@ struct refresh_rate_adjuster
|
||||
auto requested = info.available_refresh_rates.back();
|
||||
for (auto rate: info.available_refresh_rates)
|
||||
{
|
||||
if (rate < app_rate * (rate == last ? 1. : 0.9))
|
||||
if (rate > info.minimum_refresh_rate and rate < app_rate * (rate == last ? 1. : 0.9))
|
||||
requested = rate;
|
||||
}
|
||||
if (requested != last)
|
||||
@@ -912,7 +948,7 @@ struct refresh_rate_adjuster
|
||||
|
||||
void wivrn_session::run(std::stop_token stop)
|
||||
{
|
||||
refresh_rate_adjuster refresh(get_info(), app_pacers);
|
||||
refresh_rate_adjuster refresh(connection->info(), app_pacers);
|
||||
while (not stop.stop_requested())
|
||||
{
|
||||
try
|
||||
|
||||
@@ -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;
|
||||
@@ -157,7 +159,7 @@ public:
|
||||
|
||||
clock_offset get_offset();
|
||||
bool connected();
|
||||
const from_headset::headset_info_packet & get_info()
|
||||
const from_headset::headset_info_packet & get_info() const
|
||||
{
|
||||
return connection->info();
|
||||
};
|
||||
@@ -182,6 +184,7 @@ public:
|
||||
void operator()(from_headset::pin_check_1 &&) {}
|
||||
void operator()(from_headset::pin_check_3 &&) {}
|
||||
void operator()(from_headset::headset_info_packet &&);
|
||||
void operator()(from_headset::settings_changed &&);
|
||||
void operator()(from_headset::handshake &&) {}
|
||||
void operator()(from_headset::trackings &&);
|
||||
void operator()(const from_headset::tracking &);
|
||||
@@ -189,6 +192,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 &&);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
+4
-1
@@ -512,10 +512,13 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data)
|
||||
{
|
||||
std::visit(utils::overloaded{
|
||||
[&](const wivrn::from_headset::headset_info_packet & info) {
|
||||
on_headset_info_packet(std::get<wivrn::from_headset::headset_info_packet>(*packet));
|
||||
on_headset_info_packet(info);
|
||||
inhibitor.emplace();
|
||||
wivrn_server_set_headset_connected(dbus_server, true);
|
||||
},
|
||||
[&](const wivrn::from_headset::settings_changed & settings) {
|
||||
wivrn_server_set_preferred_refresh_rate(dbus_server, settings.preferred_refresh_rate);
|
||||
},
|
||||
[&](const wivrn::from_headset::start_app & request) {
|
||||
const auto & apps = list_applications();
|
||||
if (auto it = apps.find(request.app_id); it != apps.end())
|
||||
|
||||
Reference in New Issue
Block a user