libvirtualhid 10
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 namespace detail {
18 struct GamepadDevice;
19 struct KeyboardDevice;
20 struct MouseDevice;
21 struct TouchscreenDevice;
22 struct TrackpadDevice;
23 struct PenTabletDevice;
24 class RuntimeState;
25 } // namespace detail
26
31 public:
35 virtual ~VirtualDevice() = default;
36
42 virtual DeviceId device_id() const = 0;
43
49 virtual const DeviceProfile &profile() const = 0;
50
56 virtual bool is_open() const = 0;
57
63 virtual std::vector<DeviceNode> device_nodes() const = 0;
64
70 virtual OperationStatus close() = 0;
71 };
72
76 class Gamepad final: public VirtualDevice {
77 public:
81 Gamepad(const Gamepad &) = delete;
82
88 Gamepad &operator=(const Gamepad &) = delete;
89
95 Gamepad(Gamepad &&other) noexcept;
96
104
108 ~Gamepad() override;
109
113 DeviceId device_id() const override;
114
118 const DeviceProfile &profile() const override;
119
125 const GamepadMetadata &metadata() const;
126
130 bool is_open() const override;
131
135 std::vector<DeviceNode> device_nodes() const override;
136
141
149
156
164
171
177 std::vector<std::uint8_t> last_input_report() const;
178
184 std::size_t submit_count() const;
185
186 private:
187 friend class Runtime;
188
189 explicit Gamepad(std::shared_ptr<detail::GamepadDevice> device);
190
191 std::shared_ptr<detail::GamepadDevice> device_;
192 };
193
197 class Keyboard final: public VirtualDevice {
198 public:
202 Keyboard(const Keyboard &) = delete;
203
209 Keyboard &operator=(const Keyboard &) = delete;
210
217
225
229 ~Keyboard() override;
230
234 DeviceId device_id() const override;
235
239 const DeviceProfile &profile() const override;
240
244 bool is_open() const override;
245
249 std::vector<DeviceNode> device_nodes() const override;
250
255
263
271
279
287
294
300 std::size_t submit_count() const;
301
302 private:
303 friend class Runtime;
304
305 explicit Keyboard(std::shared_ptr<detail::KeyboardDevice> device);
306
307 std::shared_ptr<detail::KeyboardDevice> device_;
308 };
309
313 class Mouse final: public VirtualDevice {
314 public:
318 Mouse(const Mouse &) = delete;
319
325 Mouse &operator=(const Mouse &) = delete;
326
332 Mouse(Mouse &&other) noexcept;
333
341
345 ~Mouse() override;
346
350 DeviceId device_id() const override;
351
355 const DeviceProfile &profile() const override;
356
360 bool is_open() const override;
361
365 std::vector<DeviceNode> device_nodes() const override;
366
371
379
387 OperationStatus move_relative(std::int32_t delta_x, std::int32_t delta_y);
388
398 OperationStatus move_absolute(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
399
408
415 OperationStatus vertical_scroll(std::int32_t distance);
416
423 OperationStatus horizontal_scroll(std::int32_t distance);
424
431
437 std::size_t submit_count() const;
438
439 private:
440 friend class Runtime;
441
442 explicit Mouse(std::shared_ptr<detail::MouseDevice> device);
443
444 std::shared_ptr<detail::MouseDevice> device_;
445 };
446
450 class Touchscreen final: public VirtualDevice {
451 public:
455 Touchscreen(const Touchscreen &) = delete;
456
463
470
478
482 ~Touchscreen() override;
483
487 DeviceId device_id() const override;
488
492 const DeviceProfile &profile() const override;
493
497 bool is_open() const override;
498
502 std::vector<DeviceNode> device_nodes() const override;
503
508
516
523 OperationStatus release_contact(std::int32_t contact_id);
524
531
537 std::size_t submit_count() const;
538
539 private:
540 friend class Runtime;
541
542 explicit Touchscreen(std::shared_ptr<detail::TouchscreenDevice> device);
543
544 std::shared_ptr<detail::TouchscreenDevice> device_;
545 };
546
550 class Trackpad final: public VirtualDevice {
551 public:
555 Trackpad(const Trackpad &) = delete;
556
562 Trackpad &operator=(const Trackpad &) = delete;
563
570
578
582 ~Trackpad() override;
583
587 DeviceId device_id() const override;
588
592 const DeviceProfile &profile() const override;
593
597 bool is_open() const override;
598
602 std::vector<DeviceNode> device_nodes() const override;
603
608
616
623 OperationStatus release_contact(std::int32_t contact_id);
624
631 OperationStatus button(bool pressed);
632
639
645 std::size_t submit_count() const;
646
647 private:
648 friend class Runtime;
649
650 explicit Trackpad(std::shared_ptr<detail::TrackpadDevice> device);
651
652 std::shared_ptr<detail::TrackpadDevice> device_;
653 };
654
658 class PenTablet final: public VirtualDevice {
659 public:
663 PenTablet(const PenTablet &) = delete;
664
670 PenTablet &operator=(const PenTablet &) = delete;
671
678
686
690 ~PenTablet() override;
691
695 DeviceId device_id() const override;
696
700 const DeviceProfile &profile() const override;
701
705 bool is_open() const override;
706
710 std::vector<DeviceNode> device_nodes() const override;
711
716
724
733
740
746 std::size_t submit_count() const;
747
748 private:
749 friend class Runtime;
750
751 explicit PenTablet(std::shared_ptr<detail::PenTabletDevice> device);
752
753 std::shared_ptr<detail::PenTabletDevice> device_;
754 };
755
764
768 std::unique_ptr<Gamepad> gamepad;
769
775 explicit operator bool() const {
776 return status.ok() && gamepad != nullptr;
777 }
778 };
779
788
792 std::unique_ptr<Keyboard> keyboard;
793
799 explicit operator bool() const {
800 return status.ok() && keyboard != nullptr;
801 }
802 };
803
812
816 std::unique_ptr<Mouse> mouse;
817
823 explicit operator bool() const {
824 return status.ok() && mouse != nullptr;
825 }
826 };
827
836
840 std::unique_ptr<Touchscreen> touchscreen;
841
847 explicit operator bool() const {
848 return status.ok() && touchscreen != nullptr;
849 }
850 };
851
860
864 std::unique_ptr<Trackpad> trackpad;
865
871 explicit operator bool() const {
872 return status.ok() && trackpad != nullptr;
873 }
874 };
875
884
888 std::unique_ptr<PenTablet> pen_tablet;
889
895 explicit operator bool() const {
896 return status.ok() && pen_tablet != nullptr;
897 }
898 };
899
1067
1068} // namespace lvh
Virtual gamepad device handle.
Definition runtime.hpp:76
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.
void set_output_callback(OutputCallback callback)
Register a callback for backend output events.
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.
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.
Virtual keyboard device handle.
Definition runtime.hpp:197
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.
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:313
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.
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:658
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.
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:903
static std::unique_ptr< Runtime > create(RuntimeOptions options={})
Create a runtime with the requested options.
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:450
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.
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:550
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.
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:30
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:759
OperationStatus status
Creation status.
Definition runtime.hpp:763
std::unique_ptr< Gamepad > gamepad
Created gamepad handle when creation succeeds.
Definition runtime.hpp:768
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:783
OperationStatus status
Creation status.
Definition runtime.hpp:787
std::unique_ptr< Keyboard > keyboard
Created keyboard handle when creation succeeds.
Definition runtime.hpp:792
Keyboard key transition.
Definition types.hpp:706
UTF-8 text input request.
Definition types.hpp:721
Result returned by mouse creation.
Definition runtime.hpp:807
OperationStatus status
Creation status.
Definition runtime.hpp:811
std::unique_ptr< Mouse > mouse
Created mouse handle when creation succeeds.
Definition runtime.hpp:816
Mouse input event.
Definition types.hpp:753
Result returned by pen tablet creation.
Definition runtime.hpp:879
OperationStatus status
Creation status.
Definition runtime.hpp:883
std::unique_ptr< PenTablet > pen_tablet
Created pen tablet handle when creation succeeds.
Definition runtime.hpp:888
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:831
std::unique_ptr< Touchscreen > touchscreen
Created touchscreen handle when creation succeeds.
Definition runtime.hpp:840
OperationStatus status
Creation status.
Definition runtime.hpp:835
Result returned by trackpad creation.
Definition runtime.hpp:855
OperationStatus status
Creation status.
Definition runtime.hpp:859
std::unique_ptr< Trackpad > trackpad
Created trackpad handle when creation succeeds.
Definition runtime.hpp:864
Core public types for libvirtualhid.