libvirtualhid latest
Cross-platform C++ library for virtual HID devices.
types.hpp
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <optional>
13#include <string>
14#include <vector>
15
19namespace lvh {
20
24 using DeviceId = std::uint64_t;
25
37
42 public:
47
55
62
71
77 bool ok() const;
78
84 ErrorCode code() const;
85
91 const std::string &message() const;
92
93 private:
94 ErrorCode code_;
95 std::string message_;
96 };
97
101 enum class BackendKind {
102 fake,
104 };
105
115
123 std::string backend_name;
124
129
133 bool supports_gamepad = false;
134
138 bool supports_keyboard = false;
139
143 bool supports_mouse = false;
144
149
153 bool supports_trackpad = false;
154
159
164
169
174 };
175
179 enum class DeviceNodeKind {
181 joystick,
182 hidraw,
183 sysfs,
184 other,
185 };
186
201
205 enum class DeviceType {
206 gamepad,
207 keyboard,
208 mouse,
210 trackpad,
211 pen_tablet,
212 };
213
217 enum class BusType {
218 unknown,
219 usb,
220 bluetooth,
221 };
222
227 generic,
228 xbox_360,
229 xbox_one,
231 dualsense,
232 switch_pro,
233 };
234
242 bool supports_rumble = false;
243
247 bool supports_motion = false;
248
252 bool supports_touchpad = false;
253
257 bool supports_rgb_led = false;
258
262 bool supports_battery = false;
263
267 bool supports_adaptive_triggers = false;
268 };
269
278
283
288
292 std::uint16_t vendor_id = 0;
293
297 std::uint16_t product_id = 0;
298
302 std::uint16_t version = 0;
303
307 std::uint8_t report_id = 1;
308
312 std::size_t input_report_size = 0;
313
317 std::size_t output_report_size = 0;
318
322 std::string name;
323
327 std::string manufacturer;
328
333
337 std::vector<std::uint8_t> report_descriptor;
338 };
339
344 unknown,
345 xbox,
347 nintendo,
348 };
349
357 int global_index = -1;
358
362 int client_relative_index = -1;
363
368
372 bool has_motion_sensors = false;
373
377 bool has_touchpad = false;
378
382 bool has_rgb_led = false;
383
387 bool has_battery = false;
388
392 std::string stable_id;
393 };
394
409
418
422 std::uint32_t auto_repeat_interval_ms = 50;
423
427 std::string stable_id;
428 };
429
444
459
474
489
493 enum class GamepadButton : std::uint8_t {
494 a = 0,
495 b,
496 x,
497 y,
498 back,
499 start,
500 guide,
501 left_stick,
505 dpad_up,
506 dpad_down,
507 dpad_left,
508 dpad_right,
509 misc1,
510 };
511
515 class ButtonSet {
516 public:
523 void set(GamepadButton button, bool pressed = true);
524
531
535 void clear();
536
544
550 std::uint32_t raw_bits() const;
551
552 private:
553 std::uint32_t bits_ = 0;
554 };
555
559 struct Stick {
563 float x = 0.0F;
564
568 float y = 0.0F;
569 };
570
574 struct Vector3 {
578 float x = 0.0F;
579
583 float y = 0.0F;
584
588 float z = 0.0F;
589 };
590
594 enum class GamepadBatteryState : std::uint8_t {
595 unknown,
597 charging,
598 full,
602 };
603
612
616 std::uint8_t percentage = 100;
617 };
618
626 std::uint8_t id = 0;
627
631 bool active = false;
632
636 float x = 0.0F;
637
641 float y = 0.0F;
642 };
643
652
657
662
666 float left_trigger = 0.0F;
667
671 float right_trigger = 0.0F;
672
676 std::optional<Vector3> acceleration;
677
681 std::optional<Vector3> gyroscope;
682
686 std::optional<GamepadBattery> battery;
687
691 std::array<GamepadTouchContact, 2> touchpad_contacts {};
692 };
693
701 using KeyboardKeyCode = std::uint16_t;
702
710 KeyboardKeyCode key_code = 0;
711
715 bool pressed = false;
716 };
717
725 std::string text;
726 };
727
731 enum class MouseButton : std::uint8_t {
732 left = 0,
733 middle,
734 right,
735 side,
736 extra,
737 };
738
749
753 struct MouseEvent {
758
762 std::int32_t x = 0;
763
767 std::int32_t y = 0;
768
772 std::int32_t width = 0;
773
777 std::int32_t height = 0;
778
783
787 bool pressed = false;
788
792 std::int32_t high_resolution_scroll = 0;
793 };
794
802 std::int32_t id = 0;
803
807 float x = 0.0F;
808
812 float y = 0.0F;
813
817 float pressure = 0.0F;
818
822 std::int32_t orientation = 0;
823 };
824
828 enum class PenToolType : std::uint8_t {
829 pen,
830 eraser,
831 brush,
832 pencil,
833 airbrush,
834 touch,
835 unchanged,
836 };
837
841 enum class PenButton : std::uint8_t {
842 primary,
843 secondary,
844 tertiary,
845 };
846
855
859 float x = 0.0F;
860
864 float y = 0.0F;
865
869 float pressure = -1.0F;
870
874 float distance = -1.0F;
875
879 float tilt_x = 0.0F;
880
884 float tilt_y = 0.0F;
885 };
886
890 enum class GamepadOutputKind {
891 rumble,
892 rgb_led,
894 raw_report,
895 };
896
905
909 std::uint16_t low_frequency_rumble = 0;
910
914 std::uint16_t high_frequency_rumble = 0;
915
919 std::uint8_t red = 0;
920
924 std::uint8_t green = 0;
925
929 std::uint8_t blue = 0;
930
934 std::uint8_t adaptive_trigger_flags = 0;
935
939 std::uint8_t left_trigger_effect_type = 0;
940
944 std::uint8_t right_trigger_effect_type = 0;
945
949 std::array<std::uint8_t, 10> left_trigger_effect {};
950
954 std::array<std::uint8_t, 10> right_trigger_effect {};
955
959 std::vector<std::uint8_t> raw_report;
960 };
961
965 using OutputCallback = std::function<void(const GamepadOutput &)>;
966
967} // namespace lvh
Compact set of pressed gamepad buttons.
Definition types.hpp:515
void clear()
Clear all buttons.
std::uint32_t raw_bits() const
Get the raw bitset value.
void set(GamepadButton button, bool pressed=true)
Set or clear a button.
bool test(GamepadButton button) const
Check whether a button is pressed.
void reset(GamepadButton button)
Clear a button.
Result status with an error category and human-readable message.
Definition types.hpp:41
OperationStatus()
Construct a successful status.
ErrorCode code() const
Get the status error category.
bool ok() const
Check whether the operation succeeded.
const std::string & message() const
Get the human-readable status message.
static OperationStatus failure(ErrorCode code, std::string message)
Create a failing status.
static OperationStatus success()
Create a successful status.
OperationStatus(ErrorCode code, std::string message)
Construct a status with an explicit error code and message.
Public libvirtualhid API namespace.
Definition profiles.hpp:14
BusType
Transport bus identity advertised by a device profile.
Definition types.hpp:217
@ usb
USB-style device identity.
@ bluetooth
Bluetooth-style device identity.
@ unknown
Bus is unknown or not meaningful for the backend.
std::uint16_t KeyboardKeyCode
Keyboard key code accepted by the keyboard event model.
Definition types.hpp:701
GamepadProfileKind
Built-in gamepad profile identifiers.
Definition types.hpp:226
@ xbox_one
Xbox One-compatible profile.
@ switch_pro
Nintendo Switch Pro-compatible profile.
@ generic
Generic HID gamepad profile.
@ dualsense
PlayStation DualSense-compatible profile.
@ xbox_series
Xbox Series-compatible profile.
@ xbox_360
Xbox 360-compatible profile.
ErrorCode
Error categories returned by libvirtualhid operations.
Definition types.hpp:29
@ device_closed
Device operation was requested after the device closed.
@ backend_failure
Backend-specific operation failed.
@ ok
Operation completed successfully.
@ unsupported_profile
Backend cannot create the requested device profile.
@ backend_unavailable
Requested backend is not available on this host.
@ invalid_argument
Caller supplied invalid input.
GamepadBatteryState
Common gamepad battery states.
Definition types.hpp:594
@ charging
Battery is charging.
@ voltage_or_temperature_error
Battery reports voltage or temperature outside the supported range.
@ temperature_error
Battery reports a temperature error.
@ unknown
Battery state is unknown.
@ discharging
Battery is discharging.
@ full
Battery is fully charged.
@ charging_error
Battery reports a charging error.
ClientControllerType
Controller family reported by a streaming client.
Definition types.hpp:343
@ playstation
PlayStation-style client controller.
@ xbox
Xbox-style client controller.
@ nintendo
Nintendo-style client controller.
@ unknown
Controller family is unknown.
std::uint64_t DeviceId
Stable identifier assigned to a virtual device instance.
Definition types.hpp:24
std::function< void(const GamepadOutput &)> OutputCallback
Callback invoked when a gamepad receives output from the backend.
Definition types.hpp:965
GamepadOutputKind
Output report categories delivered by a gamepad backend.
Definition types.hpp:890
@ raw_report
Raw output report bytes.
@ rgb_led
RGB LED color output.
@ rumble
Rumble motor output.
@ adaptive_triggers
Adaptive trigger output.
DeviceType
Device categories supported by the public profile model.
Definition types.hpp:205
@ trackpad
Indirect touchpad device.
@ touchscreen
Direct touch display device.
@ keyboard
Keyboard device.
@ mouse
Mouse or pointer device.
@ gamepad
Game controller device.
@ pen_tablet
Pen tablet device.
MouseButton
Mouse buttons accepted by the mouse event model.
Definition types.hpp:731
@ middle
Middle mouse button.
@ right
Secondary mouse button.
@ left
Primary mouse button.
@ side
First auxiliary mouse button.
@ extra
Second auxiliary mouse button.
MouseEventKind
Mouse event categories accepted by the mouse event model.
Definition types.hpp:742
@ relative_motion
Relative pointer movement.
@ vertical_scroll
High-resolution vertical scroll event.
@ absolute_motion
Absolute pointer movement inside a target area.
@ button
Mouse button transition.
@ horizontal_scroll
High-resolution horizontal scroll event.
DeviceNodeKind
Platform device-node categories reported by virtual devices.
Definition types.hpp:179
@ joystick
Linux /dev/input/js* node or equivalent.
@ sysfs
Linux sysfs path or equivalent diagnostic path.
@ hidraw
Linux /dev/hidraw* node or equivalent.
@ other
Other platform-specific device path.
@ input_event
Linux /dev/input/event* node or equivalent.
BackendKind
Backend implementation selection.
Definition types.hpp:101
@ fake
In-memory backend for tests and API validation.
@ platform_default
Native backend for the current platform.
GamepadButton
Logical gamepad buttons accepted by the common gamepad state model.
Definition types.hpp:493
@ dpad_down
Directional pad down.
@ a
South face button.
@ dpad_up
Directional pad up.
@ left_shoulder
Left shoulder button.
@ y
North face button.
@ back
Back, select, or share button.
@ dpad_left
Directional pad left.
@ b
East face button.
@ misc1
Profile-specific miscellaneous button.
@ x
West face button.
@ right_stick
Right stick press.
@ guide
System guide button.
@ dpad_right
Directional pad right.
@ left_stick
Left stick press.
@ right_shoulder
Right shoulder button.
@ start
Start or options button.
PenButton
Pen tablet buttons.
Definition types.hpp:841
@ secondary
Secondary stylus button.
@ primary
Primary stylus button.
@ tertiary
Tertiary stylus button.
PenToolType
Pen tablet tool categories.
Definition types.hpp:828
@ pen
Pen tool.
@ brush
Brush tool.
@ airbrush
Airbrush tool.
@ unchanged
Keep the previously selected tool.
@ eraser
Eraser tool.
@ pencil
Pencil tool.
@ touch
Direct touch tool.
Feature set exposed by the selected backend.
Definition types.hpp:119
bool supports_pen_tablet
Whether the backend can create pen tablet devices.
Definition types.hpp:158
bool supports_trackpad
Whether the backend can create trackpad devices.
Definition types.hpp:153
bool supports_touchscreen
Whether the backend can create touchscreen devices.
Definition types.hpp:148
std::string backend_name
Human-readable backend name.
Definition types.hpp:123
bool requires_installed_driver
Whether the backend requires an installed driver package.
Definition types.hpp:173
bool supports_gamepad
Whether the backend can create gamepad devices.
Definition types.hpp:133
bool supports_output_reports
Whether the backend can deliver output reports to callers.
Definition types.hpp:163
bool supports_xtest_fallback
Whether the backend can fall back to X11 XTest input.
Definition types.hpp:168
bool supports_virtual_hid
Whether the backend can create virtual HID devices.
Definition types.hpp:128
bool supports_mouse
Whether the backend can create mouse devices.
Definition types.hpp:143
bool supports_keyboard
Whether the backend can create keyboard devices.
Definition types.hpp:138
Full gamepad creation request.
Definition types.hpp:398
GamepadMetadata metadata
Consumer metadata associated with the device.
Definition types.hpp:407
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:402
Full keyboard creation request.
Definition types.hpp:413
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:427
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:417
Full mouse creation request.
Definition types.hpp:433
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:442
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:437
Full pen tablet creation request.
Definition types.hpp:478
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:482
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:487
Full touchscreen creation request.
Definition types.hpp:448
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:452
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:457
Full trackpad creation request.
Definition types.hpp:463
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:472
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:467
Platform-visible node or path associated with a virtual device.
Definition types.hpp:190
DeviceNodeKind kind
Node category.
Definition types.hpp:194
std::string path
Platform path for this node.
Definition types.hpp:199
Descriptor and identity data used to create a virtual device.
Definition types.hpp:273
std::vector< std::uint8_t > report_descriptor
HID report descriptor bytes.
Definition types.hpp:337
GamepadProfileCapabilities capabilities
Profile feature flags.
Definition types.hpp:332
std::string name
Human-readable device name.
Definition types.hpp:322
std::string manufacturer
Human-readable device manufacturer.
Definition types.hpp:327
Gamepad battery charge metadata.
Definition types.hpp:607
Consumer-provided metadata for a gamepad device.
Definition types.hpp:353
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:392
Normalized gamepad output event delivered to the consumer.
Definition types.hpp:900
std::vector< std::uint8_t > raw_report
Raw output report payload.
Definition types.hpp:959
Optional behavior advertised by a gamepad profile.
Definition types.hpp:238
Common gamepad input state accepted by libvirtualhid.
Definition types.hpp:647
std::optional< Vector3 > gyroscope
Gyroscope data in degrees per second, when available.
Definition types.hpp:681
std::optional< GamepadBattery > battery
Battery metadata, when available.
Definition types.hpp:686
Stick left_stick
Left stick state.
Definition types.hpp:656
ButtonSet buttons
Pressed button set.
Definition types.hpp:651
Stick right_stick
Right stick state.
Definition types.hpp:661
std::optional< Vector3 > acceleration
Accelerometer data in meters per second squared, when available.
Definition types.hpp:676
Touchpad contact carried by a gamepad report.
Definition types.hpp:622
Keyboard key transition.
Definition types.hpp:706
UTF-8 text input request.
Definition types.hpp:721
std::string text
UTF-8 text to type.
Definition types.hpp:725
Mouse input event.
Definition types.hpp:753
Pen tablet tool position and analog state.
Definition types.hpp:850
Runtime creation options.
Definition types.hpp:109
BackendKind backend
Backend implementation requested by the caller.
Definition types.hpp:113
Normalized two-axis stick state.
Definition types.hpp:559
Touch contact event for touchscreen and trackpad devices.
Definition types.hpp:798
Normalized three-axis sensor state.
Definition types.hpp:574