xoreos  0.0.5
joystick.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/events/joystick.h"
28 
29 namespace Events {
30 
31 Joystick::Joystick(int index) : _index(index), _sdlJoy(0) {
32  assert(_index >= 0);
33 }
34 
36  disable();
37 }
38 
39 int Joystick::getIndex() const {
40  return _index;
41 }
42 
44  return _name;
45 }
46 
47 bool Joystick::isEnabled() const {
48  return _sdlJoy != 0;
49 }
50 
52  if (isEnabled())
53  return true;
54 
55  _sdlJoy = SDL_JoystickOpen(_index);
56  if (!_sdlJoy)
57  return false;
58 
59  _name = SDL_JoystickName(_sdlJoy);
60 
61  return true;
62 }
63 
65  if (_sdlJoy)
66  SDL_JoystickClose(_sdlJoy);
67 }
68 
70  if (!_sdlJoy)
71  return 0;
72 
73  return SDL_JoystickNumAxes(_sdlJoy);
74 }
75 
77  if (!_sdlJoy)
78  return 0;
79 
80  return SDL_JoystickNumBalls(_sdlJoy);
81 }
82 
83 int Joystick::getHatCount() const {
84  if (!_sdlJoy)
85  return 0;
86 
87  return SDL_JoystickNumHats(_sdlJoy);
88 }
89 
91  if (!_sdlJoy)
92  return 0;
93 
94  return SDL_JoystickNumButtons(_sdlJoy);
95 }
96 
97 } // End of namespace Events
int getHatCount() const
Return the number of hats the joystick has.
Definition: joystick.cpp:83
int getButtonCount() const
Return the number of buttons the joystick has.
Definition: joystick.cpp:90
A class holding an UTF-8 string.
Definition: ustring.h:48
Joystick/Gamepad handling.
SDL_Joystick * _sdlJoy
Definition: joystick.h:70
bool enable()
Enable the joystick.
Definition: joystick.cpp:51
int getAxisCount() const
Return the number of axes the joystick has.
Definition: joystick.cpp:69
bool isEnabled() const
Is the joystick currently enabled?
Definition: joystick.cpp:47
Joystick(int index)
Definition: joystick.cpp:31
Common::UString _name
Definition: joystick.h:69
int getIndex() const
Return the joystick&#39;s index within the available joysticks.
Definition: joystick.cpp:39
void disable()
Disable the joystick.
Definition: joystick.cpp:64
const Common::UString & getName() const
Return the joystick&#39;s name.
Definition: joystick.cpp:43
int getBallCount() const
Return the number of balls the joystick has.
Definition: joystick.cpp:76