-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjoystickinput.c
120 lines (93 loc) · 3.41 KB
/
joystickinput.c
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
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) 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 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
#define DIRECTINPUT_VERSION 0x0800
#include "joystickinput.h"
static LPDIRECTINPUTDEVICE8 Joysticks[MAXSTICKS];
char StickName[MAXSTICKS][STRLEN];
static unsigned char JoyStickIndex=0;
static LPDIRECTINPUT8 di;
BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* , VOID* );
BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* , VOID* );
static unsigned char CurrentStick;
int EnumerateJoysticks(void)
{
HRESULT hr;
JoyStickIndex=0;
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,IID_IDirectInput8,(VOID**)&di,NULL)))
return(0);
if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,NULL,DIEDFL_ATTACHEDONLY)))
return(0);
return(JoyStickIndex);
}
BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;
hr = di->CreateDevice(instance->guidInstance, &Joysticks[JoyStickIndex],NULL);
strncpy(StickName[JoyStickIndex],instance->tszProductName,STRLEN);
JoyStickIndex++;
return(JoyStickIndex<MAXSTICKS);
}
bool InitJoyStick (unsigned char StickNumber)
{
// DIDEVCAPS capabilities;
HRESULT hr;
CurrentStick=StickNumber;
if (Joysticks[StickNumber]==NULL)
return(0);
if (FAILED(hr= Joysticks[StickNumber]->SetDataFormat(&c_dfDIJoystick2)))
return(0);
// if (FAILED(hr= Joysticks[StickNumber]->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE )))
// return(0);
//Fails for some reason Investigate this
// if (FAILED(hr= Joysticks[StickNumber]->GetCapabilities(&capabilities)))
// return(0);
if (FAILED(hr= Joysticks[StickNumber]->EnumObjects(enumAxesCallback,NULL,DIDFT_AXIS)))
return(0);
return(1); //return true on success
}
BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg= (HWND)context;
DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
propRange.lMin=0;
propRange.lMax=0xFFFF;
if (FAILED(Joysticks[CurrentStick]->SetProperty(DIPROP_RANGE, &propRange.diph)))
return(DIENUM_STOP);
return(DIENUM_CONTINUE);
}
HRESULT JoyStickPoll(DIJOYSTATE2 *js,unsigned char StickNumber)
{
HRESULT hr;
if (Joysticks[StickNumber] ==NULL)
return (S_OK);
hr=Joysticks[StickNumber]->Poll();
if (FAILED(hr))
{
hr=Joysticks[StickNumber]->Acquire();
while (hr == DIERR_INPUTLOST)
hr = Joysticks[StickNumber]->Acquire();
if (hr == DIERR_INVALIDPARAM) //|| (hr == DIERR_NOTINITALIZED
return(E_FAIL);
if (hr == DIERR_OTHERAPPHASPRIO)
return(S_OK);
}
if (FAILED(hr= Joysticks[StickNumber]->GetDeviceState(sizeof(DIJOYSTATE2), js)))
return(hr);
return(S_OK);
}