X11: Use a single process wide X error handler

Replace the grab/release error handler pattern with a process wide
handler that is installed for the lifetime of the display. Errors are
captured by nestable traps identified by request serial numbers, so
traps only capture errors from requests made while they are open, and
errors from requests not covered by any trap are reported rather than
exiting the process.

Requests to windows owned by other clients (selection requestors, drag
and drop peers) are trapped individually, without waiting for a round
trip to the server, and without covering calls into the application.

Also fix reading uninitialized memory when handling a MULTIPLE
selection request whose requestor window has been destroyed, trap
errors when replying to drag sources, report failure to deliver drag
data, and fix detectEWMH() leaving the error handler installed on an
early return.
This commit is contained in:
Kovid Goyal 2026-11-09 09:01:19 +05:30
parent 9e5329d52a
commit 8e73e474b0
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
5 changed files with 275 additions and 121 deletions

View file

@ -165,6 +165,10 @@ Detailed list of changes
plugged and unplugged quickly and the compositor never sent its logical
geometry
- X11: Fix kitty exiting when a program requesting the clipboard or primary
selection destroys its window before kitty can reply. X errors no longer
cause kitty to exit, they are reported instead (:iss:`9248`)
0.50.0 [2026-11-03]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

15
glfw/glx_context.c vendored
View file

@ -358,6 +358,7 @@ _glfwTerminateGLX(void) {
bool
_glfwCreateContextGLX(_GLFWwindow *window, const _GLFWctxconfig *ctxconfig, const _GLFWfbconfig *fbconfig) {
int attribs[40];
int x_error = Success;
GLXFBConfig native = NULL;
GLXContext share = NULL;
@ -389,7 +390,7 @@ _glfwCreateContextGLX(_GLFWwindow *window, const _GLFWctxconfig *ctxconfig, cons
}
}
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
if (_glfw.glx.ARB_create_context) {
int index = 0, mask = 0, flags = 0;
@ -444,25 +445,27 @@ _glfwCreateContextGLX(_GLFWwindow *window, const _GLFWctxconfig *ctxconfig, cons
setAttrib(None, None);
window->context.glx.handle = _glfw.glx.CreateContextAttribsARB(_glfw.x11.display, native, share, True, attribs);
x_error = _glfwPopErrorTrapX11();
// HACK: This is a fallback for broken versions of the Mesa
// implementation of GLX_ARB_create_context_profile that fail
// default 1.0 context creation with a GLXBadProfileARB error in
// violation of the extension spec
if (!window->context.glx.handle) {
if (_glfw.x11.errorCode == _glfw.glx.errorBase + GLXBadProfileARB && ctxconfig->client == GLFW_OPENGL_API &&
ctxconfig->profile == GLFW_OPENGL_ANY_PROFILE && ctxconfig->forward == false) {
if (x_error == _glfw.glx.errorBase + GLXBadProfileARB && ctxconfig->client == GLFW_OPENGL_API && ctxconfig->profile == GLFW_OPENGL_ANY_PROFILE &&
ctxconfig->forward == false) {
_glfwPushErrorTrapX11();
window->context.glx.handle = createLegacyContextGLX(window, native, share);
x_error = _glfwPopErrorTrapX11();
}
}
} else {
window->context.glx.handle = createLegacyContextGLX(window, native, share);
x_error = _glfwPopErrorTrapX11();
}
_glfwReleaseErrorHandlerX11();
if (!window->context.glx.handle) {
_glfwInputErrorX11(GLFW_VERSION_UNAVAILABLE, "GLX: Failed to create context");
_glfwInputErrorX11(GLFW_VERSION_UNAVAILABLE, x_error, "GLX: Failed to create context");
return false;
}

164
glfw/x11_init.c vendored
View file

@ -62,10 +62,9 @@ detectEWMH(void) {
Window *windowFromRoot = NULL;
if (!_glfwGetWindowPropertyX11(_glfw.x11.root, _glfw.x11.NET_SUPPORTING_WM_CHECK, XA_WINDOW, (unsigned char **)&windowFromRoot)) { return; }
_glfwGrabErrorHandlerX11();
// If it exists, it should be the XID of a top-level window
// Then we look for the same property on that window
// Then we look for the same property on that window. The window may be
// stale, _glfwGetWindowPropertyX11() handles the resulting BadWindow.
Window *windowFromChild = NULL;
if (!_glfwGetWindowPropertyX11(*windowFromRoot, _glfw.x11.NET_SUPPORTING_WM_CHECK, XA_WINDOW, (unsigned char **)&windowFromChild)) {
@ -73,8 +72,6 @@ detectEWMH(void) {
return;
}
_glfwReleaseErrorHandlerX11();
// If the property exists, it should contain the XID of the window
if (*windowFromRoot != *windowFromChild) {
@ -165,12 +162,12 @@ read_xi_scroll_devices(void) {
if (!xi.available || xi.major < 2 || (xi.major == 2 && xi.minor < 1) || !xi.LIBINPUT_SCROLL_METHOD_ENABLED) return;
#undef xi
int deviceCount;
// Grab the error handler to prevent XI_BadDevice errors from killing kitty
// when a device is removed between XIQueryDevice and XIGetProperty calls.
_glfwGrabErrorHandlerX11();
// A device can be removed between the XIQueryDevice and XIGetProperty
// calls, the resulting XI_BadDevice errors are expected
_glfwPushErrorTrapX11();
XIDeviceInfo *devices = XIQueryDevice(_glfw.x11.display, XIAllDevices, &deviceCount);
if (!devices) {
_glfwReleaseErrorHandlerX11();
_glfwPopErrorTrapIgnoredX11();
return;
}
for (int i = 0; i < deviceCount; i++) {
@ -303,7 +300,7 @@ read_xi_scroll_devices(void) {
}
}
XIFreeDeviceInfo(devices);
_glfwReleaseErrorHandlerX11();
_glfwPopErrorTrapIgnoredX11();
}
// Look for and initialize supported X11 extensions
@ -613,47 +610,158 @@ createHelperWindow(void) {
return XCreateWindow(_glfw.x11.display, _glfw.x11.root, 0, 0, 1, 1, 0, 0, InputOnly, DefaultVisual(_glfw.x11.display, _glfw.x11.screen), CWEventMask, &wa);
}
// X error handler
// X error handling {{{
//
// A single error handler is installed for the lifetime of the display. Xlib's
// default handler exits the process on any error, which is never acceptable
// as many requests legitimately fail, for instance because they reference a
// window owned by another client that has destroyed it.
//
// Code that expects errors brackets the relevant requests with
// _glfwPushErrorTrapX11() and _glfwPopErrorTrapX11() or
// _glfwPopErrorTrapIgnoredX11(). Traps are identified by request serial
// numbers so they nest correctly and only capture errors from requests made
// while they were open. Errors from requests not covered by any trap indicate
// bugs and are reported, but do not exit the process.
#define traps _glfw.x11.error_traps
static void
remove_error_trap(size_t idx) {
traps.count--;
if (idx < traps.count) memmove(traps.items + idx, traps.items + idx + 1, (traps.count - idx) * sizeof(traps.items[0]));
}
// Remove closed traps whose requests have all been processed by the server,
// as no more errors can arrive for them
static void
prune_error_traps(void) {
const unsigned long processed = XLastKnownRequestProcessed(_glfw.x11.display);
for (size_t i = traps.count; i-- > 0;) {
if (traps.items[i].closed && traps.items[i].end_serial <= processed) remove_error_trap(i);
}
}
static int
errorHandler(Display *display, XErrorEvent *event) {
if (_glfw.x11.display != display) return 0;
_glfw.x11.errorCode = event->error_code;
if (_glfw.x11.display != display) { return traps.previous_handler ? traps.previous_handler(display, event) : 0; }
// Search innermost first, nested traps cover a subset of their parent's range
for (size_t i = traps.count; i-- > 0;) {
_GLFWerrortrapX11 *t = traps.items + i;
if (event->serial >= t->start_serial && (!t->closed || event->serial <= t->end_serial)) {
if (t->error_code == Success) t->error_code = event->error_code;
return 0;
}
}
char buffer[_GLFW_MESSAGE_SIZE];
XGetErrorText(display, event->error_code, buffer, sizeof(buffer));
_glfwInputError(
GLFW_PLATFORM_ERROR,
"X11: Unhandled X error: %s (request: %u.%u, resource: 0x%lx, serial: %lu)",
buffer,
(unsigned)event->request_code,
(unsigned)event->minor_code,
event->resourceid,
event->serial);
return 0;
}
static void
install_error_handler(void) {
traps.previous_handler = XSetErrorHandler(errorHandler);
}
static void
uninstall_error_handler(void) {
XSetErrorHandler(traps.previous_handler);
traps.previous_handler = NULL;
free(traps.items);
traps.items = NULL;
traps.count = 0;
traps.capacity = 0;
}
// Closes the innermost open trap, returning its index or traps.count if there
// is no open trap
static size_t
close_error_trap(void) {
for (size_t i = traps.count; i-- > 0;) {
if (!traps.items[i].closed) {
traps.items[i].closed = true;
traps.items[i].end_serial = XNextRequest(_glfw.x11.display) - 1;
return i;
}
}
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Popping an X error trap that was never pushed, this is a bug");
return traps.count;
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Sets the X error handler callback
// Starts capturing X errors caused by subsequent requests. Every call must be
// matched by a call to _glfwPopErrorTrapX11() or _glfwPopErrorTrapIgnoredX11().
//
void
_glfwGrabErrorHandlerX11(void) {
_glfw.x11.errorCode = Success;
XSetErrorHandler(errorHandler);
_glfwPushErrorTrapX11(void) {
prune_error_traps();
if (traps.count >= traps.capacity) {
const size_t capacity = traps.capacity ? traps.capacity * 2 : 16;
_GLFWerrortrapX11 *items = realloc(traps.items, capacity * sizeof(traps.items[0]));
if (!items) {
// Callers rely on every push being matched by a pop, so there is
// no way to recover from this
_glfwInputError(GLFW_OUT_OF_MEMORY, "X11: Failed to allocate memory for X error trap");
abort();
}
traps.items = items;
traps.capacity = capacity;
}
traps.items[traps.count++] = (_GLFWerrortrapX11){.start_serial = XNextRequest(_glfw.x11.display)};
}
// Clears the X error handler callback
// Stops capturing X errors for the innermost trap, waiting for the server to
// process its requests if needed. Returns the code of the first error caused
// by a request made while the trap was open, or Success.
//
void
_glfwReleaseErrorHandlerX11(void) {
// Synchronize to make sure all commands are processed
XSync(_glfw.x11.display, False);
XSetErrorHandler(NULL);
int
_glfwPopErrorTrapX11(void) {
const size_t i = close_error_trap();
if (i >= traps.count) return Success;
// Requests that return a reply are processed synchronously, so a round
// trip is only needed if the trap covers requests without one
if (traps.items[i].end_serial >= traps.items[i].start_serial && XLastKnownRequestProcessed(_glfw.x11.display) < traps.items[i].end_serial) {
XSync(_glfw.x11.display, False);
}
const int code = traps.items[i].error_code;
remove_error_trap(i);
return code;
}
// Reports the specified error, appending information about the last X error
// Stops capturing X errors for the innermost trap, discarding any errors
// caused by its requests without waiting for the server to process them. Use
// it for requests whose failure is expected and harmless, such as replies to
// windows owned by other clients, which may have been destroyed.
//
void
_glfwInputErrorX11(int error, const char *message) {
_glfwPopErrorTrapIgnoredX11(void) {
close_error_trap();
prune_error_traps();
}
#undef traps
// Reports the specified error, appending a description of the specified X error
//
void
_glfwInputErrorX11(int error, int x_error_code, const char *message) {
char buffer[_GLFW_MESSAGE_SIZE];
XGetErrorText(_glfw.x11.display, _glfw.x11.errorCode, buffer, sizeof(buffer));
XGetErrorText(_glfw.x11.display, x_error_code, buffer, sizeof(buffer));
_glfwInputError(error, "%s: %s", message, buffer);
}
// }}}
// Creates a native cursor object from the specified image and hotspot
//
@ -716,6 +824,7 @@ _glfwPlatformInit(bool *supports_window_occlusion) {
return false;
}
install_error_handler();
if (!initPollData(&_glfw.x11.eventLoopData, ConnectionNumber(_glfw.x11.display))) {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to initialize event loop data");
@ -769,6 +878,7 @@ _glfwPlatformTerminate(void) {
if (_glfw.x11.display) {
XCloseDisplay(_glfw.x11.display);
_glfw.x11.display = NULL;
uninstall_error_handler();
_glfw.x11.eventLoopData.fds[0].fd = -1;
}

29
glfw/x11_platform.h vendored
View file

@ -270,6 +270,18 @@ typedef struct XdndSelectionRequest {
// X11-specific global data
//
// A range of request serial numbers whose X errors are captured rather than
// reported, see _glfwPushErrorTrapX11()
typedef struct _GLFWerrortrapX11 {
// Serial of the first request covered by this trap
unsigned long start_serial;
// Serial of the last request covered by this trap, valid once closed
unsigned long end_serial;
bool closed;
// The first error received for a request covered by this trap
int error_code;
} _GLFWerrortrapX11;
typedef struct _GLFWlibraryX11 {
Display *display;
int screen;
@ -283,8 +295,14 @@ typedef struct _GLFWlibraryX11 {
Cursor hiddenCursorHandle;
// Context for mapping window XIDs to _GLFWwindow pointers
XContext context;
// Most recent error code received by X error handler
int errorCode;
// X error handling, see _glfwPushErrorTrapX11()
struct {
// Traps, in the order they were pushed. Closed traps stay here until
// the server has processed all of their requests.
_GLFWerrortrapX11 *items;
size_t count, capacity;
XErrorHandler previous_handler;
} error_traps;
// Where to place the cursor when re-enabled
double restoreCursorPosX, restoreCursorPosY;
// The window whose disabled cursor mode is active
@ -560,9 +578,10 @@ Cursor _glfwCreateCursorX11(const GLFWimage *image, int xhot, int yhot);
unsigned long _glfwGetWindowPropertyX11(Window window, Atom property, Atom type, unsigned char **value);
bool _glfwIsVisualTransparentX11(Visual *visual);
void _glfwGrabErrorHandlerX11(void);
void _glfwReleaseErrorHandlerX11(void);
void _glfwInputErrorX11(int error, const char *message);
void _glfwPushErrorTrapX11(void);
int _glfwPopErrorTrapX11(void);
void _glfwPopErrorTrapIgnoredX11(void);
void _glfwInputErrorX11(int error, int x_error_code, const char *message);
void _glfwGetSystemContentScaleX11(float *xscale, float *yscale, bool bypass_cache);
void _glfwPushSelectionToManagerX11(void);

184
glfw/x11_window.c vendored
View file

@ -325,7 +325,7 @@ set_fullscreen(_GLFWwindow *window, bool on) {
static bool warned = false;
if (!warned) {
warned = true;
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, "X11: Failed to toggle fullscreen, the window manager does not support it");
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to toggle fullscreen, the window manager does not support it");
}
}
}
@ -785,7 +785,7 @@ createNativeWindow(_GLFWwindow *window, const _GLFWwndconfig *wndconfig, Visual
wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask |
FocusChangeMask | VisibilityChangeMask | EnterWindowMask | LeaveWindowMask | PropertyChangeMask;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
window->x11.parent = _glfw.x11.root;
debug_rendering("Creating window with geometry: %dx%d@%dx%d\n", wg.width, wg.height, wg.x, wg.y);
@ -803,10 +803,10 @@ createNativeWindow(_GLFWwindow *window, const _GLFWwndconfig *wndconfig, Visual
CWBorderPixel | CWColormap | CWEventMask,
&wa);
_glfwReleaseErrorHandlerX11();
const int x_error = _glfwPopErrorTrapX11();
if (!window->x11.handle) {
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, "X11: Failed to create window");
if (!window->x11.handle || x_error != Success) {
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, x_error, "X11: Failed to create window");
return false;
}
@ -903,19 +903,36 @@ get_clipboard_data(const _GLFWClipboardData *cd, const char *mime, char **data)
static void
get_atom_names(const Atom *atoms, int count, char **atom_names) {
_glfwGrabErrorHandlerX11();
XGetAtomNames(_glfw.x11.display, (Atom *)atoms, count, atom_names);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode != Success) {
// The atoms come from other clients and may be invalid
_glfwPushErrorTrapX11();
const Status ok = XGetAtomNames(_glfw.x11.display, (Atom *)atoms, count, atom_names);
if (_glfwPopErrorTrapX11() != Success || !ok) {
for (int i = 0; i < count; i++) {
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
atom_names[i] = XGetAtomName(_glfw.x11.display, atoms[i]);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode != Success) atom_names[i] = NULL;
if (_glfwPopErrorTrapX11() != Success) atom_names[i] = NULL;
}
}
}
// Requests targeting windows owned by other clients, such as selection
// requestors and drag and drop peers. These clients can destroy their windows
// at any time, so the resulting errors are expected and ignored. Use these
// only around the X requests themselves, not around calls into the
// application, so as not to ignore errors in unrelated requests.
static void
change_foreign_property(Window w, Atom property, Atom type, int format, const unsigned char *data, int nelements) {
_glfwPushErrorTrapX11();
XChangeProperty(_glfw.x11.display, w, property, type, format, PropModeReplace, data, nelements);
_glfwPopErrorTrapIgnoredX11();
}
static void
send_foreign_event(Window w, XEvent *event) {
_glfwPushErrorTrapX11();
XSendEvent(_glfw.x11.display, w, False, NoEventMask, event);
_glfwPopErrorTrapIgnoredX11();
}
// Set the specified property to the selection converted to the requested target
//
@ -945,7 +962,7 @@ writeTargetToProperty(const XSelectionRequestEvent *request) {
targets[0] = _glfw.x11.TARGETS;
targets[1] = _glfw.x11.MULTIPLE;
for (size_t i = 0; i < aa->sz; i++) targets[i + 2] = aa->array[i].atom;
XChangeProperty(_glfw.x11.display, request->requestor, request->property, XA_ATOM, 32, PropModeReplace, (unsigned char *)targets, aa->sz + 2);
change_foreign_property(request->requestor, request->property, XA_ATOM, 32, (unsigned char *)targets, aa->sz + 2);
free(targets);
return request->property;
}
@ -953,10 +970,14 @@ writeTargetToProperty(const XSelectionRequestEvent *request) {
if (request->target == _glfw.x11.MULTIPLE) {
// Multiple conversions were requested
Atom *targets;
Atom *targets = NULL;
size_t i, j, count;
count = _glfwGetWindowPropertyX11(request->requestor, request->property, _glfw.x11.ATOM_PAIR, (unsigned char **)&targets);
// Fails if the requestor has destroyed its window or set a malformed property
if (!targets) return None;
// The property is a list of (target, property) pairs
count -= count % 2;
for (i = 0; i < count; i += 2) {
for (j = 0; j < aa->sz; j++) {
@ -967,12 +988,12 @@ writeTargetToProperty(const XSelectionRequestEvent *request) {
char *data = NULL;
size_t sz = get_clipboard_data(cd, aa->array[j].mime, &data);
if (data) XChangeProperty(_glfw.x11.display, request->requestor, targets[i + 1], targets[i], 8, PropModeReplace, (unsigned char *)data, sz);
if (data) change_foreign_property(request->requestor, targets[i + 1], targets[i], 8, (unsigned char *)data, sz);
free(data);
} else targets[i + 1] = None;
}
XChangeProperty(_glfw.x11.display, request->requestor, request->property, _glfw.x11.ATOM_PAIR, 32, PropModeReplace, (unsigned char *)targets, count);
change_foreign_property(request->requestor, request->property, _glfw.x11.ATOM_PAIR, 32, (unsigned char *)targets, count);
XFree(targets);
@ -983,7 +1004,7 @@ writeTargetToProperty(const XSelectionRequestEvent *request) {
// The request is a check whether we support SAVE_TARGETS
// It should be handled as a no-op side effect target
XChangeProperty(_glfw.x11.display, request->requestor, request->property, _glfw.x11.NULL_, 32, PropModeReplace, NULL, 0);
change_foreign_property(request->requestor, request->property, _glfw.x11.NULL_, 32, NULL, 0);
return request->property;
}
@ -996,7 +1017,7 @@ writeTargetToProperty(const XSelectionRequestEvent *request) {
char *data = NULL;
size_t sz = get_clipboard_data(cd, aa->array[i].mime, &data);
if (data) XChangeProperty(_glfw.x11.display, request->requestor, request->property, request->target, 8, PropModeReplace, (unsigned char *)data, sz);
if (data) change_foreign_property(request->requestor, request->property, request->target, 8, (unsigned char *)data, sz);
free(data);
return request->property;
@ -1020,7 +1041,7 @@ handleSelectionClear(XEvent *event) {
}
static void
answerSelectionRequest(XEvent *event) {
handleSelectionRequest(XEvent *event) {
const XSelectionRequestEvent *request = &event->xselectionrequest;
XEvent reply = {SelectionNotify};
@ -1041,15 +1062,7 @@ answerSelectionRequest(XEvent *event) {
targets[0] = _glfw.x11.TARGETS;
targets[1] = _glfw.x11.MULTIPLE;
for (size_t i = 0; i < _glfw.x11.drag.type_count; i++) targets[i + 2] = _glfw.x11.drag.type_atoms[i];
XChangeProperty(
_glfw.x11.display,
request->requestor,
request->property,
XA_ATOM,
32,
PropModeReplace,
(unsigned char *)targets,
_glfw.x11.drag.type_count + 2);
change_foreign_property(request->requestor, request->property, XA_ATOM, 32, (unsigned char *)targets, _glfw.x11.drag.type_count + 2);
free(targets);
reply.xselection.property = request->property;
}
@ -1105,17 +1118,7 @@ answerSelectionRequest(XEvent *event) {
// Handle regular clipboard/primary selection
reply.xselection.property = writeTargetToProperty(request);
}
XSendEvent(_glfw.x11.display, request->requestor, False, 0, &reply);
}
static void
handleSelectionRequest(XEvent *event) {
// The requestor owns its window and may destroy it before our reply
// arrives. The resulting BadWindow errors must not reach Xlib's default
// error handler, which exits the process.
_glfwGrabErrorHandlerX11();
answerSelectionRequest(event);
_glfwReleaseErrorHandlerX11();
send_foreign_event(request->requestor, &reply);
}
static void
@ -1441,7 +1444,7 @@ end_drop(_GLFWwindow *window, GLFWDragOperationType op) {
case GLFW_DRAG_OPERATION_NONE: reply.xclient.data.l[2] = _glfw.x11.XdndActionCopy; break;
}
}
XSendEvent(_glfw.x11.display, dnd.source, False, NoEventMask, &reply);
send_foreign_event(dnd.source, &reply);
XFlush(_glfw.x11.display);
}
@ -1475,7 +1478,7 @@ update_drop_state(_GLFWwindow *window, size_t accepted_count) {
// Always send XdndStatus (accepted or rejected). XdndFinished must only be
// sent after XdndDrop, never during drag motion — sending it here was
// prematurely terminating the drag source when no MIME type was accepted.
XSendEvent(_glfw.x11.display, _glfw.x11.xdnd.source, False, NoEventMask, &reply);
send_foreign_event(_glfw.x11.xdnd.source, &reply);
XFlush(_glfw.x11.display);
}
@ -1634,10 +1637,10 @@ drop_move(_GLFWwindow *window, XEvent *event) {
const int yabs = (event->xclient.data.l[2]) & 0xffff;
Window dummy;
int xpos = 0, ypos = 0;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XTranslateCoordinates(_glfw.x11.display, _glfw.x11.root, window->x11.handle, xabs, yabs, &xpos, &ypos, &dummy);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode != Success) _glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to get DND event position");
int x_error = _glfwPopErrorTrapX11();
if (x_error != Success) _glfwInputErrorX11(GLFW_PLATFORM_ERROR, x_error, "X11: Failed to get DND event position");
_glfwInputCursorPos(window, xpos, ypos);
if (reset_dnd_copy_mimes()) {
size_t mimes_count = _glfwInputDropEvent(window, GLFW_DROP_MOVE, xpos, ypos, dnd.copy_mimes, dnd.copy_mimes_count, dnd.from_self);
@ -2096,11 +2099,11 @@ processEvent(XEvent *event) {
// the position into root (screen) coordinates
if (!event->xany.send_event && window->x11.parent != _glfw.x11.root) {
Window dummy;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XTranslateCoordinates(_glfw.x11.display, window->x11.parent, _glfw.x11.root, xpos, ypos, &xpos, &ypos, &dummy);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode != Success) {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to translate ConfigureNotiy co-ords for reparented window");
int x_error = _glfwPopErrorTrapX11();
if (x_error != Success) {
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, x_error, "X11: Failed to translate ConfigureNotify co-ords for reparented window");
return;
}
}
@ -2250,10 +2253,19 @@ unsigned long
_glfwGetWindowPropertyX11(Window window, Atom property, Atom type, unsigned char **value) {
Atom actualType;
int actualFormat;
unsigned long itemCount, bytesAfter;
XGetWindowProperty(_glfw.x11.display, window, property, 0, LONG_MAX, False, type, &actualType, &actualFormat, &itemCount, &bytesAfter, value);
unsigned long itemCount = 0, bytesAfter;
*value = NULL;
// The window may belong to another client and have been destroyed. Xlib
// leaves the output parameters unset on failure.
_glfwPushErrorTrapX11();
const int status =
XGetWindowProperty(_glfw.x11.display, window, property, 0, LONG_MAX, False, type, &actualType, &actualFormat, &itemCount, &bytesAfter, value);
_glfwPopErrorTrapIgnoredX11();
if (status != Success) {
*value = NULL;
return 0;
}
return itemCount;
}
@ -2444,10 +2456,10 @@ _glfwPlatformGetWindowPos(_GLFWwindow *window, int *xpos, int *ypos) {
Window dummy;
int x = 0, y = 0;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XTranslateCoordinates(_glfw.x11.display, window->x11.handle, _glfw.x11.root, 0, 0, &x, &y, &dummy);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode != Success) _glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to get window position");
int x_error = _glfwPopErrorTrapX11();
if (x_error != Success) _glfwInputErrorX11(GLFW_PLATFORM_ERROR, x_error, "X11: Failed to get window position");
if (xpos) *xpos = x;
if (ypos) *ypos = y;
@ -2785,13 +2797,13 @@ _glfwPlatformWindowHovered(_GLFWwindow *window) {
int rootX, rootY, childX, childY;
unsigned int mask;
_glfwGrabErrorHandlerX11();
// The child window found in the previous iteration may have been
// destroyed in the meantime
_glfwPushErrorTrapX11();
const Bool result = XQueryPointer(_glfw.x11.display, w, &root, &w, &rootX, &rootY, &childX, &childY, &mask);
const int x_error = _glfwPopErrorTrapX11();
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode == BadWindow) w = _glfw.x11.root;
if (x_error == BadWindow) w = _glfw.x11.root;
else if (!result) return false;
else if (w == window->x11.handle) return true;
}
@ -3504,7 +3516,7 @@ window_supports_xdnd(Window win, int *version_out) {
// Find the XdndAware window at the given coordinates
static Window
find_xdnd_aware_target(Window root, int root_x, int root_y, int *version_out) {
find_xdnd_aware_target_unsafe(Window root, int root_x, int root_y, int *version_out) {
Window target = None;
Window child = root;
Window parent = root;
@ -3556,6 +3568,17 @@ find_xdnd_aware_target(Window root, int root_x, int root_y, int *version_out) {
return None;
}
static Window
find_xdnd_aware_target(Window root, int root_x, int root_y, int *version_out) {
// The windows being examined belong to other clients and may be destroyed
// at any time. All requests used have replies, so the failures are
// handled by checking their return values.
_glfwPushErrorTrapX11();
Window ans = find_xdnd_aware_target_unsafe(root, root_x, root_y, version_out);
_glfwPopErrorTrapIgnoredX11();
return ans;
}
// Send XdndEnter message to target window
static void
send_xdnd_enter(Window target, int version) {
@ -3587,11 +3610,9 @@ send_xdnd_enter(Window target, int version) {
for (size_t i = 0; i < _glfw.x11.drag.type_count && i < 3; i++) { event.xclient.data.l[2 + i] = _glfw.x11.drag.type_atoms[i]; }
}
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XSendEvent(_glfw.x11.display, target, False, NoEventMask, &event);
XFlush(_glfw.x11.display);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode == BadWindow) _glfw.x11.drag.current_target = None;
if (_glfwPopErrorTrapX11() == BadWindow) _glfw.x11.drag.current_target = None;
}
// Send XdndPosition message to target window
@ -3610,11 +3631,9 @@ send_xdnd_position(Window target, int root_x, int root_y, Time timestamp) {
event.xclient.data.l[3] = timestamp;
event.xclient.data.l[4] = _glfw.x11.drag.action_atom;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XSendEvent(_glfw.x11.display, target, False, NoEventMask, &event);
XFlush(_glfw.x11.display);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode == BadWindow) _glfw.x11.drag.current_target = None;
if (_glfwPopErrorTrapX11() == BadWindow) _glfw.x11.drag.current_target = None;
else _glfw.x11.drag.waiting_for_status = true;
}
@ -3630,11 +3649,9 @@ send_xdnd_leave(Window target) {
event.xclient.format = 32;
event.xclient.data.l[0] = _glfw.x11.drag.source_window;
_glfwGrabErrorHandlerX11();
XSendEvent(_glfw.x11.display, target, False, NoEventMask, &event);
XFlush(_glfw.x11.display);
_glfwReleaseErrorHandlerX11();
// BadWindow on leave is benign – the target window is already gone
send_foreign_event(target, &event);
XFlush(_glfw.x11.display);
}
// Send XdndDrop message to target window
@ -3651,11 +3668,9 @@ send_xdnd_drop(Window target, Time timestamp) {
event.xclient.data.l[1] = 0; // Reserved
event.xclient.data.l[2] = timestamp;
_glfwGrabErrorHandlerX11();
_glfwPushErrorTrapX11();
XSendEvent(_glfw.x11.display, target, False, NoEventMask, &event);
XFlush(_glfw.x11.display);
_glfwReleaseErrorHandlerX11();
if (_glfw.x11.errorCode == BadWindow) {
if (_glfwPopErrorTrapX11() == BadWindow) {
// Target window was destroyed; cancel the drag gracefully
_GLFWwindow *window = _glfwWindowForId(_glfw.drag.window_id);
if (window) {
@ -3899,10 +3914,10 @@ static void
send_drag_data(const char *mime_type, const char *data, size_t data_sz, Window requestor, Atom property, Atom target) {
(void)mime_type; // Parameter kept for consistency with other platforms
if (data && data_sz > 0) {
XChangeProperty(_glfw.x11.display, requestor, property, target, 8, PropModeReplace, (unsigned char *)data, data_sz);
change_foreign_property(requestor, property, target, 8, (const unsigned char *)data, data_sz);
} else {
// Send empty property to indicate no data
XChangeProperty(_glfw.x11.display, requestor, property, target, 8, PropModeReplace, (unsigned char *)"", 0);
change_foreign_property(requestor, property, target, 8, (const unsigned char *)"", 0);
}
}
@ -4084,9 +4099,8 @@ _glfwPlatformDragDataReady(const char *mime_type, const char *data UNUSED, size_
_glfwInputDragSourceRequest(window, &ev);
// The requestor may have destroyed its window in the meantime,
// see handleSelectionRequest()
_glfwGrabErrorHandlerX11();
// The requestor may have destroyed its window in the meantime
_glfwPushErrorTrapX11();
send_drag_data(
mime_type,
ev.data,
@ -4106,10 +4120,14 @@ _glfwPlatformDragDataReady(const char *mime_type, const char *data UNUSED, size_
reply.xselection.property = _glfw.x11.drag.pending_requests[i].property;
reply.xselection.time = CurrentTime;
XSendEvent(_glfw.x11.display, _glfw.x11.drag.pending_requests[i].requestor, False, 0, &reply);
_glfwReleaseErrorHandlerX11();
XSendEvent(_glfw.x11.display, _glfw.x11.drag.pending_requests[i].requestor, False, NoEventMask, &reply);
const int x_error = _glfwPopErrorTrapX11();
_glfw.x11.drag.pending_requests[i].inflight = false;
if (x_error != Success) {
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, x_error, "X11: Failed to deliver drag data to drop target");
return EPIPE;
}
break;
}
}