-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNIIS_Win_Joystick.cpp
323 lines (286 loc) · 12.4 KB
/
SNIIS_Win_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/// @file SNIIS_Win_Joystick.cpp
/// Windows implementation of joysticks, controllers, quiffles, smoeffles
#include "SNIIS_Win.h"
#include "SNIIS_Intern.h"
#if SNIIS_SYSTEM_WINDOWS
#include <XInput.h>
#include <algorithm>
using namespace SNIIS;
static const size_t JOYSTICK_DX_BUFFERSIZE = 64;
static const int MIN_AXIS = -32768, MAX_AXIS = 32767;
static const size_t XINPUT_TRANSLATED_BUTTON_COUNT = 13;
static const size_t XINPUT_TRANSLATED_AXIS_COUNT = 8;
// --------------------------------------------------------------------------------------------------------------------
WinJoystick::WinJoystick( WinInput* pSystem, size_t pId, IDirectInput8* pDI, GUID pGuidInstance, GUID pGuidProduct)
: Joystick( pId), mSystem( pSystem), mDirectInput( pDI), mGuidInstance( pGuidInstance), mGuidProduct( pGuidProduct)
{
mXInputPadIndex = SIZE_MAX;
mNumButtons = mNumAxes = 0;
memset( &mState, 0, sizeof( mState));
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = JOYSTICK_DX_BUFFERSIZE;
if( FAILED( mDirectInput->CreateDevice( mGuidInstance, &mJoystick, NULL)) )
throw std::runtime_error( "Could not initialize controller device");
if( FAILED( mJoystick->SetDataFormat( &c_dfDIJoystick2)) )
throw std::runtime_error( "Controller data format error");
if( FAILED( mJoystick->SetCooperativeLevel( mSystem->GetWindowHandle(), DISCL_FOREGROUND | DISCL_EXCLUSIVE)) )
throw std::runtime_error( "Controller failed to set cooperation level");
if( FAILED( mJoystick->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph)) )
throw std::runtime_error( "Controller failed to set buffer size property" );
// Get joystick capabilities.
mDIJoyCaps.dwSize = sizeof(DIDEVCAPS);
if( FAILED( mJoystick->GetCapabilities( &mDIJoyCaps)) )
throw std::runtime_error( "Controller failed to get capabilities");
// TODO: handle POVs, maybe as two axes?
//mPOVs = (short)mDIJoyCaps.dwPOVs;
mNumButtons = mDIJoyCaps.dwButtons;
mNumAxes = mDIJoyCaps.dwAxes;
//Enumerate and set axis constraints
mEnumAxis = 0;
mJoystick->EnumObjects( DIEnumDeviceObjectsCallback, this, DIDFT_AXIS);
}
// --------------------------------------------------------------------------------------------------------------------
void WinJoystick::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));
if( mSystem->HasFocus() )
{
// handle xbox controller differently
if( IsXInput() )
{
XINPUT_STATE inputState;
auto res = XInputGetState( (DWORD)mXInputPadIndex, &inputState);
if( res != ERROR_SUCCESS )
memset(&inputState, 0, sizeof(inputState));
//Sticks and triggers
mState.axes[0] = std::max( -1.0f, float( inputState.Gamepad.sThumbLX) / 32767.0f);
mState.axes[1] = std::max( -1.0f, float( -inputState.Gamepad.sThumbLY) / 32767.0f);
mState.axes[2] = std::max( -1.0f, float( inputState.Gamepad.bLeftTrigger) / 127.0f);
mState.axes[3] = std::max( -1.0f, float( inputState.Gamepad.sThumbRX) / 32767.0f);
mState.axes[4] = std::max( -1.0f, float( -inputState.Gamepad.sThumbRY) / 32767.0f);
mState.axes[5] = std::max( -1.0f, float( inputState.Gamepad.bRightTrigger) / 127.0f);
mState.axes[6] = ((inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) ? -1.0f : 0.0f)
+ ((inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? 1.0f : 0.0f);
mState.axes[7] = ((inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) ? -1.0f : 0.0f)
+ ((inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? 1.0f : 0.0f);
// Buttons, except the lowest four which are the DPad - we map those to axis 6 and 7 to
// match the axes reported by the USB interface
mState.buttons = (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? 0x0001 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? 0x0002 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? 0x0004 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? 0x0008 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? 0x0010 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? 0x0020 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? 0x0040 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? 0x0080 : 0;
// mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DAT_CENTER_THINGY) ? 0x0100 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? 0x0200 : 0;
mState.buttons |= (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? 0x0400 : 0;
}
else
{
//handle directinput based devices
DIDEVICEOBJECTDATA diBuff[JOYSTICK_DX_BUFFERSIZE];
DWORD entries = JOYSTICK_DX_BUFFERSIZE;
// Poll the device to read the current state
HRESULT hr = mJoystick->Poll();
if( hr == DI_OK )
hr = mJoystick->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
if( hr != DI_OK )
{
hr = mJoystick->Acquire();
while( hr == DIERR_INPUTLOST )
hr = mJoystick->Acquire();
// Poll the device to read the current state
mJoystick->Poll();
hr = mJoystick->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
//Perhaps the user just tabbed away
if( FAILED(hr) )
{
entries = 0;
memset( &mState, 0, sizeof( mState));
}
}
// Loop through all the events
for( size_t i = 0; i < entries; ++i )
{
switch( diBuff[i].dwOfs )
{
// Max 4 POVs
case DIJOFS_POV(0):
case DIJOFS_POV(1):
case DIJOFS_POV(2):
case DIJOFS_POV(3):
break;
default:
// handle button events easily using the DX Offset Macros
if( diBuff[i].dwOfs >= DIJOFS_BUTTON(0) && diBuff[i].dwOfs < DIJOFS_BUTTON(128) )
{
size_t btnidx = (diBuff[i].dwOfs - DIJOFS_BUTTON(0));
mState.buttons = (mState.buttons & (UINT64_MAX ^ (1ull << btnidx))) | (uint64_t((diBuff[i].dwData >> 7) & 1) << btnidx);
}
else if( ((diBuff[i].uAppData >> 16)&0xffff) == 0x1313 )
{
// if it was nothing else, might be axis enumerated earlier (determined by magic number)
size_t axis = (0x0000FFFF & diBuff[i].uAppData);
if( axis < mNumAxes )
mState.axes[axis] = float( diBuff[i].dwData + MIN_AXIS) / float( MAX_AXIS - MIN_AXIS);
}
break;
} // end case
} // end for
} // end if( DirectInput )
}
// send events - Axes
for( size_t i = 0; i < mNumAxes; 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 < mNumButtons; i++ )
{
if( (mState.buttons ^ mState.prevButtons) & (1ull << i) )
if( !mIsFirstUpdate )
InputSystemHelper::DoJoystickButton( this, i, (mState.buttons & (1ull << i)) != 0);
}
}
// --------------------------------------------------------------------------------------------------------------------
void WinJoystick::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
}
else
{
// zero all buttons and axes
for( size_t a = 0; a < mNumAxes; ++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 < mNumButtons; ++a )
{
if( mState.buttons & (1ull << a) )
{
mState.buttons &= UINT64_MAX ^ (1ull << a);
mState.prevButtons |= 1ull << a;
InputSystemHelper::DoJoystickButton( this, a, false);
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------
void WinJoystick::SetXInput( size_t pXIndex)
{
mXInputPadIndex = pXIndex;
mNumButtons = XINPUT_TRANSLATED_BUTTON_COUNT;
mNumAxes = XINPUT_TRANSLATED_AXIS_COUNT;
}
// --------------------------------------------------------------------------------------------------------------------
int CALLBACK WinJoystick::DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
{
WinJoystick* _this = (WinJoystick*) pvRef;
//Setup mappings
DIPROPPOINTER diptr;
diptr.diph.dwSize = sizeof(DIPROPPOINTER);
diptr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diptr.diph.dwHow = DIPH_BYID;
diptr.diph.dwObj = lpddoi->dwType;
// Add a magic number to recognize we set something
diptr.uData = 0x13130000 | _this->mEnumAxis;
if( FAILED( _this->mJoystick->SetProperty( DIPROP_APPDATA, &diptr.diph)) )
// If for some reason we could not set needed user data, just ignore this axis
return DIENUM_CONTINUE;
//Increase for next time through
_this->mEnumAxis++;
//Set range
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = lpddoi->dwType;
diprg.lMin = MIN_AXIS;
diprg.lMax = MAX_AXIS;
if( FAILED( _this->mJoystick->SetProperty( DIPROP_RANGE, &diprg.diph)) )
throw std::runtime_error( "Controller failed to set min/max range property");
return DIENUM_CONTINUE;
}
// --------------------------------------------------------------------------------------------------------------------
size_t WinJoystick::GetNumButtons() const
{
return mNumButtons;
}
// --------------------------------------------------------------------------------------------------------------------
std::string WinJoystick::GetButtonText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
size_t WinJoystick::GetNumAxes() const
{
return mNumAxes;
}
// --------------------------------------------------------------------------------------------------------------------
std::string WinJoystick::GetAxisText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
bool WinJoystick::IsButtonDown( size_t idx) const
{
if( idx < mNumButtons )
return (mState.buttons & (1ull << idx)) != 0;
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool WinJoystick::WasButtonPressed( size_t idx) const
{
if( idx < mNumButtons )
return IsButtonDown( idx) && ((mState.prevButtons & (1ull << idx)) == 0);
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool WinJoystick::WasButtonReleased( size_t idx) const
{
if( idx < mNumButtons )
return !IsButtonDown( idx) && ((mState.prevButtons & (1ull << idx)) != 0);
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
float WinJoystick::GetAxisAbsolute( size_t idx) const
{
if( idx < mNumAxes )
return mState.axes[idx];
else
return 0.0f;
}
// --------------------------------------------------------------------------------------------------------------------
float WinJoystick::GetAxisDifference( size_t idx) const
{
if( idx < mNumAxes )
return mState.diffs[idx];
else
return 0.0f;
}
#endif // SNIIS_SYSTEM_WINDOWS