Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read our own action manifest to configure inputs #156

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move handle aquisition to where actions/sets are registered
vilhalmer committed Nov 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 5824888737ca04ae22caded196f927559d69d4da
114 changes: 53 additions & 61 deletions src/open_vr/openvr_data.cpp
Original file line number Diff line number Diff line change
@@ -573,27 +573,34 @@ void openvr_data::pre_render_update() {
// Note that we can't remove action sets once added so our index
// shouldn't change
int openvr_data::register_action_set(const String p_action_set) {
for (int i = 0; i < action_sets.size(); i++) {
if (action_sets[i].name == p_action_set) {
return i;
int set_index;

for (set_index = 0; set_index < action_sets.size(); set_index++) {
if (action_sets[set_index].name == p_action_set) {
break;
}
}

action_set new_action_set;
new_action_set.name = p_action_set;
new_action_set.handle = vr::k_ulInvalidActionSetHandle;
new_action_set.is_active = false;
// If we didn't find the set, initialize a new one.
if (set_index == action_sets.size()) {
action_set set;
set.name = p_action_set;
set.handle = vr::k_ulInvalidActionSetHandle;
set.is_active = false;
action_sets.push_back(set);
}

if (is_initialised()) {
vr::EVRInputError err = vr::VRInput()->GetActionSetHandle((const char *)new_action_set.name.utf8().get_data(), &new_action_set.handle);
action_set *set = &action_sets[set_index];

// Whether it already existed or not, we may need to get a handle for it. The default set is created before we have a runtime connection.
if (set->handle == vr::k_ulInvalidActionSetHandle && is_initialised()) {
vr::EVRInputError err = vr::VRInput()->GetActionSetHandle((const char *)set->name.utf8().get_data(), &set->handle);
if (err != vr::VRInputError_None) {
UtilityFunctions::print(String("Failed to obtain action set handle for ") + new_action_set.name);
UtilityFunctions::print(String("Failed to obtain action set handle for ") + set->name);
}
}

action_sets.push_back(new_action_set);

return (int)action_sets.size() - 1;
return set_index;
}

////////////////////////////////////////////////////////////////
@@ -649,6 +656,11 @@ bool openvr_data::is_action_set_active(const String p_action_set) const {
}

void openvr_data::add_input_action(const char *p_action, const char *p_path, const InputType p_type) {
if (!is_initialised()) {
UtilityFunctions::print("Cannot add input action before connecting to OpenVR");
return;
}

for (int i = 0; i < inputs.size(); i++) {
if (inputs[i].name == p_action) {
// already registered
@@ -662,6 +674,15 @@ void openvr_data::add_input_action(const char *p_action, const char *p_path, con
input.type = p_type;
input.handle = vr::k_ulInvalidActionHandle;
inputs.push_back(input);

vr::EVRInputError err = vr::VRInput()->GetActionHandle(input.path, &input.handle);
if (err != vr::VRInputError_None) {
input.handle = vr::k_ulInvalidActionHandle;

Array arr;
arr.push_back(String(input.path));
UtilityFunctions::print(String("Failed to obtain action handle for {0}").format(arr));
}
}

void openvr_data::remove_input_action(const char *p_action) {
@@ -740,6 +761,11 @@ void openvr_data::trigger_haptic_pulse(const char *p_action, const char *p_devic
}

void openvr_data::add_pose_action(const char *p_action, const char *p_path) {
if (!is_initialised()) {
UtilityFunctions::print("Cannot add pose action before connecting to OpenVR");
return;
}

for (int i = 0; i < poses.size(); i++) {
if (poses[i].name == p_action) {
// already registered
@@ -752,6 +778,15 @@ void openvr_data::add_pose_action(const char *p_action, const char *p_path) {
action.path = p_path;
action.handle = vr::k_ulInvalidActionHandle;
poses.push_back(action);

vr::EVRInputError err = vr::VRInput()->GetActionHandle(action.path, &action.handle);
if (err != vr::VRInputError_None) {
action.handle = vr::k_ulInvalidActionHandle;

Array arr;
arr.push_back(String(action.path));
UtilityFunctions::print(String("Failed to obtain action handle for {0}").format(arr));
}
}

void openvr_data::remove_pose_action(const char *p_action) {
@@ -981,6 +1016,11 @@ bool openvr_data::set_action_manifest_path(const String p_path) {
return false;
}

for (int i = 0; i < manifest_action_sets.size(); i++) {
String name = manifest_action_sets[i].get("name");
register_action_set(name);
}

for (int i = 0; i < manifest_actions.size(); i++) {
String name = manifest_actions[i].get("name");
String type = manifest_actions[i].get("type");
@@ -1000,54 +1040,6 @@ bool openvr_data::set_action_manifest_path(const String p_path) {
}
}

for (int i = 0; i < manifest_action_sets.size(); i++) {
String name = manifest_action_sets[i].get("name");
register_action_set(name);
}

for (std::vector<action_set>::iterator it = action_sets.begin(); it != action_sets.end(); ++it) {
vr::EVRInputError err = vr::VRInput()->GetActionSetHandle((const char *)it->name.utf8().get_data(), &it->handle);
if (err != vr::VRInputError_None) {
Array arr;
arr.push_back(String(it->name));
UtilityFunctions::print(String("Failed to obtain action set handle for {0}").format(arr));
}
}

for (auto &input : inputs) {
// setup handle
char action_path[1024];
// TODO at some point support additional action sets
sprintf(action_path, "%s/in/%s", (const char *)action_sets[0].name.utf8().get_data(), input.path);

vr::EVRInputError err = vr::VRInput()->GetActionHandle(action_path, &input.handle);
if (err != vr::VRInputError_None) {
// maybe output something?
input.handle = vr::k_ulInvalidActionHandle;

Array arr;
arr.push_back(String(action_path));
UtilityFunctions::print(String("Failed to obtain action handle for {0}").format(arr));
}
}

for (auto &pose : poses) {
// setup handle
char action_path[1024];
// TODO at some point support additional action sets
sprintf(action_path, "%s/in/%s", (const char *)action_sets[0].name.utf8().get_data(), pose.path);

vr::EVRInputError err = vr::VRInput()->GetActionHandle(action_path, &pose.handle);
if (err != vr::VRInputError_None) {
// maybe output something?
pose.handle = vr::k_ulInvalidActionHandle;

Array arr;
arr.push_back(String(action_path));
UtilityFunctions::print(String("Failed to obtain action handle for {0}").format(arr));
}
}

return true;
}