uinput-mapper/mapper.py

63 lines
1.7 KiB
Python
Raw Normal View History

2013-05-18 11:39:00 -04:00
# encoding: utf-8
2013-04-21 06:42:51 -04:00
import cinput
# XXX: Also parse name, etc
2013-05-18 11:39:00 -04:00
def parse_conf(f, devname):
2013-04-21 06:42:51 -04:00
conf = {}
e = f.get_exposed_events()
for k, v in e.iteritems():
t = cinput.events[k]
if t == cinput.EV_SYN:
continue
2013-05-18 11:39:00 -04:00
conf[(devname, t)] = {}
2013-04-21 06:42:51 -04:00
for key in v:
tt = cinput.event_keys[t][key]
2013-05-18 11:39:00 -04:00
conf[(devname, t)][tt] = {
'type' : (devname, t),
2013-04-21 06:42:51 -04:00
'code' : tt,
2013-05-09 10:10:28 -04:00
'value' : None
#'value' : lambda x: x
2013-04-21 06:42:51 -04:00
}
return conf
2013-05-18 11:39:00 -04:00
def pretty_conf_print(c):
for k, v in c.iteritems():
print 'Input:', k[0], 'Type:', cinput.rev_events[k[1]]
for kk, vv in v.iteritems():
n_ev_d, n_ev_t = vv['type']
print ' ' * 4,
print cinput.rev_event_keys[k[1]][kk],
print ' → ([%d, %s], %s)' % (n_ev_d,
cinput.rev_events[n_ev_t],
cinput.rev_event_keys[n_ev_t][vv['code']])
2013-04-21 06:42:51 -04:00
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']
2013-05-09 10:10:28 -04:00
if info['value'] is not None:
ev.value = info['value'](ev.value)
else:
ev.value = ev.value
2013-04-21 06:42:51 -04:00
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'])