Skip to content

Commit 87d06ab

Browse files
committed
linux: retry if XOpenDisplay fails
1 parent 155b185 commit 87d06ab

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

clipboard_linux.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ int clipboard_test() {
2929
// if start is provided, the value of start will be changed to 1 to indicate
3030
// if the write is availiable for reading.
3131
int clipboard_write(char *typ, unsigned char *buf, size_t n, uintptr_t handle) {
32-
Display* d = XOpenDisplay(0);
32+
Display* d = NULL;
33+
for (int i = 0; i < 42; i++) {
34+
d = XOpenDisplay(0);
35+
if (d == NULL) {
36+
continue;
37+
}
38+
break;
39+
}
3340
if (d == NULL) {
34-
if (handle != 0) syncStatus(handle, -1);
41+
syncStatus(handle, -1);
3542
return -1;
3643
}
3744

@@ -47,22 +54,22 @@ int clipboard_write(char *typ, unsigned char *buf, size_t n, uintptr_t handle) {
4754
Atom target = XInternAtom(d, typ, True);
4855
if (target == None) {
4956
XCloseDisplay(d);
50-
if (handle != 0) syncStatus(handle, -2);
57+
syncStatus(handle, -2);
5158
return -2;
5259
}
5360

5461
XSetSelectionOwner(d, sel, w, CurrentTime);
5562
if (XGetSelectionOwner(d, sel) != w) {
5663
XCloseDisplay(d);
57-
if (handle != 0) syncStatus(handle, -3);
64+
syncStatus(handle, -3);
5865
return -3;
5966
}
6067

6168
XEvent event;
6269
XSelectionRequestEvent* xsr;
6370
int notified = 0;
6471
for (;;) {
65-
if (handle != 0 && notified == 0) {
72+
if (notified == 0) {
6673
syncStatus(handle, 1); // notify Go side
6774
notified = 1;
6875
}
@@ -153,7 +160,14 @@ unsigned long read_data(XSelectionEvent *sev, Atom sel, Atom prop, Atom target,
153160
//
154161
// The caller of this function should responsible for the free of the buf.
155162
unsigned long clipboard_read(char* typ, char **buf) {
156-
Display *d = XOpenDisplay(0);
163+
Display* d = NULL;
164+
for (int i = 0; i < 42; i++) {
165+
d = XOpenDisplay(0);
166+
if (d == NULL) {
167+
continue;
168+
}
169+
break;
170+
}
157171
if (d == NULL) {
158172
return -1;
159173
}

0 commit comments

Comments
 (0)