| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "Action.h" |
| 5 | #include "Path.h" |
| 6 | #include "Session.h" |
| 7 | #include "Space.h" |
| 8 | |
| 9 | #include <cassert> |
| 10 | #include <cstring> |
| 11 | |
| 12 | using namespace osgXR::OpenXR; |
| 13 | |
| 14 | Action::Action(ActionSet *actionSet, |
| 15 | const std::string &name, |
| 16 | const std::string &localizedName, |
| 17 | XrActionType type) : |
| 18 | _actionSet(actionSet), |
| 19 | _createInfo{ XR_TYPE_ACTION_CREATE_INFO }, |
| 20 | _action(XR_NULL_HANDLE) |
| 21 | { |
| 22 | strncpy(_createInfo.actionName, name.c_str(), |
| 23 | XR_MAX_ACTION_NAME_SIZE - 1); |
| 24 | strncpy(_createInfo.localizedActionName, localizedName.c_str(), |
| 25 | XR_MAX_LOCALIZED_ACTION_NAME_SIZE - 1); |
| 26 | _createInfo.actionType = type; |
| 27 | } |
| 28 | |
| 29 | Action::~Action() |
| 30 | { |
| 31 | if (_action != XR_NULL_HANDLE) |
| 32 | { |
| 33 | check(xrDestroyAction(_action), |
| 34 | "destroy OpenXR action"); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void Action::addSubaction(const Path &path) |
| 39 | { |
| 40 | assert(path.getInstance() == getInstance()); |
| 41 | _subactionPaths.push_back(path.getXrPath()); |
| 42 | } |
| 43 | |
| 44 | bool Action::init() |
| 45 | { |
| 46 | if (valid()) |
| 47 | return true; |
| 48 | |
| 49 | if (!_subactionPaths.empty()) |
| 50 | { |
| 51 | _createInfo.countSubactionPaths = _subactionPaths.size(); |
| 52 | _createInfo.subactionPaths = _subactionPaths.data(); |
| 53 | } |
| 54 | return check(xrCreateAction(getXrActionSet(), &_createInfo, &_action), |
| 55 | "create OpenXR action"); |
| 56 | } |
| 57 | |
| 58 | ActionStateBase::ActionStateBase(Action *action, Session *session, |
| 59 | Path subactionPath) : |
| 60 | _action(action), |
| 61 | _session(session), |
| 62 | _subactionPath(subactionPath), |
| 63 | _valid(false), |
| 64 | _syncCount(0) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | ActionStateBase::~ActionStateBase() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | bool ActionStateBase::checkUpdate() |
| 73 | { |
| 74 | unsigned int sessionSyncCount = _session->getActionSyncCount(); |
| 75 | // If an xrSyncActions has taken place, the state is out of date |
| 76 | bool needsUpdate = (_syncCount < sessionSyncCount); |
| 77 | // Update the counter as caller is expected to update the state |
| 78 | _syncCount = sessionSyncCount; |
| 79 | return needsUpdate; |
| 80 | } |
| 81 | |
| 82 | template <> |
| 83 | bool ActionStateCommonBoolean::updateState() |
| 84 | { |
| 85 | XrActionStateGetInfo getInfo{ XR_TYPE_ACTION_STATE_GET_INFO }; |
| 86 | getInfo.action = _action->getXrAction(); |
| 87 | getInfo.subactionPath = _subactionPath.getXrPath(); |
| 88 | |
| 89 | _state = { XR_TYPE_ACTION_STATE_BOOLEAN }; |
| 90 | |
| 91 | _valid = check(xrGetActionStateBoolean(_session->getXrSession(), &getInfo, |
| 92 | &_state), |
| 93 | "get boolean OpenXR action state"); |
| 94 | return _valid; |
| 95 | } |
| 96 | |
| 97 | template <> |
| 98 | bool ActionStateCommonFloat::updateState() |
| 99 | { |
| 100 | XrActionStateGetInfo getInfo{ XR_TYPE_ACTION_STATE_GET_INFO }; |
| 101 | getInfo.action = _action->getXrAction(); |
| 102 | getInfo.subactionPath = _subactionPath.getXrPath(); |
| 103 | |
| 104 | _state = { XR_TYPE_ACTION_STATE_FLOAT }; |
| 105 | |
| 106 | _valid = check(xrGetActionStateFloat(_session->getXrSession(), &getInfo, |
| 107 | &_state), |
| 108 | "get float OpenXR action state"); |
| 109 | return _valid; |
| 110 | } |
| 111 | |
| 112 | template <> |
| 113 | bool ActionStateCommonVector2f::updateState() |
| 114 | { |
| 115 | XrActionStateGetInfo getInfo{ XR_TYPE_ACTION_STATE_GET_INFO }; |
| 116 | getInfo.action = _action->getXrAction(); |
| 117 | getInfo.subactionPath = _subactionPath.getXrPath(); |
| 118 | |
| 119 | _state = { XR_TYPE_ACTION_STATE_VECTOR2F }; |
| 120 | |
| 121 | _valid = check(xrGetActionStateVector2f(_session->getXrSession(), &getInfo, |
| 122 | &_state), |
| 123 | "get vector2f OpenXR action state"); |
| 124 | return _valid; |
| 125 | } |
| 126 | |
| 127 | template <> |
| 128 | bool ActionStateCommonPose::updateState() |
| 129 | { |
| 130 | XrActionStateGetInfo getInfo{ XR_TYPE_ACTION_STATE_GET_INFO }; |
| 131 | getInfo.action = _action->getXrAction(); |
| 132 | getInfo.subactionPath = _subactionPath.getXrPath(); |
| 133 | |
| 134 | _state = { XR_TYPE_ACTION_STATE_POSE }; |
| 135 | |
| 136 | _valid = check(xrGetActionStatePose(_session->getXrSession(), &getInfo, |
| 137 | &_state), |
| 138 | "get pose OpenXR action state"); |
| 139 | return _valid; |
| 140 | } |
| 141 | |
| 142 | ActionStatePose::ActionStatePose(ActionPose *action, Session *session, |
| 143 | Path subactionPath) : |
| 144 | Base(action, session, subactionPath), |
| 145 | _space(new Space(session, action, subactionPath)) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | ActionStatePose::~ActionStatePose() |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | ActionStateVibration::ActionStateVibration(ActionVibration *action, |
| 154 | Session *session, |
| 155 | Path subactionPath) : |
| 156 | _action(action), |
| 157 | _session(session), |
| 158 | _subactionPath(subactionPath) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | bool ActionStateVibration::applyHapticFeedback(int64_t duration_ns, |
| 163 | float frequency, |
| 164 | float amplitude) const |
| 165 | { |
| 166 | XrHapticActionInfo actionInfo{ XR_TYPE_HAPTIC_ACTION_INFO }; |
| 167 | actionInfo.action = _action->getXrAction(); |
| 168 | actionInfo.subactionPath = _subactionPath.getXrPath(); |
| 169 | |
| 170 | XrHapticVibration vibration{ XR_TYPE_HAPTIC_VIBRATION }; |
| 171 | vibration.duration = duration_ns; |
| 172 | vibration.frequency = frequency; |
| 173 | vibration.amplitude = amplitude; |
| 174 | |
| 175 | return check(xrApplyHapticFeedback(_session->getXrSession(), &actionInfo, |
| 176 | reinterpret_cast<XrHapticBaseHeader*>(&vibration)), |
| 177 | "apply haptic feedback"); |
| 178 | } |
| 179 | |
| 180 | bool ActionStateVibration::stopHapticFeedback() const |
| 181 | { |
| 182 | XrHapticActionInfo actionInfo{ XR_TYPE_HAPTIC_ACTION_INFO }; |
| 183 | actionInfo.action = _action->getXrAction(); |
| 184 | actionInfo.subactionPath = _subactionPath.getXrPath(); |
| 185 | |
| 186 | return check(xrStopHapticFeedback(_session->getXrSession(), &actionInfo), |
| 187 | "stop haptic feedback"); |
| 188 | } |