Experimental fix for evdev failures.

This commit is contained in:
Storm Dragon 2025-03-02 17:24:45 -05:00
parent e76ca9889a
commit 09391bfe84

View File

@ -59,7 +59,7 @@ class driver(inputDriver):
self.env['runtime']['processManager'].addCustomEventThread(self.inputWatchdog) self.env['runtime']['processManager'].addCustomEventThread(self.inputWatchdog)
self._initialized = True self._initialized = True
def plugInputDeviceWatchdogUdev(self,active , eventQueue): def plugInputDeviceWatchdogUdev(self, active, eventQueue):
context = pyudev.Context() context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context) monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='input') monitor.filter_by(subsystem='input')
@ -72,31 +72,33 @@ class driver(inputDriver):
self.env['runtime']['debug'].writeDebugOut('plugInputDeviceWatchdogUdev:' + str(device), debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('plugInputDeviceWatchdogUdev:' + str(device), debug.debugLevel.INFO)
try: try:
try: try:
if device.name.upper() in ['','SPEAKUP','FENRIR-UINPUT']: # FIX: Check if attributes exist before accessing them
if hasattr(device, 'name') and device.name and device.name.upper() in ['','SPEAKUP','FENRIR-UINPUT']:
ignorePlug = True ignorePlug = True
if device.phys.upper() in ['','SPEAKUP','FENRIR-UINPUT']: if hasattr(device, 'phys') and device.phys and device.phys.upper() in ['','SPEAKUP','FENRIR-UINPUT']:
ignorePlug = True ignorePlug = True
if 'BRLTTY' in device.name.upper(): if hasattr(device, 'name') and device.name and 'BRLTTY' in device.name.upper():
ignorePlug = True ignorePlug = True
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut("plugInputDeviceWatchdogUdev CHECK NAME CRASH: " + str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut("plugInputDeviceWatchdogUdev CHECK NAME CRASH: " + str(e), debug.debugLevel.ERROR)
if not ignorePlug: if not ignorePlug:
virtual = '/sys/devices/virtual/input/' in device.sys_path virtual = '/sys/devices/virtual/input/' in device.sys_path
if device.device_node: if device.device_node:
validDevices.append({'device': device.device_node, 'virtual': virtual}) validDevices.append({'device': device.device_node, 'virtual': virtual})
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut("plugInputDeviceWatchdogUdev APPEND CRASH: " + str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut("plugInputDeviceWatchdogUdev APPEND CRASH: " + str(e), debug.debugLevel.ERROR)
try: try:
pollTimeout = 1 pollTimeout = 1
device = monitor.poll(pollTimeout) device = monitor.poll(pollTimeout)
except: except Exception:
device = None device = None
ignorePlug = False ignorePlug = False
if validDevices: if validDevices:
eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":validDevices}) eventQueue.put({"Type": fenrirEventType.PlugInputDevice, "Data": validDevices})
return time.time() return time.time()
def inputWatchdog(self,active , eventQueue): def inputWatchdog(self, active, eventQueue):
try: try:
while active.value: while active.value:
r, w, x = select(self.iDevices, [], [], 0.8) r, w, x = select(self.iDevices, [], [], 0.8)
@ -111,7 +113,7 @@ class driver(inputDriver):
self.removeDevice(fd) self.removeDevice(fd)
while(event): while(event):
self.env['runtime']['debug'].writeDebugOut('inputWatchdog: EVENT:' + str(event), debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('inputWatchdog: EVENT:' + str(event), debug.debugLevel.INFO)
self.env['input']['eventBuffer'].append( [self.iDevices[fd], self.uDevices[fd], event]) self.env['input']['eventBuffer'].append([self.iDevices[fd], self.uDevices[fd], event])
if event.type == evdev.events.EV_KEY: if event.type == evdev.events.EV_KEY:
if not foundKeyInSequence: if not foundKeyInSequence:
foundKeyInSequence = True foundKeyInSequence = True
@ -123,11 +125,11 @@ class driver(inputDriver):
if not isinstance(currMapEvent['EventName'], str): if not isinstance(currMapEvent['EventName'], str):
event = self.iDevices[fd].read_one() event = self.iDevices[fd].read_one()
continue continue
if currMapEvent['EventState'] in [0,1,2]: if currMapEvent['EventState'] in [0, 1, 2]:
eventQueue.put({"Type":fenrirEventType.KeyboardInput,"Data":currMapEvent.copy()}) eventQueue.put({"Type": fenrirEventType.KeyboardInput, "Data": currMapEvent.copy()})
eventFired = True eventFired = True
else: else:
if event.type in [2,3]: if event.type in [2, 3]:
foreward = True foreward = True
event = self.iDevices[fd].read_one() event = self.iDevices[fd].read_one()
@ -136,7 +138,7 @@ class driver(inputDriver):
self.writeEventBuffer() self.writeEventBuffer()
self.clearEventBuffer() self.clearEventBuffer()
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut("INPUT WATCHDOG CRASH: "+str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut("INPUT WATCHDOG CRASH: " + str(e), debug.debugLevel.ERROR)
def writeEventBuffer(self): def writeEventBuffer(self):
if not self._initialized: if not self._initialized:
@ -146,7 +148,7 @@ class driver(inputDriver):
if uDevice: if uDevice:
if self.gDevices[iDevice.fd]: if self.gDevices[iDevice.fd]:
self.writeUInput(uDevice, event) self.writeUInput(uDevice, event)
except Exception as e: except Exception:
pass pass
def writeUInput(self, uDevice, event): def writeUInput(self, uDevice, event):
@ -156,7 +158,7 @@ class driver(inputDriver):
time.sleep(0.0000002) time.sleep(0.0000002)
uDevice.syn() uDevice.syn()
def updateInputDevices(self, newDevices = None, init = False): def updateInputDevices(self, newDevices=None, init=False):
if init: if init:
self.removeAllDevices() self.removeAllDevices()
@ -191,7 +193,7 @@ class driver(inputDriver):
try: try:
with open(deviceFile) as f: with open(deviceFile) as f:
pass pass
except Exception as e: except Exception:
continue continue
# 3 pos absolute # 3 pos absolute
# 2 pos relative # 2 pos relative
@ -201,22 +203,23 @@ class driver(inputDriver):
except: except:
continue continue
try: try:
if currDevice.name.upper() in ['','SPEAKUP','FENRIR-UINPUT']: # FIX: Check if attributes exist before accessing them
if hasattr(currDevice, 'name') and currDevice.name and currDevice.name.upper() in ['', 'SPEAKUP', 'FENRIR-UINPUT']:
continue continue
if currDevice.phys.upper() in ['','SPEAKUP','FENRIR-UINPUT']: if hasattr(currDevice, 'phys') and currDevice.phys and currDevice.phys.upper() in ['', 'SPEAKUP', 'FENRIR-UINPUT']:
continue continue
if 'BRLTTY' in currDevice.name.upper(): if hasattr(currDevice, 'name') and currDevice.name and 'BRLTTY' in currDevice.name.upper():
continue continue
except: except:
pass pass
cap = currDevice.capabilities() cap = currDevice.capabilities()
if mode in ['ALL','NOMICE']: if mode in ['ALL', 'NOMICE']:
if eventType.EV_KEY in cap: if eventType.EV_KEY in cap:
if 116 in cap[eventType.EV_KEY] and len(cap[eventType.EV_KEY]) < 10: if 116 in cap[eventType.EV_KEY] and len(cap[eventType.EV_KEY]) < 10:
self.env['runtime']['debug'].writeDebugOut('Device Skipped (has 116):' + currDevice.name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device Skipped (has 116):' + currDevice.name, debug.debugLevel.INFO)
continue continue
if len(cap[eventType.EV_KEY]) < 60: if len(cap[eventType.EV_KEY]) < 60:
self.env['runtime']['debug'].writeDebugOut('Device Skipped (< 60 keys):' + currDevice.name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device Skipped (< 60 keys):' + currDevice.name, debug.debugLevel.INFO)
continue continue
if mode == 'ALL': if mode == 'ALL':
self.addDevice(currDevice) self.addDevice(currDevice)
@ -224,16 +227,20 @@ class driver(inputDriver):
elif mode == 'NOMICE': elif mode == 'NOMICE':
if not ((eventType.EV_REL in cap) or (eventType.EV_ABS in cap)): if not ((eventType.EV_REL in cap) or (eventType.EV_ABS in cap)):
self.addDevice(currDevice) self.addDevice(currDevice)
self.env['runtime']['debug'].writeDebugOut('Device added (NOMICE):' + self.iDevices[currDevice.fd].name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device added (NOMICE):' + self.iDevices[currDevice.fd].name, debug.debugLevel.INFO)
else: else:
self.env['runtime']['debug'].writeDebugOut('Device Skipped (NOMICE):' + currDevice.name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device Skipped (NOMICE):' + currDevice.name, debug.debugLevel.INFO)
else: else:
self.env['runtime']['debug'].writeDebugOut('Device Skipped (no EV_KEY):' + currDevice.name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device Skipped (no EV_KEY):' + currDevice.name, debug.debugLevel.INFO)
elif currDevice.name.upper() in mode.split(','): elif currDevice.name.upper() in mode.split(','):
self.addDevice(currDevice) self.addDevice(currDevice)
self.env['runtime']['debug'].writeDebugOut('Device added (Name):' + self.iDevices[currDevice.fd].name,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('Device added (Name):' + self.iDevices[currDevice.fd].name, debug.debugLevel.INFO)
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut("Device Skipped (Exception): " + deviceFile +' ' + currDevice.name +' '+ str(e),debug.debugLevel.INFO) try:
device_name = currDevice.name if hasattr(currDevice, 'name') else "unknown"
self.env['runtime']['debug'].writeDebugOut("Device Skipped (Exception): " + deviceFile + ' ' + device_name + ' ' + str(e), debug.debugLevel.INFO)
except:
self.env['runtime']['debug'].writeDebugOut("Device Skipped (Exception): " + deviceFile + ' ' + str(e), debug.debugLevel.INFO)
self.iDeviceNo = len(evdev.list_devices()) self.iDeviceNo = len(evdev.list_devices())
self.updateMPiDevicesFD() self.updateMPiDevicesFD()
@ -247,6 +254,7 @@ class driver(inputDriver):
self.iDevicesFD.remove(fd) self.iDevicesFD.remove(fd)
except: except:
pass pass
def mapEvent(self, event): def mapEvent(self, event):
if not self._initialized: if not self._initialized:
return None return None
@ -266,12 +274,12 @@ class driver(inputDriver):
mEvent['EventSec'] = event.sec mEvent['EventSec'] = event.sec
mEvent['EventUsec'] = event.usec mEvent['EventUsec'] = event.usec
mEvent['EventState'] = event.value mEvent['EventState'] = event.value
mEvent['EventType'] = event.type mEvent['EventType'] = event.type
return mEvent return mEvent
except Exception as e: except Exception:
return None return None
def getLedState(self, led = 0): def getLedState(self, led=0):
if not self.hasIDevices(): if not self.hasIDevices():
return False return False
# 0 = Numlock # 0 = Numlock
@ -281,7 +289,8 @@ class driver(inputDriver):
if led in dev.leds(): if led in dev.leds():
return True return True
return False return False
def toggleLedState(self, led = 0):
def toggleLedState(self, led=0):
if not self.hasIDevices(): if not self.hasIDevices():
return False return False
ledState = self.getLedState(led) ledState = self.getLedState(led)
@ -290,9 +299,10 @@ class driver(inputDriver):
# 17 LEDs # 17 LEDs
if 17 in self.iDevices[i].capabilities(): if 17 in self.iDevices[i].capabilities():
if ledState == 1: if ledState == 1:
self.iDevices[i].set_led(led , 0) self.iDevices[i].set_led(led, 0)
else: else:
self.iDevices[i].set_led(led , 1) self.iDevices[i].set_led(led, 1)
def grabAllDevices(self): def grabAllDevices(self):
if not self._initialized: if not self._initialized:
return True return True
@ -301,6 +311,7 @@ class driver(inputDriver):
if not self.gDevices[fd]: if not self.gDevices[fd]:
ok = ok and self.grabDevice(fd) ok = ok and self.grabDevice(fd)
return ok return ok
def ungrabAllDevices(self): def ungrabAllDevices(self):
if not self._initialized: if not self._initialized:
return True return True
@ -309,6 +320,7 @@ class driver(inputDriver):
if self.gDevices[fd]: if self.gDevices[fd]:
ok = ok and self.ungrabDevice(fd) ok = ok and self.ungrabDevice(fd)
return ok return ok
def createUInputDev(self, fd): def createUInputDev(self, fd):
if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
self.uDevices[fd] = None self.uDevices[fd] = None
@ -324,20 +336,21 @@ class driver(inputDriver):
self.uDevices[fd] = UInput.from_device(self.iDevices[fd], name='fenrir-uinput', phys='fenrir-uinput') self.uDevices[fd] = UInput.from_device(self.iDevices[fd], name='fenrir-uinput', phys='fenrir-uinput')
except Exception as e: except Exception as e:
try: try:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: compat fallback: ' + str(e),debug.debugLevel.WARNING) self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: compat fallback: ' + str(e), debug.debugLevel.WARNING)
dev = self.iDevices[fd] dev = self.iDevices[fd]
cap = dev.capabilities() cap = dev.capabilities()
del cap[0] del cap[0]
self.uDevices[fd] = UInput( self.uDevices[fd] = UInput(
cap, cap,
name = 'fenrir-uinput', name='fenrir-uinput',
phys = 'fenrir-uinput' phys='fenrir-uinput'
) )
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: init Uinput not possible: ' + str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: init Uinput not possible: ' + str(e), debug.debugLevel.ERROR)
return return
def addDevice(self, newDevice): def addDevice(self, newDevice):
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device added: ' + str(newDevice.fd) + ' ' +str(newDevice),debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device added: ' + str(newDevice.fd) + ' ' + str(newDevice), debug.debugLevel.INFO)
try: try:
self.iDevices[newDevice.fd] = newDevice self.iDevices[newDevice.fd] = newDevice
self.createUInputDev(newDevice.fd) self.createUInputDev(newDevice.fd)
@ -360,10 +373,13 @@ class driver(inputDriver):
def grabDevice(self, fd): def grabDevice(self, fd):
if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
return True return True
# FIX: Handle exception variable scope correctly
grab_error = None
try: try:
self.iDevices[fd].grab() self.iDevices[fd].grab()
self.gDevices[fd] = True self.gDevices[fd] = True
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: grab device ('+ str(self.iDevices[fd].name) + ')',debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: grab device (' + str(self.iDevices[fd].name) + ')', debug.debugLevel.INFO)
# Reset modifier keys on successful grab # Reset modifier keys on successful grab
if self.uDevices[fd]: if self.uDevices[fd]:
modifierKeys = [e.KEY_LEFTCTRL, e.KEY_RIGHTCTRL, e.KEY_LEFTALT, e.KEY_RIGHTALT, e.KEY_LEFTSHIFT, e.KEY_RIGHTSHIFT] modifierKeys = [e.KEY_LEFTCTRL, e.KEY_RIGHTCTRL, e.KEY_LEFTALT, e.KEY_RIGHTALT, e.KEY_LEFTSHIFT, e.KEY_RIGHTSHIFT]
@ -371,33 +387,44 @@ class driver(inputDriver):
try: try:
self.uDevices[fd].write(e.EV_KEY, key, 0) # 0 = key up self.uDevices[fd].write(e.EV_KEY, key, 0) # 0 = key up
self.uDevices[fd].syn() self.uDevices[fd].syn()
except Exception as e: except Exception as mod_error:
self.env['runtime']['debug'].writeDebugOut('Failed to reset modifier key: ' + str(e), debug.debugLevel.WARNING) self.env['runtime']['debug'].writeDebugOut('Failed to reset modifier key: ' + str(mod_error), debug.debugLevel.WARNING)
except IOError: except IOError:
if not self.gDevices[fd]: if not self.gDevices[fd]:
return False return False
except Exception as e: except Exception as ex:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: grabing not possible: ' + str(e),debug.debugLevel.ERROR) grab_error = ex
if grab_error:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: grabing not possible: ' + str(grab_error), debug.debugLevel.ERROR)
return False return False
return True return True
def ungrabDevice(self,fd): def ungrabDevice(self, fd):
if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
return True return True
# FIX: Handle exception variable scope correctly
ungrab_error = None
try: try:
self.iDevices[fd].ungrab() self.iDevices[fd].ungrab()
self.gDevices[fd] = False self.gDevices[fd] = False
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: ungrab device ('+ str(self.iDevices[fd].name) + ')',debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: ungrab device (' + str(self.iDevices[fd].name) + ')', debug.debugLevel.INFO)
except IOError: except IOError:
if self.gDevices[fd]: if self.gDevices[fd]:
return False return False
# self.gDevices[fd] = False except Exception as ex:
# #self.removeDevice(fd) ungrab_error = ex
except Exception as e:
if ungrab_error:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: ungrabing not possible: ' + str(ungrab_error), debug.debugLevel.ERROR)
return False return False
return True return True
def removeDevice(self,fd):
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd) + ' ' +str(self.iDevices[fd]),debug.debugLevel.INFO) def removeDevice(self, fd):
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd) + ' ' + str(self.iDevices[fd]), debug.debugLevel.INFO)
self.clearEventBuffer() self.clearEventBuffer()
try: try:
self.ungrabDevice(fd) self.ungrabDevice(fd)
@ -452,4 +479,4 @@ class driver(inputDriver):
self.iDevices.clear() self.iDevices.clear()
self.uDevices.clear() self.uDevices.clear()
self.gDevices.clear() self.gDevices.clear()
self.iDeviceNo = 0 self.iDeviceNo = 0