cgv
Loading...
Searching...
No Matches
cgv::gui::vr_server Class Reference

server for vr events More...

#include <vr_server.h>

Public Member Functions

 vr_server ()
 construct server with default configuration
 
VREventTypeFlags get_event_type_flags () const
 query the currently set event type flags
 
void set_event_type_flags (VREventTypeFlags flags)
 set the event type flags of to be emitted events
 
void set_device_scan_interval (double duration)
 set time interval in seconds to check for device connection changes
 
double get_device_scan_interval () const
 return the time interval in seconds to check for device connection changes
 
void check_device_changes (double time)
 check which vr_kits are present and emit on_device_change events
 
void check_and_emit_events (double time)
 check which vr_kits are present, query their current states and dispatch events through on_event, on_status_change and on_device_change signals
 
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 communicate the new state to the server; return whether kit_handle had been seen by server before
 
bool check_new_state (void *kit_handle, const vr::vr_kit_state &new_state, double time, VREventTypeFlags flags)
 same as previous function but with overwrite of flags
 
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
 
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)
 
void disable_log (const int kit_index=0)
 disable logging and close log file
 
vr::vr_logref_log (const int kit_index=0)
 return a reference to the used vr_log object
 
cgv::data::ref_ptr< vr::vr_logget_log (const int kit_index=0)
 returns a pointer to the active log data container, meant for extending the lifetime of the log data beyond a new enable_log call
 

Public Attributes

cgv::signal::bool_signal< cgv::gui::event & > on_event
 signal emitted to dispatch events
 
cgv::signal::signal< void *, bool > on_device_change
 signal emitted to notify about device changes, first argument is handle and second a flag telling whether device got connected or if false disconnected
 
cgv::signal::signal< void *, int, vr::VRStatus, vr::VRStatuson_status_change
 signal emitted to notify about status changes of trackables, first argument is handle, second -1 for hmd + 0|1 for left|right controller, third is old status and fourth new status
 

Protected Member Functions

void emit_events_and_update_state (void *kit_handle, const vr::vr_kit_state &new_state, int kit_index, VREventTypeFlags flags, double time)
 
float correct_deadzone_and_precision (float value, const vr::controller_input_config &IC)
 
vec2 correct_deadzone_and_precision (const vec2 &position, const vr::controller_input_config &IC)
 

Protected Attributes

double last_device_scan
 
double device_scan_interval
 
std::vector< void * > vr_kit_handles
 
std::vector< vr::vr_kit_statelast_states
 
std::vector< unsigned > last_time_stamps
 
VREventTypeFlags event_type_flags
 
VRFocus focus_type
 
event_handlerfocus
 
std::unordered_map< int, cgv::data::ref_ptr< vr::vr_log > > log_data
 

Detailed Description

server for vr events

It keeps the previous state of each vr kit and compares it with current state. In case of state changes, the server can emit device change, status change, key, throttle, stick and pose events. For key, throttle, stick and pose events, the classes cgv::gui::vr_key_event, cgv::gui::vr_throttle_event, cgv::gui::vr_stick_event, and cgv::gui::vr_pose_event are used and the event flag cgv::gui::EF_VR is set to mark it as vr event. To process for example a stick event in the cgv::gui::event_handler::handle() function, the following shows a code sample:

#include <cg_vr/vr_server.h>
#include <cgv/gui/event_handler.h>
bool handle(cgv::gui::event& e)
{
// check if vr event flag is not set and don't process events in this case
if ((e.get_flags() & cgv::gui::EF_VR) == 0)
return false;
// check event id
switch (vrse.get_action()) {
std::cout << "stick " << vrse.get_stick_index()
<< " of controller " << vrse.get_controller_index()
<< " at " << vrse.get_x() << ", " << vrse.get_y() << std::endl;
return true;
std::cout << "stick " << vrse.get_stick_index()
<< " of controller " << vrse.get_controller_index()
<< " from " << vrse.get_last_x() << ", " << vrse.get_last_y()
<< " to " << vrse.get_x() << ", " << vrse.get_y() << std::endl;
return true;
}
return true;
}
return false;
}
unsigned get_kind() const
return, what kind of event this is, typically a value from the EventId enum
Definition event.cxx:202
unsigned get_flags() const
return the event flags
Definition event.cxx:214
unsigned get_stick_index() const
return stick index
float get_last_x() const
return the last x value of the stick
float get_y() const
return the current y value of the stick
float get_last_y() const
return the last y value of the stick
unsigned get_controller_index() const
return controller index
StickAction get_action() const
return the stick action
float get_x() const
return the current x value of the stick
vr extension of stick event
Definition vr_events.h:78
std::string get_stick_action_string(StickAction action)
convert a key action into a readable string
@ SA_DRAG
stick moved in pressed state
Definition stick_event.h:21
@ SA_RELEASE
stick release action
Definition stick_event.h:19
@ SA_PRESS
stick press action
Definition stick_event.h:17
@ SA_TOUCH
stick touch action
Definition stick_event.h:16
@ SA_UNPRESS
stick unpress repeated press action
Definition stick_event.h:18
@ SA_MOVE
stick moved with respect to last event
Definition stick_event.h:20
@ EID_STICK
id of a stick event describing a two axis controller that optionally can be touched and pressed
Definition event.h:20
@ EF_VR
whether event is from VR kit
Definition event.h:35

Which events are emitted by the server can be configured with the set_event_type_flags() function. Device change events are emitted with the vr_server::on_device_change signal, status change events with the vr_server::on_status_change signal and all others via the vr_server::on_event signal.

There is a singleton vr_server instance provided with cgv::gui::ref_vr_server(). The current vr_kit states are polled with the check_and_emit_events() or in case some other class has queried the states with the check_new_state() functions for each vr_kit separately. In an interval of get_device_scan_interval() seconds the vr_kits are scanned again to detect device changes.

The vr_server can be used in three ways. In all cases you have to connect directly to the vr_server::on_device_change or vr_server::on_status_change signals in order to receive device change or status change events.

  1. call the cgv::gui::connect_vr_server() function in the constructor of one of your plugin classes with false as function parameter. Then a predefined callback function is attached to the animation trigger singleton (accessed through the header <cgv/gui/trigger.h>) that calls the vr_server singleton's check_and_emit_events() function with 60Hz. The vr_server::on_event signal is attached to a callback that dispatches the events with the standard event processing of the framework. All registered classes derived from cgv::gui::event_handler will see the events and can process them.
  2. call the cgv::gui::connect_vr_server() function in the constructor of one of your plugin classes with true as function parameter. Again a callback function is attached to the animation trigger singleton but it calls the vr_server singleton's check_device_changes() such that only device change events are generated. In this case you or the crg_vr_view plugin have to poll the vr_kit states and call the vr_server's check_new_state() function. Again the vr_server::on_event signal is attached to a callback that dispatches the events to all registered cgv::gui::event_handler instances.
  3. connect directly to the vr_server::on_event signal and a timer event function to the animation trigger. Then you need to call the vr_server's check_and_emit_events() or check_new_state() function in your timer event function. In this approach all vr events are dispatched only to the callback function that you attach to the vr_server::on_event signal of the vr_server singleton.

Definition at line 135 of file vr_server.h.

Constructor & Destructor Documentation

◆ vr_server()

cgv::gui::vr_server::vr_server ( )

construct server with default configuration

Definition at line 21 of file vr_server.cxx.

References cgv::gui::VRE_ALL.

Member Function Documentation

◆ check_and_emit_events()

void cgv::gui::vr_server::check_and_emit_events ( double  time)

check which vr_kits are present, query their current states and dispatch events through on_event, on_status_change and on_device_change signals

check enabled gamepad devices for new events and dispatch them through the on_event signal

Definition at line 320 of file vr_server.cxx.

References check_device_changes(), emit_events_and_update_state(), vr::get_vr_kit(), and vr::vr_kit::query_state().

Referenced by cgv::gui::connect_vr_server().

◆ check_device_changes()

void cgv::gui::vr_server::check_device_changes ( double  time)

check which vr_kits are present and emit on_device_change events

Definition at line 282 of file vr_server.cxx.

References get_event_type_flags(), on_device_change, vr::scan_vr_kits(), and cgv::gui::VRE_DEVICE.

Referenced by check_and_emit_events(), and cgv::gui::connect_vr_server().

◆ check_new_state() [1/2]

bool cgv::gui::vr_server::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 communicate the new state to the server; return whether kit_handle had been seen by server before

in case the current vr state of a kit had been queried outside, use this function to communicate the new state to the server

Definition at line 337 of file vr_server.cxx.

References check_new_state().

Referenced by check_new_state().

◆ check_new_state() [2/2]

bool cgv::gui::vr_server::check_new_state ( void *  kit_handle,
const vr::vr_kit_state new_state,
double  time,
VREventTypeFlags  flags 
)

same as previous function but with overwrite of flags

Definition at line 342 of file vr_server.cxx.

References emit_events_and_update_state().

◆ correct_deadzone_and_precision() [1/2]

vec2 cgv::gui::vr_server::correct_deadzone_and_precision ( const vec2 position,
const vr::controller_input_config IC 
)
protected

Definition at line 89 of file vr_server.cxx.

◆ correct_deadzone_and_precision() [2/2]

float cgv::gui::vr_server::correct_deadzone_and_precision ( float  value,
const vr::controller_input_config IC 
)
protected

Definition at line 77 of file vr_server.cxx.

◆ disable_log()

void cgv::gui::vr_server::disable_log ( const int  kit_index = 0)

disable logging and close log file

Definition at line 381 of file vr_server.cxx.

◆ dispatch()

bool cgv::gui::vr_server::dispatch ( cgv::gui::event e)

dispatch an event to focus handler and or signal attachments

Definition at line 351 of file vr_server.cxx.

References cgv::gui::event_handler::handle(), and on_event.

Referenced by emit_events_and_update_state().

◆ emit_events_and_update_state()

void cgv::gui::vr_server::emit_events_and_update_state ( void *  kit_handle,
const vr::vr_kit_state new_state,
int  kit_index,
VREventTypeFlags  flags,
double  time 
)
protected

construct a throttle event from value and value change

Definition at line 100 of file vr_server.cxx.

References vr::vr_controller_state::axes, vr::vr_kit_info::controller, vr::vr_kit_state::controller, dispatch(), vr::vr_kit::get_controller_input_config(), vr::vr_kit::get_device_info(), vr::get_vr_kit(), vr::vr_kit_state::hmd, vr::vr_controller_info::input_type, cgv::gui::KA_PRESS, cgv::gui::KA_RELEASE, vr::max_nr_controller_inputs, vr::max_nr_controllers, on_status_change, vr::vr_trackable_state::pose, cgv::gui::SA_DRAG, cgv::gui::SA_MOVE, cgv::gui::SA_PRESS, cgv::gui::SA_RELEASE, cgv::gui::SA_TOUCH, cgv::gui::SA_UNPRESS, vr::vr_trackable_state::status, vr::controller_input_config::threshold, vr::VR_A, vr::VR_DPAD_DOWN, vr::VR_DPAD_LEFT, vr::VR_DPAD_RIGHT, vr::VR_DPAD_UP, vr::VR_GRIP, vr::VR_INPUT0, vr::VR_INPUT0_TOUCH, vr::VR_INPUT1, vr::VR_INPUT1_TOUCH, vr::VR_INPUT2, vr::VR_INPUT2_TOUCH, vr::VR_INPUT3, vr::VR_INPUT3_TOUCH, vr::VR_INPUT4, vr::VR_INPUT4_TOUCH, vr::VR_MENU, vr::VR_PROXIMITY, vr::VR_SYSTEM, cgv::gui::VRE_KEY, cgv::gui::VRE_ONE_AXIS, cgv::gui::VRE_ONE_AXIS_GENERATES_KEY, cgv::gui::VRE_POSE, cgv::gui::VRE_STATUS, cgv::gui::VRE_TWO_AXES, cgv::gui::VRE_TWO_AXES_GENERATES_DPAD, vr::VRF_A, vr::VRF_DPAD_DOWN, vr::VRF_DPAD_LEFT, vr::VRF_DPAD_RIGHT, vr::VRF_DPAD_UP, vr::VRF_GRIP, vr::VRF_INPUT0, vr::VRF_INPUT0_TOUCH, vr::VRF_INPUT1, vr::VRF_INPUT1_TOUCH, vr::VRF_INPUT2, vr::VRF_INPUT2_TOUCH, vr::VRF_INPUT3, vr::VRF_INPUT3_TOUCH, vr::VRF_INPUT4, vr::VRF_INPUT4_TOUCH, vr::VRF_MENU, vr::VRF_PROXIMITY, vr::VRF_SYSTEM, vr::VRS_DETACHED, and vr::VRS_TRACKED.

Referenced by check_and_emit_events(), and check_new_state().

◆ enable_log()

void cgv::gui::vr_server::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 
)

creates a logfile and activates logging of vr .

Parameters
fnpath to logfile. pass an empty string to disable writing to a log file

Definition at line 361 of file vr_server.cxx.

References vr::vr_log::enable_in_memory_log(), vr::vr_log::enable_ostream_log(), vr::vr_log::lock_settings(), and vr::vr_log::set_filter().

◆ get_device_scan_interval()

double cgv::gui::vr_server::get_device_scan_interval ( ) const
inline

return the time interval in seconds to check for device connection changes

Definition at line 163 of file vr_server.h.

◆ get_event_type_flags()

VREventTypeFlags cgv::gui::vr_server::get_event_type_flags ( ) const

query the currently set event type flags

Definition at line 67 of file vr_server.cxx.

Referenced by check_device_changes().

◆ get_log()

cgv::data::ref_ptr< vr::vr_log > cgv::gui::vr_server::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 beyond a new enable_log call

Definition at line 392 of file vr_server.cxx.

◆ grab_focus()

bool cgv::gui::vr_server::grab_focus ( VRFocus  focus,
event_handler handler 
)

grab the event focus to the given event handler and return whether this was possible

Definition at line 39 of file vr_server.cxx.

References cgv::gui::EF_VR, cgv::gui::trigger::get_current_time(), cgv::gui::event_handler::handle(), cgv::gui::event::set_flags(), and cgv::gui::choice_event::set_type().

◆ ref_log()

vr::vr_log & cgv::gui::vr_server::ref_log ( const int  kit_index = 0)

return a reference to the used vr_log object

Definition at line 388 of file vr_server.cxx.

◆ release_focus()

bool cgv::gui::vr_server::release_focus ( event_handler handler)

release focus of handler and return whether handler had the focus

Definition at line 58 of file vr_server.cxx.

◆ set_device_scan_interval()

void cgv::gui::vr_server::set_device_scan_interval ( double  duration)

set time interval in seconds to check for device connection changes

Definition at line 29 of file vr_server.cxx.

◆ set_event_type_flags()

void cgv::gui::vr_server::set_event_type_flags ( VREventTypeFlags  flags)

set the event type flags of to be emitted events

Definition at line 72 of file vr_server.cxx.

Member Data Documentation

◆ device_scan_interval

double cgv::gui::vr_server::device_scan_interval
protected

Definition at line 139 of file vr_server.h.

◆ event_type_flags

VREventTypeFlags cgv::gui::vr_server::event_type_flags
protected

Definition at line 143 of file vr_server.h.

◆ focus

event_handler* cgv::gui::vr_server::focus
protected

Definition at line 145 of file vr_server.h.

◆ focus_type

VRFocus cgv::gui::vr_server::focus_type
protected

Definition at line 144 of file vr_server.h.

◆ last_device_scan

double cgv::gui::vr_server::last_device_scan
protected

Definition at line 138 of file vr_server.h.

◆ last_states

std::vector<vr::vr_kit_state> cgv::gui::vr_server::last_states
protected

Definition at line 141 of file vr_server.h.

◆ last_time_stamps

std::vector<unsigned> cgv::gui::vr_server::last_time_stamps
protected

Definition at line 142 of file vr_server.h.

◆ log_data

std::unordered_map<int, cgv::data::ref_ptr<vr::vr_log> > cgv::gui::vr_server::log_data
protected

Definition at line 146 of file vr_server.h.

◆ on_device_change

cgv::signal::signal<void*, bool> cgv::gui::vr_server::on_device_change

signal emitted to notify about device changes, first argument is handle and second a flag telling whether device got connected or if false disconnected

Definition at line 181 of file vr_server.h.

Referenced by check_device_changes().

◆ on_event

cgv::signal::bool_signal<cgv::gui::event&> cgv::gui::vr_server::on_event

signal emitted to dispatch events

Definition at line 179 of file vr_server.h.

Referenced by dispatch().

◆ on_status_change

cgv::signal::signal<void*, int, vr::VRStatus, vr::VRStatus> cgv::gui::vr_server::on_status_change

signal emitted to notify about status changes of trackables, first argument is handle, second -1 for hmd + 0|1 for left|right controller, third is old status and fourth new status

Definition at line 183 of file vr_server.h.

Referenced by emit_events_and_update_state().

◆ vr_kit_handles

std::vector<void*> cgv::gui::vr_server::vr_kit_handles
protected

Definition at line 140 of file vr_server.h.


The documentation for this class was generated from the following files: