uinput-mapper/uinputmapper/mapper.py

120 lines
3.4 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
2013-05-18 12:32:44 -04:00
"""
Module to help out with config parsing and input mapping
"""
2013-05-18 11:39:00 -04:00
def parse_conf(f, devname):
2013-05-18 12:32:44 -04:00
"""
Reads in input devices and returns a config that contains all the events
exported by the device.
devname specificies the idx of this input device
"""
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
}
2013-05-19 07:07:59 -04:00
if t == cinput.EV_ABS:
p = f.get_absprop(tt)
conf[(devname, t)][tt]['prop'] = {
'max' : p.maximum,
'min' : p.minimum,
'fuzz' : p.fuzz,
'flat' : p.flat
}
2013-04-21 06:42:51 -04:00
return conf
2013-05-18 11:39:00 -04:00
def pretty_conf_print(c):
2013-05-18 12:32:44 -04:00
"""
Function to print an entire configuration
"""
2013-05-18 11:39:00 -04:00
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-05-19 07:07:59 -04:00
if n_ev_t == cinput.EV_ABS:
print 'Properties: Max: %d Min: %d Fuzz: %d Flat: %d' % (
vv['prop']['max'], vv['prop']['min'],
vv['prop']['fuzz'], vv['prop']['flat'])
def get_exported_device_count(c):
2013-05-18 12:32:44 -04:00
"""
Iterate dictionary to find out how many devices are exported.
(Rather simple at the moment)
"""
m = 0
for _, v in c.iteritems():
for _, o in v.iteritems():
m = max(m, o['type'][0])
return m + 1
2013-05-18 11:39:00 -04:00
2013-04-21 06:42:51 -04:00
class KeyMapper(object):
def __init__(self, config):
self._config = config
def map_event(self, ev, fd):
2013-05-18 12:32:44 -04:00
"""
Maps an event *ev* from fd *fd* to a possibly different event and output
fd *ofd*.
"""
2013-04-21 06:42:51 -04:00
_type = ev.type
2013-05-18 16:26:18 -04:00
ofd = fd
2013-05-18 16:25:17 -04:00
if (fd, _type) in self._config:
typemaps = self._config[(fd, _type)]
2013-04-21 06:42:51 -04:00
if ev.code in typemaps:
info = typemaps[ev.code]
ofd, ev.type = info['type']
2013-04-21 06:42:51 -04:00
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 ofd, ev
2013-04-21 06:42:51 -04:00
def expose(self, d, fd):
2013-05-18 12:32:44 -04:00
"""
Expose exposes events to a uinput-device *d* with index *fd* from the
config passed to __init__.
"""
for (n, evt), v in self._config.iteritems():
2013-04-21 06:42:51 -04:00
for code, dat in v.iteritems():
ofd, t = dat['type']
if ofd != fd:
continue
d.expose_event_type(t)
d.expose_event(t, dat['code'])
2013-05-19 07:07:59 -04:00
if t == cinput.EV_ABS:
p = dat['prop']
d.set_absprop(dat['code'], _max=p['max'], _min=p['min'],
fuzz=p['fuzz'], flat=p['flat'])