basic device detection

This commit is contained in:
chrys 2017-02-27 21:51:29 +01:00
parent 6f9106d4a8
commit bdaf8e61fc
2 changed files with 72 additions and 46 deletions

View File

@ -22,7 +22,6 @@ class inputManager():
self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock'] self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock']
self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getLedState(2) self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getLedState(2)
self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock'] self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock']
self.grabDevices()
def shutdown(self): def shutdown(self):
self.env['runtime']['inputManager'].releaseDevices() self.env['runtime']['inputManager'].releaseDevices()

View File

@ -22,6 +22,7 @@ class driver():
def __init__(self): def __init__(self):
self.iDevices = {} self.iDevices = {}
self.uDevices = {} self.uDevices = {}
self.iDeviceNo = 0
self._initialized = False self._initialized = False
def initialize(self, environment): def initialize(self, environment):
@ -32,17 +33,16 @@ class driver():
global _evdevAvailableError global _evdevAvailableError
self.env['runtime']['debug'].writeDebugOut('InputDriver: ' + _evdevAvailableError,debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('InputDriver: ' + _evdevAvailableError,debug.debugLevel.ERROR)
return return
self.getInputDevices() self.updateInputDevices()
def shutdown(self): def shutdown(self):
if not self._initialized: if not self._initialized:
return return
def getInputEvent(self): def getInputEvent(self):
if not self.hasIDevices(): if not self.hasIDevices():
time.sleep(0.008) # dont flood CPU time.sleep(0.008) # dont flood CPU
return None return None
self.updateInputDevices()
event = None event = None
r, w, x = select(self.iDevices, [], [], self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')) r, w, x = select(self.iDevices, [], [], self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay'))
if r != []: if r != []:
@ -95,31 +95,55 @@ class driver():
return return
uDevice.write_event(event) uDevice.write_event(event)
uDevice.syn() uDevice.syn()
def getInputDevices(self):
if not self._initialized:
return
if self.iDevices != {}:
self.releaseDevices()
deviceList = evdev.list_devices()
readableDevices = []
for dev in deviceList:
try:
open(dev)
readableDevices.append(dev)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut("Skip Inputdevice : " + dev +' ' + str(e),debug.debugLevel.ERROR)
self.iDevices = map(evdev.InputDevice, (readableDevices))
def updateInputDevices(self, force = False, init = False):
if init:
self.iDevices = {}
self.iDeviceNo = 0
deviceFileList = evdev.list_devices()
if not force:
if len(deviceFileList) == self.iDeviceNo:
return
self.iDeviceNo = len(deviceFileList)
mode = self.env['runtime']['settingsManager'].getSetting('keyboard', 'device').upper()
iDevicesFiles = []
for device in self.iDevices:
iDevicesFiles.append(self.iDevices[device].fn)
if len(iDevicesFiles) == len(deviceFileList):
return
for deviceFile in deviceFileList:
try:
if deviceFile in iDevicesFiles:
print('skip')
continue
open(deviceFile)
# 3 pos absolute # 3 pos absolute
# 2 pos relative # 2 pos relative
# 1 Keys # 1 Keys
# we try to filter out mices and other stuff here currDevice = evdev.InputDevice(deviceFile)
if self.env['runtime']['settingsManager'].getSetting('keyboard', 'device').upper() == 'ALL': if currDevice.name.upper() in ['SPEAKUP','PY-EVDEV-UINPUT']:
self.iDevices = {dev.fd: dev for dev in self.iDevices if 1 in dev.capabilities()} continue
elif self.env['runtime']['settingsManager'].getSetting('keyboard', 'device').upper() == 'NOMICE': cap = currDevice.capabilities()
self.iDevices = {dev.fd: dev for dev in self.iDevices if 1 in dev.capabilities() and not 3 in dev.capabilities() and not 2 in dev.capabilities()} if mode in ['ALL','NOMICE']:
else: if 1 in cap:
self.iDevices = {dev.fd: dev for dev in self.iDevices if dev.name.upper() in self.env['runtime']['settingsManager'].getSetting('keyboard', 'device').upper().split(',')} if 116 in cap[1] and len(cap[1]) < 5:
print('power')
continue
if mode == 'ALL':
self.iDevices[currDevice.fd] = currDevice
self.grabDevice(currDevice.fd)
print('Device added (ALL):' + self.iDevices[currDevice.fd].name)
elif mode == 'NOMICE':
if not ((2 in cap) or (3 in cap)):
self.iDevices[currDevice.fd] = currDevice
self.grabDevice(currDevice.fd)
print('Device added (NOMICE):' + self.iDevices[currDevice.fd].name)
elif currDevice.name.upper() in mode.split(','):
self.iDevices[currDevice.fd] = currDevice
self.grabDevice(currDevice.fd)
print('Device added (Name):' + self.iDevices[currDevice.fd].name)
except Exception as e:
print("Skip Inputdevice : " + deviceFile +' ' + str(e))
def mapEvent(self, event): def mapEvent(self, event):
if not self._initialized: if not self._initialized:
@ -162,6 +186,8 @@ class driver():
if not self._initialized: if not self._initialized:
return return
for fd in self.iDevices: for fd in self.iDevices:
self.grabDevice()
def grabDevice(self, fd):
try: try:
self.uDevices[fd] = UInput.from_device(self.iDevices[fd].fn) self.uDevices[fd] = UInput.from_device(self.iDevices[fd].fn)
except Exception as e: except Exception as e:
@ -197,6 +223,7 @@ class driver():
# ) # )
# dev.grab() # dev.grab()
def removeDevice(self,fd): def removeDevice(self,fd):
self.clearEventBuffer()
try: try:
self.iDevices[fd].ungrab() self.iDevices[fd].ungrab()
except: except: