From 310bcda934a88f2c93e8b34b093853ce695c8d3b Mon Sep 17 00:00:00 2001
From: Po Lu <luangruo@yahoo.com>
Date: Sun, 30 Jan 2022 09:23:40 +0800
Subject: [PATCH] Add support for ARGB visuals on non-Xt builds

* src/gtkutil.c (xg_create_frame_widgets): Enable RGBA visual
on non-PGTK builds.
* src/xfns.c (select_visual): Select 32-bit visuals on non-Xt
builds.
* src/xterm.c (x_draw_fringe_bitmap):
(x_draw_image_glyph_string): Stop using DefaultDepthOfScreen.
(x_query_colors):
* src/xterm.h (x_make_truecolor_pixel): Make colors opaque on
32-bit visuals.
---
 src/gtkutil.c |  2 +-
 src/xfns.c    |  4 ++--
 src/xterm.c   | 19 +++++++++++++++----
 src/xterm.h   |  7 +++++--
 4 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/src/gtkutil.c b/src/gtkutil.c
index 067df216a4a..ef6270dbcf8 100644
--- a/src/gtkutil.c
+++ b/src/gtkutil.c
@@ -1593,7 +1593,7 @@ xg_create_frame_widgets (struct frame *f)
 
   GdkScreen *screen = gtk_widget_get_screen (wtop);
 
-#if !defined HAVE_PGTK && defined USE_CAIRO
+#if !defined HAVE_PGTK
   if (FRAME_DISPLAY_INFO (f)->n_planes == 32)
     {
       GdkVisual *visual = gdk_screen_get_rgba_visual (screen);
diff --git a/src/xfns.c b/src/xfns.c
index b37ba139a22..7fe181fa3fd 100644
--- a/src/xfns.c
+++ b/src/xfns.c
@@ -6375,7 +6375,7 @@ select_visual (struct x_display_info *dpyinfo)
 
       vinfo_template.screen = XScreenNumberOfScreen (screen);
 
-#if defined (USE_GTK) && defined (USE_CAIRO)
+#if !defined USE_X_TOOLKIT
       /* First attempt to use 32-bit visual if available */
 
       vinfo_template.depth = 32;
@@ -6391,7 +6391,7 @@ select_visual (struct x_display_info *dpyinfo)
 	  return;
 	}
 
-#endif /* USE_GTK && USE_CAIRO */
+#endif /* !USE_X_TOOLKIT */
 
       /* 32-bit visual not available, fallback to default visual */
       dpyinfo->visual = DefaultVisualOfScreen (screen);
diff --git a/src/xterm.c b/src/xterm.c
index ab5fd46c4c0..209e99fc09c 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -1966,7 +1966,7 @@ x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fring
       Drawable drawable = FRAME_X_DRAWABLE (f);
       char *bits;
       Pixmap pixmap, clipmask = (Pixmap) 0;
-      int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
+      int depth = FRAME_DISPLAY_INFO (f)->n_planes;
       XGCValues gcv;
 
       if (p->wd > 8)
@@ -2812,12 +2812,12 @@ void
 x_query_colors (struct frame *f, XColor *colors, int ncolors)
 {
   struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
+  int i;
 
   if (dpyinfo->red_bits > 0)
     {
       /* For TrueColor displays, we can decompose the RGB value
 	 directly.  */
-      int i;
       unsigned int rmult, gmult, bmult;
       unsigned int rmask, gmask, bmask;
 
@@ -2854,6 +2854,12 @@ x_query_colors (struct frame *f, XColor *colors, int ncolors)
 	  colors[i].green = (g * gmult) >> 16;
 	  colors[i].blue = (b * bmult) >> 16;
 	}
+
+      if (FRAME_DISPLAY_INFO (f)->n_planes == 32)
+	{
+	  for (i = 0; i < ncolors; ++i)
+	    colors[i].pixel |= ((unsigned long) 0xFF << 24);
+	}
       return;
     }
 
@@ -2871,6 +2877,12 @@ x_query_colors (struct frame *f, XColor *colors, int ncolors)
     }
 
   XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
+
+  if (FRAME_DISPLAY_INFO (f)->n_planes == 32)
+    {
+      for (i = 0; i < ncolors; ++i)
+	colors[i].pixel |= ((unsigned long) 0xFF << 24);
+    }
 }
 
 /* Store F's background color into *BGCOLOR.  */
@@ -3924,8 +3936,7 @@ x_draw_image_glyph_string (struct glyph_string *s)
 	  /* Create a pixmap as large as the glyph string.  Fill it
 	     with the background color.  Copy the image to it, using
 	     its mask.  Copy the temporary pixmap to the display.  */
-	  Screen *screen = FRAME_X_SCREEN (s->f);
-	  int depth = DefaultDepthOfScreen (screen);
+	  int depth = FRAME_DISPLAY_INFO (s->f)->n_planes;
 
 	  /* Create a pixmap as large as the glyph string.  */
           pixmap = XCreatePixmap (display, FRAME_X_DRAWABLE (s->f),
diff --git a/src/xterm.h b/src/xterm.h
index 33887be52b0..aa5bd2caa19 100644
--- a/src/xterm.h
+++ b/src/xterm.h
@@ -1270,7 +1270,7 @@ x_display_set_last_user_time (struct x_display_info *dpyinfo, Time t)
 INLINE unsigned long
 x_make_truecolor_pixel (struct x_display_info *dpyinfo, int r, int g, int b)
 {
-  unsigned long pr, pg, pb;
+  unsigned long pr, pg, pb, pa = 0;
 
   /* Scale down RGB values to the visual's bits per RGB, and shift
      them to the right position in the pixel color.  Note that the
@@ -1279,8 +1279,11 @@ x_make_truecolor_pixel (struct x_display_info *dpyinfo, int r, int g, int b)
   pg = (g >> (16 - dpyinfo->green_bits)) << dpyinfo->green_offset;
   pb = (b >> (16 - dpyinfo->blue_bits))  << dpyinfo->blue_offset;
 
+  if (dpyinfo->n_planes == 32)
+    pa = ((unsigned long) 0xFF << 24);
+
   /* Assemble the pixel color.  */
-  return pr | pg | pb;
+  return pr | pg | pb | pa;
 }
 
 /* If display has an immutable color map, freeing colors is not
-- 
2.39.5