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