Code optimization and bug fixes. Removed the broken, partial atspi driver implementation.

This commit is contained in:
Storm Dragon
2025-06-18 15:08:36 -04:00
parent d81d563bb6
commit 27dcff23bb
5 changed files with 156 additions and 452 deletions

View File

@ -24,6 +24,7 @@ from select import select
import multiprocessing
from multiprocessing.sharedctypes import Value
from ctypes import c_bool
import threading
from fenrirscreenreader.core.eventData import fenrirEventType
from fenrirscreenreader.core import inputData
@ -41,6 +42,7 @@ class driver(inputDriver):
self.iDeviceNo = 0
self.watchDog = Value(c_bool, True)
self.UInputinject = UInput()
self._deviceLock = threading.Lock()
def initialize(self, environment):
self.env = environment
self.env['runtime']['inputManager'].setShortcutType('KEY')
@ -101,29 +103,45 @@ class driver(inputDriver):
def inputWatchdog(self, active, eventQueue):
try:
while active.value:
r, w, x = select(self.iDevices, [], [], 0.8)
# Get a snapshot of devices for select() to avoid lock contention
with self._deviceLock:
devices_snapshot = self.iDevices.copy()
if not devices_snapshot:
time.sleep(0.1)
continue
r, w, x = select(devices_snapshot, [], [], 0.8)
event = None
foundKeyInSequence = False
foreward = False
eventFired = False
for fd in r:
# Check if device still exists before accessing
with self._deviceLock:
if fd not in self.iDevices:
continue
device = self.iDevices[fd]
udevice = self.uDevices.get(fd)
try:
event = self.iDevices[fd].read_one()
event = device.read_one()
except:
self.removeDevice(fd)
continue
while(event):
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([device, udevice, event])
if event.type == evdev.events.EV_KEY:
if not foundKeyInSequence:
foundKeyInSequence = True
if event.code != 0:
currMapEvent = self.mapEvent(event)
if not currMapEvent:
event = self.iDevices[fd].read_one()
event = device.read_one()
continue
if not isinstance(currMapEvent['EventName'], str):
event = self.iDevices[fd].read_one()
event = device.read_one()
continue
if currMapEvent['EventState'] in [0, 1, 2]:
eventQueue.put({"Type": fenrirEventType.KeyboardInput, "Data": currMapEvent.copy()})
@ -132,7 +150,7 @@ class driver(inputDriver):
if event.type in [2, 3]:
foreward = True
event = self.iDevices[fd].read_one()
event = device.read_one()
if not foundKeyInSequence:
if foreward and not eventFired:
self.writeEventBuffer()
@ -351,24 +369,26 @@ class driver(inputDriver):
def addDevice(self, newDevice):
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device added: ' + str(newDevice.fd) + ' ' + str(newDevice), debug.debugLevel.INFO)
try:
self.iDevices[newDevice.fd] = newDevice
self.createUInputDev(newDevice.fd)
self.gDevices[newDevice.fd] = False
except:
# if it doesnt work clean up
with self._deviceLock:
try:
del(self.iDevices[newDevice.fd])
except:
pass
try:
del(self.uDevices[newDevice.fd])
except:
pass
try:
del(self.gDevices[newDevice.fd])
except:
pass
self.iDevices[newDevice.fd] = newDevice
self.createUInputDev(newDevice.fd)
self.gDevices[newDevice.fd] = False
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: error adding device: ' + str(e), debug.debugLevel.ERROR)
# if it doesnt work clean up
try:
del(self.iDevices[newDevice.fd])
except:
pass
try:
del(self.uDevices[newDevice.fd])
except:
pass
try:
del(self.gDevices[newDevice.fd])
except:
pass
def grabDevice(self, fd):
if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
@ -424,33 +444,37 @@ class driver(inputDriver):
return True
def removeDevice(self, fd):
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd) + ' ' + str(self.iDevices[fd]), debug.debugLevel.INFO)
self.clearEventBuffer()
try:
self.ungrabDevice(fd)
except:
pass
try:
self.iDevices[fd].close()
except:
pass
try:
self.uDevices[fd].close()
except:
pass
try:
del(self.iDevices[fd])
except:
pass
try:
del(self.uDevices[fd])
except:
pass
try:
del(self.gDevices[fd])
except:
pass
self.updateMPiDevicesFD()
with self._deviceLock:
try:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd) + ' ' + str(self.iDevices[fd]), debug.debugLevel.INFO)
except:
self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd), debug.debugLevel.INFO)
self.clearEventBuffer()
try:
self.ungrabDevice(fd)
except:
pass
try:
self.iDevices[fd].close()
except:
pass
try:
self.uDevices[fd].close()
except:
pass
try:
del(self.iDevices[fd])
except:
pass
try:
del(self.uDevices[fd])
except:
pass
try:
del(self.gDevices[fd])
except:
pass
self.updateMPiDevicesFD()
def hasIDevices(self):
if not self._initialized: