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 HidDeviceBase;
43
44// Forward decl
45struct HidDevice;
46
48using HidDeviceRaw = SDL_hid_device*;
49
56
62using hid_bus_type = SDL_hid_bus_type;
63
65 SDL_HID_API_BUS_UNKNOWN;
66
72constexpr hid_bus_type HID_API_BUS_USB = SDL_HID_API_BUS_USB;
73
81constexpr hid_bus_type HID_API_BUS_BLUETOOTH = SDL_HID_API_BUS_BLUETOOTH;
82
88constexpr hid_bus_type HID_API_BUS_I2C = SDL_HID_API_BUS_I2C;
89
95constexpr hid_bus_type HID_API_BUS_SPI = SDL_HID_API_BUS_SPI;
96
102using hid_device_info = SDL_hid_device_info;
103
109struct HidDeviceBase : ResourceBaseT<HidDeviceRaw>
110{
112
120 void close();
121
122#if SDL_VERSION_ATLEAST(3, 4, 0)
123
138
139#endif // SDL_VERSION_ATLEAST(3, 4, 0)
140
164 int write(SourceBytes data);
165
181 int read_timeout(TargetBytes data, Milliseconds timeout);
182
197 int read(TargetBytes data);
198
214 void set_nonblocking(bool nonblock);
215
237
257
277
287 void get_manufacturer_string(wchar_t* string, size_t maxlen);
288
298 void get_product_string(wchar_t* string, size_t maxlen);
299
309 void get_serial_number_string(wchar_t* string, size_t maxlen);
310
321 void get_indexed_string(int string_index, wchar_t* string, size_t maxlen);
322
334
348};
349
358{
359 using HidDeviceBase::HidDeviceBase;
360
368 constexpr explicit HidDevice(HidDeviceRaw resource) noexcept
369 : HidDeviceBase(resource)
370 {
371 }
372
374 constexpr HidDevice(HidDevice&& other) noexcept
375 : HidDevice(other.release())
376 {
377 }
378
394 HidDevice(unsigned short vendor_id,
395 unsigned short product_id,
396 const wchar_t* serial_number);
397
410
412 ~HidDevice() { SDL_hid_close(get()); }
413
415 constexpr HidDevice& operator=(HidDevice&& other) noexcept
416 {
417 swap(*this, other);
418 return *this;
419 }
420};
421
439inline void hid_init() { CheckErrorIfNot(SDL_hid_init(), 0); }
440
453inline void hid_exit() { CheckErrorIfNot(SDL_hid_exit(), 0); }
454
475{
476 return SDL_hid_device_change_count();
477}
478
505inline hid_device_info* hid_enumerate(unsigned short vendor_id,
506 unsigned short product_id)
507{
508 return CheckError(SDL_hid_enumerate(vendor_id, product_id));
509}
510
521{
522 SDL_hid_free_enumeration(devs);
523}
524
541inline HidDevice hid_open(unsigned short vendor_id,
542 unsigned short product_id,
543 const wchar_t* serial_number)
544{
545 return HidDevice(vendor_id, product_id, serial_number);
546}
547
548inline HidDevice::HidDevice(unsigned short vendor_id,
549 unsigned short product_id,
550 const wchar_t* serial_number)
551 : HidDevice(CheckError(SDL_hid_open(vendor_id, product_id, serial_number)))
552{
553}
554
556 : HidDevice(CheckError(SDL_hid_open_path(path)))
557{
558}
559
572inline HidDevice hid_open_path(StringParam path) { return HidDevice(path); }
573
574#if SDL_VERSION_ATLEAST(3, 4, 0)
575
591{
592 return CheckError(SDL_hid_get_properties(dev));
593}
594
599
607namespace prop::Hidapi {
608
610 SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER;
612
613} // namespace prop::Hidapi
614
615#endif // SDL_VERSION_ATLEAST(3, 4, 0)
616
638inline int hid_write(HidDeviceRef dev, SourceBytes data)
639{
640 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
641}
642
644{
645 return SDL::hid_write(get(), std::move(data));
646}
647
665 TargetBytes data,
666 Milliseconds timeout)
667{
668 return SDL_hid_read_timeout(
669 dev, data.data_as<Uint8>(), data.size_bytes(), narrowS32(timeout.count()));
670}
671
673{
674 return SDL::hid_read_timeout(get(), std::move(data), timeout);
675}
676
692inline int hid_read(HidDeviceRef dev, TargetBytes data)
693{
694 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
695}
696
698{
699 return SDL::hid_read(get(), std::move(data));
700}
701
718inline void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
719{
720 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
721}
722
723inline void HidDeviceBase::set_nonblocking(bool nonblock)
724{
725 SDL::hid_set_nonblocking(get(), nonblock);
726}
727
749{
750 return SDL_hid_send_feature_report(
751 dev, data.data_as<Uint8>(), data.size_bytes());
752}
753
755{
756 return SDL::hid_send_feature_report(get(), std::move(data));
757}
758
779{
780 return SDL_hid_get_feature_report(
781 dev, data.data_as<Uint8>(), data.size_bytes());
782}
783
785{
786 return SDL::hid_get_feature_report(get(), std::move(data));
787}
788
809{
810 return SDL_hid_get_input_report(
811 dev, data.data_as<Uint8>(), data.size_bytes());
812}
813
815{
816 return SDL::hid_get_input_report(get(), std::move(data));
817}
818
827inline void hid_close(HidDeviceRaw dev)
828{
829 CheckErrorIfNot(SDL_hid_close(dev), 0);
830}
831
833
845 wchar_t* string,
846 size_t maxlen)
847{
848 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
849}
850
851inline void HidDeviceBase::get_manufacturer_string(wchar_t* string,
852 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 HidDeviceBase::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 HidDeviceBase::get_serial_number_string(wchar_t* string,
897 size_t maxlen)
898{
899 SDL::hid_get_serial_number_string(get(), string, maxlen);
900}
901
914 int string_index,
915 wchar_t* string,
916 size_t maxlen)
917{
918 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
919 0);
920}
921
922inline void HidDeviceBase::get_indexed_string(int string_index,
923 wchar_t* string,
924 size_t maxlen)
925{
926 SDL::hid_get_indexed_string(get(), string_index, string, maxlen);
927}
928
941{
942 return CheckError(SDL_hid_get_device_info(dev));
943}
944
949
964{
965 return SDL_hid_get_report_descriptor(
966 dev, buf.data_as<Uint8>(), buf.size_bytes());
967}
968
970{
971 return SDL::hid_get_report_descriptor(get(), std::move(buf));
972}
973
981inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
982
984
985} // namespace SDL
986
987#endif /* SDL3PP_HIDAPI_H_ */
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Source byte stream.
Definition SDL3pp_strings.h:246
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:310
constexpr const T * data_as() const
Retrieves contained data.
Definition SDL3pp_strings.h:320
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
Target byte stream.
Definition SDL3pp_strings.h:332
constexpr T * data_as() const
Retrieves contained data.
Definition SDL3pp_strings.h:422
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:415
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:827
int hid_get_report_descriptor(HidDeviceRef dev, TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:963
hid_device_info * get_device_info()
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:945
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:922
void close()
Close a HID device.
Definition SDL3pp_hidapi.h:832
int hid_write(HidDeviceRef dev, SourceBytes data)
Write an Output report to a HID device.
Definition SDL3pp_hidapi.h:638
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
ResourceRefT< HidDeviceBase > HidDeviceRef
Reference for HidDevice.
Definition SDL3pp_hidapi.h:55
int get_input_report(TargetBytes data)
Get an input report from a HID device.
Definition SDL3pp_hidapi.h:814
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition SDL3pp_hidapi.h:851
void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:718
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition SDL3pp_hidapi.h:754
int hid_get_input_report(HidDeviceRef dev, TargetBytes data)
Get an input report from a HID device.
Definition SDL3pp_hidapi.h:808
hid_device_info * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition SDL3pp_hidapi.h:505
void get_product_string(wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition SDL3pp_hidapi.h:874
PropertiesRef hid_get_properties()
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:595
int hid_read(HidDeviceRef dev, TargetBytes data)
Read an Input report from a HID device.
Definition SDL3pp_hidapi.h:692
hid_device_info * hid_get_device_info(HidDeviceRef dev)
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:940
constexpr hid_bus_type HID_API_BUS_I2C
I2C bus Specifications:
Definition SDL3pp_hidapi.h:88
void hid_exit()
Finalize the HIDAPI library.
Definition SDL3pp_hidapi.h:453
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 hid_read_timeout(HidDeviceRef dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition SDL3pp_hidapi.h:664
SDL_hid_device_info hid_device_info
Information about a connected HID device.
Definition SDL3pp_hidapi.h:102
int write(SourceBytes data)
Write an Output report to a HID device.
Definition SDL3pp_hidapi.h:643
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:723
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:64
SDL_hid_bus_type hid_bus_type
HID underlying bus types.
Definition SDL3pp_hidapi.h:62
Uint32 hid_device_change_count()
Check to see if devices may have been added or removed.
Definition SDL3pp_hidapi.h:474
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:541
void hid_free_enumeration(hid_device_info *devs)
Free an enumeration linked list.
Definition SDL3pp_hidapi.h:520
void hid_ble_scan(bool active)
Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers.
Definition SDL3pp_hidapi.h:981
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition SDL3pp_hidapi.h:784
void hid_init()
Initialize the HIDAPI library.
Definition SDL3pp_hidapi.h:439
int hid_get_feature_report(HidDeviceRef dev, TargetBytes data)
Get a feature report from a HID device.
Definition SDL3pp_hidapi.h:778
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:913
constexpr hid_bus_type HID_API_BUS_BLUETOOTH
Bluetooth or Bluetooth LE bus Specifications:
Definition SDL3pp_hidapi.h:81
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:844
constexpr hid_bus_type HID_API_BUS_USB
USB bus Specifications:
Definition SDL3pp_hidapi.h:72
int hid_send_feature_report(HidDeviceRef dev, SourceBytes data)
Send a Feature report to the device.
Definition SDL3pp_hidapi.h:748
constexpr hid_bus_type HID_API_BUS_SPI
SPI bus Specifications:
Definition SDL3pp_hidapi.h:95
HidDevice hid_open_path(StringParam path)
Open a HID device by its path name.
Definition SDL3pp_hidapi.h:572
int read(TargetBytes data)
Read an Input report from a HID device.
Definition SDL3pp_hidapi.h:697
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:969
SDL_hid_device * HidDeviceRaw
Alias to raw representation for HidDevice.
Definition SDL3pp_hidapi.h:48
int read_timeout(TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition SDL3pp_hidapi.h:672
PropertiesRef hid_get_properties(HidDeviceRef dev)
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:590
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:55
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition SDL3pp_stdinc.h:244
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:341
HIDAPI properties.
Definition SDL3pp_hidapi.h:607
constexpr auto LIBUSB_DEVICE_HANDLE_POINTER
Pointer to libusb device handle.
Definition SDL3pp_hidapi.h:609
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6262
Base class to HidDevice.
Definition SDL3pp_hidapi.h:110
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An opaque handle representing an open HID device.
Definition SDL3pp_hidapi.h:358
constexpr HidDevice(HidDeviceRaw resource) noexcept
Constructs from raw HidDevice.
Definition SDL3pp_hidapi.h:368
~HidDevice()
Destructor.
Definition SDL3pp_hidapi.h:412
constexpr HidDevice & operator=(HidDevice &&other) noexcept
Assignment operator.
Definition SDL3pp_hidapi.h:415
constexpr HidDevice(HidDevice &&other) noexcept
Move constructor.
Definition SDL3pp_hidapi.h:374
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93