From b4ccc8a42335c0eb74ab8d4bcd0c50ce67459df0 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Sat, 9 Nov 2024 16:31:13 -0800 Subject: [PATCH] clipserve: Retry GetSelectionOwner check when acquiring selection This hasn't come up, but better to avoid using expect() if possible. --- src/clipserve.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/clipserve.c b/src/clipserve.c index ba69a78..43cbf7f 100644 --- a/src/clipserve.c +++ b/src/clipserve.c @@ -116,8 +116,21 @@ static void _nonnull_ serve_clipboard(uint64_t hash, selections[1] = XInternAtom(dpy, "CLIPBOARD", False); for (size_t i = 0; i < arrlen(selections); i++) { - XSetSelectionOwner(dpy, selections[i], win, CurrentTime); - expect(XGetSelectionOwner(dpy, selections[i]) == win); // ICCCM 2.1 + bool success = false; + for (int attempts = 0; attempts < 5; attempts++) { + XSetSelectionOwner(dpy, selections[i], win, CurrentTime); + + // According to ICCCM 2.1, a client acquiring a selection should + // confirm success by verifying with GetSelectionOwner. + if (XGetSelectionOwner(dpy, selections[i]) == win) { + success = true; + break; + } + } + if (!success) { + die("Failed to set selection for %s\n", + XGetAtomName(dpy, selections[i])); + } } remaining_selections = arrlen(selections);