4 * This file is part of SDLGamepad.
6 * SDLGamepad 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 3 of the License, or
9 * (at your option) any later version.
11 * SDLGamepad 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.
16 * You should have received a copy of the GNU General Public License
17 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
20 * mail.nalla@gmail.com
26 #include "sdlgamepad_global.h"
31 * The Axis range that is treated as null.
33 * This is a sort of callibration mechanism for the physical gamepad.
34 * SDL axis values greater than -NULL_RANGE and smaller than +NULL_RANGE
35 * will be treated as null.
37 #define NULL_RANGE 2800
40 * The default tick rate of refreshing the SDL info.
42 * This is the default ms value in the thread method to sleep. If you
43 * dont set a sleep rate you processor will have a much higher load!
45 * @see SDLGamepad::setTickRate()
52 * This enumeration can be used to have a more readable code for
53 * dealing with axes numbers. Up to 10 axes are supported. That should
54 * be more than enough for every gamepad out there.
71 * This enumeration is uesed internaly in the singal for the buttons.
72 * Be aware that you need to register this enumeration with
73 * qRegisterMetaType when you want to use the signal buttonState with
76 * @see SDLGamepad::buttonState()
102 * A typedef for qRegisterMetaType().
104 * This typedef is used to int the signal axesValues. You need this if
105 * you want to use the signal with queued connections, because you
106 * can not register QList<qint16> directly.
108 * @see SDLGamepad::axisValues()
110 typedef QList
<qint16
> QListInt16
;
112 class SDLGamepadPrivate
;
115 * A class for communication with a sdl gamepad.
117 * This class inherts QThread. The run method will look for new button
118 * states via SDL function calls and emit signals for every button if
119 * the state of the button has changed. It will also emit signals with
120 * the current axes values. The default sleep after this two tasks is
123 * @author Manuel Blanquett (mail.nalla@gmail.com)
127 class SDLGAMEPADSHARED_EXPORT SDLGamepad
: public QThread
{
135 * Some private variables will be initzialized here and the tick
136 * rate will be set to MIN_RATE.
141 * Class deconstructor.
143 * The SDL gamepad will be cracefully closed. After that the SDL
144 * system will be terminated.
149 * Implemented virtual run method from QThread.
151 * The base of operation so to speak. Very abstract this method
152 * does the following:
153 * - refresh SDL information
160 * Getter method for the axis count.
162 * This class member retunrs the actual number of axes present
163 * on the currently opened gamepad. If no gamepad is present after
164 * setting a new one with setGamepad() the value will be -1. This
165 * is also the default value.
170 * Getter method for the button count.
172 * This class member retunrs the actual number of buttons present
173 * on the currently opened gamepad. If no gamepad is present after
174 * setting a new one with setGamepad() the value will be -1. This
175 * is also the default value.
182 * Init the SDL system and set up gamepad.
184 * You have to connect to the gamepads signal if you want to
185 * receive the gamepads present in the SDL system.
188 * @return True if the initzialisation was successfull.
193 * Exiting the thread.
195 * This is a overwritten method from QThread to gracefully end the
196 * execution of this thread. You should connect the QT App's
197 * aboutToQuit signal with this.
202 * Change the default tickrate.
204 * You can change the tickrate at any time. So you can change the
205 * thread sleep dynamicly accordingly to your application's state.
207 * @param ms The new tickrate in milliseconds.
209 void setTickRate(qint16 ms
);
212 * Change the active gamepad.
214 * You can set active gamepad here. The first gamepad to be
215 * activaed is 0. If the gamepad could not be activated a qCritical
216 * will be printed and false is returned.
219 * @param index The new gamepad by index.
220 * @return True if the gamepad was successfully changed.
222 bool setGamepad(qint16 index
);
227 * Get new axes information from the SDL system.
229 * This class member is called from the run method to ask the SDL
230 * system for new axes values. Afterwords those values are emitted
231 * via the axesValues signal.
239 * Get new button information from the SDL system.
241 * This class member is called from the run method to ask the SDL
242 * system for new button states. If changed, the states are will be
243 * emitted via the buttonState signal.
248 void updateButtons();
251 * Variable to control thread.
253 * This class member variable is false at construction time. If
254 * the SDL init was successful it will be set to true. The quit
255 * slot will false it again.
264 * If a new gamepad is opened via SDL the acutal button count is
265 * saved in this class member to be used in control statements.
272 * If a new gamepad is opened via SDL the acutal axes count is
273 * saved in this class member to be used in control statements.
280 * A variable to be used in the run method to sleep after getting
281 * and emitting SDL information.
288 * SDL_Joystick index.
290 * A variable that holds the SDL_Joystick index of the currently
291 * opend SDL_Joystick Object.
296 * A QList to store the current button states.
298 * This list stores the current states of all available buttons.
300 QList
<qint16
> buttonStates
;
303 * Variable that holds private members.
305 SDLGamepadPrivate
*priv
;
310 * A signal that emitts the number of gamepads present in SDL.
312 * This signal is emitted in the init slot, so you need connect to
313 * this bevore you call init! You get a quint8 with the number of
314 * gamepads currently present in the SDL system. The first gamepad
315 * will be opened at default. You can set another gamepad with the
316 * setGamepad slot. If you have 5 gamepads in the system you need
317 * to use 0-4 in the setGamepad slot.
320 * @param count The number of gamepads present in SDL.
322 void gamepads(quint8 count
);
325 * A signal that emitts the current state of a gamepad button.
327 * You can connect to this signal to receive the state of the
328 * gamepad buttons. The states are not pressed at default. If the
329 * first pressed state will occur within SDL you will get a signal.
330 * You will get another signal if you release the button. With
331 * every tick the states will be reread from the SDL system.
333 * @param number A ButtonNumber enum value.
334 * @param pressed A bool value wether the button is pressed or not.
336 void buttonState(ButtonNumber number
, bool pressed
);
339 * A signal that emitts the current values of the gamepad axes.
341 * You can connect to this signal to receive the values of the
342 * gamepad axes. Unlike the button signal, this signal is thrown
343 * with every tick. You will get a QListInt16 containing the value
344 * of every present axis in a QList.
347 * @param values A QListInt16 Type containing all axes values.
349 void axesValues(QListInt16 values
);
352 #endif // SDLGAMEPAD_H