| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "ActionSet.h" |
| 5 | #include "Action.h" |
| 6 | |
| 7 | #include "OpenXR/ActionSet.h" |
| 8 | |
| 9 | #include <osgXR/Manager> |
| 10 | |
| 11 | #include "XRState.h" |
| 12 | |
| 13 | using namespace osgXR; |
| 14 | |
| 15 | // Internal API |
| 16 | |
| 17 | ActionSet::Private::Private(XRState *state) : |
| 18 | _state(state), |
| 19 | _priority(0), |
| 20 | _updated(true) |
| 21 | { |
| 22 | state->addActionSet(this); |
| 23 | } |
| 24 | |
| 25 | ActionSet::Private::~Private() |
| 26 | { |
| 27 | XRState *state = _state.get(); |
| 28 | if (state) |
| 29 | state->removeActionSet(this); |
| 30 | } |
| 31 | |
| 32 | void ActionSet::Private::setName(const std::string &name) |
| 33 | { |
| 34 | _updated = true; |
| 35 | _name = name; |
| 36 | } |
| 37 | |
| 38 | const std::string &ActionSet::Private::getName() const |
| 39 | { |
| 40 | return _name; |
| 41 | } |
| 42 | |
| 43 | void ActionSet::Private::setLocalizedName(const std::string &localizedName) |
| 44 | { |
| 45 | _updated = true; |
| 46 | _localizedName = localizedName; |
| 47 | } |
| 48 | |
| 49 | const std::string &ActionSet::Private::getLocalizedName() const |
| 50 | { |
| 51 | return _localizedName; |
| 52 | } |
| 53 | |
| 54 | void ActionSet::Private::setPriority(uint32_t priority) |
| 55 | { |
| 56 | _updated = true; |
| 57 | _priority = priority; |
| 58 | } |
| 59 | |
| 60 | uint32_t ActionSet::Private::getPriority() const |
| 61 | { |
| 62 | return _priority; |
| 63 | } |
| 64 | |
| 65 | bool ActionSet::Private::getUpdated() const |
| 66 | { |
| 67 | if (_updated) |
| 68 | return true; |
| 69 | for (Action::Private *action: _actions) |
| 70 | if (action->getUpdated()) |
| 71 | return true; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | void ActionSet::Private::activate(std::shared_ptr<Subaction::Private> subaction) |
| 76 | { |
| 77 | _activeSubactions.insert(subaction); |
| 78 | |
| 79 | if (_actionSet.valid() && _session.valid()) |
| 80 | { |
| 81 | OpenXR::Path path; |
| 82 | if (subaction) |
| 83 | path = subaction->setup(_session->getInstance()); |
| 84 | _session->activateActionSet(_actionSet, path); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void ActionSet::Private::deactivate(std::shared_ptr<Subaction::Private> subaction) |
| 89 | { |
| 90 | _activeSubactions.erase(subaction); |
| 91 | |
| 92 | if (_actionSet.valid() && _session.valid()) |
| 93 | { |
| 94 | OpenXR::Path path; |
| 95 | if (subaction) |
| 96 | path = subaction->setup(_session->getInstance()); |
| 97 | _session->deactivateActionSet(_actionSet, path); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool ActionSet::Private::isActive() |
| 102 | { |
| 103 | return !_activeSubactions.empty(); |
| 104 | } |
| 105 | |
| 106 | void ActionSet::Private::registerAction(Action::Private *action) |
| 107 | { |
| 108 | _actions.insert(action); |
| 109 | } |
| 110 | |
| 111 | void ActionSet::Private::unregisterAction(Action::Private *action) |
| 112 | { |
| 113 | _actions.erase(action); |
| 114 | } |
| 115 | |
| 116 | OpenXR::ActionSet *ActionSet::Private::setup(OpenXR::Instance *instance) |
| 117 | { |
| 118 | if (_updated) |
| 119 | { |
| 120 | _actionSet = new OpenXR::ActionSet(instance, _name, _localizedName, |
| 121 | _priority); |
| 122 | _updated = false; |
| 123 | } |
| 124 | return _actionSet; |
| 125 | } |
| 126 | |
| 127 | bool ActionSet::Private::setup(OpenXR::Session *session) |
| 128 | { |
| 129 | _session = session; |
| 130 | if (_actionSet.valid()) |
| 131 | { |
| 132 | session->addActionSet(_actionSet); |
| 133 | // Init all the actions |
| 134 | for (Action::Private *action: _actions) |
| 135 | { |
| 136 | OpenXR::Action *xrAction = action->setup(session->getInstance()); |
| 137 | if (xrAction) |
| 138 | xrAction->init(); |
| 139 | } |
| 140 | for (auto &subaction: _activeSubactions) |
| 141 | { |
| 142 | OpenXR::Path path; |
| 143 | if (subaction) |
| 144 | path = subaction->setup(session->getInstance()); |
| 145 | session->activateActionSet(_actionSet, path); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | void ActionSet::Private::cleanupSession() |
| 153 | { |
| 154 | for (auto *action: _actions) |
| 155 | action->cleanupSession(); |
| 156 | } |
| 157 | |
| 158 | void ActionSet::Private::cleanupInstance() |
| 159 | { |
| 160 | _updated = true; |
| 161 | _actionSet = nullptr; |
| 162 | for (auto *action: _actions) |
| 163 | action->cleanupInstance(); |
| 164 | } |
| 165 | |
| 166 | // Public API |
| 167 | |
| 168 | ActionSet::ActionSet(Manager *manager) : |
| 169 | _private(new Private(manager->_getXrState())) |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | ActionSet::ActionSet(Manager *manager, |
| 174 | const std::string &name) : |
| 175 | _private(new Private(manager->_getXrState())) |
| 176 | { |
| 177 | setName(name, name); |
| 178 | } |
| 179 | |
| 180 | ActionSet::ActionSet(Manager *manager, |
| 181 | const std::string &name, |
| 182 | const std::string &localizedName) : |
| 183 | _private(new Private(manager->_getXrState())) |
| 184 | { |
| 185 | setName(name, localizedName); |
| 186 | } |
| 187 | |
| 188 | ActionSet::~ActionSet() |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | void ActionSet::setName(const std::string &name, |
| 193 | const std::string &localizedName) |
| 194 | { |
| 195 | _private->setName(name); |
| 196 | _private->setLocalizedName(localizedName); |
| 197 | } |
| 198 | |
| 199 | void ActionSet::setName(const std::string &name) |
| 200 | { |
| 201 | _private->setName(name); |
| 202 | } |
| 203 | |
| 204 | const std::string &ActionSet::getName() const |
| 205 | { |
| 206 | return _private->getName(); |
| 207 | } |
| 208 | |
| 209 | void ActionSet::setLocalizedName(const std::string &localizedName) |
| 210 | { |
| 211 | _private->setLocalizedName(localizedName); |
| 212 | } |
| 213 | |
| 214 | const std::string &ActionSet::getLocalizedName() const |
| 215 | { |
| 216 | return _private->getLocalizedName(); |
| 217 | } |
| 218 | |
| 219 | void ActionSet::setPriority(uint32_t priority) |
| 220 | { |
| 221 | _private->setPriority(priority); |
| 222 | } |
| 223 | |
| 224 | uint32_t ActionSet::getPriority() const |
| 225 | { |
| 226 | return _private->getPriority(); |
| 227 | } |
| 228 | |
| 229 | void ActionSet::activate(Subaction *subaction) |
| 230 | { |
| 231 | _private->activate(Subaction::Private::get(subaction)); |
| 232 | } |
| 233 | |
| 234 | void ActionSet::deactivate(Subaction *subaction) |
| 235 | { |
| 236 | _private->deactivate(Subaction::Private::get(subaction)); |
| 237 | } |
| 238 | |
| 239 | bool ActionSet::isActive() |
| 240 | { |
| 241 | return _private->isActive(); |
| 242 | } |