Add linux_uinput; cleanups.

This commit is contained in:
Merlijn Wajer 2013-04-19 00:10:15 +02:00
parent 4eb6a0f1d4
commit 52736ea198
4 changed files with 161 additions and 98 deletions

View File

@ -1,69 +1,42 @@
#TODO: remove this soon
from gen import input_constants_dict as icd
# TODO: Just combine linux_input and cinput and use locals() to set the
# variables rather than the silly hack
for k, v in icd.iteritems():
locals()[k] = v
from linux_input import *
import array, struct, fcntl
rdict = lambda x: dict(map(lambda (k, v): (v, k), x))
def get_input_version(f):
buf = array.array('i', [0])
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
v = struct.unpack('@i', buf)[0]
del r
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
events = 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())
rev_events = rdict(events)
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
_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: Get proper ``names'' like evtest:
"""
static const char * const * const names[EV_MAX + 1] = {
[0 ... EV_MAX] = NULL,
[EV_SYN] = events, [EV_KEY] = keys,
[EV_REL] = relatives, [EV_ABS] = absolutes,
[EV_MSC] = misc, [EV_LED] = leds,
[EV_SND] = sounds, [EV_REP] = repeats,
[EV_SW] = switches,
[EV_FF] = force, [EV_FF_STATUS] = forcestatus,
};
"""
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
keys = filter(lambda (k, v): k.startswith("KEY_") or k.startswith("BTN_"),
icd.iteritems())
rev_keys = rdict(keys)
v = struct.unpack('@%dL' % _ll, buf)
del buf
absaxes = filter(lambda (k, v): k.startswith("ABS_"),
icd.iteritems())
rev_absaxes = rdict(absaxes)
for j in range(0, KEY_MAX):
if test_bit(j, v):
yield j
#yield rev_keys[j]
rel = filter(lambda (k, v): k.startswith("REL_"),
icd.iteritems())
rev_rel = rdict(rel)
return
syn = filter(lambda (k, v): k.startswith("SYN_"),
icd.iteritems())
rev_syn = rdict(syn)
del rdict
misc = {}
leds = sounds = repeats = switches = force = forcestatus = None
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_FF: force,
EV_FF_STATUS: forcestatus
}

View File

@ -1,5 +1,7 @@
import ctypes
import struct
"""
struct input_event {
struct timeval time;
@ -9,6 +11,71 @@ struct input_event {
};
"""
from gen import input_constants_dict as icd
for k, v in icd.iteritems():
locals()[k] = v
rdict = lambda x: dict(map(lambda (k, v): (v, k), x))
events = 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())
rev_events = rdict(events)
# event sets
"""
static const char * const * const names[EV_MAX + 1] = {
[0 ... EV_MAX] = NULL,
[EV_SYN] = events, [EV_KEY] = keys,
[EV_REL] = relatives, [EV_ABS] = absolutes,
[EV_MSC] = misc, [EV_LED] = leds,
[EV_SND] = sounds, [EV_REP] = repeats,
[EV_SW] = switches,
[EV_FF] = force, [EV_FF_STATUS] = forcestatus,
};
"""
keys = filter(lambda (k, v): k.startswith("KEY_") or k.startswith("BTN_"),
icd.iteritems())
rev_keys = rdict(keys)
absaxes = filter(lambda (k, v): k.startswith("ABS_"),
icd.iteritems())
rev_absaxes = rdict(absaxes)
rel = filter(lambda (k, v): k.startswith("REL_"),
icd.iteritems())
rev_rel = rdict(rel)
syn = filter(lambda (k, v): k.startswith("SYN_"),
icd.iteritems())
rev_syn = rdict(syn)
del rdict
# TODO
misc = {}
leds = sounds = repeats = switches = force = forcestatus = None
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_FF: force,
EV_FF_STATUS: forcestatus
}
class timeval(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long), ("tv_usec", ctypes.c_long)]
@ -20,6 +87,14 @@ class input_event(ctypes.Structure):
("value", ctypes.c_int32)
]
class input_id (ctypes.Structure):
_fields_ = [
("bustype", ctypes.c_uint16),
("vendor", ctypes.c_uint16),
("product", ctypes.c_uint16),
("version", ctypes.c_uint16),
]
from ioctlhelp import IOR, IOW, IOC, IO, _IOC_READ
# Get driver version
@ -80,18 +155,3 @@ EVIOCGBIT = lambda ev, _len: IOC(_IOC_READ, ord('E'), 0x20 + ev, _len)
#
#EVIOCSCLOCKID _IOW('E', 0xa0, int) /* Set clockid to be used for timestamps */
import array, struct, fcntl
def get_input_version(f):
buf = array.array('i', [0])
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
v = struct.unpack('@i', buf)[0]
del r
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
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

51
py/linux_uinput.py Normal file
View File

@ -0,0 +1,51 @@
from ioctlhelp import *
import linux_input
# For uinput version 3
UINPUT_IOCTL_BASE = ord('U')
UI_DEV_CREATE = IO(UINPUT_IOCTL_BASE, 1)
UI_DEV_DESTROY = IO(UINPUT_IOCTL_BASE, 2)
#define UI_DEV_CREATE _IO(UINPUT_IOCTL_BASE, 1)
#define UI_DEV_DESTROY _IO(UINPUT_IOCTL_BASE, 2)
UI_SET_EVBIT = IOW(UINPUT_IOCTL_BASE, 100, '@i')
UI_SET_KEYBIT = IOW(UINPUT_IOCTL_BASE, 101, '@i')
UI_SET_RELBIT = IOW(UINPUT_IOCTL_BASE, 102, '@i')
UI_SET_ABSBIT = IOW(UINPUT_IOCTL_BASE, 103, '@i')
UI_SET_MSCBIT = IOW(UINPUT_IOCTL_BASE, 104, '@i')
UI_SET_LEDBIT = IOW(UINPUT_IOCTL_BASE, 105, '@i')
UI_SET_SNDBIT = IOW(UINPUT_IOCTL_BASE, 106, '@i')
UI_SET_FFBIT = IOW(UINPUT_IOCTL_BASE, 107, '@i')
UI_SET_PHYS = IOW(UINPUT_IOCTL_BASE, 108, '@i')
UI_SET_SWBIT = IOW(UINPUT_IOCTL_BASE, 109, '@i')
UI_SET_PROPBIT = IOW(UINPUT_IOCTL_BASE, 110, '@i')
# TODO
#define UI_BEGIN_FF_UPLOAD _IOWR(UINPUT_IOCTL_BASE, 200, struct uinput_ff_upload)
#define UI_END_FF_UPLOAD _IOW(UINPUT_IOCTL_BASE, 201, struct uinput_ff_upload)
#define UI_BEGIN_FF_ERASE _IOWR(UINPUT_IOCTL_BASE, 202, struct uinput_ff_erase)
#define UI_END_FF_ERASE _IOW(UINPUT_IOCTL_BASE, 203, struct uinput_ff_erase)
EV_UINPUT = 0x0101
UI_FF_UPLOAD = 1
UI_FF_ERASE = 2
import ctypes
UINPUT_MAX_NAME_SIZE = 80
class uinput_user_dev(ctypes.Structure):
_fields_ = [
("name", ctypes.c_char * UINPUT_MAX_NAME_SIZE),
("id", linux_input.input_id),
("ff_effects_max", ctypes.c_uint32),
("absmax", ctypes.c_int32),
("absmin", ctypes.c_int32),
("absfuzz", ctypes.c_int32),
("absflac", ctypes.c_int32)
]

View File

@ -7,31 +7,10 @@ f = open(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
print 'Version:', cinput.get_input_version(f)
print cinput.get_input_name(f)
print cinput.EVIOCGBIT(0, cinput.EV_MAX)
import struct, array, fcntl
bpl = struct.calcsize('@L') * 8
nbits = lambda x: ((x-1) / bpl) + 1
ll = nbits(cinput.KEY_MAX)
test_bit = lambda j, v: (v[j / bpl] >> (j % bpl)) & 1
#for b in xrange(cinput.EV_MAX):
for b in xrange(1, 2):
buf = array.array('L', [0L] * ll)
print 'ioctl return:', fcntl.ioctl(f, cinput.EVIOCGBIT(b, cinput.KEY_MAX), buf)
v = struct.unpack('@%dL' % ll, buf)
print v
for j in range(0, cinput.KEY_MAX):
if test_bit(j, v):
print cinput.rev_keys[j]
del buf
print [cinput.rev_keys[_] for _ in cinput.get_keys(f, cinput.EV_KEY)]
print [cinput.rev_absaxes[_] for _ in cinput.get_keys(f, cinput.EV_ABS)]
print [cinput.rev_rel[_] for _ in cinput.get_keys(f, cinput.EV_REL)]
while True:
estr = f.read(ctypes.sizeof(cinput.input_event))