Fix "cannot open display: wayland-0" error in Wayland #1

This commit is contained in:
RunasSudo 2025-01-11 17:56:03 +11:00
parent 8a7940bfbb
commit df8c4042c4
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -35,9 +35,22 @@ def xsanecli_run(procedure, config, run_data, *args):
tempdir = tempfile.mkdtemp('gimp-plugin-xsanecli')
png_out = os.path.join(tempdir, 'out.png')
xsane_env = dict(os.environb)
# Fix if DISPLAY is set to a Wayland display
# For some reason, in Wayland contexts, DISPLAY is clobbered with WAYLAND_DISPLAY when Python is called
# This causes "Gtk-WARNING: cannot open display: wayland-0"
if b'DISPLAY' in xsane_env and xsane_env[b'DISPLAY'].startswith(b'wayland-'):
# Recover the original environment from GIMP (the parent process)
with open('/proc/{}/environ'.format(os.getppid()), 'rb') as f:
orig_env = {var[:var.index(b'=')]: var[var.index(b'=')+1:] for var in f.read().split(b'\0') if var}
if b'DISPLAY' in orig_env:
xsane_env[b'DISPLAY'] = orig_env[b'DISPLAY']
# Open XSane
args = ['xsane', '--save', '--no-mode-selection', '--force-filename', png_out, '--print-filenames'] + ([DEVICE_NAME] if DEVICE_NAME else [])
proc = subprocess.Popen(args, stdout=subprocess.PIPE, encoding='utf-8')
proc = subprocess.Popen(args, stdout=subprocess.PIPE, encoding='utf-8', env=xsane_env)
while True:
# Wait until XSane prints the name of the scanned file, indicating scanning is finished