Same update for export to x clipboard. Now using pyperclip.
This commit is contained in:
parent
73206ce393
commit
e76ca9889a
@ -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'
|
||||
]
|
||||
# Remember original display environment variable if it exists
|
||||
originalDisplay = os.environ.get('DISPLAY', '')
|
||||
success = False
|
||||
|
||||
xclip_path = None
|
||||
for path in xclip_paths:
|
||||
if os.path.isfile(path) and os.access(path, os.X_OK):
|
||||
xclip_path = path
|
||||
# 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
|
||||
except Exception:
|
||||
# Failed for this display, try next one
|
||||
continue
|
||||
|
||||
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)
|
||||
# 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user