@@ -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.
3131int 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.
155162unsigned 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