From 1808d26aa74e02808e316ddede33986f49f59328 Mon Sep 17 00:00:00 2001
From: nehalmistry <>
Date: Tue, 25 Feb 2003 23:28:51 +0000
Subject: [PATCH] use proper way to get selection from X11 (Pierre-Paul Lavoie)

---
 src/ui/menu_proc.cpp | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/ui/menu_proc.cpp b/src/ui/menu_proc.cpp
index 3998426f7..1e33652b1 100644
--- a/src/ui/menu_proc.cpp
+++ b/src/ui/menu_proc.cpp
@@ -1648,6 +1648,7 @@ local void MenuHandleButtonDown(unsigned b __attribute__((unused)))
     unsigned long nitem;
     unsigned long dummy;
     int retform;
+    XEvent event;
 #endif
 #endif
 
@@ -1746,11 +1747,6 @@ local void MenuHandleButtonDown(unsigned b __attribute__((unused)))
 			    break;
 			}
 
-			if (XGetSelectionOwner(display, XA_PRIMARY) == None) {
-			    XCloseDisplay(display);
-			    break;
-			}
-
 			// Creates a non maped temporary X window to hold the selection
 			if (!(window = XCreateSimpleWindow(display, 
 				DefaultRootWindow(display), 0, 0, 1, 1, 0, 0, 0))) {
@@ -1761,19 +1757,27 @@ local void MenuHandleButtonDown(unsigned b __attribute__((unused)))
 			XConvertSelection(display, XA_PRIMARY, XA_STRING, XA_STRING,
 			    window, CurrentTime);
 
-			XFlush(display);
+			XNextEvent(display, &event);
 
-			// FIXME: loops 4 times or until we get selection, need to use proper way
-			clipboard = NULL;
-			for ( i = 0; i < 5 && !clipboard; ++i) {
-			    XGetWindowProperty(display, window, XA_STRING, 0, 1024, False, 
-				AnyPropertyType, &rettype, &retform, &nitem, 
-				&dummy, (unsigned char **)&clipboard);
+			if (event.type != SelectionNotify || 
+				event.xselection.property != XA_STRING) {
+			    break;
 			}
 
+			XGetWindowProperty(display, window, XA_STRING, 0, 1024, False, 
+			    XA_STRING, &rettype, &retform, &nitem, &dummy, 
+			    (unsigned char **)&clipboard);
+
 			XDestroyWindow(display, window);
 			XCloseDisplay(display);
 
+			if (rettype != XA_STRING || retform != 8) {
+			    if (clipboard != NULL) {
+				XFree(clipboard);
+			    }
+			    clipboard = NULL;
+			}
+
 			if (clipboard == NULL) {
 			    break;
 			}