Initial mapping/creating multiple devices.

This commit is contained in:
Merlijn Wajer 2013-05-18 18:10:19 +02:00
parent 57065a7319
commit 72dde4af77
3 changed files with 50 additions and 23 deletions

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python
import linux_uinput, ctypes, fcntl, os, sys
from cinput import *
from mapper import KeyMapper, parse_conf
from mapper import KeyMapper, parse_conf, pretty_conf_print, \
get_exported_device_count
from linux_input import timeval, input_event
try:
@ -23,29 +24,43 @@ parser.add_option('-C', '--compat', action='store_true',
args, cfg = parser.parse_args()
# Unpickle from stdin ; currently this is the default and only way
f = pickle.Unpickler(sys.stdin)
in_f = pickle.Unpickler(sys.stdin)
conf = f.load()
nifd = in_f.load()
print 'Got', nifd, 'inputs'
conf = in_f.load()
for path in cfg:
config_merge = imp.load_source('', path).config_merge
config_merge(conf)
pretty_conf_print(conf)
m = KeyMapper(conf)
d = UInputDevice()
m.expose(d)
d.setup('Example input device')
# TODO: Get amount of uinput devices to export
nofd = get_exported_device_count(conf)
ofs = []
for f in xrange(nofd):
d = UInputDevice()
m.expose(d, f)
d.setup('Example input device')
ofs.append(d)
while True:
if not args.compat:
ev = f.load()
fd, ev = in_f.load()
else:
ev_p = f.load()
fd, ev_p = in_f.load()
ti = timeval(ev_p[0], ev_p[1])
ev = input_event(ti, ev_p[2], ev_p[3], ev_p[4])
ev = m.map_event(ev)
idx, ev = m.map_event(ev, fd)
d = ofs[idx]
d.fire_event(ev)

View File

@ -3,7 +3,7 @@ import linux_uinput, ctypes, fcntl, os, sys
import select
from cinput import *
from mapper import KeyMapper, parse_conf, pretty_conf_print
from mapper import KeyMapper, parse_conf
try:
import cPickle as pickle
@ -37,11 +37,9 @@ for idx, f in enumerate(fs):
config.update(c)
pretty_conf_print(config)
p = select.epoll()
pp = select.epoll()
for f in fs:
p.register(f.get_fd(), select.EPOLLIN)
pp.register(f.get_fd(), select.EPOLLIN)
if args.dump:
for f in fs:
@ -63,7 +61,7 @@ else:
i = 0
while True:
events = p.poll()
events = pp.poll()
for e in events:
fd, ev_mask = e

View File

@ -35,28 +35,42 @@ def pretty_conf_print(c):
cinput.rev_events[n_ev_t],
cinput.rev_event_keys[n_ev_t][vv['code']])
def get_exported_device_count(c):
m = 0
for _, v in c.iteritems():
for _, o in v.iteritems():
m = max(m, o['type'][0])
return m + 1
class KeyMapper(object):
def __init__(self, config):
self._config = config
def map_event(self, ev):
def map_event(self, ev, fd):
_type = ev.type
if _type in self._config:
typemaps = self._config[_type]
typemaps = self._config[(fd, _type)]
if ev.code in typemaps:
info = typemaps[ev.code]
ev.type = info['type']
ofd, ev.type = info['type']
ev.code = info['code']
if info['value'] is not None:
ev.value = info['value'](ev.value)
else:
ev.value = ev.value
else:
ofd = fd
return ev
return ofd, ev
def expose(self, d):
for evt, v in self._config.iteritems():
def expose(self, d, fd):
for (n, evt), v in self._config.iteritems():
for code, dat in v.iteritems():
d.expose_event_type(dat['type'])
d.expose_event(dat['type'], dat['code'])
ofd, t = dat['type']
if ofd != fd:
continue
d.expose_event_type(t)
d.expose_event(t, dat['code'])