fenrir/src/fenrir-package/core/inputManager.py

122 lines
4.7 KiB
Python
Raw Normal View History

#!/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-08-23 18:41:16 -04:00
from utils import debug
2016-07-07 15:40:10 -04:00
class inputManager():
def __init__(self):
2016-08-11 08:37:46 -04:00
self.iDevices = {}
self.uDevices = {}
self.getInputDevices()
self.grabDevices()
2016-08-13 18:11:37 -04:00
self.ignoreKeyRelease = 0
2016-07-07 15:40:10 -04:00
def proceedInputEvents(self, environment):
2016-07-16 18:56:18 -04:00
timeout = True
2016-08-13 18:11:37 -04:00
if not environment['input']['keyForeward']:
self.ignoreKeyRelease = 0
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 self.isFenrirKey(environment, event):
2016-08-11 17:16:44 -04:00
environment['input']['consumeKey'] = not environment['input']['keyForeward']
if self.isConsumeKeypress(environment):
self.writeUInput(self.uDevices[fd], event)
2016-08-13 18:11:37 -04:00
keyString = ''
if self.isFenrirKey(environment, event):
keyString = 'FENRIR'
2016-08-11 08:37:46 -04:00
else:
2016-08-13 18:11:37 -04:00
keyString = str(event.code)
if event.type == evdev.ecodes.EV_KEY:
if event.value != 0:
environment['input']['currShortcut'][keyString] = 1 #event.value
2016-08-11 17:16:44 -04:00
else:
2016-08-13 18:11:37 -04:00
try:
del(environment['input']['currShortcut'][keyString])
except:
pass
2016-08-11 08:37:46 -04:00
except Exception as e:
self.freeDevices()
2016-08-11 17:16:44 -04:00
time.sleep(0.01)
2016-07-08 12:33:32 -04:00
environment['input']['currShortcutString'] = self.getShortcutString(environment)
2016-08-13 18:11:37 -04:00
if not timeout:
environment['input']['lastInputTime'] = time.time()
2016-08-13 18:11:37 -04:00
environment['input']['consumeKey'] = environment['input']['currShortcut'] != {} and environment['input']['consumeKey']
if (environment['input']['keyForeward'] and environment['input']['currShortcut'] == {}):
self.ignoreKeyRelease += 1
if self.ignoreKeyRelease >= 2: # a hack... has to bee done more clean
environment['input']['keyForeward'] = environment['input']['keyForeward'] and not environment['input']['currShortcut'] == {}
2016-07-16 18:56:18 -04:00
return environment, timeout
def isConsumeKeypress(self, environment):
return not environment['input']['consumeKey'] or \
environment['input']['keyForeward'] or \
not environment['runtime']['settingsManager'].getSettingAsBool(environment, 'keyboard', 'grabDevices')
def writeUInput(self, uDevice, event):
uDevice.write_event(event)
uDevice.syn()
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-08-11 17:16:44 -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 17:16:44 -04:00
def isFenrirKey(self,environment, event):
return str(event.code) in environment['input']['fenrirKey']
def getInputDevices(self):
2016-08-11 08:37:46 -04:00
self.iDevices = map(evdev.InputDevice, (evdev.list_devices()))
self.iDevices = {dev.fd: dev for dev in self.iDevices if 1 in dev.capabilities()}
def grabDevices(self):
# if environment['runtime']['settingsManager'].getSettingAsBool(environment, 'keyboard', 'grabDevices'):
# return
2016-08-11 08:37:46 -04:00
for fd in self.iDevices:
dev = self.iDevices[fd]
cap = dev.capabilities()
del cap[0]
self.uDevices[fd] = UInput(
cap,
dev.name,
2016-08-13 18:11:37 -04:00
#dev.info.vendor,
#dev.info.product,
#dev.version,
#dev.info.bustype,
#'/dev/uinput'
2016-08-11 08:37:46 -04:00
)
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