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

95 lines
3.5 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-07 15:40:10 -04:00
import evdev
from evdev import InputDevice
from select import select
class inputManager():
def __init__(self):
2016-07-07 15:40:10 -04:00
self.devices = map(evdev.InputDevice, (evdev.list_devices()))
self.devices = {dev.fd: dev for dev in self.devices}
2016-07-08 20:48:22 -04:00
#for dev in self.devices.values(): print(dev)
2016-07-07 15:40:10 -04:00
2016-07-08 05:24:34 -04:00
def getKeyPressed(self, environment):
2016-07-13 15:40:19 -04:00
try:
r, w, x = select(self.devices, [], [])
2016-07-14 17:00:02 -04:00
environment['runtime']['globalLock'].acquire(True)
2016-07-13 15:40:19 -04:00
if r != []:
for fd in r:
for event in self.devices[fd].read():
if event.type == evdev.ecodes.EV_KEY:
if event.value != 0:
2016-07-14 17:25:33 -04:00
environment['input']['currShortcut'][str(event.code)] = event.value
2016-07-13 15:40:19 -04:00
else:
try:
2016-07-14 17:25:33 -04:00
del(environment['input']['currShortcut'][str(event.code)])
2016-07-13 15:40:19 -04:00
except:
pass
except:
pass
2016-07-08 12:33:32 -04:00
environment['input']['currShortcutString'] = self.getShortcutString(environment)
2016-07-08 05:24:34 -04:00
return environment
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-07-10 12:34:44 -04:00
2016-07-10 13:09:50 -04:00
def loadShortcuts(self, environment, kbConfigPath='../../config/keyboard/desktop.kb'):
2016-07-10 12:34:44 -04:00
kbConfig = open(kbConfigPath,"r")
while(True):
line = kbConfig.readline()
if not line:
break
line = line.replace('\n','')
if line.replace(" ","").startswith("#"):
continue
2016-07-11 04:40:40 -04:00
if line.split('#')[0].count("=") != 1:
2016-07-10 12:34:44 -04:00
continue
2016-07-11 04:40:40 -04:00
sepLine = line.split('#')[0].split('=')
2016-07-10 12:34:44 -04:00
commandString = sepLine[1]
keys = sepLine[0].replace(" ","").split(',')
currShortcut = []
validKeyString = True
for key in keys:
if len(key) < 3:
validKeyString = False
break
if not key[0] in ['0','1','2']:
validKeyString = False
break
if key[1] != '-':
validKeyString = False
break
if key[2:] != '':
keyInt = self.getCodeForKeyID(key[2:])
else:
validKeyString = False
break
if keyInt == 0:
validKeyString = False
break
if not validKeyString:
break
else:
currShortcut.append(key[0] + '-' + str(keyInt))
if validKeyString:
keyString = ''
for k in sorted(currShortcut):
if keyString != '':
keyString += ','
keyString += k
environment['bindings'][keyString] = commandString
kbConfig.close()
return environment
def getCodeForKeyID(self, keyID):
try:
return evdev.ecodes.ecodes[keyID.upper()]
except:
return 0