diff --git a/xsanecli.py b/xsanecli.py index 7710012..6dda419 100755 --- a/xsanecli.py +++ b/xsanecli.py @@ -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