-
Notifications
You must be signed in to change notification settings - Fork 0
/
xinput.py
313 lines (292 loc) · 11.2 KB
/
xinput.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
import ctypes
from enum import Enum
from time import sleep
from time import time as time_now
# XINPUT ACCESS #
# Constants
NOT_CONNECTED = 1167
UBYTE = 255
CSHORT = 32767
USHORT = 65535
# Structures
class XINPUTGAMEPAD(ctypes.Structure):
_fields_ = [("wButtons", ctypes.c_ushort),
("bLeftTrigger", ctypes.c_ubyte),
("bRightTrigger", ctypes.c_ubyte),
("sThumbLX", ctypes.c_short),
("sThumbLY", ctypes.c_short),
("sThumbRX", ctypes.c_short),
("sThumbRY", ctypes.c_short)]
class XINPUTSTATE(ctypes.Structure):
_fields_ = [("dwPacketNumber", ctypes.c_uint32),
("Gamepad", XINPUTGAMEPAD)]
class XINPUTVIBRATION(ctypes.Structure):
_fields_ = [("wLeftMotorSpeed", ctypes.c_ushort),
("wRightMotorSpeed", ctypes.c_ushort)]
# load Xinput.dll
try:
_xinput = ctypes.windll.xinput9_1_0
except:
raise ImportError
# getState
_XInputGetState = _xinput.XInputGetState
_XInputGetState.argtypes = [ctypes.c_uint, ctypes.POINTER(XINPUTSTATE)]
_XInputGetState.resttype = ctypes.c_uint
# setVibration
_XInputSetState = _xinput.XInputSetState
_XInputSetState.argtypes = [ctypes.c_uint, ctypes.POINTER(XINPUTVIBRATION)]
_XInputSetState.restype = ctypes.c_uint
class AnalogStick:
def __init__(self, raw_x, raw_y, deadzone):
self.raw_x = raw_x
self.raw_y = raw_y
axis_x = raw_x / CSHORT
axis_y = raw_y / CSHORT
magnitude = (axis_x ** 2 + axis_y ** 2) ** 0.5
if magnitude > deadzone:
dz_factor = (magnitude - deadzone) / (1 - deadzone)
self.x = axis_x / magnitude * dz_factor
self.y = axis_y / magnitude * dz_factor
self.magnitude = magnitude
else:
self.x = 0
self.y = 0
self.magnitude = 0
class Gamepad(Enum):
gamepad_0 = 0
gamepad_1 = 1
gamepad_2 = 2
gamepad_3 = 3
@staticmethod
def update():
t_now = time_now()
state = XINPUTSTATE()
for gamepad in Gamepad:
if not gamepad.disabled:
if gamepad.connected:
gamepad.get_state(state)
elif t_now - gamepad.last_update > 5:
print("XGamepad checking up on {}".format(gamepad))
gamepad.get_state(state)
def __init__(self, value):
# hardware info
self._raw_id = value
self.disabled = False
self.connected = False
self.deadzone = 0.3
self.last_update = 0
self.input_state = None
self._reset_input()
# vibration
self.vibration_left = 0
self.vibration_right = 0
def __repr__(self):
return "XGamepad:gamepad{}".format(self._raw_id)
def __str__(self):
if self.connected:
tmp = "connected"
else:
tmp = "not connected"
return "gamepad{}({})".format(self._raw_id, tmp)
def _reset_input(self):
self.input_state = {
"analog_right": AnalogStick(0, 0, self.deadzone),
"analog_left": AnalogStick(0, 0, self.deadzone),
"trigger_right": 0,
"trigger_left": 0,
"shoulder_right": False,
"shoulder_left": False,
"button_start": False,
"button_back": False,
"stick_right": False,
"stick_left": False,
"button_a": False,
"button_b": False,
"button_x": False,
"button_y": False,
"dpad_right": False,
"dpad_left": False,
"dpad_down": False,
"dpad_up": False,
"events": None
}
def set_vibration(self, left: float=0, right: float=0):
self.vibration_left = left
self.vibration_right = right
vibration = XINPUTVIBRATION(int(left * USHORT), int(right * USHORT))
_XInputSetState(self._raw_id, ctypes.byref(vibration))
def get_state(self, state=None):
if not state:
state = XINPUTSTATE()
self.last_update = time_now()
error_code = _XInputGetState(self._raw_id, state)
if error_code == NOT_CONNECTED:
if self.connected:
# update connection status
self.connected = False
# reset input fields
self._reset_input()
# disconnected!
print(self)
# update connection status
self.connected = False
else:
if not self.connected:
# update connection status
self.connected = True
# deactivate vibration
self.set_vibration()
# reconnected!
print(self)
# update state and stats
self.connected = True
gamepad = state.Gamepad
new_state = dict()
events = dict()
# analog_right
new_value = AnalogStick(gamepad.sThumbRX, gamepad.sThumbRY, self.deadzone)
old_value = self.input_state["analog_right"]
dx = new_value.x - old_value.x
dy = new_value.y - old_value.y
if abs(dx) > 0 or abs(dy) > 0:
events["analog_right"] = (dx, dy)
new_state["analog_right"] = new_value
# analog_left
new_value = AnalogStick(gamepad.sThumbLX, gamepad.sThumbLY, self.deadzone)
old_value = self.input_state["analog_left"]
dx = new_value.x - old_value.x
dy = new_value.y - old_value.y
if abs(dx) > 0 or abs(dy) > 0:
events["analog_left"] = (dx, dy)
new_state["analog_left"] = new_value
# trigger_right
new_value = gamepad.bRightTrigger/UBYTE
old_value = self.input_state["trigger_right"]
delta = new_value - old_value
if abs(delta) > 0:
events["trigger_right"] = delta
new_state["trigger_right"] = new_value
# trigger_left
new_value = gamepad.bLeftTrigger/UBYTE
old_value = self.input_state["trigger_left"]
delta = new_value - old_value
if abs(delta) > 0:
events["trigger_left"] = delta
new_state["trigger_left"] = new_value
def get_button(mask):
return gamepad.wButtons & mask is not 0
# dpad_up
new_value = get_button(0x0001)
old_value = self.input_state["dpad_up"]
if new_value and not old_value:
events["dpad_up"] = True
elif old_value and not new_value:
events["dpad_up"] = False
new_state["dpad_up"] = new_value
# dpad_down
new_value = get_button(0x0002)
old_value = self.input_state["dpad_down"]
if new_value and not old_value:
events["dpad_down"] = True
elif old_value and not new_value:
events["dpad_down"] = False
new_state["dpad_down"] = new_value
# dpad_left
new_value = get_button(0x0004)
old_value = self.input_state["dpad_left"]
if new_value and not old_value:
events["dpad_left"] = True
elif old_value and not new_value:
events["dpad_left"] = False
new_state["dpad_left"] = new_value
# dpad_right
new_value = get_button(0x0008)
old_value = self.input_state["dpad_right"]
if new_value and not old_value:
events["dpad_right"] = True
elif old_value and not new_value:
events["dpad_right"] = False
new_state["dpad_right"] = new_value
# button_start
new_value = get_button(0x0010)
old_value = self.input_state["button_start"]
if new_value and not old_value:
events["button_start"] = True
elif old_value and not new_value:
events["button_start"] = False
new_state["button_start"] = new_value
# button_back
new_value = get_button(0x0020)
old_value = self.input_state["button_back"]
if new_value and not old_value:
events["button_back"] = True
elif old_value and not new_value:
events["button_back"] = False
new_state["button_back"] = new_value
# stick_left
new_value = get_button(0x0040)
old_value = self.input_state["stick_left"]
if new_value and not old_value:
events["stick_left"] = True
elif old_value and not new_value:
events["stick_left"] = False
new_state["stick_left"] = new_value
# stick_right
new_value = get_button(0x0080)
old_value = self.input_state["stick_right"]
if new_value and not old_value:
events["stick_right"] = True
elif old_value and not new_value:
events["stick_right"] = False
new_state["stick_right"] = new_value
# shoulder_left
new_value = get_button(0x0100)
old_value = self.input_state["shoulder_left"]
if new_value and not old_value:
events["shoulder_left"] = True
elif old_value and not new_value:
events["shoulder_left"] = False
new_state["shoulder_left"] = new_value
# shoulder_right
new_value = get_button(0x0200)
old_value = self.input_state["shoulder_right"]
if new_value and not old_value:
events["shoulder_right"] = True
elif old_value and not new_value:
events["shoulder_right"] = False
new_state["shoulder_right"] = new_value
# button_a
new_value = get_button(0x1000)
old_value = self.input_state["button_a"]
if new_value and not old_value:
events["button_a"] = True
elif old_value and not new_value:
events["button_a"] = False
new_state["button_a"] = new_value
# button_b
new_value = get_button(0x2000)
old_value = self.input_state["button_b"]
if new_value and not old_value:
events["button_b"] = True
elif old_value and not new_value:
events["button_b"] = False
new_state["button_b"] = new_value
# button_x
new_value = get_button(0x4000)
old_value = self.input_state["button_x"]
if new_value and not old_value:
events["button_x"] = True
elif old_value and not new_value:
events["button_x"] = False
new_state["button_x"] = new_value
# button_y
new_value = get_button(0x8000)
old_value = self.input_state["button_y"]
if new_value and not old_value:
events["button_y"] = True
elif old_value and not new_value:
events["button_y"] = False
new_state["button_y"] = new_value
# inauguration
new_state["event"] = events
self.input_state = new_state