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
573{
574 return HidDevice(std::move(path));
575}
576
577#if SDL_VERSION_ATLEAST(3, 4, 0)
578
594{
595 return CheckError(SDL_hid_get_properties(dev));
596}
597
602
610namespace prop::Hidapi {
611
613 SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER;
615
616} // namespace prop::Hidapi
617
618#endif // SDL_VERSION_ATLEAST(3, 4, 0)
619
641inline int hid_write(HidDeviceRef dev, SourceBytes data)
642{
643 return SDL_hid_write(dev, data.data_as<Uint8>(), data.size_bytes());
644}
645
647{
648 return SDL::hid_write(get(), std::move(data));
649}
650
668 TargetBytes data,
669 Milliseconds timeout)
670{
671 return SDL_hid_read_timeout(
672 dev, data.data_as<Uint8>(), data.size_bytes(), narrowS32(timeout.count()));
673}
674
676{
677 return SDL::hid_read_timeout(get(), std::move(data), timeout);
678}
679
695inline int hid_read(HidDeviceRef dev, TargetBytes data)
696{
697 return SDL_hid_read(dev, data.data_as<Uint8>(), data.size_bytes());
698}
699
701{
702 return SDL::hid_read(get(), std::move(data));
703}
704
721inline void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
722{
723 CheckErrorIfNot(SDL_hid_set_nonblocking(dev, nonblock), 0);
724}
725
726inline void HidDeviceBase::set_nonblocking(bool nonblock)
727{
728 SDL::hid_set_nonblocking(get(), nonblock);
729}
730
752{
753 return SDL_hid_send_feature_report(
754 dev, data.data_as<Uint8>(), data.size_bytes());
755}
756
758{
759 return SDL::hid_send_feature_report(get(), std::move(data));
760}
761
782{
783 return SDL_hid_get_feature_report(
784 dev, data.data_as<Uint8>(), data.size_bytes());
785}
786
788{
789 return SDL::hid_get_feature_report(get(), std::move(data));
790}
791
812{
813 return SDL_hid_get_input_report(
814 dev, data.data_as<Uint8>(), data.size_bytes());
815}
816
818{
819 return SDL::hid_get_input_report(get(), std::move(data));
820}
821
830inline void hid_close(HidDeviceRaw dev)
831{
832 CheckErrorIfNot(SDL_hid_close(dev), 0);
833}
834
836
848 wchar_t* string,
849 size_t maxlen)
850{
851 CheckErrorIfNot(SDL_hid_get_manufacturer_string(dev, string, maxlen), 0);
852}
853
854inline void HidDeviceBase::get_manufacturer_string(wchar_t* string,
855 size_t maxlen)
856{
857 SDL::hid_get_manufacturer_string(get(), string, maxlen);
858}
859
871 wchar_t* string,
872 size_t maxlen)
873{
874 CheckErrorIfNot(SDL_hid_get_product_string(dev, string, maxlen), 0);
875}
876
877inline void HidDeviceBase::get_product_string(wchar_t* string, size_t maxlen)
878{
879 SDL::hid_get_product_string(get(), string, maxlen);
880}
881
893 wchar_t* string,
894 size_t maxlen)
895{
896 CheckErrorIfNot(SDL_hid_get_serial_number_string(dev, string, maxlen), 0);
897}
898
899inline void HidDeviceBase::get_serial_number_string(wchar_t* string,
900 size_t maxlen)
901{
902 SDL::hid_get_serial_number_string(get(), string, maxlen);
903}
904
917 int string_index,
918 wchar_t* string,
919 size_t maxlen)
920{
921 CheckErrorIfNot(SDL_hid_get_indexed_string(dev, string_index, string, maxlen),
922 0);
923}
924
925inline void HidDeviceBase::get_indexed_string(int string_index,
926 wchar_t* string,
927 size_t maxlen)
928{
929 SDL::hid_get_indexed_string(get(), string_index, string, maxlen);
930}
931
944{
945 return CheckError(SDL_hid_get_device_info(dev));
946}
947
952
967{
968 return SDL_hid_get_report_descriptor(
969 dev, buf.data_as<Uint8>(), buf.size_bytes());
970}
971
973{
974 return SDL::hid_get_report_descriptor(get(), std::move(buf));
975}
976
984inline void hid_ble_scan(bool active) { SDL_hid_ble_scan(active); }
985
987
988} // namespace SDL
989
990#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: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:830
int hid_get_report_descriptor(HidDeviceRef dev, TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:966
hid_device_info * get_device_info()
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:948
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:925
void close()
Close a HID device.
Definition SDL3pp_hidapi.h:835
int hid_write(HidDeviceRef dev, SourceBytes data)
Write an Output report to a HID device.
Definition SDL3pp_hidapi.h:641
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:870
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:817
void get_manufacturer_string(wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition SDL3pp_hidapi.h:854
void hid_set_nonblocking(HidDeviceRef dev, bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:721
int send_feature_report(SourceBytes data)
Send a Feature report to the device.
Definition SDL3pp_hidapi.h:757
int hid_get_input_report(HidDeviceRef dev, TargetBytes data)
Get an input report from a HID device.
Definition SDL3pp_hidapi.h:811
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:877
PropertiesRef hid_get_properties()
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:598
int hid_read(HidDeviceRef dev, TargetBytes data)
Read an Input report from a HID device.
Definition SDL3pp_hidapi.h:695
hid_device_info * hid_get_device_info(HidDeviceRef dev)
Get the device info from a HID device.
Definition SDL3pp_hidapi.h:943
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:899
int hid_read_timeout(HidDeviceRef dev, TargetBytes data, Milliseconds timeout)
Read an Input report from a HID device with timeout.
Definition SDL3pp_hidapi.h:667
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:646
void set_nonblocking(bool nonblock)
Set the device handle to be non-blocking.
Definition SDL3pp_hidapi.h:726
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:892
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:984
int get_feature_report(TargetBytes data)
Get a feature report from a HID device.
Definition SDL3pp_hidapi.h:787
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:781
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:916
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:847
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:751
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:700
int get_report_descriptor(TargetBytes buf)
Get a report descriptor from a HID device.
Definition SDL3pp_hidapi.h:972
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:675
PropertiesRef hid_get_properties(HidDeviceRef dev)
Get the properties associated with an HidDevice.
Definition SDL3pp_hidapi.h:593
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:53
::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:610
constexpr auto LIBUSB_DEVICE_HANDLE_POINTER
Pointer to libusb device handle.
Definition SDL3pp_hidapi.h:612
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6263
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