forked from tidzo/pyvjoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_sdk.py
executable file
·195 lines (135 loc) · 4.44 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
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:
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 SetBtn(state,rID,buttonID):
"""Sets the state of 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 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