-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoystickhandler.cpp
More file actions
166 lines (154 loc) · 6.85 KB
/
Copy pathjoystickhandler.cpp
File metadata and controls
166 lines (154 loc) · 6.85 KB
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
#include "joystickhandler.h"
JoystickHandler::JoystickHandler(QObject *parent)
: QObject{parent}, m_joystick(new Joystick),
m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope,
"CfRD", "RovUI2", this)) {
SDL_Init(SDL_INIT_JOYSTICK);
SDL_JoystickUpdate();
if (SDL_NumJoysticks() > 0) {
m_sdlJoystick = SDL_JoystickOpen(0);
joystickConnected = true;
updateJoystick();
updateSettings();
QTimer::singleShot(200, this, [this]() {
emit joystickChanged(Joystick(m_joystick.data()));
});
}
startTimer(16);
}
void JoystickHandler::updateSettings() {
m_settings.data()->sync();
for (int i = 0; i < 6; i++) // init axes from settings
m_joystick.data()->axes_id[i] =
m_settings
->value(QString(SDL_JoystickName(m_sdlJoystick)) +
QString("/joystickAxes/") +
JoystickHelpers::axesNames[i])
.toInt();
for (int i = 0; i < 16; i++) // same for buttons
m_joystick.data()->buttons_id[i] =
m_settings
->value(QString(SDL_JoystickName(m_sdlJoystick)) +
QString("/joystickButtons/") +
JoystickHelpers::buttonsNames[i])
.toInt();
for (int i = 0; i < 4; i++) { // same for hats
m_joystick.data()->hats_id[i] =
m_settings
->value(QString(SDL_JoystickName(m_sdlJoystick)) +
QString("/joystickHats/") +
JoystickHelpers::hatsNames[i])
.toInt();
m_joystick.data()->hats_hor[i] =
m_settings
->value(QString(SDL_JoystickName(m_sdlJoystick)) +
QString("/joystickHatsHor/") +
JoystickHelpers::hatsNames[i])
.toBool();
}
}
void JoystickHandler::updateJoystick() {
m_joystick->numAxes = SDL_JoystickNumAxes(m_sdlJoystick);
m_joystick->numHats = SDL_JoystickNumHats(m_sdlJoystick);
m_joystick->numButtons = SDL_JoystickNumButtons(m_sdlJoystick);
m_joystick->joystickName = QString(SDL_JoystickName(m_sdlJoystick));
emit joystickChanged(Joystick(m_joystick.data()));
}
void JoystickHandler::updateASFs(int asf[6]) {
for (int i = 0; i < 8; ++i) {
m_joystick->runtimeASF[i] = asf[i];
}
}
void JoystickHandler::timerEvent(QTimerEvent *) {
m_settings.data()->sync();
SDL_JoystickUpdate();
if (SDL_NumJoysticks() <= 0) { // no joysticks connected
if (!notifiedNoJoysticks) {
qInfo() << "SDL cannot find joysticks on this system";
emit joystickUpdated(Joystick());
notifiedNoJoysticks = true;
joystickConnected = false;
}
} else if (m_sdlJoystick == nullptr ||
joystickConnected != true) { // joystick exists but isn't opened
m_sdlJoystick = SDL_JoystickOpen(0);
qInfo() << "Connected joystick 0 (" << SDL_JoystickName(m_sdlJoystick)
<< ")," << SDL_JoystickNumAxes(m_sdlJoystick) << " axes,"
<< SDL_JoystickNumButtons(m_sdlJoystick) << "buttons";
joystickConnected = true;
updateJoystick();
updateSettings();
} else if (!SDL_JoystickGetAttached(
m_sdlJoystick)) { // joystick exists but isn't opened
m_sdlJoystick = SDL_JoystickOpen(0);
qInfo() << "Connected joystick 0 (" << SDL_JoystickName(m_sdlJoystick)
<< "), " << SDL_JoystickNumAxes(m_sdlJoystick) << " axes, "
<< SDL_JoystickNumButtons(m_sdlJoystick) << " buttons";
joystickConnected = true;
updateJoystick();
updateSettings();
} else {
for (size_t i = 0; i < helpers::size(m_joystick.data()->axes);
i++) { // joystick input smoothing 3x
if (m_joystick.data()->axes_id[i] >= 0) {
float currentAxisVal = SDL_JoystickGetAxis(
m_sdlJoystick, m_joystick.data()->axes_id[i]);
float lastAxisVal = m_joystick.data()->axes[i];
float secLastAxisVal = m_joystick.data()->axes_last[i];
m_joystick.data()->axes[i] =
(map(currentAxisVal, -32768, 32767, -101, 101) +
lastAxisVal + secLastAxisVal) /
3;
m_joystick.data()->axes_last[i] = lastAxisVal;
} else {
m_joystick.data()->axes[i] = 0;
m_joystick.data()->axes_last[i] = 0;
}
}
for (size_t i = 0; i < sizeof(m_joystick.data()->buttons) * 8;
i++) { // write buttons into the variable according to
// buttonsNames
if (SDL_JoystickGetButton(m_sdlJoystick,
m_joystick.data()->buttons_id[i]))
BIT_SET(m_joystick.data()->buttons.rawData, i);
else
BIT_CLEAR(m_joystick.data()->buttons.rawData, i);
}
for (size_t i = 0; i < helpers::size(m_joystick.data()->hats);
i++) { // write hats
int hat = SDL_JoystickGetHat(m_sdlJoystick,
m_joystick.data()->hats_id[i]);
if (m_joystick.data()->hats_hor[i]) { // left -> -1, centered -> 0,
// right -> 1, else -> 0
m_joystick.data()->hats[i] = (hat & SDL_HAT_LEFT) ? -1
: (hat & SDL_HAT_RIGHT) ? 1
: 0;
} else { // down -> -1, centered -> 0, up -> 1, else -> 0
m_joystick.data()->hats[i] = (hat & SDL_HAT_DOWN) ? -1
: (hat & SDL_HAT_UP) ? 1
: 0;
}
}
if (m_joystick.data()->buttons.ASFFast) {
for (size_t i = 0; i < helpers::size(m_joystick.data()->runtimeASF);
i++)
m_joystick.data()->runtimeASF[i] = 1.0f;
}
if (m_joystick.data()->buttons.ASFMedium) {
for (size_t i = 0; i < helpers::size(m_joystick.data()->runtimeASF);
i++)
m_joystick.data()->runtimeASF[i] = .75f;
}
if (m_joystick.data()->buttons.ASFSlow) {
for (size_t i = 0; i < helpers::size(m_joystick.data()->runtimeASF);
i++)
m_joystick.data()->runtimeASF[i] = .5f;
}
if (m_joystick.data()->buttons.ASFUltraSlow) {
for (size_t i = 0; i < helpers::size(m_joystick.data()->runtimeASF);
i++)
m_joystick.data()->runtimeASF[i] = .25f;
}
emit joystickUpdated(Joystick(m_joystick.data()));
}
}