| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
from fenrirscreenreader.core import debug |
| 8 |
import threading |
| 9 |
import time |
| 10 |
|
| 11 |
|
| 12 |
class ReadAllManager: |
| 13 |
"""Manages continuous read-all functionality with speech completion callbacks.""" |
| 14 |
|
| 15 |
def __init__(self): |
| 16 |
self.active = False |
| 17 |
self.mode = None # 'line' or 'page' |
| 18 |
self.stop_requested = False |
| 19 |
self.env = None |
| 20 |
self.items_read = 0 |
| 21 |
self._lock = threading.Lock() |
| 22 |
|
| 23 |
def initialize(self, environment): |
| 24 |
self.env = environment |
| 25 |
|
| 26 |
def shutdown(self): |
| 27 |
self.stop_read_all() |
| 28 |
|
| 29 |
def start_read_all(self, mode): |
| 30 |
"""Start continuous reading in line or page mode.""" |
| 31 |
with self._lock: |
| 32 |
if self.active: |
| 33 |
self.stop_read_all() |
| 34 |
|
| 35 |
self.active = True |
| 36 |
self.mode = mode |
| 37 |
self.stop_requested = False |
| 38 |
self.items_read = 0 |
| 39 |
|
| 40 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 41 |
f"ReadAllManager: Starting read-all mode '{mode}'", |
| 42 |
debug.DebugLevel.INFO |
| 43 |
) |
| 44 |
|
| 45 |
# Send first navigation key |
| 46 |
self._send_navigation_key() |
| 47 |
|
| 48 |
def stop_read_all(self): |
| 49 |
"""Stop continuous reading.""" |
| 50 |
with self._lock: |
| 51 |
if not self.active: |
| 52 |
return |
| 53 |
|
| 54 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 55 |
f"ReadAllManager: Stopping read-all after {self.items_read} {self.mode}s", |
| 56 |
debug.DebugLevel.INFO |
| 57 |
) |
| 58 |
|
| 59 |
self.active = False |
| 60 |
self.stop_requested = True |
| 61 |
self.mode = None |
| 62 |
|
| 63 |
# Cancel any pending speech |
| 64 |
try: |
| 65 |
self.env['runtime']['SpeechDriver'].cancel() |
| 66 |
except Exception as e: |
| 67 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 68 |
f"ReadAllManager: Error canceling speech: {e}", |
| 69 |
debug.DebugLevel.ERROR |
| 70 |
) |
| 71 |
|
| 72 |
def is_active(self): |
| 73 |
"""Check if read-all is currently active.""" |
| 74 |
return self.active |
| 75 |
|
| 76 |
def get_mode(self): |
| 77 |
"""Get current read-all mode ('line' or 'page').""" |
| 78 |
return self.mode |
| 79 |
|
| 80 |
def _send_navigation_key(self): |
| 81 |
"""Send appropriate navigation key based on mode.""" |
| 82 |
if not self.active or self.stop_requested: |
| 83 |
return |
| 84 |
|
| 85 |
try: |
| 86 |
if self.mode == 'line': |
| 87 |
key_sequence = [[1, 'KEY_DOWN'], [0, 'KEY_DOWN']] |
| 88 |
key_name = 'KEY_DOWN' |
| 89 |
elif self.mode == 'page': |
| 90 |
key_sequence = [[1, 'KEY_PAGEDOWN'], [0, 'KEY_PAGEDOWN']] |
| 91 |
key_name = 'KEY_PAGEDOWN' |
| 92 |
else: |
| 93 |
return |
| 94 |
|
| 95 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 96 |
f"ReadAllManager: Sending {key_name} ({self.items_read + 1})", |
| 97 |
debug.DebugLevel.INFO |
| 98 |
) |
| 99 |
|
| 100 |
self.env['runtime']['InputManager'].send_keys(key_sequence) |
| 101 |
self.items_read += 1 |
| 102 |
|
| 103 |
except Exception as e: |
| 104 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 105 |
f"ReadAllManager: Error sending key: {e}", |
| 106 |
debug.DebugLevel.ERROR |
| 107 |
) |
| 108 |
self.stop_read_all() |
| 109 |
|
| 110 |
def speech_completed(self): |
| 111 |
"""Called when speech completes - continue reading if active.""" |
| 112 |
if not self.active or self.stop_requested: |
| 113 |
return |
| 114 |
|
| 115 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 116 |
f"ReadAllManager: Speech completed, continuing read-all", |
| 117 |
debug.DebugLevel.INFO |
| 118 |
) |
| 119 |
|
| 120 |
# Small delay to prevent overwhelming the system |
| 121 |
def continue_reading(): |
| 122 |
time.sleep(0.1) |
| 123 |
if self.active and not self.stop_requested: |
| 124 |
self._send_navigation_key() |
| 125 |
|
| 126 |
# Use daemon thread to avoid blocking |
| 127 |
thread = threading.Thread(target=continue_reading, daemon=True) |
| 128 |
thread.start() |