-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystickapi_linux.py
More file actions
62 lines (45 loc) · 1.68 KB
/
Copy pathjoystickapi_linux.py
File metadata and controls
62 lines (45 loc) · 1.68 KB
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
import evdev
import glob
def map_snes_xy(value):
if value < 32:
return -1
elif value > 196:
return 1
return 0
def map_snes_button(value):
if 288 <= value <= 295:
return 1 << (value - 288)
return 0
def enumerate_joysticks(devices, checked_paths):
if devices is None:
devices = []
if checked_paths is None:
checked_paths = set()
current_paths = set(glob.glob('/dev/input/event*'))
devices = [device for device in devices if device.path in current_paths]
new_paths = current_paths - checked_paths
if checked_paths:
not_ready_yet = set([p for p in new_paths if not evdev.util.is_device(p)])
current_paths = set([p for p in current_paths if p not in not_ready_yet])
for device in [evdev.InputDevice(path) for path in new_paths if evdev.util.is_device(path)]:
caps = device.capabilities()
if evdev.ecodes.EV_ABS in caps and \
evdev.ecodes.EV_KEY in caps and \
evdev.ecodes.BTN_JOYSTICK in caps[evdev.ecodes.EV_KEY]:
devices.append(device)
return devices, current_paths
def poll_joysticks(devices):
joysticks = {}
for device in devices:
key = device.path
try:
x = map_snes_xy(device.absinfo(evdev.ecodes.ABS_X).value)
y = map_snes_xy(device.absinfo(evdev.ecodes.ABS_Y).value)
buttons = 0
for code in device.active_keys():
buttons = buttons | map_snes_button(code)
except OSError as o:
devices.remove(device)
else:
joysticks[key] = (x, y, buttons)
return devices, joysticks