Debian Codegraph @

engine/core/eventchannel/joystick/joystickmanager.h

raw
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 //
40 #include "loaders/native/input/controllermappingloader.h"
41 #include "savers/native/input/controllermappingsaver.h"
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
52 /** Joystick Manager manages all events related to Joysticks and Gamecontrollers
53 */
54 class JoystickManager:
55 public IJoystickController,
56 public IEventSource {
57 public:
58 /** Constructor.
59 */
60 JoystickManager();
61
62 /** Destructor
63 */
64 virtual ~JoystickManager();
65
66 /** Adds a joystick with the given device index.
67 */
68 Joystick* addJoystick(int32_t deviceIndex);
69
70 /** Return the joystick with the given instance id.
71 */
72 Joystick* getJoystick(int32_t instanceId);
73
74 /** Removes the given joystick, can be reused.
75 */
76 void removeJoystick(Joystick* joystick);
77
78 /** Return the number of joysticks / gamecontrollers.
79 */
80 uint8_t getJoystickCount() const;
81
82 /** Loads controller mappings from given file and if possible, it opens the related controllers.
83 */
84 void loadMapping(const std::string& file);
85
86 /** Saves controller mapping for given GUID in the specified file.
87 */
88 void saveMapping(const std::string guid, const std::string& file);
89
90 /** Saves all controller mappings that were used during the season.
91 */
92 void saveMappings(const std::string& file);
93
94 /** Return the controller mapping for given GUID as string.
95 */
96 std::string getStringMapping(const std::string& guid);
97
98 /** Sets controller mapping from string and adds or updates the related controllers.
99 */
100 void setStringMapping(const std::string& mapping);
101
102 // Implementation from IJoystickController.
103 void addJoystickListener(IJoystickListener* listener);
104 void addJoystickListenerFront(IJoystickListener* listener);
105 void removeJoystickListener(IJoystickListener* listener);
106
107 /** Creates and process joystick events.
108 */
109 void processJoystickEvent(SDL_Event event);
110
111 /** Creates and process gamecontroller events.
112 */
113 void processControllerEvent(SDL_Event event);
114
115 /** Dispatches joystick / controller events.
116 */
117 void dispatchJoystickEvent(JoystickEvent& evt);
118
119 // Implementation from IEventSource.
120 EventSourceType getEventSourceType();
121
122 private:
123 /** Return GUID for given device index as string.
124 */
125 std::string getGuidString(int32_t deviceIndex);
126
127 /** Converts the int16 in -1.0 to 1.0 range.
128 */
129 float convertRange(int16_t value);
130
131 /** Adds GUID from controller. Needed for saving.
132 */
133 void addControllerGuid(Joystick* joystick);
134
135 /** Removes / decal controller GUID.
136 */
137 void removeControllerGuid(Joystick* joystick);
138
139 //! Loader for gamecontroller mapping.
140 ControllerMappingLoader m_mappingLoader;
141
142 //! Saver for gamecontroller mapping.
143 ControllerMappingSaver m_mappingSaver;
144
145 //! All active / connected Joysticks.
146 std::vector<Joystick*> m_activeJoysticks;
147
148 //! All "known" Joysticks. Useful if a user reconnect a Joystick.
149 std::vector<Joystick*> m_joysticks;
150
151 //! Map to hold the relation between Joystick InstanceId and JoystickId
152 std::map<int32_t, uint32_t> m_joystickIndices;
153
154 //! Each sort of gamepad have a GUID from SDL. Indicates the number of gamepads with given GUID are connected.
155 std::map<std::string, uint8_t> m_gamepadGuids;
156
157 //! The Joystick listeners.
158 std::deque<IJoystickListener*> m_joystickListeners;
159 };
160 } //FIFE
161
162 #endif