Monado OpenXR Runtime
oxr_input_transform.h
Go to the documentation of this file.
1 // Copyright 2018-2020, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Defines ways of performing (possibly multi-step) conversions of input
6  * data.
7  * @author Rylie Pavlik <rylie.pavlik@collabora.com>
8  * @ingroup oxr_input_transform
9  */
10 
11 #pragma once
12 
13 #include "xrt/xrt_device.h"
14 #include "oxr_objects.h"
15 
16 // we need no platform-specific defines from OpenXR.
17 #include "openxr/openxr.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 struct oxr_logger;
24 struct oxr_sink_logger;
25 struct oxr_action;
26 struct oxr_action_cache;
27 
28 /*!
29  * @defgroup oxr_input_transform OpenXR input transformation
30  *
31  *
32  * @ingroup oxr_input
33  * @{
34  */
35 
36 /*!
37  * Tag for the input transform
38  *
39  * @see oxr_input_transform
40  */
42 {
43  /*!
44  * Invalid value, so that zero-initialization without further assignment
45  * is caught.
46  */
48 
49  /*!
50  * Do not modify the input.
51  *
52  * This is only used as the root/head transform, to set the initial
53  * type.
54  */
56 
57  /*!
58  * Get the X component of a 2D float input of any range.
59  */
61 
62  /*!
63  * Get the Y component of a 2D float input of any range.
64  */
66 
67  /*!
68  * Apply a threshold to any 1D float input to make a bool.
69  *
70  * This transform type has data:
71  *
72  * @see oxr_input_transform_threshold_data
73  */
75 
76  /*!
77  * Convert a bool to some range of 1D float input.
78  *
79  * This transform type has data:
80  *
81  * @see oxr_input_transform_bool_to_vec1_data
82  */
84 
85  /*!
86  * Interpret a 2D joystick or trackpad as a dpad.
87  *
88  * This transform type has data:
89  *
90  * @see oxr_input_transform_dpad_data
91  */
93 };
94 
95 struct oxr_input_transform;
96 /*!
97  * Data required for INPUT_TRANSFORM_THRESHOLD
98  */
100 {
101  //! The "greater-than" threshold value
102  float threshold;
103 
104  //! If true, values greater than `threshold` are false instead of
105  //! true
106  bool invert;
107 };
108 
109 /*!
110  * Data required for INPUT_TRANSFORM_BOOL_TO_VEC1
111  * @see oxr_input_transform
112  * @see INPUT_TRANSFORM_BOOL_TO_VEC1
113  */
115 {
116  //! Value produced if bool is true.
117  float true_val;
118 
119  //! Value produced if bool is false.
120  float false_val;
121 };
122 
123 /*!
124  * Data required for INPUT_TRANSFORM_DPAD
125  * @see oxr_input_transform
126  * @see INPUT_TRANSFORM_DPAD
127  */
129 {
130  enum oxr_dpad_region bound_region;
131  enum oxr_dpad_region active_regions;
132  struct oxr_dpad_settings settings;
133  enum xrt_input_type activation_input_type;
134  struct xrt_input *activation_input;
135  bool already_active;
136 };
137 
138 /*!
139  * Variant type for input transforms.
140  *
141  * Some values for @p type do not have any additional data in the union
142  */
144 {
145  /*!
146  * The type of this transform.
147  *
148  * Some values imply that a member of oxr_input_transform::data is
149  * populated.
150  */
152 
153  //! The type output by this transform.
155 
156  union {
157  /*!
158  * Populated when oxr_input_transform::type is
159  * INPUT_TRANSFORM_THRESHOLD
160  */
162  /*!
163  * Populated when oxr_input_transform::type is
164  * INPUT_TRANSFORM_BOOL_TO_VEC1
165  */
167  /*!
168  * Populated when oxr_input_transform::type is
169  * INPUT_TRANSFORM_DPAD
170  */
172  } data;
173 };
174 
175 /*!
176  * An input value enum with the associated tag required to interpret it.
177  */
179 {
180  enum xrt_input_type type;
181  union xrt_input_value value;
182 };
183 
184 /*!
185  * Destroy an array of input transforms.
186  *
187  * Performs null check and sets to NULL.
188  *
189  * @public @memberof oxr_input_transform
190  */
191 void
192 oxr_input_transform_destroy(struct oxr_input_transform **transform_ptr);
193 
194 /*!
195  * Apply an array of input transforms.
196  *
197  * @param[in] transforms An array of input transforms
198  * @param[in] transform_count The number of elements in @p transform
199  * @param[in] input The input value and type
200  * @param[out] out The transformed value and type
201  *
202  * @returns false if there was a type mismatch
203  * @public @memberof oxr_input_transform
204  */
205 bool
207  size_t transform_count,
208  const struct oxr_input_value_tagged *input,
209  struct oxr_input_value_tagged *out);
210 
211 /*!
212  * Allocate an identity transform serving as the root/head of the transform
213  * chain.
214  *
215  * Usually called automatically by @ref oxr_input_transform_create_chain
216  *
217  * @param[in,out] transform A pointer to the @ref oxr_input_transform struct to
218  * initialize.
219  * @param[in] input_type The native input type from the device
220  *
221  * @public @memberof oxr_input_transform
222  */
223 bool
224 oxr_input_transform_init_root(struct oxr_input_transform *transform, enum xrt_input_type input_type);
225 
226 /*!
227  * Allocate a transform to get the X component of a Vec2.
228  *
229  * Usually called automatically by @ref oxr_input_transform_create_chain
230  *
231  * @param[in,out] transform A pointer to the @ref oxr_input_transform struct to
232  * initialize.
233  * @param[in] parent The preceding transform
234  *
235  * @pre parent->result_type is @ref XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE
236  *
237  * @public @memberof oxr_input_transform
238  */
239 bool
240 oxr_input_transform_init_vec2_get_x(struct oxr_input_transform *transform, const struct oxr_input_transform *parent);
241 
242 /*!
243  * Allocate a transform to get the Y component of a Vec2.
244  *
245  * Usually called automatically by @ref oxr_input_transform_create_chain
246  *
247  * @param[in,out] transform A pointer to the @ref oxr_input_transform struct to
248  * initialize.
249  * @param[in] parent The preceding transform
250  *
251  * @pre parent->result_type is @ref XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE
252  *
253  * @public @memberof oxr_input_transform
254  */
255 bool
256 oxr_input_transform_init_vec2_get_y(struct oxr_input_transform *transform, const struct oxr_input_transform *parent);
257 
258 /*!
259  * Allocate a transform to threshold a float to a bool.
260  *
261  * Usually called automatically by @ref oxr_input_transform_create_chain
262  *
263  * @param[in,out] transform A pointer to the @ref oxr_input_transform struct to
264  * initialize.
265  * @param[in] parent The preceding transform
266  * @param[in] threshold Threshold value to use
267  * @param[in] invert If true, condition is "value <= threshold" instead of
268  * "value > threshold"
269  *
270  * @pre parent->result_type is @ref XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE or
271  * @ref XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE
272  *
273  * @public @memberof oxr_input_transform
274  */
275 bool
277  const struct oxr_input_transform *parent,
278  float threshold,
279  bool invert);
280 
281 /*!
282  * Allocate a transform to turn a bool into an arbitrary 1D float.
283  *
284  * Usually called automatically by @ref oxr_input_transform_create_chain
285  *
286  * @param[in,out] transform A pointer to the @ref oxr_input_transform struct to
287  * initialize.
288  * @param[in] parent The preceding transform
289  * @param[in] result_type Either XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE or
290  * XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE
291  * @param[in] true_val Value to return when true
292  * @param[in] false_val Value to return when false
293  *
294  * @pre parent->result_type is XRT_INPUT_TYPE_BOOLEAN
295  *
296  * @public @memberof oxr_input_transform
297  */
298 bool
300  const struct oxr_input_transform *parent,
301  enum xrt_input_type result_type,
302  float true_val,
303  float false_val);
304 
305 /*!
306  * Create a transform array to convert @p input_type to @p result_type.
307  *
308  * @todo This should be configured using knowledge from the device as well as user options/policy.
309  *
310  * @param[in] log The logger
311  * @param[in] slog The sink logger
312  * @param[in] input_type The type of input received from the hardware
313  * @param[in] result_type The type of input the application requested
314  * @param[in] action_name The action name - used for error prints only
315  * @param[in] bound_path_string The path name string that has been bound.
316  * @param[out] out_transforms A pointer that will be populated with the output
317  * array's address, or NULL.
318  * @param[out] out_transform_count Where to populate the array size
319  * @return false if not possible
320  *
321  * @relates oxr_input_transform
322  */
323 bool
325  struct oxr_sink_logger *slog,
326  enum xrt_input_type input_type,
327  XrActionType result_type,
328  const char *action_name,
329  const char *bound_path_string,
330  struct oxr_input_transform **out_transforms,
331  size_t *out_transform_count);
332 
333 /*!
334  * Create a transform array to process a 2D input plus activation input to a dpad.
335  *
336  * @param[in] log The logger
337  * @param[in] slog The sink logger
338  * @param[in] input_type The type of input received from the hardware
339  * @param[in] result_type The type of input the application requested
340  * @param[in] bound_path_string The path name string that has been bound.
341  * @param[in] dpad_settings The dpad settings provided by the application
342  * as a binding modification. NULL means use the defaults as per the spec.
343  * @param[in] dpad_region The dpad region associated with this binding
344  * @param[in] activation_input_type The type of the activation input
345  * @param[in] activation_input The activation input, i.e. the input used
346  * to determine when the emulated dpad buttons should activate
347  * @param[out] out_transforms A pointer that will be populated with the output
348  * array's address, or NULL.
349  * @param[out] out_transform_count Where to populate the array size
350  * @return false if not possible
351  *
352  * @relates oxr_input_transform
353  */
354 bool
356  struct oxr_sink_logger *slog,
357  enum xrt_input_type input_type,
358  XrActionType result_type,
359  const char *bound_path_string,
360  struct oxr_dpad_binding_modification *dpad_binding_modification,
361  enum oxr_dpad_region dpad_region,
362  enum xrt_input_type activation_input_type,
363  struct xrt_input *activation_input,
364  struct oxr_input_transform **out_transforms,
365  size_t *out_transform_count);
366 
367 /*!
368  * @}
369  */
370 
371 
372 #ifdef __cplusplus
373 }
374 #endif
bool oxr_input_transform_process(struct oxr_input_transform *transforms, size_t transform_count, const struct oxr_input_value_tagged *input, struct oxr_input_value_tagged *out)
Apply an array of input transforms.
Definition: oxr_input_transform.c:162
bool oxr_input_transform_init_threshold(struct oxr_input_transform *transform, const struct oxr_input_transform *parent, float threshold, bool invert)
Allocate a transform to threshold a float to a bool.
Definition: oxr_input_transform.c:120
bool oxr_input_transform_init_vec2_get_x(struct oxr_input_transform *transform, const struct oxr_input_transform *parent)
Allocate a transform to get the X component of a Vec2.
Definition: oxr_input_transform.c:68
bool oxr_input_transform_init_bool_to_vec1(struct oxr_input_transform *transform, const struct oxr_input_transform *parent, enum xrt_input_type result_type, float true_val, float false_val)
Allocate a transform to turn a bool into an arbitrary 1D float.
Definition: oxr_input_transform.c:140
bool oxr_input_transform_init_vec2_get_y(struct oxr_input_transform *transform, const struct oxr_input_transform *parent)
Allocate a transform to get the Y component of a Vec2.
Definition: oxr_input_transform.c:82
void oxr_input_transform_destroy(struct oxr_input_transform **transform_ptr)
Destroy an array of input transforms.
Definition: oxr_input_transform.c:46
bool oxr_input_transform_create_chain_dpad(struct oxr_logger *log, struct oxr_sink_logger *slog, enum xrt_input_type input_type, XrActionType result_type, const char *bound_path_string, struct oxr_dpad_binding_modification *dpad_binding_modification, enum oxr_dpad_region dpad_region, enum xrt_input_type activation_input_type, struct xrt_input *activation_input, struct oxr_input_transform **out_transforms, size_t *out_transform_count)
Create a transform array to process a 2D input plus activation input to a dpad.
Definition: oxr_input_transform.c:431
oxr_input_transform_type
Tag for the input transform.
Definition: oxr_input_transform.h:42
bool oxr_input_transform_create_chain(struct oxr_logger *log, struct oxr_sink_logger *slog, enum xrt_input_type input_type, XrActionType result_type, const char *action_name, const char *bound_path_string, struct oxr_input_transform **out_transforms, size_t *out_transform_count)
Create a transform array to convert input_type to result_type.
Definition: oxr_input_transform.c:365
bool oxr_input_transform_init_root(struct oxr_input_transform *transform, enum xrt_input_type input_type)
Allocate an identity transform serving as the root/head of the transform chain.
Definition: oxr_input_transform.c:57
@ INPUT_TRANSFORM_INVALID
Invalid value, so that zero-initialization without further assignment is caught.
Definition: oxr_input_transform.h:47
@ INPUT_TRANSFORM_THRESHOLD
Apply a threshold to any 1D float input to make a bool.
Definition: oxr_input_transform.h:74
@ INPUT_TRANSFORM_DPAD
Interpret a 2D joystick or trackpad as a dpad.
Definition: oxr_input_transform.h:92
@ INPUT_TRANSFORM_IDENTITY
Do not modify the input.
Definition: oxr_input_transform.h:55
@ INPUT_TRANSFORM_BOOL_TO_VEC1
Convert a bool to some range of 1D float input.
Definition: oxr_input_transform.h:83
@ INPUT_TRANSFORM_VEC2_GET_Y
Get the Y component of a 2D float input of any range.
Definition: oxr_input_transform.h:65
@ INPUT_TRANSFORM_VEC2_GET_X
Get the X component of a 2D float input of any range.
Definition: oxr_input_transform.h:60
oxr_dpad_region
Region of a dpad binding that an input is mapped to.
Definition: oxr_defines.h:73
xrt_input_type
Base type of this inputs.
Definition: xrt_defines.h:784
The objects representing OpenXR handles, and prototypes for internal functions used in the state trac...
The set of inputs/outputs for a single sub-action path for an action.
Definition: oxr_objects.h:2079
A single action.
Definition: oxr_objects.h:2422
dpad binding extracted from XrInteractionProfileDpadBindingEXT
Definition: oxr_objects.h:1812
dpad settings we need extracted from XrInteractionProfileDpadBindingEXT
Definition: oxr_objects.h:1800
Data required for INPUT_TRANSFORM_BOOL_TO_VEC1.
Definition: oxr_input_transform.h:115
float false_val
Value produced if bool is false.
Definition: oxr_input_transform.h:120
float true_val
Value produced if bool is true.
Definition: oxr_input_transform.h:117
Data required for INPUT_TRANSFORM_DPAD.
Definition: oxr_input_transform.h:129
Data required for INPUT_TRANSFORM_THRESHOLD.
Definition: oxr_input_transform.h:100
bool invert
If true, values greater than threshold are false instead of true.
Definition: oxr_input_transform.h:106
float threshold
The "greater-than" threshold value.
Definition: oxr_input_transform.h:102
Variant type for input transforms.
Definition: oxr_input_transform.h:144
enum xrt_input_type result_type
The type output by this transform.
Definition: oxr_input_transform.h:154
struct oxr_input_transform_threshold_data threshold
Populated when oxr_input_transform::type is INPUT_TRANSFORM_THRESHOLD.
Definition: oxr_input_transform.h:161
enum oxr_input_transform_type type
The type of this transform.
Definition: oxr_input_transform.h:151
struct oxr_input_transform_dpad_data dpad_state
Populated when oxr_input_transform::type is INPUT_TRANSFORM_DPAD.
Definition: oxr_input_transform.h:171
struct oxr_input_transform_bool_to_vec1_data bool_to_vec1
Populated when oxr_input_transform::type is INPUT_TRANSFORM_BOOL_TO_VEC1.
Definition: oxr_input_transform.h:166
An input value enum with the associated tag required to interpret it.
Definition: oxr_input_transform.h:179
Logger struct that lives on the stack, one for each call client call.
Definition: oxr_logger.h:40
Allocate on the stack, make sure to zero initialize.
Definition: oxr_logger.h:88
A single named input, that sits on a xrt_device.
Definition: xrt_device.h:161
A union of all input types.
Definition: xrt_defines.h:1249
Header defining an xrt display or controller device.