diff --git a/core/addon_communication/ipc_client.py b/core/addon_communication/ipc_client.py index 1860f5d..7f490b6 100644 --- a/core/addon_communication/ipc_client.py +++ b/core/addon_communication/ipc_client.py @@ -1,4 +1,4 @@ -from talon import Module, Context, actions +from talon import Module, Context, actions, settings import os, ipaddress, json, socket from .ipc_helpers import Mutex import threading @@ -55,7 +55,6 @@ def send_ipc_command(command: str): if command not in valid_commands: raise ValueError(f"Invalid NVDA IPC command: '{command}'") - with lock: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(0.1) @@ -68,7 +67,8 @@ def send_ipc_command(command: str): # We don't want to execute commands until # we know the screen reader has the proper settings response = sock.recv(1024) - print('Received', repr(response)) + if settings.get("user.addon_debug"): + print('Received', repr(response)) except socket.timeout: print("NVDA Connection timed out") except: diff --git a/core/settings.py b/core/settings.py index cdec82c..1d6d9d6 100644 --- a/core/settings.py +++ b/core/settings.py @@ -111,6 +111,10 @@ default=True ) +mod.setting("addon_debug", + type=bool, + default=False +) # mod.mode("strict_dictation", desc="Dictation mode with only a subset of dictation commands") # mod.mode('strict_command', desc='Command mode with only a subset of command commands') diff --git a/nvda/.addOn/nvda-addon.py b/nvda/.addOn/nvda-addon.py deleted file mode 100644 index 0bae3cf..0000000 --- a/nvda/.addOn/nvda-addon.py +++ /dev/null @@ -1,80 +0,0 @@ -import speech -from scriptHandler import script -import config -import tones, nvwave, ui -import os, json, socket, threading - -commands = ["disableSpeechInterruptForCharacters", "restoreSpeechInterruptForCharacters", "debug", "playWAV"] - -def bind_to_available_port(server_socket, start_port, end_port): - for port in range(start_port, end_port): - try: - server_socket.bind(('127.0.0.1', port)) - return port - except OSError: - continue - raise OSError(f"No available ports in the range {start_port}-{end_port}") - -def handle_command(command: str): - debug_message = "" - if command == "disableSpeechInterruptForCharacters": - interrupt = config.conf["keyboard"]["speechInterruptForCharacters"] - config.conf["keyboard"]["speechInterruptForCharacters"] = False - debug_message = f"Speech interrupt changed from {interrupt} to False" - elif command == "restoreSpeechInterruptForCharacters": - config.conf["keyboard"]["speechInterruptForCharacters"] = True - debug_message = f"Speech interrupt restored to True" - elif command == "debug": - tones.beep(640, 100) - debug_message = "Debug connection successful" - else: - debug_message = f"Invalid command: '{command}', type='{type(command)}'" - print(debug_message) - return debug_message - -class IPC_Server(): - port = None - - def handle_client(self, client_socket: socket.socket): - data = client_socket.recv(1024) - message = data.decode().strip() - result = handle_command(message) - response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nMessage received. Success: {result}".encode() # Create a HTTP response - client_socket.send(response) - - def output_spec_file(self): - # write a json file to let clients know how to connect and what commands are available - PATH = os.path.expanduser("~\\AppData\\Roaming\\nvda\\talon_server_spec.json") - spec = { - "address": "127.0.0.1", - "port": str(self.get_port()), - "valid_commands": commands - } - with open(PATH, "w") as f: - json.dump(spec, f) - - def set_port(self, port): - self.port = port - - def get_port(self): - return self.port - - def create_server(self): - server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - port = bind_to_available_port(server_socket, 8888, 9000) - self.set_port(port) - self.output_spec_file() - - server_socket.listen(1) - server_socket.settimeout(None) # Set the timeout to None - print(f'Serving on {server_socket.getsockname()}') - - while True: - client_socket, addr = server_socket.accept() - print(f"Connection from {addr}") - self.handle_client(client_socket) - client_socket.close() - -server = IPC_Server() -server_thread = threading.Thread(target=server.create_server) -server_thread.start() \ No newline at end of file