SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_hidapi.h
1#ifndef SDL3PP_HIDAPI_H_
2#define SDL3PP_HIDAPI_H_
3
4#include <SDL3/SDL_hidapi.h>
5#include "SDL3pp_error.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
41// Forward decl
42struct HidDevice;
43
45using HidDeviceRaw = SDL_hid_device*;
46
47// Forward decl
48struct HidDeviceRef;
49
52{
54
57 : value(value)
58 {
59 }
60
62 constexpr HidDeviceParam(std::nullptr_t = nullptr)
63 : value(nullptr)
64 {
65 }
66
68 constexpr explicit operator bool() const { return !!value; }
69
71 constexpr auto operator<=>(const HidDeviceParam& other) const = default;
72
74 constexpr operator HidDeviceRaw() const { return value; }
75};
76
82using hid_bus_type = SDL_hid_bus_type;
83
85 SDL_HID_API_BUS_UNKNOWN;
86
92constexpr hid_bus_type HID_API_BUS_USB = SDL_HID_API_BUS_USB;
93
101constexpr hid_bus_type HID_API_BUS_BLUETOOTH = SDL_HID_API_BUS_BLUETOOTH;
102
108constexpr hid_bus_type HID_API_BUS_I2C = SDL_HID_API_BUS_I2C;
109
115constexpr hid_bus_type HID_API_BUS_SPI = SDL_HID_API_BUS_SPI;
116
122using hid_device_info = SDL_hid_device_info;
123
132{
133 HidDeviceRaw m_resource = nullptr;
134
135public:
137 constexpr HidDevice(std::nullptr_t = nullptr) noexcept
138 : m_resource(0)
139 {
140 }
141
149 constexpr explicit HidDevice(const HidDeviceRaw resource) noexcept
150 : m_resource(resource)
151 {
152 }
153
154protected:
156 constexpr HidDevice(const HidDevice& other) noexcept = default;
157
158public:
160 constexpr HidDevice(HidDevice&& other) noexcept
161 : HidDevice(other.release())
162 {
163 }
164
165 constexpr HidDevice(const HidDeviceRef& other) = delete;
166
167 constexpr HidDevice(HidDeviceRef&& other) = delete;
168
184 HidDevice(unsigned short vendor_id,
185 unsigned short product_id,
186 const wchar_t* serial_number)
187 : m_resource(CheckError(SDL_hid_open(vendor_id, product_id, serial_number)))
188 {
189 }
190
203 : m_resource(CheckError(SDL_hid_open_path(path)))
204 {
205 }
206
208 ~HidDevice() { SDL_hid_close(m_resource); }
209
211 constexpr HidDevice& operator=(HidDevice&& other) noexcept
212 {
213 std::swap(m_resource, other.m_resource);
214 return *this;
215 }
216
217protected:
219 constexpr HidDevice& operator=(const HidDevice& other) noexcept = default;
220
221public:
223 constexpr HidDeviceRaw get() const noexcept { return m_resource; }
224
226 constexpr HidDeviceRaw release() noexcept
227 {
228 auto r = m_resource;
229 m_resource = nullptr;
230 return r;
231 }
232
234 constexpr auto operator<=>(const HidDevice& other) const noexcept = default;
235
237 constexpr explicit operator bool() const noexcept { return !!m_resource; }
238
240 constexpr operator HidDeviceParam() const noexcept { return {m_resource}; }
241
249 void close();
250
251#if SDL_VERSION_ATLEAST(3, 4, 0)
252
267
268#endif // SDL_VERSION_ATLEAST(3, 4, 0)
269
293 int write(SourceBytes data);
294
310 int read_timeout(TargetBytes data, Milliseconds timeout);
311
326 int read(TargetBytes data);
327
344 void set_nonblocking(bool nonblock);
345
367
387
407
417 void get_manufacturer_string(wchar_t* string, size_t maxlen);
418
428 void get_product_string(wchar_t* string, size_t maxlen);
429
439 void get_serial_number_string(wchar_t* string, size_t maxlen);
440
451 void get_indexed_string(int string_index, wchar_t* string, size_t maxlen);
452
464
478};
479
482{
484
492 HidDeviceRef(HidDeviceParam resource) noexcept
493 : HidDevice(resource.value)
494 {
495 }
496
504 HidDeviceRef(HidDeviceRaw resource) noexcept
505 : HidDevice(resource)
506 {
507 }
508
510 constexpr HidDeviceRef(const HidDeviceRef& other) noexcept = default;
511
514};
515
533inline void hid_init() { CheckErrorIfNot(SDL_hid_init(), 0); }
534
547inline void hid_exit() { CheckErrorIfNot(SDL_hid_exit(), 0); }
548
569{
570 return SDL_hid_device_change_count();
571}
572
599inline hid_device_info* hid_enumerate(unsigned short vendor_id,
600 unsigned short product_id)
601{
602 return CheckError(SDL_hid_enumerate(vendor_id, product_id));
603}
604
615{
616 SDL_hid_free_enumeration(devs);
617}
618
635inline HidDevice hid_open(unsigned short vendor_id,
636 unsigned short product_id,
637 const wchar_t* serial_number)
638{
639 return HidDevice(vendor_id, product_id, serial_number);
640}
641
655{
656 return HidDevice(std::move(path));
657}
658
659#if SDL_VERSION_ATLEAST(3, 4, 0)
660
676{
677 return CheckError(SDL_hid_get_properties(dev));
678}
679
681{
682 return SDL::hid_get_properties(m_resource);
683}
684
685namespace prop::Hidapi {
686
687constexpr auto LIBUSB_DEVICE_HANDLE_POINTER =
688 SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER;
689
690} // namespace prop::Hidapi
691
692#endif // SDL_VERSION_ATLEAST(3, 4, 0)
693
717{
718 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
719}
720
722{
723 return SDL::hid_write(m_resource, std::move(data));
724}
725
743 TargetBytes data,
744 Milliseconds timeout)
745{
746 return SDL_hid_read_timeout(
747 dev, data.data_as<Uint8>(), data.size_bytes(), timeout.count());
748}
749
751{
752 return SDL::hid_read_timeout(m_resource, std::move(data), timeout);
753}
754
771{
772 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
773}
774
776{
777 return SDL::hid_read(m_resource, std::move(data));
778}
779
796inline void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
797{
798 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
799}
800
801inline void HidDevice::set_nonblocking(bool nonblock)
802{
803 SDL::hid_set_nonblocking(m_resource, nonblock);
804}
805
827{
828 return SDL_hid_send_feature_report(
829 dev, data.data_as<Uint8>(), data.size_bytes());
830}
831
833{
834 return SDL::hid_send_feature_report(m_resource, std::move(data));
835}
836
857{
858 return SDL_hid_get_feature_report(
859 dev, data.data_as<Uint8>(), data.size_bytes());
860}
861
863{
864 return SDL::hid_get_feature_report(m_resource, std::move(data));
865}
866
887{
888 return SDL_hid_get_input_report(
889 dev, data.data_as<Uint8>(), data.size_bytes());
890}
891
893{
894 return SDL::hid_get_input_report(m_resource, std::move(data));
895}
896
905inline void hid_close(HidDeviceRaw dev)
906{
907 CheckErrorIfNot(SDL_hid_close(dev), 0);
908}
909
910inline void HidDevice::close() { hid_close(release()); }
911
923 wchar_t* string,
924 size_t maxlen)
925{
926 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
927}
928
929inline void HidDevice::get_manufacturer_string(wchar_t* string, size_t maxlen)
930{
931 SDL::hid_get_manufacturer_string(m_resource, string, maxlen);
932}
933
945 wchar_t* string,
946 size_t maxlen)
947{
948 CheckErrorIfNot(SDL_hid_get_product_string(dev, string, maxlen), 0);
949}
950
951inline void HidDevice::get_product_string(wchar_t* string, size_t maxlen)
952{
953 SDL::hid_get_product_string(m_resource, string, maxlen);
954}
955
967 wchar_t* string,
968 size_t maxlen)
969{
970 CheckErrorIfNot(SDL_hid_get_serial_number_string(dev, string, maxlen), 0);
971}
972
973inline void HidDevice::get_serial_number_string(wchar_t* string, size_t maxlen)
974{
975 SDL::hid_get_serial_number_string(m_resource, string, maxlen);
976}
977
990 int string_index,
991 wchar_t* string,
992 size_t maxlen)
993{
994 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
995 0);
996}
997
998inline void HidDevice::get_indexed_string(int string_index,
999 wchar_t* string,
1000 size_t maxlen)
1001{
1002 SDL::hid_get_indexed_string(m_resource, string_index, string, maxlen);
1003}
1004
1017{
1018 return CheckError(SDL_hid_get_device_info(dev));
1019}
1020
1022{
1023 return SDL::hid_get_device_info(m_resource);
1024}
1025
1040{
1041 return SDL_hid_get_report_descriptor(
1042 dev, buf.data_as<Uint8>(), buf.size_bytes());
1043}
1044
1046{
1047 return SDL::hid_get_report_descriptor(m_resource, std::move(buf));
1048}
1049
1057inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
1058
1060
1061} // namespace SDL
1062
1063#endif /* SDL3PP_HIDAPI_H_ */
An opaque handle representing an open HID device.
Definition: SDL3pp_hidapi.h:132
constexpr auto operator<=>(const HidDevice &other) const noexcept=default
Comparison.
constexpr HidDeviceRaw release() noexcept
Retrieves underlying HidDeviceRaw and clear this.
Definition: SDL3pp_hidapi.h:226
HidDevice(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:202
HidDevice(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
Definition: SDL3pp_hidapi.h:184
constexpr HidDevice(const HidDeviceRaw resource) noexcept
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:149
constexpr HidDeviceRaw get() const noexcept
Retrieves underlying HidDeviceRaw.
Definition: SDL3pp_hidapi.h:223
~HidDevice()
Destructor.
Definition: SDL3pp_hidapi.h:208
constexpr HidDevice(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_hidapi.h:137
constexpr HidDevice & operator=(HidDevice &&other) noexcept
Assignment operator.
Definition: SDL3pp_hidapi.h:211
constexpr HidDevice(HidDevice &&other) noexcept
Move constructor.
Definition: SDL3pp_hidapi.h:160
constexpr HidDevice(const HidDevice &other) noexcept=default
Copy constructor.
constexpr HidDevice & operator=(const HidDevice &other) noexcept=default
Assignment operator.
Source byte stream.
Definition: SDL3pp_strings.h:240
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:304
constexpr const T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:314
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
Target byte stream.
Definition: SDL3pp_strings.h:326
constexpr T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:416
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:409
constexpr void CheckErrorIfNot(T result, T validValue)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:244
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
int hid_get_feature_report(HidDeviceParam dev, TargetBytes data)
Get a feature report from a HID device.
Definition: SDL3pp_hidapi.h:856
void hid_close(HidDeviceRaw dev)
Close a HID device.
Definition: SDL3pp_hidapi.h:905
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: SDL3pp_hidapi.h:929
PropertiesRef hid_get_properties()
Get the properties associated with an HidDevice.
Definition: SDL3pp_hidapi.h:680
SDL_hid_bus_type hid_bus_type
HID underlying bus types.
Definition: SDL3pp_hidapi.h:82
SDL_hid_device * HidDeviceRaw
Alias to raw representation for HidDevice.
Definition: SDL3pp_hidapi.h:45
hid_device_info * hid_get_device_info(HidDeviceParam dev)
Get the device info from a HID device.
Definition: SDL3pp_hidapi.h:1016
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:801
hid_device_info * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: SDL3pp_hidapi.h:599
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition: SDL3pp_hidapi.h:862
int read(TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:775
SDL_hid_device_info hid_device_info
Information about a connected HID device.
Definition: SDL3pp_hidapi.h:122
hid_device_info * get_device_info()
Get the device info from a HID device.
Definition: SDL3pp_hidapi.h:1021
constexpr hid_bus_type HID_API_BUS_I2C
I2C bus Specifications:
Definition: SDL3pp_hidapi.h:108
int hid_get_report_descriptor(HidDeviceParam dev, TargetBytes buf)
Get a report descriptor from a HID device.
Definition: SDL3pp_hidapi.h:1039
void hid_exit()
Finalize the HIDAPI library.
Definition: SDL3pp_hidapi.h:547
void get_product_string(wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: SDL3pp_hidapi.h:951
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:832
int hid_write(HidDeviceParam dev, SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:716
int read_timeout(TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:750
int hid_read(HidDeviceParam dev, TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:770
int get_input_report(TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:892
void close()
Close a HID device.
Definition: SDL3pp_hidapi.h:910
PropertiesRef hid_get_properties(HidDeviceParam dev)
Get the properties associated with an HidDevice.
Definition: SDL3pp_hidapi.h:675
constexpr hid_bus_type HID_API_BUS_UNKNOWN
Unknown bus type.
Definition: SDL3pp_hidapi.h:84
Uint32 hid_device_change_count()
Check to see if devices may have been added or removed.
Definition: SDL3pp_hidapi.h:568
int hid_send_feature_report(HidDeviceParam dev, SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:826
HidDevice hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
Definition: SDL3pp_hidapi.h:635
void get_indexed_string(int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
Definition: SDL3pp_hidapi.h:998
void hid_free_enumeration(hid_device_info *devs)
Free an enumeration linked list.
Definition: SDL3pp_hidapi.h:614
void hid_get_product_string(HidDeviceParam dev, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: SDL3pp_hidapi.h:944
void hid_ble_scan(bool active)
Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.
Definition: SDL3pp_hidapi.h:1057
void get_serial_number_string(wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: SDL3pp_hidapi.h:973
void hid_get_serial_number_string(HidDeviceParam dev, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: SDL3pp_hidapi.h:966
int write(SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:721
void hid_init()
Initialize the HIDAPI library.
Definition: SDL3pp_hidapi.h:533
int hid_read_timeout(HidDeviceParam dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:742
constexpr hid_bus_type HID_API_BUS_BLUETOOTH
Bluetooth or Bluetooth LE bus Specifications:
Definition: SDL3pp_hidapi.h:101
constexpr hid_bus_type HID_API_BUS_USB
USB bus Specifications:
Definition: SDL3pp_hidapi.h:92
constexpr hid_bus_type HID_API_BUS_SPI
SPI bus Specifications:
Definition: SDL3pp_hidapi.h:115
void hid_get_indexed_string(HidDeviceParam dev, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
Definition: SDL3pp_hidapi.h:989
int hid_get_input_report(HidDeviceParam dev, TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:886
HidDevice hid_open_path(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:654
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition: SDL3pp_hidapi.h:1045
void hid_get_manufacturer_string(HidDeviceParam dev, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: SDL3pp_hidapi.h:922
void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:796
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition: SDL3pp_stdinc.h:386
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:289
Main include header for the SDL3pp library.
Safely wrap HidDevice for non owning parameters.
Definition: SDL3pp_hidapi.h:52
constexpr HidDeviceParam(HidDeviceRaw value)
Constructs from HidDeviceRaw.
Definition: SDL3pp_hidapi.h:56
HidDeviceRaw value
parameter's HidDeviceRaw
Definition: SDL3pp_hidapi.h:53
constexpr auto operator<=>(const HidDeviceParam &other) const =default
Comparison.
constexpr HidDeviceParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_hidapi.h:62
Semi-safe reference for HidDevice.
Definition: SDL3pp_hidapi.h:482
~HidDeviceRef()
Destructor.
Definition: SDL3pp_hidapi.h:513
constexpr HidDeviceRef(const HidDeviceRef &other) noexcept=default
Copy constructor.
HidDeviceRef(HidDeviceParam resource) noexcept
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:492
HidDeviceRef(HidDeviceRaw resource) noexcept
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:504
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716