Clean up code a bit more; add a class.

This commit is contained in:
Merlijn Wajer 2013-04-20 01:10:16 +02:00
parent e9de2ec43d
commit f6efe9f331
4 changed files with 117 additions and 97 deletions

View File

@ -1,8 +1,12 @@
from linux_input import *
from linux_uinput import *
import array, struct, fcntl
import array, struct, fcntl, os
def get_input_version(f):
"""
Returns the input version of a specified fd of a device
"""
buf = array.array('i', [0])
r = fcntl.ioctl(f, EVIOCGVERSION, buf)
v = struct.unpack('@i', buf)[0]
@ -10,6 +14,9 @@ def get_input_version(f):
return "%d.%d.%d" % ( v >> 16, (v >> 8) & 0xff, v & 0xff)
def get_input_name(f, l=256):
"""
Returns the name of a specified fd of a device
"""
buf = array.array('c', ' ' * l)
r = fcntl.ioctl(f, EVIOCGNAME(l), buf)
v = struct.unpack('%ds' % l, buf)
@ -21,6 +28,7 @@ _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:
@ -36,7 +44,61 @@ def get_keys(f, ev):
for j in range(0, KEY_MAX):
if test_bit(j, v):
yield j
#yield rev_keys[j]
return
def open_uinput():
try:
f = os.open('/dev/uinput', os.O_WRONLY | os.O_NONBLOCK)
except IOError:
try:
f = os.open('/dev/input/uinput', os.O_WRONLY | os.O_NONBLOCK)
except IOError:
print 'FAIL MUCH?'
return None
return f
def create_uinput_device(name, specs):
"""
Create uinput device
"""
f = open_uinput()
if not f:
print 'Failed to open uinput'
return None
# Add keys, etc
#handle_specs(f, specs)
# Allocate other info
uidev = uinput_user_dev()
# TODO: Get from specs
uidev.name = name
uidev._id.bustype = 0x03 # BUS_USB (TODO)
uidev._id.vendor = 0x42
uidev._id.product = 0xbebe
uidev._id.version = 1
buf = buffer(uidev)[:]
# Write dev info
os.write(f, buf)
fcntl.ioctl(f, UI_DEV_CREATE)
return f
def free_uinput_device(f):
return fcntl.ioctl(f, UI_DEV_DESTROY)
class UInputDevice(object):
def __init__(self, name, specs):
self.f = create_uinput_device(name, specs)
def __del__(self):
free_uinput_device(self.f)

View File

@ -1,81 +1,39 @@
import cinput, linux_uinput, ctypes, fcntl, os
def open_uinput():
try:
f = os.open('/dev/uinput', os.O_WRONLY | os.O_NONBLOCK)
except IOError:
try:
f = os.open('/dev/input/uinput', os.O_WRONLY | os.O_NONBLOCK)
except IOError:
print 'FAIL MUCH?'
return None
return f
from cinput import *
def handle_specs(f, s):
print 'ioctl:', fcntl.ioctl(f, linux_uinput.UI_SET_EVBIT, cinput.EV_KEY)
print 'ioctl:', fcntl.ioctl(f, linux_uinput.UI_SET_KEYBIT, cinput.KEY_UP)
print 'ioctl:', fcntl.ioctl(f, UI_SET_EVBIT, cinput.EV_KEY)
print 'ioctl:', fcntl.ioctl(f, UI_SET_KEYBIT, cinput.KEY_UP)
def create_device(specs):
f = open_uinput()
d = cinput.UInputDevice('Example input device', None)
if not f:
print 'Failed to open uinput'
return None
# Add keys, etc
handle_specs(f, specs)
# Allocate other info
uidev = linux_uinput.uinput_user_dev()
uidev.name = 'key2joy:0'
uidev._id.bustype = 0x03 # BUS_USB (TODO)
uidev._id.vendor = 0x42
uidev._id.product = 0xbebe
uidev._id.version = 1
buf = buffer(uidev)[:]
# Write dev info
os.write(f, buf)
print 'ioctl:', fcntl.ioctl(f, linux_uinput.UI_DEV_CREATE)
return f
def free_device(f):
return fcntl.ioctl(f, linux_uinput.UI_DEV_DESTROY)
_ = create_device(None)
print 'Hoi'
import time
time.sleep(5)
free_device(_)
# config
#dev = {
# "input_devices" : [
# ("/dev/input/event3", "keyboard1"),
# ],
# "type" : "mouse", # "mixed" "mouse" "keyboard" "joystick" "clone"?
# "keymap" : {
# "any" : { # From
# EV_KEY : {
# KEY_UP : {
# "type" : EV_REL
# "key" : REL_X
# "value" : lambda x: -x*10
# }
# KEY_DOWN : {
# "type": EV_REL
# "key": REL_X
# "value" : lambda x: x*10
# }
# }
# },
# }
#}
# Config
dev = {
"input_devices" : [
("/dev/input/event3", "keyboard1"),
],
"type" : "mouse", # "mixed" "mouse" "keyboard" "joystick" "clone"?
"keymap" : {
"any" : { # From
EV_KEY : {
KEY_UP : {
"type" : EV_REL,
"key" : REL_X,
"value" : lambda x: -x*10
},
KEY_DOWN : {
"type": EV_REL,
"key": REL_X,
"value" : lambda x: x*10
}
}
},
}
}

View File

@ -1,25 +0,0 @@
import cinput
import ctypes
import sys
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.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))
e = ctypes.cast(estr, ctypes.POINTER(cinput.input_event))
ev = e.contents
print 'Event type:', cinput.rev_events[ev.type]
try:
print 'Code:', cinput.event_keys[ev.type][ev.code]
except KeyError:
pass

25
py/read.py Normal file
View File

@ -0,0 +1,25 @@
from cinput import *
import ctypes
import sys
f = open(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
print 'Version:', get_input_version(f)
print get_input_name(f)
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)]
while True:
estr = f.read(ctypes.sizeof(input_event))
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]
except KeyError:
pass