#include "openxractionset.hpp" #include "openxrdebug.hpp" #include "vrenvironment.hpp" #include "openxrmanager.hpp" #include "openxrmanagerimpl.hpp" #include "openxraction.hpp" #include #include #include // TODO: should implement actual safe strcpy #ifdef __linux__ #define strcpy_s(dst, src) int(strcpy(dst, src) != nullptr) #endif namespace MWVR { OpenXRActionSet::OpenXRActionSet(const std::string& actionSetName, std::shared_ptr deadzone) : mActionSet(nullptr) , mLocalizedName(actionSetName) , mInternalName(Misc::StringUtils::lowerCase(actionSetName)) { mActionSet = createActionSet(actionSetName); // When starting to account for more devices than oculus touch, this section may need some expansion/redesign. // Currently the set of action paths was determined using the oculus touch (i know nothing about the vive and the index). // The set of action paths may therefore need expansion. E.g. /click vs /value may vary with controllers. /* // Applicable actions not (yet) included A_QuickKey1, A_QuickKey2, A_QuickKey3, A_QuickKey4, A_QuickKey5, A_QuickKey6, A_QuickKey7, A_QuickKey8, A_QuickKey9, A_QuickKey10, A_QuickKeysMenu, A_QuickLoad, A_CycleSpellLeft, A_CycleSpellRight, A_CycleWeaponLeft, A_CycleWeaponRight, A_Screenshot, // Generate a VR screenshot? A_Console, // Currently awkward due to a lack of virtual keyboard, but should be included when that's in place */ // To fit more actions onto controllers i created a system of short and long press actions. Allowing one action to activate // on a short press, and another on long. Here, what actions are short press and what actions are long press is simply // hardcoded at init, rather than interpreted from bindings. That's bad, and should be fixed, but that's hard to do // while staying true to openxr's binding system, so if the system i wrote for the oculus touch isn't a good fit for // the vive/index, we might want to rewrite this to handle bindings ourselves. createMWAction(MWInput::A_GameMenu, "game_menu", "Game Menu"); createMWAction(A_VrMetaMenu, "meta_menu", "Meta Menu"); createMWAction(A_Recenter, "reposition_menu", "Reposition Menu"); createMWAction(MWInput::A_Inventory, "inventory", "Inventory"); createMWAction(MWInput::A_Activate, "activate", "Activate"); createMWAction(MWInput::A_Use, "use", "Use"); createMWAction(MWInput::A_Jump, "jump", "Jump"); createMWAction(MWInput::A_ToggleWeapon, "weapon", "Weapon"); createMWAction(MWInput::A_ToggleSpell, "spell", "Spell"); createMWAction(MWInput::A_CycleSpellLeft, "cycle_spell_left", "Cycle Spell Left"); createMWAction(MWInput::A_CycleSpellRight, "cycle_spell_right", "Cycle Spell Right"); createMWAction(MWInput::A_CycleWeaponLeft, "cycle_weapon_left", "Cycle Weapon Left"); createMWAction(MWInput::A_CycleWeaponRight, "cycle_weapon_right", "Cycle Weapon Right"); createMWAction(MWInput::A_Sneak, "sneak", "Sneak"); createMWAction(MWInput::A_QuickKeysMenu, "quick_menu", "Quick Menu"); createMWAction(MWInput::A_LookLeftRight, "look_left_right", "Look Left Right", deadzone); createMWAction(MWInput::A_MoveForwardBackward, "move_forward_backward", "Move Forward Backward", deadzone); createMWAction(MWInput::A_MoveLeftRight, "move_left_right", "Move Left Right", deadzone); createMWAction(MWInput::A_Journal, "journal_book", "Journal Book"); createMWAction(MWInput::A_QuickSave, "quick_save", "Quick Save"); createMWAction(MWInput::A_Rest, "rest", "Rest"); createMWAction(A_ActivateTouch, "activate_touched", "Activate Touch", deadzone); createMWAction(MWInput::A_AlwaysRun, "always_run", "Always Run"); createMWAction(MWInput::A_AutoMove, "auto_move", "Auto Move"); createMWAction(MWInput::A_ToggleHUD, "toggle_hud", "Toggle HUD"); createMWAction(MWInput::A_ToggleDebug, "toggle_debug", "Toggle the debug hud"); createMWAction(A_MenuUpDown, "menu_up_down", "Menu Up Down", deadzone); createMWAction(A_MenuLeftRight, "menu_left_right", "Menu Left Right", deadzone); createMWAction(A_MenuSelect, "menu_select", "Menu Select"); createMWAction(A_MenuBack, "menu_back", "Menu Back"); createPoseAction(TrackedLimb::LEFT_HAND, "left_hand_pose", "Left Hand Pose"); createPoseAction(TrackedLimb::RIGHT_HAND, "right_hand_pose", "Right Hand Pose"); createHapticsAction(TrackedLimb::RIGHT_HAND, "right_hand_haptics", "Right Hand Haptics"); createHapticsAction(TrackedLimb::LEFT_HAND, "left_hand_haptics", "Left Hand Haptics"); }; void OpenXRActionSet::createPoseAction( TrackedLimb limb, const std::string& actionName, const std::string& localName) { mTrackerMap.emplace(limb, new PoseAction(std::move(createXRAction(XR_ACTION_TYPE_POSE_INPUT, actionName, localName)))); } void OpenXRActionSet::createHapticsAction( TrackedLimb limb, const std::string& actionName, const std::string& localName) { mHapticsMap.emplace(limb, new HapticsAction(std::move(createXRAction(XR_ACTION_TYPE_VIBRATION_OUTPUT, actionName, localName)))); } template void OpenXRActionSet::createMWAction( int openMWAction, const std::string& actionName, const std::string& localName) { auto xrAction = createXRAction(A::ActionType, mInternalName + "_" + actionName, mLocalizedName + " " + localName); mActionMap.emplace(actionName, new A(openMWAction, std::move(xrAction))); } template void OpenXRActionSet::createMWAction( int openMWAction, const std::string& actionName, const std::string& localName, std::shared_ptr deadzone) { auto xrAction = createXRAction(AxisAction::ActionType, mInternalName + "_" + actionName, mLocalizedName + " " + localName); mActionMap.emplace(actionName, new AxisAction(openMWAction, std::move(xrAction), deadzone)); } XrActionSet OpenXRActionSet::createActionSet(const std::string& name) { std::string localized_name = name; std::string internal_name = Misc::StringUtils::lowerCase(name); auto* xr = Environment::get().getManager(); XrActionSet actionSet = XR_NULL_HANDLE; XrActionSetCreateInfo createInfo{ XR_TYPE_ACTION_SET_CREATE_INFO }; strcpy_s(createInfo.actionSetName, internal_name.c_str()); strcpy_s(createInfo.localizedActionSetName, localized_name.c_str()); createInfo.priority = 0; CHECK_XRCMD(xrCreateActionSet(xr->impl().xrInstance(), &createInfo, &actionSet)); VrDebug::setName(actionSet, "OpenMW XR Action Set " + name); return actionSet; } void OpenXRActionSet::suggestBindings(std::vector& xrSuggestedBindings, const SuggestedBindings& mwSuggestedBindings) { std::vector suggestedBindings = { {*mTrackerMap[TrackedLimb::LEFT_HAND], getXrPath("/user/hand/left/input/aim/pose")}, {*mTrackerMap[TrackedLimb::RIGHT_HAND], getXrPath("/user/hand/right/input/aim/pose")}, {*mHapticsMap[TrackedLimb::LEFT_HAND], getXrPath("/user/hand/left/output/haptic")}, {*mHapticsMap[TrackedLimb::RIGHT_HAND], getXrPath("/user/hand/right/output/haptic")}, }; for (auto& mwSuggestedBinding : mwSuggestedBindings) { auto xrAction = mActionMap.find(mwSuggestedBinding.action); if (xrAction == mActionMap.end()) { Log(Debug::Error) << "OpenXRActionSet: Unknown action " << mwSuggestedBinding.action; continue; } suggestedBindings.push_back({ *xrAction->second, getXrPath(mwSuggestedBinding.path) }); } xrSuggestedBindings.insert(xrSuggestedBindings.end(), suggestedBindings.begin(), suggestedBindings.end()); } std::unique_ptr OpenXRActionSet::createXRAction( XrActionType actionType, const std::string& actionName, const std::string& localName) { std::vector subactionPaths; XrActionCreateInfo createInfo{ XR_TYPE_ACTION_CREATE_INFO }; createInfo.actionType = actionType; strcpy_s(createInfo.actionName, actionName.c_str()); strcpy_s(createInfo.localizedActionName, localName.c_str()); XrAction action = XR_NULL_HANDLE; CHECK_XRCMD(xrCreateAction(mActionSet, &createInfo, &action)); return std::unique_ptr{new OpenXRAction{ action, actionType, actionName, localName }}; } void OpenXRActionSet::updateControls() { auto* xr = Environment::get().getManager(); if (!xr->impl().appShouldReadInput()) return; const XrActiveActionSet activeActionSet{ mActionSet, XR_NULL_PATH }; XrActionsSyncInfo syncInfo{ XR_TYPE_ACTIONS_SYNC_INFO }; syncInfo.countActiveActionSets = 1; syncInfo.activeActionSets = &activeActionSet; CHECK_XRCMD(xrSyncActions(xr->impl().xrSession(), &syncInfo)); for (auto& action : mActionMap) action.second->updateAndQueue(mActionQueue); } XrPath OpenXRActionSet::getXrPath(const std::string& path) { auto* xr = Environment::get().getManager(); XrPath xrpath = 0; CHECK_XRCMD(xrStringToPath(xr->impl().xrInstance(), path.c_str(), &xrpath)); return xrpath; } const Action* OpenXRActionSet::nextAction() { if (mActionQueue.empty()) return nullptr; const auto* action = mActionQueue.front(); mActionQueue.pop_front(); return action; } Pose OpenXRActionSet::getLimbPose( int64_t time, TrackedLimb limb) { auto it = mTrackerMap.find(limb); if (it == mTrackerMap.end()) { Log(Debug::Error) << "OpenXRActionSet: No such tracker: " << limb; return Pose{}; } it->second->update(time); return it->second->value(); } void OpenXRActionSet::applyHaptics(TrackedLimb limb, float intensity) { auto it = mHapticsMap.find(limb); if (it == mHapticsMap.end()) { Log(Debug::Error) << "OpenXRActionSet: No such tracker: " << limb; return; } it->second->apply(intensity); } }