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:
parent
f6efe9f331
commit
9113cb02b7
|
@ -6,7 +6,7 @@ Generate:
|
||||||
- REL_*
|
- REL_*
|
||||||
- ABS_*
|
- 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*
|
Lambas for EVIO*
|
||||||
|
|
74
py/cinput.py
74
py/cinput.py
|
@ -1,7 +1,7 @@
|
||||||
from linux_input import *
|
from linux_input import *
|
||||||
from linux_uinput import *
|
from linux_uinput import *
|
||||||
|
|
||||||
import array, struct, fcntl, os
|
import array, struct, fcntl, os, sys
|
||||||
|
|
||||||
def get_input_version(f):
|
def get_input_version(f):
|
||||||
"""
|
"""
|
||||||
|
@ -10,7 +10,7 @@ def get_input_version(f):
|
||||||
buf = array.array('i', [0])
|
buf = array.array('i', [0])
|
||||||
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
|
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
|
||||||
v = struct.unpack('@i', buf)[0]
|
v = struct.unpack('@i', buf)[0]
|
||||||
del r
|
del buf
|
||||||
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
|
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
|
||||||
|
|
||||||
def get_input_name(f, l=256):
|
def get_input_name(f, l=256):
|
||||||
|
@ -19,33 +19,75 @@ def get_input_name(f, l=256):
|
||||||
"""
|
"""
|
||||||
buf = array.array('c', ' ' * l)
|
buf = array.array('c', ' ' * l)
|
||||||
r = fcntl.ioctl(f, EVIOCGNAME(l), buf)
|
r = fcntl.ioctl(f, EVIOCGNAME(l), buf)
|
||||||
v = struct.unpack('%ds' % l, buf)
|
v = struct.unpack('%ds' % l, buf)[0]
|
||||||
del r
|
del buf
|
||||||
return v
|
return v[:r]
|
||||||
|
|
||||||
_bpl = struct.calcsize('@L') * 8
|
_bpl = struct.calcsize('@L') * 8
|
||||||
_nbits = lambda x: ((x-1) / _bpl) + 1
|
_nbits = lambda x: ((x-1) / _bpl) + 1
|
||||||
_ll = _nbits(KEY_MAX)
|
_ll = _nbits(KEY_MAX)
|
||||||
test_bit = lambda j, v: (v[j / _bpl] >> (j % _bpl)) & 1
|
test_bit = lambda j, v: (v[j / _bpl] >> (j % _bpl)) & 1
|
||||||
|
|
||||||
# TODO: Do this for all EV_* ?
|
|
||||||
def get_keys(f, ev):
|
def get_keys(f, ev):
|
||||||
buf = array.array('L', [0L] * _ll)
|
buf = array.array('L', [0L] * _ll)
|
||||||
try:
|
try:
|
||||||
fcntl.ioctl(f, EVIOCGBIT(ev, KEY_MAX), buf)
|
fcntl.ioctl(f, EVIOCGBIT(ev, KEY_MAX), buf)
|
||||||
except IOError:
|
except IOError:
|
||||||
print 'Whoops!'
|
#print >>sys.stderr, 'Whoops!', rev_events[ev]
|
||||||
yield None
|
return None
|
||||||
return
|
|
||||||
|
|
||||||
v = struct.unpack('@%dL' % _ll, buf)
|
v = struct.unpack('@%dL' % _ll, buf)
|
||||||
del buf
|
del buf
|
||||||
|
|
||||||
|
r = []
|
||||||
for j in range(0, KEY_MAX):
|
for j in range(0, KEY_MAX):
|
||||||
if test_bit(j, v):
|
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():
|
def open_uinput():
|
||||||
|
@ -59,7 +101,7 @@ def open_uinput():
|
||||||
return None
|
return None
|
||||||
return f
|
return f
|
||||||
|
|
||||||
def create_uinput_device(name, specs):
|
def write_uinput_device_info(name):
|
||||||
"""
|
"""
|
||||||
Create uinput device
|
Create uinput device
|
||||||
"""
|
"""
|
||||||
|
@ -97,8 +139,12 @@ def free_uinput_device(f):
|
||||||
class UInputDevice(object):
|
class UInputDevice(object):
|
||||||
|
|
||||||
def __init__(self, name, specs):
|
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):
|
def __del__(self):
|
||||||
free_uinput_device(self.f)
|
free_uinput_device(self._f)
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,11 @@ for k, v in icd.iteritems():
|
||||||
locals()[k] = v
|
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_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)
|
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_"),
|
filter_event = lambda c: dict(filter(lambda (k, v): c(k), icd.iteritems()))
|
||||||
icd.iteritems())
|
|
||||||
|
keys = filter_event(lambda x: x.startswith("KEY_") or x.startswith("BTN_"))
|
||||||
rev_keys = rdict(keys)
|
rev_keys = rdict(keys)
|
||||||
|
|
||||||
absaxes = filter(lambda (k, v): k.startswith("ABS_"),
|
absaxes = filter_event(lambda x: x.startswith("ABS_"))
|
||||||
icd.iteritems())
|
|
||||||
rev_absaxes = rdict(absaxes)
|
rev_absaxes = rdict(absaxes)
|
||||||
|
|
||||||
rel = filter(lambda (k, v): k.startswith("REL_"),
|
rel = filter_event(lambda x: x.startswith("REL_"))
|
||||||
icd.iteritems())
|
|
||||||
rev_rel = rdict(rel)
|
rev_rel = rdict(rel)
|
||||||
|
|
||||||
syn = filter(lambda (k, v): k.startswith("SYN_"),
|
syn = filter_event(lambda x: x.startswith("SYN_"))
|
||||||
icd.iteritems())
|
|
||||||
rev_syn = rdict(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
|
del rdict
|
||||||
|
|
||||||
# TODO
|
force, forcestatus = {}, {}
|
||||||
misc = {}
|
|
||||||
|
|
||||||
leds = sounds = repeats = switches = force = forcestatus = None
|
|
||||||
|
|
||||||
event_keys = {
|
event_keys = {
|
||||||
EV_SYN: rev_syn,
|
EV_SYN: rev_syn,
|
||||||
EV_KEY: rev_keys,
|
EV_KEY: rev_keys,
|
||||||
EV_REL: rev_rel,
|
EV_REL: rev_rel,
|
||||||
EV_ABS: rev_absaxes,
|
EV_ABS: rev_absaxes,
|
||||||
EV_MSC: misc,
|
EV_MSC: rev_misc,
|
||||||
EV_LED: leds,
|
EV_LED: rev_leds,
|
||||||
EV_SND: sounds,
|
EV_SND: rev_sounds,
|
||||||
EV_REP: repeats,
|
EV_REP: rev_repeats,
|
||||||
EV_SW: switches,
|
EV_SW: rev_switches,
|
||||||
EV_FF: force,
|
EV_FF: force,
|
||||||
EV_FF_STATUS: forcestatus
|
EV_FF_STATUS: forcestatus
|
||||||
}
|
}
|
||||||
|
|
25
py/read.py
25
py/read.py
|
@ -3,23 +3,26 @@ import ctypes
|
||||||
|
|
||||||
import sys
|
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 'Version:', f.get_version()
|
||||||
print get_input_name(f)
|
print f.get_name()
|
||||||
|
|
||||||
print [rev_keys[_] for _ in get_keys(f, EV_KEY)]
|
d = f.get_exposed_events()
|
||||||
print [rev_absaxes[_] for _ in get_keys(f, EV_ABS)]
|
for k, v in d.iteritems():
|
||||||
print [rev_rel[_] for _ in get_keys(f, EV_REL)]
|
print k + ':', ', '.join(v)
|
||||||
|
|
||||||
while True:
|
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))
|
#e = ctypes.cast(estr, ctypes.POINTER(input_event))
|
||||||
ev = e.contents
|
#ev = e.contents
|
||||||
|
|
||||||
print 'Event type:', rev_events[ev.type]
|
|
||||||
try:
|
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:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue