Skip to content

Instantly share code, notes, and snippets.

@ChristophHaag
Last active March 20, 2020 12:43
Show Gist options
  • Save ChristophHaag/7a96c6775dc24709cfbbf9e5bf1d5a65 to your computer and use it in GitHub Desktop.
Save ChristophHaag/7a96c6775dc24709cfbbf9e5bf1d5a65 to your computer and use it in GitHub Desktop.
openvr actions test snippet
// g++ actions.cpp -o actions -lopenvr_api
#include <openvr/openvr.h>
#include <stdio.h>
#include <chrono>
#include <thread>
bool showTrigger = false;
bool showActionSet = true;
using namespace vr;
void check_error(int line, EVRInitError error) {
if (error != 0)
printf("%d: error %s\n", line, VR_GetVRInitErrorAsSymbol(error));
}
void check_input_error(int line, EVRInputError error) {
if (error != EVRInputError::VRInputError_None) {
printf("Input error at %d: %d\n", line, error);
exit(1);
}
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage:\n\t%s /path/to/actions.json", argv[0]);
return 1;
}
const char *manifest_path = argv[1];
EVRInitError error;
VR_Init(&error, vr::VRApplication_Overlay);
check_error(__LINE__, error);
EVRInputError inputerr = VRInput()->SetActionManifestPath(manifest_path);
if (inputerr != EVRInputError::VRInputError_None)
printf("Input error %d: %d\n", __LINE__, inputerr);
VRActionSetHandle_t test_handle = 0;
inputerr = VRInput()->GetActionSetHandle("/actions/test", &test_handle);
check_input_error(__LINE__, inputerr);
VRActionHandle_t trigger_handle = 0;
inputerr = VRInput()->GetActionHandle("/actions/test/in/trigger", &trigger_handle);
check_input_error(__LINE__, inputerr);
VRActionHandle_t haptic_handle = 0;
inputerr = VRInput()->GetActionHandle("/actions/test/out/haptic", &haptic_handle);
check_input_error(__LINE__, inputerr);
VRActionSetHandle_t test2_handle = 0;
inputerr = VRInput()->GetActionSetHandle("/actions/test2", &test2_handle);
check_input_error(__LINE__, inputerr);
VRActionHandle_t pose_handle = 0;
inputerr = VRInput()->GetActionHandle("/actions/test2/in/hand_pose", &pose_handle);
check_input_error(__LINE__, inputerr);
VRInputValueHandle_t right_hand_handle = 0;
inputerr = VRInput()->GetInputSourceHandle("/user/hand/right", &right_hand_handle);
check_input_error(__LINE__, inputerr);
struct VRActiveActionSet_t active_test_action_set = {};
active_test_action_set.ulActionSet = test_handle;
struct VRActiveActionSet_t active_test2_action_set = {};
active_test2_action_set.ulActionSet = test2_handle;
struct VRActiveActionSet_t both[2] = {0};
both[0].ulActionSet = test_handle;
both[1].ulActionSet = test2_handle;
//std::this_thread::sleep_for(std::chrono::milliseconds(100));
while (true) {
inputerr = VRInput()->UpdateActionState(&active_test_action_set, sizeof(active_test_action_set), 1);
check_input_error(__LINE__, inputerr);
VRInputValueHandle_t origin_handles[k_unMaxActionOriginCount];
EVRInputError err = VRInput()->GetActionOrigins(test_handle, trigger_handle, origin_handles, k_unMaxActionOriginCount);
if (err != VRInputError_None) {
printf("GetActionOrigins for test failed, retrying later...\n");
} else {
int origin_count = -1;
while (origin_handles[++origin_count] != k_ulInvalidInputValueHandle);
printf ("Action test has %d origins\n", origin_count);
}
InputDigitalActionData_t data;
inputerr = VRInput()->GetDigitalActionData(trigger_handle, &data, sizeof(data), right_hand_handle);
check_input_error(__LINE__, inputerr);
//printf("Right Trigger State: %d, Changed: %d\n", data.bState, data.bChanged);
/*
if (showTrigger && data.bState && data.bChanged) {
printf("Showing action origins for test handle -> trigger handle\n");
inputerr = VRInput()->ShowActionOrigins(test_handle, trigger_handle);
check_input_error(__LINE__, inputerr);
}
if (showActionSet && data.bState && data.bChanged) {
printf("Showing bindings for actionset test handle\n");
VRActiveActionSet_t ActiveActionSets[] = {0};
ActiveActionSets[0].ulActionSet = test_handle;
inputerr = VRInput()->ShowBindingsForActionSet(ActiveActionSets, sizeof(VRActiveActionSet_t), 1, right_hand_handle);
check_input_error(__LINE__, inputerr);
}
*/
if (data.bState && data.bChanged) {
printf("Trigger haptic for %lu\n", right_hand_handle);
inputerr = VRInput()->TriggerHapticVibrationAction (
haptic_handle,
0,
0.1,
50,
1.0,
right_hand_handle);
check_input_error(__LINE__, inputerr);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}
{
"default_bindings":[
{
"controller_type":"vive_controller",
"binding_url":"bindings_vive_controller.json"
},
{
"controller_type": "knuckles",
"binding_url": "bindings_vive_controller.json"
}
],
"actions":[
{
"name":"/actions/test/in/trigger",
"type":"boolean"
},
{
"name": "/actions/test/in/hand_pose",
"type": "pose"
},
{
"name": "/actions/test/out/haptic",
"type": "vibration"
}
],
"action_sets": [
{
"name": "/actions/test",
"usage": "leftright"
}
],
"localization":[
{
"language_tag":"en_US",
"/actions/test/in/trigger":"Trigger",
"/actions/test/in/hand_pose":"Hand Pose"
}
]
}
{
"bindings" : {
"/actions/test" : {
"sources" : [
{
"path": "/user/hand/right/input/trigger",
"mode": "button",
"inputs": {
"click": {
"output": "/actions/test/in/trigger"
}
}
},
{
"path": "/user/hand/left/input/trigger",
"mode": "button",
"inputs": {
"click": {
"output": "/actions/test/in/trigger"
}
}
}
],
"poses": [
{
"output": "/actions/test/in/hand_pose",
"path": "/user/hand/right/pose/raw"
},
{
"output": "/actions/test/in/hand_pose",
"path": "/user/hand/left/pose/raw"
}
],
"haptics": [
{
"output": "/actions/test/out/haptic",
"path": "/user/hand/right/output/haptic"
},
{
"output": "/actions/test/out/haptic",
"path": "/user/hand/left/output/haptic"
}
]
}
},
"controller_type": "vive_controller",
"description" : "Test bindings",
"name" : "Vive Controller"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment