| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "Subaction.h" |
| 5 | #include "XRState.h" |
| 6 | |
| 7 | #include <osgXR/Manager> |
| 8 | |
| 9 | using namespace osgXR; |
| 10 | |
| 11 | // Internal API |
| 12 | |
| 13 | Subaction::Private::Private(XRState *state, |
| 14 | const std::string &path) : |
| 15 | _state(state), |
| 16 | _pathString(path) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | void Subaction::Private::registerPublic(Subaction *subaction) |
| 21 | { |
| 22 | _publics.insert(subaction); |
| 23 | } |
| 24 | |
| 25 | void Subaction::Private::unregisterPublic(Subaction *subaction) |
| 26 | { |
| 27 | _publics.erase(subaction); |
| 28 | } |
| 29 | |
| 30 | InteractionProfile *Subaction::Private::getCurrentProfile() |
| 31 | { |
| 32 | if (!_currentProfile.valid()) |
| 33 | { |
| 34 | if (_path.valid()) |
| 35 | _currentProfile = _state->getCurrentInteractionProfile(_path); |
| 36 | } |
| 37 | |
| 38 | return _currentProfile.get(); |
| 39 | } |
| 40 | |
| 41 | void Subaction::Private::onInteractionProfileChanged(OpenXR::Session *session) |
| 42 | { |
| 43 | // Ensure path is set up |
| 44 | setup(session->getInstance()); |
| 45 | |
| 46 | // Find whether this subaction's current interaction profile has changed |
| 47 | InteractionProfile *prevProfile = _currentProfile.get(); |
| 48 | _currentProfile = nullptr; |
| 49 | InteractionProfile *newProfile = getCurrentProfile(); |
| 50 | if (newProfile != prevProfile) |
| 51 | { |
| 52 | // Notify any derived Subaction classes from the app |
| 53 | for (auto *pub: _publics) |
| 54 | pub->onProfileChanged(newProfile); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const OpenXR::Path &Subaction::Private::setup(OpenXR::Instance *instance) |
| 59 | { |
| 60 | if (!_path.valid()) |
| 61 | _path = OpenXR::Path(instance, _pathString); |
| 62 | return _path; |
| 63 | } |
| 64 | |
| 65 | void Subaction::Private::cleanupSession() |
| 66 | { |
| 67 | bool hadProfile = _currentProfile.valid(); |
| 68 | _currentProfile = nullptr; |
| 69 | if (hadProfile) |
| 70 | { |
| 71 | // Notify any derived Subaction classes from the app |
| 72 | for (auto *pub: _publics) |
| 73 | pub->onProfileChanged(nullptr); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void Subaction::Private::cleanupInstance() |
| 78 | { |
| 79 | _path = OpenXR::Path(); |
| 80 | } |
| 81 | |
| 82 | // Public API |
| 83 | |
| 84 | Subaction::Subaction(Manager *manager, |
| 85 | const std::string &path) : |
| 86 | _private(manager->_getXrState()->getSubaction(path)) |
| 87 | { |
| 88 | _private->registerPublic(this); |
| 89 | } |
| 90 | |
| 91 | Subaction::~Subaction() |
| 92 | { |
| 93 | _private->unregisterPublic(this); |
| 94 | } |
| 95 | |
| 96 | const std::string &Subaction::getPath() const |
| 97 | { |
| 98 | return _private->getPathString(); |
| 99 | } |
| 100 | |
| 101 | InteractionProfile *Subaction::getCurrentProfile() |
| 102 | { |
| 103 | return _private->getCurrentProfile(); |
| 104 | } |
| 105 | |
| 106 | void Subaction::onProfileChanged(InteractionProfile *newProfile) |
| 107 | { |
| 108 | // This is for derived classes to implement to their own ends |
| 109 | } |