17namespace platf::virtualhid {
22 float position(
float normalized,
int offset,
int span) {
23 if (!std::isfinite(normalized)) {
26 const float value = normalized + (span > 0 ?
static_cast<float>(offset) / span : 0.0f);
27 return std::clamp(value, 0.0f, 1.0f);
30 float analog(
float value) {
31 return std::isfinite(value) ? std::clamp(value, 0.0f, 1.0f) : 0.0f;
34 float contact_size(
float value) {
35 return std::isfinite(value) && value > 0.0F ? value : 0.0F;
38 int orientation(std::uint16_t rotation) {
39 if (rotation == LI_ROT_UNKNOWN) {
42 int degrees =
static_cast<int>(rotation) % 360;
46 if (degrees > 90 && degrees < 270) {
47 degrees = 180 - degrees;
52 return std::clamp(degrees, -90, 90);
57 client_devices_t::client_devices_t(
const std::string &session_tag, std::unique_ptr<lvh::Runtime> input_runtime) {
58 runtime = input_runtime ? std::move(input_runtime) : create_runtime();
60 BOOST_LOG(error) <<
"[libvirtualhid] Failed to create Linux HID runtime";
64 lvh::CreateTouchscreenOptions touch_options;
65 touch_options.profile = with_hermes_identity(
66 lvh::profiles::touchscreen(),
68 session_tag.empty() ? VDISPLAY::VIRTUAL_TOUCH_DEVICE_NAME :
"Hermes Session Touch"
70 touch_options.stable_id = session_tag +
"-touch";
71 auto created_touch = runtime->create_touchscreen(touch_options);
73 touch = std::move(created_touch.touchscreen);
75 BOOST_LOG(warning) <<
"[libvirtualhid] Touchscreen unavailable: "
76 << created_touch.status.message();
79 lvh::CreatePenTabletOptions pen_options;
80 pen_options.profile = with_hermes_identity(
81 lvh::profiles::pen_tablet(),
83 session_tag.empty() ? VDISPLAY::VIRTUAL_PEN_DEVICE_NAME :
"Hermes Session Pen"
85 pen_options.stable_id = session_tag +
"-pen";
86 auto created_pen = runtime->create_pen_tablet(pen_options);
88 pen = std::move(created_pen.pen_tablet);
90 BOOST_LOG(warning) <<
"[libvirtualhid] Pen tablet unavailable: "
91 << created_pen.status.message();
95 void touch_update(
client_devices_t &devices,
const touch_port_t &port,
const touch_input_t &event) {
99 const auto id =
static_cast<std::int32_t
>(
event.pointerId);
100 switch (event.eventType) {
101 case LI_TOUCH_EVENT_HOVER:
102 case LI_TOUCH_EVENT_DOWN:
103 case LI_TOUCH_EVENT_MOVE:
105 lvh::TouchContact contact;
107 contact.x = position(event.x, port.offset_x, port.width);
108 contact.y = position(event.y, port.offset_y, port.height);
109 contact.pressure = analog(event.pressureOrDistance);
110 contact.orientation = orientation(event.rotation);
111 contact.touching =
event.eventType != LI_TOUCH_EVENT_HOVER;
113 .offset_x = port.offset_x,
114 .offset_y = port.offset_y,
116 .height = port.height,
118 contact.contact_major_axis = contact_size(event.contactAreaMajor);
119 contact.contact_minor_axis = contact_size(event.contactAreaMinor);
120 virtualhid::log_failure(
"touch update", devices.touch->place_contact(contact));
121 devices.active_contacts.insert(
id);
124 case LI_TOUCH_EVENT_UP:
125 virtualhid::log_failure(
"touch release", devices.touch->release_contact(
id));
126 devices.active_contacts.erase(
id);
128 case LI_TOUCH_EVENT_CANCEL:
129 virtualhid::log_failure(
"touch cancel", devices.touch->cancel_contact(
id));
130 devices.active_contacts.erase(
id);
132 case LI_TOUCH_EVENT_HOVER_LEAVE:
133 virtualhid::log_failure(
"touch leave", devices.touch->leave_contact(
id));
134 devices.active_contacts.erase(
id);
136 case LI_TOUCH_EVENT_CANCEL_ALL:
139 for (
const auto active : devices.active_contacts) {
140 virtualhid::log_failure(
"touch cancel all", devices.touch->cancel_contact(active));
142 devices.active_contacts.clear();
150 void pen_update(
client_devices_t &devices,
const touch_port_t &port,
const pen_input_t &event) {
158 const auto current_buttons =
static_cast<std::uint8_t
>(
event.penButtons);
159 auto update_button = [&](std::uint8_t bit, lvh::PenButton button) {
160 if (((current_buttons ^ devices.pen_buttons) & bit) == 0) {
163 const bool pressed = (current_buttons & bit) != 0;
164 const auto result = devices.pen->button(button, pressed);
165 virtualhid::log_failure(
"pen button", result);
168 devices.pen_buttons |= bit;
170 devices.pen_buttons &=
static_cast<std::uint8_t
>(~bit);
174 update_button(LI_PEN_BUTTON_PRIMARY, lvh::PenButton::primary);
175 update_button(LI_PEN_BUTTON_SECONDARY, lvh::PenButton::secondary);
176 update_button(LI_PEN_BUTTON_TERTIARY, lvh::PenButton::tertiary);
178 if (event.eventType == LI_TOUCH_EVENT_BUTTON_ONLY) {
182 lvh::PenToolState tool;
183 switch (event.toolType) {
184 case LI_TOOL_TYPE_PEN:
185 tool.tool = lvh::PenToolType::pen;
187 case LI_TOOL_TYPE_ERASER:
188 tool.tool = lvh::PenToolType::eraser;
191 tool.tool = lvh::PenToolType::unchanged;
194 tool.x = position(event.x, port.offset_x, port.width);
195 tool.y = position(event.y, port.offset_y, port.height);
197 switch (event.eventType) {
198 case LI_TOUCH_EVENT_DOWN:
199 case LI_TOUCH_EVENT_MOVE:
200 tool.transition = lvh::PointerTransition::update;
201 tool.pressure = analog(event.pressureOrDistance);
203 case LI_TOUCH_EVENT_HOVER:
204 tool.transition = lvh::PointerTransition::update;
205 tool.distance = analog(event.pressureOrDistance);
207 case LI_TOUCH_EVENT_UP:
208 tool.transition = lvh::PointerTransition::release;
210 case LI_TOUCH_EVENT_CANCEL:
211 case LI_TOUCH_EVENT_CANCEL_ALL:
212 tool.transition = lvh::PointerTransition::cancel;
214 case LI_TOUCH_EVENT_HOVER_LEAVE:
215 tool.transition = lvh::PointerTransition::leave;
221 if (event.rotation != LI_ROT_UNKNOWN && event.tilt != LI_TILT_UNKNOWN) {
222 const float rotation_rad =
static_cast<float>(
event.rotation) * (std::numbers::pi_v<float> / 180.0f);
223 const float tilt_rad =
static_cast<float>(
event.tilt) * (std::numbers::pi_v<float> / 180.0f);
224 const float r = std::sin(tilt_rad);
225 const float z = std::cos(tilt_rad);
226 tool.tilt_x = std::atan2(-std::sin(rotation_rad) * r, z) * 180.0f / std::numbers::pi_v<float>;
227 tool.tilt_y = std::atan2(std::cos(rotation_rad) * r, z) * 180.0f / std::numbers::pi_v<float>;
230 virtualhid::log_failure(
"pen tool update", devices.pen->place_tool(tool));
Declarations for logging related functions.
Virtual display declarations for Linux.
Shared state for the Linux libvirtualhid input backend.
Per-client pen/touch adapter using MrOz59/libvirtualhid.