forked from tidzo/pyvjoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_sdk.py
executable file
·423 lines (322 loc) · 10.6 KB
/
_sdk.py
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
import os
import sys
from ctypes import *
from pyvjoy.constants import *
from pyvjoy.exceptions import *
from ctypes import wintypes # Makes this lib work in Python36
dll_path = os.path.dirname(__file__) + os.sep + DLL_FILENAME
try:
_vj = cdll.LoadLibrary(dll_path)
except OSError:
sys.exit("Unable to load vJoy SDK DLL. Ensure that %s is present" % DLL_FILENAME)
def vJoyEnabled():
"""Returns True if vJoy is installed and enabled"""
result = _vj.vJoyEnabled()
if result == 0:
raise vJoyNotEnabledException()
else:
return True
def DriverMatch():
"""Check if the version of vJoyInterface.dll and the vJoy Driver match"""
result = _vj.DriverMatch()
if result == 0:
raise vJoyDriverMismatch()
else:
return True
def GetVJDStatus(rID):
"""Get the status of a given vJoy Device"""
return _vj.GetVJDStatus(rID)
def AcquireVJD(rID):
"""Attempt to acquire a vJoy Device"""
result = _vj.AcquireVJD(rID)
if result == 0:
#Check status
status = GetVJDStatus(rID)
if status != VJD_STAT_FREE:
raise vJoyFailedToAcquireException("Cannot acquire vJoy Device because it is not in VJD_STAT_FREE")
else:
raise vJoyFailedToAcquireException()
else:
return True
def RelinquishVJD(rID):
"""Relinquish control of a vJoy Device"""
result = _vj.RelinquishVJD(rID)
if result == 0:
raise vJoyFailedToRelinquishException()
else:
FfbRemoveCB(rID) # Also delete FFB callback if present
return True
def SetBtn(state,rID,buttonID):
"""Sets the state of a vJoy Button to on or off. SetBtn(state,rID,buttonID)"""
result = _vj.SetBtn(state,rID,buttonID)
if result == 0:
raise vJoyButtonException()
else:
return True
def SetAxis(AxisValue,rID,AxisID):
"""Sets the value of a vJoy Axis SetAxis(value,rID,AxisID)"""
#TODO validate AxisID
#TODO validate AxisValue
result = _vj.SetAxis(AxisValue,rID,AxisID)
if result == 0:
#TODO raise specific exception
raise vJoyException()
else:
return True
def SetDiscPov(PovValue, rID, PovID):
"""Write Value to a given discrete POV defined in the specified VDJ"""
if PovValue < -1 or PovValue > 3:
raise vJoyInvalidPovValueException()
if PovID < 1 or PovID > 4:
raise vJoyInvalidPovIDException
return _vj.SetDiscPov(PovValue,rID,PovID)
def SetContPov(PovValue, rID, PovID):
"""Write Value to a given continuous POV defined in the specified VDJ"""
if PovValue < -1 or PovValue > 35999:
raise vJoyInvalidPovValueException()
if PovID < 1 or PovID > 4:
raise vJoyInvalidPovIDException
return _vj.SetContPov(PovValue,rID,PovID)
def ResetVJD(rID):
"""Reset all axes and buttons to default for specified vJoy Device"""
return _vj.ResetVJD(rID)
def ResetButtons(rID):
"""Reset all buttons to default for specified vJoy Device"""
return _vj.ResetButtons(rID)
def ResetPovs(rID):
"""Reset all POV hats to default for specified vJoy Device"""
return _vj.ResetPovs(rID)
def UpdateVJD(rID, data):
"""Pass data for all buttons and axes to vJoy Device efficiently"""
return _vj.UpdateVJD(rID, pointer(data))
def CreateDataStructure(rID):
data = _JOYSTICK_POSITION_V2()
data.set_defaults(rID)
return data
class _JOYSTICK_POSITION_V2(Structure):
_fields_ = [
('bDevice', c_byte),
('wThrottle', c_long),
('wRudder', c_long),
('wAileron', c_long),
('wAxisX', c_long),
('wAxisY', c_long),
('wAxisZ', c_long),
('wAxisXRot', c_long),
('wAxisYRot', c_long),
('wAxisZRot', c_long),
('wSlider', c_long),
('wDial', c_long),
('wWheel', c_long),
('wAxisVX', c_long),
('wAxisVY', c_long),
('wAxisVZ', c_long),
('wAxisVBRX', c_long),
('wAxisVRBY', c_long),
('wAxisVRBZ', c_long),
('lButtons', c_long), # 32 buttons: 0x00000001 means button1 is pressed, 0x80000000 -> button32 is pressed
('bHats', wintypes.DWORD ), # Lower 4 bits: HAT switch or 16-bit of continuous HAT switch
('bHatsEx1', wintypes.DWORD ), # Lower 4 bits: HAT switch or 16-bit of continuous HAT switch
('bHatsEx2', wintypes.DWORD ), # Lower 4 bits: HAT switch or 16-bit of continuous HAT switch
('bHatsEx3', wintypes.DWORD ), # Lower 4 bits: HAT switch or 16-bit of continuous HAT switch LONG lButtonsEx1
# JOYSTICK_POSITION_V2 Extension
('lButtonsEx1', c_long), # Buttons 33-64
('lButtonsEx2', c_long), # Buttons 65-96
('lButtonsEx3', c_long), # Buttons 97-128
]
def set_defaults(self, rID):
self.bDevice=c_byte(rID)
self.bHats=-1
# FFB:
class PacketStruct(Structure):
def to_dict(self):
return dict((field, getattr(self, field)) for field, _ in self._fields_ if field)
def keys(self):
return [field for field, _ in self._fields_ if field]
def __getitem__(self,key):
return getattr(self, key)
def values(self):
return [getattr(self, field) for field, _ in self._fields_ if field]
def __str__(self):
return str(self.to_dict())
class _FFB_DATA(Structure):
_fields_ = [
('size', c_ulong),
('cmd', c_ulong),
('data', c_void_p),
]
class _FFB_EFFECT(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('EffectType',c_uint),
('Duration',c_uint16),
('TriggerRpt',c_uint16),
('SamplePrd',c_uint16),
('StartDelay',c_uint16),
('Gain',c_ubyte),
('TriggerBtn',c_ubyte),
('Polar',c_ubyte), # Axes enable or direction enable (Bit 3)
('',c_ubyte), # Reserved padding
('',c_uint32), # Reserved padding TODO Seems to contain data
('DirX',c_uint16), # Polar direction or dirX depending on Polar.
('DirY',c_uint16),
]
class _FFB_EFF_RAMP(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('Start',c_int16),
('',c_int16), # Reserved padding
('End',c_int16),
]
class _FFB_EFF_OP(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('EffectOp',c_uint32),
('LoopCount',c_uint32),
]
class _FFB_EFF_PERIOD(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('Magnitude',c_uint32),
('Offset',c_int16),
('',c_int16), # Padding
('Phase',c_uint32),
('Period',c_uint32),
]
class _FFB_EFF_COND(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('isY',c_uint32),
('CenterPointOffset',c_int16),
('',c_int16), # Padding
('PosCoeff',c_int16),
('',c_int16), # Padding
('NegCoeff',c_int16),
('',c_int16), # Padding
('PosSatur',c_uint32),
('NegSatur',c_uint32),
('DeadBand',c_int32),
]
class _FFB_EFF_ENVLP(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('AttackLevel',c_uint32),
('FadeLevel',c_uint32),
('AttackTime',c_uint32),
('FadeTime',c_uint32),
]
class _FFB_EFF_CONST(PacketStruct):
_pack_ = 1
_fields_ = [
('EffectBlockIndex',c_uint32),
('Magnitude',c_int16),
]
class FFBCallback():
"""Helper class for FFB callbacks between python and vjoy"""
vJoy_ffb_callback = None # Workaround to store callback functions
def __init__(self):
self.callbacks = {}
self.internalcbtype = CFUNCTYPE(None,_FFB_DATA, c_void_p)
# Callback can not be a member function
def ffbCallback(ffbpacket,userdata):
"""Helper callback passed to vjoy. Will call previously registered python function with parsed FFB data"""
parsedData,reptype,devid = FFBCallback._parse_ffb_packet(ffbpacket)
if parsedData and (devid in self.callbacks):
# packet,typename = self.packet_to_dict(reptype,parsedData)
self.callbacks[devid](parsedData,reptype)
self._internalcb = self.internalcbtype(ffbCallback)
def addCallback(self,callback,rID):
"""Add callback to rID device. Gets called when FFB data for rID is received"""
self.callbacks[rID] = callback
def removeCallback(self,rID):
"""Remove rID from internal callback dict"""
if rID in self.callbacks:
del self.callbacks[rID]
@staticmethod
def _parse_ffb_packet(ffbpacket : _FFB_DATA):
"""Helper function parse ffb data using vjoy functions"""
t = c_int(0)
res = _vj.Ffb_h_Type(ffbpacket, pointer(t))
reptype = t.value
if res != 0: # Invalid packet
return None,0,0
devid = c_int(0)
_vj.Ffb_h_DeviceID(ffbpacket,pointer(devid)) # ID of vjoy device
parsedPacket = None
# Parse report type
if reptype == PT_CTRLREP: # Control rep
ctrl = c_int(0)
if _vj.Ffb_h_DevCtrl(ffbpacket,pointer(ctrl)) == 0:
parsedPacket = ctrl.value
elif reptype == PT_EFFREP: # Set effect rep
tstruct = _FFB_EFFECT()
if _vj.Ffb_h_Eff_Report(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_RAMPREP: # Ramp rep
tstruct = _FFB_EFF_RAMP()
if _vj.Ffb_h_Eff_Ramp(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_EFOPREP: # EffOp rep
tstruct = _FFB_EFF_OP()
if _vj.Ffb_h_EffOp(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_PRIDREP: # EffPeriod rep
tstruct = _FFB_EFF_PERIOD()
if _vj.Ffb_h_Eff_Period(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_CONDREP: # Conditional rep
tstruct = _FFB_EFF_COND()
if _vj.Ffb_h_Eff_Cond(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_ENVREP: # Envelope rep
tstruct = _FFB_EFF_ENVLP()
if _vj.Ffb_h_Eff_Envlp(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_NEWEFREP: # NewEff rep
neweff = c_int(0)
if _vj.Ffb_h_EffNew(ffbpacket,pointer(neweff)) == 0:
parsedPacket = neweff.value
elif reptype == PT_CONSTREP: # Constant force rep
tstruct = _FFB_EFF_CONST()
if _vj.Ffb_h_Eff_Constant(ffbpacket,pointer(tstruct)) == 0:
parsedPacket = tstruct
elif reptype == PT_GAINREP: # Gain rep
gainrep = c_int(0)
if _vj.Ffb_h_DevGain(ffbpacket,pointer(gainrep)) == 0:
parsedPacket = gainrep.value
elif reptype == PT_BLKFRREP: # Block free rep
blk = c_int(0)
if _vj.Ffb_h_EBI(ffbpacket,pointer(blk)) == 0:
parsedPacket = blk.value
return parsedPacket,reptype,devid.value
def getCcallback(self):
"""Helper function returning the external C-type callback"""
return self._internalcb
def FfbRegisterGenCB(func,rID):
"""Registers a python FFB callback and translates packets"""
if not FFBCallback.vJoy_ffb_callback:
FFBCallback.vJoy_ffb_callback = FFBCallback()
FFBCallback.vJoy_ffb_callback.addCallback(func,rID)
devid = c_int(rID)
_vj.FfbRegisterGenCB(FFBCallback.vJoy_ffb_callback.getCcallback(),pointer(devid))
def FfbRemoveCB(rID):
"""Removes a callback from the helper class"""
if FFBCallback.vJoy_ffb_callback:
FFBCallback.vJoy_ffb_callback.removeCallback(rID)
def vJoyFfbCap():
"""Returns True if vjoy is FFB capable"""
ret = c_bool(False)
_vj.vJoyFfbCap(pointer(ret))
return ret.value
def IsDeviceFfb(rID):
"""Returns True if device is FFB capable"""
return _vj.IsDeviceFfb(rID) != 0
def IsDeviceFfbEffect(rID, effect):
"""Returns True if device supports effect usage type"""
return _vj.IsDeviceFfbEffect(rID,effect) != 0