| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
import inspect |
| 8 |
import os |
| 9 |
import time |
| 10 |
|
| 11 |
from fenrirscreenreader.core import debug |
| 12 |
from fenrirscreenreader.core.i18n import _ |
| 13 |
from fenrirscreenreader.utils import module_utils |
| 14 |
|
| 15 |
currentdir = os.path.dirname( |
| 16 |
os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 17 |
) |
| 18 |
fenrir_path = os.path.dirname(currentdir) |
| 19 |
|
| 20 |
|
| 21 |
class VmenuManager: |
| 22 |
def __init__(self): |
| 23 |
self.menuDict = {} |
| 24 |
self.curr_index = None |
| 25 |
self.currMenu = "" |
| 26 |
self.active = False |
| 27 |
self.reset = True |
| 28 |
self.useTimeout = True |
| 29 |
self.searchText = "" |
| 30 |
self.lastSearchTime = time.time() |
| 31 |
|
| 32 |
def initialize(self, environment): |
| 33 |
self.env = environment |
| 34 |
# use default path |
| 35 |
self.defaultVMenuPath = ( |
| 36 |
fenrir_path + "/commands/vmenu-profiles/KEY" |
| 37 |
) |
| 38 |
# if there is no user configuration |
| 39 |
if ( |
| 40 |
self.env["runtime"]["SettingsManager"].get_setting( |
| 41 |
"menu", "vmenu_path" |
| 42 |
) |
| 43 |
!= "" |
| 44 |
): |
| 45 |
self.defaultVMenuPath = self.env["runtime"][ |
| 46 |
"SettingsManager" |
| 47 |
].get_setting("menu", "vmenu_path") |
| 48 |
if not self.defaultVMenuPath.endswith("/"): |
| 49 |
self.defaultVMenuPath += "/" |
| 50 |
self.defaultVMenuPath += "KEY" |
| 51 |
|
| 52 |
self.create_menu_tree() |
| 53 |
self.closeAfterAction = False |
| 54 |
|
| 55 |
def shutdown(self): |
| 56 |
pass |
| 57 |
|
| 58 |
def clear_search_text(self): |
| 59 |
self.searchText = "" |
| 60 |
|
| 61 |
def search_entry(self, value, forceReset=False): |
| 62 |
if self.curr_index is None: |
| 63 |
return "" |
| 64 |
if self.reset or forceReset: |
| 65 |
self.clear_search_text() |
| 66 |
else: |
| 67 |
if self.useTimeout: |
| 68 |
if time.time() - self.lastSearchTime > 1: |
| 69 |
self.clear_search_text() |
| 70 |
self.searchText += value.upper() |
| 71 |
self.lastSearchTime = time.time() |
| 72 |
start_index = self.get_curr_index() |
| 73 |
while True: |
| 74 |
if not self.next_index(): |
| 75 |
return "" |
| 76 |
entry = self.get_current_entry() |
| 77 |
if entry.upper().startswith(self.searchText): |
| 78 |
return entry |
| 79 |
if start_index == self.get_curr_index(): |
| 80 |
return "" |
| 81 |
|
| 82 |
def set_curr_menu(self, currMenu=""): |
| 83 |
self.curr_index = None |
| 84 |
self.currMenu = "" |
| 85 |
if currMenu != "": |
| 86 |
currMenu += " " + _("Menu") |
| 87 |
try: |
| 88 |
t = self.menuDict[currMenu] |
| 89 |
l = list(self.menuDict.keys()) |
| 90 |
self.curr_index = [l.index(currMenu)] |
| 91 |
except Exception as e: |
| 92 |
print(e) |
| 93 |
self.currMenu = "" |
| 94 |
self.curr_index = None |
| 95 |
return |
| 96 |
if self.inc_level(): |
| 97 |
self.currMenu = currMenu |
| 98 |
else: |
| 99 |
self.currMenu = "" |
| 100 |
self.curr_index = None |
| 101 |
|
| 102 |
def get_curr_menu(self): |
| 103 |
return self.currMenu |
| 104 |
|
| 105 |
def get_active(self): |
| 106 |
return self.active |
| 107 |
|
| 108 |
def toggle_vmenu_mode(self, closeAfterAction=True): |
| 109 |
self.set_active(not self.get_active(), closeAfterAction) |
| 110 |
|
| 111 |
def set_active(self, active, closeAfterAction=True): |
| 112 |
if self.env["runtime"]["HelpManager"].is_tutorial_mode(): |
| 113 |
return |
| 114 |
self.active = active |
| 115 |
if self.active: |
| 116 |
self.closeAfterAction = closeAfterAction |
| 117 |
try: |
| 118 |
self.create_menu_tree() |
| 119 |
except Exception as e: |
| 120 |
print(e) |
| 121 |
try: |
| 122 |
if self.currMenu != "": |
| 123 |
self.set_curr_menu(self.currMenu) |
| 124 |
if self.curr_index is None: |
| 125 |
if len(self.menuDict) > 0: |
| 126 |
self.curr_index = [0] |
| 127 |
except Exception as e: |
| 128 |
print(e) |
| 129 |
try: |
| 130 |
# navigation |
| 131 |
self.env["bindings"][ |
| 132 |
str([1, ["KEY_ESC"]]) |
| 133 |
] = "TOGGLE_VMENU_MODE" |
| 134 |
self.env["bindings"][str([1, ["KEY_UP"]])] = "PREV_VMENU_ENTRY" |
| 135 |
self.env["bindings"][ |
| 136 |
str([1, ["KEY_DOWN"]]) |
| 137 |
] = "NEXT_VMENU_ENTRY" |
| 138 |
self.env["bindings"][ |
| 139 |
str([1, ["KEY_SPACE"]]) |
| 140 |
] = "CURR_VMENU_ENTRY" |
| 141 |
self.env["bindings"][ |
| 142 |
str([1, ["KEY_LEFT"]]) |
| 143 |
] = "DEC_LEVEL_VMENU" |
| 144 |
self.env["bindings"][ |
| 145 |
str([1, ["KEY_RIGHT"]]) |
| 146 |
] = "INC_LEVEL_VMENU" |
| 147 |
self.env["bindings"][ |
| 148 |
str([1, ["KEY_ENTER"]]) |
| 149 |
] = "EXEC_VMENU_ENTRY" |
| 150 |
# search |
| 151 |
self.env["bindings"][str([1, ["KEY_A"]])] = "SEARCH_A" |
| 152 |
self.env["bindings"][str([1, ["KEY_B"]])] = "SEARCH_B" |
| 153 |
self.env["bindings"][str([1, ["KEY_C"]])] = "SEARCH_C" |
| 154 |
self.env["bindings"][str([1, ["KEY_D"]])] = "SEARCH_D" |
| 155 |
self.env["bindings"][str([1, ["KEY_E"]])] = "SEARCH_E" |
| 156 |
self.env["bindings"][str([1, ["KEY_F"]])] = "SEARCH_F" |
| 157 |
self.env["bindings"][str([1, ["KEY_G"]])] = "SEARCH_G" |
| 158 |
self.env["bindings"][str([1, ["KEY_H"]])] = "SEARCH_H" |
| 159 |
self.env["bindings"][str([1, ["KEY_I"]])] = "SEARCH_I" |
| 160 |
self.env["bindings"][str([1, ["KEY_J"]])] = "SEARCH_J" |
| 161 |
self.env["bindings"][str([1, ["KEY_K"]])] = "SEARCH_K" |
| 162 |
self.env["bindings"][str([1, ["KEY_L"]])] = "SEARCH_L" |
| 163 |
self.env["bindings"][str([1, ["KEY_M"]])] = "SEARCH_M" |
| 164 |
self.env["bindings"][str([1, ["KEY_N"]])] = "SEARCH_N" |
| 165 |
self.env["bindings"][str([1, ["KEY_O"]])] = "SEARCH_O" |
| 166 |
self.env["bindings"][str([1, ["KEY_P"]])] = "SEARCH_P" |
| 167 |
self.env["bindings"][str([1, ["KEY_Q"]])] = "SEARCH_Q" |
| 168 |
self.env["bindings"][str([1, ["KEY_R"]])] = "SEARCH_R" |
| 169 |
self.env["bindings"][str([1, ["KEY_S"]])] = "SEARCH_S" |
| 170 |
self.env["bindings"][str([1, ["KEY_T"]])] = "SEARCH_T" |
| 171 |
self.env["bindings"][str([1, ["KEY_U"]])] = "SEARCH_U" |
| 172 |
self.env["bindings"][str([1, ["KEY_V"]])] = "SEARCH_V" |
| 173 |
self.env["bindings"][str([1, ["KEY_W"]])] = "SEARCH_W" |
| 174 |
self.env["bindings"][str([1, ["KEY_X"]])] = "SEARCH_X" |
| 175 |
self.env["bindings"][str([1, ["KEY_Y"]])] = "SEARCH_Y" |
| 176 |
self.env["bindings"][str([1, ["KEY_Z"]])] = "SEARCH_Z" |
| 177 |
# page navigation |
| 178 |
self.env["bindings"][ |
| 179 |
str([1, ["KEY_PAGEUP"]]) |
| 180 |
] = "PAGE_UP_VMENU" |
| 181 |
self.env["bindings"][ |
| 182 |
str([1, ["KEY_PAGEDOWN"]]) |
| 183 |
] = "PAGE_DOWN_VMENU" |
| 184 |
except Exception as e: |
| 185 |
print(e) |
| 186 |
else: |
| 187 |
try: |
| 188 |
self.curr_index = None |
| 189 |
self.env["bindings"] = self.env["runtime"][ |
| 190 |
"SettingsManager" |
| 191 |
].get_binding_backup() |
| 192 |
except Exception as e: |
| 193 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 194 |
"VmenuManager set_active: Error loading binding backup: " |
| 195 |
+ str(e), |
| 196 |
debug.DebugLevel.ERROR, |
| 197 |
) |
| 198 |
|
| 199 |
def create_menu_tree(self, resetIndex=True): |
| 200 |
if resetIndex: |
| 201 |
self.curr_index = None |
| 202 |
menu = self.fs_tree_to_dict(self.defaultVMenuPath) |
| 203 |
if menu: |
| 204 |
self.menuDict = menu |
| 205 |
|
| 206 |
# Add dynamic voice menus |
| 207 |
try: |
| 208 |
from fenrirscreenreader.core.dynamicVoiceMenu import ( |
| 209 |
add_dynamic_voice_menus, |
| 210 |
) |
| 211 |
|
| 212 |
add_dynamic_voice_menus(self) |
| 213 |
except Exception as e: |
| 214 |
print(f"Error adding dynamic voice menus: {e}") |
| 215 |
|
| 216 |
# Add dynamic keyboard layout menus |
| 217 |
try: |
| 218 |
from fenrirscreenreader.core.dynamicKeyboardLayoutMenu import ( |
| 219 |
add_dynamic_keyboard_layout_menus, |
| 220 |
) |
| 221 |
|
| 222 |
add_dynamic_keyboard_layout_menus(self) |
| 223 |
except Exception as e: |
| 224 |
print(f"Error adding dynamic keyboard layout menus: {e}") |
| 225 |
|
| 226 |
# index still valid? |
| 227 |
if self.curr_index is not None: |
| 228 |
try: |
| 229 |
r = self.get_value_by_path(self.menuDict, self.curr_index) |
| 230 |
if r == {}: |
| 231 |
self.curr_index = None |
| 232 |
except Exception as e: |
| 233 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 234 |
"VmenuManager create_menu_tree: Error checking menu index validity: " |
| 235 |
+ str(e), |
| 236 |
debug.DebugLevel.ERROR, |
| 237 |
) |
| 238 |
self.curr_index = None |
| 239 |
|
| 240 |
def execute_menu(self): |
| 241 |
if self.curr_index is None: |
| 242 |
return |
| 243 |
try: |
| 244 |
command = self.get_value_by_path(self.menuDict, self.curr_index) |
| 245 |
if command is not None: |
| 246 |
command.run() |
| 247 |
if self.closeAfterAction: |
| 248 |
self.set_active(False) |
| 249 |
except Exception as e: |
| 250 |
try: |
| 251 |
self.inc_level() |
| 252 |
text = self.get_current_entry() |
| 253 |
self.env["runtime"]["OutputManager"].present_text( |
| 254 |
text, interrupt=True |
| 255 |
) |
| 256 |
except Exception as ex: |
| 257 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 258 |
"VmenuManager execute_menu: Error presenting menu text: " |
| 259 |
+ str(ex), |
| 260 |
debug.DebugLevel.ERROR, |
| 261 |
) |
| 262 |
|
| 263 |
def inc_level(self): |
| 264 |
if self.curr_index is None: |
| 265 |
return False |
| 266 |
try: |
| 267 |
r = self.get_value_by_path(self.menuDict, self.curr_index + [0]) |
| 268 |
if r == {}: |
| 269 |
return False |
| 270 |
except Exception as e: |
| 271 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 272 |
"VmenuManager inc_level: Error accessing menu path: " + str(e), |
| 273 |
debug.DebugLevel.ERROR, |
| 274 |
) |
| 275 |
return False |
| 276 |
self.curr_index.append(0) |
| 277 |
return True |
| 278 |
|
| 279 |
def dec_level(self): |
| 280 |
if self.curr_index is None: |
| 281 |
return False |
| 282 |
if self.currMenu != "": |
| 283 |
if len(self.curr_index) <= 2: |
| 284 |
return False |
| 285 |
elif len(self.curr_index) == 1: |
| 286 |
return False |
| 287 |
self.curr_index = self.curr_index[: len(self.curr_index) - 1] |
| 288 |
return True |
| 289 |
|
| 290 |
def next_index(self): |
| 291 |
if self.curr_index is None: |
| 292 |
return False |
| 293 |
if self.curr_index[len(self.curr_index) - 1] + 1 >= len( |
| 294 |
self.get_nested_by_path(self.menuDict, self.curr_index[:-1]) |
| 295 |
): |
| 296 |
self.curr_index[len(self.curr_index) - 1] = 0 |
| 297 |
else: |
| 298 |
self.curr_index[len(self.curr_index) - 1] += 1 |
| 299 |
return True |
| 300 |
|
| 301 |
def get_curr_index(self): |
| 302 |
if self.curr_index is None: |
| 303 |
return 0 |
| 304 |
return self.curr_index[len(self.curr_index) - 1] |
| 305 |
|
| 306 |
def prev_index(self): |
| 307 |
if self.curr_index is None: |
| 308 |
return False |
| 309 |
if self.curr_index[len(self.curr_index) - 1] == 0: |
| 310 |
self.curr_index[len(self.curr_index) - 1] = ( |
| 311 |
len( |
| 312 |
self.get_nested_by_path( |
| 313 |
self.menuDict, self.curr_index[:-1] |
| 314 |
) |
| 315 |
) |
| 316 |
- 1 |
| 317 |
) |
| 318 |
else: |
| 319 |
self.curr_index[len(self.curr_index) - 1] -= 1 |
| 320 |
return True |
| 321 |
|
| 322 |
def page_up(self): |
| 323 |
if self.curr_index is None: |
| 324 |
return False |
| 325 |
menu_size = len( |
| 326 |
self.get_nested_by_path(self.menuDict, self.curr_index[:-1]) |
| 327 |
) |
| 328 |
if menu_size <= 1: |
| 329 |
return False |
| 330 |
jump_size = max(1, int(menu_size * 0.1)) # 10% of menu size, minimum 1 |
| 331 |
new_index = self.curr_index[len(self.curr_index) - 1] - jump_size |
| 332 |
if new_index < 0: |
| 333 |
new_index = 0 |
| 334 |
self.curr_index[len(self.curr_index) - 1] = new_index |
| 335 |
return True |
| 336 |
|
| 337 |
def page_down(self): |
| 338 |
if self.curr_index is None: |
| 339 |
return False |
| 340 |
menu_size = len( |
| 341 |
self.get_nested_by_path(self.menuDict, self.curr_index[:-1]) |
| 342 |
) |
| 343 |
if menu_size <= 1: |
| 344 |
return False |
| 345 |
jump_size = max(1, int(menu_size * 0.1)) # 10% of menu size, minimum 1 |
| 346 |
new_index = self.curr_index[len(self.curr_index) - 1] + jump_size |
| 347 |
if new_index >= menu_size: |
| 348 |
new_index = menu_size - 1 |
| 349 |
self.curr_index[len(self.curr_index) - 1] = new_index |
| 350 |
return True |
| 351 |
|
| 352 |
def get_current_entry(self): |
| 353 |
return self.get_keys_by_path(self.menuDict, self.curr_index)[ |
| 354 |
self.curr_index[-1] |
| 355 |
] |
| 356 |
|
| 357 |
def fs_tree_to_dict(self, path_): |
| 358 |
for root, dirs, files in os.walk(path_): |
| 359 |
tree = { |
| 360 |
d |
| 361 |
+ " " |
| 362 |
+ _("Menu"): self.fs_tree_to_dict(os.path.join(root, d)) |
| 363 |
for d in dirs |
| 364 |
if not d.startswith("__") |
| 365 |
} |
| 366 |
for f in files: |
| 367 |
try: |
| 368 |
file_name, file_extension = os.path.splitext(f) |
| 369 |
file_name = file_name.split("/")[-1] |
| 370 |
if file_name.startswith("__"): |
| 371 |
continue |
| 372 |
# Skip base classes that shouldn't be loaded as commands |
| 373 |
if file_name.endswith("_base"): |
| 374 |
continue |
| 375 |
command = self.env["runtime"]["CommandManager"].load_file( |
| 376 |
root + "/" + f |
| 377 |
) |
| 378 |
tree.update({file_name + " " + _("Action"): command}) |
| 379 |
except Exception as e: |
| 380 |
print(e) |
| 381 |
return tree # note we discontinue iteration trough os.walk |
| 382 |
|
| 383 |
def get_nested_by_path(self, complete, path): |
| 384 |
path = path.copy() |
| 385 |
if path != []: |
| 386 |
index = list(complete.keys())[path[0]] |
| 387 |
nested = self.get_nested_by_path(complete[index], path[1:]) |
| 388 |
return nested |
| 389 |
else: |
| 390 |
return complete |
| 391 |
|
| 392 |
def get_keys_by_path(self, complete, path): |
| 393 |
if not isinstance(complete, dict): |
| 394 |
return [] |
| 395 |
d = complete |
| 396 |
for i in path[:-1]: |
| 397 |
d = d[list(d.keys())[i]] |
| 398 |
return list(d.keys()) |
| 399 |
|
| 400 |
def get_value_by_path(self, complete, path): |
| 401 |
if not isinstance(complete, dict): |
| 402 |
return complete |
| 403 |
d = complete.copy() |
| 404 |
for i in path: |
| 405 |
d = d[list(d.keys())[i]] |
| 406 |
return d |