FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
joystickmanager.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_EVENTCHANNEL_JOYSTICKMANAGER_H
23 #define FIFE_EVENTCHANNEL_JOYSTICKMANAGER_H
24 
25 // Standard C++ library includes
26 //
27 #include <vector>
28 #include <deque>
29 #include <map>
30 #include <list>
31 
32 // 3rd party library includes
33 //
34 
35 // FIFE includes
36 // These includes are split up in two parts, separated by one empty line
37 // First block: files included from the FIFE root src directory
38 // Second block: files included from the same folder
39 //
42 #include "util/base/fife_stdint.h"
43 
44 #include "ijoystickcontroller.h"
45 #include "ijoysticklistener.h"
46 #include "joystickevent.h"
47 #include "joystick.h"
48 
49 
50 namespace FIFE {
51 
55  public IJoystickController,
56  public IEventSource {
57  public:
61 
64  virtual ~JoystickManager();
65 
68  Joystick* addJoystick(int32_t deviceIndex);
69 
72  Joystick* getJoystick(int32_t instanceId);
73 
76  void removeJoystick(Joystick* joystick);
77 
80  uint8_t getJoystickCount() const;
81 
84  void loadMapping(const std::string& file);
85 
88  void saveMapping(const std::string guid, const std::string& file);
89 
92  void saveMappings(const std::string& file);
93 
96  std::string getStringMapping(const std::string& guid);
97 
100  void setStringMapping(const std::string& mapping);
101 
102  // Implementation from IJoystickController.
103  void addJoystickListener(IJoystickListener* listener);
106 
109  void processJoystickEvent(SDL_Event event);
110 
113  void processControllerEvent(SDL_Event event);
114 
118 
119  // Implementation from IEventSource.
121 
122  private:
125  std::string getGuidString(int32_t deviceIndex);
126 
129  float convertRange(int16_t value);
130 
133  void addControllerGuid(Joystick* joystick);
134 
137  void removeControllerGuid(Joystick* joystick);
138 
141 
144 
146  std::vector<Joystick*> m_activeJoysticks;
147 
149  std::vector<Joystick*> m_joysticks;
150 
152  std::map<int32_t, uint32_t> m_joystickIndices;
153 
155  std::map<std::string, uint8_t> m_gamepadGuids;
156 
158  std::deque<IJoystickListener*> m_joystickListeners;
159  };
160 } //FIFE
161 
162 #endif
void addJoystickListener(IJoystickListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
JoystickManager()
Constructor.
virtual ~JoystickManager()
Destructor.
void removeJoystick(Joystick *joystick)
Removes the given joystick, can be reused.
void processJoystickEvent(SDL_Event event)
Creates and process joystick events.
ControllerMappingLoader m_mappingLoader
Loader for gamecontroller mapping.
uint8_t getJoystickCount() const
Return the number of joysticks / gamecontrollers.
float convertRange(int16_t value)
Converts the int16 in -1.0 to 1.0 range.
void removeControllerGuid(Joystick *joystick)
Removes / decal controller GUID.
std::map< std::string, uint8_t > m_gamepadGuids
Each sort of gamepad have a GUID from SDL. Indicates the number of gamepads with given GUID are conne...
void setStringMapping(const std::string &mapping)
Sets controller mapping from string and adds or updates the related controllers.
std::vector< Joystick * > m_joysticks
All "known" Joysticks. Useful if a user reconnect a Joystick.
Listener of joystick events.
std::vector< Joystick * > m_activeJoysticks
All active / connected Joysticks.
Represents a Joystick and if available the Gamecontroller.
Definition: joystick.h:39
Controller provides a way to receive events from the system Using this interface, clients can subscri...
unsigned char uint8_t
Definition: core.h:38
void addControllerGuid(Joystick *joystick)
Adds GUID from controller.
std::map< int32_t, uint32_t > m_joystickIndices
Map to hold the relation between Joystick InstanceId and JoystickId.
void dispatchJoystickEvent(JoystickEvent &evt)
Dispatches joystick / controller events.
ControllerMappingSaver m_mappingSaver
Saver for gamecontroller mapping.
Joystick Manager manages all events related to Joysticks and Gamecontrollers.
void saveMapping(const std::string guid, const std::string &file)
Saves controller mapping for given GUID in the specified file.
void processControllerEvent(SDL_Event event)
Creates and process gamecontroller events.
std::string getGuidString(int32_t deviceIndex)
Return GUID for given device index as string.
Joystick * getJoystick(int32_t instanceId)
Return the joystick with the given instance id.
void loadMapping(const std::string &file)
Loads controller mappings from given file and if possible, it opens the related controllers.
void addJoystickListenerFront(IJoystickListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
Class for Joystick events.
Definition: joystickevent.h:45
std::string getStringMapping(const std::string &guid)
Return the controller mapping for given GUID as string.
void removeJoystickListener(IJoystickListener *listener)
Removes an added listener from the controller.
Joystick * addJoystick(int32_t deviceIndex)
Adds a joystick with the given device index.
void saveMappings(const std::string &file)
Saves all controller mappings that were used during the season.
EventSourceType
Types for different event sources.
Representation of event source (a thing sending events)
Definition: ieventsource.h:42
std::deque< IJoystickListener * > m_joystickListeners
The Joystick listeners.
EventSourceType getEventSourceType()
Gets the source type of this event.