Blender V5.3
wm_xr_action.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "BLI_listbase.hh"
15#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_string.hh"
19
20#include "GHOST_Xr-api.hh"
21
22#include "MEM_guardedalloc.h"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27#include "wm_xr_intern.hh"
28
29#include <cstring>
30
31namespace blender {
32
33/* -------------------------------------------------------------------- */
39
40static wmXrActionSet *action_set_create(const char *action_set_name)
41{
42 wmXrActionSet *action_set = MEM_new_zeroed<wmXrActionSet>(__func__);
43 action_set->name = BLI_strdup(action_set_name);
44 return action_set;
45}
46
47static void action_set_destroy(void *val)
48{
49 wmXrActionSet *action_set = static_cast<wmXrActionSet *>(val);
50
51 MEM_SAFE_DELETE(action_set->name);
52
53 action_set->active_modal_actions.free_no_destruct();
54 action_set->active_haptic_actions.free_no_destruct();
55
56 MEM_delete(action_set);
57}
58
59static wmXrActionSet *action_set_find(wmXrData *xr, const char *action_set_name)
60{
61 return static_cast<wmXrActionSet *>(
63}
64
65static wmXrAction *action_create(const char *action_name,
66 eXrActionType type,
67 const ListBaseT<XrUserPath> *user_paths,
69 IDProperty *op_properties,
70 const char *haptic_name,
71 const int64_t *haptic_duration,
72 const float *haptic_frequency,
73 const float *haptic_amplitude,
74 eXrOpFlag op_flag,
75 eXrActionFlag action_flag,
76 eXrHapticFlag haptic_flag)
77{
78 wmXrAction *action = MEM_new_zeroed<wmXrAction>(__func__);
79 action->name = BLI_strdup(action_name);
80 action->type = type;
81
82 const uint count = uint(user_paths->count());
83 action->count_subaction_paths = count;
84
85 action->subaction_paths = MEM_new_array_uninitialized<char *>(count, "XrAction_SubactionPaths");
86 for (auto [subaction_idx, user_path] : user_paths->enumerate()) {
87 action->subaction_paths[subaction_idx] = BLI_strdup(user_path.path);
88 }
89
90 size_t size;
91 switch (type) {
93 size = sizeof(bool);
94 break;
95 case XR_FLOAT_INPUT:
96 size = sizeof(float);
97 break;
99 size = sizeof(float) * 2;
100 break;
101 case XR_POSE_INPUT:
102 size = sizeof(GHOST_XrPose);
103 break;
105 return action;
106 }
107 action->states = MEM_new_array_zeroed(count, size, "XrAction_States");
108 action->states_prev = MEM_new_array_zeroed(count, size, "XrAction_StatesPrev");
109
110 const bool is_float_action = ELEM(type, XR_FLOAT_INPUT, XR_VECTOR2F_INPUT);
111 const bool is_button_action = (is_float_action || type == XR_BOOLEAN_INPUT);
112 if (is_float_action) {
113 action->float_thresholds = MEM_new_array_zeroed<float>(count, "XrAction_FloatThresholds");
114 }
115 if (is_button_action) {
116 action->axis_flags = MEM_new_array_zeroed<eXrAxisFlag>(count, "XrAction_AxisFlags");
117 }
118
119 action->ot = ot;
120 action->op_properties = op_properties;
121
122 if (haptic_name) {
123 BLI_assert(is_button_action);
124 action->haptic_name = BLI_strdup(haptic_name);
125 action->haptic_duration = *haptic_duration;
126 action->haptic_frequency = *haptic_frequency;
127 action->haptic_amplitude = *haptic_amplitude;
128 }
129
130 action->op_flag = op_flag;
131 action->action_flag = action_flag;
132 action->haptic_flag = haptic_flag;
133
134 return action;
135}
136
137static void action_destroy(void *val)
138{
139 wmXrAction *action = static_cast<wmXrAction *>(val);
140
141 MEM_SAFE_DELETE(action->name);
142
143 char **subaction_paths = action->subaction_paths;
144 if (subaction_paths) {
145 for (uint i = 0; i < action->count_subaction_paths; ++i) {
146 MEM_SAFE_DELETE(subaction_paths[i]);
147 }
148 MEM_delete(subaction_paths);
149 }
150
151 MEM_SAFE_DELETE_VOID(action->states);
152 MEM_SAFE_DELETE_VOID(action->states_prev);
153
154 MEM_SAFE_DELETE(action->float_thresholds);
155 MEM_SAFE_DELETE(action->axis_flags);
156
157 MEM_SAFE_DELETE(action->haptic_name);
158
159 MEM_delete(action);
160}
161
162static wmXrAction *action_find(wmXrData *xr, const char *action_set_name, const char *action_name)
163{
164 return static_cast<wmXrAction *>(
165 GHOST_XrGetActionCustomdata(xr->runtime->ghost_context, action_set_name, action_name));
166}
167
168bool WM_xr_action_set_create(wmXrData *xr, const char *action_set_name)
169{
170 if (action_set_find(xr, action_set_name)) {
171 return false;
172 }
173
174 wmXrActionSet *action_set = action_set_create(action_set_name);
175
176 GHOST_XrActionSetInfo info{};
177 info.name = action_set_name;
178 info.customdata_free_fn = action_set_destroy;
179 info.customdata = action_set;
180
182 return false;
183 }
184
185 return true;
186}
187
188void WM_xr_action_set_destroy(wmXrData *xr, const char *action_set_name)
189{
190 wmXrActionSet *action_set = action_set_find(xr, action_set_name);
191 if (!action_set) {
192 return;
193 }
194
195 wmXrSessionState *session_state = &xr->runtime->session_state;
196
197 if (action_set == session_state->active_action_set) {
198 if (action_set->controller_grip_action || action_set->controller_aim_action) {
200 action_set->controller_grip_action = action_set->controller_aim_action = nullptr;
201 }
202
203 action_set->active_modal_actions.free_no_destruct();
204 action_set->active_haptic_actions.free_no_destruct();
205
206 session_state->active_action_set = nullptr;
207 }
208
209 GHOST_XrDestroyActionSet(xr->runtime->ghost_context, action_set_name);
210}
211
213 const char *action_set_name,
214 const char *action_name,
215 eXrActionType type,
216 const ListBaseT<XrUserPath> *user_paths,
217 wmOperatorType *ot,
218 IDProperty *op_properties,
219 const char *haptic_name,
220 const int64_t *haptic_duration,
221 const float *haptic_frequency,
222 const float *haptic_amplitude,
223 eXrOpFlag op_flag,
224 eXrActionFlag action_flag,
225 eXrHapticFlag haptic_flag)
226{
227 if (action_find(xr, action_set_name, action_name)) {
228 return false;
229 }
230
231 wmXrAction *action = action_create(action_name,
232 type,
233 user_paths,
234 ot,
235 op_properties,
236 haptic_name,
237 haptic_duration,
238 haptic_frequency,
239 haptic_amplitude,
240 op_flag,
241 action_flag,
242 haptic_flag);
243
244 const uint count = uint(user_paths->count());
245
246 char **subaction_paths = MEM_new_array_zeroed<char *>(count, "XrAction_SubactionPathPointers");
247
248 for (auto [subaction_idx, user_path] : user_paths->enumerate()) {
249 subaction_paths[subaction_idx] = (char *)user_path.path;
250 }
251
252 GHOST_XrActionInfo info{};
253 info.name = action_name;
254 info.count_subaction_paths = count;
255 info.subaction_paths = const_cast<const char **>(subaction_paths);
256 info.states = action->states;
257 info.float_thresholds = action->float_thresholds;
258 info.axis_flags = reinterpret_cast<int16_t *>(action->axis_flags);
259 info.customdata_free_fn = action_destroy;
260 info.customdata = action;
261
262 switch (type) {
263 case XR_BOOLEAN_INPUT:
264 info.type = GHOST_kXrActionTypeBooleanInput;
265 break;
266 case XR_FLOAT_INPUT:
267 info.type = GHOST_kXrActionTypeFloatInput;
268 break;
270 info.type = GHOST_kXrActionTypeVector2fInput;
271 break;
272 case XR_POSE_INPUT:
273 info.type = GHOST_kXrActionTypePoseInput;
274 break;
276 info.type = GHOST_kXrActionTypeVibrationOutput;
277 break;
278 }
279
280 const bool success = GHOST_XrCreateActions(
281 xr->runtime->ghost_context, action_set_name, 1, &info);
282
283 MEM_delete(subaction_paths);
284
285 return success;
286}
287
288void WM_xr_action_destroy(wmXrData *xr, const char *action_set_name, const char *action_name)
289{
290 wmXrActionSet *action_set = action_set_find(xr, action_set_name);
291 if (!action_set) {
292 return;
293 }
294
295 wmXrAction *action = action_find(xr, action_set_name, action_name);
296 if (!action) {
297 return;
298 }
299
300 if ((action_set->controller_grip_action &&
301 STREQ(action_set->controller_grip_action->name, action_name)) ||
302 (action_set->controller_aim_action &&
303 STREQ(action_set->controller_aim_action->name, action_name)))
304 {
305 if (action_set == xr->runtime->session_state.active_action_set) {
307 }
308 action_set->controller_grip_action = action_set->controller_aim_action = nullptr;
309 }
310
311 for (LinkData &ld : action_set->active_modal_actions) {
312 wmXrAction *active_modal_action = static_cast<wmXrAction *>(ld.data);
313 if (STREQ(active_modal_action->name, action_name)) {
314 BLI_freelinkN(&action_set->active_modal_actions, &ld);
315 break;
316 }
317 }
318
319 for (wmXrHapticAction &ha : action_set->active_haptic_actions.items_mutable()) {
320 if (STREQ(ha.action->name, action_name)) {
321 BLI_freelinkN(&action_set->active_haptic_actions, &ha);
322 }
323 }
324
325 GHOST_XrDestroyActions(xr->runtime->ghost_context, action_set_name, 1, &action_name);
326}
327
329 const char *action_set_name,
330 const char *action_name,
331 const char *profile_path,
332 const ListBaseT<XrUserPath> *user_paths,
333 const ListBaseT<XrComponentPath> *component_paths,
334 const float *float_thresholds,
335 const eXrAxisFlag *axis_flags,
336 const wmXrPose *poses)
337{
338 const uint count = uint(user_paths->count());
339 BLI_assert(count == uint(component_paths->count()));
340
341 GHOST_XrActionBindingInfo *binding_infos = MEM_new_array_zeroed<GHOST_XrActionBindingInfo>(
342 count, "XrActionBinding_Infos");
343
344 char **subaction_paths = MEM_new_array_zeroed<char *>(count,
345 "XrActionBinding_SubactionPathPointers");
346
347 for (uint i = 0; i < count; ++i) {
348 GHOST_XrActionBindingInfo *binding_info = &binding_infos[i];
349 const XrUserPath *user_path = static_cast<const XrUserPath *>(BLI_findlink(user_paths, i));
350 const XrComponentPath *component_path = static_cast<const XrComponentPath *>(
351 BLI_findlink(component_paths, i));
352
353 subaction_paths[i] = const_cast<char *>(user_path->path);
354
355 binding_info->component_path = component_path->path;
356 if (float_thresholds) {
357 binding_info->float_threshold = float_thresholds[i];
358 }
359 if (axis_flags) {
360 binding_info->axis_flag = axis_flags[i];
361 }
362 if (poses) {
363 copy_v3_v3(binding_info->pose.position, poses[i].position);
364 copy_qt_qt(binding_info->pose.orientation_quat, poses[i].orientation_quat);
365 }
366 }
367
368 GHOST_XrActionProfileInfo profile_info{};
369 profile_info.action_name = action_name;
370 profile_info.profile_path = profile_path;
371 profile_info.count_subaction_paths = count;
372 profile_info.subaction_paths = const_cast<const char **>(subaction_paths);
373 profile_info.bindings = binding_infos;
374
375 const bool success = GHOST_XrCreateActionBindings(
376 xr->runtime->ghost_context, action_set_name, 1, &profile_info);
377
378 MEM_delete(subaction_paths);
379 MEM_delete(binding_infos);
380
381 return success;
382}
383
385 const char *action_set_name,
386 const char *action_name,
387 const char *profile_path)
388{
390 xr->runtime->ghost_context, action_set_name, 1, &action_name, &profile_path);
391}
392
393bool WM_xr_active_action_set_set(wmXrData *xr, const char *action_set_name, bool delayed)
394{
395 wmXrActionSet *action_set = action_set_find(xr, action_set_name);
396 if (!action_set) {
397 return false;
398 }
399
400 if (delayed) {
401 /* Save name to activate action set later, before next actions sync
402 * (see #wm_xr_session_actions_update()). */
404 return true;
405 }
406
407 {
408 /* Clear any active modal/haptic actions. */
409 wmXrActionSet *active_action_set = xr->runtime->session_state.active_action_set;
410 if (active_action_set) {
411 active_action_set->active_modal_actions.free_no_destruct();
412 active_action_set->active_haptic_actions.free_no_destruct();
413 }
414 }
415
416 xr->runtime->session_state.active_action_set = action_set;
417
418 if (action_set->controller_grip_action && action_set->controller_aim_action) {
420 action_set->controller_grip_action, action_set->controller_aim_action, xr);
421 }
422 else {
424 }
425
426 return true;
427}
428
430 const char *action_set_name,
431 const char *grip_action_name,
432 const char *aim_action_name)
433{
434 wmXrActionSet *action_set = action_set_find(xr, action_set_name);
435 if (!action_set) {
436 return false;
437 }
438
439 wmXrAction *grip_action = action_find(xr, action_set_name, grip_action_name);
440 if (!grip_action) {
441 return false;
442 }
443
444 wmXrAction *aim_action = action_find(xr, action_set_name, aim_action_name);
445 if (!aim_action) {
446 return false;
447 }
448
449 /* Ensure consistent subaction paths. */
450 const uint count = grip_action->count_subaction_paths;
451 if (count != aim_action->count_subaction_paths) {
452 return false;
453 }
454
455 for (uint i = 0; i < count; ++i) {
456 if (!STREQ(grip_action->subaction_paths[i], aim_action->subaction_paths[i])) {
457 return false;
458 }
459 }
460
461 action_set->controller_grip_action = grip_action;
462 action_set->controller_aim_action = aim_action;
463
464 if (action_set == xr->runtime->session_state.active_action_set) {
465 wm_xr_session_controller_data_populate(grip_action, aim_action, xr);
466 }
467
468 return true;
469}
470
472 const char *action_set_name,
473 const char *action_name,
474 const char *subaction_path,
475 wmXrActionState *r_state)
476{
477 const wmXrAction *action = action_find(const_cast<wmXrData *>(xr), action_set_name, action_name);
478 if (!action) {
479 return false;
480 }
481
482 r_state->type = int(action->type);
483
484 /* Find the action state corresponding to the subaction path. */
485 for (uint i = 0; i < action->count_subaction_paths; ++i) {
486 if (STREQ(subaction_path, action->subaction_paths[i])) {
487 switch (action->type) {
488 case XR_BOOLEAN_INPUT:
489 r_state->state_boolean = (static_cast<bool *>(action->states))[i];
490 break;
491 case XR_FLOAT_INPUT:
492 r_state->state_float = (static_cast<float *>(action->states))[i];
493 break;
495 copy_v2_v2(r_state->state_vector2f, (static_cast<float (*)[2]>(action->states))[i]);
496 break;
497 case XR_POSE_INPUT: {
498 const GHOST_XrPose *pose = &((GHOST_XrPose *)action->states)[i];
499 copy_v3_v3(r_state->state_pose.position, pose->position);
500 copy_qt_qt(r_state->state_pose.orientation_quat, pose->orientation_quat);
501 break;
502 }
505 break;
506 }
507 return true;
508 }
509 }
510
511 return false;
512}
513
515 const char *action_set_name,
516 const char *action_name,
517 const char *subaction_path,
518 const int64_t *duration,
519 const float *frequency,
520 const float *amplitude)
521{
523 action_set_name,
524 action_name,
525 subaction_path,
526 duration,
527 frequency,
528 amplitude) ?
529 true :
530 false;
531}
532
534 const char *action_set_name,
535 const char *action_name,
536 const char *subaction_path)
537{
539 xr->runtime->ghost_context, action_set_name, action_name, subaction_path);
540}
541 /* XR-Action API. */
543
544} // namespace blender
Abstraction for XR (VR, AR, MR, ..) access via OpenXR.
void GHOST_XrDestroyActionSet(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:121
int GHOST_XrCreateActions(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const GHOST_XrActionInfo *infos)
Definition GHOST_Xr.cc:127
void GHOST_XrDestroyActions(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const char *const *action_names)
Definition GHOST_Xr.cc:137
int GHOST_XrCreateActionSet(GHOST_IXrContext *xr_context, const GHOST_XrActionSetInfo *info)
Definition GHOST_Xr.cc:114
void * GHOST_XrGetActionCustomdata(GHOST_IXrContext *xr_context, const char *action_set_name, const char *action_name)
Definition GHOST_Xr.cc:216
int GHOST_XrCreateActionBindings(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const GHOST_XrActionProfileInfo *infos)
Definition GHOST_Xr.cc:146
void GHOST_XrStopHapticAction(GHOST_IXrContext *xr_context_handle, const char *action_set_name, const char *action_name, const char *subaction_path)
Definition GHOST_Xr.cc:199
void * GHOST_XrGetActionSetCustomdata(GHOST_IXrContext *xr_context, const char *action_set_name)
Definition GHOST_Xr.cc:209
void GHOST_XrDestroyActionBindings(GHOST_IXrContext *xr_context, const char *action_set_name, uint32_t count, const char *const *action_names, const char *const *profile_paths)
Definition GHOST_Xr.cc:157
int GHOST_XrApplyHapticAction(GHOST_IXrContext *xr_context_handle, const char *action_set_name, const char *action_name, const char *subaction_path, const int64_t *duration, const float *frequency, const float *amplitude)
Definition GHOST_Xr.cc:183
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
#define frequency(freq)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ XR_POSE_INPUT
@ XR_BOOLEAN_INPUT
@ XR_FLOAT_INPUT
@ XR_VIBRATION_OUTPUT
@ XR_VECTOR2F_INPUT
static SpaceLink * action_create(const ScrArea *area, const Scene *scene)
void wm_xr_session_controller_data_populate(const wmXrAction *grip_action, const wmXrAction *aim_action, wmXrData *xr)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
static wmXrAction * action_find(wmXrData *xr, const char *action_set_name, const char *action_name)
static wmXrActionSet * action_set_create(const char *action_set_name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void WM_xr_action_binding_destroy(wmXrData *xr, const char *action_set_name, const char *action_name, const char *profile_path)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void action_set_destroy(void *val)
bool WM_xr_controller_pose_actions_set(wmXrData *xr, const char *action_set_name, const char *grip_action_name, const char *aim_action_name)
void wm_xr_session_controller_data_clear(wmXrSessionState *state)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
static wmXrActionSet * action_set_find(wmXrData *xr, const char *action_set_name)
void WM_xr_action_set_destroy(wmXrData *xr, const char *action_set_name)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void copy_qt_qt(float q[4], const float a[4])
void WM_xr_haptic_action_stop(wmXrData *xr, const char *action_set_name, const char *action_name, const char *subaction_path)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
bool WM_xr_action_create(wmXrData *xr, const char *action_set_name, const char *action_name, eXrActionType type, const ListBaseT< XrUserPath > *user_paths, wmOperatorType *ot, IDProperty *op_properties, const char *haptic_name, const int64_t *haptic_duration, const float *haptic_frequency, const float *haptic_amplitude, eXrOpFlag op_flag, eXrActionFlag action_flag, eXrHapticFlag haptic_flag)
bool WM_xr_active_action_set_set(wmXrData *xr, const char *action_set_name, bool delayed)
static void action_destroy(void *val)
bool WM_xr_action_state_get(const wmXrData *xr, const char *action_set_name, const char *action_name, const char *subaction_path, wmXrActionState *r_state)
bool WM_xr_haptic_action_apply(wmXrData *xr, const char *action_set_name, const char *action_name, const char *subaction_path, const int64_t *duration, const float *frequency, const float *amplitude)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
void WM_xr_action_destroy(wmXrData *xr, const char *action_set_name, const char *action_name)
bool WM_xr_action_binding_create(wmXrData *xr, const char *action_set_name, const char *action_name, const char *profile_path, const ListBaseT< XrUserPath > *user_paths, const ListBaseT< XrComponentPath > *component_paths, const float *float_thresholds, const eXrAxisFlag *axis_flags, const wmXrPose *poses)
bool WM_xr_action_set_create(wmXrData *xr, const char *action_set_name)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
signed short int16_t
Definition BKE_text.h:77
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
wmXrAction * controller_aim_action
ListBaseT< LinkData > active_modal_actions
wmXrAction * controller_grip_action
ListBaseT< wmXrHapticAction > active_haptic_actions
IDProperty * op_properties
eXrAxisFlag * axis_flags
eXrHapticFlag haptic_flag
eXrActionFlag action_flag
struct wmOperatorType * ot
unsigned int count_subaction_paths
struct wmXrRuntimeData * runtime
wmXrSessionState session_state
GHOST_IXrContext * ghost_context
struct wmXrActionSet * active_action_set