53969 29.0.50; display frame parameter set incorrectly on pgtk

Johan Myréen <[email protected]>
Newsgroups gmane.emacs.devel
Message-ID <CA+TD5hi-S8RJZytbswC2XU7J62WS=AwWKs=9rSSfr6b7LYOB6Q@mail.gmail.com>
Any progress on this? It would be good if somebody in addition to me
tried Danny Milosavljevic's patch from February, which IMO fixes
the problem. If there are no objections to the patch, it would be nice to
have it installed and the list of open PGTK bugs shortened by one.

I have attached an updated version of the patch against the current master
branch.
bug-53969.patch (application/x-patch, 7.2 KB)
diff --git a/lisp/server.el b/lisp/server.el
index b912b185275..b316687e31c 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -936,6 +936,10 @@ server-create-tty-frame
     ;; the DISPLAY environment on subprocesses.
     (set-frame-parameter frame 'display
                          (getenv-internal "DISPLAY" (process-get proc 'env)))
+    (set-frame-parameter frame 'child-env-display
+                         (getenv-internal "DISPLAY" (process-get proc 'env)))
+    (set-frame-parameter frame 'child-env-wayland-display
+                         (getenv-internal "WAYLAND_DISPLAY" (process-get proc 'env)))
     frame))
 
 (defun server-create-window-system-frame (display nowait proc parent-id
@@ -965,6 +969,12 @@ server-create-window-system-frame
                (server--create-frame
                 nowait proc
                 `((display . ,display)
+                  (child-env-display
+                   . ,(getenv-internal "DISPLAY"
+                                       (process-get proc 'env)))
+                  (child-env-wayland-display
+                   . ,(getenv-internal "WAYLAND_DISPLAY"
+                                       (process-get proc 'env)))
                   ,@(if parent-id
                         `((parent-id . ,(string-to-number parent-id))))
                   ,@parameters))
@@ -992,6 +1002,8 @@ server-create-dumb-terminal-frame
 
 (defun server--create-frame (nowait proc parameters)
   (add-to-list 'frame-inherited-parameters 'client)
+  (add-to-list 'frame-inherited-parameters 'child-env-display)
+  (add-to-list 'frame-inherited-parameters 'child-env-wayland-display)
   ;; When `nowait' is set, flag frame as client-created, but use
   ;; a dummy client.  This will prevent the frame from being deleted
   ;; when emacsclient quits while also preventing
diff --git a/src/callproc.c b/src/callproc.c
index 7eb245f915c..d6e3e9d885f 100644
--- a/src/callproc.c
+++ b/src/callproc.c
@@ -1742,16 +1742,22 @@ getenv_internal (const char *var, ptrdiff_t varlen, char **value,
   /* For DISPLAY try to get the values from the frame or the initial env.  */
   if (strcmp (var, "DISPLAY") == 0)
     {
-#ifndef HAVE_PGTK
+#ifdef HAVE_PGTK
+      /* Use child-env-display, not display, because the display frame
+	 parameter on pgtk contains the Wayland display name, not X11 DISPLAY.  */
+      Lisp_Object display
+	= Fframe_parameter (NILP (frame) ? selected_frame : frame,
+			    Qchild_env_display);
+#else
       Lisp_Object display
 	= Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
+#endif
       if (STRINGP (display))
 	{
 	  *value    = SSDATA (display);
 	  *valuelen = SBYTES (display);
 	  return 1;
 	}
-#endif /* !HAVE_PGTK */
       /* If still not found, Look for DISPLAY in Vinitial_environment.  */
       if (getenv_internal_1 (var, varlen, value, valuelen,
 			     Vinitial_environment))
@@ -1759,6 +1765,26 @@ getenv_internal (const char *var, ptrdiff_t varlen, char **value,
     }
 #endif /* !HAVE_ANDROID */
 
+#ifdef HAVE_PGTK
+  /* For WAYLAND_DISPLAY try to get the value from the frame or the initial env.  */
+  if (strcmp (var, "WAYLAND_DISPLAY") == 0)
+    {
+      Lisp_Object wayland_display
+	= Fframe_parameter (NILP (frame) ? selected_frame : frame,
+			    Qchild_env_wayland_display);
+      if (STRINGP (wayland_display))
+	{
+	  *value    = SSDATA (wayland_display);
+	  *valuelen = SBYTES (wayland_display);
+	  return 1;
+	}
+      /* If still not found, Look for WAYLAND_DISPLAY in Vinitial_environment.  */
+      if (getenv_internal_1 (var, varlen, value, valuelen,
+			     Vinitial_environment))
+	return *value ? 1 : 0;
+    }
+#endif /* HAVE_PGTK */
+
   return 0;
 }
 
@@ -1853,6 +1879,9 @@ make_environment_block (Lisp_Object current_dir)
 #ifndef HAVE_ANDROID
     Lisp_Object display = Qnil;
 #endif /* !HAVE_ANDROID */
+#ifdef HAVE_PGTK
+    Lisp_Object wayland_display = Qnil;
+#endif /* HAVE_PGTK */
 
     new_length = 0;
 
@@ -1867,6 +1896,13 @@ make_environment_block (Lisp_Object current_dir)
 	  /* DISPLAY is specified in process-environment.  */
 	  display = Qt;
 #endif /* !HAVE_ANDROID */
+#ifdef HAVE_PGTK
+	if (strncmp (SSDATA (XCAR (tem)), "WAYLAND_DISPLAY", 15) == 0
+	    && (SDATA (XCAR (tem)) [15] == '\0'
+		|| SDATA (XCAR (tem)) [15] == '='))
+	  /* WAYLAND_DISPLAY is specified in process-environment.  */
+	  wayland_display = Qt;
+#endif /* HAVE_PGTK */
 	new_length++;
       }
 
@@ -1877,18 +1913,13 @@ make_environment_block (Lisp_Object current_dir)
     /* If not provided yet, use the frame's DISPLAY.  */
     if (NILP (display))
       {
-	Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
-
 #ifdef HAVE_PGTK
-	/* The only time GDK actually returns correct information is
-	   when it's running under X Windows.  DISPLAY shouldn't be
-	   set to a Wayland display either, since that's an X specific
-	   variable.  */
-	if (FRAME_WINDOW_P (SELECTED_FRAME ())
-	    && strcmp (G_OBJECT_TYPE_NAME (FRAME_X_DISPLAY (SELECTED_FRAME ())),
-		       "GdkX11Display"))
-	  tmp = Qnil;
-#endif /* HAVE_PGTK */
+	/* Use child-env-display, not display, because the display frame
+	   parameter on pgtk contains the Wayland display name, not X11 DISPLAY.  */
+	Lisp_Object tmp = Fframe_parameter (selected_frame, Qchild_env_display);
+#else
+	Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
+#endif
 
 	if (!STRINGP (tmp) && CONSP (Vinitial_environment))
 	  /* If still not found, Look for DISPLAY in Vinitial_environment.  */
@@ -1902,6 +1933,24 @@ make_environment_block (Lisp_Object current_dir)
       }
 #endif /* !HAVE_ANDROID */
 
+#ifdef HAVE_PGTK
+    /* If not provided yet, use the frame's WAYLAND_DISPLAY.  */
+    if (NILP (wayland_display))
+      {
+	Lisp_Object tmp = Fframe_parameter (selected_frame, Qchild_env_wayland_display);
+
+	if (!STRINGP (tmp) && CONSP (Vinitial_environment))
+	  /* If still not found, Look for WAYLAND_DISPLAY in Vinitial_environment.  */
+	  tmp = Fgetenv_internal (build_string ("WAYLAND_DISPLAY"),
+				  Vinitial_environment);
+	if (STRINGP (tmp))
+	  {
+	    wayland_display = tmp;
+	    new_length++;
+	  }
+      }
+#endif /* HAVE_PGTK */
+
     /* new_length + 2 to include PWD and terminating 0.  */
     env = new_env = xnmalloc (new_length + 2, sizeof *env);
     record_unwind_protect_ptr (xfree, env);
@@ -1920,6 +1969,16 @@ make_environment_block (Lisp_Object current_dir)
       }
 #endif /* !HAVE_ANDROID */
 
+#ifdef HAVE_PGTK
+    if (STRINGP (wayland_display))
+      {
+        char *vdata = xmalloc (sizeof "WAYLAND_DISPLAY=" + SBYTES (wayland_display));
+        record_unwind_protect_ptr (xfree, vdata);
+        lispstpcpy (stpcpy (vdata, "WAYLAND_DISPLAY="), wayland_display);
+        new_env = add_env (env, new_env, vdata);
+      }
+#endif /* HAVE_PGTK */
+
     /* Overrides.  */
     for (tem = Vprocess_environment;
 	 CONSP (tem) && STRINGP (XCAR (tem));
@@ -2258,5 +2317,9 @@ syms_of_callproc (void)
   DEFSYM (Qafter_insert_file_set_buffer_file_coding_system,
 	  "after-insert-file-set-buffer-file-coding-system");
   DEFSYM (Qcoding_system_for_write, "coding-system-for-write");
+#ifdef HAVE_PGTK
+  DEFSYM (Qchild_env_wayland_display, "child-env-wayland-display");
+  DEFSYM (Qchild_env_display, "child-env-display");
+#endif /* HAVE_PGTK */
   DEFSYM (Qexec_suffixes, "exec-suffixes");
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.