summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/hidapi/hidtest
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/hidapi/hidtest')
-rw-r--r--contrib/SDL-3.2.8/src/hidapi/hidtest/CMakeLists.txt40
-rw-r--r--contrib/SDL-3.2.8/src/hidapi/hidtest/Makefile.am28
-rw-r--r--contrib/SDL-3.2.8/src/hidapi/hidtest/test.c316
3 files changed, 384 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/hidapi/hidtest/CMakeLists.txt b/contrib/SDL-3.2.8/src/hidapi/hidtest/CMakeLists.txt
new file mode 100644
index 0000000..19c50e1
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/hidapi/hidtest/CMakeLists.txt
@@ -0,0 +1,40 @@
1cmake_minimum_required(VERSION 3.1.3...3.25 FATAL_ERROR)
2project(hidtest C)
3
4if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
5 # hidtest is build as a standalone project
6
7 if(POLICY CMP0074)
8 # allow using hidapi_ROOT if CMake supports it
9 cmake_policy(SET CMP0074 NEW)
10 endif()
11
12 find_package(hidapi 0.12 REQUIRED)
13 message(STATUS "Using HIDAPI: ${hidapi_VERSION}")
14else()
15 # hidtest is built as part of the main HIDAPI build
16 message(STATUS "Building hidtest")
17endif()
18
19set(HIDAPI_HIDTEST_TARGETS)
20if(NOT WIN32 AND NOT APPLE AND CMAKE_SYSTEM_NAME MATCHES "Linux")
21 if(TARGET hidapi::hidraw)
22 add_executable(hidtest_hidraw test.c)
23 target_link_libraries(hidtest_hidraw hidapi::hidraw)
24 list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_hidraw)
25 endif()
26 if(TARGET hidapi::libusb)
27 add_executable(hidtest_libusb test.c)
28 target_compile_definitions(hidtest_libusb PRIVATE USING_HIDAPI_LIBUSB)
29 target_link_libraries(hidtest_libusb hidapi::libusb)
30 list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_libusb)
31 endif()
32else()
33 add_executable(hidtest test.c)
34 target_link_libraries(hidtest hidapi::hidapi)
35 list(APPEND HIDAPI_HIDTEST_TARGETS hidtest)
36endif()
37
38install(TARGETS ${HIDAPI_HIDTEST_TARGETS}
39 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
40)
diff --git a/contrib/SDL-3.2.8/src/hidapi/hidtest/Makefile.am b/contrib/SDL-3.2.8/src/hidapi/hidtest/Makefile.am
new file mode 100644
index 0000000..b601307
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/hidapi/hidtest/Makefile.am
@@ -0,0 +1,28 @@
1AM_CPPFLAGS = -I$(top_srcdir)/hidapi/
2
3## Linux
4if OS_LINUX
5noinst_PROGRAMS = hidtest-libusb hidtest-hidraw
6
7hidtest_hidraw_SOURCES = test.c
8hidtest_hidraw_LDADD = $(top_builddir)/linux/libhidapi-hidraw.la
9
10hidtest_libusb_SOURCES = test.c
11hidtest_libusb_LDADD = $(top_builddir)/libusb/libhidapi-libusb.la
12else
13
14# Other OS's
15noinst_PROGRAMS = hidtest
16
17hidtest_SOURCES = test.c
18hidtest_LDADD = $(top_builddir)/$(backend)/libhidapi.la
19
20endif
21
22if OS_DARWIN
23AM_CPPFLAGS += -I$(top_srcdir)/mac/
24endif
25
26if OS_WINDOWS
27AM_CPPFLAGS += -I$(top_srcdir)/windows/
28endif
diff --git a/contrib/SDL-3.2.8/src/hidapi/hidtest/test.c b/contrib/SDL-3.2.8/src/hidapi/hidtest/test.c
new file mode 100644
index 0000000..1eb6582
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/hidapi/hidtest/test.c
@@ -0,0 +1,316 @@
1/*******************************************************
2 HIDAPI - Multi-Platform library for
3 communication with HID devices.
4
5 Alan Ott
6 Signal 11 Software
7
8 libusb/hidapi Team
9
10 Copyright 2022.
11
12 This contents of this file may be used by anyone
13 for any reason without any conditions and may be
14 used as a starting point for your own applications
15 which use HIDAPI.
16********************************************************/
17
18#include <stdio.h>
19#include <wchar.h>
20#include <string.h>
21#include <stdlib.h>
22
23#include <hidapi.h>
24
25// Headers needed for sleeping.
26#ifdef _WIN32
27 #include <windows.h>
28#else
29 #include <unistd.h>
30#endif
31
32// Fallback/example
33#ifndef HID_API_MAKE_VERSION
34#define HID_API_MAKE_VERSION(mj, mn, p) (((mj) << 24) | ((mn) << 8) | (p))
35#endif
36#ifndef HID_API_VERSION
37#define HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH)
38#endif
39
40//
41// Sample using platform-specific headers
42#if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
43#include <hidapi_darwin.h>
44#endif
45
46#if defined(_WIN32) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
47#include <hidapi_winapi.h>
48#endif
49
50#if defined(USING_HIDAPI_LIBUSB) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
51#include <hidapi_libusb.h>
52#endif
53//
54
55const char *hid_bus_name(hid_bus_type bus_type) {
56 static const char *const HidBusTypeName[] = {
57 "Unknown",
58 "USB",
59 "Bluetooth",
60 "I2C",
61 "SPI",
62 };
63
64 if ((int)bus_type < 0)
65 bus_type = HID_API_BUS_UNKNOWN;
66 if ((int)bus_type >= (int)(sizeof(HidBusTypeName) / sizeof(HidBusTypeName[0])))
67 bus_type = HID_API_BUS_UNKNOWN;
68
69 return HidBusTypeName[bus_type];
70}
71
72void print_device(struct hid_device_info *cur_dev) {
73 printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
74 printf("\n");
75 printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
76 printf(" Product: %ls\n", cur_dev->product_string);
77 printf(" Release: %hx\n", cur_dev->release_number);
78 printf(" Interface: %d\n", cur_dev->interface_number);
79 printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
80 printf(" Bus type: %u (%s)\n", (unsigned)cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
81 printf("\n");
82}
83
84void print_hid_report_descriptor_from_device(hid_device *device) {
85 unsigned char descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];
86 int res = 0;
87
88 printf(" Report Descriptor: ");
89 res = hid_get_report_descriptor(device, descriptor, sizeof(descriptor));
90 if (res < 0) {
91 printf("error getting: %ls", hid_error(device));
92 }
93 else {
94 printf("(%d bytes)", res);
95 }
96 for (int i = 0; i < res; i++) {
97 if (i % 10 == 0) {
98 printf("\n");
99 }
100 printf("0x%02x, ", descriptor[i]);
101 }
102 printf("\n");
103}
104
105void print_hid_report_descriptor_from_path(const char *path) {
106 hid_device *device = hid_open_path(path);
107 if (device) {
108 print_hid_report_descriptor_from_device(device);
109 hid_close(device);
110 }
111 else {
112 printf(" Report Descriptor: Unable to open device by path\n");
113 }
114}
115
116void print_devices(struct hid_device_info *cur_dev) {
117 for (; cur_dev; cur_dev = cur_dev->next) {
118 print_device(cur_dev);
119 }
120}
121
122void print_devices_with_descriptor(struct hid_device_info *cur_dev) {
123 for (; cur_dev; cur_dev = cur_dev->next) {
124 print_device(cur_dev);
125 print_hid_report_descriptor_from_path(cur_dev->path);
126 }
127}
128
129int main(int argc, char* argv[])
130{
131 (void)argc;
132 (void)argv;
133
134 int res;
135 unsigned char buf[256];
136 #define MAX_STR 255
137 wchar_t wstr[MAX_STR];
138 hid_device *handle;
139 int i;
140
141 struct hid_device_info *devs;
142
143 printf("hidapi test/example tool. Compiled with hidapi version %s, runtime version %s.\n", HID_API_VERSION_STR, hid_version_str());
144 if (HID_API_VERSION == HID_API_MAKE_VERSION(hid_version()->major, hid_version()->minor, hid_version()->patch)) {
145 printf("Compile-time version matches runtime version of hidapi.\n\n");
146 }
147 else {
148 printf("Compile-time version is different than runtime version of hidapi.\n]n");
149 }
150
151 if (hid_init())
152 return -1;
153
154#if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
155 // To work properly needs to be called before hid_open/hid_open_path after hid_init.
156 // Best/recommended option - call it right after hid_init.
157 hid_darwin_set_open_exclusive(0);
158#endif
159
160 devs = hid_enumerate(0x0, 0x0);
161 print_devices_with_descriptor(devs);
162 hid_free_enumeration(devs);
163
164 // Set up the command buffer.
165 memset(buf,0x00,sizeof(buf));
166 buf[0] = 0x01;
167 buf[1] = 0x81;
168
169
170 // Open the device using the VID, PID,
171 // and optionally the Serial number.
172 ////handle = hid_open(0x4d8, 0x3f, L"12345");
173 handle = hid_open(0x4d8, 0x3f, NULL);
174 if (!handle) {
175 printf("unable to open device\n");
176 hid_exit();
177 return 1;
178 }
179
180 // Read the Manufacturer String
181 wstr[0] = 0x0000;
182 res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
183 if (res < 0)
184 printf("Unable to read manufacturer string\n");
185 printf("Manufacturer String: %ls\n", wstr);
186
187 // Read the Product String
188 wstr[0] = 0x0000;
189 res = hid_get_product_string(handle, wstr, MAX_STR);
190 if (res < 0)
191 printf("Unable to read product string\n");
192 printf("Product String: %ls\n", wstr);
193
194 // Read the Serial Number String
195 wstr[0] = 0x0000;
196 res = hid_get_serial_number_string(handle, wstr, MAX_STR);
197 if (res < 0)
198 printf("Unable to read serial number string\n");
199 printf("Serial Number String: (%d) %ls\n", wstr[0], wstr);
200
201 print_hid_report_descriptor_from_device(handle);
202
203 struct hid_device_info* info = hid_get_device_info(handle);
204 if (info == NULL) {
205 printf("Unable to get device info\n");
206 } else {
207 print_devices(info);
208 }
209
210 // Read Indexed String 1
211 wstr[0] = 0x0000;
212 res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
213 if (res < 0)
214 printf("Unable to read indexed string 1\n");
215 printf("Indexed String 1: %ls\n", wstr);
216
217 // Set the hid_read() function to be non-blocking.
218 hid_set_nonblocking(handle, 1);
219
220 // Try to read from the device. There should be no
221 // data here, but execution should not block.
222 res = hid_read(handle, buf, 17);
223
224 // Send a Feature Report to the device
225 buf[0] = 0x2;
226 buf[1] = 0xa0;
227 buf[2] = 0x0a;
228 buf[3] = 0x00;
229 buf[4] = 0x00;
230 res = hid_send_feature_report(handle, buf, 17);
231 if (res < 0) {
232 printf("Unable to send a feature report.\n");
233 }
234
235 memset(buf,0,sizeof(buf));
236
237 // Read a Feature Report from the device
238 buf[0] = 0x2;
239 res = hid_get_feature_report(handle, buf, sizeof(buf));
240 if (res < 0) {
241 printf("Unable to get a feature report: %ls\n", hid_error(handle));
242 }
243 else {
244 // Print out the returned buffer.
245 printf("Feature Report\n ");
246 for (i = 0; i < res; i++)
247 printf("%02x ", (unsigned int) buf[i]);
248 printf("\n");
249 }
250
251 memset(buf,0,sizeof(buf));
252
253 // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
254 buf[0] = 0x1;
255 buf[1] = 0x80;
256 res = hid_write(handle, buf, 17);
257 if (res < 0) {
258 printf("Unable to write(): %ls\n", hid_error(handle));
259 }
260
261
262 // Request state (cmd 0x81). The first byte is the report number (0x1).
263 buf[0] = 0x1;
264 buf[1] = 0x81;
265 hid_write(handle, buf, 17);
266 if (res < 0) {
267 printf("Unable to write()/2: %ls\n", hid_error(handle));
268 }
269
270 // Read requested state. hid_read() has been set to be
271 // non-blocking by the call to hid_set_nonblocking() above.
272 // This loop demonstrates the non-blocking nature of hid_read().
273 res = 0;
274 i = 0;
275 while (res == 0) {
276 res = hid_read(handle, buf, sizeof(buf));
277 if (res == 0) {
278 printf("waiting...\n");
279 }
280 if (res < 0) {
281 printf("Unable to read(): %ls\n", hid_error(handle));
282 break;
283 }
284
285 i++;
286 if (i >= 10) { /* 10 tries by 500 ms - 5 seconds of waiting*/
287 printf("read() timeout\n");
288 break;
289 }
290
291#ifdef _WIN32
292 Sleep(500);
293#else
294 usleep(500*1000);
295#endif
296 }
297
298 if (res > 0) {
299 printf("Data read:\n ");
300 // Print out the returned buffer.
301 for (i = 0; i < res; i++)
302 printf("%02x ", (unsigned int) buf[i]);
303 printf("\n");
304 }
305
306 hid_close(handle);
307
308 /* Free static HIDAPI objects. */
309 hid_exit();
310
311#ifdef _WIN32
312 system("pause");
313#endif
314
315 return 0;
316}