@@ -29,9 +29,16 @@ int clipboard_test() {
29
29
// if start is provided, the value of start will be changed to 1 to indicate
30
30
// if the write is availiable for reading.
31
31
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
+ }
33
40
if (d == NULL ) {
34
- if ( handle != 0 ) syncStatus (handle , -1 );
41
+ syncStatus (handle , -1 );
35
42
return -1 ;
36
43
}
37
44
@@ -47,22 +54,22 @@ int clipboard_write(char *typ, unsigned char *buf, size_t n, uintptr_t handle) {
47
54
Atom target = XInternAtom (d , typ , True );
48
55
if (target == None ) {
49
56
XCloseDisplay (d );
50
- if ( handle != 0 ) syncStatus (handle , -2 );
57
+ syncStatus (handle , -2 );
51
58
return -2 ;
52
59
}
53
60
54
61
XSetSelectionOwner (d , sel , w , CurrentTime );
55
62
if (XGetSelectionOwner (d , sel ) != w ) {
56
63
XCloseDisplay (d );
57
- if ( handle != 0 ) syncStatus (handle , -3 );
64
+ syncStatus (handle , -3 );
58
65
return -3 ;
59
66
}
60
67
61
68
XEvent event ;
62
69
XSelectionRequestEvent * xsr ;
63
70
int notified = 0 ;
64
71
for (;;) {
65
- if (handle != 0 && notified == 0 ) {
72
+ if (notified == 0 ) {
66
73
syncStatus (handle , 1 ); // notify Go side
67
74
notified = 1 ;
68
75
}
@@ -153,7 +160,14 @@ unsigned long read_data(XSelectionEvent *sev, Atom sel, Atom prop, Atom target,
153
160
//
154
161
// The caller of this function should responsible for the free of the buf.
155
162
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
+ }
157
171
if (d == NULL ) {
158
172
return -1 ;
159
173
}
0 commit comments