| 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 |
import traceback |
| 11 |
|
| 12 |
from fenrirscreenreader.core import debug |
| 13 |
from fenrirscreenreader.core import inputData |
| 14 |
from fenrirscreenreader.core.i18n import _ |
| 15 |
|
| 16 |
currentdir = os.path.dirname( |
| 17 |
os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 18 |
) |
| 19 |
fenrir_path = os.path.dirname(currentdir) |
| 20 |
|
| 21 |
SHORTCUT_MODIFIER_KEYS = { |
| 22 |
"KEY_ALT", |
| 23 |
"KEY_CTRL", |
| 24 |
"KEY_META", |
| 25 |
"KEY_SHIFT", |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
class InputManager: |
| 30 |
def __init__(self): |
| 31 |
self.executeDeviceGrab = False |
| 32 |
self.lastDetectedDevices = None |
| 33 |
|
| 34 |
def initialize(self, environment): |
| 35 |
self.env = environment |
| 36 |
self.env["runtime"]["SettingsManager"].load_driver( |
| 37 |
self.env["runtime"]["SettingsManager"].get_setting( |
| 38 |
"keyboard", "driver" |
| 39 |
), |
| 40 |
"InputDriver", |
| 41 |
) |
| 42 |
self.update_input_devices() |
| 43 |
|
| 44 |
# init LEDs with current state |
| 45 |
self.env["input"]["new_num_lock"] = self.env["runtime"][ |
| 46 |
"InputDriver" |
| 47 |
].get_led_state() |
| 48 |
self.env["input"]["old_num_lock"] = self.env["input"]["new_num_lock"] |
| 49 |
self.env["input"]["new_caps_lock"] = self.env["runtime"][ |
| 50 |
"InputDriver" |
| 51 |
].get_led_state(1) |
| 52 |
self.env["input"]["old_caps_lock"] = self.env["input"]["new_caps_lock"] |
| 53 |
self.env["input"]["new_scroll_lock"] = self.env["runtime"][ |
| 54 |
"InputDriver" |
| 55 |
].get_led_state(2) |
| 56 |
self.env["input"]["old_scroll_lock"] = self.env["input"]["new_scroll_lock"] |
| 57 |
self.lastDeepestInput = [] |
| 58 |
self.lastEvent = None |
| 59 |
self.env["input"]["shortcut_repeat"] = 1 |
| 60 |
self.lastInputTime = time.time() |
| 61 |
|
| 62 |
def shutdown(self): |
| 63 |
self.remove_all_devices() |
| 64 |
self.env["runtime"]["SettingsManager"].shutdown_driver("InputDriver") |
| 65 |
|
| 66 |
def get_input_event(self): |
| 67 |
event = None |
| 68 |
try: |
| 69 |
event = self.env["runtime"]["InputDriver"].get_input_event() |
| 70 |
except Exception as e: |
| 71 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 72 |
"InputManager get_input_event: Error getting input event: " |
| 73 |
+ str(e), |
| 74 |
debug.DebugLevel.ERROR, |
| 75 |
) |
| 76 |
return event |
| 77 |
|
| 78 |
def set_execute_device_grab(self, newExecuteDeviceGrab=True): |
| 79 |
self.executeDeviceGrab = newExecuteDeviceGrab |
| 80 |
|
| 81 |
def handle_device_grab(self, force=False): |
| 82 |
if force: |
| 83 |
self.set_execute_device_grab() |
| 84 |
if not self.executeDeviceGrab: |
| 85 |
return |
| 86 |
if self.env["input"]["event_buffer"] != []: |
| 87 |
return |
| 88 |
if not self.no_key_pressed(): |
| 89 |
return |
| 90 |
if not self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 91 |
"keyboard", "grab_devices" |
| 92 |
): |
| 93 |
self.executeDeviceGrab = False |
| 94 |
return |
| 95 |
|
| 96 |
# Add maximum retries to prevent infinite loops |
| 97 |
max_retries = 5 |
| 98 |
retry_count = 0 |
| 99 |
grab_timeout = 3 # Timeout in seconds |
| 100 |
start_time = time.time() |
| 101 |
|
| 102 |
if self.env["runtime"]["ScreenManager"].get_curr_screen_ignored(): |
| 103 |
while not self.ungrab_all_devices(): |
| 104 |
retry_count += 1 |
| 105 |
if ( |
| 106 |
retry_count >= max_retries |
| 107 |
or (time.time() - start_time) > grab_timeout |
| 108 |
): |
| 109 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 110 |
"Failed to ungrab devices after multiple attempts", |
| 111 |
debug.DebugLevel.ERROR, |
| 112 |
) |
| 113 |
# Force a release of devices if possible through |
| 114 |
# alternative means |
| 115 |
try: |
| 116 |
self.env["runtime"]["InputDriver"].force_ungrab() |
| 117 |
except Exception as e: |
| 118 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 119 |
"InputManager handle_device_grab: Error forcing ungrab: " |
| 120 |
+ str(e), |
| 121 |
debug.DebugLevel.ERROR, |
| 122 |
) |
| 123 |
break |
| 124 |
time.sleep(0.25) |
| 125 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 126 |
f"retry ungrab_all_devices {retry_count}/{max_retries}", |
| 127 |
debug.DebugLevel.WARNING, |
| 128 |
) |
| 129 |
else: |
| 130 |
while not self.grab_all_devices(): |
| 131 |
retry_count += 1 |
| 132 |
if ( |
| 133 |
retry_count >= max_retries |
| 134 |
or (time.time() - start_time) > grab_timeout |
| 135 |
): |
| 136 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 137 |
"Failed to grab devices after multiple attempts", |
| 138 |
debug.DebugLevel.ERROR, |
| 139 |
) |
| 140 |
# Continue without grabbing input - limited functionality |
| 141 |
# but not locked |
| 142 |
break |
| 143 |
time.sleep(0.25) |
| 144 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 145 |
f"retry grab_all_devices {retry_count}/{max_retries}", |
| 146 |
debug.DebugLevel.WARNING, |
| 147 |
) |
| 148 |
|
| 149 |
self.executeDeviceGrab = False |
| 150 |
|
| 151 |
def send_keys(self, keyMacro): |
| 152 |
for e in keyMacro: |
| 153 |
key = "" |
| 154 |
value = 0 |
| 155 |
if len(e) != 2: |
| 156 |
continue |
| 157 |
if isinstance(e[0], int) and isinstance(e[1], str): |
| 158 |
key = e[1].upper() |
| 159 |
value = e[0] |
| 160 |
elif isinstance(e[1], int) and isinstance(e[0], str): |
| 161 |
key = e[0].upper() |
| 162 |
value = e[1] |
| 163 |
else: |
| 164 |
continue |
| 165 |
if key.upper() == "SLEEP": |
| 166 |
time.sleep(value) |
| 167 |
else: |
| 168 |
self.env["runtime"]["InputDriver"].send_key(key, value) |
| 169 |
|
| 170 |
def get_last_event(self): |
| 171 |
return self.lastEvent |
| 172 |
|
| 173 |
def record_unmanaged_keypress(self, event_name): |
| 174 |
self.lastEvent = { |
| 175 |
"event_name": event_name, |
| 176 |
"event_state": 1, |
| 177 |
} |
| 178 |
self.set_last_deepest_input([event_name]) |
| 179 |
self.lastInputTime = time.time() |
| 180 |
|
| 181 |
def handle_input_event(self, event_data): |
| 182 |
if not event_data: |
| 183 |
return |
| 184 |
self.lastEvent = event_data |
| 185 |
# a hang apears.. try to fix |
| 186 |
if self.env["input"]["event_buffer"] == []: |
| 187 |
if self.env["input"]["curr_input"] != []: |
| 188 |
self.env["input"]["curr_input"] = [] |
| 189 |
self.env["input"]["shortcut_repeat"] = 1 |
| 190 |
|
| 191 |
self.env["input"]["prev_input"] = self.env["input"]["curr_input"].copy() |
| 192 |
if event_data["event_state"] == 0: |
| 193 |
if event_data["event_name"] in self.env["input"]["curr_input"]: |
| 194 |
self.env["input"]["curr_input"].remove(event_data["event_name"]) |
| 195 |
if len(self.env["input"]["curr_input"]) > 1: |
| 196 |
self.env["input"]["curr_input"] = sorted( |
| 197 |
self.env["input"]["curr_input"] |
| 198 |
) |
| 199 |
self.lastInputTime = time.time() |
| 200 |
elif event_data["event_state"] == 1: |
| 201 |
if not event_data["event_name"] in self.env["input"]["curr_input"]: |
| 202 |
self.env["input"]["curr_input"].append(event_data["event_name"]) |
| 203 |
if len(self.env["input"]["curr_input"]) > 1: |
| 204 |
self.env["input"]["curr_input"] = sorted( |
| 205 |
self.env["input"]["curr_input"] |
| 206 |
) |
| 207 |
if self.lastDeepestInput == self.env["input"]["curr_input"]: |
| 208 |
if time.time() - self.lastInputTime <= self.env["runtime"][ |
| 209 |
"SettingsManager" |
| 210 |
].get_setting_as_float("keyboard", "double_tap_timeout"): |
| 211 |
self.env["input"]["shortcut_repeat"] += 1 |
| 212 |
else: |
| 213 |
self.env["input"]["shortcut_repeat"] = 1 |
| 214 |
else: |
| 215 |
self.env["input"]["shortcut_repeat"] = 1 |
| 216 |
self.set_last_deepest_input( |
| 217 |
self.env["input"]["curr_input"].copy() |
| 218 |
) |
| 219 |
self.handle_led_states(event_data) |
| 220 |
self.lastInputTime = time.time() |
| 221 |
elif event_data["event_state"] == 2: |
| 222 |
self.lastInputTime = time.time() |
| 223 |
|
| 224 |
self.env["input"]["old_num_lock"] = self.env["input"]["new_num_lock"] |
| 225 |
self.env["input"]["new_num_lock"] = self.env["runtime"][ |
| 226 |
"InputDriver" |
| 227 |
].get_led_state() |
| 228 |
self.env["input"]["old_caps_lock"] = self.env["input"]["new_caps_lock"] |
| 229 |
self.env["input"]["new_caps_lock"] = self.env["runtime"][ |
| 230 |
"InputDriver" |
| 231 |
].get_led_state(1) |
| 232 |
self.env["input"]["old_scroll_lock"] = self.env["input"]["new_scroll_lock"] |
| 233 |
self.env["input"]["new_scroll_lock"] = self.env["runtime"][ |
| 234 |
"InputDriver" |
| 235 |
].get_led_state(2) |
| 236 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 237 |
"curr_input " + str(self.env["input"]["curr_input"]), |
| 238 |
debug.DebugLevel.INFO, |
| 239 |
) |
| 240 |
if self.no_key_pressed(): |
| 241 |
self.env["input"]["prev_input"] = [] |
| 242 |
|
| 243 |
def handle_led_states(self, m_event): |
| 244 |
if self.curr_key_is_modifier(): |
| 245 |
return |
| 246 |
try: |
| 247 |
if m_event["event_name"] == "KEY_NUMLOCK": |
| 248 |
self.env["runtime"]["InputDriver"].toggle_led_state() |
| 249 |
elif m_event["event_name"] == "KEY_CAPSLOCK": |
| 250 |
self.env["runtime"]["InputDriver"].toggle_led_state(1) |
| 251 |
elif m_event["event_name"] == "KEY_SCROLLLOCK": |
| 252 |
self.env["runtime"]["InputDriver"].toggle_led_state(2) |
| 253 |
except Exception as e: |
| 254 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 255 |
"InputManager handle_led_states: Error toggling LED state: " |
| 256 |
+ str(e), |
| 257 |
debug.DebugLevel.ERROR, |
| 258 |
) |
| 259 |
|
| 260 |
def grab_all_devices(self): |
| 261 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 262 |
"keyboard", "grab_devices" |
| 263 |
): |
| 264 |
try: |
| 265 |
return self.env["runtime"]["InputDriver"].grab_all_devices() |
| 266 |
except Exception as e: |
| 267 |
return False |
| 268 |
return True |
| 269 |
|
| 270 |
def ungrab_all_devices(self): |
| 271 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 272 |
"keyboard", "grab_devices" |
| 273 |
): |
| 274 |
try: |
| 275 |
return self.env["runtime"]["InputDriver"].ungrab_all_devices() |
| 276 |
except Exception as e: |
| 277 |
return False |
| 278 |
return True |
| 279 |
|
| 280 |
def set_help_capture(self, enabled): |
| 281 |
try: |
| 282 |
return self.env["runtime"]["InputDriver"].set_help_capture( |
| 283 |
enabled |
| 284 |
) |
| 285 |
except Exception as error: |
| 286 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 287 |
"InputManager could not change help capture: " + str(error), |
| 288 |
debug.DebugLevel.ERROR, |
| 289 |
) |
| 290 |
return not enabled |
| 291 |
|
| 292 |
def handle_plug_input_device(self, event_data): |
| 293 |
for deviceEntry in event_data: |
| 294 |
self.update_input_devices(deviceEntry["device"]) |
| 295 |
|
| 296 |
def update_input_devices(self, newDevice=None): |
| 297 |
try: |
| 298 |
self.env["runtime"]["InputDriver"].update_input_devices(newDevice) |
| 299 |
except Exception as e: |
| 300 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 301 |
"InputManager update_input_devices: Error updating input devices: " |
| 302 |
+ str(e), |
| 303 |
debug.DebugLevel.ERROR, |
| 304 |
) |
| 305 |
try: |
| 306 |
if self.env["runtime"]["ScreenManager"]: |
| 307 |
self.handle_device_grab(force=True) |
| 308 |
except Exception as e: |
| 309 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 310 |
"InputManager update_input_devices: Error handling device grab: " |
| 311 |
+ str(e), |
| 312 |
debug.DebugLevel.ERROR, |
| 313 |
) |
| 314 |
|
| 315 |
def remove_all_devices(self): |
| 316 |
try: |
| 317 |
self.env["runtime"]["InputDriver"].remove_all_devices() |
| 318 |
except Exception as e: |
| 319 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 320 |
"InputManager remove_all_devices: Error removing devices: " |
| 321 |
+ str(e), |
| 322 |
debug.DebugLevel.ERROR, |
| 323 |
) |
| 324 |
|
| 325 |
def convert_event_name(self, event_name): |
| 326 |
if not event_name: |
| 327 |
return "" |
| 328 |
if event_name == "": |
| 329 |
return "" |
| 330 |
event_name = event_name.upper() |
| 331 |
if event_name == "KEY_LEFTCTRL": |
| 332 |
event_name = "KEY_CTRL" |
| 333 |
elif event_name == "KEY_RIGHTCTRL": |
| 334 |
event_name = "KEY_CTRL" |
| 335 |
elif event_name == "KEY_LEFTSHIFT": |
| 336 |
event_name = "KEY_SHIFT" |
| 337 |
elif event_name == "KEY_RIGHTSHIFT": |
| 338 |
event_name = "KEY_SHIFT" |
| 339 |
elif event_name == "KEY_LEFTALT": |
| 340 |
event_name = "KEY_ALT" |
| 341 |
elif event_name == "KEY_RIGHTALT": |
| 342 |
event_name = "KEY_ALT" |
| 343 |
elif event_name == "KEY_LEFTMETA": |
| 344 |
event_name = "KEY_META" |
| 345 |
elif event_name == "KEY_RIGHTMETA": |
| 346 |
event_name = "KEY_META" |
| 347 |
if self.is_fenrir_key(event_name): |
| 348 |
event_name = "KEY_FENRIR" |
| 349 |
if self.is_script_key(event_name): |
| 350 |
event_name = "KEY_SCRIPT" |
| 351 |
return event_name |
| 352 |
|
| 353 |
def clear_event_buffer(self): |
| 354 |
try: |
| 355 |
self.env["runtime"]["InputDriver"].clear_event_buffer() |
| 356 |
except Exception as e: |
| 357 |
pass |
| 358 |
|
| 359 |
def reset_input_state(self): |
| 360 |
self.env["input"]["curr_input"] = [] |
| 361 |
self.env["input"]["prev_input"] = [] |
| 362 |
self.env["input"]["event_buffer"] = [] |
| 363 |
self.env["input"]["shortcut_repeat"] = 1 |
| 364 |
self.clear_last_deep_input() |
| 365 |
self.clear_event_buffer() |
| 366 |
|
| 367 |
def set_last_deepest_input(self, currentDeepestInput): |
| 368 |
self.lastDeepestInput = currentDeepestInput |
| 369 |
|
| 370 |
def clear_last_deep_input(self): |
| 371 |
self.lastDeepestInput = [] |
| 372 |
|
| 373 |
def get_last_input_time(self): |
| 374 |
return self.lastInputTime |
| 375 |
|
| 376 |
def get_last_deepest_input(self): |
| 377 |
return self.lastDeepestInput |
| 378 |
|
| 379 |
def write_event_buffer(self): |
| 380 |
try: |
| 381 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 382 |
"keyboard", "grab_devices" |
| 383 |
): |
| 384 |
self.env["runtime"]["InputDriver"].write_event_buffer() |
| 385 |
self.clear_event_buffer() |
| 386 |
except Exception as e: |
| 387 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 388 |
"Error while write_u_input", debug.DebugLevel.ERROR |
| 389 |
) |
| 390 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 391 |
str(e), debug.DebugLevel.ERROR |
| 392 |
) |
| 393 |
|
| 394 |
def no_key_pressed(self): |
| 395 |
return self.env["input"]["curr_input"] == [] |
| 396 |
|
| 397 |
def is_key_press(self): |
| 398 |
return (self.env["input"]["prev_input"] == []) and ( |
| 399 |
self.env["input"]["curr_input"] != [] |
| 400 |
) |
| 401 |
|
| 402 |
def get_prev_deepest_shortcut(self): |
| 403 |
shortcut = [] |
| 404 |
shortcut.append(self.env["input"]["shortcut_repeat"]) |
| 405 |
shortcut.append(self.get_last_deepest_input()) |
| 406 |
return str(shortcut) |
| 407 |
|
| 408 |
def get_prev_shortcut(self): |
| 409 |
shortcut = [] |
| 410 |
shortcut.append(self.env["input"]["shortcut_repeat"]) |
| 411 |
shortcut.append(self.env["input"]["prev_input"]) |
| 412 |
return str(shortcut) |
| 413 |
|
| 414 |
def get_curr_shortcut(self, inputSequence=None): |
| 415 |
shortcut = [] |
| 416 |
shortcut.append(self.env["input"]["shortcut_repeat"]) |
| 417 |
|
| 418 |
numpad_keys = [ |
| 419 |
"KEY_KP0", |
| 420 |
"KEY_KP1", |
| 421 |
"KEY_KP2", |
| 422 |
"KEY_KP3", |
| 423 |
"KEY_KP4", |
| 424 |
"KEY_KP5", |
| 425 |
"KEY_KP6", |
| 426 |
"KEY_KP7", |
| 427 |
"KEY_KP8", |
| 428 |
"KEY_KP9", |
| 429 |
"KEY_KPDOT", |
| 430 |
"KEY_KPPLUS", |
| 431 |
"KEY_KPMINUS", |
| 432 |
"KEY_KPASTERISK", |
| 433 |
"KEY_KPSLASH", |
| 434 |
"KEY_KPENTER", |
| 435 |
"KEY_KPEQUAL", |
| 436 |
] |
| 437 |
if inputSequence: |
| 438 |
# Check if any key in the sequence is a numpad key and numlock is ON |
| 439 |
# If numlock is ON and any key in the sequence is a numpad key, |
| 440 |
# return an empty shortcut |
| 441 |
if not self.env["runtime"][ |
| 442 |
"CursorManager" |
| 443 |
].should_process_numpad_commands(): |
| 444 |
for key in inputSequence: |
| 445 |
if key in numpad_keys: |
| 446 |
# Return an empty/invalid shortcut that won't match any |
| 447 |
# command |
| 448 |
return "[]" |
| 449 |
|
| 450 |
shortcut.append(inputSequence) |
| 451 |
else: |
| 452 |
# Same check for current input |
| 453 |
|
| 454 |
if not self.env["runtime"][ |
| 455 |
"CursorManager" |
| 456 |
].should_process_numpad_commands(): |
| 457 |
for key in self.env["input"]["curr_input"]: |
| 458 |
if key in numpad_keys: |
| 459 |
# Return an empty/invalid shortcut that won't match any |
| 460 |
# command |
| 461 |
return "[]" |
| 462 |
|
| 463 |
shortcut.append(self.env["input"]["curr_input"]) |
| 464 |
|
| 465 |
if len(self.env["input"]["prev_input"]) < len( |
| 466 |
self.env["input"]["curr_input"] |
| 467 |
): |
| 468 |
if self.env["input"][ |
| 469 |
"shortcut_repeat" |
| 470 |
] > 1 and not self.shortcut_exists(str(shortcut)): |
| 471 |
shortcut = [] |
| 472 |
self.env["input"]["shortcut_repeat"] = 1 |
| 473 |
shortcut.append(self.env["input"]["shortcut_repeat"]) |
| 474 |
shortcut.append(self.env["input"]["curr_input"]) |
| 475 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 476 |
"curr_shortcut " + str(shortcut), debug.DebugLevel.INFO |
| 477 |
) |
| 478 |
return str(shortcut) |
| 479 |
|
| 480 |
def curr_key_is_modifier(self): |
| 481 |
if len(self.get_last_deepest_input()) != 1: |
| 482 |
return False |
| 483 |
return (self.env["input"]["curr_input"][0] == "KEY_FENRIR") or ( |
| 484 |
self.env["input"]["curr_input"][0] == "KEY_SCRIPT" |
| 485 |
) |
| 486 |
|
| 487 |
def curr_input_has_command_modifier(self): |
| 488 |
current_input = self.env["input"]["curr_input"] |
| 489 |
return "KEY_FENRIR" in current_input or "KEY_SCRIPT" in current_input |
| 490 |
|
| 491 |
def curr_input_is_modifier_prefix(self): |
| 492 |
current_input = self.env["input"]["curr_input"] |
| 493 |
return bool(current_input) and all( |
| 494 |
key_name in SHORTCUT_MODIFIER_KEYS for key_name in current_input |
| 495 |
) |
| 496 |
|
| 497 |
def is_fenrir_key(self, event_name): |
| 498 |
return event_name in self.env["input"]["fenrir_key"] |
| 499 |
|
| 500 |
def is_script_key(self, event_name): |
| 501 |
return event_name in self.env["input"]["script_key"] |
| 502 |
|
| 503 |
def get_command_for_shortcut(self, shortcut): |
| 504 |
if not self.shortcut_exists(shortcut): |
| 505 |
return "" |
| 506 |
return self.env["bindings"][shortcut] |
| 507 |
|
| 508 |
def key_echo(self, event_data=None): |
| 509 |
if not event_data: |
| 510 |
event_data = self.get_last_event() |
| 511 |
if not event_data: |
| 512 |
return |
| 513 |
key_name = "" |
| 514 |
if event_data["event_state"] == 1: |
| 515 |
key_name = event_data["event_name"].lower() |
| 516 |
if key_name.startswith("key_"): |
| 517 |
key_name = key_name[4:] |
| 518 |
self.env["runtime"]["OutputManager"].present_text( |
| 519 |
_(key_name), interrupt=True |
| 520 |
) |
| 521 |
|
| 522 |
def shortcut_exists(self, shortcut): |
| 523 |
return shortcut in self.env["bindings"] |
| 524 |
|
| 525 |
def load_shortcuts( |
| 526 |
self, |
| 527 |
kb_config_path=fenrir_path + "/../../config/keyboard/desktop.conf", |
| 528 |
): |
| 529 |
kb_config = open(kb_config_path, "r") |
| 530 |
while True: |
| 531 |
invalid = False |
| 532 |
line = kb_config.readline() |
| 533 |
if not line: |
| 534 |
break |
| 535 |
line = line.replace("\n", "") |
| 536 |
if line.replace(" ", "") == "": |
| 537 |
continue |
| 538 |
if line.replace(" ", "").startswith("#"): |
| 539 |
continue |
| 540 |
if line.count("=") != 1: |
| 541 |
continue |
| 542 |
sep_line = line.split("=") |
| 543 |
command_name = sep_line[1].upper() |
| 544 |
sep_line[0] = sep_line[0].replace(" ", "") |
| 545 |
sep_line[0] = sep_line[0].replace("'", "") |
| 546 |
sep_line[0] = sep_line[0].replace('"', "") |
| 547 |
keys = sep_line[0].split(",") |
| 548 |
shortcut_keys = [] |
| 549 |
shortcut_repeat = 1 |
| 550 |
shortcut = [] |
| 551 |
for key in keys: |
| 552 |
try: |
| 553 |
shortcut_repeat = int(key) |
| 554 |
except Exception as e: |
| 555 |
if not self.is_valid_key(key.upper()): |
| 556 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 557 |
"invalid key : " |
| 558 |
+ key.upper() |
| 559 |
+ " command:" |
| 560 |
+ command_name, |
| 561 |
debug.DebugLevel.WARNING, |
| 562 |
) |
| 563 |
invalid = True |
| 564 |
break |
| 565 |
shortcut_keys.append(key.upper()) |
| 566 |
if invalid: |
| 567 |
continue |
| 568 |
shortcut.append(shortcut_repeat) |
| 569 |
shortcut.append(sorted(shortcut_keys)) |
| 570 |
if len(shortcut_keys) != 1 and "KEY_FENRIR" not in shortcut_keys: |
| 571 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 572 |
"invalid shortcut (missing KEY_FENRIR): " |
| 573 |
+ str(shortcut) |
| 574 |
+ " command:" |
| 575 |
+ command_name, |
| 576 |
debug.DebugLevel.ERROR, |
| 577 |
) |
| 578 |
continue |
| 579 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 580 |
"Shortcut: " + str(shortcut) + " command:" + command_name, |
| 581 |
debug.DebugLevel.INFO, |
| 582 |
on_any_level=True, |
| 583 |
) |
| 584 |
self.env["bindings"][str(shortcut)] = command_name |
| 585 |
self.env["rawBindings"][str(shortcut)] = shortcut |
| 586 |
kb_config.close() |
| 587 |
# fix bindings |
| 588 |
self.env["bindings"][ |
| 589 |
str([1, ["KEY_F1", "KEY_FENRIR"]]) |
| 590 |
] = "TOGGLE_TUTORIAL_MODE" |
| 591 |
self.env["rawBindings"][ |
| 592 |
str([1, ["KEY_F1", "KEY_FENRIR"]]) |
| 593 |
] = [1, ["KEY_F1", "KEY_FENRIR"]] |
| 594 |
|
| 595 |
def is_valid_key(self, key): |
| 596 |
return key in inputData.key_names |
| 597 |
|
| 598 |
def set_last_detected_devices(self, devices): |
| 599 |
self.lastDetectedDevices = devices |
| 600 |
|
| 601 |
def get_last_detected_devices(self): |
| 602 |
return self.lastDetectedDevices |
| 603 |
|
| 604 |
def reload_shortcuts(self): |
| 605 |
"""Reload keyboard shortcuts from current layout setting""" |
| 606 |
# Clear existing bindings |
| 607 |
self.env["bindings"].clear() |
| 608 |
self.env["rawBindings"].clear() |
| 609 |
|
| 610 |
# Get current layout path |
| 611 |
layout_setting = self.env["runtime"]["SettingsManager"].get_setting( |
| 612 |
"keyboard", "keyboard_layout" |
| 613 |
) |
| 614 |
|
| 615 |
# Resolve full path if needed |
| 616 |
if not os.path.exists(layout_setting): |
| 617 |
settings_root = "/etc/fenrirscreenreader/" |
| 618 |
if not os.path.exists(settings_root): |
| 619 |
import fenrirscreenreader |
| 620 |
|
| 621 |
fenrir_path = os.path.dirname(fenrirscreenreader.__file__) |
| 622 |
settings_root = fenrir_path + "/../../config/" |
| 623 |
|
| 624 |
layout_path = ( |
| 625 |
settings_root + "keyboard/" + layout_setting + ".conf" |
| 626 |
) |
| 627 |
if not os.path.exists(layout_path): |
| 628 |
# Fallback to default if layout not found |
| 629 |
layout_path = settings_root + "keyboard/desktop.conf" |
| 630 |
else: |
| 631 |
layout_path = layout_setting |
| 632 |
|
| 633 |
# Reload shortcuts |
| 634 |
self.load_shortcuts(layout_path) |
| 635 |
|
| 636 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 637 |
"Reloaded shortcuts from: " + layout_path, |
| 638 |
debug.DebugLevel.INFO, |
| 639 |
on_any_level=True, |
| 640 |
) |