-
Notifications
You must be signed in to change notification settings - Fork 2
/
Joystick.hpp
205 lines (181 loc) · 4.38 KB
/
Joystick.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#pragma once
#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <chrono>
#include <mutex>
/**
* Manages a joystick using the SDL 2 library.
*
* Note: This is a legacy class for users on Ubuntu
* 20.04 and older. Users on newer systems
* can use the GameController class.
*/
class Joystick {
private:
SDL_Joystick *_joy = nullptr;
int _port;
static std::mutex s_joyCntLck;
static uint64_t s_joyCnt;
/** Initialize this joystick. */
void Init()
{
if (_joy) {
/* close any existing joystick first */
Close();
}
_joy = CreateJoystick();
if (_joy) {
/* Print information about the joystick */
PrintJoystickInfo();
}
}
/** Closes this joystick. */
void Close()
{
/* do nothing if no joystick */
if (!_joy) return;
SDL_JoystickClose(_joy);
_joy = nullptr;
}
public:
/**
* Creates a joystick on the given port.
*/
Joystick(int port) : _port{port}
{
/* first increment joystick count */
{
std::lock_guard lck{s_joyCntLck};
++s_joyCnt;
}
/* then initialize this joystick */
Init();
}
~Joystick()
{
/* first close this joystick */
Close();
/* then decrement joystick count */
{
std::lock_guard lck{s_joyCntLck};
if (--s_joyCnt == 0) {
/* this was the last joystick, quit SDL */
SDL_Quit();
}
}
}
/**
* Returns the port of this joystick.
*/
int GetPort() const { return _port; }
/**
* Returns the name of this joystick.
*/
std::string GetName() const
{
if (_joy) {
return SDL_JoystickName(_joy);
} else {
return "";
}
}
/**
* Returns the SDL type of this joystick.
*/
SDL_JoystickType GetType() const
{
if (_joy) {
return SDL_JoystickGetType(_joy);
} else {
return SDL_JOYSTICK_TYPE_UNKNOWN;
}
}
/**
* Returns the number of buttons on this joystick.
*/
int GetNumButtons() const
{
if (_joy) {
return SDL_JoystickNumButtons(_joy);
} else {
return -1;
}
}
/**
* Returns the number of axes on this joystick.
*/
int GetNumAxes() const
{
if (_joy) {
return SDL_JoystickNumAxes(_joy);
} else {
return -1;
}
}
/**
* Returns the number of hats on this joystick.
*/
int GetNumHats() const
{
if (_joy) {
return SDL_JoystickNumHats(_joy);
} else {
return -1;
}
}
/**
* Returns true if the given button is pressed,
* false otherwise or in the case of an error.
*/
bool GetButton(int button) const
{
if (_joy) {
return SDL_JoystickGetButton(_joy, button);
} else {
return false;
}
}
/**
* Returns the value of the given axis from
* -1.0 to 1.0, or 0 in the case of an error.
*/
double GetAxis(int axis) const
{
if (_joy) {
return SDL_JoystickGetAxis(_joy, axis) / 32768.0;
} else {
return 0.0;
}
}
/**
* Returns whether this joystick is currently connected.
*/
bool IsConnected() const;
/**
* Periodically manages this joystick, handling disconnect
* events in the process.
*/
void Periodic()
{
/* poll for joystick disconnects */
if (!IsConnected()) {
/* one of the joysticks disconnected, assume it was ours */
Close();
}
/* joystick may have disconnected, make sure it's still valid */
if (!_joy) {
/* no joystick, initialize a new one */
Init();
}
}
private:
static constexpr auto kErrorTimeMs = 2000;
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
/** Reports a missing joystick with debouncing. */
void ReportMissingJoystick();
/** Creates and returns a joystick. */
SDL_Joystick *CreateJoystick();
/** Prints out information about this joystick. */
void PrintJoystickInfo() const;
};