-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvJoyManager.cs
252 lines (217 loc) · 5.85 KB
/
vJoyManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using vJoyInterfaceWrap;
namespace XJoy
{
class vJoyManager
{
public const int MaxButtons = 128;
public class DigitalInput
{
public uint ButtonIndex;
string DisplayName;
public DigitalInput(uint _ButtonIndex, string _DisplayName)
{
ButtonIndex = _ButtonIndex;
DisplayName = _DisplayName;
}
public override string ToString()
{
return DisplayName;
}
}
public class AnalogInput
{
public HID_USAGES Axis;
string DisplayName;
public AnalogInput(HID_USAGES _Axis, string _DisplayName)
{
Axis = _Axis;
DisplayName = _DisplayName;
}
public override string ToString()
{
return DisplayName;
}
}
public struct AxisExtents
{
public long Min;
public long Max;
public AxisExtents(long m1, long m2) { Min = m1; Max = m2; }
}
private vJoy m_joystick;
private uint m_vJoyID = 0;
private bool bDeviceAcquired = false;
public uint ActiveVJoyID
{
get { return m_vJoyID; }
}
public bool IsDeviceAcquired
{
get
{
return bDeviceAcquired;
}
}
public vJoyManager()
{
m_joystick = new vJoy();
if(!vJoyEnabled())
{
System.Windows.Forms.MessageBox.Show("Couldn't detect vJoy support. Make sure vJoy is installed.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
UInt32 DllVer = 0;
UInt32 DrvVer = 0;
bool match = m_joystick.DriverMatch(ref DllVer, ref DrvVer);
if (!match)
System.Windows.Forms.MessageBox.Show("vJoy Dll version (" + DllVer + ") and Driver version (" + DrvVer + ") doesn't match. May cause bugs.", "vJoy warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
~vJoyManager()
{
ReleaseDevice();
}
public bool vJoyEnabled()
{
try
{
return (m_joystick != null && m_joystick.vJoyEnabled());
}
catch (DllNotFoundException)
{
System.Windows.Forms.MessageBox.Show("Error, vJoyInterface.dll not found. Exiting...", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
Environment.Exit(-1);
return false;
}
}
public void InitDevice(uint ID)
{
if (!vJoyEnabled())
return;
ReleaseDevice();
bDeviceAcquired = m_joystick.AcquireVJD(ID);
m_vJoyID = ID;
}
public void ReleaseDevice()
{
if (bDeviceAcquired)
{
m_joystick.ResetVJD(m_vJoyID);
m_joystick.RelinquishVJD(m_vJoyID);
bDeviceAcquired = false;
}
}
public bool[] GetAvailableIndices()
{
bool[] devices = new bool[16];
if(vJoyEnabled())
{
for(uint i = 0; i < 16; i++)
{
VjdStat status = m_joystick.GetVJDStatus(i);
switch (status)
{
case VjdStat.VJD_STAT_FREE:
devices[i] = true;
break;
case VjdStat.VJD_STAT_OWN:
case VjdStat.VJD_STAT_BUSY:
case VjdStat.VJD_STAT_MISS:
case VjdStat.VJD_STAT_UNKN:
default:
devices[i] = false;
break;
}
}
}
return devices;
}
public long GetMaxForAxis(HID_USAGES Axis)
{
long maxval = 0;
if (bDeviceAcquired)
{
m_joystick.GetVJDAxisMax(m_vJoyID, Axis, ref maxval);
}
return maxval;
}
public long GetMinForAxis(HID_USAGES Axis)
{
long minval = 0;
if (bDeviceAcquired)
{
m_joystick.GetVJDAxisMin(m_vJoyID, Axis, ref minval);
}
return minval;
}
public int GetButtonCount(uint Index)
{
return m_joystick.GetVJDButtonNumber(Index);
}
public List<HID_USAGES> GetExistingAxes(uint Index)
{
List<HID_USAGES> availableAxes = new List<HID_USAGES>();
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_X)) availableAxes.Add(HID_USAGES.HID_USAGE_X);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_Y)) availableAxes.Add(HID_USAGES.HID_USAGE_Y);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_Z)) availableAxes.Add(HID_USAGES.HID_USAGE_Z);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_RX)) availableAxes.Add(HID_USAGES.HID_USAGE_RX);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_RY)) availableAxes.Add(HID_USAGES.HID_USAGE_RY);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_RZ)) availableAxes.Add(HID_USAGES.HID_USAGE_RZ);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_SL0)) availableAxes.Add(HID_USAGES.HID_USAGE_SL0);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_SL1)) availableAxes.Add(HID_USAGES.HID_USAGE_SL1);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_WHL)) availableAxes.Add(HID_USAGES.HID_USAGE_WHL);
if (m_joystick.GetVJDAxisExist(Index, HID_USAGES.HID_USAGE_POV)) availableAxes.Add(HID_USAGES.HID_USAGE_POV);
return availableAxes;
}
public AxisExtents GetAxisExtents(HID_USAGES Axis)
{
return new AxisExtents(GetMinForAxis(Axis), GetMaxForAxis(Axis));
}
public void SetButton(uint Index, bool Value)
{
if(bDeviceAcquired)
{
m_joystick.SetBtn(Value, m_vJoyID, Index);
}
}
public void SetAxis(HID_USAGES Axis, int Value)
{
if(bDeviceAcquired)
{
m_joystick.SetAxis(Value, m_vJoyID, Axis);
}
}
public static string AxisToFriendlyName(HID_USAGES Axis)
{
switch (Axis)
{
case HID_USAGES.HID_USAGE_X:
return "X";
case HID_USAGES.HID_USAGE_Y:
return "Y";
case HID_USAGES.HID_USAGE_Z:
return "Z";
case HID_USAGES.HID_USAGE_RX:
return "RX";
case HID_USAGES.HID_USAGE_RY:
return "RY";
case HID_USAGES.HID_USAGE_RZ:
return "RZ";
case HID_USAGES.HID_USAGE_SL0:
return "Slider";
case HID_USAGES.HID_USAGE_SL1:
return "Dial/Slider 2";
case HID_USAGES.HID_USAGE_WHL:
return "Wheel";
case HID_USAGES.HID_USAGE_POV:
return "PoV";
default:
return Axis.ToString();
}
}
}
}