Overte C++ Documentation
SpacemouseManager.h
1 // SpacemouseManager.h
2 // interface/src/devices
3 //
4 // Created by Marcel Verhagen on 09-06-15.
5 // Copyright 2015 High Fidelity, Inc.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 
11 #ifndef hifi_SpacemouseManager_h
12 #define hifi_SpacemouseManager_h
13 
14 #include <QObject>
15 #include <QLibrary>
16 #include <controllers/UserInputMapper.h>
17 #include <controllers/InputDevice.h>
18 #include <controllers/StandardControls.h>
19 
20 #include <plugins/InputPlugin.h>
21 
22 // the windows connexion rawinput
23 #ifdef Q_OS_WIN
24 
25 #include "../../../interface/external/3dconnexionclient/include/I3dMouseParams.h"
26 #include <QAbstractNativeEventFilter>
27 #include <QAbstractEventDispatcher>
28 #include <Winsock2.h>
29 #include <windows.h>
30 
31 // windows rawinput parameters
32 class MouseParameters : public I3dMouseParam {
33 public:
34  MouseParameters();
35 
36  // I3dmouseSensor interface
37  bool IsPanZoom() const;
38  bool IsRotate() const;
39  Speed GetSpeed() const;
40 
41  void SetPanZoom(bool isPanZoom);
42  void SetRotate(bool isRotate);
43  void SetSpeed(Speed speed);
44 
45  // I3dmouseNavigation interface
46  Navigation GetNavigationMode() const;
47  Pivot GetPivotMode() const;
48  PivotVisibility GetPivotVisibility() const;
49  bool IsLockHorizon() const;
50 
51  void SetLockHorizon(bool bOn);
52  void SetNavigationMode(Navigation navigation);
53  void SetPivotMode(Pivot pivot);
54  void SetPivotVisibility(PivotVisibility visibility);
55 
56  static bool Is3dmouseAttached();
57 
58 
59 
60 private:
61  MouseParameters(const MouseParameters&);
62  const MouseParameters& operator = (const MouseParameters&);
63 
64  Navigation fNavigation;
65  Pivot fPivot;
66  PivotVisibility fPivotVisibility;
67  bool fIsLockHorizon;
68 
69  bool fIsPanZoom;
70  bool fIsRotate;
71  Speed fSpeed;
72 };
73 
74 class SpacemouseManager : public InputPlugin, public QAbstractNativeEventFilter {
75 
76  Q_OBJECT
77 public:
78  bool isSupported() const override;
79  const QString getName() const override { return NAME; }
80  const QString getID() const override { return NAME; }
81 
82  bool activate() override;
83  void deactivate() override;
84 
85  void pluginFocusOutEvent() override;
86  void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
87 
88  bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override;
89 
90 private:
91  void Move3d(HANDLE device, std::vector<float>& motionData);
92  void On3dmouseKeyDown(HANDLE device, int virtualKeyCode);
93  void On3dmouseKeyUp(HANDLE device, int virtualKeyCode);
94  bool InitializeRawInput(HWND hwndTarget);
95 
96  bool RawInputEventFilter(void* msg, long* result);
97 
98  void OnRawInput(UINT nInputCode, HRAWINPUT hRawInput);
99  UINT GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader);
100  bool TranslateRawInputData(UINT nInputCode, PRAWINPUT pRawInput);
101  void On3dmouseInput();
102 
103  class TInputData {
104  public:
105  TInputData() : fAxes(6) {}
106 
107  bool IsZero() {
108  return (0.0f == fAxes[0] && 0.0f == fAxes[1] && 0.0f == fAxes[2] &&
109  0.0f == fAxes[3] && 0.0f == fAxes[4] && 0.0f == fAxes[5]);
110  }
111 
112  int fTimeToLive; // For telling if the device was unplugged while sending data
113  bool fIsDirty;
114  std::vector<float> fAxes;
115 
116  };
117 
118  HWND fWindow;
119 
120  // Data cache to handle multiple rawinput devices
121  std::map< HANDLE, TInputData> fDevice2Data;
122  std::map< HANDLE, unsigned long> fDevice2Keystate;
123 
124  // 3dmouse parameters
125  MouseParameters f3dMouseParams; // Rotate, Pan Zoom etc.
126 
127  // use to calculate distance traveled since last event
128  DWORD fLast3dmouseInputTime;
129 
130  static const char* NAME;
131  friend class SpacemouseDevice;
132 };
133 
134 // the osx connexion api
135 #else
136 
137 #include <glm/glm.hpp>
138 #include "../../../interface/external/3dconnexionclient/include/ConnexionClientAPI.h"
139 
140 class SpacemouseManager : public QObject {
141  Q_OBJECT
142 public:
143  void init();
144  void destroy();
145  bool Is3dmouseAttached();
146  public slots:
147  void toggleSpacemouse(bool shouldEnable);
148 };
149 
150 #endif // __APPLE__
151 
152 
153 // connnects to the userinputmapper
154 class SpacemouseDevice : public QObject, public controller::InputDevice {
155  Q_OBJECT
156 
157 public:
158  SpacemouseDevice();
159  enum PositionChannel {
160  TRANSLATE_X,
161  TRANSLATE_Y,
162  TRANSLATE_Z,
163  ROTATE_X,
164  ROTATE_Y,
165  ROTATE_Z,
166  };
167 
168  enum ButtonChannel {
169  BUTTON_1 = 1,
170  BUTTON_2 = 2,
171  BUTTON_3 = 3
172  };
173 
174  typedef std::unordered_set<int> ButtonPressedMap;
175  typedef std::map<int, float> AxisStateMap;
176 
177  float getButton(int channel) const;
178  float getAxis(int channel) const;
179 
180  controller::Input makeInput(SpacemouseDevice::PositionChannel axis) const;
181  controller::Input makeInput(SpacemouseDevice::ButtonChannel button) const;
182 
183  controller::Input::NamedPair makePair(SpacemouseDevice::PositionChannel axis, const QString& name) const;
184  controller::Input::NamedPair makePair(SpacemouseDevice::ButtonChannel button, const QString& name) const;
185 
186  virtual controller::Input::NamedVector getAvailableInputs() const override;
187  virtual QString getDefaultMappingConfig() const override;
188  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
189  virtual void focusOutEvent() override;
190 
191  glm::vec3 cc_position;
192  glm::vec3 cc_rotation;
193  int clientId;
194 
195  void setButton(int lastButtonState);
196  void handleAxisEvent();
197 };
198 
199 #endif // defined(hifi_SpacemouseManager_h)