From 4323e92a13c0e00209f36c4ed234cc7e9084269f Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Fri, 10 May 2013 11:05:31 +0200 Subject: [PATCH] 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 --- py/configs/touchscreen.py | 40 +++++++++++++++++++ py/create.py | 43 +++++++++++++------- py/mapper.py | 1 + py/read.py | 83 +++++++++++++++++++++++++-------------- py/test.py | 24 ----------- 5 files changed, 122 insertions(+), 69 deletions(-) create mode 100644 py/configs/touchscreen.py delete mode 100644 py/test.py diff --git a/py/configs/touchscreen.py b/py/configs/touchscreen.py new file mode 100644 index 0000000..ba789bb --- /dev/null +++ b/py/configs/touchscreen.py @@ -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 + } + } +} diff --git a/py/create.py b/py/create.py index 21b9821..147f484 100644 --- a/py/create.py +++ b/py/create.py @@ -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 diff --git a/py/mapper.py b/py/mapper.py index 0359211..14926a4 100644 --- a/py/mapper.py +++ b/py/mapper.py @@ -1,5 +1,6 @@ import cinput +# XXX: Also parse name, etc def parse_conf(f): conf = {} e = f.get_exposed_events() diff --git a/py/read.py b/py/read.py index 082fbb7..10b7e00 100644 --- a/py/read.py +++ b/py/read.py @@ -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() diff --git a/py/test.py b/py/test.py deleted file mode 100644 index 1847ca1..0000000 --- a/py/test.py +++ /dev/null @@ -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