Debian Codegraph @

source/code/gamecontroller.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2016 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 http://www.blockattack.net
21 ===========================================================================
22 */
23
24 #include "gamecontroller.h"
25 #include "SDL_gamecontroller.h"
26 #include "platform_folders.h"
27 #include "common.h"
28 #include <iostream>
29 #include <map>
30 #include <SDL2/SDL_joystick.h>
31
32 static bool verbose = false;
33
34 struct ControllerStatus {
35 std::map<int, bool> AxisInDeadZone;
36 int player = 1;
37 };
38
39 static std::map<SDL_JoystickID, ControllerStatus> controllerStatusMap;
40 static std::map<std::string, int> gamecontrollers_assigned;
41 static std::vector<std::string> supportedControllers;
42 static std::vector<SDL_GameController*> controllersOpened;
43
44
45 void GameControllerSetVerbose(bool value) {
46 verbose = value;
47 }
48
49 static const char* GameControllerGetName(SDL_GameController* gamecontroller) {
50 const char* result = SDL_GameControllerName(gamecontroller);
51 if (!result) {
52 result = "Unnamed";
53 }
54 return result;
55 }
56
57 static std::string GetGuidAsHex(const SDL_JoystickGUID& guid) {
58 std::string ret;
59 char buffer[3];
60 for (size_t j = 0; j < sizeof(guid.data); j++) {
61 snprintf(buffer, sizeof(buffer), "%02X", guid.data[j]);
62 ret += buffer;
63 }
64 return ret;
65 }
66
67
68 static int GetNextPlayerByGui(const SDL_JoystickGUID& guid) {
69 Config::getInstance()->setDefault("gc_AllToOnePlayer", "0");
70 int fixedPlayer = Config::getInstance()->getInt("gc_AllToOnePlayer");
71 if (fixedPlayer > 0 && fixedPlayer <= 2) {
72 return fixedPlayer;
73 }
74 std::string guidAsHex= GetGuidAsHex(guid);
75 std::string configName = "gc_assign_"+guidAsHex;
76 Config::getInstance()->setDefault(configName, "1");
77 int player = Config::getInstance()->getInt(configName) + gamecontrollers_assigned[guidAsHex];
78 gamecontrollers_assigned[guidAsHex]++; //Next controller with same guid should be assigned to different player.
79
80 if (player%2==0) {
81 return 2; //Even number means player 2
82 }
83 return 1;
84 }
85
86 void UnInitGameControllers() {
87 if (controllersOpened.empty()) {
88 return;
89 }
90 for (SDL_GameController* t : controllersOpened) {
91 SDL_GameControllerClose(t);
92 }
93 controllersOpened.clear();
94 controllerStatusMap.clear();
95 gamecontrollers_assigned.clear();
96 supportedControllers.clear();
97 }
98
99 void InitGameControllers() {
100 std::string configFile = sago::getConfigHome()+"/blockattack/gamecontrollerdb.txt";
101 int errorCode = SDL_GameControllerAddMappingsFromFile(configFile.c_str());
102 if (errorCode == -1 && verbose) {
103 std::cerr << "Could not load mapping file: " << configFile << "\n";
104 }
105 if (verbose) {
106 std::cout << "Number of Game controllers: " << SDL_NumJoysticks() << "\n";
107 }
108 SDL_GameController* controller = nullptr;
109 for (int i = 0; i < SDL_NumJoysticks(); ++i) {
110 if (SDL_IsGameController(i)) {
111 controller = SDL_GameControllerOpen(i);
112 SDL_Joystick* j = SDL_GameControllerGetJoystick(controller);
113 SDL_JoystickID instanceId = SDL_JoystickInstanceID(j);
114 SDL_JoystickGUID guid = SDL_JoystickGetGUID(j);
115 int assingToPlayer = GetNextPlayerByGui(guid);
116 controllerStatusMap[instanceId].player = assingToPlayer;
117 supportedControllers.push_back(GameControllerGetName(controller));
118 controllersOpened.push_back(controller);
119 if (verbose) {
120 std::cout << "Supported game controller detected: " << GameControllerGetName(controller) << ", mapping: " << SDL_GameControllerMapping(controller) << "\n";
121 std::cout << "Assigned to player: " << controllerStatusMap[instanceId].player << "\n";
122 }
123 }
124 }
125 }
126
127 const std::vector<std::string>& GetSupportedControllerNames() {
128 return supportedControllers;
129 }
130
131 void GameControllerCheckDeadZone(const SDL_Event& event) {
132 if (event.type != SDL_CONTROLLERAXISMOTION) {
133 return; //assert?
134 }
135 int value = event.caxis.value;
136 if (value > -deadZoneLimit && value < deadZoneLimit) {
137 controllerStatusMap[event.caxis.which].AxisInDeadZone[event.caxis.axis] = true;
138 }
139 }
140
141 bool GameControllerIsInDeadZone(SDL_JoystickID id, int axis) {
142 return controllerStatusMap[id].AxisInDeadZone[axis];
143 }
144
145 void GameControllerSetIsInDeadZone(SDL_JoystickID id, int axis, bool value) {
146 controllerStatusMap[id].AxisInDeadZone[axis] = value;
147 }
148
149 static bool GameControllerExtSkipThisPlayer(int playerNumber, const SDL_Event& event) {
150
151 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
152 ControllerStatus& cs = controllerStatusMap[event.cbutton.which];
153 if (cs.player == playerNumber) {
154 return false;
155 }
156 }
157 if (event.type == SDL_CONTROLLERAXISMOTION ) {
158 ControllerStatus& cs = controllerStatusMap[event.caxis.which];
159 if (cs.player == playerNumber) {
160 return false;
161 }
162 }
163 return true;
164 }
165
166
167 bool GameControllerIsConnectionEvent(const SDL_Event& event) {
168 if ( event.type == SDL_CONTROLLERDEVICEADDED
169 || event.type == SDL_CONTROLLERDEVICEREMOVED
170 || event.type == SDL_CONTROLLERDEVICEREMAPPED ) {
171 return true;
172 }
173 return false;
174 }
175
176
177
178 bool GameControllerExtIsPlayerDownEvent(int playerNumber, const SDL_Event& event) {
179 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
180 return false;
181 }
182 return GameControllerIsDownEvent(event);
183 }
184
185 bool GameControllerIsControllerDirectionEvent(const SDL_Event& event, SDL_GameControllerButton dpad_direction, SDL_GameControllerAxis axis, float axis_mod = 1.0f) {
186 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
187 if (event.cbutton.button == dpad_direction ) {
188 return true;
189 }
190 }
191 if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == axis ) {
192 const SDL_ControllerAxisEvent& a = event.caxis;
193 GameControllerCheckDeadZone(event);
194 if (GameControllerIsInDeadZone(a.which, a.axis)) {
195 if (event.caxis.value * axis_mod > deadZoneLimit) {
196 GameControllerSetIsInDeadZone(a.which,a.axis,false);
197 return true;
198 }
199 }
200 }
201 return false;
202 }
203
204 bool GameControllerIsDownEvent(const SDL_Event& event) {
205 return GameControllerIsControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_AXIS_LEFTY);
206 }
207
208 bool GameControllerIsUpEvent(const SDL_Event& event) {
209 return GameControllerIsControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_AXIS_LEFTY, -1.0f);
210 }
211
212 bool GameControllerIsLeftEvent(const SDL_Event& event) {
213 return GameControllerIsControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_AXIS_LEFTX, -1.0f);
214 }
215
216 bool GameControllerIsRightEvent(const SDL_Event& event) {
217 return GameControllerIsControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_AXIS_LEFTX);
218 }
219
220 bool GameControllerExtIsPlayerUpEvent(int playerNumber, const SDL_Event& event) {
221 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
222 return false;
223 }
224 return GameControllerIsUpEvent(event);
225 }
226
227 bool GameControllerExtIsPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
228 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
229 return false;
230 }
231 return GameControllerIsLeftEvent(event);
232 }
233
234 bool GameControllerExtIsPlayerRightEvent(int playerNumber, const SDL_Event& event) {
235 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
236 return false;
237 }
238 return GameControllerIsRightEvent(event);
239 }
240
241 bool GameControllerExtIsPlayerSwitchEvent(int playerNumber, const SDL_Event& event) {
242 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
243 return false;
244 }
245 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
246 if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
247 return true;
248 }
249 }
250 return false;
251 }
252
253 bool GameControllerExtIsPlayerPushEvent(int playerNumber, const SDL_Event& event) {
254 if (GameControllerExtSkipThisPlayer(playerNumber, event)) {
255 return false;
256 }
257 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
258 if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
259 return true;
260 }
261 }
262 if (event.type == SDL_CONTROLLERAXISMOTION && (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT ) ) {
263 GameControllerCheckDeadZone(event);
264 const SDL_ControllerAxisEvent& a = event.caxis;
265 if (GameControllerIsInDeadZone(a.which, a.axis)) {
266 if (event.caxis.value > deadZoneLimit) {
267 GameControllerSetIsInDeadZone(a.which,a.axis,false);
268 return true;
269 }
270 }
271 }
272 return false;
273 }