]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle color allocation failures caused by colormap changes
authorPo Lu <luangruo@yahoo.com>
Mon, 7 Mar 2022 12:08:13 +0000 (20:08 +0800)
committerPo Lu <luangruo@yahoo.com>
Mon, 7 Mar 2022 13:10:53 +0000 (21:10 +0800)
* src/xterm.c (x_alloc_nearest_color_1): Recompute color cells
if allocation of cached value failed.

src/xterm.c

index d3e3ed3a06b6c2b1dfbfee22d2ffd5bc16c04269..30229c45a47d04c4901123d77d744f0987a69be6 100644 (file)
@@ -3857,6 +3857,10 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
       long nearest_delta, trial_delta;
       int x;
       Status status;
+      bool retry = false;
+      int ncolor_cells, i;
+
+    start:
 
       cells = x_color_cells (dpy, &no_cells);
 
@@ -3887,6 +3891,30 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
       color->blue = cells[nearest].blue;
       status = XAllocColor (dpy, cmap, color);
 
+      if (status != 0 && !retry)
+       {
+         /* Our private cache of color cells is probably out of date.
+            Refresh it here, and try to allocate the nearest color
+            from the new colormap.  */
+
+         retry = true;
+         xfree (dpyinfo->color_cells);
+
+         ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (dpyinfo->screen));
+
+         dpyinfo->color_cells = xnmalloc (ncolor_cells,
+                                          sizeof *dpyinfo->color_cells);
+         dpyinfo->ncolor_cells = ncolor_cells;
+
+         for (i = 0; i < ncolor_cells; ++i)
+           dpyinfo->color_cells[i].pixel = i;
+
+         XQueryColors (dpy, dpyinfo->cmap,
+                       dpyinfo->color_cells, ncolor_cells);
+
+         goto start;
+       }
+
       rc = status != 0;
     }
   else