Overte C++ Documentation
OscPlugin.h
1 //
2 // OscPlugin.h
3 //
4 // Created by Anthony Thibault on 2019/8/24
5 // Copyright 2019 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_OscPlugin_h
12 #define hifi_OscPlugin_h
13 
14 #include <controllers/InputDevice.h>
15 #include <controllers/StandardControls.h>
16 #include <plugins/InputPlugin.h>
17 
18 #include "lo/lo.h"
19 
20 // OSC (Open Sound Control) input plugin.
21 class OscPlugin : public InputPlugin {
22  Q_OBJECT
23 public:
24 
25  // Plugin functions
26  virtual void init() override;
27  virtual bool isSupported() const override;
28  virtual const QString getName() const override { return NAME; }
29  const QString getID() const override { return OSC_ID_STRING; }
30 
31  virtual bool activate() override;
32  virtual void deactivate() override;
33 
34  virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); }
35  virtual void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
36 
37  virtual void saveSettings() const override;
38  virtual void loadSettings() override;
39 
40  bool startServer();
41  void stopServer();
42  void restartServer();
43 
44 protected:
45 
46  class InputDevice : public controller::InputDevice {
47  public:
48  friend class OscPlugin;
49 
50  InputDevice() : controller::InputDevice("OSC") {}
51 
52  // Device functions
53  virtual controller::Input::NamedVector getAvailableInputs() const override;
54  virtual QString getDefaultMappingConfig() const override;
55  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
56  virtual void focusOutEvent() override {};
57 
58  void clearState();
59  void setContainer(OscPlugin* container) { _container = container; }
60 
61  OscPlugin* _container { nullptr };
62  };
63 
64  std::shared_ptr<InputDevice> _inputDevice;
65 
66  static const char* NAME;
67  static const char* OSC_ID_STRING;
68 
69  bool _enabled { false };
70  mutable bool _initialized { false };
71 
72  lo_server_thread _oscServerThread { nullptr };
73 
74 public:
75  bool _debug { false };
76  enum Constants { DEFAULT_OSC_SERVER_PORT = 7700 };
77  int _serverPort { DEFAULT_OSC_SERVER_PORT };
78  controller::InputCalibrationData _lastInputCalibrationData;
79 
80  std::vector<float> _blendshapeValues;
81  std::vector<bool> _blendshapeValidFlags;
82  glm::quat _headRot;
83  bool _headRotValid { false };
84  glm::vec3 _headTransTarget;
85  glm::vec3 _headTransSmoothed;
86  bool _headTransValid { false };
87  glm::quat _eyeLeftRot;
88  bool _eyeLeftRotValid { false };
89  glm::quat _eyeRightRot;
90  bool _eyeRightRotValid { false };
91  std::mutex _dataMutex;
92 };
93 
94 #endif // hifi_OscPlugin_h
95