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)
34 XrActionSpaceCreateInfo action_space_info{XR_TYPE_ACTION_SPACE_CREATE_INFO};
35 action_space_info.action = action;
36 action_space_info.subactionPath = subaction_path;
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 +
"\".")
67 GHOST_XrActionType type,
68 const GHOST_XrActionProfileInfo &info)
70 CHECK_XR(xrStringToPath(instance, info.profile_path, &profile_),
71 (std::string(
"Failed to get interaction profile path \"") + info.profile_path +
"\".")
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);
80 XrInteractionProfileSuggestedBinding bindings_info{
81 XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING};
82 bindings_info.interactionProfile = profile_;
83 bindings_info.countSuggestedBindings = 1;
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];
89 const std::string interaction_path = std::string(subaction_path_str) +
90 binding_info.component_path;
91 if (bindings_.find(interaction_path) != bindings_.end()) {
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;
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?")
109 bindings_.insert({interaction_path, sbinding.binding});
111 if (subaction_data_.find(subaction_path_str) == subaction_data_.end()) {
112 std::map<std::string, GHOST_XrSubactionData>::iterator it =
115 std::piecewise_construct, std::make_tuple(subaction_path_str), std::make_tuple())
120 (std::string(
"Failed to get user path \"") + subaction_path_str +
"\".").data());
122 if (is_float_action || is_button_action) {
123 if (is_float_action) {
126 if (is_button_action) {
127 subaction.
axis_flag = binding_info.axis_flag;
130 else if (is_pose_action) {
132 subaction.
space = std::make_unique<GHOST_XrActionSpace>(session,
160 XrAction action, std::map<XrPath, std::vector<XrActionSuggestedBinding>> &r_bindings)
const
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())
168 std::vector<XrActionSuggestedBinding> &sbindings = it->second;
170 for (
auto &[path, binding] : bindings_) {
171 XrActionSuggestedBinding sbinding;
172 sbinding.action = action;
173 sbinding.binding = binding;
175 sbindings.push_back(std::move(sbinding));
187 XrActionSet action_set,
188 const GHOST_XrActionInfo &info)
190 states_(info.states),
191 float_thresholds_(info.float_thresholds),
192 axis_flags_(info.axis_flags),
196 subaction_paths_.resize(info.count_subaction_paths);
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});
205 XrActionCreateInfo action_info{XR_TYPE_ACTION_CREATE_INFO};
206 strcpy(action_info.actionName, info.name);
209 strcpy(action_info.localizedActionName, info.name);
212 case GHOST_kXrActionTypeBooleanInput:
213 action_info.actionType = XR_ACTION_TYPE_BOOLEAN_INPUT;
215 case GHOST_kXrActionTypeFloatInput:
216 action_info.actionType = XR_ACTION_TYPE_FLOAT_INPUT;
218 case GHOST_kXrActionTypeVector2fInput:
219 action_info.actionType = XR_ACTION_TYPE_VECTOR2F_INPUT;
221 case GHOST_kXrActionTypePoseInput:
222 action_info.actionType = XR_ACTION_TYPE_POSE_INPUT;
224 case GHOST_kXrActionTypeVibrationOutput:
225 action_info.actionType = XR_ACTION_TYPE_VIBRATION_OUTPUT;
228 action_info.countSubactionPaths = info.count_subaction_paths;
229 action_info.subactionPaths = subaction_paths_.data();
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 '.'.")
247 const GHOST_XrActionProfileInfo &info)
249 if (profiles_.find(info.profile_path) != profiles_.end()) {
253 profiles_.emplace(std::piecewise_construct,
254 std::make_tuple(info.profile_path),
255 std::make_tuple(instance, session, action_, type_, info));
267 const char *action_name,
268 XrSpace reference_space,
269 const XrTime &predicted_display_time)
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);
275 XrActionStateGetInfo state_info{XR_TYPE_ACTION_STATE_GET_INFO};
276 state_info.action = action_;
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];
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.");
288 for (
auto &[profile_path, profile] : profiles_) {
289 if (profile.getProfile() == profile_state.interactionProfile) {
290 subaction = profile.getSubaction(state_info.subactionPath);
295 if (subaction !=
nullptr) {
296 if (is_float_action) {
299 if (is_button_action) {
300 axis_flags_[subaction_idx] = subaction->
axis_flag;
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 +
"\".")
310 if (
state.isActive) {
311 ((
bool *)states_)[subaction_idx] =
state.currentState;
315 case GHOST_kXrActionTypeFloatInput: {
316 XrActionStateFloat
state{XR_TYPE_ACTION_STATE_FLOAT};
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;
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 +
"\".")
330 if (
state.isActive) {
331 memcpy(((
float (*)[2])states_)[subaction_idx], &
state.currentState,
sizeof(
float[2]));
335 case GHOST_kXrActionTypePoseInput: {
337 if (predicted_display_time > 0) {
338 XrActionStatePose
state{XR_TYPE_ACTION_STATE_POSE};
340 (std::string(
"Failed to get state for pose action \"") + action_name +
"\".")
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() :
347 if (pose_space != XR_NULL_HANDLE) {
348 XrSpaceLocation space_location{XR_TYPE_SPACE_LOCATION};
351 pose_space, reference_space, predicted_display_time, &space_location),
352 (std::string(
"Failed to query pose space for action \"") + action_name +
"\".")
355 ((GHOST_XrPose *)states_)[subaction_idx]);
361 case GHOST_kXrActionTypeVibrationOutput: {
369 const char *action_name,
370 const char *subaction_path_str,
373 const float &litude)
375 XrHapticVibration vibration{XR_TYPE_HAPTIC_VIBRATION};
376 vibration.duration = (duration == 0) ? XR_MIN_HAPTIC_DURATION :
377 static_cast<XrDuration
>(duration);
379 vibration.amplitude = amplitude;
381 XrHapticActionInfo haptic_info{XR_TYPE_HAPTIC_ACTION_INFO};
382 haptic_info.action = action_;
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];
389 xrApplyHapticFeedback(session, &haptic_info, (
const XrHapticBaseHeader *)&vibration),
390 (std::string(
"Failed to apply haptic action \"") + action_name +
"\".").data());
394 for (
const XrPath &subaction_path : subaction_paths_) {
395 haptic_info.subactionPath = subaction_path;
397 xrApplyHapticFeedback(session, &haptic_info, (
const XrHapticBaseHeader *)&vibration),
398 (std::string(
"Failed to apply haptic action \"") + action_name +
"\".").data());
404 const char *action_name,
405 const char *subaction_path_str)
407 XrHapticActionInfo haptic_info{XR_TYPE_HAPTIC_ACTION_INFO};
408 haptic_info.action = action_;
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());
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());
454 XrActionSetCreateInfo action_set_info{XR_TYPE_ACTION_SET_CREATE_INFO};
455 strcpy(action_set_info.actionSetName, info.name);
458 strcpy(action_set_info.localizedActionSetName, info.name);
460 action_set_info.priority = 0;
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 '.'.")
482 if (actions_.find(info.name) != actions_.end()) {
486 actions_.emplace(std::piecewise_construct,
487 std::make_tuple(info.name),
488 std::make_tuple(instance, action_set_, info));