uinput-mapper/input-read

108 lines
2.6 KiB
Plaintext
Raw Normal View History

2013-05-14 17:46:18 -04:00
#!/usr/bin/env python
2013-05-09 10:10:28 -04:00
import linux_uinput, ctypes, fcntl, os, sys
2013-05-18 11:39:00 -04:00
import select
2013-05-09 10:10:28 -04:00
from cinput import *
2013-05-19 07:07:59 -04:00
from mapper import KeyMapper, parse_conf, pretty_conf_print
2013-05-09 10:10:28 -04:00
try:
import cPickle as pickle
except ImportError:
import pickle
import optparse
2013-05-09 10:10:28 -04:00
2013-05-14 17:46:18 -04:00
_usage = 'input-read /dev/input/event<0> ... /dev/input/event<N>'
2013-05-14 15:30:03 -04:00
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')
2013-05-19 07:07:59 -04:00
parser.add_option('-v', '--verbose', action='store_true',
default=False, help='Enable verbose mode (do not combine with -D)')
2013-05-09 10:10:28 -04:00
parser.add_option('-C', '--compat', action='store_true',
help='Enable compatibility mode; for Python < 2.7')
2013-05-09 10:10:28 -04:00
2013-05-10 11:33:25 -04:00
args, input_file = parser.parse_args()
2013-05-10 11:33:25 -04:00
if len(input_file) == 0:
parser.print_help()
exit(0)
2013-05-09 10:10:28 -04:00
2013-05-18 12:32:44 -04:00
# Open input devices
2013-05-18 11:39:00 -04:00
fs = map(InputDevice, input_file)
2013-05-18 12:32:44 -04:00
# Create configuration
2013-05-18 11:39:00 -04:00
config = {}
for idx, f in enumerate(fs):
c = parse_conf(f, idx)
config.update(c)
2013-05-19 07:07:59 -04:00
if args.verbose:
pretty_conf_print(config)
poll_obj, poll_mask = (select.poll, select.POLLIN) if args.compat else \
(select.epoll, select.EPOLLIN)
2013-05-18 12:32:44 -04:00
# Add all devices to epoll
pp = poll_obj()
2013-05-18 11:39:00 -04:00
for f in fs:
pp.register(f.get_fd(), poll_mask)
2013-05-18 12:32:44 -04:00
# Human readable info
if args.dump:
2013-05-18 11:39:00 -04:00
for f in fs:
print 'Version:', f.get_version()
print f.get_name()
2013-05-18 11:39:00 -04:00
d = f.get_exposed_events()
for k, v in d.iteritems():
print k + ':', ', '.join(v)
else:
2013-05-18 12:32:44 -04:00
# Dump initial information over pickle to stdout
p = pickle.Pickler(sys.stdout)
2013-05-18 11:39:00 -04:00
p.dump(len(fs))
p.dump(config)
2013-05-09 10:10:28 -04:00
sys.stdout.flush()
while True:
events = pp.poll()
2013-05-09 10:10:28 -04:00
2013-05-18 11:39:00 -04:00
for e in events:
fd, ev_mask = e
if not ev_mask & poll_mask:
2013-05-18 11:39:00 -04:00
continue
2013-05-18 12:32:44 -04:00
# Lets undo that epoll speedup ;-) FIXME XXX
2013-05-18 11:39:00 -04:00
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
2013-05-18 12:32:44 -04:00
s = '%s %s %d' % (rev_events[ev.type],
rev_event_keys[ev.type][ev.code], ev.value)
2013-05-18 11:39:00 -04:00
print 'Event type:', s
except KeyError:
pass
else:
if not args.compat:
p.dump((i, ev))
else:
2013-05-18 12:32:44 -04:00
p.dump((i, (ev.time.tv_sec, ev.time.tv_usec,
ev.type, ev.code, ev.value)))
2013-05-18 11:39:00 -04:00
sys.stdout.flush()