Same update for export to x clipboard. Now using pyperclip.

This commit is contained in:
Storm Dragon 2025-03-02 16:04:38 -05:00
parent 73206ce393
commit e76ca9889a

View File

@ -5,15 +5,16 @@
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug from fenrirscreenreader.core import debug
import subprocess, os import os
from subprocess import Popen, PIPE
import _thread import _thread
import pyperclip
class command(): class command():
def __init__(self): def __init__(self):
pass pass
def initialize(self, environment): def initialize(self, environment, scriptPath=''):
self.env = environment self.env = environment
self.scriptPath = scriptPath
def shutdown(self): def shutdown(self):
pass pass
def getDescription(self): def getDescription(self):
@ -22,56 +23,47 @@ class command():
_thread.start_new_thread(self._threadRun , ()) _thread.start_new_thread(self._threadRun , ())
def _threadRun(self): def _threadRun(self):
try: try:
# Check if clipboard is empty
if self.env['runtime']['memoryManager'].isIndexListEmpty('clipboardHistory'): if self.env['runtime']['memoryManager'].isIndexListEmpty('clipboardHistory'):
self.env['runtime']['outputManager'].presentText(_('clipboard empty'), interrupt=True) self.env['runtime']['outputManager'].presentText(_('clipboard empty'), interrupt=True)
return return
# Get current clipboard content
clipboard = self.env['runtime']['memoryManager'].getIndexListElement('clipboardHistory') clipboard = self.env['runtime']['memoryManager'].getIndexListElement('clipboardHistory')
user = self.env['general']['currUser']
# Remember original display environment variable if it exists
# First try to find xclip in common locations originalDisplay = os.environ.get('DISPLAY', '')
xclip_paths = [ success = False
'/usr/bin/xclip',
'/bin/xclip', # Try different display options
'/usr/local/bin/xclip' for i in range(10):
] display = f":{i}"
try:
xclip_path = None # Set display environment variable
for path in xclip_paths: os.environ['DISPLAY'] = display
if os.path.isfile(path) and os.access(path, os.X_OK): # Attempt to set clipboard content
xclip_path = path pyperclip.copy(clipboard)
# If we get here without exception, we found a working display
success = True
break break
except Exception:
if not xclip_path: # Failed for this display, try next one
self.env['runtime']['outputManager'].presentText( continue
'xclip not found in common locations',
interrupt=True # Restore original display setting
) if originalDisplay:
return os.environ['DISPLAY'] = originalDisplay
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)
else: 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: except Exception as e:
self.env['runtime']['outputManager'].presentText(str(e), soundIcon='', interrupt=False) self.env['runtime']['outputManager'].presentText(str(e), soundIcon='', interrupt=False)
def setCallback(self, callback): def setCallback(self, callback):
pass pass