Yalantis/uCrop · error · CImgDisplayException
CImgDisplay::screen_width(): Failed to open X11 display.
Error message
CImgDisplay::screen_width(): Failed to open X11 display.
What it means
CImgDisplay::screen_width() queries the X server for the screen width; if no display is currently open it tries XOpenDisplay(NULL) itself, and throws CImgDisplayException when that connection fails. Same root causes as [26] but triggered by the static screen-size query.
Source
Thrown at ucrop/src/main/jni/CImg.h:10544
colormap[index].green = (unsigned short)(g<<8);
colormap[index].blue = (unsigned short)(b<<8);
colormap[index++].flags = DoRed | DoGreen | DoBlue;
}
}
}
XStoreColors(X11_attr.display,cmap,colormap,256);
delete[] colormap;
}
// Public functions.
static int screen_width() {
cimg::X11_attr &X11_attr = cimg::X11_attr::ref();
Display *const dpy = X11_attr.display;
int res = 0;
if (!dpy) {
Display *const _dpy = XOpenDisplay(0);
if (!_dpy)
throw CImgDisplayException("CImgDisplay::screen_width(): Failed to open X11 display.");
res = DisplayWidth(_dpy,DefaultScreen(_dpy));
XCloseDisplay(_dpy);
} else {
#ifdef cimg_use_xrandr
if (X11_attr.resolutions && X11_attr.curr_resolution)
res = X11_attr.resolutions[X11_attr.curr_resolution].width;
else res = DisplayWidth(dpy,DefaultScreen(dpy));
#else
res = DisplayWidth(dpy,DefaultScreen(dpy));
#endif
}
return res;
}
static int screen_height() {
cimg::X11_attr &X11_attr = cimg::X11_attr::ref();
Display *const dpy = X11_attr.display;View on GitHub (pinned to f788b534b4)
Solutions
- Ensure DISPLAY is set and an X server is reachable before calling (xset -q as a probe)
- Use Xvfb in headless/CI environments: Xvfb :99 & export DISPLAY=:99
- Avoid the query in headless mode — hardcode/default the window size or read resolution from your own config
Example fix
// before
int w = cimg::CImgDisplay::screen_width(); // throws headless
// after
const char *dpy = getenv("DISPLAY");
int w = (dpy && *dpy) ? cimg::CImgDisplay::screen_width() : 1280; // fallback size Defensive patterns
Strategy: try-catch
Validate before calling
const char *d = getenv("DISPLAY");
bool xReady = d && *d && (system("xset -q > /dev/null 2>&1") == 0); Type guard
bool canQueryScreen() { const char *d = getenv("DISPLAY"); return d && *d; } Try / catch
try { w = cimg::CImgDisplay::screen_width(); } catch (const cimg_library::CImgDisplayException &e) { w = 1280; // default fallback size
} Prevention
- Verify DISPLAY before any static CImgDisplay query
- Provide sensible default window dimensions for headless environments
- Start Xvfb in CI jobs that exercise display code
- Do not call screen_width()/screen_height() at process startup on servers
When it happens
Trigger: Calling cimg::CImgDisplay::screen_width() (or screen_height()) in an environment with no reachable X server: DISPLAY unset/invalid, headless CI/container, SSH without forwarding, or X server down.
Common situations: Code that queries screen size at startup to size windows, running on headless servers or in Docker; cron/systemd jobs without DISPLAY; Wayland sessions without XWayland.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- CImgDisplay::assign(): Failed to open X11 display.
- CImgDisplay(): No display available.
- CImgDisplay::assign(): Invalid %u bits screen mode detected
- CImgDisplay::assign(): Max number of displays (512) already
- init_fullscreen(): Xrandr extension not supported by the X s
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/fa141536395ef3a4.
Report an issue: GitHub.