-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevices.py
47 lines (42 loc) · 1.41 KB
/
devices.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
class InputDevice():
def __init__(self):
self.name = ''
self.handler = ''
def __str__(self):
return '<Input Device: name=%s, handler=%s>' % (self.name, self.handler)
def setName(self, name):
if len(name) >= 2 and name.startswith('"') and name.endswith('"'):
name = name[1:len(name)-1]
self.name = name
def setHandler(self, handlers):
for handler in handlers.split(' '):
if handler.startswith('event'):
self.handler = handler
def listDevices():
devices = []
with open('/proc/bus/input/devices', 'r') as f:
device = None
while True:
s = f.readline()
if s == '':
break
s = s.strip()
if s == '':
devices.append(device)
device = None
else:
if device is None:
device = InputDevice()
if s.startswith('N: Name='):
device.setName(s[8:])
elif s.startswith('H: Handlers='):
device.setHandler(s[12:])
return devices
def detectJoystick(joystickNames):
for device in listDevices():
for joystickName in joystickNames:
if joystickName in device.name:
# 返回输入文件:
return '/dev/input/%s' % device.handler
# 未找到返回None:
return None