From 4b0e1c6502534298465675a32ff65653c12df17d Mon Sep 17 00:00:00 2001 From: Po Lu Date: Mon, 7 Mar 2022 20:08:13 +0800 Subject: [PATCH] Handle color allocation failures caused by colormap changes * src/xterm.c (x_alloc_nearest_color_1): Recompute color cells if allocation of cached value failed. --- src/xterm.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/xterm.c b/src/xterm.c index d3e3ed3a06b..30229c45a47 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -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 -- 2.39.5