From c8e7b07805ef7d8d78b315ac029dcb1b746538fa Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Sat, 20 Apr 2013 13:24:36 +0200 Subject: [PATCH] Extend uinput; replace open with os.open. --- py/cinput.py | 19 ++++++++++++++++--- py/create.py | 8 ++++---- py/linux_uinput.py | 12 ++++++++++++ py/read.py | 4 ---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/py/cinput.py b/py/cinput.py index 216d560..7d85531 100644 --- a/py/cinput.py +++ b/py/cinput.py @@ -48,6 +48,10 @@ def get_keys(f, ev): def copy_event(estr): + """ + Copy event from a string returned by read(). + We return a copy because the string will probably be freed. + """ e = ctypes.cast(estr, ctypes.POINTER(input_event)) ev = e.contents @@ -56,7 +60,7 @@ def copy_event(estr): class InputDevice(object): def __init__(self, path): - self._f = open(path) + self._f = os.open(path, os.O_RDONLY) def get_version(self): return get_input_version(self._f) @@ -79,7 +83,7 @@ class InputDevice(object): return d def next_event(self): - estr = self._f.read(ctypes.sizeof(input_event)) + estr = os.read(self._f, ctypes.sizeof(input_event)) return copy_event(estr) def get_fd(self): @@ -87,7 +91,7 @@ class InputDevice(object): def __del__(self): - self._f.close() + os.close(self._f) def open_uinput(): @@ -145,6 +149,15 @@ class UInputDevice(object): # direct methods / programming it rather than just the dict as config self._f = write_uinput_device_info(name) + def expose_event(evt, evk): + evbit = evbits[ev] + fcntl.ioctl(self._f, evbit, evk) + + def fire_event(ev): + """ + """ + os.write(self._f, buffer(ev)[:]) + def __del__(self): free_uinput_device(self._f) diff --git a/py/create.py b/py/create.py index 684d6d9..87acab3 100644 --- a/py/create.py +++ b/py/create.py @@ -1,14 +1,14 @@ -import cinput, linux_uinput, ctypes, fcntl, os +import linux_uinput, ctypes, fcntl, os from cinput import * def handle_specs(f, s): - print 'ioctl:', fcntl.ioctl(f, UI_SET_EVBIT, cinput.EV_KEY) - print 'ioctl:', fcntl.ioctl(f, UI_SET_KEYBIT, cinput.KEY_UP) + print 'ioctl:', fcntl.ioctl(f, UI_SET_EVBIT, EV_KEY) + print 'ioctl:', fcntl.ioctl(f, UI_SET_KEYBIT, KEY_UP) -d = cinput.UInputDevice('Example input device', None) +d = UInputDevice('Example input device', None) import time time.sleep(5) diff --git a/py/linux_uinput.py b/py/linux_uinput.py index f55a4b5..c5474fe 100644 --- a/py/linux_uinput.py +++ b/py/linux_uinput.py @@ -24,6 +24,18 @@ 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: Lacking PHYS and PROP +evbits = { + linux_input.EV_KEY : UI_SET_KEYBIT, + linux_input.EV_REL : UI_SET_RELBIT, + linux_input.EV_ABS : UI_SET_ABSBIT, + linux_input.EV_MSC : UI_SET_MSCBIT, + linux_input.EV_LED : UI_SET_LEDBIT, + linux_input.EV_SND : UI_SET_SNDBIT, + linux_input.EV_FF : UI_SET_FFBIT, + linux_input.EV_SW : UI_SET_SWBIT +} + # 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) diff --git a/py/read.py b/py/read.py index 8e8a328..c857e0e 100644 --- a/py/read.py +++ b/py/read.py @@ -15,10 +15,6 @@ for k, v in d.iteritems(): while True: ev = f.next_event() - #estr = f._f.read(ctypes.sizeof(input_event)) - - #e = ctypes.cast(estr, ctypes.POINTER(input_event)) - #ev = e.contents try: print ev.time.tv_sec, ev.time.tv_usec