libvirtualhid 16
Cross-platform C++ library for virtual HID devices.
runtime.hpp
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <cstddef>
9#include <memory>
10#include <vector>
11
12// local includes
14
15namespace lvh {
16
17 class Runtime;
18
19 namespace detail {
24 private:
25 friend class ::lvh::Runtime;
26
27 RuntimeConstructionToken() = default;
28 };
29
30 struct GamepadDevice;
31 struct KeyboardDevice;
32 struct MouseDevice;
33 struct TouchscreenDevice;
34 struct TrackpadDevice;
35 struct PenTabletDevice;
36 class RuntimeState;
37 } // namespace detail
38
43 public:
47 virtual ~VirtualDevice() = default;
48
54 virtual DeviceId device_id() const = 0;
55
61 virtual const DeviceProfile &profile() const = 0;
62
68 virtual bool is_open() const = 0;
69
75 virtual std::vector<DeviceNode> device_nodes() const = 0;
76
82 virtual OperationStatus close() = 0;
83 };
84
88 class Gamepad final: public VirtualDevice {
89 public:
93 Gamepad(const Gamepad &) = delete;
94
100 Gamepad &operator=(const Gamepad &) = delete;
101
107 Gamepad(Gamepad &&other) noexcept;
108
116
123 Gamepad(detail::RuntimeConstructionToken token, std::shared_ptr<detail::GamepadDevice> device);
124
128 ~Gamepad() override;
129
133 DeviceId device_id() const override;
134
138 const DeviceProfile &profile() const override;
139
145 const GamepadMetadata &metadata() const;
146
150 bool is_open() const override;
151
155 std::vector<DeviceNode> device_nodes() const override;
156
161
169
175 void set_output_callback(const OutputCallback &callback);
176
184
191
197 std::vector<std::uint8_t> last_input_report() const;
198
204 std::size_t submit_count() const;
205
206 private:
207 std::shared_ptr<detail::GamepadDevice> device_;
208 };
209
213 class Keyboard final: public VirtualDevice {
214 public:
218 Keyboard(const Keyboard &) = delete;
219
225 Keyboard &operator=(const Keyboard &) = delete;
226
233
241
248 Keyboard(detail::RuntimeConstructionToken token, std::shared_ptr<detail::KeyboardDevice> device);
249
253 ~Keyboard() override;
254
258 DeviceId device_id() const override;
259
263 const DeviceProfile &profile() const override;
264
268 bool is_open() const override;
269
273 std::vector<DeviceNode> device_nodes() const override;
274
279
287
295
303
311
318
324 std::size_t submit_count() const;
325
326 private:
327 std::shared_ptr<detail::KeyboardDevice> device_;
328 };
329
333 class Mouse final: public VirtualDevice {
334 public:
338 Mouse(const Mouse &) = delete;
339
345 Mouse &operator=(const Mouse &) = delete;
346
352 Mouse(Mouse &&other) noexcept;
353
361
368 Mouse(detail::RuntimeConstructionToken token, std::shared_ptr<detail::MouseDevice> device);
369
373 ~Mouse() override;
374
378 DeviceId device_id() const override;
379
383 const DeviceProfile &profile() const override;
384
388 bool is_open() const override;
389
393 std::vector<DeviceNode> device_nodes() const override;
394
399
407
415 OperationStatus move_relative(std::int32_t delta_x, std::int32_t delta_y);
416
426 OperationStatus move_absolute(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
427
436
443 OperationStatus vertical_scroll(std::int32_t distance);
444
451 OperationStatus horizontal_scroll(std::int32_t distance);
452
459
465 std::size_t submit_count() const;
466
467 private:
468 std::shared_ptr<detail::MouseDevice> device_;
469 };
470
474 class Touchscreen final: public VirtualDevice {
475 public:
479 Touchscreen(const Touchscreen &) = delete;
480
487
494
502
509 Touchscreen(detail::RuntimeConstructionToken token, std::shared_ptr<detail::TouchscreenDevice> device);
510
514 ~Touchscreen() override;
515
519 DeviceId device_id() const override;
520
524 const DeviceProfile &profile() const override;
525
529 bool is_open() const override;
530
534 std::vector<DeviceNode> device_nodes() const override;
535
540
548
555 OperationStatus release_contact(std::int32_t contact_id);
556
563
569 std::size_t submit_count() const;
570
571 private:
572 std::shared_ptr<detail::TouchscreenDevice> device_;
573 };
574
578 class Trackpad final: public VirtualDevice {
579 public:
583 Trackpad(const Trackpad &) = delete;
584
590 Trackpad &operator=(const Trackpad &) = delete;
591
598
606
613 Trackpad(detail::RuntimeConstructionToken token, std::shared_ptr<detail::TrackpadDevice> device);
614
618 ~Trackpad() override;
619
623 DeviceId device_id() const override;
624
628 const DeviceProfile &profile() const override;
629
633 bool is_open() const override;
634
638 std::vector<DeviceNode> device_nodes() const override;
639
644
652
659 OperationStatus release_contact(std::int32_t contact_id);
660
667 OperationStatus button(bool pressed);
668
675
681 std::size_t submit_count() const;
682
683 private:
684 std::shared_ptr<detail::TrackpadDevice> device_;
685 };
686
690 class PenTablet final: public VirtualDevice {
691 public:
695 PenTablet(const PenTablet &) = delete;
696
702 PenTablet &operator=(const PenTablet &) = delete;
703
710
718
725 PenTablet(detail::RuntimeConstructionToken token, std::shared_ptr<detail::PenTabletDevice> device);
726
730 ~PenTablet() override;
731
735 DeviceId device_id() const override;
736
740 const DeviceProfile &profile() const override;
741
745 bool is_open() const override;
746
750 std::vector<DeviceNode> device_nodes() const override;
751
756
764
773
780
786 std::size_t submit_count() const;
787
788 private:
789 std::shared_ptr<detail::PenTabletDevice> device_;
790 };
791
800
804 std::unique_ptr<Gamepad> gamepad;
805
811 explicit operator bool() const {
812 return status.ok() && gamepad != nullptr;
813 }
814 };
815
824
828 std::unique_ptr<Keyboard> keyboard;
829
835 explicit operator bool() const {
836 return status.ok() && keyboard != nullptr;
837 }
838 };
839
848
852 std::unique_ptr<Mouse> mouse;
853
859 explicit operator bool() const {
860 return status.ok() && mouse != nullptr;
861 }
862 };
863
872
876 std::unique_ptr<Touchscreen> touchscreen;
877
883 explicit operator bool() const {
884 return status.ok() && touchscreen != nullptr;
885 }
886 };
887
896
900 std::unique_ptr<Trackpad> trackpad;
901
907 explicit operator bool() const {
908 return status.ok() && trackpad != nullptr;
909 }
910 };
911
920
924 std::unique_ptr<PenTablet> pen_tablet;
925
931 explicit operator bool() const {
932 return status.ok() && pen_tablet != nullptr;
933 }
934 };
935
939 class Runtime final {
940 public:
944 Runtime(const Runtime &) = delete;
945
951 Runtime &operator=(const Runtime &) = delete;
952
958 Runtime(Runtime &&other) noexcept;
959
967
975
980
987 static std::unique_ptr<Runtime> create(RuntimeOptions options = {});
988
995
1002
1010
1018
1025
1033
1040
1048
1055
1063
1070
1078
1085
1093
1099 std::size_t active_device_count() const;
1100
1105
1106 private:
1107 std::shared_ptr<detail::RuntimeState> state_;
1108 };
1109
1110} // namespace lvh
Virtual gamepad device handle.
Definition runtime.hpp:88
Gamepad & operator=(Gamepad &&other) noexcept
Move assign a gamepad handle.
std::size_t submit_count() const
Get the number of successful submit operations.
Gamepad & operator=(const Gamepad &)=delete
Copy assignment is disabled because the handle owns device lifetime.
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
const GamepadMetadata & metadata() const
Get the metadata supplied when the gamepad was created.
~Gamepad() override
Destroy the gamepad handle.
Gamepad(Gamepad &&other) noexcept
Move construct a gamepad handle.
Gamepad(detail::RuntimeConstructionToken token, std::shared_ptr< detail::GamepadDevice > device)
Construct a gamepad handle for Runtime-owned state.
GamepadState last_submitted_state() const
Get the most recently submitted gamepad state.
OperationStatus submit(const GamepadState &state)
Submit the latest gamepad input state.
bool is_open() const override
Check whether the device is open.
std::vector< std::uint8_t > last_input_report() const
Get the most recently packed input report.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
const DeviceProfile & profile() const override
Get the profile used to create this device.
OperationStatus close() override
Close the virtual device.
OperationStatus dispatch_output(const GamepadOutput &output)
Dispatch an output event to the registered callback.
Gamepad(const Gamepad &)=delete
Copy construction is disabled because the handle owns device lifetime.
void set_output_callback(const OutputCallback &callback)
Register a callback for backend output events.
Virtual keyboard device handle.
Definition runtime.hpp:213
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
OperationStatus type_text(const KeyboardTextEvent &event)
Type UTF-8 text.
const DeviceProfile & profile() const override
Get the profile used to create this device.
OperationStatus close() override
Close the virtual device.
Keyboard(detail::RuntimeConstructionToken token, std::shared_ptr< detail::KeyboardDevice > device)
Construct a keyboard handle for Runtime-owned state.
OperationStatus submit(const KeyboardEvent &event)
Submit a keyboard key transition.
OperationStatus press(KeyboardKeyCode key_code)
Press a keyboard key.
Keyboard(Keyboard &&other) noexcept
Move construct a keyboard handle.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
KeyboardEvent last_submitted_event() const
Get the most recently submitted keyboard event.
std::size_t submit_count() const
Get the number of successful submit operations.
Keyboard(const Keyboard &)=delete
Copy construction is disabled because the handle owns device lifetime.
~Keyboard() override
Destroy the keyboard handle.
Keyboard & operator=(const Keyboard &)=delete
Copy assignment is disabled because the handle owns device lifetime.
OperationStatus release(KeyboardKeyCode key_code)
Release a keyboard key.
Keyboard & operator=(Keyboard &&other) noexcept
Move assign a keyboard handle.
bool is_open() const override
Check whether the device is open.
Virtual mouse device handle.
Definition runtime.hpp:333
std::size_t submit_count() const
Get the number of successful submit operations.
Mouse(Mouse &&other) noexcept
Move construct a mouse handle.
const DeviceProfile & profile() const override
Get the profile used to create this device.
Mouse(const Mouse &)=delete
Copy construction is disabled because the handle owns device lifetime.
MouseEvent last_submitted_event() const
Get the most recently submitted mouse event.
OperationStatus button(MouseButton button, bool pressed)
Submit a mouse button transition.
OperationStatus submit(const MouseEvent &event)
Submit a mouse event.
OperationStatus move_absolute(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height)
Submit absolute pointer movement.
~Mouse() override
Destroy the mouse handle.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
OperationStatus horizontal_scroll(std::int32_t distance)
Submit high-resolution horizontal scroll.
Mouse(detail::RuntimeConstructionToken token, std::shared_ptr< detail::MouseDevice > device)
Construct a mouse handle for Runtime-owned state.
OperationStatus move_relative(std::int32_t delta_x, std::int32_t delta_y)
Submit relative pointer movement.
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
OperationStatus vertical_scroll(std::int32_t distance)
Submit high-resolution vertical scroll.
Mouse & operator=(const Mouse &)=delete
Copy assignment is disabled because the handle owns device lifetime.
Mouse & operator=(Mouse &&other) noexcept
Move assign a mouse handle.
OperationStatus close() override
Close the virtual device.
bool is_open() const override
Check whether the device is open.
Result status with an error category and human-readable message.
Definition types.hpp:41
bool ok() const
Check whether the operation succeeded.
Virtual pen tablet device handle.
Definition runtime.hpp:690
OperationStatus place_tool(const PenToolState &state)
Place or move the active tablet tool.
PenTablet(const PenTablet &)=delete
Copy construction is disabled because the handle owns device lifetime.
OperationStatus close() override
Close the virtual device.
~PenTablet() override
Destroy the pen tablet handle.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
bool is_open() const override
Check whether the device is open.
PenTablet(PenTablet &&other) noexcept
Move construct a pen tablet handle.
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
PenTablet & operator=(const PenTablet &)=delete
Copy assignment is disabled because the handle owns device lifetime.
const DeviceProfile & profile() const override
Get the profile used to create this device.
PenTablet & operator=(PenTablet &&other) noexcept
Move assign a pen tablet handle.
PenTablet(detail::RuntimeConstructionToken token, std::shared_ptr< detail::PenTabletDevice > device)
Construct a pen tablet handle for Runtime-owned state.
OperationStatus button(PenButton button, bool pressed)
Submit a tablet button transition.
std::size_t submit_count() const
Get the number of successful submit operations.
PenToolState last_submitted_tool() const
Get the most recently submitted tool state.
Runtime that owns backend state and creates virtual devices.
Definition runtime.hpp:939
static std::unique_ptr< Runtime > create(RuntimeOptions options={})
Create a runtime with the requested options.
Runtime(detail::RuntimeConstructionToken token, RuntimeOptions options)
Construct a runtime for Runtime::create.
GamepadCreationResult create_gamepad(const CreateGamepadOptions &options)
Create a gamepad from full creation options.
GamepadCreationResult create_gamepad(const DeviceProfile &profile)
Create a gamepad from a profile.
KeyboardCreationResult create_keyboard(const CreateKeyboardOptions &options)
Create a keyboard from full creation options.
void close_all()
Close every device owned by the runtime.
TouchscreenCreationResult create_touchscreen(const CreateTouchscreenOptions &options)
Create a touchscreen from full creation options.
const BackendCapabilities & capabilities() const
Get capabilities for the selected backend.
TrackpadCreationResult create_trackpad()
Create a trackpad with the built-in trackpad profile.
Runtime(Runtime &&other) noexcept
Move construct a runtime.
TouchscreenCreationResult create_touchscreen()
Create a touchscreen with the built-in touchscreen profile.
Runtime & operator=(Runtime &&other) noexcept
Move assign a runtime.
std::size_t active_device_count() const
Get the number of open devices owned by the runtime.
MouseCreationResult create_mouse()
Create a mouse with the built-in mouse profile.
TrackpadCreationResult create_trackpad(const CreateTrackpadOptions &options)
Create a trackpad from full creation options.
KeyboardCreationResult create_keyboard()
Create a keyboard with the built-in keyboard profile.
~Runtime()
Destroy the runtime and close any remaining devices.
Runtime(const Runtime &)=delete
Copy construction is disabled because the runtime owns backend state.
PenTabletCreationResult create_pen_tablet()
Create a pen tablet with the built-in pen tablet profile.
Runtime & operator=(const Runtime &)=delete
Copy assignment is disabled because the runtime owns backend state.
PenTabletCreationResult create_pen_tablet(const CreatePenTabletOptions &options)
Create a pen tablet from full creation options.
BackendKind backend_kind() const
Get the backend kind used by this runtime.
MouseCreationResult create_mouse(const CreateMouseOptions &options)
Create a mouse from full creation options.
Virtual touchscreen device handle.
Definition runtime.hpp:474
Touchscreen(Touchscreen &&other) noexcept
Move construct a touchscreen handle.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
OperationStatus close() override
Close the virtual device.
Touchscreen & operator=(Touchscreen &&other) noexcept
Move assign a touchscreen handle.
Touchscreen & operator=(const Touchscreen &)=delete
Copy assignment is disabled because the handle owns device lifetime.
~Touchscreen() override
Destroy the touchscreen handle.
std::size_t submit_count() const
Get the number of successful submit operations.
OperationStatus place_contact(const TouchContact &contact)
Place or move a touch contact.
bool is_open() const override
Check whether the device is open.
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
OperationStatus release_contact(std::int32_t contact_id)
Release a touch contact.
Touchscreen(const Touchscreen &)=delete
Copy construction is disabled because the handle owns device lifetime.
Touchscreen(detail::RuntimeConstructionToken token, std::shared_ptr< detail::TouchscreenDevice > device)
Construct a touchscreen handle for Runtime-owned state.
TouchContact last_submitted_contact() const
Get the most recently submitted touch contact.
const DeviceProfile & profile() const override
Get the profile used to create this device.
Virtual trackpad device handle.
Definition runtime.hpp:578
OperationStatus button(bool pressed)
Submit a physical trackpad button transition.
Trackpad & operator=(const Trackpad &)=delete
Copy assignment is disabled because the handle owns device lifetime.
bool is_open() const override
Check whether the device is open.
OperationStatus release_contact(std::int32_t contact_id)
Release a trackpad contact.
Trackpad(detail::RuntimeConstructionToken token, std::shared_ptr< detail::TrackpadDevice > device)
Construct a trackpad handle for Runtime-owned state.
std::size_t submit_count() const
Get the number of successful submit operations.
OperationStatus place_contact(const TouchContact &contact)
Place or move a trackpad contact.
Trackpad(Trackpad &&other) noexcept
Move construct a trackpad handle.
~Trackpad() override
Destroy the trackpad handle.
TouchContact last_submitted_contact() const
Get the most recently submitted touch contact.
OperationStatus close() override
Close the virtual device.
Trackpad & operator=(Trackpad &&other) noexcept
Move assign a trackpad handle.
const DeviceProfile & profile() const override
Get the profile used to create this device.
std::vector< DeviceNode > device_nodes() const override
Get platform-visible nodes associated with the device.
DeviceId device_id() const override
Get the device identifier assigned by the runtime.
Trackpad(const Trackpad &)=delete
Copy construction is disabled because the handle owns device lifetime.
Common interface for virtual device handles.
Definition runtime.hpp:42
virtual DeviceId device_id() const =0
Get the device identifier assigned by the runtime.
virtual OperationStatus close()=0
Close the virtual device.
virtual std::vector< DeviceNode > device_nodes() const =0
Get platform-visible nodes associated with the device.
virtual ~VirtualDevice()=default
Destroy the virtual device handle.
virtual const DeviceProfile & profile() const =0
Get the profile used to create this device.
virtual bool is_open() const =0
Check whether the device is open.
Public libvirtualhid API namespace.
Definition profiles.hpp:14
std::uint16_t KeyboardKeyCode
Keyboard key code accepted by the keyboard event model.
Definition types.hpp:701
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
MouseButton
Mouse buttons accepted by the mouse event model.
Definition types.hpp:731
@ button
Mouse button transition.
@ other
Other platform-specific device path.
BackendKind
Backend implementation selection.
Definition types.hpp:101
@ y
North face button.
@ x
West face button.
PenButton
Pen tablet buttons.
Definition types.hpp:841
Feature set exposed by the selected backend.
Definition types.hpp:119
Full gamepad creation request.
Definition types.hpp:398
Full keyboard creation request.
Definition types.hpp:413
Full mouse creation request.
Definition types.hpp:433
Full pen tablet creation request.
Definition types.hpp:478
Full touchscreen creation request.
Definition types.hpp:448
Full trackpad creation request.
Definition types.hpp:463
Descriptor and identity data used to create a virtual device.
Definition types.hpp:273
Result returned by gamepad creation.
Definition runtime.hpp:795
OperationStatus status
Creation status.
Definition runtime.hpp:799
std::unique_ptr< Gamepad > gamepad
Created gamepad handle when creation succeeds.
Definition runtime.hpp:804
Consumer-provided metadata for a gamepad device.
Definition types.hpp:353
Normalized gamepad output event delivered to the consumer.
Definition types.hpp:900
Common gamepad input state accepted by libvirtualhid.
Definition types.hpp:647
Result returned by keyboard creation.
Definition runtime.hpp:819
OperationStatus status
Creation status.
Definition runtime.hpp:823
std::unique_ptr< Keyboard > keyboard
Created keyboard handle when creation succeeds.
Definition runtime.hpp:828
Keyboard key transition.
Definition types.hpp:706
UTF-8 text input request.
Definition types.hpp:721
Result returned by mouse creation.
Definition runtime.hpp:843
OperationStatus status
Creation status.
Definition runtime.hpp:847
std::unique_ptr< Mouse > mouse
Created mouse handle when creation succeeds.
Definition runtime.hpp:852
Mouse input event.
Definition types.hpp:753
Result returned by pen tablet creation.
Definition runtime.hpp:915
OperationStatus status
Creation status.
Definition runtime.hpp:919
std::unique_ptr< PenTablet > pen_tablet
Created pen tablet handle when creation succeeds.
Definition runtime.hpp:924
Pen tablet tool position and analog state.
Definition types.hpp:850
Runtime creation options.
Definition types.hpp:109
Touch contact event for touchscreen and trackpad devices.
Definition types.hpp:798
Result returned by touchscreen creation.
Definition runtime.hpp:867
std::unique_ptr< Touchscreen > touchscreen
Created touchscreen handle when creation succeeds.
Definition runtime.hpp:876
OperationStatus status
Creation status.
Definition runtime.hpp:871
Result returned by trackpad creation.
Definition runtime.hpp:891
OperationStatus status
Creation status.
Definition runtime.hpp:895
std::unique_ptr< Trackpad > trackpad
Created trackpad handle when creation succeeds.
Definition runtime.hpp:900
Token used by Runtime to construct runtime-owned handles.
Definition runtime.hpp:23
Core public types for libvirtualhid.