NAP
sdlinputservice.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <inputservice.h>
9 #include <unordered_map>
10 #include <SDL_gamecontroller.h>
11 #include <SDL_joystick.h>
12 
13 namespace nap
14 {
27  class NAPAPI SDLInputService : public Service
28  {
29  RTTI_ENABLE(Service)
30  public:
31 
37  class NAPAPI SDLController final
38  {
39  public:
40  SDLController(int deviceID);
41  ~SDLController() { close(); }
42 
43  int mDeviceID = -1;
44  int mInstanceID = -1;
45  void* mController = nullptr;
46  bool mIsJoystick = false;
47 
48  private:
49  void close();
50  };
51 
52  // Default constructor
53  SDLInputService(ServiceConfiguration* configuration);
54 
59  virtual void getDependentServices(std::vector<rtti::TypeInfo>& dependencies) override;
60 
65  virtual bool init(utility::ErrorState& error) override;
66 
71  virtual void shutdown() override;
72 
79  bool isConnected(int controllerNumber) const;
80 
86  bool controllerInstanceExists(int instance) const;
87 
94  int getControllerNumber(int instance) const;
95 
102  bool isGameController(int instance) const;
103 
104  private:
105  std::unordered_map<int, std::unique_ptr<SDLController>> mSystemControllers;
106  nap::InputService* mInputService = nullptr;
107 
108  // Adds a new controller
109  void addController(int deviceID);
110 
111  // Removes a controller if registered
112  void removeController(int deviceID);
113 
118  void onConnectionChanged(const ControllerConnectionEvent& event);
119 
120  // Slot that is called when a controller connection changes
121  NSLOT(mConnectionChanged, const ControllerConnectionEvent&, onConnectionChanged);
122  };
123 }
nap::SDLInputService::SDLController::~SDLController
~SDLController()
Definition: sdlinputservice.h:41
nap::SDLInputService
Definition: sdlinputservice.h:27
nap::utility::ErrorState
Definition: errorstate.h:19
nap::SDLInputService::SDLController
Definition: sdlinputservice.h:37
nap::ServiceConfiguration
Definition: service.h:28
nap::InputService
Definition: inputservice.h:32
nap::Service
Definition: templateservice.h:8
nap
Definition: templateapp.h:17
nap::ControllerConnectionEvent
Definition: inputevent.h:403