2013-05-14 17:46:18 -04:00
|
|
|
#!/usr/bin/env python
|
2013-04-20 19:46:44 -04:00
|
|
|
import linux_uinput, ctypes, fcntl, os, sys
|
2013-04-19 19:10:16 -04:00
|
|
|
from cinput import *
|
2013-05-18 12:10:19 -04:00
|
|
|
from mapper import KeyMapper, parse_conf, pretty_conf_print, \
|
|
|
|
get_exported_device_count
|
2013-05-09 10:10:28 -04:00
|
|
|
from linux_input import timeval, input_event
|
2013-04-20 19:46:44 -04:00
|
|
|
|
2013-05-09 10:10:28 -04:00
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2013-04-20 19:46:44 -04:00
|
|
|
|
2013-05-14 15:30:03 -04:00
|
|
|
import imp
|
2013-05-10 05:05:31 -04:00
|
|
|
import optparse
|
|
|
|
|
2013-05-14 15:30:03 -04:00
|
|
|
_usage = 'python create.py /path/to/config1 ... /path/to/configN'
|
|
|
|
parser = optparse.OptionParser(description='Create input devices.',
|
|
|
|
usage=_usage,
|
|
|
|
version='0.01'
|
|
|
|
)
|
2013-05-10 05:05:31 -04:00
|
|
|
parser.add_option('-C', '--compat', action='store_true',
|
|
|
|
help='Enable compatibility mode; for Python < 2.7')
|
2013-05-18 12:32:44 -04:00
|
|
|
parser.add_option('-v', '--verbose', action='store_true',
|
|
|
|
help='Enable verbose mode')
|
2013-05-10 05:05:31 -04:00
|
|
|
|
2013-05-10 11:33:25 -04:00
|
|
|
args, cfg = parser.parse_args()
|
2013-05-10 05:05:31 -04:00
|
|
|
|
|
|
|
# Unpickle from stdin ; currently this is the default and only way
|
2013-05-18 12:10:19 -04:00
|
|
|
in_f = pickle.Unpickler(sys.stdin)
|
2013-04-20 19:46:44 -04:00
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Read input device count
|
2013-05-18 12:10:19 -04:00
|
|
|
nifd = in_f.load()
|
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Read configuration
|
2013-05-18 12:10:19 -04:00
|
|
|
conf = in_f.load()
|
2013-05-10 05:05:31 -04:00
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Allow configurations to change our current configuration
|
2013-05-10 11:33:25 -04:00
|
|
|
for path in cfg:
|
2013-05-13 20:15:01 -04:00
|
|
|
config_merge = imp.load_source('', path).config_merge
|
|
|
|
config_merge(conf)
|
2013-05-10 05:05:31 -04:00
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
if args.verbose:
|
|
|
|
pretty_conf_print(conf)
|
2013-05-18 12:10:19 -04:00
|
|
|
|
2013-05-09 10:10:28 -04:00
|
|
|
m = KeyMapper(conf)
|
2013-04-20 19:46:44 -04:00
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Get number of output devices (determined from conf)
|
2013-05-18 12:10:19 -04:00
|
|
|
nofd = get_exported_device_count(conf)
|
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Create and expose uinput devices
|
2013-05-18 12:10:19 -04:00
|
|
|
ofs = []
|
|
|
|
for f in xrange(nofd):
|
|
|
|
d = UInputDevice()
|
|
|
|
m.expose(d, f)
|
|
|
|
d.setup('Example input device')
|
|
|
|
ofs.append(d)
|
2013-04-20 19:46:44 -04:00
|
|
|
|
2013-05-18 12:32:44 -04:00
|
|
|
# Map events
|
2013-04-20 19:46:44 -04:00
|
|
|
while True:
|
2013-05-10 05:05:31 -04:00
|
|
|
if not args.compat:
|
2013-05-18 12:10:19 -04:00
|
|
|
fd, ev = in_f.load()
|
2013-05-10 05:05:31 -04:00
|
|
|
else:
|
2013-05-18 12:10:19 -04:00
|
|
|
fd, ev_p = in_f.load()
|
2013-05-10 05:05:31 -04:00
|
|
|
ti = timeval(ev_p[0], ev_p[1])
|
|
|
|
ev = input_event(ti, ev_p[2], ev_p[3], ev_p[4])
|
2013-04-18 19:11:56 -04:00
|
|
|
|
2013-05-18 12:10:19 -04:00
|
|
|
idx, ev = m.map_event(ev, fd)
|
|
|
|
d = ofs[idx]
|
2013-04-18 19:11:56 -04:00
|
|
|
|
2013-04-20 19:46:44 -04:00
|
|
|
d.fire_event(ev)
|