Improve mapping and config support.

This commit is contained in:
Merlijn Wajer 2013-04-21 12:42:51 +02:00
parent 9d21419deb
commit d09d6011d8
2 changed files with 58 additions and 61 deletions

View File

@ -2,85 +2,39 @@ import linux_uinput, ctypes, fcntl, os, sys
from cinput import *
clone = True
from mapper import KeyMapper, parse_conf
overrule = lambda x: -x*2
passthrough = lambda x: x
config = {
EV_REL : {
REL_X : {
'type' : EV_REL,
'code' : REL_X,
'value': overrule
},
REL_Y : {
'type': EV_REL,
'code': REL_Y,
'value' : overrule
}
},
EV_KEY : {
BTN_LEFT : {
'type' : EV_KEY,
'code' : BTN_LEFT,
'value' : passthrough
}
}
}
from example_conf import config
clone = False
f = InputDevice(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
d = UInputDevice()
if clone:
e = f.get_exposed_events()
for k, v in e.iteritems():
t = events[k]
if t == EV_SYN:
continue
d.expose_event_type(t)
for key in v:
tt = event_keys[t][key]
d.expose_event(t, tt)
conf = parse_conf(f)
m = KeyMapper(conf)
print k + ':', ', '.join(v)
else:
for evt, v in config.iteritems():
for code, dat in v.iteritems():
d.expose_event_type(dat['type'])
d.expose_event(dat['type'], dat['code'])
m = KeyMapper(config)
m.expose(d)
d.setup('Example input device' )
def map_ev(ev):
_type = ev.type
if _type in config:
typemaps = config[_type]
if ev.code in typemaps:
info = typemaps[ev.code]
ev.type = info['type']
ev.code = info['code']
ev.value = info['value'](ev.value)
return ev
while True:
ev = f.next_event()
if not clone:
ev = map_ev(ev)
ev = m.map_event(ev)
d.fire_event(ev)
try:
#print 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
#try:
# print 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
#except KeyError:
# pass

43
py/mapper.py Normal file
View File

@ -0,0 +1,43 @@
import cinput
def parse_conf(f):
conf = {}
e = f.get_exposed_events()
for k, v in e.iteritems():
t = cinput.events[k]
if t == cinput.EV_SYN:
continue
conf[t] = {}
for key in v:
tt = cinput.event_keys[t][key]
conf[t][tt] = {
'type' : t,
'code' : tt,
'value' : lambda x: x
}
return conf
class KeyMapper(object):
def __init__(self, config):
self._config = config
def map_event(self, ev):
_type = ev.type
if _type in self._config:
typemaps = self._config[_type]
if ev.code in typemaps:
info = typemaps[ev.code]
ev.type = info['type']
ev.code = info['code']
ev.value = info['value'](ev.value)
return ev
def expose(self, d):
for 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'])