Monado OpenXR Runtime
Loading...
Searching...
No Matches
h_hid.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Wrapper around OS native hid functions.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
8 *
9 * @ingroup aux_hid
10 */
11
12#pragma once
13
14#include "xrt/xrt_config_os.h"
15#include "xrt/xrt_config_have.h"
16#include <stdint.h>
17#include <stddef.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23
24/*!
25 * @defgroup aux_hid HID
26 * @ingroup aux
27 *
28 * @brief Wrapper around HID devices.
29 */
30
31/*!
32 * @dir auxiliary/hid
33 *
34 * @brief @ref aux_hid files.
35 */
36
37/*!
38 * @interface h_hid_device
39 *
40 * Representing a single hid interface on a device.
41 */
43{
44 int (*read)(struct h_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds);
45
46 int (*write)(struct h_hid_device *hid_dev, const uint8_t *data, size_t size);
47
48 int (*get_feature)(struct h_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size);
49
50 int (*get_feature_timeout)(struct h_hid_device *hid_dev, void *data, size_t size, uint32_t timeout);
51
52 int (*set_feature)(struct h_hid_device *hid_dev, const uint8_t *data, size_t size);
53
54 int (*get_physical_address)(struct h_hid_device *hid_dev, uint8_t *data, size_t size);
55
56 void (*destroy)(struct h_hid_device *hid_dev);
57};
58
59/*!
60 * Read the next input report, if any, from the given hid device.
61 *
62 * If milliseconds are negative, this call blocks indefinitely, 0 polls,
63 * and positive will block for that amount of milliseconds.
64 *
65 * @public @memberof h_hid_device
66 */
67static inline int
68h_hid_read(struct h_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds)
69{
70 return hid_dev->read(hid_dev, data, size, milliseconds);
71}
72
73/*!
74 * Write an output report to the given device.
75 *
76 * @public @memberof h_hid_device
77 */
78static inline int
79h_hid_write(struct h_hid_device *hid_dev, const uint8_t *data, size_t size)
80{
81 return hid_dev->write(hid_dev, data, size);
82}
83
84/*!
85 * Get a numbered feature report.
86 *
87 * If the device doesn't have more than one feature report, request report 0.
88 *
89 * @public @memberof h_hid_device
90 */
91static inline int
92h_hid_get_feature(struct h_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size)
93{
94 return hid_dev->get_feature(hid_dev, report_num, data, size);
95}
96
97/*!
98 * Get a feature report with a timeout.
99 *
100 * @public @memberof h_hid_device
101 */
102static inline int
103h_hid_get_feature_timeout(struct h_hid_device *hid_dev, void *data, size_t size, uint32_t timeout)
104{
105 return hid_dev->get_feature_timeout(hid_dev, data, size, timeout);
106}
107
108/*!
109 * Set a feature report.
110 *
111 * The first byte of the buffer is the report number, to be followed by
112 * the data of the report.
113 *
114 * @public @memberof h_hid_device
115 */
116static inline int
117h_hid_set_feature(struct h_hid_device *hid_dev, const uint8_t *data, size_t size)
118{
119 return hid_dev->set_feature(hid_dev, data, size);
120}
121
122/*!
123 * Get the physical address.
124 *
125 * For USB devices, the string contains the physical path to the device (the
126 * USB controller, hubs, ports, etc). For Bluetooth *devices, the string
127 * contains the hardware (MAC) address of the device.
128 *
129 * @public @memberof h_hid_device
130 */
131static inline int
132h_hid_get_physical_address(struct h_hid_device *hid_dev, uint8_t *data, size_t size)
133{
134 return hid_dev->get_physical_address(hid_dev, data, size);
135}
136
137/*!
138 * Close and free the given device.
139 *
140 * @public @memberof h_hid_device
141 */
142static inline void
144{
145 hid_dev->destroy(hid_dev);
146}
147
148#ifdef XRT_OS_LINUX
149/*!
150 * Open the given path as a hidraw device.
151 *
152 * @see hid_hidraw
153 * @public @memberof h_hid_device
154 */
155int
156h_hid_open_hidraw(const char *path, struct h_hid_device **out_hid);
157#endif
158
159#if defined(XRT_HAVE_HIDAPI) && !defined(XRT_OS_LINUX)
160/*!
161 * Open the given path, as listed by hidapi's hid_enumerate, as a hidapi device.
162 *
163 * @see hid_hidapi
164 * @public @memberof h_hid_device
165 */
166int
167h_hid_open_hidapi(const char *path, struct h_hid_device **out_hid);
168#endif
169
170#ifdef __cplusplus
171} // extern "C"
172#endif
Representing a single hid interface on a device.
Definition h_hid.h:43
static int h_hid_read(struct h_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds)
Read the next input report, if any, from the given hid device.
Definition h_hid.h:68
int h_hid_open_hidraw(const char *path, struct h_hid_device **out_hid)
Open the given path as a hidraw device.
Definition h_hid_hidraw.c:114
static int h_hid_get_physical_address(struct h_hid_device *hid_dev, uint8_t *data, size_t size)
Get the physical address.
Definition h_hid.h:132
static int h_hid_write(struct h_hid_device *hid_dev, const uint8_t *data, size_t size)
Write an output report to the given device.
Definition h_hid.h:79
static int h_hid_get_feature(struct h_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size)
Get a numbered feature report.
Definition h_hid.h:92
static int h_hid_set_feature(struct h_hid_device *hid_dev, const uint8_t *data, size_t size)
Set a feature report.
Definition h_hid.h:117
static int h_hid_get_feature_timeout(struct h_hid_device *hid_dev, void *data, size_t size, uint32_t timeout)
Get a feature report with a timeout.
Definition h_hid.h:103
static void h_hid_destroy(struct h_hid_device *hid_dev)
Close and free the given device.
Definition h_hid.h:143
Auto detect OS and certain features.