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
42// Forward decl
43struct HidDevice;
44
46using HidDeviceRaw = SDL_hid_device*;
47
48// Forward decl
49struct HidDeviceRef;
50
53{
55
58 : value(value)
59 {
60 }
61
63 constexpr HidDeviceParam(std::nullptr_t _ = nullptr)
64 : value(nullptr)
65 {
66 }
67
69 constexpr explicit operator bool() const { return !!value; }
70
72 constexpr auto operator<=>(const HidDeviceParam& other) const = default;
73
75 constexpr operator HidDeviceRaw() const { return value; }
76};
77
83using hid_bus_type = SDL_hid_bus_type;
84
86 SDL_HID_API_BUS_UNKNOWN;
87
89 SDL_HID_API_BUS_USB;
90
92 SDL_HID_API_BUS_BLUETOOTH;
93
95 SDL_HID_API_BUS_I2C;
96
98 SDL_HID_API_BUS_SPI;
99
105using hid_device_info = SDL_hid_device_info;
106
115{
116 HidDeviceRaw m_resource = nullptr;
117
118public:
120 constexpr HidDevice() = default;
121
129 constexpr explicit HidDevice(const HidDeviceRaw resource)
130 : m_resource(resource)
131 {
132 }
133
135 constexpr HidDevice(const HidDevice& other) = delete;
136
138 constexpr HidDevice(HidDevice&& other)
139 : HidDevice(other.release())
140 {
141 }
142
143 constexpr HidDevice(const HidDeviceRef& other) = delete;
144
145 constexpr HidDevice(HidDeviceRef&& other) = delete;
146
162 HidDevice(unsigned short vendor_id,
163 unsigned short product_id,
164 const wchar_t* serial_number)
165 : m_resource(CheckError(SDL_hid_open(vendor_id, product_id, serial_number)))
166 {
167 }
168
181 : m_resource(CheckError(SDL_hid_open_path(path)))
182 {
183 }
184
186 ~HidDevice() { SDL_hid_close(m_resource); }
187
190 {
191 std::swap(m_resource, other.m_resource);
192 return *this;
193 }
194
196 constexpr HidDeviceRaw get() const { return m_resource; }
197
200 {
201 auto r = m_resource;
202 m_resource = nullptr;
203 return r;
204 }
205
207 constexpr auto operator<=>(const HidDevice& other) const = default;
208
210 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
211
213 constexpr explicit operator bool() const { return !!m_resource; }
214
216 constexpr operator HidDeviceParam() const { return {m_resource}; }
217
225 void close();
226
250 int write(SourceBytes data);
251
267 int read_timeout(TargetBytes data, Milliseconds timeout);
268
284 int read(TargetBytes data);
285
302 void set_nonblocking(bool nonblock);
303
325
345
365
375 void get_manufacturer_string(wchar_t* string, size_t maxlen);
376
386 void get_product_string(wchar_t* string, size_t maxlen);
387
397 void get_serial_number_string(wchar_t* string, size_t maxlen);
398
409 void get_indexed_string(int string_index, wchar_t* string, size_t maxlen);
410
422
436};
437
440{
449 : HidDevice(resource.value)
450 {
451 }
452
455 : HidDevice(other.get())
456 {
457 }
458
461};
462
480inline void hid_init() { CheckErrorIfNot(SDL_hid_init(), 0); }
481
494inline void hid_exit() { CheckErrorIfNot(SDL_hid_exit(), 0); }
495
516{
517 return SDL_hid_device_change_count();
518}
519
546inline hid_device_info* hid_enumerate(unsigned short vendor_id,
547 unsigned short product_id)
548{
549 return CheckError(SDL_hid_enumerate(vendor_id, product_id));
550}
551
563{
564 SDL_hid_free_enumeration(devs);
565}
566
583inline HidDevice hid_open(unsigned short vendor_id,
584 unsigned short product_id,
585 const wchar_t* serial_number)
586{
587 return HidDevice(vendor_id, product_id, serial_number);
588}
589
603{
604 return HidDevice(std::move(path));
605}
606
632{
633 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
634}
635
637{
638 return SDL::hid_write(m_resource, std::move(data));
639}
640
658 TargetBytes data,
659 Milliseconds timeout)
660{
661 return SDL_hid_read_timeout(
662 dev, data.data_as<Uint8>(), data.size_bytes(), timeout.count());
663}
664
666{
667 return SDL::hid_read_timeout(m_resource, std::move(data), timeout);
668}
669
687{
688 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
689}
690
692{
693 return SDL::hid_read(m_resource, std::move(data));
694}
695
712inline void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
713{
714 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
715}
716
717inline void HidDevice::set_nonblocking(bool nonblock)
718{
719 SDL::hid_set_nonblocking(m_resource, nonblock);
720}
721
744{
745 return SDL_hid_send_feature_report(
746 dev, data.data_as<Uint8>(), data.size_bytes());
747}
748
750{
751 return SDL::hid_send_feature_report(m_resource, std::move(data));
752}
753
774{
775 return SDL_hid_get_feature_report(
776 dev, data.data_as<Uint8>(), data.size_bytes());
777}
778
780{
781 return SDL::hid_get_feature_report(m_resource, std::move(data));
782}
783
804{
805 return SDL_hid_get_input_report(
806 dev, data.data_as<Uint8>(), data.size_bytes());
807}
808
810{
811 return SDL::hid_get_input_report(m_resource, std::move(data));
812}
813
822inline void hid_close(HidDeviceRaw dev)
823{
824 CheckErrorIfNot(SDL_hid_close(dev), 0);
825}
826
827inline void HidDevice::close() { hid_close(release()); }
828
840 wchar_t* string,
841 size_t maxlen)
842{
843 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
844}
845
846inline void HidDevice::get_manufacturer_string(wchar_t* string, size_t maxlen)
847{
848 SDL::hid_get_manufacturer_string(m_resource, string, maxlen);
849}
850
862 wchar_t* string,
863 size_t maxlen)
864{
865 CheckErrorIfNot(SDL_hid_get_product_string(dev, string, maxlen), 0);
866}
867
868inline void HidDevice::get_product_string(wchar_t* string, size_t maxlen)
869{
870 SDL::hid_get_product_string(m_resource, string, maxlen);
871}
872
884 wchar_t* string,
885 size_t maxlen)
886{
887 CheckErrorIfNot(SDL_hid_get_serial_number_string(dev, string, maxlen), 0);
888}
889
890inline void HidDevice::get_serial_number_string(wchar_t* string, size_t maxlen)
891{
892 SDL::hid_get_serial_number_string(m_resource, string, maxlen);
893}
894
907 int string_index,
908 wchar_t* string,
909 size_t maxlen)
910{
911 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
912 0);
913}
914
915inline void HidDevice::get_indexed_string(int string_index,
916 wchar_t* string,
917 size_t maxlen)
918{
919 SDL::hid_get_indexed_string(m_resource, string_index, string, maxlen);
920}
921
934{
935 return CheckError(SDL_hid_get_device_info(dev));
936}
937
939{
940 return SDL::hid_get_device_info(m_resource);
941}
942
957{
958 return SDL_hid_get_report_descriptor(
959 dev, buf.data_as<Uint8>(), buf.size_bytes());
960}
961
963{
964 return SDL::hid_get_report_descriptor(m_resource, std::move(buf));
965}
966
974inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
975
977
978} // namespace SDL
979
980#endif /* SDL3PP_HIDAPI_H_ */
An opaque handle representing an open HID device.
Definition: SDL3pp_hidapi.h:115
constexpr HidDevice(const HidDeviceRaw resource)
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:129
constexpr HidDevice(const HidDevice &other)=delete
Copy constructor.
constexpr HidDevice(HidDevice &&other)
Move constructor.
Definition: SDL3pp_hidapi.h:138
HidDevice(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:180
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:162
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_hidapi.h:210
constexpr HidDeviceRaw get() const
Retrieves underlying HidDeviceRaw.
Definition: SDL3pp_hidapi.h:196
constexpr HidDeviceRaw release()
Retrieves underlying HidDeviceRaw and clear this.
Definition: SDL3pp_hidapi.h:199
constexpr auto operator<=>(const HidDevice &other) const =default
Comparison.
~HidDevice()
Destructor.
Definition: SDL3pp_hidapi.h:186
HidDevice & operator=(HidDevice other)
Assignment operator.
Definition: SDL3pp_hidapi.h:189
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:245
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
int hid_get_feature_report(HidDeviceParam dev, TargetBytes data)
Get a feature report from a HID device.
Definition: SDL3pp_hidapi.h:773
void hid_close(HidDeviceRaw dev)
Close a HID device.
Definition: SDL3pp_hidapi.h:822
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: SDL3pp_hidapi.h:846
SDL_hid_bus_type hid_bus_type
HID underlying bus types.
Definition: SDL3pp_hidapi.h:83
SDL_hid_device * HidDeviceRaw
Alias to raw representation for HidDevice.
Definition: SDL3pp_hidapi.h:46
hid_device_info * hid_get_device_info(HidDeviceParam dev)
Get the device info from a HID device.
Definition: SDL3pp_hidapi.h:933
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:717
hid_device_info * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: SDL3pp_hidapi.h:546
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition: SDL3pp_hidapi.h:779
int read(TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:691
SDL_hid_device_info hid_device_info
Information about a connected HID device.
Definition: SDL3pp_hidapi.h:105
hid_device_info * get_device_info()
Get the device info from a HID device.
Definition: SDL3pp_hidapi.h:938
constexpr hid_bus_type HID_API_BUS_I2C
[object Object]
Definition: SDL3pp_hidapi.h:94
int hid_get_report_descriptor(HidDeviceParam dev, TargetBytes buf)
Get a report descriptor from a HID device.
Definition: SDL3pp_hidapi.h:956
void hid_exit()
Finalize the HIDAPI library.
Definition: SDL3pp_hidapi.h:494
void get_product_string(wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: SDL3pp_hidapi.h:868
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:749
int hid_write(HidDeviceParam dev, SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:631
int read_timeout(TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:665
int hid_read(HidDeviceParam dev, TargetBytes data)
Read an Input report from a HID device.
Definition: SDL3pp_hidapi.h:686
int get_input_report(TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:809
void close()
Close a HID device.
Definition: SDL3pp_hidapi.h:827
constexpr hid_bus_type HID_API_BUS_UNKNOWN
Unknown bus type.
Definition: SDL3pp_hidapi.h:85
Uint32 hid_device_change_count()
Check to see if devices may have been added or removed.
Definition: SDL3pp_hidapi.h:515
int hid_send_feature_report(HidDeviceParam dev, SourceBytes data)
Send a Feature report to the device.
Definition: SDL3pp_hidapi.h:743
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:583
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:915
void hid_free_enumeration(hid_device_info *devs)
Free an enumeration linked list.
Definition: SDL3pp_hidapi.h:562
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:861
void hid_ble_scan(bool active)
Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.
Definition: SDL3pp_hidapi.h:974
void get_serial_number_string(wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: SDL3pp_hidapi.h:890
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:883
int write(SourceBytes data)
Write an Output report to a HID device.
Definition: SDL3pp_hidapi.h:636
void hid_init()
Initialize the HIDAPI library.
Definition: SDL3pp_hidapi.h:480
int hid_read_timeout(HidDeviceParam dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition: SDL3pp_hidapi.h:657
constexpr hid_bus_type HID_API_BUS_BLUETOOTH
[object Object]
Definition: SDL3pp_hidapi.h:91
constexpr hid_bus_type HID_API_BUS_USB
[object Object]
Definition: SDL3pp_hidapi.h:88
constexpr hid_bus_type HID_API_BUS_SPI
[object Object]
Definition: SDL3pp_hidapi.h:97
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:906
int hid_get_input_report(HidDeviceParam dev, TargetBytes data)
Get an input report from a HID device.
Definition: SDL3pp_hidapi.h:803
HidDevice hid_open_path(StringParam path)
Open a HID device by its path name.
Definition: SDL3pp_hidapi.h:602
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition: SDL3pp_hidapi.h:962
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:839
void hid_set_nonblocking(HidDeviceParam dev, bool nonblock)
Set the device handle to be non-blocking.
Definition: SDL3pp_hidapi.h:712
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition: SDL3pp_stdinc.h:380
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:325
Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:257
Main include header for the SDL3pp library.
Safely wrap HidDevice for non owning parameters.
Definition: SDL3pp_hidapi.h:53
constexpr HidDeviceParam(HidDeviceRaw value)
Constructs from HidDeviceRaw.
Definition: SDL3pp_hidapi.h:57
constexpr HidDeviceParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_hidapi.h:63
HidDeviceRaw value
parameter's HidDeviceRaw
Definition: SDL3pp_hidapi.h:54
constexpr auto operator<=>(const HidDeviceParam &other) const =default
Comparison.
Semi-safe reference for HidDevice.
Definition: SDL3pp_hidapi.h:440
HidDeviceRef(const HidDeviceRef &other)
Copy constructor.
Definition: SDL3pp_hidapi.h:454
~HidDeviceRef()
Destructor.
Definition: SDL3pp_hidapi.h:460
HidDeviceRef(HidDeviceParam resource)
Constructs from HidDeviceParam.
Definition: SDL3pp_hidapi.h:448