-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinput-read
executable file
·124 lines (94 loc) · 2.94 KB
/
input-read
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
#!/usr/bin/env python
import ctypes, fcntl, os, sys
import select
import uinputmapper
import uinputmapper.linux_uinput
from uinputmapper.cinput import *
from uinputmapper.mapper import KeyMapper, parse_conf, pretty_conf_print
try:
import cPickle as pickle
except ImportError:
import pickle
import optparse
_usage = 'input-read /dev/input/event<0> ... /dev/input/event<N>'
parser = optparse.OptionParser(description='Read input devices.',
usage = _usage,
version='0.01')
parser.add_option('-D', '--dump', action='store_false',
default=True, help='Dump will marshall all the events to stdout')
parser.add_option('-v', '--verbose', action='store_true',
default=False, help='Enable verbose mode (do not combine with -D)')
parser.add_option('-G', '--grab', action='append',
type=str, default=[])
parser.add_option('-C', '--compat', action='store_true',
help='Enable compatibility mode; for Python < 2.7')
args, input_file = parser.parse_args()
if len(input_file) + len(args.grab) == 0:
parser.print_help()
exit(0)
# Open input devices
fs = map(InputDevice, input_file)
# Open devices in grab mode
fsg = map(InputDevice, args.grab)
# Grab devices
for _ in fsg:
_.grab()
# Combine grabbed and not-grabbed devices
fs += fsg
# Create configuration
config = {}
names = {}
for idx, f in enumerate(fs):
c = parse_conf(f, idx)
names[idx] = f.get_name()
config.update(c)
if args.verbose:
pretty_conf_print(config)
poll_obj, poll_mask = (select.poll, select.POLLIN) if args.compat else \
(select.epoll, select.EPOLLIN)
# Add all devices to epoll
pp = poll_obj()
for f in fs:
pp.register(f.get_fd(), poll_mask)
# Human readable info
if args.dump:
for f in fs:
print 'Version:', f.get_version()
print f.get_name()
d = f.get_exposed_events()
for k, v in d.iteritems():
print k + ':', ', '.join(v)
else:
# Dump initial information over pickle to stdout
p = pickle.Pickler(sys.stdout)
p.dump(len(fs))
p.dump(config)
p.dump(names)
sys.stdout.flush()
while True:
events = pp.poll()
for e in events:
fd, ev_mask = e
if not ev_mask & poll_mask:
continue
# Lets undo that epoll speedup ;-) FIXME XXX
for idx, _ in enumerate(fs):
if _.get_fd() == fd:
f = _
i = idx
ev = f.next_event()
if args.dump:
try:
print i, ev.time.tv_sec, ev.time.tv_usec
s = '%s %s %d' % (rev_events[ev.type],
rev_event_keys[ev.type][ev.code], ev.value)
print 'Event type:', s
except KeyError:
pass
else:
if not args.compat:
p.dump((i, ev))
else:
p.dump((i, (ev.time.tv_sec, ev.time.tv_usec,
ev.type, ev.code, ev.value)))
sys.stdout.flush()