diff --git a/src/fenrirscreenreader/core/brailleDriver.py b/src/fenrirscreenreader/core/brailleDriver.py index f66ef054..94fb07d9 100644 --- a/src/fenrirscreenreader/core/brailleDriver.py +++ b/src/fenrirscreenreader/core/brailleDriver.py @@ -1,41 +1,85 @@ -#!/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- - # Fenrir TTY screen reader -# By Chrys, Storm Dragon, and contributers. +# By Chrys, Storm Dragon, and contributors. +import brlapi from fenrirscreenreader.core import debug class brailleDriver(): def __init__(self): self._isInitialized = False self.deviceSize = None + self._brl = None + def initialize(self, environment): + """Initialize the BRLTTY connection.""" self.env = environment - self._isInitialized = True + try: + self._brl = brlapi.Connection() + self._brl.enterTtyMode() + self.deviceSize = self._brl.displaySize + self._isInitialized = True + return True + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Initializing braille failed:' + str(e),debug.debugLevel.ERROR) + return False def getDeviceSize(self): + """Get the size of the braille display.""" if not self._isInitialized: - return (0,0) - return (0,0) + return (0, 0) + return self.deviceSize if self.deviceSize else (0, 0) - def writeText(self,text): + def writeText(self, text): + """Write text to the braille display.""" if not self._isInitialized: return + try: + # Ensure text doesn't exceed display width + if self.deviceSize: + text = text[:self.deviceSize[0]] + self._brl.writeText(text) + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Writing braille failed:' + str(e),debug.debugLevel.ERROR) def connectDevice(self): - pass + """Attempt to connect to the braille device.""" + try: + if self._brl: + self._brl.enterTtyMode() + return True + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Connecting braille failed:' + str(e),debug.debugLevel.ERROR) + return False def enterScreen(self, screen): + """Enter a new screen context.""" if not self._isInitialized: return + try: + self._brl.enterTtyModeWithPath(screen) + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Entering screen failed:' + str(e),debug.debugLevel.ERROR) - def leveScreen(self): + def leaveScreen(self): + """Leave the current screen context.""" if not self._isInitialized: return + try: + self._brl.leaveTtyMode() + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Leaving screen failed:' + str(e),debug.debugLevel.ERROR) def shutdown(self): + """Shutdown the braille driver.""" if not self._isInitialized: return - self.leveScreen() - self._isInitialized = False + try: + self.leaveScreen() + if self._brl: + self._brl.closeConnection() + self._brl = None + self._isInitialized = False + except Exception as e: + self.env['runtime']['debug'].writeDebugOut('ERROR: Shutting down braille failed:' + str(e),debug.debugLevel.ERROR) diff --git a/src/fenrirscreenreader/core/debug.py b/src/fenrirscreenreader/core/debug.py index 72b5a61a..7c7d0885 100644 --- a/src/fenrirscreenreader/core/debug.py +++ b/src/fenrirscreenreader/core/debug.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 from enum import Enum diff --git a/src/fenrirscreenreader/core/debugManager.py b/src/fenrirscreenreader/core/debugManager.py index cdd12e38..ffe9ffb9 100644 --- a/src/fenrirscreenreader/core/debugManager.py +++ b/src/fenrirscreenreader/core/debugManager.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # Debugger module for the Fenrir screen reader. from fenrirscreenreader.core import debug diff --git a/src/fenrirscreenreader/core/speechDriver.py b/src/fenrirscreenreader/core/speechDriver.py index ca37f5d9..faf5e813 100644 --- a/src/fenrirscreenreader/core/speechDriver.py +++ b/src/fenrirscreenreader/core/speechDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/debugDriver.py b/src/fenrirscreenreader/speechDriver/debugDriver.py index 0f7378e8..2b1f34f6 100644 --- a/src/fenrirscreenreader/speechDriver/debugDriver.py +++ b/src/fenrirscreenreader/speechDriver/debugDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/dummyDriver.py b/src/fenrirscreenreader/speechDriver/dummyDriver.py index 32e893d4..8860d403 100644 --- a/src/fenrirscreenreader/speechDriver/dummyDriver.py +++ b/src/fenrirscreenreader/speechDriver/dummyDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/emacspeakDriver.py b/src/fenrirscreenreader/speechDriver/emacspeakDriver.py index c03be98a..a09994f6 100644 --- a/src/fenrirscreenreader/speechDriver/emacspeakDriver.py +++ b/src/fenrirscreenreader/speechDriver/emacspeakDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/genericDriver.py b/src/fenrirscreenreader/speechDriver/genericDriver.py index 7b404e46..971f6c62 100644 --- a/src/fenrirscreenreader/speechDriver/genericDriver.py +++ b/src/fenrirscreenreader/speechDriver/genericDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/pyttsxDriver.py b/src/fenrirscreenreader/speechDriver/pyttsxDriver.py index e6afb501..bcb2f379 100644 --- a/src/fenrirscreenreader/speechDriver/pyttsxDriver.py +++ b/src/fenrirscreenreader/speechDriver/pyttsxDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/speechDriver/speechdDriver.py b/src/fenrirscreenreader/speechDriver/speechdDriver.py index e0b5a583..b711d9e7 100644 --- a/src/fenrirscreenreader/speechDriver/speechdDriver.py +++ b/src/fenrirscreenreader/speechDriver/speechdDriver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader diff --git a/src/fenrirscreenreader/utils/fenrir-config.py b/src/fenrirscreenreader/utils/fenrir-config.py index d512ba67..97d1087f 100644 --- a/src/fenrirscreenreader/utils/fenrir-config.py +++ b/src/fenrirscreenreader/utils/fenrir-config.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import configparser import os