2016-07-07 13:43:31 -04:00
|
|
|
#!/bin/python
|
|
|
|
|
2016-07-07 15:40:10 -04:00
|
|
|
import evdev
|
2016-08-11 08:37:46 -04:00
|
|
|
from evdev import InputDevice, UInput
|
2016-07-07 15:40:10 -04:00
|
|
|
from select import select
|
2016-08-11 08:37:46 -04:00
|
|
|
import time
|
2016-07-07 15:40:10 -04:00
|
|
|
|
2016-07-07 13:43:31 -04:00
|
|
|
class inputManager():
|
|
|
|
def __init__(self):
|
2016-08-11 08:37:46 -04:00
|
|
|
self.iDevices = {}
|
|
|
|
self.uDevices = {}
|
|
|
|
self.getDevices()
|
2016-07-07 15:40:10 -04:00
|
|
|
|
2016-07-08 05:24:34 -04:00
|
|
|
def getKeyPressed(self, environment):
|
2016-07-16 18:56:18 -04:00
|
|
|
timeout = True
|
2016-07-13 15:40:19 -04:00
|
|
|
try:
|
2016-08-11 08:37:46 -04:00
|
|
|
r, w, x = select(self.iDevices, [], [], environment['runtime']['settingsManager'].getSettingAsFloat(environment, 'screen', 'screenUpdateDelay'))
|
2016-07-13 15:40:19 -04:00
|
|
|
if r != []:
|
2016-07-16 18:56:18 -04:00
|
|
|
timeout = False
|
2016-07-13 15:40:19 -04:00
|
|
|
for fd in r:
|
2016-08-11 08:37:46 -04:00
|
|
|
for event in self.iDevices[fd].read():
|
|
|
|
if event.code == 82: # a
|
|
|
|
environment['generalInformation']['consumeKey'] = True
|
|
|
|
if not environment['generalInformation']['consumeKey']:
|
|
|
|
self.uDevices[fd].write_event(event)
|
|
|
|
self.uDevices[fd].syn()
|
|
|
|
time.sleep(0.01)
|
|
|
|
else:
|
|
|
|
if event.type == evdev.ecodes.EV_KEY:
|
|
|
|
if event.value != 0:
|
|
|
|
environment['input']['currShortcut'][str(event.code)] = 1 #event.value
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
del(environment['input']['currShortcut'][str(event.code)])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
self.freeDevices()
|
2016-07-08 12:33:32 -04:00
|
|
|
environment['input']['currShortcutString'] = self.getShortcutString(environment)
|
2016-08-11 08:37:46 -04:00
|
|
|
environment['generalInformation']['consumeKey'] = environment['input']['currShortcut'] != {}
|
2016-07-16 18:56:18 -04:00
|
|
|
return environment, timeout
|
2016-07-08 12:33:32 -04:00
|
|
|
|
|
|
|
def getShortcutString(self, environment):
|
|
|
|
if environment['input']['currShortcut'] == {}:
|
|
|
|
return ''
|
|
|
|
currShortcutStringList = []
|
2016-07-10 12:34:44 -04:00
|
|
|
for key in environment['input']['currShortcut']:
|
2016-07-08 12:33:32 -04:00
|
|
|
currShortcutStringList.append("%s-%s" % (environment['input']['currShortcut'][key], key))
|
2016-07-10 12:34:44 -04:00
|
|
|
currShortcutStringList = sorted(currShortcutStringList)
|
2016-07-08 12:33:32 -04:00
|
|
|
return str(currShortcutStringList)[1:-1].replace(" ","").replace("'","")
|
2016-08-11 08:37:46 -04:00
|
|
|
|
|
|
|
def getDevices(self):
|
|
|
|
self.iDevices = map(evdev.InputDevice, (evdev.list_devices()))
|
|
|
|
self.iDevices = {dev.fd: dev for dev in self.iDevices if 1 in dev.capabilities()}
|
|
|
|
|
|
|
|
for fd in self.iDevices:
|
|
|
|
dev = self.iDevices[fd]
|
|
|
|
cap = dev.capabilities()
|
|
|
|
del cap[0]
|
|
|
|
print(dev.name)
|
|
|
|
self.uDevices[fd] = UInput(
|
|
|
|
cap,
|
|
|
|
dev.name,
|
|
|
|
dev.info.vendor
|
|
|
|
# dev.info.product,
|
|
|
|
# dev.version,
|
|
|
|
# dev.info.bustype,
|
|
|
|
# '/dev/uinput'
|
|
|
|
)
|
|
|
|
dev.grab()
|
|
|
|
|
|
|
|
def freeDevices(self):
|
|
|
|
for fd in self.iDevices:
|
|
|
|
try:
|
|
|
|
self.iDevices[fd].ungrab()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.iDevices[fd].close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.uDevices[fd].close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.iDevices.clear()
|
|
|
|
self.uDevices.clear()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.freeDevices()
|
|
|
|
|
2016-07-10 12:34:44 -04:00
|
|
|
|