diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index c95984a7..74034875 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -242,11 +242,28 @@ std::shared_ptr scenes::stream::create(std::unique_ptrfence = self->device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled}); // Look up the XrActions for haptics - self->haptics_actions[0].action = application::get_action("/user/hand/left/output/haptic").first; - self->haptics_actions[0].path = application::string_to_path("/user/hand/left"); + for (auto [id, path, output]: { + std::tuple(device_id::LEFT_CONTROLLER_HAPTIC, "/user/hand/left", "/output/haptic"), + std::tuple(device_id::RIGHT_CONTROLLER_HAPTIC, "/user/hand/right", "/output/haptic"), - self->haptics_actions[1].action = application::get_action("/user/hand/right/output/haptic").first; - self->haptics_actions[1].path = application::string_to_path("/user/hand/right"); + std::tuple(device_id::LEFT_TRIGGER_HAPTIC, "/user/hand/left", "/output/haptic_trigger"), + std::tuple(device_id::RIGHT_TRIGGER_HAPTIC, "/user/hand/right", "/output/haptic_trigger"), + std::tuple(device_id::LEFT_TRIGGER_HAPTIC, "/user/hand/left", "/output/haptic_trigger_fb"), + std::tuple(device_id::RIGHT_TRIGGER_HAPTIC, "/user/hand/right", "/output/haptic_trigger_fb"), + + std::tuple(device_id::LEFT_THUMB_HAPTIC, "/user/hand/left", "/output/haptic_thumb"), + std::tuple(device_id::RIGHT_THUMB_HAPTIC, "/user/hand/right", "/output/haptic_thumb"), + std::tuple(device_id::LEFT_THUMB_HAPTIC, "/user/hand/left", "/output/haptic_thumb_fb"), + std::tuple(device_id::RIGHT_THUMB_HAPTIC, "/user/hand/right", "/output/haptic_thumb_fb")}) + { + if (auto action = application::get_action(std::string(path) + output); action.first) + { + self->haptics_actions.emplace(id, haptics_action{ + .action = action.first, + .path = application::string_to_path(path), + }); + } + } // Look up the XrActions for input for (const auto & [action, action_type, name]: application::inputs()) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index d3cf49a8..2f3bc860 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -113,9 +113,9 @@ private: { XrAction action; XrPath path; - std::atomic amplitude; + float amplitude; }; - std::array haptics_actions; + std::unordered_multimap haptics_actions; std::vector> input_actions; state state_ = state::initializing; diff --git a/client/scenes/stream_actions.cpp b/client/scenes/stream_actions.cpp index b7d2f07e..322c5d47 100644 --- a/client/scenes/stream_actions.cpp +++ b/client/scenes/stream_actions.cpp @@ -70,23 +70,20 @@ void scenes::stream::read_actions() void scenes::stream::operator()(to_headset::haptics && haptics) { - size_t i; - if (haptics.id == device_id::LEFT_CONTROLLER_HAPTIC) - i = 0; - else if (haptics.id == device_id::RIGHT_CONTROLLER_HAPTIC) - i = 1; - else - return; + auto range = haptics_actions.equal_range(haptics.id); + for (auto it = range.first; it != range.second; ++it) + { + XrAction action = it->second.action; + float old = it->second.amplitude; + it->second.amplitude = old; + // Some runtimes may be slow to process actions + // Skip it if not necessary + if (old == 0 and haptics.amplitude == 0) + continue; - XrAction action = haptics_actions[i].action; - float old = haptics_actions[i].amplitude.exchange(haptics.amplitude); - // Some runtimes may be slow to process actions - // Skip it if not necessary - if (old == 0 and haptics.amplitude == 0) - return; - - if (haptics.amplitude > 0) - application::haptic_start(action, XR_NULL_PATH, haptics.duration.count(), haptics.frequency, std::min(1.0f, haptics.amplitude)); - else - application::haptic_stop(action, XR_NULL_PATH); + if (haptics.amplitude > 0) + application::haptic_start(action, XR_NULL_PATH, haptics.duration.count(), haptics.frequency, std::min(1.0f, haptics.amplitude)); + else + application::haptic_stop(action, XR_NULL_PATH); + } }