Skip to content

Commit

Permalink
linux: retry if XOpenDisplay fails
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Aug 29, 2021
1 parent 155b185 commit 87d06ab
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions clipboard_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ int clipboard_test() {
// if start is provided, the value of start will be changed to 1 to indicate
// if the write is availiable for reading.
int clipboard_write(char *typ, unsigned char *buf, size_t n, uintptr_t handle) {
Display* d = XOpenDisplay(0);
Display* d = NULL;
for (int i = 0; i < 42; i++) {
d = XOpenDisplay(0);
if (d == NULL) {
continue;
}
break;
}
if (d == NULL) {
if (handle != 0) syncStatus(handle, -1);
syncStatus(handle, -1);
return -1;
}

Expand All @@ -47,22 +54,22 @@ int clipboard_write(char *typ, unsigned char *buf, size_t n, uintptr_t handle) {
Atom target = XInternAtom(d, typ, True);
if (target == None) {
XCloseDisplay(d);
if (handle != 0) syncStatus(handle, -2);
syncStatus(handle, -2);
return -2;
}

XSetSelectionOwner(d, sel, w, CurrentTime);
if (XGetSelectionOwner(d, sel) != w) {
XCloseDisplay(d);
if (handle != 0) syncStatus(handle, -3);
syncStatus(handle, -3);
return -3;
}

XEvent event;
XSelectionRequestEvent* xsr;
int notified = 0;
for (;;) {
if (handle != 0 && notified == 0) {
if (notified == 0) {
syncStatus(handle, 1); // notify Go side
notified = 1;
}
Expand Down Expand Up @@ -153,7 +160,14 @@ unsigned long read_data(XSelectionEvent *sev, Atom sel, Atom prop, Atom target,
//
// The caller of this function should responsible for the free of the buf.
unsigned long clipboard_read(char* typ, char **buf) {
Display *d = XOpenDisplay(0);
Display* d = NULL;
for (int i = 0; i < 42; i++) {
d = XOpenDisplay(0);
if (d == NULL) {
continue;
}
break;
}
if (d == NULL) {
return -1;
}
Expand Down

0 comments on commit 87d06ab

Please sign in to comment.