Extend uinput; replace open with os.open.

This commit is contained in:
Merlijn Wajer 2013-04-20 13:24:36 +02:00
parent 9113cb02b7
commit c8e7b07805
4 changed files with 32 additions and 11 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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