Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
virtualhid_pen_touch.cpp
Go to the documentation of this file.
1
6
7#include "src/logging.h"
9#include "virtualhid_common.h"
10
11#include <algorithm>
12#include <cmath>
13#include <numbers>
14#include <string>
15#include <utility>
16
17namespace platf::virtualhid {
18 namespace {
19 // Preserve the coordinate convention used by Hermes' desktop binding:
20 // offsets are zero for a private output and locate the output within the
21 // host desktop for a shared session.
22 float position(float normalized, int offset, int span) {
23 if (!std::isfinite(normalized)) {
24 return 0.0f;
25 }
26 const float value = normalized + (span > 0 ? static_cast<float>(offset) / span : 0.0f);
27 return std::clamp(value, 0.0f, 1.0f);
28 }
29
30 float analog(float value) {
31 return std::isfinite(value) ? std::clamp(value, 0.0f, 1.0f) : 0.0f;
32 }
33
34 float contact_size(float value) {
35 return std::isfinite(value) && value > 0.0F ? value : 0.0F;
36 }
37
38 int orientation(std::uint16_t rotation) {
39 if (rotation == LI_ROT_UNKNOWN) {
40 return 0;
41 }
42 int degrees = static_cast<int>(rotation) % 360;
43 if (degrees < 0) {
44 degrees += 360;
45 }
46 if (degrees > 90 && degrees < 270) {
47 degrees = 180 - degrees;
48 }
49 if (degrees > 90) {
50 degrees -= 360;
51 }
52 return std::clamp(degrees, -90, 90);
53 }
54
55 } // namespace
56
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();
59 if (!runtime) {
60 BOOST_LOG(error) << "[libvirtualhid] Failed to create Linux HID runtime";
61 return;
62 }
63
64 lvh::CreateTouchscreenOptions touch_options;
65 touch_options.profile = with_hermes_identity(
66 lvh::profiles::touchscreen(),
67 session_tag,
68 session_tag.empty() ? VDISPLAY::VIRTUAL_TOUCH_DEVICE_NAME : "Hermes Session Touch"
69 );
70 touch_options.stable_id = session_tag + "-touch";
71 auto created_touch = runtime->create_touchscreen(touch_options);
72 if (created_touch) {
73 touch = std::move(created_touch.touchscreen);
74 } else {
75 BOOST_LOG(warning) << "[libvirtualhid] Touchscreen unavailable: "
76 << created_touch.status.message();
77 }
78
79 lvh::CreatePenTabletOptions pen_options;
80 pen_options.profile = with_hermes_identity(
81 lvh::profiles::pen_tablet(),
82 session_tag,
83 session_tag.empty() ? VDISPLAY::VIRTUAL_PEN_DEVICE_NAME : "Hermes Session Pen"
84 );
85 pen_options.stable_id = session_tag + "-pen";
86 auto created_pen = runtime->create_pen_tablet(pen_options);
87 if (created_pen) {
88 pen = std::move(created_pen.pen_tablet);
89 } else {
90 BOOST_LOG(warning) << "[libvirtualhid] Pen tablet unavailable: "
91 << created_pen.status.message();
92 }
93 }
94
95 void touch_update(client_devices_t &devices, const touch_port_t &port, const touch_input_t &event) {
96 if (!devices.touch) {
97 return;
98 }
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:
104 {
105 lvh::TouchContact contact;
106 contact.id = id;
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;
112 contact.viewport = {
113 .offset_x = port.offset_x,
114 .offset_y = port.offset_y,
115 .width = port.width,
116 .height = port.height,
117 };
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);
122 return;
123 }
124 case LI_TOUCH_EVENT_UP:
125 virtualhid::log_failure("touch release", devices.touch->release_contact(id));
126 devices.active_contacts.erase(id);
127 return;
128 case LI_TOUCH_EVENT_CANCEL:
129 virtualhid::log_failure("touch cancel", devices.touch->cancel_contact(id));
130 devices.active_contacts.erase(id);
131 return;
132 case LI_TOUCH_EVENT_HOVER_LEAVE:
133 virtualhid::log_failure("touch leave", devices.touch->leave_contact(id));
134 devices.active_contacts.erase(id);
135 return;
136 case LI_TOUCH_EVENT_CANCEL_ALL:
137 {
138 // Release every contact tracked for this client in one operation.
139 for (const auto active : devices.active_contacts) {
140 virtualhid::log_failure("touch cancel all", devices.touch->cancel_contact(active));
141 }
142 devices.active_contacts.clear();
143 return;
144 }
145 default:
146 return;
147 }
148 }
149
150 void pen_update(client_devices_t &devices, const touch_port_t &port, const pen_input_t &event) {
151 if (!devices.pen) {
152 return;
153 }
154
155 // libvirtualhid syncs every button operation into its own evdev frame.
156 // Only submit transitions; forwarding all three buttons at 120 Hz would
157 // generate 360 unnecessary SYN_REPORT frames per second.
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) {
161 return;
162 }
163 const bool pressed = (current_buttons & bit) != 0;
164 const auto result = devices.pen->button(button, pressed);
165 virtualhid::log_failure("pen button", result);
166 if (result.ok()) {
167 if (pressed) {
168 devices.pen_buttons |= bit;
169 } else {
170 devices.pen_buttons &= static_cast<std::uint8_t>(~bit);
171 }
172 }
173 };
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);
177
178 if (event.eventType == LI_TOUCH_EVENT_BUTTON_ONLY) {
179 return;
180 }
181
182 lvh::PenToolState tool;
183 switch (event.toolType) {
184 case LI_TOOL_TYPE_PEN:
185 tool.tool = lvh::PenToolType::pen;
186 break;
187 case LI_TOOL_TYPE_ERASER:
188 tool.tool = lvh::PenToolType::eraser;
189 break;
190 default:
191 tool.tool = lvh::PenToolType::unchanged;
192 break;
193 }
194 tool.x = position(event.x, port.offset_x, port.width);
195 tool.y = position(event.y, port.offset_y, port.height);
196
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);
202 break;
203 case LI_TOUCH_EVENT_HOVER:
204 tool.transition = lvh::PointerTransition::update;
205 tool.distance = analog(event.pressureOrDistance);
206 break;
207 case LI_TOUCH_EVENT_UP:
208 tool.transition = lvh::PointerTransition::release;
209 break;
210 case LI_TOUCH_EVENT_CANCEL:
211 case LI_TOUCH_EVENT_CANCEL_ALL:
212 tool.transition = lvh::PointerTransition::cancel;
213 break;
214 case LI_TOUCH_EVENT_HOVER_LEAVE:
215 tool.transition = lvh::PointerTransition::leave;
216 break;
217 default:
218 return;
219 }
220
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>;
228 }
229
230 virtualhid::log_failure("pen tool update", devices.pen->place_tool(tool));
231 }
232} // namespace platf::virtualhid
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.