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() = default;
138
146 constexpr explicit HidDevice(const HidDeviceRaw resource)
147 : m_resource(resource)
148 {
149 }
150
152 constexpr HidDevice(const HidDevice& other) = delete;
153
155 constexpr HidDevice(HidDevice&& other)
156 : HidDevice(other.release())
157 {
158 }
159
160 constexpr HidDevice(const HidDeviceRef& other) = delete;
161
162 constexpr HidDevice(HidDeviceRef&& other) = delete;
163
179 HidDevice(unsigned short vendor_id,
180 unsigned short product_id,
181 const wchar_t* serial_number)
182 : m_resource(CheckError(SDL_hid_open(vendor_id, product_id, serial_number)))
183 {
184 }
185
198 : m_resource(CheckError(SDL_hid_open_path(path)))
199 {
200 }
201
203 ~HidDevice() { SDL_hid_close(m_resource); }
204
207 {
208 std::swap(m_resource, other.m_resource);
209 return *this;
210 }
211
213 constexpr HidDeviceRaw get() const { return m_resource; }
214
217 {
218 auto r = m_resource;
219 m_resource = nullptr;
220 return r;
221 }
222
224 constexpr auto operator<=>(const HidDevice& other) const = default;
225
227 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
228
230 constexpr explicit operator bool() const { return !!m_resource; }
231
233 constexpr operator HidDeviceParam() const { return {m_resource}; }
234
242 void close();
243
267 int write(SourceBytes data);
268
284 int read_timeout(TargetBytes data, Milliseconds timeout);
285
300 int read(TargetBytes data);
301
318 void set_nonblocking(bool nonblock);
319
341
361
381
391 void get_manufacturer_string(wchar_t* string, size_t maxlen);
392
402 void get_product_string(wchar_t* string, size_t maxlen);
403
413 void get_serial_number_string(wchar_t* string, size_t maxlen);
414
425 void get_indexed_string(int string_index, wchar_t* string, size_t maxlen);
426
438
452};
453
456{
465 : HidDevice(resource.value)
466 {
467 }
468
471 : HidDevice(other.get())
472 {
473 }
474
477};
478
496inline void hid_init() { CheckErrorIfNot(SDL_hid_init(), 0); }
497
510inline void hid_exit() { CheckErrorIfNot(SDL_hid_exit(), 0); }
511
532{
533 return SDL_hid_device_change_count();
534}
535
562inline hid_device_info* hid_enumerate(unsigned short vendor_id,
563 unsigned short product_id)
564{
565 return CheckError(SDL_hid_enumerate(vendor_id, product_id));
566}
567
578{
579 SDL_hid_free_enumeration(devs);
580}
581
598inline HidDevice hid_open(unsigned short vendor_id,
599 unsigned short product_id,
600 const wchar_t* serial_number)
601{
602 return HidDevice(vendor_id, product_id, serial_number);
603}
604
618{
619 return HidDevice(std::move(path));
620}
621
645{
646 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
647}
648
650{
651 return SDL::hid_write(m_resource, std::move(data));
652}
653
671 TargetBytes data,
672 Milliseconds timeout)
673{
674 return SDL_hid_read_timeout(
675 dev, data.data_as<Uint8>(), data.size_bytes(), timeout.count());
676}
677
679{
680 return SDL::hid_read_timeout(m_resource, std::move(data), timeout);
681}
682
699{
700 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
701}
702
704{
705 return SDL::hid_read(m_resource, std::move(data));
706}
707
724inline void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
725{
726 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
727}
728
729inline void HidDevice::set_nonblocking(bool nonblock)
730{
731 SDL::hid_set_nonblocking(m_resource, nonblock);
732}
733
755{
756 return SDL_hid_send_feature_report(
757 dev, data.data_as<Uint8>(), data.size_bytes());
758}
759
761{
762 return SDL::hid_send_feature_report(m_resource, std::move(data));
763}
764
785{
786 return SDL_hid_get_feature_report(
787 dev, data.data_as<Uint8>(), data.size_bytes());
788}
789
791{
792 return SDL::hid_get_feature_report(m_resource, std::move(data));
793}
794
815{
816 return SDL_hid_get_input_report(
817 dev, data.data_as<Uint8>(), data.size_bytes());
818}
819
821{
822 return SDL::hid_get_input_report(m_resource, std::move(data));
823}
824
833inline void hid_close(HidDeviceRaw dev)
834{
835 CheckErrorIfNot(SDL_hid_close(dev), 0);
836}
837
838inline void HidDevice::close() { hid_close(release()); }
839
851 wchar_t* string,
852 size_t maxlen)
853{
854 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
855}
856
857inline void HidDevice::get_manufacturer_string(wchar_t* string, size_t maxlen)
858{
859 SDL::hid_get_manufacturer_string(m_resource, string, maxlen);
860}
861
873 wchar_t* string,
874 size_t maxlen)
875{
876 CheckErrorIfNot(SDL_hid_get_product_string(dev, string, maxlen), 0);
877}
878
879inline void HidDevice::get_product_string(wchar_t* string, size_t maxlen)
880{
881 SDL::hid_get_product_string(m_resource, string, maxlen);
882}
883
895 wchar_t* string,
896 size_t maxlen)
897{
898 CheckErrorIfNot(SDL_hid_get_serial_number_string(dev, string, maxlen), 0);
899}
900
901inline void HidDevice::get_serial_number_string(wchar_t* string, size_t maxlen)
902{
903 SDL::hid_get_serial_number_string(m_resource, string, maxlen);
904}
905
918 int string_index,
919 wchar_t* string,
920 size_t maxlen)
921{
922 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
923 0);
924}
925
926inline void HidDevice::get_indexed_string(int string_index,
927 wchar_t* string,
928 size_t maxlen)
929{
930 SDL::hid_get_indexed_string(m_resource, string_index, string, maxlen);
931}
932
945{
946 return CheckError(SDL_hid_get_device_info(dev));
947}
948
950{
951 return SDL::hid_get_device_info(m_resource);
952}
953
968{
969 return SDL_hid_get_report_descriptor(
970 dev, buf.data_as<Uint8>(), buf.size_bytes());
971}
972
974{
975 return SDL::hid_get_report_descriptor(m_resource, std::move(buf));
976}
977
985inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
986
988
989} // namespace SDL
990
991#endif /* SDL3PP_HIDAPI_H_ */
An opaque handle representing an open HID device.
Definition: SDL3pp_hidapi.h:132
constexpr HidDevice(const HidDeviceRaw resource)
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:146
constexpr HidDevice(const HidDevice &other)=delete
Copy constructor.
constexpr HidDevice(HidDevice &&other)
Move constructor.
Definition: SDL3pp_hidapi.h:155
HidDevice(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:197
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:179
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_hidapi.h:227
constexpr HidDeviceRaw get() const
Retrieves underlying HidDeviceRaw.
Definition: SDL3pp_hidapi.h:213
constexpr HidDeviceRaw release()
Retrieves underlying HidDeviceRaw and clear this.
Definition: SDL3pp_hidapi.h:216
constexpr auto operator<=>(const HidDevice &other) const =default
Comparison.
~HidDevice()
Destructor.
Definition: SDL3pp_hidapi.h:203
HidDevice & operator=(HidDevice other)
Assignment operator.
Definition: SDL3pp_hidapi.h:206
constexpr HidDevice()=default
Default ctor.
Source byte stream.
Definition: SDL3pp_strings.h:239
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:303
constexpr const T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:313
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
Target byte stream.
Definition: SDL3pp_strings.h:325
constexpr T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:415
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:408
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:784
void hid_close(HidDeviceRaw dev)
Close a HID device.
Definition: SDL3pp_hidapi.h:833
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: SDL3pp_hidapi.h:857
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:944
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:729
hid_device_info * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: SDL3pp_hidapi.h:562
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition: SDL3pp_hidapi.h:790
int read(TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:703
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:949
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:967
void hid_exit()
Finalize the HIDAPI library.
Definition: SDL3pp_hidapi.h:510
void get_product_string(wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: SDL3pp_hidapi.h:879
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:760
int hid_write(HidDeviceParam dev, SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:644
int read_timeout(TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:678
int hid_read(HidDeviceParam dev, TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:698
int get_input_report(TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:820
void close()
Close a HID device.
Definition: SDL3pp_hidapi.h:838
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:531
int hid_send_feature_report(HidDeviceParam dev, SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:754
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:598
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:926
void hid_free_enumeration(hid_device_info *devs)
Free an enumeration linked list.
Definition: SDL3pp_hidapi.h:577
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:872
void hid_ble_scan(bool active)
Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.
Definition: SDL3pp_hidapi.h:985
void get_serial_number_string(wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: SDL3pp_hidapi.h:901
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:894
int write(SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:649
void hid_init()
Initialize the HIDAPI library.
Definition: SDL3pp_hidapi.h:496
int hid_read_timeout(HidDeviceParam dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:670
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:917
int hid_get_input_report(HidDeviceParam dev, TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:814
HidDevice hid_open_path(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:617
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition: SDL3pp_hidapi.h:973
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:850
void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:724
::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
constexpr HidDeviceParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_hidapi.h:62
HidDeviceRaw value
parameter's HidDeviceRaw
Definition: SDL3pp_hidapi.h:53
constexpr auto operator<=>(const HidDeviceParam &other) const =default
Comparison.
Semi-safe reference for HidDevice.
Definition: SDL3pp_hidapi.h:456
HidDeviceRef(const HidDeviceRef &other)
Copy constructor.
Definition: SDL3pp_hidapi.h:470
~HidDeviceRef()
Destructor.
Definition: SDL3pp_hidapi.h:476
HidDeviceRef(HidDeviceParam resource)
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:464