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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/*
* Copyright (C) 2012-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "WINJoystick.h"
#include "input/ButtonTranslator.h"
#include "peripherals/devices/PeripheralImon.h"
#include "settings/AdvancedSettings.h"
#include "settings/lib/Setting.h"
#include "utils/log.h"
#include "utils/RegExp.h"
#include <math.h>
#include <boost/shared_ptr.hpp>
#include <dinput.h>
#include <dinputd.h>
using namespace std;
extern HWND g_hWnd;
#define MAX_AXISAMOUNT 32768
#define AXIS_MIN -32768 /* minimum value for axis coordinate */
#define AXIS_MAX 32767 /* maximum value for axis coordinate */
#if !defined(HAS_SDL)
#define SDL_HAT_CENTERED 0x00
#define SDL_HAT_UP 0x01
#define SDL_HAT_RIGHT 0x02
#define SDL_HAT_DOWN 0x04
#define SDL_HAT_LEFT 0x08
#define SDL_HAT_RIGHTUP (SDL_HAT_RIGHT|SDL_HAT_UP)
#define SDL_HAT_RIGHTDOWN (SDL_HAT_RIGHT|SDL_HAT_DOWN)
#define SDL_HAT_LEFTUP (SDL_HAT_LEFT|SDL_HAT_UP)
#define SDL_HAT_LEFTDOWN (SDL_HAT_LEFT|SDL_HAT_DOWN)
#endif
CJoystick::CJoystick()
{
CSingleLock lock(m_critSection);
m_joystickEnabled = false;
SetDeadzone(0);
Reset();
}
CJoystick::~CJoystick()
{
ReleaseJoysticks();
}
void CJoystick::OnSettingChanged(const CSetting *setting)
{
if (setting == NULL)
return;
const std::string &settingId = setting->GetId();
if (settingId == "input.enablejoystick")
SetEnabled(((CSettingBool*)setting)->GetValue() && PERIPHERALS::CPeripheralImon::GetCountOfImonsConflictWithDInput() == 0);
}
void CJoystick::Reset()
{
m_AxisIdx = -1;
m_ButtonIdx = -1;
m_HatIdx = -1;
m_HatState = SDL_HAT_CENTERED;
for (std::vector<AxisState>::iterator it = m_Axes.begin(); it != m_Axes.end(); ++it)
it->reset();
}
void CJoystick::ReleaseJoysticks()
{
CSingleLock lock(m_critSection);
// Unacquire the device one last time just in case
// the app tried to exit while the device is still acquired.
for(std::vector<LPDIRECTINPUTDEVICE8>::iterator it = m_pJoysticks.begin(); it != m_pJoysticks.end(); ++it)
{
if( (*it) )
(*it)->Unacquire();
SAFE_RELEASE( (*it) );
}
m_pJoysticks.clear();
m_JoystickNames.clear();
m_Axes.clear();
m_devCaps.clear();
Reset();
// Release any DirectInput objects.
SAFE_RELEASE( m_pDI );
}
BOOL CALLBACK CJoystick::EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext )
{
HRESULT hr;
CJoystick* p_this = (CJoystick*) pContext;
LPDIRECTINPUTDEVICE8 pJoystick = NULL;
// Obtain an interface to the enumerated joystick.
hr = p_this->m_pDI->CreateDevice( pdidInstance->guidInstance, &pJoystick, NULL );
if( SUCCEEDED( hr ) )
{
// Set the data format to "simple joystick" - a predefined data format
//
// A data format specifies which controls on a device we are interested in,
// and how they should be reported. This tells DInput that we will be
// passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
if( SUCCEEDED( hr = pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) )
{
// Set the cooperative level to let DInput know how this device should
// interact with the system and with other DInput applications.
if( SUCCEEDED( hr = pJoystick->SetCooperativeLevel( g_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND ) ) )
{
DIDEVCAPS diDevCaps;
diDevCaps.dwSize = sizeof(DIDEVCAPS);
if (SUCCEEDED(hr = pJoystick->GetCapabilities(&diDevCaps)))
{
CLog::Log(LOGNOTICE, __FUNCTION__" : Enabled Joystick: %s", pdidInstance->tszProductName);
CLog::Log(LOGNOTICE, __FUNCTION__" : Total Axis: %d Total Hats: %d Total Buttons: %d", diDevCaps.dwAxes, diDevCaps.dwPOVs, diDevCaps.dwButtons);
p_this->m_pJoysticks.push_back(pJoystick);
std::string joyName(pdidInstance->tszProductName);
p_this->m_JoystickNames.push_back(joyName);;
p_this->m_Axes.resize(p_this->m_Axes.size() + 6); // dinput hardcodes to 6 axes
p_this->m_devCaps.push_back(diDevCaps);
// load axes configuration from keymap
std::map<boost::shared_ptr<CRegExp>, AxesConfig>::const_iterator axesCfg;
for (axesCfg = p_this->m_AxesConfigs.begin(); axesCfg != p_this->m_AxesConfigs.end(); axesCfg++)
{
if (axesCfg->first->RegFind(joyName) >= 0)
break;
}
if (axesCfg != p_this->m_AxesConfigs.end())
{
for (AxesConfig::const_iterator it = axesCfg->second.begin(); it != axesCfg->second.end(); ++it)
{
int axis = p_this->MapAxis(pJoystick, it->axis - 1);
p_this->m_Axes[axis].trigger = it->isTrigger;
p_this->m_Axes[axis].rest = it->rest;
}
}
}
else
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to GetCapabilities for: %s", pdidInstance->tszProductName);
}
else
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to SetCooperativeLevel on: %s", pdidInstance->tszProductName);
}
else
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to SetDataFormat on: %s", pdidInstance->tszProductName);
}
else
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to CreateDevice: %s", pdidInstance->tszProductName);
return DIENUM_CONTINUE;
}
//-----------------------------------------------------------------------------
// Name: EnumObjectsCallback()
// Desc: Callback function for enumerating objects (axes, buttons, POVs) on a
// joystick. This function enables user interface elements for objects
// that are found to exist, and scales axes min/max values.
//-----------------------------------------------------------------------------
BOOL CALLBACK CJoystick::EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext )
{
LPDIRECTINPUTDEVICE8 pJoy = (LPDIRECTINPUTDEVICE8) pContext;
// For axes that are returned, set the DIPROP_RANGE property for the
// enumerated axis in order to scale min/max values.
if( pdidoi->dwType & DIDFT_AXIS )
{
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof( DIPROPRANGE );
diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = pdidoi->dwType; // Specify the enumerated axis
diprg.lMin = AXIS_MIN;
diprg.lMax = AXIS_MAX;
// Set the range for the axis
if( FAILED( pJoy->SetProperty( DIPROP_RANGE, &diprg.diph ) ) )
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to set property on %s", pdidoi->tszName);
}
return DIENUM_CONTINUE;
}
void CJoystick::Initialize()
{
if (!IsEnabled())
return;
HRESULT hr;
// clear old joystick names
ReleaseJoysticks();
CSingleLock lock(m_critSection);
if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, ( VOID** )&m_pDI, NULL ) ) )
{
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to create DirectInput");
return;
}
if( FAILED( hr = m_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, this, DIEDFL_ATTACHEDONLY ) ) )
return;
if(m_pJoysticks.size() == 0)
{
CLog::Log(LOGDEBUG, __FUNCTION__" : No Joystick found");
return;
}
for(std::vector<LPDIRECTINPUTDEVICE8>::iterator it = m_pJoysticks.begin(); it != m_pJoysticks.end(); ++it)
{
LPDIRECTINPUTDEVICE8 pJoy = (*it);
// Enumerate the joystick objects. The callback function enabled user
// interface elements for objects that are found, and sets the min/max
// values property for discovered axes.
if( FAILED( hr = pJoy->EnumObjects( EnumObjectsCallback, pJoy, DIDFT_ALL ) ) )
CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to enumerate objects");
}
// Set deadzone range
SetDeadzone(g_advancedSettings.m_controllerDeadzone);
}
void CJoystick::Update()
{
if (!IsEnabled())
return;
int axisNum;
int buttonIdx = -1, buttonNum;
int hatIdx = -1, hatNum;
// go through all joysticks
for (size_t j = 0; j < m_pJoysticks.size(); j++)
{
LPDIRECTINPUTDEVICE8 pjoy = m_pJoysticks[j];
DIDEVCAPS caps = m_devCaps[j];
HRESULT hr;
DIJOYSTATE2 js; // DInput joystick state
hr = pjoy->Poll();
if( FAILED( hr ) )
{
int i=0;
// DInput is telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done. We
// just re-acquire and try again.
hr = pjoy->Acquire();
while( (hr == DIERR_INPUTLOST) && (i++ < 10) )
hr = pjoy->Acquire();
// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
Reset();
return;
}
// Get the input's device state
if( FAILED( hr = pjoy->GetDeviceState( sizeof( DIJOYSTATE2 ), &js ) ) )
{
Reset();
return; // The device should have been acquired during the Poll()
}
// get button states first, they take priority over axis
for (int b = 0; b < caps.dwButtons; b++)
{
if (js.rgbButtons[b] & 0x80)
{
buttonIdx = MapButton(pjoy, b);
j = m_pJoysticks.size();
break;
}
}
// get hat position
for (int h = 0; h < caps.dwPOVs; h++)
{
if ((LOWORD(js.rgdwPOV[h]) == 0xFFFF) != true)
{
uint8_t hatval = SDL_HAT_CENTERED;
if ((js.rgdwPOV[0] > JOY_POVLEFT) || (js.rgdwPOV[0] < JOY_POVRIGHT))
hatval |= SDL_HAT_UP;
if ((js.rgdwPOV[0] > JOY_POVFORWARD) && (js.rgdwPOV[0] < JOY_POVBACKWARD))
hatval |= SDL_HAT_RIGHT;
if ((js.rgdwPOV[0] > JOY_POVRIGHT) && (js.rgdwPOV[0] < JOY_POVLEFT))
hatval |= SDL_HAT_DOWN;
if (js.rgdwPOV[0] > JOY_POVBACKWARD)
hatval |= SDL_HAT_LEFT;
if (hatval != SDL_HAT_CENTERED)
{
hatIdx = MapHat(pjoy, h);
j = m_pJoysticks.size();
m_HatState = hatval;
}
}
}
// get axis states
int axisIdx = MapAxis(pjoy, 0);
m_Axes[axisIdx + 0].val = js.lX;
m_Axes[axisIdx + 1].val = js.lY;
m_Axes[axisIdx + 2].val = js.lZ;
m_Axes[axisIdx + 3].val = js.lRx;
m_Axes[axisIdx + 4].val = js.lRy;
m_Axes[axisIdx + 5].val = js.lRz;
}
LPDIRECTINPUTDEVICE8 pjoy;
m_AxisIdx = GetAxisWithMaxAmount();
if (m_AxisIdx >= 0)
{
MapAxis(m_AxisIdx, pjoy, axisNum);
// CLog::Log(LOGDEBUG, "Joystick %d Axis %d Amount %d", JoystickIndex(pjoy), axisNum + 1, m_Axes[m_AxisIdx].val);
}
if (hatIdx == -1 && m_HatIdx != -1)
{
// now centered
MapHat(m_HatIdx, pjoy, hatNum);
CLog::Log(LOGDEBUG, "Joystick %d hat %u hat centered", JoystickIndex(pjoy), hatNum + 1);
m_pressTicksHat = 0;
}
else if (hatIdx != m_HatIdx)
{
MapHat(hatIdx, pjoy, hatNum);
CLog::Log(LOGDEBUG, "Joystick %d hat %u value %d", JoystickIndex(pjoy), hatNum + 1, m_HatState);
m_pressTicksHat = XbmcThreads::SystemClockMillis();
}
m_HatIdx = hatIdx;
if (buttonIdx == -1 && m_ButtonIdx != -1)
{
MapButton(m_ButtonIdx, pjoy, buttonNum);
CLog::Log(LOGDEBUG, "Joystick %d button %d Up", JoystickIndex(pjoy), buttonNum + 1);
m_pressTicksButton = 0;
m_ButtonIdx = -1;
}
else if (buttonIdx != m_ButtonIdx)
{
MapButton(buttonIdx, pjoy, buttonNum);
CLog::Log(LOGDEBUG, "Joystick %d button %d Down", JoystickIndex(pjoy), buttonNum + 1);
m_pressTicksButton = XbmcThreads::SystemClockMillis();
}
m_ButtonIdx = buttonIdx;
}
bool CJoystick::GetHat(std::string &joyName, int &id, int &position, bool consider_repeat)
{
if (!IsEnabled() || !IsHatActive())
return false;
LPDIRECTINPUTDEVICE8 joy;
MapHat(m_HatIdx, joy, id);
joyName = m_JoystickNames[JoystickIndex(joy)];
position = m_HatState;
if (!consider_repeat)
return true;
static uint32_t lastPressTicksHat = 0;
// allow it if it is the first press
if (lastPressTicksHat < m_pressTicksHat)
{
lastPressTicksHat = m_pressTicksHat;
return true;
}
uint32_t nowTicks = XbmcThreads::SystemClockMillis();
if (nowTicks - m_pressTicksHat < 500) // 500ms delay before we repeat
return false;
if (nowTicks - lastPressTicksHat < 100) // 100ms delay before successive repeats
return false;
lastPressTicksHat = nowTicks;
return true;
}
bool CJoystick::GetButton(std::string &joyName, int &id, bool consider_repeat)
{
if (!IsEnabled() || !IsButtonActive())
return false;
LPDIRECTINPUTDEVICE8 joy;
MapButton(m_ButtonIdx, joy, id);
joyName = m_JoystickNames[JoystickIndex(joy)];
if (!consider_repeat)
return true;
static uint32_t lastPressTicksButton = 0;
// allow it if it is the first press
if (lastPressTicksButton < m_pressTicksButton)
{
lastPressTicksButton = m_pressTicksButton;
return true;
}
uint32_t nowTicks = XbmcThreads::SystemClockMillis();
if (nowTicks - m_pressTicksButton < 500) // 500ms delay before we repeat
return false;
if (nowTicks - lastPressTicksButton < 100) // 100ms delay before successive repeats
return false;
lastPressTicksButton = nowTicks;
return true;
}
bool CJoystick::GetAxis(std::string &joyName, int &id)
{
if (!IsEnabled() || !IsAxisActive())
return false;
LPDIRECTINPUTDEVICE8 joy;
MapAxis(m_AxisIdx, joy, id);
joyName = m_JoystickNames[JoystickIndex(joy)];
return true;
}
int CJoystick::GetAxisWithMaxAmount() const
{
int maxAmount = 0, axis = -1;
for (size_t i = 0; i < m_Axes.size(); i++)
{
int deadzone = m_Axes[i].trigger ? 0 : m_DeadzoneRange,
amount = abs(m_Axes[i].val - m_Axes[i].rest);
if (amount > deadzone && amount > maxAmount)
{
maxAmount = amount;
axis = i;
}
}
return axis;
}
float CJoystick::GetAmount(const std::string &joyName, int axisNum) const
{
int joyIdx = JoystickIndex(joyName);
if (joyIdx == -1)
return 0;
int axisIdx = MapAxis(m_pJoysticks[joyIdx], axisNum);
int amount = m_Axes[axisIdx].val - m_Axes[axisIdx].rest;
int range = MAX_AXISAMOUNT - m_Axes[axisIdx].rest;
// deadzones on axes are undesirable
int deadzone = m_Axes[axisIdx].trigger ? 0 : m_DeadzoneRange;
if (amount > deadzone)
return (float)(amount - deadzone) / (float)(range - deadzone);
else if (amount < -deadzone)
return (float)(amount + deadzone) / (float)(range - deadzone);
return 0;
}
void CJoystick::SetEnabled(bool enabled /*=true*/)
{
if( enabled && !m_joystickEnabled )
{
m_joystickEnabled = true;
Initialize();
}
else if( !enabled && m_joystickEnabled )
{
ReleaseJoysticks();
m_joystickEnabled = false;
}
}
float CJoystick::SetDeadzone(float val)
{
if (val<0) val=0;
if (val>1) val=1;
m_DeadzoneRange = (int)(val*MAX_AXISAMOUNT);
return val;
}
bool CJoystick::Reinitialize()
{
Initialize();
return true;
}
void CJoystick::Acquire()
{
if (!IsEnabled())
return;
if(!m_pJoysticks.empty())
{
CLog::Log(LOGDEBUG, __FUNCTION__": Focus back, acquire Joysticks");
for(std::vector<LPDIRECTINPUTDEVICE8>::iterator it = m_pJoysticks.begin(); it != m_pJoysticks.end(); ++it)
{
if( (*it) )
(*it)->Acquire();
}
}
}
void CJoystick::LoadAxesConfigs(const std::map<boost::shared_ptr<CRegExp>, AxesConfig> &axesConfigs)
{
m_AxesConfigs.clear();
m_AxesConfigs.insert(axesConfigs.begin(), axesConfigs.end());
}
int CJoystick::JoystickIndex(const std::string &joyName) const
{
for (size_t i = 0; i < m_JoystickNames.size(); ++i)
{
if (joyName == m_JoystickNames[i])
return i;
}
return -1;
}
int CJoystick::JoystickIndex(LPDIRECTINPUTDEVICE8 joy) const
{
for (size_t i = 0; i < m_pJoysticks.size(); ++i)
{
if (joy == m_pJoysticks[i])
return i;
}
return -1;
}
int CJoystick::MapAxis(LPDIRECTINPUTDEVICE8 joy, int axisNum) const
{
int idx = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy; ++i)
idx += m_devCaps[i].dwAxes;
if (i == m_pJoysticks.size())
return -1;
else
return idx + axisNum;
}
void CJoystick::MapAxis(int axisIdx, LPDIRECTINPUTDEVICE8 &joy, int &axisNum) const
{
int axisCount = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy && axisCount + m_devCaps[i].dwAxes <= axisIdx; ++i)
axisCount += m_devCaps[i].dwAxes;
if (i != m_pJoysticks.size())
{
joy = m_pJoysticks[i];
axisNum = axisIdx - axisCount;
}
else
{
joy = NULL;
axisNum = -1;
}
}
int CJoystick::MapButton(LPDIRECTINPUTDEVICE8 joy, int buttonNum) const
{
int idx = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy; ++i)
idx += m_devCaps[i].dwButtons;
if (i == m_pJoysticks.size())
return -1;
else
return idx + buttonNum;
}
void CJoystick::MapButton(int buttonIdx, LPDIRECTINPUTDEVICE8 &joy, int &buttonNum) const
{
int buttonCount = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy && buttonCount + m_devCaps[i].dwButtons <= buttonIdx; ++i)
buttonCount += m_devCaps[i].dwButtons;
if (i != m_pJoysticks.size())
{
joy = m_pJoysticks[i];
buttonNum = buttonIdx - buttonCount;
}
else
{
joy = NULL;
buttonNum = -1;
}
}
int CJoystick::MapHat(LPDIRECTINPUTDEVICE8 joy, int hatNum) const
{
int idx = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy; ++i)
idx += m_devCaps[i].dwPOVs;
if (i == m_pJoysticks.size())
return -1;
else
return idx + hatNum;
}
void CJoystick::MapHat(int hatIdx, LPDIRECTINPUTDEVICE8 &joy, int &hatNum) const
{
int hatCount = 0;
size_t i;
for (i = 0; i != m_pJoysticks.size() && m_pJoysticks[i] != joy && hatCount + m_devCaps[i].dwPOVs <= hatIdx; ++i)
hatCount += m_devCaps[i].dwPOVs;
if (i != m_pJoysticks.size())
{
joy = m_pJoysticks[i];
hatNum = hatIdx - hatCount;
}
else
{
joy = NULL;
hatNum = -1;
}
}
|