libvirtualhid 23
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 dualshock4,
234 };
235
243 bool supports_rumble = false;
244
248 bool supports_motion = false;
249
253 bool supports_touchpad = false;
254
258 bool supports_rgb_led = false;
259
263 bool supports_battery = false;
264
268 bool supports_adaptive_triggers = false;
269 };
270
279
284
289
293 std::uint16_t vendor_id = 0;
294
298 std::uint16_t product_id = 0;
299
303 std::uint16_t version = 0;
304
308 std::uint8_t report_id = 1;
309
313 std::size_t input_report_size = 0;
314
318 std::size_t output_report_size = 0;
319
323 std::string name;
324
328 std::string manufacturer;
329
334
338 std::vector<std::uint8_t> report_descriptor;
339 };
340
345 unknown,
346 xbox,
348 nintendo,
349 };
350
358 int global_index = -1;
359
363 int client_relative_index = -1;
364
369
373 bool has_motion_sensors = false;
374
378 bool has_touchpad = false;
379
383 bool has_rgb_led = false;
384
388 bool has_battery = false;
389
393 std::string stable_id;
394 };
395
410
419
423 std::uint32_t auto_repeat_interval_ms = 50;
424
428 std::string stable_id;
429 };
430
445
460
475
490
494 enum class GamepadButton : std::uint8_t {
495 a = 0,
496 b,
497 x,
498 y,
499 back,
500 start,
501 guide,
502 left_stick,
506 dpad_up,
507 dpad_down,
508 dpad_left,
509 dpad_right,
510 misc1,
511 touchpad,
512 };
513
517 class ButtonSet {
518 public:
525 void set(GamepadButton button, bool pressed = true);
526
533
537 void clear();
538
546
552 std::uint32_t raw_bits() const;
553
554 private:
555 std::uint32_t bits_ = 0;
556 };
557
561 struct Stick {
565 float x = 0.0F;
566
570 float y = 0.0F;
571 };
572
576 struct Vector3 {
580 float x = 0.0F;
581
585 float y = 0.0F;
586
590 float z = 0.0F;
591 };
592
596 enum class GamepadBatteryState : std::uint8_t {
597 unknown,
599 charging,
600 full,
604 };
605
614
618 std::uint8_t percentage = 100;
619 };
620
628 std::uint8_t id = 0;
629
633 bool active = false;
634
638 float x = 0.0F;
639
643 float y = 0.0F;
644 };
645
654
659
664
668 float left_trigger = 0.0F;
669
673 float right_trigger = 0.0F;
674
678 std::optional<Vector3> acceleration;
679
683 std::optional<Vector3> gyroscope;
684
688 std::optional<GamepadBattery> battery;
689
693 std::array<GamepadTouchContact, 2> touchpad_contacts {};
694 };
695
703 using KeyboardKeyCode = std::uint16_t;
704
712 KeyboardKeyCode key_code = 0;
713
717 bool pressed = false;
718 };
719
727 std::string text;
728 };
729
733 enum class MouseButton : std::uint8_t {
734 left = 0,
735 middle,
736 right,
737 side,
738 extra,
739 };
740
751
755 struct MouseEvent {
760
764 std::int32_t x = 0;
765
769 std::int32_t y = 0;
770
774 std::int32_t width = 0;
775
779 std::int32_t height = 0;
780
785
789 bool pressed = false;
790
794 std::int32_t high_resolution_scroll = 0;
795 };
796
804 std::int32_t id = 0;
805
809 float x = 0.0F;
810
814 float y = 0.0F;
815
819 float pressure = 0.0F;
820
824 std::int32_t orientation = 0;
825 };
826
830 enum class PenToolType : std::uint8_t {
831 pen,
832 eraser,
833 brush,
834 pencil,
835 airbrush,
836 touch,
837 unchanged,
838 };
839
843 enum class PenButton : std::uint8_t {
844 primary,
845 secondary,
846 tertiary,
847 };
848
857
861 float x = 0.0F;
862
866 float y = 0.0F;
867
871 float pressure = -1.0F;
872
876 float distance = -1.0F;
877
881 float tilt_x = 0.0F;
882
886 float tilt_y = 0.0F;
887 };
888
892 enum class GamepadOutputKind {
893 rumble,
894 rgb_led,
896 raw_report,
897 };
898
907
911 std::uint16_t low_frequency_rumble = 0;
912
916 std::uint16_t high_frequency_rumble = 0;
917
921 std::uint8_t red = 0;
922
926 std::uint8_t green = 0;
927
931 std::uint8_t blue = 0;
932
936 std::uint8_t adaptive_trigger_flags = 0;
937
941 std::uint8_t left_trigger_effect_type = 0;
942
946 std::uint8_t right_trigger_effect_type = 0;
947
951 std::array<std::uint8_t, 10> left_trigger_effect {};
952
956 std::array<std::uint8_t, 10> right_trigger_effect {};
957
961 std::vector<std::uint8_t> raw_report;
962 };
963
967 using OutputCallback = std::function<void(const GamepadOutput &)>;
968
969} // namespace lvh
Compact set of pressed gamepad buttons.
Definition types.hpp:517
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:703
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.
@ dualshock4
PlayStation DualShock 4-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:596
@ 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:344
@ 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:967
GamepadOutputKind
Output report categories delivered by a gamepad backend.
Definition types.hpp:892
@ 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:733
@ 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:744
@ 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:494
@ 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.
@ touchpad
Touchpad click button.
@ start
Start or options button.
PenButton
Pen tablet buttons.
Definition types.hpp:843
@ secondary
Secondary stylus button.
@ primary
Primary stylus button.
@ tertiary
Tertiary stylus button.
PenToolType
Pen tablet tool categories.
Definition types.hpp:830
@ 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:399
GamepadMetadata metadata
Consumer metadata associated with the device.
Definition types.hpp:408
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:403
Full keyboard creation request.
Definition types.hpp:414
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:428
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:418
Full mouse creation request.
Definition types.hpp:434
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:443
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:438
Full pen tablet creation request.
Definition types.hpp:479
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:483
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:488
Full touchscreen creation request.
Definition types.hpp:449
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:453
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:458
Full trackpad creation request.
Definition types.hpp:464
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:473
DeviceProfile profile
Device profile to instantiate.
Definition types.hpp:468
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:274
std::vector< std::uint8_t > report_descriptor
HID report descriptor bytes.
Definition types.hpp:338
GamepadProfileCapabilities capabilities
Profile feature flags.
Definition types.hpp:333
std::string name
Human-readable device name.
Definition types.hpp:323
std::string manufacturer
Human-readable device manufacturer.
Definition types.hpp:328
Gamepad battery charge metadata.
Definition types.hpp:609
Consumer-provided metadata for a gamepad device.
Definition types.hpp:354
std::string stable_id
Consumer-defined stable identity string.
Definition types.hpp:393
Normalized gamepad output event delivered to the consumer.
Definition types.hpp:902
std::vector< std::uint8_t > raw_report
Raw output report payload.
Definition types.hpp:961
Optional behavior advertised by a gamepad profile.
Definition types.hpp:239
Common gamepad input state accepted by libvirtualhid.
Definition types.hpp:649
std::optional< Vector3 > gyroscope
Gyroscope data in degrees per second, when available.
Definition types.hpp:683
std::optional< GamepadBattery > battery
Battery metadata, when available.
Definition types.hpp:688
Stick left_stick
Left stick state.
Definition types.hpp:658
ButtonSet buttons
Pressed button set.
Definition types.hpp:653
Stick right_stick
Right stick state.
Definition types.hpp:663
std::optional< Vector3 > acceleration
Accelerometer data in meters per second squared, when available.
Definition types.hpp:678
Touchpad contact carried by a gamepad report.
Definition types.hpp:624
Keyboard key transition.
Definition types.hpp:708
UTF-8 text input request.
Definition types.hpp:723
std::string text
UTF-8 text to type.
Definition types.hpp:727
Mouse input event.
Definition types.hpp:755
Pen tablet tool position and analog state.
Definition types.hpp:852
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:561
Touch contact event for touchscreen and trackpad devices.
Definition types.hpp:800
Normalized three-axis sensor state.
Definition types.hpp:576