Karsten Hopp ac5438
To: vim-dev@vim.org
Karsten Hopp ac5438
Subject: Patch 7.2.013
Karsten Hopp ac5438
Fcc: outbox
Karsten Hopp ac5438
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp ac5438
Mime-Version: 1.0
Karsten Hopp ac5438
Content-Type: text/plain; charset=ISO-8859-1
Karsten Hopp ac5438
Content-Transfer-Encoding: 8bit
Karsten Hopp ac5438
------------
Karsten Hopp ac5438
Karsten Hopp ac5438
Patch 7.2.013
Karsten Hopp ac5438
Problem:    While waiting for the X selection Vim consumes a lot of CPU time
Karsten Hopp ac5438
	    and hangs until a response is received.
Karsten Hopp ac5438
Solution:   Sleep a bit when the selection event hasn't been received yet.
Karsten Hopp ac5438
	    Time out after a couple of seconds to avoid a hang when the
Karsten Hopp ac5438
	    selection owner isn't responding.
Karsten Hopp ac5438
Files:	    src/ui.c
Karsten Hopp ac5438
Karsten Hopp ac5438
Karsten Hopp ac5438
*** ../vim-7.2.012/src/ui.c	Mon Jul 14 21:47:49 2008
Karsten Hopp ac5438
--- src/ui.c	Sun Sep  7 16:54:35 2008
Karsten Hopp ac5438
***************
Karsten Hopp ac5438
*** 2110,2115 ****
Karsten Hopp ac5438
--- 2110,2117 ----
Karsten Hopp ac5438
      int		i;
Karsten Hopp ac5438
      int		nbytes = 0;
Karsten Hopp ac5438
      char_u	*buffer;
Karsten Hopp ac5438
+     time_t	start_time;
Karsten Hopp ac5438
+     int		timed_out = FALSE;
Karsten Hopp ac5438
  
Karsten Hopp ac5438
      for (i =
Karsten Hopp ac5438
  #ifdef FEAT_MBYTE
Karsten Hopp ac5438
***************
Karsten Hopp ac5438
*** 2129,2134 ****
Karsten Hopp ac5438
--- 2131,2137 ----
Karsten Hopp ac5438
  	    case 3:  type = text_atom;		break;
Karsten Hopp ac5438
  	    default: type = XA_STRING;
Karsten Hopp ac5438
  	}
Karsten Hopp ac5438
+ 	success = FALSE;
Karsten Hopp ac5438
  	XtGetSelectionValue(myShell, cbd->sel_atom, type,
Karsten Hopp ac5438
  	    clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
Karsten Hopp ac5438
  
Karsten Hopp ac5438
***************
Karsten Hopp ac5438
*** 2141,2167 ****
Karsten Hopp ac5438
  	 * characters, then they will appear before the one that requested the
Karsten Hopp ac5438
  	 * paste!  Don't worry, we will catch up with any other events later.
Karsten Hopp ac5438
  	 */
Karsten Hopp ac5438
  	for (;;)
Karsten Hopp ac5438
  	{
Karsten Hopp ac5438
  	    if (XCheckTypedEvent(dpy, SelectionNotify, &event))
Karsten Hopp ac5438
  		break;
Karsten Hopp ac5438
  	    if (XCheckTypedEvent(dpy, SelectionRequest, &event))
Karsten Hopp ac5438
  		/* We may get a SelectionRequest here and if we don't handle
Karsten Hopp ac5438
  		 * it we hang.  KDE klipper does this, for example. */
Karsten Hopp ac5438
  		XtDispatchEvent(&event);
Karsten Hopp ac5438
  
Karsten Hopp ac5438
  	    /* Do we need this?  Probably not. */
Karsten Hopp ac5438
  	    XSync(dpy, False);
Karsten Hopp ac5438
  
Karsten Hopp ac5438
! 	    /* Bernhard Walle solved a slow paste response in an X terminal by
Karsten Hopp ac5438
! 	     * adding: usleep(10000); here. */
Karsten Hopp ac5438
  	}
Karsten Hopp ac5438
  
Karsten Hopp ac5438
- 	/* this is where clip_x11_request_selection_cb() is actually called */
Karsten Hopp ac5438
- 	XtDispatchEvent(&event);
Karsten Hopp ac5438
- 
Karsten Hopp ac5438
  	if (success)
Karsten Hopp ac5438
  	    return;
Karsten Hopp ac5438
      }
Karsten Hopp ac5438
  
Karsten Hopp ac5438
      /* Final fallback position - use the X CUT_BUFFER0 store */
Karsten Hopp ac5438
--- 2144,2189 ----
Karsten Hopp ac5438
  	 * characters, then they will appear before the one that requested the
Karsten Hopp ac5438
  	 * paste!  Don't worry, we will catch up with any other events later.
Karsten Hopp ac5438
  	 */
Karsten Hopp ac5438
+ 	start_time = time(NULL);
Karsten Hopp ac5438
  	for (;;)
Karsten Hopp ac5438
  	{
Karsten Hopp ac5438
  	    if (XCheckTypedEvent(dpy, SelectionNotify, &event))
Karsten Hopp ac5438
+ 	    {
Karsten Hopp ac5438
+ 		/* this is where clip_x11_request_selection_cb() is actually
Karsten Hopp ac5438
+ 		 * called */
Karsten Hopp ac5438
+ 		XtDispatchEvent(&event);
Karsten Hopp ac5438
  		break;
Karsten Hopp ac5438
+ 	    }
Karsten Hopp ac5438
  	    if (XCheckTypedEvent(dpy, SelectionRequest, &event))
Karsten Hopp ac5438
  		/* We may get a SelectionRequest here and if we don't handle
Karsten Hopp ac5438
  		 * it we hang.  KDE klipper does this, for example. */
Karsten Hopp ac5438
  		XtDispatchEvent(&event);
Karsten Hopp ac5438
  
Karsten Hopp ac5438
+ 	    /* Time out after 2 to 3 seconds to avoid that we hang when the
Karsten Hopp ac5438
+ 	     * other process doesn't respond.  Note that the SelectionNotify
Karsten Hopp ac5438
+ 	     * event may still come later when the selection owner comes back
Karsten Hopp ac5438
+ 	     * to life and the text gets inserted unexpectedly (by xterm).
Karsten Hopp ac5438
+ 	     * Don't know how to avoid that :-(. */
Karsten Hopp ac5438
+ 	    if (time(NULL) > start_time + 2)
Karsten Hopp ac5438
+ 	    {
Karsten Hopp ac5438
+ 		timed_out = TRUE;
Karsten Hopp ac5438
+ 		break;
Karsten Hopp ac5438
+ 	    }
Karsten Hopp ac5438
+ 
Karsten Hopp ac5438
  	    /* Do we need this?  Probably not. */
Karsten Hopp ac5438
  	    XSync(dpy, False);
Karsten Hopp ac5438
  
Karsten Hopp ac5438
! 	    /* Wait for 1 msec to avoid that we eat up all CPU time. */
Karsten Hopp ac5438
! 	    ui_delay(1L, TRUE);
Karsten Hopp ac5438
  	}
Karsten Hopp ac5438
  
Karsten Hopp ac5438
  	if (success)
Karsten Hopp ac5438
  	    return;
Karsten Hopp ac5438
+ 
Karsten Hopp ac5438
+ 	/* don't do a retry with another type after timing out, otherwise we
Karsten Hopp ac5438
+ 	 * hang for 15 seconds. */
Karsten Hopp ac5438
+ 	if (timed_out)
Karsten Hopp ac5438
+ 	    break;
Karsten Hopp ac5438
      }
Karsten Hopp ac5438
  
Karsten Hopp ac5438
      /* Final fallback position - use the X CUT_BUFFER0 store */
Karsten Hopp ac5438
*** ../vim-7.2.012/src/version.c	Sun Sep  7 15:49:45 2008
Karsten Hopp ac5438
--- src/version.c	Sun Sep  7 21:45:55 2008
Karsten Hopp ac5438
***************
Karsten Hopp ac5438
*** 678,679 ****
Karsten Hopp ac5438
--- 678,681 ----
Karsten Hopp ac5438
  {   /* Add new patch number below this line */
Karsten Hopp ac5438
+ /**/
Karsten Hopp ac5438
+     13,
Karsten Hopp ac5438
  /**/
Karsten Hopp ac5438
Karsten Hopp ac5438
-- 
Karsten Hopp ac5438
The users that I support would double-click on a landmine to find out
Karsten Hopp ac5438
what happens.				-- A system administrator
Karsten Hopp ac5438
Karsten Hopp ac5438
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp ac5438
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp ac5438
\\\        download, build and distribute -- http://www.A-A-P.org        ///
Karsten Hopp ac5438
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///