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

139 lines
5.7 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2016-08-11 08:37:46 -04:00
import time
from core import debug
2016-09-17 17:38:40 -04:00
from core import inputEvent
2016-07-07 15:40:10 -04:00
class inputManager():
def __init__(self):
pass
def initialize(self, environment):
self.env = environment
self.env['runtime']['settingsManager'].loadDriver(\
self.env['runtime']['settingsManager'].getSetting('keyboard', 'driver'), 'inputDriver')
2016-09-18 07:13:58 -04:00
# init LEDs with current state
self.env['input']['newNumLock'] = self.env['runtime']['inputDriver'].getNumlock()
self.env['input']['oldNumLock'] = self.env['input']['newNumLock']
self.env['input']['newCapsLock'] = self.env['runtime']['inputDriver'].getCapslock()
self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock']
self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getScrollLock()
self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock']
self.grabDevices()
def shutdown(self):
self.env['runtime']['inputManager'].releaseDevices()
self.env['runtime']['settingsManager'].shutdownDriver('inputDriver')
2016-09-21 12:07:28 -04:00
def getInputEvent(self):
2016-09-21 12:07:28 -04:00
eventReceived = False
mEvent = self.env['runtime']['inputDriver'].getInputEvent()
2016-09-18 09:13:24 -04:00
if mEvent:
mEvent['EventName'] = self.convertEventName(mEvent['EventName'])
2016-09-17 17:38:40 -04:00
if mEvent['EventValue'] == 0:
2016-09-21 04:00:45 -04:00
return False
eventReceived = True
2016-09-17 17:38:40 -04:00
if mEvent['EventState'] == 0:
if mEvent['EventName'] in self.env['input']['currInput']:
self.env['input']['currInput'].remove(mEvent['EventName'])
self.env['input']['currInput'] = sorted(self.env['input']['currInput'])
2016-09-17 17:38:40 -04:00
elif mEvent['EventState'] == 1:
if not mEvent['EventName'] in self.env['input']['currInput']:
self.env['input']['currInput'].append(mEvent['EventName'])
self.env['input']['currInput'] = sorted(self.env['input']['currInput'])
2016-09-17 17:38:40 -04:00
elif mEvent['EventState'] == 2:
pass
else:
pass
self.env['input']['oldNumLock'] = self.env['input']['newNumLock']
self.env['input']['newNumLock'] = self.env['runtime']['inputDriver'].getNumlock()
self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock']
self.env['input']['newCapsLock'] = self.env['runtime']['inputDriver'].getCapslock()
self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock']
self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getScrollLock()
self.env['input']['lastInputTime'] = time.time()
self.env['input']['shortcutRepeat'] = 1
2016-09-21 04:00:45 -04:00
return eventReceived
2016-09-12 17:29:16 -04:00
def grabDevices(self):
if self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
self.env['runtime']['inputDriver'].grabDevices()
def releaseDevices(self):
self.env['runtime']['inputDriver'].releaseDevices()
2016-09-21 03:56:59 -04:00
def convertEventName(self, eventName):
2016-09-21 12:07:28 -04:00
if not eventName:
return ''
2016-09-21 03:56:59 -04:00
if eventName == 'KEY_LEFTCTRL':
eventName == 'KEY_CTRL'
elif eventName == 'KEY_RIGHTCTRL':
eventName = 'KEY_CTRL'
elif eventName == 'KEY_LEFTSHIFT':
eventName = 'KEY_SHIFT'
elif eventName == 'KEY_RIGHTSHIFT':
eventName = 'KEY_SHIFT'
elif eventName == 'KEY_LEFTALT':
eventName = 'KEY_ALT'
elif eventName == 'KEY_RIGHTALT':
eventName = 'KEY_ALT'
if self.isFenrirKey(eventName):
2016-09-21 12:07:28 -04:00
eventName = 'KEY_FENRIR'
return eventName
def isConsumeInput(self):
return self.env['runtime']['commandManager'].isCommandQueued() and \
not self.env['input']['keyForeward']
2016-09-21 12:07:28 -04:00
#and
# not (self.env['input']['keyForeward'] or \
# self.env['runtime']['settingsManager'].getSettingAsBool(, 'keyboard', 'grabDevices'))
2016-09-18 09:13:24 -04:00
def clearEventBuffer(self):
self.env['runtime']['inputDriver'].clearEventBuffer()
def writeEventBuffer(self):
2016-08-31 07:27:04 -04:00
try:
self.env['runtime']['inputDriver'].writeEventBuffer()
2016-09-02 12:08:44 -04:00
except Exception as e:
2016-09-18 09:13:24 -04:00
print(e)
self.env['runtime']['debug'].writeDebugOut("Error while writeUInput",debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
2016-09-21 12:07:28 -04:00
def isFenrirKeyPressed(self):
return 'KEY_FENRIR' in self.env['input']['currInput']
2016-09-12 17:29:16 -04:00
def noKeyPressed(self):
return self.env['input']['currInput'] == []
def getPrevDeepestInput(self):
2016-09-16 19:59:38 -04:00
shortcut = []
shortcut.append(self.env['input']['shortcutRepeat'])
shortcut.append(sorted(self.env['input']['prevDeepestInput']))
2016-09-16 19:59:38 -04:00
def getPrevShortcut(self):
2016-09-16 19:59:38 -04:00
shortcut = []
shortcut.append(self.env['input']['shortcutRepeat'])
shortcut.append(sorted(self.env['input']['prevInput']))
2016-09-17 17:38:40 -04:00
return str(shortcut)
2016-09-16 19:59:38 -04:00
def getCurrShortcut(self):
2016-09-16 19:59:38 -04:00
shortcut = []
shortcut.append(self.env['input']['shortcutRepeat'])
shortcut.append(sorted(self.env['input']['currInput']))
2016-09-17 17:38:40 -04:00
return str(shortcut)
def isFenrirKey(self, eventName):
return eventName in self.env['input']['fenrirKey']
def getCommandForShortcut(self, shortcut):
shortcut = shortcut.upper()
if not self.shortcutExists(shortcut):
return ''
return self.env['bindings'][shortcut].upper()
def shortcutExists(self, shortcut):
return( str(shortcut).upper() in self.env['bindings'])