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

101 lines
3.8 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-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.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():
2016-08-11 17:16:44 -04:00
if self.isFenrirKey(environment, event): # a
environment['input']['consumeKey'] = not environment['input']['keyForeward']
2016-08-12 18:06:54 -04:00
if not environment['input']['consumeKey'] or environment['input']['keyForeward']:
2016-08-11 08:37:46 -04:00
self.uDevices[fd].write_event(event)
self.uDevices[fd].syn()
else:
2016-08-11 17:16:44 -04:00
keyString = ''
if self.isFenrirKey(environment, event):
keyString = 'FENRIR'
else:
keyString = str(event.code)
2016-08-11 08:37:46 -04:00
if event.type == evdev.ecodes.EV_KEY:
if event.value != 0:
2016-08-11 17:16:44 -04:00
environment['input']['currShortcut'][keyString] = 1 #event.value
2016-08-11 08:37:46 -04:00
else:
try:
2016-08-11 17:16:44 -04:00
del(environment['input']['currShortcut'][keyString])
2016-08-11 08:37:46 -04:00
except:
pass
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-11 17:16:44 -04:00
environment['input']['consumeKey'] = environment['input']['currShortcut'] != {} and environment['input']['consumeKey']
environment['input']['keyForeward'] = environment['input']['keyForeward'] and 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-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']
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]
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