From e76ca9889a1a489b0835f802d179948a4873ce04 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sun, 2 Mar 2025 16:04:38 -0500 Subject: [PATCH] Same update for export to x clipboard. Now using pyperclip. --- .../commands/export_clipboard_to_x.py | 84 +++++++++---------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/src/fenrirscreenreader/commands/commands/export_clipboard_to_x.py b/src/fenrirscreenreader/commands/commands/export_clipboard_to_x.py index 203024df..e4a7fe49 100644 --- a/src/fenrirscreenreader/commands/commands/export_clipboard_to_x.py +++ b/src/fenrirscreenreader/commands/commands/export_clipboard_to_x.py @@ -5,15 +5,16 @@ # By Chrys, Storm Dragon, and contributers. from fenrirscreenreader.core import debug -import subprocess, os -from subprocess import Popen, PIPE +import os import _thread +import pyperclip class command(): def __init__(self): pass - def initialize(self, environment): + def initialize(self, environment, scriptPath=''): self.env = environment + self.scriptPath = scriptPath def shutdown(self): pass def getDescription(self): @@ -22,56 +23,47 @@ class command(): _thread.start_new_thread(self._threadRun , ()) def _threadRun(self): try: + # Check if clipboard is empty if self.env['runtime']['memoryManager'].isIndexListEmpty('clipboardHistory'): self.env['runtime']['outputManager'].presentText(_('clipboard empty'), interrupt=True) return - + + # Get current clipboard content clipboard = self.env['runtime']['memoryManager'].getIndexListElement('clipboardHistory') - user = self.env['general']['currUser'] - - # First try to find xclip in common locations - xclip_paths = [ - '/usr/bin/xclip', - '/bin/xclip', - '/usr/local/bin/xclip' - ] - - xclip_path = None - for path in xclip_paths: - if os.path.isfile(path) and os.access(path, os.X_OK): - xclip_path = path + + # Remember original display environment variable if it exists + originalDisplay = os.environ.get('DISPLAY', '') + success = False + + # Try different display options + for i in range(10): + display = f":{i}" + try: + # Set display environment variable + os.environ['DISPLAY'] = display + # Attempt to set clipboard content + pyperclip.copy(clipboard) + # If we get here without exception, we found a working display + success = True break - - if not xclip_path: - self.env['runtime']['outputManager'].presentText( - 'xclip not found in common locations', - interrupt=True - ) - return - - for display in range(10): - p = Popen( - ['su', user, '-p', '-c', f"{xclip_path} -d :{display} -selection clipboard"], - stdin=PIPE, stdout=PIPE, stderr=PIPE, preexec_fn=os.setpgrp - ) - stdout, stderr = p.communicate(input=clipboard.encode('utf-8')) - - self.env['runtime']['outputManager'].interruptOutput() - - stderr = stderr.decode('utf-8') - stdout = stdout.decode('utf-8') - - if stderr == '': - break - - if stderr != '': - self.env['runtime']['outputManager'].presentText(stderr, soundIcon='', interrupt=False) + except Exception: + # Failed for this display, try next one + continue + + # Restore original display setting + if originalDisplay: + os.environ['DISPLAY'] = originalDisplay else: - self.env['runtime']['outputManager'].presentText('exported to the X session.', interrupt=True) - + os.environ.pop('DISPLAY', None) + + # Notify the user of the result + if success: + self.env['runtime']['outputManager'].presentText(_('exported to the X session.'), interrupt=True) + else: + self.env['runtime']['outputManager'].presentText(_('failed to export to X clipboard. No available display found.'), interrupt=True) + except Exception as e: self.env['runtime']['outputManager'].presentText(str(e), soundIcon='', interrupt=False) - - + def setCallback(self, callback): pass