History

almost finished plugin architecture, general plugin improvements. menu actions don't work all the time, joint controllers need to be handled better, and activate/deactive aren't always called correctly

Anthony J. Thibault committed 11 years ago

Commit 060f6c11 parent 108353420 files · +367 −204Browse files

Showing the changes to libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp (1 of 20 files). Show all

Unified · Split

libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp+53−58
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include "UserActivityLogger.h"
2323
2424 extern vr::IVRSystem *_hmd;
25+extern int hmdRefCount;
2526 extern vr::TrackedDevicePose_t _trackedDevicePose[vr::k_unMaxTrackedDeviceCount];
2627 extern mat4 _trackedDevicePoseMat4[vr::k_unMaxTrackedDeviceCount];
2728
@@ -43,15 +44,8 @@ const QString CONTROLLER_MODEL_STRING = "vr_controller_05_wireless_b"; · 15 unmodified lines
4344
4445 const QString ViveControllerManager::NAME = "OpenVR";
4546
46−ViveControllerManager& ViveControllerManager::getInstance() {
47− static ViveControllerManager sharedInstance;
48− return sharedInstance;
49−}
50−
5147 ViveControllerManager::ViveControllerManager() :
5248 InputDevice("SteamVR Controller"),
53− _isInitialized(false),
54− _isEnabled(true),
5549 _trackedControllers(0),
5650 _modelLoaded(false),
5751 _leftHandRenderID(0),
@@ -60,19 +54,12 @@ ViveControllerManager::ViveControllerManager() : · 2 unmodified lines
6054
6155 }
6256
63−ViveControllerManager::~ViveControllerManager() {
64−
65−}
66−
6757 bool ViveControllerManager::isSupported() const {
6858 return vr::VR_IsHmdPresent();
6959 }
7060
71−void ViveControllerManager::deinit() {
72− // TODO: deinit openvr? avoid conflicts with display plugin
73−}
74−
75−void ViveControllerManager::init() {
61+void ViveControllerManager::activate(PluginContainer* container) {
62+ hmdRefCount++;
7663 if (!_hmd) {
7764 vr::HmdError eError = vr::HmdError_None;
7865 _hmd = vr::VR_Init(&eError);
@@ -125,8 +112,16 @@ void ViveControllerManager::init() { · 46 unmodified lines
125112
126113 _modelLoaded = true;
127114 }
115+}
116+
117+void ViveControllerManager::deactivate() {
118+ hmdRefCount--;
128119
129− _isInitialized = true;
120+ if (hmdRefCount == 0 && _hmd) {
121+ vr::VR_Shutdown();
122+ _hmd = nullptr;
123+ }
124+ _poseStateMap.clear();
130125 }
131126
132127 void ViveControllerManager::updateRendering(RenderArgs* args, render::ScenePointer scene, render::PendingChanges pendingChanges) {
@@ -192,68 +187,68 @@ void ViveControllerManager::renderHand(UserInputMapper::PoseValue pose, gpu::Bat · 59 unmodified lines
192187 }
193188
194189 void ViveControllerManager::update(float deltaTime) {
190+ // TODO: This shouldn't be necessary
195191 if (!_hmd) {
196192 return;
197193 }
194+
198195 _buttonPressedMap.clear();
199− if (_isInitialized && _isEnabled) {
200196
201− PerformanceTimer perfTimer("ViveControllerManager::update");
197+ PerformanceTimer perfTimer("ViveControllerManager::update");
202198
203− int numTrackedControllers = 0;
199+ int numTrackedControllers = 0;
204200
205− for (vr::TrackedDeviceIndex_t device = vr::k_unTrackedDeviceIndex_Hmd + 1;
206− device < vr::k_unMaxTrackedDeviceCount && numTrackedControllers < 2; ++device) {
201+ for (vr::TrackedDeviceIndex_t device = vr::k_unTrackedDeviceIndex_Hmd + 1;
202+ device < vr::k_unMaxTrackedDeviceCount && numTrackedControllers < 2; ++device) {
207203
208− if (!_hmd->IsTrackedDeviceConnected(device)) {
209− continue;
210− }
204+ if (!_hmd->IsTrackedDeviceConnected(device)) {
205+ continue;
206+ }
211207
212− if(_hmd->GetTrackedDeviceClass(device) != vr::TrackedDeviceClass_Controller) {
213− continue;
214− }
208+ if(_hmd->GetTrackedDeviceClass(device) != vr::TrackedDeviceClass_Controller) {
209+ continue;
210+ }
215211
216− if (!_trackedDevicePose[device].bPoseIsValid) {
217− continue;
218− }
212+ if (!_trackedDevicePose[device].bPoseIsValid) {
213+ continue;
214+ }
219215
220− numTrackedControllers++;
216+ numTrackedControllers++;
221217
222− const mat4& mat = _trackedDevicePoseMat4[device];
218+ const mat4& mat = _trackedDevicePoseMat4[device];
223219
224− handlePoseEvent(mat, numTrackedControllers - 1);
220+ handlePoseEvent(mat, numTrackedControllers - 1);
225221
226− // handle inputs
227− vr::VRControllerState_t controllerState = vr::VRControllerState_t();
228− if (_hmd->GetControllerState(device, &controllerState)) {
229− //qDebug() << (numTrackedControllers == 1 ? "Left: " : "Right: ");
230− //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y;
231− //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y;
232− handleButtonEvent(controllerState.ulButtonPressed, numTrackedControllers - 1);
233− for (int i = 0; i < vr::k_unControllerStateAxisCount; i++) {
234− handleAxisEvent(Axis(i), controllerState.rAxis[i].x, controllerState.rAxis[i].y, numTrackedControllers - 1);
235− }
222+ // handle inputs
223+ vr::VRControllerState_t controllerState = vr::VRControllerState_t();
224+ if (_hmd->GetControllerState(device, &controllerState)) {
225+ //qDebug() << (numTrackedControllers == 1 ? "Left: " : "Right: ");
226+ //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y;
227+ //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y;
228+ handleButtonEvent(controllerState.ulButtonPressed, numTrackedControllers - 1);
229+ for (int i = 0; i < vr::k_unControllerStateAxisCount; i++) {
230+ handleAxisEvent(Axis(i), controllerState.rAxis[i].x, controllerState.rAxis[i].y, numTrackedControllers - 1);
236231 }
237232 }
233+ }
238234
239− auto userInputMapper = DependencyManager::get<UserInputMapper>();
240−
241− if (numTrackedControllers == 0) {
242− if (_deviceID != 0) {
243− userInputMapper->removeDevice(_deviceID);
244− _deviceID = 0;
245− _poseStateMap.clear();
246− }
247− }
235+ auto userInputMapper = DependencyManager::get<UserInputMapper>();
248236
249− if (_trackedControllers == 0 && numTrackedControllers > 0) {
250− registerToUserInputMapper(*userInputMapper);
251− assignDefaultInputMapping(*userInputMapper);
252− UserActivityLogger::getInstance().connectedDevice("spatial_controller", "steamVR");
237+ if (numTrackedControllers == 0) {
238+ if (_deviceID != 0) {
239+ userInputMapper->removeDevice(_deviceID);
240+ _deviceID = 0;
241+ _poseStateMap.clear();
253242 }
243+ }
254244
255− _trackedControllers = numTrackedControllers;
245+ if (_trackedControllers == 0 && numTrackedControllers > 0) {
246+ registerToUserInputMapper(*userInputMapper);
247+ assignDefaultInputMapping(*userInputMapper);
248+ UserActivityLogger::getInstance().connectedDevice("spatial_controller", "steamVR");
256249 }
250+
251+ _trackedControllers = numTrackedControllers;
257252 }
258253
259254 void ViveControllerManager::focusOutEvent() {