Support trigger and thumb haptics

This commit is contained in:
Patrick Nicolas
2025-02-21 18:43:15 +01:00
parent 8443406a8f
commit 52fd337526
3 changed files with 38 additions and 24 deletions
+21 -4
View File
@@ -242,11 +242,28 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
self->fence = 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())
+2 -2
View File
@@ -113,9 +113,9 @@ private:
{
XrAction action;
XrPath path;
std::atomic<float> amplitude;
float amplitude;
};
std::array<haptics_action, 2> haptics_actions;
std::unordered_multimap<device_id, haptics_action> haptics_actions;
std::vector<std::tuple<device_id, XrAction, XrActionType>> input_actions;
state state_ = state::initializing;
+15 -18
View File
@@ -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);
}
}