cgv
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 More...
 
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 More...
 
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)
 

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;
}

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.

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

◆ check_new_state()

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

◆ 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

◆ 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

The documentation for this class was generated from the following files:
cgv::gui::vr_stick_event
vr extension of stick event
Definition: vr_events.h:75
cgv::gui::stick_event::get_action
StickAction get_action() const
return the stick action
Definition: stick_event.cxx:63
cgv::gui::SA_TOUCH
@ SA_TOUCH
stick touch action
Definition: stick_event.h:16
cgv::gui::SA_DRAG
@ SA_DRAG
stick moved in pressed state
Definition: stick_event.h:21
cgv::gui::stick_event::get_controller_index
unsigned get_controller_index() const
return controller index
Definition: stick_event.cxx:34
cgv::gui::EF_VR
@ EF_VR
whether event is from VR kit
Definition: event.h:35
cgv::gui::SA_UNPRESS
@ SA_UNPRESS
stick unpress repeated press action
Definition: stick_event.h:18
cgv::gui::stick_event::get_last_x
float get_last_x() const
return the last x value of the stick
Definition: stick_event.cxx:107
cgv::gui::stick_event::get_stick_index
unsigned get_stick_index() const
return stick index
Definition: stick_event.cxx:39
cgv::gui::stick_event::get_y
float get_y() const
return the current y value of the stick
Definition: stick_event.cxx:74
cgv::gui::event::get_flags
unsigned get_flags() const
return the event flags
Definition: event.cxx:214
cgv::gui::EID_STICK
@ EID_STICK
id of a stick event describing a two axis controller that optionally can be touched and pressed
Definition: event.h:20
cgv::gui::SA_RELEASE
@ SA_RELEASE
stick release action
Definition: stick_event.h:19
cgv::gui::stick_event::get_last_y
float get_last_y() const
return the last y value of the stick
Definition: stick_event.cxx:112
cgv::gui::event
Definition: event.h:59
cgv::gui::get_stick_action_string
std::string get_stick_action_string(StickAction action)
convert a key action into a readable string
Definition: stick_event.cxx:8
cgv::gui::event::get_kind
unsigned get_kind() const
return, what kind of event this is, typically a value from the EventId enum
Definition: event.cxx:202
cgv::gui::SA_PRESS
@ SA_PRESS
stick press action
Definition: stick_event.h:17
cgv::gui::SA_MOVE
@ SA_MOVE
stick moved with respect to last event
Definition: stick_event.h:20
cgv::gui::stick_event::get_x
float get_x() const
return the current x value of the stick
Definition: stick_event.cxx:69