Updated brlapi driver. Hopefully will now actually work.

This commit is contained in:
Storm Dragon 2024-12-08 05:02:29 -05:00
parent 1696d62526
commit 3757a1ceeb

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Fenrir TTY screen reader # Fenrir TTY screen reader
@ -8,8 +8,7 @@ from fenrirscreenreader.core import debug
from fenrirscreenreader.core.brailleDriver import brailleDriver from fenrirscreenreader.core.brailleDriver import brailleDriver
class driver(brailleDriver): class driver(brailleDriver):
"""BRLAPI implementation of the braille driver interface. """BRLAPI implementation of the braille driver interface."""
Supports all braille displays compatible with BRLTTY."""
def __init__(self): def __init__(self):
"""Initialize the BRLAPI driver.""" """Initialize the BRLAPI driver."""
@ -24,7 +23,6 @@ class driver(brailleDriver):
import brlapi import brlapi
self._brl = brlapi.Connection() self._brl = brlapi.Connection()
self._deviceSize = self._brl.displaySize self._deviceSize = self._brl.displaySize
# Accept all key types from any braille display
self._brl.acceptKeys(brlapi.rangeType_all, [0, brlapi.KEY_MAX]) self._brl.acceptKeys(brlapi.rangeType_all, [0, brlapi.KEY_MAX])
self._current_cursor_pos = 0 self._current_cursor_pos = 0
except Exception as e: except Exception as e:
@ -39,16 +37,31 @@ class driver(brailleDriver):
return self._deviceSize return self._deviceSize
def writeText(self, text): def writeText(self, text):
"""Write text to the braille display.""" """Write text to the braille display.
Handles both flush and non-flush text from the output manager."""
if not self._isInitialized: if not self._isInitialized:
return return
try: try:
# Remove the 'flush ' or 'notflush ' prefix
if text.startswith('flush '):
text = text[6:] # Remove 'flush ' prefix
elif text.startswith('notflush '):
text = text[9:] # Remove 'notflush ' prefix
self._last_written_text = text self._last_written_text = text
# Handle unicode properly for international braille # Handle unicode properly for international braille
if isinstance(text, str): if isinstance(text, str):
self._brl.writeText(text) self._brl.writeText(text)
else: else:
self._brl.writeText(text.decode('utf-8', 'replace')) self._brl.writeText(text.decode('utf-8', 'replace'))
# Update cursor if needed
if 'screen' in self.env and 'newCursorReview' in self.env['screen']:
new_pos = self.env['screen']['newCursorReview']['x']
if new_pos != self._current_cursor_pos:
self.setCursor(new_pos)
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut('BRAILLE.writeText ' + str(e), debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('BRAILLE.writeText ' + str(e), debug.debugLevel.ERROR)
@ -57,7 +70,6 @@ class driver(brailleDriver):
try: try:
import brlapi import brlapi
self._brl = brlapi.Connection() self._brl = brlapi.Connection()
# Accept all key types from the connected display
self._brl.acceptKeys(brlapi.rangeType_all, [0, brlapi.KEY_MAX]) self._brl.acceptKeys(brlapi.rangeType_all, [0, brlapi.KEY_MAX])
return True return True
except Exception as e: except Exception as e:
@ -89,7 +101,6 @@ class driver(brailleDriver):
try: try:
max_pos = self.getDeviceSize()[0] - 1 max_pos = self.getDeviceSize()[0] - 1
self._current_cursor_pos = max(0, min(position, max_pos)) self._current_cursor_pos = max(0, min(position, max_pos))
# Use BRLAPI's cursor command which works across different displays
self._brl.writeDots(1 << self._current_cursor_pos) self._brl.writeDots(1 << self._current_cursor_pos)
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut('BRAILLE.setCursor ' + str(e), debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('BRAILLE.setCursor ' + str(e), debug.debugLevel.ERROR)
@ -122,8 +133,7 @@ class driver(brailleDriver):
self.env['runtime']['debug'].writeDebugOut('BRAILLE.flush ' + str(e), debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('BRAILLE.flush ' + str(e), debug.debugLevel.ERROR)
def handleKeyInput(self, key): def handleKeyInput(self, key):
"""Handle input from any BRLTTY-compatible braille display. """Handle input from any BRLTTY-compatible braille display."""
Returns the key event for processing by Fenrir's input system."""
if not self._isInitialized: if not self._isInitialized:
return False return False
try: try:
@ -131,8 +141,6 @@ class driver(brailleDriver):
if not key_code: if not key_code:
return False return False
# Let Fenrir's input system handle the key
# This allows proper handling regardless of display type
if self.env['runtime']['debug'].debugLevel >= debug.debugLevel.INFO: if self.env['runtime']['debug'].debugLevel >= debug.debugLevel.INFO:
self.env['runtime']['debug'].writeDebugOut( self.env['runtime']['debug'].writeDebugOut(
'BRAILLE.keyPressed: ' + str(key_code), 'BRAILLE.keyPressed: ' + str(key_code),