Blender V5.3
GHOST_XrAction.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cassert>
10#include <cstring>
11
12#include "GHOST_Types.hh"
13
14#include "GHOST_XrException.hh"
15#include "GHOST_Xr_intern.hh"
16
17#include "GHOST_XrAction.hh"
18
19#include "GHOST_utildefines.hh"
20
21/* -------------------------------------------------------------------- */
25
27 XrAction action,
28 const char *action_name,
29 const char *profile_path,
30 XrPath subaction_path,
31 const char *subaction_path_str,
32 const GHOST_XrPose &pose)
33{
34 XrActionSpaceCreateInfo action_space_info{XR_TYPE_ACTION_SPACE_CREATE_INFO};
35 action_space_info.action = action;
36 action_space_info.subactionPath = subaction_path;
37 copy_ghost_pose_to_openxr_pose(pose, action_space_info.poseInActionSpace);
38
39 CHECK_XR(xrCreateActionSpace(session, &action_space_info, &space_),
40 (std::string("Failed to create space \"") + subaction_path_str + "\" for action \"" +
41 action_name + "\" and profile \"" + profile_path + "\".")
42 .data());
43}
44
46{
47 if (space_ != XR_NULL_HANDLE) {
48 CHECK_XR_ASSERT(xrDestroySpace(space_));
49 }
50}
51
53{
54 return space_;
55}
56
58
59/* -------------------------------------------------------------------- */
63
65 XrSession session,
66 XrAction action,
67 GHOST_XrActionType type,
68 const GHOST_XrActionProfileInfo &info)
69{
70 CHECK_XR(xrStringToPath(instance, info.profile_path, &profile_),
71 (std::string("Failed to get interaction profile path \"") + info.profile_path + "\".")
72 .data());
73
74 const bool is_float_action = ELEM(
75 type, GHOST_kXrActionTypeFloatInput, GHOST_kXrActionTypeVector2fInput);
76 const bool is_button_action = (is_float_action || type == GHOST_kXrActionTypeBooleanInput);
77 const bool is_pose_action = (type == GHOST_kXrActionTypePoseInput);
78
79 /* Create bindings. */
80 XrInteractionProfileSuggestedBinding bindings_info{
81 XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING};
82 bindings_info.interactionProfile = profile_;
83 bindings_info.countSuggestedBindings = 1;
84
85 for (uint32_t subaction_idx = 0; subaction_idx < info.count_subaction_paths; ++subaction_idx) {
86 const char *subaction_path_str = info.subaction_paths[subaction_idx];
87 const GHOST_XrActionBindingInfo &binding_info = info.bindings[subaction_idx];
88
89 const std::string interaction_path = std::string(subaction_path_str) +
90 binding_info.component_path;
91 if (bindings_.find(interaction_path) != bindings_.end()) {
92 continue;
93 }
94
95 XrActionSuggestedBinding sbinding;
96 sbinding.action = action;
97 CHECK_XR(xrStringToPath(instance, interaction_path.data(), &sbinding.binding),
98 (std::string("Failed to get interaction path \"") + interaction_path + "\".").data());
99 bindings_info.suggestedBindings = &sbinding;
100
101 /* Although the bindings will be re-suggested in GHOST_XrSession::attachActionSets(), it
102 * greatly improves error checking to suggest them here first. */
103 CHECK_XR(xrSuggestInteractionProfileBindings(instance, &bindings_info),
104 (std::string("Failed to create binding for action \"") + info.action_name +
105 "\" and profile \"" + info.profile_path +
106 "\". Are the action and profile paths correct?")
107 .data());
108
109 bindings_.insert({interaction_path, sbinding.binding});
110
111 if (subaction_data_.find(subaction_path_str) == subaction_data_.end()) {
112 std::map<std::string, GHOST_XrSubactionData>::iterator it =
113 subaction_data_
114 .emplace(
115 std::piecewise_construct, std::make_tuple(subaction_path_str), std::make_tuple())
116 .first;
117 GHOST_XrSubactionData &subaction = it->second;
118
119 CHECK_XR(xrStringToPath(instance, subaction_path_str, &subaction.subaction_path),
120 (std::string("Failed to get user path \"") + subaction_path_str + "\".").data());
121
122 if (is_float_action || is_button_action) {
123 if (is_float_action) {
124 subaction.float_threshold = binding_info.float_threshold;
125 }
126 if (is_button_action) {
127 subaction.axis_flag = binding_info.axis_flag;
128 }
129 }
130 else if (is_pose_action) {
131 /* Create action space for pose bindings. */
132 subaction.space = std::make_unique<GHOST_XrActionSpace>(session,
133 action,
134 info.action_name,
135 info.profile_path,
136 subaction.subaction_path,
137 subaction_path_str,
138 binding_info.pose);
139 }
140 }
141 }
142}
143
145{
146 return profile_;
147}
148
150{
151 for (auto &[subaction_path_str, subaction] : subaction_data_) {
152 if (subaction.subaction_path == subaction_path) {
153 return &subaction;
154 }
155 }
156 return nullptr;
157}
158
160 XrAction action, std::map<XrPath, std::vector<XrActionSuggestedBinding>> &r_bindings) const
161{
162 std::map<XrPath, std::vector<XrActionSuggestedBinding>>::iterator it = r_bindings.find(profile_);
163 if (it == r_bindings.end()) {
164 it = r_bindings.emplace(std::piecewise_construct, std::make_tuple(profile_), std::make_tuple())
165 .first;
166 }
167
168 std::vector<XrActionSuggestedBinding> &sbindings = it->second;
169
170 for (auto &[path, binding] : bindings_) {
171 XrActionSuggestedBinding sbinding;
172 sbinding.action = action;
173 sbinding.binding = binding;
174
175 sbindings.push_back(std::move(sbinding));
176 }
177}
178
180
181/* -------------------------------------------------------------------- */
185
187 XrActionSet action_set,
188 const GHOST_XrActionInfo &info)
189 : type_(info.type),
190 states_(info.states),
191 float_thresholds_(info.float_thresholds),
192 axis_flags_(info.axis_flags),
193 custom_data_(
194 std::make_unique<GHOST_C_CustomDataWrapper>(info.customdata, info.customdata_free_fn))
195{
196 subaction_paths_.resize(info.count_subaction_paths);
197
198 for (uint32_t i = 0; i < info.count_subaction_paths; ++i) {
199 const char *subaction_path_str = info.subaction_paths[i];
200 CHECK_XR(xrStringToPath(instance, subaction_path_str, &subaction_paths_[i]),
201 (std::string("Failed to get user path \"") + subaction_path_str + "\".").data());
202 subaction_indices_.insert({subaction_path_str, i});
203 }
204
205 XrActionCreateInfo action_info{XR_TYPE_ACTION_CREATE_INFO};
206 strcpy(action_info.actionName, info.name);
207
208 /* Just use same name for localized. This can be changed in the future if necessary. */
209 strcpy(action_info.localizedActionName, info.name);
210
211 switch (info.type) {
212 case GHOST_kXrActionTypeBooleanInput:
213 action_info.actionType = XR_ACTION_TYPE_BOOLEAN_INPUT;
214 break;
215 case GHOST_kXrActionTypeFloatInput:
216 action_info.actionType = XR_ACTION_TYPE_FLOAT_INPUT;
217 break;
218 case GHOST_kXrActionTypeVector2fInput:
219 action_info.actionType = XR_ACTION_TYPE_VECTOR2F_INPUT;
220 break;
221 case GHOST_kXrActionTypePoseInput:
222 action_info.actionType = XR_ACTION_TYPE_POSE_INPUT;
223 break;
224 case GHOST_kXrActionTypeVibrationOutput:
225 action_info.actionType = XR_ACTION_TYPE_VIBRATION_OUTPUT;
226 break;
227 }
228 action_info.countSubactionPaths = info.count_subaction_paths;
229 action_info.subactionPaths = subaction_paths_.data();
230
231 CHECK_XR(xrCreateAction(action_set, &action_info, &action_),
232 (std::string("Failed to create action \"") + info.name +
233 "\". Action name and/or paths are invalid. Name must not contain upper "
234 "case letters or special characters other than '-', '_', or '.'.")
235 .data());
236}
237
239{
240 if (action_ != XR_NULL_HANDLE) {
241 CHECK_XR_ASSERT(xrDestroyAction(action_));
242 }
243}
244
245bool GHOST_XrAction::createBinding(XrInstance instance,
246 XrSession session,
247 const GHOST_XrActionProfileInfo &info)
248{
249 if (profiles_.find(info.profile_path) != profiles_.end()) {
250 return false;
251 }
252
253 profiles_.emplace(std::piecewise_construct,
254 std::make_tuple(info.profile_path),
255 std::make_tuple(instance, session, action_, type_, info));
256
257 return true;
258}
259
260void GHOST_XrAction::destroyBinding(const char *profile_path)
261{
262 /* It's possible nothing is removed. */
263 profiles_.erase(profile_path);
264}
265
266void GHOST_XrAction::updateState(XrSession session,
267 const char *action_name,
268 XrSpace reference_space,
269 const XrTime &predicted_display_time)
270{
271 const bool is_float_action = ELEM(
272 type_, GHOST_kXrActionTypeFloatInput, GHOST_kXrActionTypeVector2fInput);
273 const bool is_button_action = (is_float_action || type_ == GHOST_kXrActionTypeBooleanInput);
274
275 XrActionStateGetInfo state_info{XR_TYPE_ACTION_STATE_GET_INFO};
276 state_info.action = action_;
277
278 const size_t count_subaction_paths = subaction_paths_.size();
279 for (size_t subaction_idx = 0; subaction_idx < count_subaction_paths; ++subaction_idx) {
280 state_info.subactionPath = subaction_paths_[subaction_idx];
281
282 /* Set subaction data based on current interaction profile. */
283 XrInteractionProfileState profile_state{XR_TYPE_INTERACTION_PROFILE_STATE};
284 CHECK_XR(xrGetCurrentInteractionProfile(session, state_info.subactionPath, &profile_state),
285 "Failed to get current interaction profile.");
286
287 const GHOST_XrSubactionData *subaction = nullptr;
288 for (auto &[profile_path, profile] : profiles_) {
289 if (profile.getProfile() == profile_state.interactionProfile) {
290 subaction = profile.getSubaction(state_info.subactionPath);
291 break;
292 }
293 }
294
295 if (subaction != nullptr) {
296 if (is_float_action) {
297 float_thresholds_[subaction_idx] = subaction->float_threshold;
298 }
299 if (is_button_action) {
300 axis_flags_[subaction_idx] = subaction->axis_flag;
301 }
302 }
303
304 switch (type_) {
305 case GHOST_kXrActionTypeBooleanInput: {
306 XrActionStateBoolean state{XR_TYPE_ACTION_STATE_BOOLEAN};
307 CHECK_XR(xrGetActionStateBoolean(session, &state_info, &state),
308 (std::string("Failed to get state for boolean action \"") + action_name + "\".")
309 .data());
310 if (state.isActive) {
311 ((bool *)states_)[subaction_idx] = state.currentState;
312 }
313 break;
314 }
315 case GHOST_kXrActionTypeFloatInput: {
316 XrActionStateFloat state{XR_TYPE_ACTION_STATE_FLOAT};
317 CHECK_XR(
318 xrGetActionStateFloat(session, &state_info, &state),
319 (std::string("Failed to get state for float action \"") + action_name + "\".").data());
320 if (state.isActive) {
321 ((float *)states_)[subaction_idx] = state.currentState;
322 }
323 break;
324 }
325 case GHOST_kXrActionTypeVector2fInput: {
326 XrActionStateVector2f state{XR_TYPE_ACTION_STATE_VECTOR2F};
327 CHECK_XR(xrGetActionStateVector2f(session, &state_info, &state),
328 (std::string("Failed to get state for vector2f action \"") + action_name + "\".")
329 .data());
330 if (state.isActive) {
331 memcpy(((float (*)[2])states_)[subaction_idx], &state.currentState, sizeof(float[2]));
332 }
333 break;
334 }
335 case GHOST_kXrActionTypePoseInput: {
336 /* Check for valid display time to avoid an error in #xrLocateSpace(). */
337 if (predicted_display_time > 0) {
338 XrActionStatePose state{XR_TYPE_ACTION_STATE_POSE};
339 CHECK_XR(xrGetActionStatePose(session, &state_info, &state),
340 (std::string("Failed to get state for pose action \"") + action_name + "\".")
341 .data());
342 ((GHOST_XrPose *)states_)[subaction_idx].is_active = state.isActive;
343 if (state.isActive) {
344 XrSpace pose_space = ((subaction != nullptr) && (subaction->space != nullptr)) ?
345 subaction->space->getSpace() :
346 XR_NULL_HANDLE;
347 if (pose_space != XR_NULL_HANDLE) {
348 XrSpaceLocation space_location{XR_TYPE_SPACE_LOCATION};
349 CHECK_XR(
350 xrLocateSpace(
351 pose_space, reference_space, predicted_display_time, &space_location),
352 (std::string("Failed to query pose space for action \"") + action_name + "\".")
353 .data());
354 copy_openxr_pose_to_ghost_pose(space_location.pose,
355 ((GHOST_XrPose *)states_)[subaction_idx]);
356 }
357 }
358 }
359 break;
360 }
361 case GHOST_kXrActionTypeVibrationOutput: {
362 break;
363 }
364 }
365 }
366}
367
369 const char *action_name,
370 const char *subaction_path_str,
371 const int64_t &duration,
372 const float &frequency,
373 const float &amplitude)
374{
375 XrHapticVibration vibration{XR_TYPE_HAPTIC_VIBRATION};
376 vibration.duration = (duration == 0) ? XR_MIN_HAPTIC_DURATION :
377 static_cast<XrDuration>(duration);
378 vibration.frequency = frequency;
379 vibration.amplitude = amplitude;
380
381 XrHapticActionInfo haptic_info{XR_TYPE_HAPTIC_ACTION_INFO};
382 haptic_info.action = action_;
383
384 if (subaction_path_str != nullptr) {
385 SubactionIndexMap::iterator it = subaction_indices_.find(subaction_path_str);
386 if (it != subaction_indices_.end()) {
387 haptic_info.subactionPath = subaction_paths_[it->second];
388 CHECK_XR(
389 xrApplyHapticFeedback(session, &haptic_info, (const XrHapticBaseHeader *)&vibration),
390 (std::string("Failed to apply haptic action \"") + action_name + "\".").data());
391 }
392 }
393 else {
394 for (const XrPath &subaction_path : subaction_paths_) {
395 haptic_info.subactionPath = subaction_path;
396 CHECK_XR(
397 xrApplyHapticFeedback(session, &haptic_info, (const XrHapticBaseHeader *)&vibration),
398 (std::string("Failed to apply haptic action \"") + action_name + "\".").data());
399 }
400 }
401}
402
404 const char *action_name,
405 const char *subaction_path_str)
406{
407 XrHapticActionInfo haptic_info{XR_TYPE_HAPTIC_ACTION_INFO};
408 haptic_info.action = action_;
409
410 if (subaction_path_str != nullptr) {
411 SubactionIndexMap::iterator it = subaction_indices_.find(subaction_path_str);
412 if (it != subaction_indices_.end()) {
413 haptic_info.subactionPath = subaction_paths_[it->second];
414 CHECK_XR(xrStopHapticFeedback(session, &haptic_info),
415 (std::string("Failed to stop haptic action \"") + action_name + "\".").data());
416 }
417 }
418 else {
419 for (const XrPath &subaction_path : subaction_paths_) {
420 haptic_info.subactionPath = subaction_path;
421 CHECK_XR(xrStopHapticFeedback(session, &haptic_info),
422 (std::string("Failed to stop haptic action \"") + action_name + "\".").data());
423 }
424 }
425}
426
428{
429 if (custom_data_ == nullptr) {
430 return nullptr;
431 }
432 return custom_data_->custom_data_;
433}
434
436 std::map<XrPath, std::vector<XrActionSuggestedBinding>> &r_bindings) const
437{
438 for (auto &[path, profile] : profiles_) {
439 profile.getBindings(action_, r_bindings);
440 }
441}
442
444
445/* -------------------------------------------------------------------- */
449
450GHOST_XrActionSet::GHOST_XrActionSet(XrInstance instance, const GHOST_XrActionSetInfo &info)
451 : custom_data_(
452 std::make_unique<GHOST_C_CustomDataWrapper>(info.customdata, info.customdata_free_fn))
453{
454 XrActionSetCreateInfo action_set_info{XR_TYPE_ACTION_SET_CREATE_INFO};
455 strcpy(action_set_info.actionSetName, info.name);
456
457 /* Just use same name for localized. This can be changed in the future if necessary. */
458 strcpy(action_set_info.localizedActionSetName, info.name);
459
460 action_set_info.priority = 0; /* Use same (default) priority for all action sets. */
461
462 CHECK_XR(xrCreateActionSet(instance, &action_set_info, &action_set_),
463 (std::string("Failed to create action set \"") + info.name +
464 "\". Name must not contain upper case letters or special characters "
465 "other than '-', '_', or '.'.")
466 .data());
467}
468
470{
471 /* This needs to be done before xrDestroyActionSet() to avoid an assertion in the GHOST_XrAction
472 * destructor (which calls xrDestroyAction()). */
473 actions_.clear();
474
475 if (action_set_ != XR_NULL_HANDLE) {
476 CHECK_XR_ASSERT(xrDestroyActionSet(action_set_));
477 }
478}
479
480bool GHOST_XrActionSet::createAction(XrInstance instance, const GHOST_XrActionInfo &info)
481{
482 if (actions_.find(info.name) != actions_.end()) {
483 return false;
484 }
485
486 actions_.emplace(std::piecewise_construct,
487 std::make_tuple(info.name),
488 std::make_tuple(instance, action_set_, info));
489
490 return true;
491}
492
493void GHOST_XrActionSet::destroyAction(const char *action_name)
494{
495 /* It's possible nothing is removed. */
496 actions_.erase(action_name);
497}
498
500{
501 std::map<std::string, GHOST_XrAction>::iterator it = actions_.find(action_name);
502 if (it == actions_.end()) {
503 return nullptr;
504 }
505 return &it->second;
506}
507
508void GHOST_XrActionSet::updateStates(XrSession session,
509 XrSpace reference_space,
510 const XrTime &predicted_display_time)
511{
512 for (auto &[name, action] : actions_) {
513 action.updateState(session, name.data(), reference_space, predicted_display_time);
514 }
515}
516
518{
519 return action_set_;
520}
521
523{
524 if (custom_data_ == nullptr) {
525 return nullptr;
526 }
527 return custom_data_->custom_data_;
528}
529
531{
532 return uint32_t(actions_.size());
533}
534
535void GHOST_XrActionSet::getActionCustomdataArray(void **r_customdata_array)
536{
537 uint32_t i = 0;
538 for (auto &[name, action] : actions_) {
539 r_customdata_array[i++] = action.getCustomdata();
540 }
541}
542
544 std::map<XrPath, std::vector<XrActionSuggestedBinding>> &r_bindings) const
545{
546 for (auto &[name, action] : actions_) {
547 action.getBindings(r_bindings);
548 }
549}
550
void copy_openxr_pose_to_ghost_pose(const XrPosef &oxr_pose, GHOST_XrPose &r_ghost_pose)
void copy_ghost_pose_to_openxr_pose(const GHOST_XrPose &ghost_pose, XrPosef &r_oxr_pose)
#define CHECK_XR_ASSERT(call)
#define CHECK_XR(call, error_msg)
#define ELEM(...)
long long int int64_t
const GHOST_XrSubactionData * getSubaction(XrPath subaction_path) const
XrPath getProfile() const
GHOST_XrActionProfile()=delete
void getBindings(XrAction action, std::map< XrPath, std::vector< XrActionSuggestedBinding > > &r_bindings) const
GHOST_XrActionSet()=delete
void updateStates(XrSession session, XrSpace reference_space, const XrTime &predicted_display_time)
GHOST_XrAction * findAction(const char *action_name)
bool createAction(XrInstance instance, const GHOST_XrActionInfo &info)
void getBindings(std::map< XrPath, std::vector< XrActionSuggestedBinding > > &r_bindings) const
XrActionSet getActionSet() const
void getActionCustomdataArray(void **r_customdata_array)
uint32_t getActionCount() const
void destroyAction(const char *action_name)
XrSpace getSpace() const
GHOST_XrActionSpace()=delete
bool createBinding(XrInstance instance, XrSession session, const GHOST_XrActionProfileInfo &info)
void updateState(XrSession session, const char *action_name, XrSpace reference_space, const XrTime &predicted_display_time)
void getBindings(std::map< XrPath, std::vector< XrActionSuggestedBinding > > &r_bindings) const
void destroyBinding(const char *profile_path)
void applyHapticFeedback(XrSession session, const char *action_name, const char *subaction_path_str, const int64_t &duration, const float &frequency, const float &amplitude)
void stopHapticFeedback(XrSession session, const char *action_name, const char *subaction_path_str)
GHOST_XrAction()=delete
#define frequency(freq)
const int state
std::unique_ptr< GHOST_XrActionSpace > space