#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributors. import os import shutil import subprocess import threading _display_lock = threading.RLock() _CLIPBOARD_TIMEOUT = 2.0 _ENCODING = "utf-8" def _command_exists(command): return shutil.which(command) is not None def _env_for_display(display): env = os.environ.copy() if display: env["DISPLAY"] = display return env def _display_candidates(display="", scan_displays=False): if display: return [display] current_display = os.environ.get("DISPLAY", "") candidates = [] if current_display: candidates.append(current_display) if scan_displays: candidates.extend(f":{index}" for index in range(10)) if not candidates: candidates.append("") return list(dict.fromkeys(candidates)) def _read_commands(display): commands = [] if display: if _command_exists("xclip"): commands.append(["xclip", "-selection", "clipboard", "-o"]) if _command_exists("xsel"): commands.append(["xsel", "-b", "-o"]) elif os.environ.get("WAYLAND_DISPLAY") and _command_exists("wl-paste"): commands.append(["wl-paste", "-n", "-t", "text"]) return commands def _write_commands(display): commands = [] if display: if _command_exists("xclip"): commands.append(["xclip", "-selection", "clipboard"]) if _command_exists("xsel"): commands.append(["xsel", "-b", "-i"]) elif os.environ.get("WAYLAND_DISPLAY") and _command_exists("wl-copy"): commands.append(["wl-copy"]) return commands def _run_command(command, display, input_text=None): input_bytes = None if input_text is not None: input_bytes = input_text.encode(_ENCODING) output_target = ( subprocess.PIPE if input_text is None else subprocess.DEVNULL ) return subprocess.run( command, input=input_bytes, stdout=output_target, stderr=output_target, env=_env_for_display(display), timeout=_CLIPBOARD_TIMEOUT, check=False, ) def _command_error(command, result): stderr = (result.stderr or b"").decode( _ENCODING, errors="replace" ).strip() if stderr: return RuntimeError(stderr) return RuntimeError( "clipboard command failed: " + " ".join(command) + " exited with " + str(result.returncode) ) def read_text(display="", scan_displays=False): last_error = None command_succeeded = False with _display_lock: for candidate in _display_candidates(display, scan_displays): commands = _read_commands(candidate) if not commands: last_error = RuntimeError("no supported clipboard reader found") for command in commands: try: result = _run_command(command, candidate) if result.returncode != 0: last_error = _command_error(command, result) continue command_succeeded = True text = result.stdout.decode(_ENCODING) if text: return text except Exception as error: last_error = error if last_error: if command_succeeded: return None raise last_error return None def write_text(text, display="", scan_displays=False): if not isinstance(text, str) or not text: return False last_error = None with _display_lock: for candidate in _display_candidates(display, scan_displays): commands = _write_commands(candidate) if not commands: last_error = RuntimeError("no supported clipboard writer found") for command in commands: try: result = _run_command(command, candidate, text) if result.returncode == 0: return True last_error = _command_error(command, result) except Exception as error: last_error = error if last_error: raise last_error return False