More networked code; experimental config support.

read.py now deprecates test.py

I went with optparse rather than argparse to support Python <2.7.
Switching to argparse later should not be that hard.

Configurations still need to be extended with, among other things:

* Name for the device
This commit is contained in:
Merlijn Wajer 2013-05-10 11:05:31 +02:00
parent 19868360a3
commit 4323e92a13
5 changed files with 122 additions and 69 deletions

40
py/configs/touchscreen.py Normal file
View File

@ -0,0 +1,40 @@
from cinput import *
def transform_x(x):
print 'old y', x
# offset
x -= 200
x = int(x / (3800. / 1366.))
#x = int(x / (3800. / 1920.))
print 'new x', x
return x
def transform_y(y):
print 'old y', y
# invert
y = 3800 - y
# offset
y -= 200
y = int(y / (3800. / 768.))
#y = int(y / (3800. / 1080.))
print 'new y', y
return y
config = {
EV_ABS : {
ABS_X : {
'type' : EV_ABS,
'code' : ABS_X,
'value' : transform_x
},
ABS_Y : {
'type' : EV_ABS,
'code' : ABS_Y,
'value' : transform_y
}
}
}

View File

@ -4,15 +4,37 @@ from mapper import KeyMapper, parse_conf
from example_conf import config
from linux_input import timeval, input_event
import imp
try:
import cPickle as pickle
except ImportError:
import pickle
import optparse
parser = optparse.OptionParser(description='Create input devices. '
'TODO')
parser.add_option('-c', '--config', type=str, action='append',
default=[],
help='Merge configuration file with default '
'configuration (allowed to be used multiple times)')
parser.add_option('-C', '--compat', action='store_true',
help='Enable compatibility mode; for Python < 2.7')
args = parser.parse_args()
# Unpickle from stdin ; currently this is the default and only way
f = pickle.Unpickler(sys.stdin)
conf = f.load()
for path in args.config:
if path:
config = imp.load_source('', path).config
conf.update(config)
m = KeyMapper(conf)
d = UInputDevice()
@ -21,22 +43,13 @@ d.setup('Example input device')
while True:
ev = f.load()
# Use code below rather than the line above if you use an old python
# version (also edit read.py)
#ev_p = f.load()
#ti = timeval(ev_p[0], ev_p[1])
#ev = input_event(ti, ev_p[2], ev_p[3], ev_p[4])
if not args.compat:
ev = f.load()
else:
ev_p = f.load()
ti = timeval(ev_p[0], ev_p[1])
ev = input_event(ti, ev_p[2], ev_p[3], ev_p[4])
ev = m.map_event(ev)
d.fire_event(ev)
#try:
# print ev.time.tv_sec, ev.time.tv_usec
# s = '%s %s %d' % (rev_events[ev.type], rev_event_keys[ev.type][ev.code], ev.value)
# print 'Event type:', s
#except KeyError:
# pass

View File

@ -1,5 +1,6 @@
import cinput
# XXX: Also parse name, etc
def parse_conf(f):
conf = {}
e = f.get_exposed_events()

View File

@ -3,48 +3,71 @@ from cinput import *
from mapper import KeyMapper, parse_conf
from example_conf import config
clone = True
f = InputDevice(sys.argv[1] if len(sys.argv) == 2 else "/dev/input/event3")
if clone:
config = parse_conf(f)
m = KeyMapper(config)
else:
m = KeyMapper(config)
try:
import cPickle as pickle
except ImportError:
import pickle
p = pickle.Pickler(sys.stdout)
p.dump(config)
sys.stdout.flush()
import optparse
#d = UInputDevice()
#m.expose(d)
#d.setup('Example input device')
parser = optparse.OptionParser(description='Read input devices. '
'TODO')
parser.add_option('-D', '--dump', action='store_false',
default=True, help='Dump will marshall all the events to stdout')
parser.add_option('-i', '--input-file', action='append',
type=str, default=[],
help='Read events from this input device')
parser.add_option('-C', '--compat', action='store_true',
help='Enable compatibility mode; for Python < 2.7')
args = parser.parse_args()
while True:
ev = f.next_event()
p.dump(ev)
if len(args.input_file) == 0:
parser.print_help()
exit(0)
# Use this rather than the line above if you use an old python version (also
# edit create.py)
#p.dump((ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code, ev.value))
# TODO: Support multiple input files + epoll; InputDevices?
f = InputDevice(args.input_file[0])
config = parse_conf(f)
m = KeyMapper(config)
if args.dump:
print 'Version:', f.get_version()
print f.get_name()
d = f.get_exposed_events()
for k, v in d.iteritems():
print k + ':', ', '.join(v)
else:
p = pickle.Pickler(sys.stdout)
p.dump(config)
sys.stdout.flush()
#ev = m.map_event(ev)
while True:
# TODO: Poll multiple files ; add file description (not descriptor...)
# f, ev = fs.next_event()
ev = f.next_event()
#d.fire_event(ev)
if args.dump:
try:
print ev.time.tv_sec, ev.time.tv_usec
s = '%s %s %d' % (rev_events[ev.type], rev_event_keys[ev.type][ev.code], ev.value)
print 'Event type:', s
except KeyError:
pass
#try:
# print ev.time.tv_sec, ev.time.tv_usec
# s = '%s %s %d' % (rev_events[ev.type], rev_event_keys[ev.type][ev.code], ev.value)
# print 'Event type:', s
else:
if not args.compat:
p.dump(ev)
else:
# Use this rather than the line above if you use an old python version (also
# edit create.py)
p.dump((ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code, ev.value))
#except KeyError:
# pass
sys.stdout.flush()

View File

@ -1,24 +0,0 @@
from cinput import *
import ctypes
import sys
#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:', f.get_version()
print f.get_name()
d = f.get_exposed_events()
for k, v in d.iteritems():
print k + ':', ', '.join(v)
while True:
ev = f.next_event()
try:
print ev.time.tv_sec, ev.time.tv_usec
s = '%s %s %d' % (rev_events[ev.type], rev_event_keys[ev.type][ev.code], ev.value)
print 'Event type:', s
except KeyError:
pass