Add classes, more event constants.

- Added all event constants except for (FF_)
- Added InputDevice class.
- Cleaned up linux_input a bit
- list of events per event class is now a dictionary
This commit is contained in:
Merlijn Wajer 2013-04-20 11:54:17 +02:00
parent f6efe9f331
commit 9113cb02b7
4 changed files with 105 additions and 46 deletions

View File

@ -6,7 +6,7 @@ Generate:
- REL_*
- ABS_*
gcc -E -dM /usr/include/linux/input.h | egrep ' (KEY|BTN|EV|REL|ABS|SYN)_[A-Za-z0-9_]+' | ( echo "#include <linux/input.h>" ; echo "input_constants_dict = {" ; sed -r 's/[^ ]+ +([^ ]+).*/"\1" : \1,/' ; echo "}" ) | gcc -E -o /dev/stdout - | grep 'input_constants_dict = {' -A 100000 > gen.py
gcc -E -dM /usr/include/linux/input.h | egrep ' (EV|SYN|KEY|BTN|REL|ABS|MSC|LED|SND|REP|SW)_[A-Za-z0-9_]+' | ( echo "#include <linux/input.h>" ; echo "input_constants_dict = {" ; sed -r 's/[^ ]+ +([^ ]+).*/"\1" : \1,/' ; echo "}" ) | gcc -E -o /dev/stdout - | grep 'input_constants_dict = {' -A 100000 > gen.py
Lambas for EVIO*

View File

@ -1,7 +1,7 @@
from linux_input import *
from linux_uinput import *
import array, struct, fcntl, os
import array, struct, fcntl, os, sys
def get_input_version(f):
"""
@ -10,7 +10,7 @@ def get_input_version(f):
buf = array.array('i', [0])
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
v = struct.unpack('@i', buf)[0]
del r
del buf
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
def get_input_name(f, l=256):
@ -19,33 +19,75 @@ def get_input_name(f, l=256):
"""
buf = array.array('c', ' ' * l)
r = fcntl.ioctl(f, EVIOCGNAME(l), buf)
v = struct.unpack('%ds' % l, buf)
del r
return v
v = struct.unpack('%ds' % l, buf)[0]
del buf
return v[:r]
_bpl = struct.calcsize('@L') * 8
_nbits = lambda x: ((x-1) / _bpl) + 1
_ll = _nbits(KEY_MAX)
test_bit = lambda j, v: (v[j / _bpl] >> (j % _bpl)) & 1
# TODO: Do this for all EV_* ?
def get_keys(f, ev):
buf = array.array('L', [0L] * _ll)
try:
fcntl.ioctl(f, EVIOCGBIT(ev, KEY_MAX), buf)
except IOError:
print 'Whoops!'
yield None
return
#print >>sys.stderr, 'Whoops!', rev_events[ev]
return None
v = struct.unpack('@%dL' % _ll, buf)
del buf
r = []
for j in range(0, KEY_MAX):
if test_bit(j, v):
yield j
r.append(j)
return
return r
def copy_event(estr):
e = ctypes.cast(estr, ctypes.POINTER(input_event))
ev = e.contents
return input_event(ev.time, ev.type, ev.code, ev.value)
class InputDevice(object):
def __init__(self, path):
self._f = open(path)
def get_version(self):
return get_input_version(self._f)
def get_name(self):
return get_input_name(self._f)
def get_exposed_events(self):
d = dict()
for k, v in events.iteritems():
l = get_keys(self._f, v)
if l:
d[k] = []
for ev in l:
try:
d[k].append(event_keys[v][ev])
except KeyError:
pass
return d
def next_event(self):
estr = self._f.read(ctypes.sizeof(input_event))
return copy_event(estr)
def get_fd(self):
return fd
def __del__(self):
self._f.close()
def open_uinput():
@ -59,7 +101,7 @@ def open_uinput():
return None
return f
def create_uinput_device(name, specs):
def write_uinput_device_info(name):
"""
Create uinput device
"""
@ -97,8 +139,12 @@ def free_uinput_device(f):
class UInputDevice(object):
def __init__(self, name, specs):
self.f = create_uinput_device(name, specs)
# TODO: Other methods for specs etc
# TODO: Maybe don't create the device yet; add a seperate method?
# TODO: Take inspiration from the config.h files ! Also allow using
# direct methods / programming it rather than just the dict as config
self._f = write_uinput_device_info(name)
def __del__(self):
free_uinput_device(self.f)
free_uinput_device(self._f)

View File

@ -8,11 +8,11 @@ for k, v in icd.iteritems():
locals()[k] = v
rdict = lambda x: dict(map(lambda (k, v): (v, k), x))
rdict = lambda x: dict(map(lambda (k, v): (v, k), x.iteritems()))
events = filter(lambda (k, v): k in ["EV_SYN", "EV_KEY", "EV_REL",
events = dict(filter(lambda (k, v): k in ["EV_SYN", "EV_KEY", "EV_REL",
"EV_ABS", "EV_MSC", "EV_SW", "EV_LED", "EV_SND", "EV_REP",
"EV_FF", "EV_PWR", "EV_FF_STATUS"], icd.iteritems())
"EV_FF", "EV_PWR", "EV_FF_STATUS"], icd.iteritems()))
rev_events = rdict(events)
@ -29,39 +29,49 @@ static const char * const * const names[EV_MAX + 1] = {
};
"""
keys = filter(lambda (k, v): k.startswith("KEY_") or k.startswith("BTN_"),
icd.iteritems())
filter_event = lambda c: dict(filter(lambda (k, v): c(k), icd.iteritems()))
keys = filter_event(lambda x: x.startswith("KEY_") or x.startswith("BTN_"))
rev_keys = rdict(keys)
absaxes = filter(lambda (k, v): k.startswith("ABS_"),
icd.iteritems())
absaxes = filter_event(lambda x: x.startswith("ABS_"))
rev_absaxes = rdict(absaxes)
rel = filter(lambda (k, v): k.startswith("REL_"),
icd.iteritems())
rel = filter_event(lambda x: x.startswith("REL_"))
rev_rel = rdict(rel)
syn = filter(lambda (k, v): k.startswith("SYN_"),
icd.iteritems())
syn = filter_event(lambda x: x.startswith("SYN_"))
rev_syn = rdict(syn)
misc = filter_event(lambda x: x.startswith("MSC_"))
rev_misc = rdict(misc)
leds = filter_event(lambda x: x.startswith("LED_"))
rev_leds = rdict(leds)
sounds = filter_event(lambda x: x.startswith("SND_"))
rev_sounds = rdict(sounds)
repeats = filter_event(lambda x: x.startswith("REP_"))
rev_repeats = rdict(repeats)
switches = filter_event(lambda x: x.startswith("SW_"))
rev_switches = rdict(switches)
del rdict
# TODO
misc = {}
leds = sounds = repeats = switches = force = forcestatus = None
force, forcestatus = {}, {}
event_keys = {
EV_SYN: rev_syn,
EV_KEY: rev_keys,
EV_REL: rev_rel,
EV_ABS: rev_absaxes,
EV_MSC: misc,
EV_LED: leds,
EV_SND: sounds,
EV_REP: repeats,
EV_SW: switches,
EV_MSC: rev_misc,
EV_LED: rev_leds,
EV_SND: rev_sounds,
EV_REP: rev_repeats,
EV_SW: rev_switches,
EV_FF: force,
EV_FF_STATUS: forcestatus
}

View File

@ -3,23 +3,26 @@ import ctypes
import sys
f = open(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
#f = open(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
f = InputDevice(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
print 'Version:', get_input_version(f)
print get_input_name(f)
print 'Version:', f.get_version()
print f.get_name()
print [rev_keys[_] for _ in get_keys(f, EV_KEY)]
print [rev_absaxes[_] for _ in get_keys(f, EV_ABS)]
print [rev_rel[_] for _ in get_keys(f, EV_REL)]
d = f.get_exposed_events()
for k, v in d.iteritems():
print k + ':', ', '.join(v)
while True:
estr = f.read(ctypes.sizeof(input_event))
ev = f.next_event()
#estr = f._f.read(ctypes.sizeof(input_event))
e = ctypes.cast(estr, ctypes.POINTER(input_event))
ev = e.contents
#e = ctypes.cast(estr, ctypes.POINTER(input_event))
#ev = e.contents
print 'Event type:', rev_events[ev.type]
try:
print 'Code:', event_keys[ev.type][ev.code]
print ev.time.tv_sec, ev.time.tv_usec
s = '%s %s %d' % (rev_events[ev.type], event_keys[ev.type][ev.code], ev.value)
print 'Event type:', s
except KeyError:
pass