FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
joystick.cpp
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 // Standard C++ library includes
23 #include <iostream>
24 
25 // 3rd party library includes
26 #include <SDL.h>
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/base/exception.h"
33 #include "util/log/logger.h"
34 #include "util/math/fife_math.h"
35 
36 #include "joystick.h"
37 
38 namespace FIFE {
39  static Logger _log(LM_EVTCHANNEL);
40 
41  Joystick::Joystick(int32_t joystickId, int32_t deviceIndex):
42  m_joystickHandle(NULL),
43  m_controllerHandle(NULL),
44  m_instanceId(-1),
45  m_joystickId(joystickId),
46  m_deviceIndex(deviceIndex),
47  m_guidStr(""),
48  m_name("") {
49 
50  }
51 
53  close();
54  }
55 
56  int32_t Joystick::getInstanceId() const {
57  return static_cast<int32_t>(m_instanceId);
58  }
59 
60  int32_t Joystick::getJoystickId() const {
61  return m_joystickId;
62  }
63 
64  void Joystick::setDeviceIndex(int32_t deviceIndex) {
65  m_deviceIndex = deviceIndex;
66  }
67 
68  int32_t Joystick::getDeviceIndex() const {
69  return m_deviceIndex;
70  }
71 
72  const std::string& Joystick::getGuid() {
73  return m_guidStr;
74  }
75 
76  const std::string& Joystick::getName() {
77  return m_name;
78  }
79 
80  void Joystick::open() {
81  if (m_joystickHandle) {
82  close();
83  }
84 
85  m_joystickHandle = SDL_JoystickOpen(m_deviceIndex);
86  if (m_joystickHandle) {
87  m_instanceId = SDL_JoystickInstanceID(m_joystickHandle);
88 
89  char tmp[33];
90  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(m_deviceIndex);
91  SDL_JoystickGetGUIDString(guid, tmp, sizeof(tmp));
92  m_guidStr = std::string(tmp);
93 
95  const char* name = SDL_JoystickNameForIndex(m_deviceIndex);
96  if (isController() && !name) {
97  name = SDL_GameControllerNameForIndex(m_deviceIndex);
98  }
99  m_name = std::string(name);
100  } else {
101  throw SDLException(SDL_GetError());
102  }
103  }
104 
106  closeController();
107  if (m_joystickHandle) {
108  SDL_JoystickClose(m_joystickHandle);
109  m_joystickHandle = NULL;
110  }
111  m_instanceId = -1;
112  m_deviceIndex = -1;
113  }
114 
115  bool Joystick::isConnected() const {
116  return m_joystickHandle && SDL_JoystickGetAttached(m_joystickHandle);
117  }
118 
119  bool Joystick::isController() const {
120  return m_controllerHandle != NULL;
121  }
122 
124  closeController();
125  if (!SDL_IsGameController(m_deviceIndex)) {
126  return;
127  }
128 
129  m_controllerHandle = SDL_GameControllerOpen(m_deviceIndex);
130  }
131 
133  if (m_controllerHandle) {
134  SDL_GameControllerClose(m_controllerHandle);
135  m_controllerHandle = NULL;
136  }
137  }
138 
140  uint8_t number = 0;
141  if (isConnected()) {
142  number = SDL_JoystickNumAxes(m_joystickHandle);
143  }
144  return number;
145  }
146 
148  uint8_t number = 0;
149  if (isConnected()) {
150  number = SDL_JoystickNumButtons(m_joystickHandle);
151  }
152  return number;
153  }
154 
156  uint8_t number = 0;
157  if (isConnected()) {
158  number = SDL_JoystickNumHats(m_joystickHandle);
159  }
160  return number;
161  }
162 
163  float Joystick::getAxisValue(int8_t axis) const {
164  if (axis < 0 || !isConnected()) {
165  return 0;
166  }
167 
168  if (!isController()) {
169  return convertRange(SDL_JoystickGetAxis(m_joystickHandle, axis));
170  }
171 
172  SDL_GameControllerAxis sdlAxis = static_cast<SDL_GameControllerAxis>(axis);
173  return convertRange(SDL_GameControllerGetAxis(m_controllerHandle, sdlAxis));
174  }
175 
176  int8_t Joystick::getHatValue(int8_t hat) const {
177  if (hat < 0 || !isConnected()) {
178  return HAT_INVALID;
179  }
180  return SDL_JoystickGetHat(m_joystickHandle, hat);
181  }
182 
183  bool Joystick::isButtonPressed(int8_t button) const {
184  if (button < 0 || !isConnected()) {
185  return false;
186  }
187  if (!isController()) {
188  if (SDL_JoystickGetButton(m_joystickHandle, button) == 1) {
189  return true;
190  }
191  return false;
192  }
193 
194  SDL_GameControllerButton sdlButton = static_cast<SDL_GameControllerButton>(button);
195  if (SDL_GameControllerGetButton(m_controllerHandle, sdlButton) == 1) {
196  return true;
197  }
198  return false;
199  }
200 
201  float Joystick::convertRange(int16_t value) const {
202  float range = static_cast<float>(value) / 32768.0f;
203  if (Mathf::FAbs(range) < 0.01f) {
204  return 0.0f;
205  }
206  if (range < -0.99f) {
207  return -1.0f;
208  } else if (range > 0.99f) {
209  return 1.0f;
210  }
211  return range;
212  }
213 }
bool isConnected() const
Indicates if the joystick / gamecontroller is connected.
Definition: joystick.cpp:115
int32_t m_joystickId
Our joystick id.
Definition: joystick.h:185
void closeController()
Closes / deactivates the gamecontroller.
Definition: joystick.cpp:132
int32_t m_deviceIndex
SDLs device index.
Definition: joystick.h:187
uint8_t getNumberOfHats() const
Return the number of hats.
Definition: joystick.cpp:155
SDL_JoystickID m_instanceId
SDLs joystick id (different from device index)
Definition: joystick.h:183
bool isController() const
Indicates if this a controller.
Definition: joystick.cpp:119
void close()
Closes / deactivates the joystick.
Definition: joystick.cpp:105
static Logger _log(LM_AUDIO)
std::string m_name
The name of the joystick / controller.
Definition: joystick.h:191
void setDeviceIndex(int32_t deviceIndex)
Sets the device index of the joystick.
Definition: joystick.cpp:64
uint8_t getNumberOfAxes() const
Return the number of axes.
Definition: joystick.cpp:139
unsigned char uint8_t
Definition: core.h:38
bool isButtonPressed(int8_t button) const
Return the current value for given axis.
Definition: joystick.cpp:183
const std::string & getGuid()
Return the GUID of the joystick / gamecontroller class as string.
Definition: joystick.cpp:72
int32_t getJoystickId() const
Sets the instance id of the joystick.
Definition: joystick.cpp:60
Joystick(int32_t joystickId, int32_t deviceIndex)
Constructor.
Definition: joystick.cpp:41
float convertRange(int16_t value) const
Converts the int16 in -1.0 to 1.0 range.
Definition: joystick.cpp:201
int32_t getDeviceIndex() const
Return the device index of the joystick.
Definition: joystick.cpp:68
void openController()
Opens / activates the gamecontroller, only possible with valid GUID mapping.
Definition: joystick.cpp:123
~Joystick()
Destructor.
Definition: joystick.cpp:52
uint8_t getNumberOfButtons() const
Return the number of buttons.
Definition: joystick.cpp:147
SDL_GameController * m_controllerHandle
SDLs controller handle.
Definition: joystick.h:181
float getAxisValue(int8_t axis) const
Return the current value for given axis.
Definition: joystick.cpp:163
const std::string & getName()
Return the name of the joystick.
Definition: joystick.cpp:76
static T FAbs(T _val)
Definition: fife_math.h:227
int32_t getInstanceId() const
Return the instance id of the joystick.
Definition: joystick.cpp:56
SDL_Joystick * m_joystickHandle
SDLs joystick handle.
Definition: joystick.h:179
void open()
Opens / activates the joystick and sets values.
Definition: joystick.cpp:80
std::string m_guidStr
The GUID as string.
Definition: joystick.h:189
int8_t getHatValue(int8_t hat) const
Return the hat value (see Hat positions), for given hat index.
Definition: joystick.cpp:176