1#include <cgv/base/base.h>
5#include <cgv/gui/application.h>
6#include <cgv/signal/rebind.h>
7#include <cgv/gui/trigger.h>
8#include <cgv/gui/choice_event.h>
14 bool array_unequal(
const float* a1,
const float* a2,
unsigned n) {
15 for (
unsigned i = 0; i < n; ++i)
23 last_device_scan = -1;
24 device_scan_interval = 1;
26 last_time_stamps.resize(vr_kit_handles.size(), 0);
31 device_scan_interval = duration;
46 if ((focus_type & VRF_PERMANENT) != 0)
53 focus_type = _focus_type;
60 if (!focus || focus != handler)
62 focus_type = VRF_RELEASED;
69 return event_type_flags;
74 event_type_flags = flags;
91 vec2 result = position;
137 const auto& CS_new = new_state.
controller[ci];
138 const auto& CS_lst = last_state.
controller[ci];
143 if (CS_new.button_flags == CS_lst.button_flags)
147 for (
unsigned j = 0; j < 19; ++j) {
150 if ((CS_new.button_flags & button_keys[j].flag) != (CS_lst.button_flags & button_keys[j].flag)) {
164 short key_offset = 0;
166 int x = CS_new.axes[0] > th ? 1 : (CS_new.axes[0] < -th ? -1 : 0);
167 int y = CS_new.axes[1] > th ? 1 : (CS_new.axes[1] < -th ? -1 : 0);
169 key_offset = 3 * y + x;
171 if (key_offset != 0) {
177 vr_key_event vrke(kit_handle, kit_index, ci, new_state, key,
186 const auto& CS_new = new_state.
controller[ci];
187 const auto& CS_lst = last_state.
controller[ci];
197 if (it == vr::VRI_NONE)
199 if (it == vr::VRI_TRIGGER) {
202 float x = correct_deadzone_and_precision(CS_new.axes[ai], CIC);
203 float dx = x - correct_deadzone_and_precision(CS_lst.axes[ai], CIC);
217 int new_p_or_t = ((CS_new.button_flags & touch_flag) != 0) ? 1 : 0;
218 if ((CS_new.button_flags & input_flag) != 0)
220 int old_p_or_t = ((CS_lst.button_flags & touch_flag) != 0) ? 1 : 0;
221 if ((CS_lst.button_flags & input_flag) != 0)
223 if (new_p_or_t != old_p_or_t) {
229 StickAction action = actions[new_p_or_t + 3 * old_p_or_t];
233 action, x, y, 0, 0, kit_index, 0, time);
239 vec2 p(CS_new.axes[ai], CS_new.axes[ai + 1]);
240 p = correct_deadzone_and_precision(p, CIC);
241 vec2 last_p(CS_lst.axes[ai], CS_lst.axes[ai + 1]);
242 last_p = correct_deadzone_and_precision(last_p, CIC);
243 vec2 diff = p - last_p;
244 if (diff(0) != 0 || diff(1) != 0) {
246 if ((CS_new.button_flags & input_flag) != 0)
249 action, p(0), p(1), diff(0), diff(1), kit_index, ii, time);
276 if (log_data[kit_index] && !(new_state == last_state)) {
277 log_data[kit_index]->log_vr_state(new_state,time);
279 last_state = new_state;
284 std::vector<bool> is_first_state(vr_kit_handles.size(),
false);
285 if (last_device_scan < 0 ||
286 ((device_scan_interval > 0) && (time > last_device_scan + device_scan_interval))) {
287 last_device_scan = time;
289 std::vector<vr::vr_kit_state> new_last_states(new_handles.size());
290 is_first_state.resize(new_handles.size(),
false);
293 for (
void* h1 : vr_kit_handles)
294 if (std::find(new_handles.begin(), new_handles.end(), h1) == new_handles.end())
297 std::vector<std::pair<void*, bool> > on_device_change_params;
300 for (
void* h2 : new_handles) {
301 auto iter = std::find(vr_kit_handles.begin(), vr_kit_handles.end(), h2);
303 if (iter == vr_kit_handles.end()) {
305 on_device_change_params.push_back(std::pair<void*, bool>(h2,
true));
306 is_first_state.at(i) =
true;
309 new_last_states[i] = last_states.at(iter - vr_kit_handles.begin());
313 vr_kit_handles = new_handles;
314 last_states = new_last_states;
315 for (
auto pp : on_device_change_params)
325 for (i = 0; i < vr_kit_handles.size(); ++i) {
344 auto iter = std::find(vr_kit_handles.begin(), vr_kit_handles.end(), kit_handle);
345 if (iter == vr_kit_handles.end())
347 size_t i = iter - vr_kit_handles.begin();
353 if (focus && focus_type != VRF_RELEASED) {
356 if ((focus_type & VRF_EXCLUSIVE) != 0)
363 auto it = log_data.find(kit_index);
364 if (log_data[kit_index]) {
365 log_data[kit_index]->disable_log();
366 log_data[kit_index] =
nullptr;
372 auto p = std::make_shared<std::ofstream>(fn);
383 auto it = log_data.find(kit_index);
384 if (it != log_data.end() && it->second)
385 it->second->disable_log();
390 return *log_data[kit_index];
394 return log_data[kit_index];
418 if (ref_dispatch_window_pointer_vr() != w) {
419 ref_dispatch_window_pointer_vr() = w;
421 if (connect_device_change_only_to_animation_trigger)
reference counted pointer, which can work together with types that are derived from ref_counted,...
bool empty() const
check if pointer is not yet set
static window_ptr get_window(unsigned int i)
return the i-th created window
class to represent choice events that include focus change and selection change events
void set_type(ChoiceEventType _type)
set the type of the choice event
interface for all classes that want to receive events
virtual bool handle(event &e)=0
overload and implement this method to handle events
void set_flags(unsigned char _flags)
return the set the event flags
static double get_current_time()
return the current time
vr key events use the key codes defined in vr::VRKeys
vr extension of pose events
bool dispatch(cgv::gui::event &e)
dispatch an event to focus handler and or signal attachments
void enable_log(const std::string fn="", const bool in_memory_log=true, const int filter=vr::vr_log::F_ALL, const int kit_index=0)
cgv::signal::signal< void *, int, vr::VRStatus, vr::VRStatus > on_status_change
signal emitted to notify about status changes of trackables, first argument is handle,...
void disable_log(const int kit_index=0)
disable logging and close log file
void check_device_changes(double time)
check which vr_kits are present and emit on_device_change events
bool grab_focus(VRFocus focus, event_handler *handler)
grab the event focus to the given event handler and return whether this was possible
bool release_focus(event_handler *handler)
release focus of handler and return whether handler had the focus
cgv::data::ref_ptr< vr::vr_log > get_log(const int kit_index=0)
returns a pointer to the active log data container, meant for extending the lifetime of the log data ...
vr_server()
construct server with default configuration
void set_event_type_flags(VREventTypeFlags flags)
set the event type flags of to be emitted events
vr::vr_log & ref_log(const int kit_index=0)
return a reference to the used vr_log object
cgv::signal::signal< void *, bool > on_device_change
signal emitted to notify about device changes, first argument is handle and second a flag telling whe...
void emit_events_and_update_state(void *kit_handle, const vr::vr_kit_state &new_state, int kit_index, VREventTypeFlags flags, double time)
void check_and_emit_events(double time)
check which vr_kits are present, query their current states and dispatch events through on_event,...
void set_device_scan_interval(double duration)
set time interval in seconds to check for device connection changes
bool check_new_state(void *kit_handle, const vr::vr_kit_state &new_state, double time)
in case the current vr state of a kit had been queried somewhere else, use this function to communica...
VREventTypeFlags get_event_type_flags() const
query the currently set event type flags
cgv::signal::bool_signal< cgv::gui::event & > on_event
signal emitted to dispatch events
vr extension of stick event
vr extension of throttle event
virtual bool dispatch_event(event &e)
dispatch a cgv event
T length() const
length of the vector L2-Norm
void zeros()
fill the vector with zeros
a vr kit is composed of headset, two controllers, and two trackers, where all devices can be attached...
const controller_input_config & get_controller_input_config(int controller_index, int input_index) const
query the configuration of a controller input
bool query_state(vr_kit_state &state, int pose_query=2)
query current state of vr kit and return whether this was successful
const vr_kit_info & get_device_info() const
return information on the currently attached devices
helper struct for logging vr events
void enable_in_memory_log()
enable in memory log
void lock_settings()
prevent changes to settings and enables log_vr_state methods
void set_filter(int f)
define what data should be recorded.
void enable_ostream_log(const std::shared_ptr< std::ostream > &stream)
enable writing to ostream.
data::ref_ptr< window > window_ptr
ref counted pointer to &window
vr_server & ref_vr_server()
return a reference to gamepad server singleton
@ KA_PRESS
key press action
@ KA_RELEASE
key release action
StickAction
different actions that a stick can perform
@ SA_DRAG
stick moved in pressed state
@ SA_RELEASE
stick release action
@ SA_PRESS
stick press action
@ SA_TOUCH
stick touch action
@ SA_UNPRESS
stick unpress repeated press action
@ SA_MOVE
stick moved with respect to last event
VRFocus
different types of event focus grabbing
VREventTypeFlags
flags to define which events should be generated by server
@ VRE_ONE_AXIS_GENERATES_KEY
whether one axis events should generate a key event when passing inputs threshold value
@ VRE_TWO_AXES
pad / stick events
@ VRE_ONE_AXIS
trigger / throttle / pedal events
@ VRE_STATUS
status change events
@ VRE_TWO_AXES_GENERATES_DPAD
whether two axes input generates direction pad keys when presses
@ VRE_DEVICE
device change events
void connect_vr_server(bool connect_device_change_only_to_animation_trigger, cgv::gui::window_ptr w)
connect the gamepad server to the given window or the first window of the application,...
trigger & get_animation_trigger()
return the global trigger used for animation, which runs by default with 60 Hz
@ EF_VR
whether event is from VR kit
VRButtonStateFlags
one flag for each vr controller button
@ VRF_INPUT0
button of input 0
@ VRF_MENU
application menu button
@ VRF_DPAD_LEFT
direction pad left button
@ VRF_INPUT1_TOUCH
touch sensor for input 1 which often is touchpad or stick
@ VRF_DPAD_RIGHT
direction pad right button
@ VRF_INPUT3_TOUCH
touch sensor for input 3 which often is touchpad or stick
@ VRF_DPAD_UP
direction pad up button
@ VRF_INPUT1
button of input 1
@ VRF_SYSTEM
system button
@ VRF_PROXIMITY
proximity sensor
@ VRF_INPUT4_TOUCH
touch sensor for input 4 which often is touchpad or stick
@ VRF_INPUT0_TOUCH
touch sensor for input 0 which often is touchpad or stick
@ VRF_INPUT2
button of input 2
@ VRF_INPUT2_TOUCH
touch sensor for input 2 which often is touchpad or stick
@ VRF_INPUT4
button of input 4
@ VRF_DPAD_DOWN
direction pad down button
@ VRF_INPUT3
button of input 3
vr_kit * get_vr_kit(void *handle)
query a pointer to a vr kit by its device handle, function can return null pointer in case that no vr...
std::vector< void * > scan_vr_kits()
iterate all registered vr drivers to scan for vr kits and return vector of vr kit handles
@ VRS_TRACKED
trackable is connected and tracked
@ VRS_DETACHED
trackable is not reachable via wireless
const unsigned max_nr_controllers
maximum number of attachable controller and tracker devices
VRInputType
different controller input types
const unsigned max_nr_controller_inputs
maximum number of inputs per controller
VRKeys
enumerate all VR keys starting at 1024
@ VR_INPUT3_TOUCH
touched input 3
@ VR_PROXIMITY
proximity sensor
@ VR_DPAD_RIGHT
direction pad right
@ VR_INPUT2_TOUCH
touched input 2
@ VR_INPUT1_TOUCH
touched input 1
@ VR_DPAD_LEFT
direction pad left
@ VR_MENU
VIVE: menu button; occulus: start button.
@ VR_DPAD_DOWN
direction pad down
@ VR_INPUT0_TOUCH
touched input 0
@ VR_DPAD_UP
direction pad up
@ VR_INPUT4_TOUCH
touched input 4
@ VR_SYSTEM
VIVE: system button; occulus: ???
VRInputType input_type[max_nr_controller_inputs]
type of up to 5vr::max_nr_controller_inputs inputs built into the controller
float axes[max_nr_controller_axes]
up to vr::max_nr_controller_axes axis values in the range [-1,1] or [0,1] (VIVE: 0|1....
vr_controller_info controller[max_nr_controllers]
information for attached controllers and trackers
structure that stores all information describing the state of a VR kit
vr_controller_state controller[max_nr_controllers]
status, pose, button, axes, and vibration information of up to vr::max_nr_controllers controller and ...
vr_trackable_state hmd
status and pose of hmd
VRStatus status
whether trackable is currently tracked, only in case of true, the pose member contains useful informa...
float pose[12]
pose as 3x4 matrix in column major format, where each column is a vector in world coordinates
defines the class vr::vr_driver class and gives access to the driver registry with the functions vr::...