keyboard & mouse forwarding
This commit is contained in:
committed by
Patrick Nicolas
parent
efde4bf247
commit
2063cae3ae
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 &&);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user