/
SNIIS_Linux_Joystick.cpp
284 lines (248 loc) · 8.62 KB
/
SNIIS_Linux_Joystick.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/// @file SNIIS_Linux_Joystick.cpp
/// Linux implementation of anything that does not fit the description of a "mouse" or a "keyboard"
#include "SNIIS_Linux.h"
#include "SNIIS_Intern.h"
#if SNIIS_SYSTEM_LINUX
#include <cstring>
#include <linux/input.h>
using namespace SNIIS;
static bool IsBitSet( const uint8_t* bits, size_t i) { return (bits[i/8] & (1<<(i&7))) != 0; }
// --------------------------------------------------------------------------------------------------------------------
LinuxJoystick::LinuxJoystick( LinuxInput* pSystem, size_t pId, int pFileDesc)
: Joystick( pId), mSystem( pSystem), mFileDesc( pFileDesc)
{
memset( &mState, 0, sizeof( mState));
// enumerate, now for real
uint8_t ev_bits[(EV_MAX+7)/8];
memset( ev_bits, 0, sizeof(ev_bits) );
//Read "all" (hence 0) components of the device
if( ioctl( mFileDesc, EVIOCGBIT( 0, sizeof(ev_bits)), ev_bits) == -1 )
throw std::runtime_error( "Could not read device events features");
// Absolute axes
if( IsBitSet( ev_bits, EV_ABS) )
{
uint8_t abs_bits[(ABS_MAX+7)/8];
memset( abs_bits, 0, sizeof(abs_bits) );
if( ioctl( mFileDesc, EVIOCGBIT( EV_ABS, sizeof(abs_bits)), abs_bits) == -1 )
throw std::runtime_error( "Could not read device absolute axis features");
for( size_t a = 0; a < ABS_MAX; a++ )
{
if( IsBitSet( abs_bits, a) )
{
input_absinfo abInfo;
if( ioctl( mFileDesc, EVIOCGABS(a), &abInfo ) == -1 )
continue;
mAxes.push_back( Axis{ a, true, abInfo.minimum, abInfo.maximum, abInfo.flat });
}
}
}
// Relative axes
if( IsBitSet( ev_bits, EV_REL) )
{
uint8_t rel_bits[(REL_MAX+7)/8];
memset( rel_bits, 0, sizeof(rel_bits) );
if( ioctl( mFileDesc, EVIOCGBIT( EV_REL, sizeof(rel_bits)), rel_bits) == -1 )
throw std::runtime_error( "Could not read device relative axis features");
for( size_t a = 0; a < REL_MAX; a++ )
{
if( IsBitSet( rel_bits, a) )
{
mAxes.push_back( Axis{ a, false, 0, 0, 0 });
}
}
}
// Buttons
if( IsBitSet( ev_bits, EV_KEY) )
{
uint8_t key_bits[(KEY_MAX+7)/8];
memset( key_bits, 0, sizeof(key_bits) );
if( ioctl( mFileDesc, EVIOCGBIT( EV_KEY, sizeof(key_bits)), key_bits) == -1 )
throw std::runtime_error( "Could not read device button features");
for( size_t a = 0; a < KEY_MAX; a++ )
{
if( IsBitSet( key_bits, a) )
{
mButtons.push_back( Button{ a });
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxJoystick::StartUpdate()
{
mState.prevButtons = mState.buttons;
memset( mState.diffs, 0, sizeof( mState.diffs));
float prevAxes[16];
static_assert( sizeof( prevAxes) == sizeof( mState.axes), "Hardcoded array size");
memcpy( prevAxes, mState.axes, sizeof( mState.axes));
// read events from file descriptor
input_event js[64];
while( true )
{
int ret = read( mFileDesc, &js, sizeof(js));
if( ret <= 0 )
break;
size_t numEvents = size_t( ret) / sizeof(struct input_event);
for( size_t a = 0; a < numEvents; ++a )
{
const auto& ev = js[a];
switch( ev.type )
{
case EV_KEY: // Button
{
size_t bt = ev.code;
auto it = std::find_if( mButtons.cbegin(), mButtons.cend(), [=](const Button& b) { return b.idx == bt; });
if( it == mButtons.cend() )
break;
size_t btidx = std::distance( mButtons.cbegin(), it);
bool isPressed = (ev.value != 0);
if( isPressed )
mState.buttons |= 1ull << btidx;
else
mState.buttons &= (UINT64_MAX ^ (1ull << btidx));
break;
}
case EV_ABS: // Absolute Axis
{
size_t ax = ev.code;
auto it = std::find_if( mAxes.cbegin(), mAxes.cend(), [=](const Axis& c) { return c.isAbsolute && c.idx == ax; });
if( it == mAxes.cend() )
break;
size_t axidx = std::distance( mAxes.cbegin(), it);
float v = 0.0f;
// try to tell one-sided axes apart from symmetric axes and act accordingly
if( std::abs( it->min) <= std::abs( it->max) / 10 )
v = float( ev.value - it->min) / float( it->max - it->min);
else
v = (float( ev.value - it->min) / float( it->max - it->min)) * 2.0f - 1.0f;
mState.diffs[axidx] = v - mState.axes[axidx];
mState.axes[axidx] = v;
break;
}
case EV_REL: // Relative Axis.
{
size_t ax = ev.code;
auto it = std::find_if( mAxes.cbegin(), mAxes.cend(), [=](const Axis& c) { return !c.isAbsolute && c.idx == ax; });
if( it == mAxes.cend() )
break;
size_t axidx = std::distance( mAxes.cbegin(), it);
float d = float( ev.value);
mState.diffs[axidx] += d;
mState.axes[axidx] += d;
break;
}
default:
break;
}
}
}
// send events - Axes
for( size_t i = 0; i < mAxes.size(); i++ )
{
if( mState.axes[i] != prevAxes[i] )
{
mState.diffs[i] = mState.axes[i] - prevAxes[i];
if( !mIsFirstUpdate )
InputSystemHelper::DoJoystickAxis( this, i, mState.axes[i]);
}
}
// send events - buttons
for( size_t i = 0; i < mButtons.size(); i++ )
{
if( !mIsFirstUpdate )
if( (mState.buttons ^ mState.prevButtons) & (1ull << i) )
InputSystemHelper::DoJoystickButton( this, i, (mState.buttons & (1ull << i)) != 0);
}
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxJoystick::SetFocus( bool pHasFocus)
{
if( pHasFocus )
{
// don't do anything. Buttons currently pressed are lost then, all other controls will be handled correctly at next update
// TODO: relative axes? If anyone ever spots one in the wilds, please let me know
}
else
{
// zero all buttons and axes
for( size_t a = 0; a < mAxes.size(); ++a )
{
if( mState.axes[a] != 0.0f )
{
mState.diffs[a] = -mState.axes[a];
mState.axes[a] = 0.0f;
InputSystemHelper::DoJoystickAxis( this, a, 0.0f);
}
}
for( size_t a = 0; a < mButtons.size(); ++a )
{
if( mState.buttons & (1ull << a) )
{
mState.buttons &= UINT64_MAX ^ (1ull << a);
mState.prevButtons |= 1ull << a;
InputSystemHelper::DoJoystickButton( this, a, false);
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------
size_t LinuxJoystick::GetNumButtons() const
{
return mButtons.size();
}
// --------------------------------------------------------------------------------------------------------------------
std::string LinuxJoystick::GetButtonText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
size_t LinuxJoystick::GetNumAxes() const
{
return mAxes.size();
}
// --------------------------------------------------------------------------------------------------------------------
std::string LinuxJoystick::GetAxisText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxJoystick::IsButtonDown( size_t idx) const
{
if( idx < mButtons.size() )
return (mState.buttons & (1ull << idx)) != 0;
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxJoystick::WasButtonPressed( size_t idx) const
{
if( idx < mButtons.size() )
return IsButtonDown( idx) && ((mState.prevButtons & (1ull << idx)) == 0);
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxJoystick::WasButtonReleased( size_t idx) const
{
if( idx < mButtons.size() )
return !IsButtonDown( idx) && ((mState.prevButtons & (1ull << idx)) != 0);
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
float LinuxJoystick::GetAxisAbsolute( size_t idx) const
{
if( idx < mAxes.size() )
return mState.axes[idx];
else
return 0.0f;
}
// --------------------------------------------------------------------------------------------------------------------
float LinuxJoystick::GetAxisDifference( size_t idx) const
{
if( idx < mAxes.size() )
return mState.diffs[idx];
else
return 0.0f;
}
#endif // SNIIS_SYSTEM_LINUX