Monado OpenXR Runtime
os_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 Ryan Pavlik <ryan.pavlik@collabora.com>
8  *
9  * @ingroup aux_os
10  */
11 
12 #pragma once
13 
14 #include "xrt/xrt_config_os.h"
15 #include <stdint.h>
16 #include <stddef.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 
23 /*!
24  * Representing a single hid interface on a device.
25  */
27 {
28  int (*read)(struct os_hid_device *hid_dev,
29  uint8_t *data,
30  size_t size,
31  int milliseconds);
32 
33  int (*write)(struct os_hid_device *hid_dev,
34  const uint8_t *data,
35  size_t size);
36 
37  int (*get_feature)(struct os_hid_device *hid_dev,
38  uint8_t report_num,
39  uint8_t *data,
40  size_t size);
41 
42  int (*get_feature_timeout)(struct os_hid_device *hid_dev,
43  void *data,
44  size_t size,
45  uint32_t timeout);
46 
47  int (*set_feature)(struct os_hid_device *hid_dev,
48  const uint8_t *data,
49  size_t size);
50 
51  void (*destroy)(struct os_hid_device *hid_dev);
52 };
53 
54 /*!
55  * Read the next input report, if any, from the given hid device.
56  *
57  * If milliseconds are negative, this call blocks indefinitely, 0 polls,
58  * and positive will block for that amount of milliseconds.
59  */
60 static inline int
61 os_hid_read(struct os_hid_device *hid_dev,
62  uint8_t *data,
63  size_t size,
64  int milliseconds)
65 {
66  return hid_dev->read(hid_dev, data, size, milliseconds);
67 }
68 
69 /*!
70  * Write an output report to the given device.
71  */
72 static inline int
73 os_hid_write(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
74 {
75  return hid_dev->write(hid_dev, data, size);
76 }
77 
78 /*!
79  * Get a numbered feature report.
80  *
81  * If the device doesn't have more than one feature report, just request
82  * report 0.
83  */
84 static inline int
85 os_hid_get_feature(struct os_hid_device *hid_dev,
86  uint8_t report_num,
87  uint8_t *data,
88  size_t size)
89 {
90  return hid_dev->get_feature(hid_dev, report_num, data, size);
91 }
92 
93 /*!
94  * Get a feature report with a timeout.
95  */
96 static inline int
97 os_hid_get_feature_timeout(struct os_hid_device *hid_dev,
98  void *data,
99  size_t size,
100  uint32_t timeout)
101 {
102  return hid_dev->get_feature_timeout(hid_dev, data, size, timeout);
103 }
104 
105 /*!
106  * Set a feature report.
107  *
108  * The first byte of the buffer is the report number, to be followed by
109  * the data of the report.
110  */
111 static inline int
112 os_hid_set_feature(struct os_hid_device *hid_dev,
113  const uint8_t *data,
114  size_t size)
115 {
116  return hid_dev->set_feature(hid_dev, data, size);
117 }
118 
119 /*!
120  * Close and free the given device.
121  */
122 static inline void
123 os_hid_destroy(struct os_hid_device *hid_dev)
124 {
125  hid_dev->destroy(hid_dev);
126 }
127 
128 #ifdef XRT_OS_LINUX
129 /*!
130  * Open the given path as a hidraw device.
131  */
132 int
133 os_hid_open_hidraw(const char *path, struct os_hid_device **out_hid);
134 #endif
135 
136 #ifdef __cplusplus
137 } // extern "C"
138 #endif
Auto detect OS and certain features.
int(* read)(struct os_hid_device *hid_dev, uint8_t *data, size_t size, int milliseconds)
Definition: os_hid.h:28
int(* get_feature)(struct os_hid_device *hid_dev, uint8_t report_num, uint8_t *data, size_t size)
Definition: os_hid.h:37
int(* set_feature)(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
Definition: os_hid.h:47
Representing a single hid interface on a device.
Definition: os_hid.h:26
void(* destroy)(struct os_hid_device *hid_dev)
Definition: os_hid.h:51
int(* get_feature_timeout)(struct os_hid_device *hid_dev, void *data, size_t size, uint32_t timeout)
Definition: os_hid.h:42
int(* write)(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
Definition: os_hid.h:33