gvr::ControllerApi

#include <gvr_controller.h>

This is a convenience C++ wrapper for the Controller C API.

Summary

This wrapper strategy prevents ABI compatibility issues between compilers by ensuring that the interface between client code and the implementation code in libgvr.so is a pure C interface. The translation from C++ calls to C calls provided by this wrapper runs entirely in the client's binary and is compiled by the client's compiler.

Methods in this class are only documented insofar as the C++ wrapping logic is concerned; for information about the method itself, please refer to the corresponding function in the C API.

Typical C++ initialization example:

std::unique_ptr controller_api(new ControllerApi);

// Your GVR context pointer (which can be obtained from GvrLayout)
gvr_context* context = .....;  // (get it from GvrLayout)

// Set up the options:
int32_t options = ControllerApi::DefaultOptions();

// Enable non-default options, if you need them:
options |= GVR_CONTROLLER_ENABLE_GYRO;

// Init the ControllerApi object:
bool success = controller_api->Init(options, context);
if (!success) {
  // Handle failure.
  // Do NOT call other methods (like Resume, etc) if init failed.
  return;
}

// Resume the ControllerApi (if your app is on the foreground).
controller_api->Resume();

ControllerState state;

Usage example:

void DrawFrame() {
  state.Update(*controller_api);
  // ... process controller state ...
}

// When your application gets paused:
void OnPause() {
  controller_api->Pause();
}

// When your application gets resumed:
void OnResume() {
  controller_api->Resume();
}

To conserve battery, be sure to call Pause() and Resume() when your app gets paused and resumed, respectively. This will allow the underlying logic to unbind from the VR service and let the controller sleep when no apps are using it.

THREADING: this class is thread-safe and reentrant after initialized with Init().

Inheritance

Inherits from: gvr::WrapperBase< gvr_controller_context, gvr_controller_destroy >

Public static functions

Create()
std::unique_ptr< ControllerApi >
DefaultOptions()
int32_t
Returns the default controller options.
ToString(ControllerApiStatus status)
const char *
Convenience functions to convert enums to strings.
ToString(ControllerConnectionState state)
const char *
ToString(ControllerButton button)
const char *
ToString(ControllerBatteryLevel level)
const char *

Public functions

ApplyArmModel(const ControllerHandedness handedness, const ArmModelBehavior behavior, const Mat4f & head_space_from_start_space_rotation)
void
For more information, see gvr_controller_apply_arm_model().
ApplyArmModel(int32_t controller_index, const ControllerHandedness handedness, const ArmModelBehavior behavior, const Mat4f & head_space_from_start_space_rotation)
void
For more information, see gvr_controller_apply_arm_model().
GetControllerCount()
int32_t
Returns the number (N) of controllers currently available.
Init(int32_t options, gvr_context *context)
bool
Initializes the controller API.
Init(JNIEnv *env, jobject android_context, jobject class_loader, int32_t options, gvr_context *context)
bool
Overload of Init() with explicit Android context and class loader (for Android only).
Init(int32_t options)
bool
Convenience overload that calls Init without a gvr_context.
Pause()
void
Pauses the controller.
Resume()
void
Resumes the controller.

Public static functions

Create

std::unique_ptr< ControllerApi > Create()

DefaultOptions

int32_t DefaultOptions()

Returns the default controller options.

ToString

const char * ToString(
  ControllerApiStatus status
)

Convenience functions to convert enums to strings.

For more information, see the corresponding functions in the C API.

ToString

const char * ToString(
  ControllerConnectionState state
)

ToString

const char * ToString(
  ControllerButton button
)

ToString

const char * ToString(
  ControllerBatteryLevel level
)

Public functions

ApplyArmModel

void ApplyArmModel(
  const ControllerHandedness handedness,
  const ArmModelBehavior behavior,
  const Mat4f & head_space_from_start_space_rotation
)

For more information, see gvr_controller_apply_arm_model().

ApplyArmModel

void ApplyArmModel(
  int32_t controller_index,
  const ControllerHandedness handedness,
  const ArmModelBehavior behavior,
  const Mat4f & head_space_from_start_space_rotation
)

For more information, see gvr_controller_apply_arm_model().

GetControllerCount

int32_t GetControllerCount()

Returns the number (N) of controllers currently available.

Each controller can be identified by an index in the range [0, N), which can be passed to the ControllerState constructor to get the state instance corresponding to the controller at that index.

Init

bool Init(
  int32_t options,
  gvr_context *context
)

Initializes the controller API.

This method must be called exactly once in the lifetime of this object. Other methods in this class may only be called after Init() returns true. Note: this does not cause the ControllerApi to be resumed. You must call Resume() explicitly in order to start using the controller.

For more information see gvr_controller_create_and_init().

Details
Returns
True if initialization was successful, false if it failed. Initialization may fail, for example, because invalid options were supplied.

Init

bool Init(
  JNIEnv *env,
  jobject android_context,
  jobject class_loader,
  int32_t options,
  gvr_context *context
)

Overload of Init() with explicit Android context and class loader (for Android only).

For more information, see: gvr_controller_create_and_init_android().

Init

bool Init(
  int32_t options
)

Convenience overload that calls Init without a gvr_context.

Pause

void Pause()

Pauses the controller.

For more information, see gvr_controller_pause().

Resume

void Resume()

Resumes the controller.

For more information, see gvr_controller_resume().