VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
ControllerManager.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CONTROLLERMANAGER_H
18 #define CONTROLLERMANAGER_H
19 
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include "ControllerFactory.h"
25 #include "VirtualController.h"
26 
33 public:
39  public:
40  virtual ~DeviceConnectionListener() { }
45  virtual void onDeviceConnectionChanged(int deviceId, bool isConnected) = 0;
46  };
47 
52  typedef std::function<void(int, bool)> DeviceConnectionCallback;
53 
60  public:
61  virtual void onDeviceConnectionChanged(int deviceId, bool isConnected) override;
62 
69  return new StaticDeviceConnectionListener(callback);
70  }
71  private:
73 
74  DeviceConnectionCallback mDeviceConnectionCallback;
75  };
76 
85  template <class T = VirtualController>
86  T* getController(int deviceId, const ControllerFactory* factory = nullptr) const {
87  std::unique_ptr<VirtualController>& controller = mControllers[deviceId];
88  initControllerIfNeeded(deviceId, controller, factory);
89  return dynamic_cast<T*>(controller.get());
90  }
91 
96  template <class T>
97  bool isControllerOfType(VirtualController* controller) const {
98  return dynamic_cast<T*>(controller) != nullptr;
99  }
100 
106  template <class T>
107  bool isControllerOfType(int deviceId) const {
108  return isControllerOfType<T>(mControllers[deviceId].get());
109  }
110 
114  void reset();
115 
120  void registerDeviceConnectionListener(DeviceConnectionListener* listener);
125  void unregisterDeviceConnectionListener(DeviceConnectionListener* listener);
126 
133  bool isControllerConnected(int deviceId) const {
134  return mControllerIds.find(deviceId) != mControllerIds.end();
135  }
136 
140  const std::set<int>& getConnectedControllers() const {
141  return mControllerIds;
142  }
143 
151  void onControllerDisconnect(int deviceId);
152 
156  static ControllerManager* getInstance();
157 
158 private:
159  mutable std::map<int, std::unique_ptr<VirtualController>> mControllers;
160  mutable std::set<int> mControllerIds;
161  std::set<DeviceConnectionListener*> mDeviceConnectionListeners;
162 
163  void notifyDeviceConnectionChanged(int deviceId, bool isConnected) const;
164  void initControllerIfNeeded(int deviceId, std::unique_ptr<VirtualController>& controller,
165  const ControllerFactory* factory) const;
166 
167  static ControllerManager* sInstance;
168 };
169 
170 #endif // CONTROLLERMANAGER_H
virtual void onDeviceConnectionChanged(int deviceId, bool isConnected) override
Callback function invoked when the device identified by deviceId has connected or disconnected...
bool isControllerOfType(int deviceId) const
Returns whether or not the VirtualController specified by deviceId is of the template type...
Definition: ControllerManager.h:107
const std::set< int > & getConnectedControllers() const
Returns the list of all VirtualController deviceIds that are currently connected. ...
Definition: ControllerManager.h:140
Helper factory class to generate a new DeviceConnectionListerer instance for a static callback functi...
Definition: ControllerManager.h:59
Software represention and state capable of describing almost all physical controllers.
Definition: VirtualController.h:36
void unregisterDeviceConnectionListener(DeviceConnectionListener *listener)
Unregisters a listener to no longer be notified of device connection change events.
Base interface for constructing virtual controllers from a deviceId.
Definition: ControllerFactory.h:34
void reset()
Resets the state (i.e. disconnects) of all connected controllers.
std::function< void(int, bool)> DeviceConnectionCallback
Callback indicating whether the device identified by deviceId has either connected or disconnected...
Definition: ControllerManager.h:52
Interface for listening to changes in device connection statuses.
Definition: ControllerManager.h:38
bool isControllerConnected(int deviceId) const
Returns whether or not the VirtualController specified by deviceId is currently known to be connected...
Definition: ControllerManager.h:133
void registerDeviceConnectionListener(DeviceConnectionListener *listener)
Registers a new listener to be notified of device connection change events.
Maintains global list of connected controllers and notifies registered listeners of changes in these ...
Definition: ControllerManager.h:32
void onControllerDisconnect(int deviceId)
Notifies the ControllerManager that the VirtualController specified by deviceId has disconnected...
T * getController(int deviceId, const ControllerFactory *factory=nullptr) const
Returns the VirtualController corresponding to the specified deviceId.
Definition: ControllerManager.h:86
static ControllerManager * getInstance()
Returns the global singleton instance of the ControllerManager.
bool isControllerOfType(VirtualController *controller) const
Returns whether or not the specified VirtualController is of the template type.
Definition: ControllerManager.h:97
virtual void onDeviceConnectionChanged(int deviceId, bool isConnected)=0
Callback function invoked when the device identified by deviceId has connected or disconnected...