-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairgamepad.py
executable file
·76 lines (66 loc) · 2.02 KB
/
airgamepad.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
from __future__ import print_function
import uinput
from uinput.ev import *
from select import select
from evdev import ecodes, InputDevice
air_kbd = "/dev/input/by-id/usb-SAGE_SAGE_AirMouse-event-kbd"
air_mouse = "/dev/input/by-id/usb-SAGE_SAGE_AirMouse-if01-event-mouse"
events = {
BTN_SOUTH,
BTN_EAST,
BTN_NORTH,
BTN_WEST,
BTN_TL,
BTN_TR,
BTN_SELECT,
BTN_START,
BTN_MODE,
BTN_THUMBL,
BTN_THUMBR,
ABS_X + (0, 1024, 0, 0),
ABS_Y + (0, 1024, 0, 0)
}
keymap = {
KEY_UP: BTN_SOUTH,
KEY_DOWN: BTN_EAST,
KEY_LEFT: BTN_NORTH,
KEY_RIGHT: BTN_WEST,
BTN_LEFT: BTN_TL,
BTN_RIGHT: BTN_TR,
KEY_PLAYPAUSE: BTN_SELECT,
KEY_ENTER: BTN_START,
KEY_ESC: BTN_MODE
}
def main():
joy = uinput.Device(events,
name="Microsoft X-Box 360 pad",
bustype=0x03,
vendor=0x45e,
product=0x28e,
version=0x110
)
joy.emit(ABS_X, 512)
joy.emit(ABS_Y, 512)
devices = map(InputDevice, (air_kbd, air_mouse))
devices = {dev.fd: dev for dev in devices}
for d in devices.values():
d.grab()
print('Listening for events ...\n')
while True:
r, w, e = select(devices, [], [])
for d in r:
for ev in devices[d].read():
if ev.type == ecodes.EV_KEY and ev.value != 2:
k = (ev.type, ev.code)
if k in keymap.keys():
joy.emit(keymap[k], ev.value)
elif k == KEY_VOLUMEUP:
joy.emit(ABS_Y, 0 if ev.value else 512)
elif k == KEY_VOLUMEDOWN:
joy.emit(ABS_Y, 1024 if ev.value else 512)
elif k == KEY_PREVIOUSSONG:
joy.emit(ABS_X, 0 if ev.value else 512)
elif k == KEY_NEXTSONG:
joy.emit(ABS_X, 1024 if ev.value else 512)
if __name__ == '__main__':
main()