1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef GFX_VR_SERVICE_OPENVRCONTROLLERMAPPER_H
8 #define GFX_VR_SERVICE_OPENVRCONTROLLERMAPPER_H
9 
10 #include "openvr.h"
11 #include "nsString.h"
12 
13 #include "moz_external_vr.h"
14 
15 namespace mozilla {
16 namespace gfx {
17 
18 struct ControllerAction {
19   nsCString name;
20   nsCString type;
21   vr::VRActionHandle_t handle = vr::k_ulInvalidActionHandle;
22 
23   ControllerAction() = default;
24 
ControllerActionControllerAction25   ControllerAction(const char* aName, const char* aType)
26       : name(aName), type(aType) {}
27 };
28 
29 struct ControllerInfo {
30   vr::VRInputValueHandle_t mSource = vr::k_ulInvalidInputValueHandle;
31 
32   ControllerAction mActionPose;
33   ControllerAction mActionHaptic;
34 
35   ControllerAction mActionTrackpad_Analog;
36   ControllerAction mActionTrackpad_Pressed;
37   ControllerAction mActionTrackpad_Touched;
38 
39   ControllerAction mActionTrigger_Value;
40 
41   ControllerAction mActionGrip_Pressed;
42   ControllerAction mActionGrip_Touched;
43   ControllerAction mActionMenu_Pressed;
44   ControllerAction mActionMenu_Touched;
45   // It seems like there's no way to get response from a sys. btn.
46   ControllerAction mActionSystem_Pressed;
47   ControllerAction mActionSystem_Touched;
48 
49   // --- Knuckles & Cosmos
50   ControllerAction mActionA_Pressed;
51   ControllerAction mActionA_Touched;
52   ControllerAction mActionB_Pressed;
53   ControllerAction mActionB_Touched;
54 
55   // --- Knuckles, Cosmos, and WMR
56   ControllerAction mActionThumbstick_Analog;
57   ControllerAction mActionThumbstick_Pressed;
58   ControllerAction mActionThumbstick_Touched;
59 
60   // --- Knuckles
61   ControllerAction mActionFingerIndex_Value;
62   ControllerAction mActionFingerMiddle_Value;
63   ControllerAction mActionFingerRing_Value;
64   ControllerAction mActionFingerPinky_Value;
65 
66   // --- Cosmos
67   ControllerAction mActionBumper_Pressed;
68 };
69 
70 class OpenVRControllerMapper {
71  public:
72   OpenVRControllerMapper();
73   virtual ~OpenVRControllerMapper() = default;
74 
75   virtual void UpdateButtons(VRControllerState& aControllerState,
76                              ControllerInfo& aControllerInfo) = 0;
GetButtonCount()77   uint32_t GetButtonCount() { return mNumButtons; }
GetAxisCount()78   uint32_t GetAxisCount() { return mNumAxes; }
79 
80  protected:
81   void GetButtonValueFromAction(VRControllerState& aControllerState,
82                                 const ControllerAction& aPressAction,
83                                 const ControllerAction& aTouchAction);
84   void GetTriggerValueFromAction(VRControllerState& aControllerState,
85                                  const ControllerAction& aAction);
86   void GetAxisValueFromAction(VRControllerState& aControllerState,
87                               const ControllerAction& aAction,
88                               bool aInvertAxis = false);
89   uint32_t mNumButtons;
90   uint32_t mNumAxes;
91 };
92 
93 }  // namespace gfx
94 }  // namespace mozilla
95 
96 #endif  // GFX_VR_SERVICE_OPENVRCONTROLLERMAPPER_H