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
40
41// Forward decl
42struct HidDevice;
43
45using HidDeviceRaw = SDL_hid_device*;
46
53
59using hid_bus_type = SDL_hid_bus_type;
60
62 SDL_HID_API_BUS_UNKNOWN;
63
69constexpr hid_bus_type HID_API_BUS_USB = SDL_HID_API_BUS_USB;
70
78constexpr hid_bus_type HID_API_BUS_BLUETOOTH = SDL_HID_API_BUS_BLUETOOTH;
79
85constexpr hid_bus_type HID_API_BUS_I2C = SDL_HID_API_BUS_I2C;
86
92constexpr hid_bus_type HID_API_BUS_SPI = SDL_HID_API_BUS_SPI;
93
99using hid_device_info = SDL_hid_device_info;
100
108struct HidDevice : ResourceBase<HidDeviceRaw>
109{
111
119 constexpr explicit HidDevice(HidDeviceRaw resource) noexcept
120 : ResourceBase(resource)
121 {
122 }
123
125 constexpr HidDevice(const HidDevice& other) = delete;
126
128 constexpr HidDevice(HidDevice&& other) noexcept
129 : HidDevice(other.release())
130 {
131 }
132
133 constexpr HidDevice(const HidDeviceRef& other) = delete;
134
135 constexpr HidDevice(HidDeviceRef&& other) = delete;
136
152 HidDevice(unsigned short vendor_id,
153 unsigned short product_id,
154 const wchar_t* serial_number);
155
168
170 ~HidDevice() { SDL_hid_close(get()); }
171
173 constexpr HidDevice& operator=(HidDevice&& other) noexcept
174 {
175 swap(*this, other);
176 return *this;
177 }
178
180 HidDevice& operator=(const HidDevice& other) = delete;
181
189 void close();
190
191#if SDL_VERSION_ATLEAST(3, 4, 0)
192
207
208#endif // SDL_VERSION_ATLEAST(3, 4, 0)
209
233 int write(SourceBytes data);
234
250 int read_timeout(TargetBytes data, Milliseconds timeout);
251
266 int read(TargetBytes data);
267
284 void set_nonblocking(bool nonblock);
285
307
327
347
357 void get_manufacturer_string(wchar_t* string, size_t maxlen);
358
368 void get_product_string(wchar_t* string, size_t maxlen);
369
379 void get_serial_number_string(wchar_t* string, size_t maxlen);
380
391 void get_indexed_string(int string_index, wchar_t* string, size_t maxlen);
392
404
418};
419
437inline void hid_init() { CheckErrorIfNot(SDL_hid_init(), 0); }
438
451inline void hid_exit() { CheckErrorIfNot(SDL_hid_exit(), 0); }
452
473{
474 return SDL_hid_device_change_count();
475}
476
503inline hid_device_info* hid_enumerate(unsigned short vendor_id,
504 unsigned short product_id)
505{
506 return CheckError(SDL_hid_enumerate(vendor_id, product_id));
507}
508
519{
520 SDL_hid_free_enumeration(devs);
521}
522
539inline HidDevice hid_open(unsigned short vendor_id,
540 unsigned short product_id,
541 const wchar_t* serial_number)
542{
543 return HidDevice(vendor_id, product_id, serial_number);
544}
545
546inline HidDevice::HidDevice(unsigned short vendor_id,
547 unsigned short product_id,
548 const wchar_t* serial_number)
549 : HidDevice(CheckError(SDL_hid_open(vendor_id, product_id, serial_number)))
550{
551}
552
554 : HidDevice(CheckError(SDL_hid_open_path(path)))
555{
556}
557
571{
572 return HidDevice(std::move(path));
573}
574
575#if SDL_VERSION_ATLEAST(3, 4, 0)
576
592{
593 return CheckError(SDL_hid_get_properties(dev));
594}
595
600
608namespace prop::Hidapi {
609
610constexpr auto LIBUSB_DEVICE_HANDLE_POINTER =
611 SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER;
612
613} // namespace prop::Hidapi
614
615#endif // SDL_VERSION_ATLEAST(3, 4, 0)
616
639inline int hid_write(HidDeviceRef dev, SourceBytes data)
640{
641 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
642}
643
645{
646 return SDL::hid_write(get(), std::move(data));
647}
648
666 TargetBytes data,
667 Milliseconds timeout)
668{
669 return SDL_hid_read_timeout(
670 dev, data.data_as<Uint8>(), data.size_bytes(), narrowS32(timeout.count()));
671}
672
674{
675 return SDL::hid_read_timeout(get(), std::move(data), timeout);
676}
677
693inline int hid_read(HidDeviceRef dev, TargetBytes data)
694{
695 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
696}
697
699{
700 return SDL::hid_read(get(), std::move(data));
701}
702
719inline void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
720{
721 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
722}
723
724inline void HidDevice::set_nonblocking(bool nonblock)
725{
726 SDL::hid_set_nonblocking(get(), nonblock);
727}
728
750{
751 return SDL_hid_send_feature_report(
752 dev, data.data_as<Uint8>(), data.size_bytes());
753}
754
756{
757 return SDL::hid_send_feature_report(get(), std::move(data));
758}
759
780{
781 return SDL_hid_get_feature_report(
782 dev, data.data_as<Uint8>(), data.size_bytes());
783}
784
786{
787 return SDL::hid_get_feature_report(get(), std::move(data));
788}
789
810{
811 return SDL_hid_get_input_report(
812 dev, data.data_as<Uint8>(), data.size_bytes());
813}
814
816{
817 return SDL::hid_get_input_report(get(), std::move(data));
818}
819
828inline void hid_close(HidDeviceRaw dev)
829{
830 CheckErrorIfNot(SDL_hid_close(dev), 0);
831}
832
833inline void HidDevice::close() { hid_close(release()); }
834
846 wchar_t* string,
847 size_t maxlen)
848{
849 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
850}
851
852inline void HidDevice::get_manufacturer_string(wchar_t* string, size_t maxlen)
853{
854 SDL::hid_get_manufacturer_string(get(), string, maxlen);
855}
856
868 wchar_t* string,
869 size_t maxlen)
870{
871 CheckErrorIfNot(SDL_hid_get_product_string(dev, string, maxlen), 0);
872}
873
874inline void HidDevice::get_product_string(wchar_t* string, size_t maxlen)
875{
876 SDL::hid_get_product_string(get(), string, maxlen);
877}
878
890 wchar_t* string,
891 size_t maxlen)
892{
893 CheckErrorIfNot(SDL_hid_get_serial_number_string(dev, string, maxlen), 0);
894}
895
896inline void HidDevice::get_serial_number_string(wchar_t* string, size_t maxlen)
897{
898 SDL::hid_get_serial_number_string(get(), string, maxlen);
899}
900
913 int string_index,
914 wchar_t* string,
915 size_t maxlen)
916{
917 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
918 0);
919}
920
921inline void HidDevice::get_indexed_string(int string_index,
922 wchar_t* string,
923 size_t maxlen)
924{
925 SDL::hid_get_indexed_string(get(), string_index, string, maxlen);
926}
927
940{
941 return CheckError(SDL_hid_get_device_info(dev));
942}
943
948
963{
964 return SDL_hid_get_report_descriptor(
965 dev, buf.data_as<Uint8>(), buf.size_bytes());
966}
967
969{
970 return SDL::hid_get_report_descriptor(get(), std::move(buf));
971}
972
980inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
981
983
984} // namespace SDL
985
986#endif /* SDL3PP_HIDAPI_H_ */
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
Source byte stream.
Definition SDL3pp_strings.h:237
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:301
constexpr const T * data_as() const
Retrieves contained data.
Definition SDL3pp_strings.h:311
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
Target byte stream.
Definition SDL3pp_strings.h:323
constexpr T * data_as() const
Retrieves contained data.
Definition SDL3pp_strings.h:413
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:406
constexpr void CheckErrorIfNot(T result, T validValue)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:246
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
void hid_close(HidDeviceRaw dev)
Close a HID device.
Definition SDL3pp_hidapi.h:828
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition SDL3pp_hidapi.h:852
int hid_get_report_descriptor(HidDeviceRef dev, TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:962
PropertiesRef hid_get_properties()
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:596
int hid_write(HidDeviceRef dev, SourceBytes data)
Write an Output report to a HID device.
Definition SDL3pp_hidapi.h:639
void hid_get_product_string(HidDeviceRef dev, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition SDL3pp_hidapi.h:867
void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:719
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:724
int hid_get_input_report(HidDeviceRef dev, TargetBytes data)
Get an input report from a HID device.
Definition SDL3pp_hidapi.h:809
hid_device_info * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition SDL3pp_hidapi.h:503
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition SDL3pp_hidapi.h:785
int read(TargetBytes data)
Read an Input report from a HID device.
Definition SDL3pp_hidapi.h:698
hid_device_info * get_device_info()
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:944
int hid_read(HidDeviceRef dev, TargetBytes data)
Read an Input report from a HID device.
Definition SDL3pp_hidapi.h:693
hid_device_info * hid_get_device_info(HidDeviceRef dev)
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:939
constexpr hid_bus_type HID_API_BUS_I2C
I2C bus Specifications:
Definition SDL3pp_hidapi.h:85
void hid_exit()
Finalize the HIDAPI library.
Definition SDL3pp_hidapi.h:451
void get_product_string(wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition SDL3pp_hidapi.h:874
int hid_read_timeout(HidDeviceRef dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition SDL3pp_hidapi.h:665
SDL_hid_device_info hid_device_info
Information about a connected HID device.
Definition SDL3pp_hidapi.h:99
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition SDL3pp_hidapi.h:755
int read_timeout(TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition SDL3pp_hidapi.h:673
int get_input_report(TargetBytes data)
Get an input report from a HID device.
Definition SDL3pp_hidapi.h:815
ResourceRef< HidDevice > HidDeviceRef
Reference for HidDevice.
Definition SDL3pp_hidapi.h:52
void close()
Close a HID device.
Definition SDL3pp_hidapi.h:833
void hid_get_serial_number_string(HidDeviceRef dev, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition SDL3pp_hidapi.h:889
constexpr hid_bus_type HID_API_BUS_UNKNOWN
Unknown bus type.
Definition SDL3pp_hidapi.h:61
SDL_hid_bus_type hid_bus_type
HID underlying bus types.
Definition SDL3pp_hidapi.h:59
Uint32 hid_device_change_count()
Check to see if devices may have been added or removed.
Definition SDL3pp_hidapi.h:472
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:539
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:921
void hid_free_enumeration(hid_device_info *devs)
Free an enumeration linked list.
Definition SDL3pp_hidapi.h:518
void hid_ble_scan(bool active)
Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.
Definition SDL3pp_hidapi.h:980
void get_serial_number_string(wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition SDL3pp_hidapi.h:896
int write(SourceBytes data)
Write an Output report to a HID device.
Definition SDL3pp_hidapi.h:644
void hid_init()
Initialize the HIDAPI library.
Definition SDL3pp_hidapi.h:437
int hid_get_feature_report(HidDeviceRef dev, TargetBytes data)
Get a feature report from a HID device.
Definition SDL3pp_hidapi.h:779
void hid_get_indexed_string(HidDeviceRef 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:912
constexpr hid_bus_type HID_API_BUS_BLUETOOTH
Bluetooth or Bluetooth LE bus Specifications:
Definition SDL3pp_hidapi.h:78
void hid_get_manufacturer_string(HidDeviceRef dev, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition SDL3pp_hidapi.h:845
constexpr hid_bus_type HID_API_BUS_USB
USB bus Specifications:
Definition SDL3pp_hidapi.h:69
int hid_send_feature_report(HidDeviceRef dev, SourceBytes data)
Send a Feature report to the device.
Definition SDL3pp_hidapi.h:749
constexpr hid_bus_type HID_API_BUS_SPI
SPI bus Specifications:
Definition SDL3pp_hidapi.h:92
HidDevice hid_open_path(StringParam path)
Open a HID device by its path name.
Definition SDL3pp_hidapi.h:570
SDL_hid_device * HidDeviceRaw
Alias to raw representation for HidDevice.
Definition SDL3pp_hidapi.h:45
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:968
PropertiesRef hid_get_properties(HidDeviceRef dev)
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:591
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition SDL3pp_stdinc.h:238
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:335
HIDAPI properties.
Definition SDL3pp_hidapi.h:608
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6257
An opaque handle representing an open HID device.
Definition SDL3pp_hidapi.h:109
constexpr HidDevice(HidDeviceRaw resource) noexcept
Constructs from raw HidDevice.
Definition SDL3pp_hidapi.h:119
constexpr HidDevice(const HidDevice &other)=delete
Copy constructor.
HidDevice & operator=(const HidDevice &other)=delete
Assignment operator.
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
~HidDevice()
Destructor.
Definition SDL3pp_hidapi.h:170
constexpr HidDevice & operator=(HidDevice &&other) noexcept
Assignment operator.
Definition SDL3pp_hidapi.h:173
constexpr HidDevice(HidDevice &&other) noexcept
Move constructor.
Definition SDL3pp_hidapi.h:128
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156