Subject: Fix for timestamp related popup-menu problem with some X servers

From: Bernhard Herzog <[email-masked]>
Date: Mon, 22 Mar 2010 20:02:46 +0100

[ resent from
  http://lists.gnu.org/archive/html/emacs-devel/2010-03/msg01011.html ]

Hi,

I'm regularly running into a problem with the popup menus bound by
default to C-down-mouse-1 and other events.  The symptom is that instead
of a popup menu, I only get the error message: <C-mouse-1> is undefined.
This is exactly the same problem as described in
http://debbugs.gnu.org/cgi/bugreport.cgi?bug=4930. 

The root cause is as described in there in message #16:

   Emacs lisp ints are only 30 bits wide.  Timestamps come in from
   XButtonEvent and make_lispy_event calls make_lispy_position, which
   uses make_number to stash away the event timestamp.  Unfortunately
   this will truncate the top two bits of the timestamp.  This is
   subsequently passed to gtk_menu_popup via
   xmenu_show/create_and_show_popup_menu.  It ends up going to
   XGrab<xxx> which appears to be failing when given such a truncated
   timestamp.

So, whether this defect can be observed depends on the value of the
timestamp.  That timestamp is a 32 bit value giving the time since an
X-server dependent point in time in milliseconds.  This means it will
wrap around about every 50 days.

Different servers seem to choose different starting points for the
timestamp.  Some may reset the timer when the server is reset or
started, others, including one I regularly use at work (the one coming
with Debian etch), apparently base the timestamp on the system time,
which has the effect that for a quarter of the 50 days the timestamps
are small enough and the popup menus work, and the rest of the time,
they don't.

Anyway, to cut the story short, below is a patch to fix this problem.
It uses last_event_timestamp from keyboard.c instead of the timestamp
>From the button event.  AFAICT, last_event_timestamp is the timestamp
>From the button event, most of the time anyway, because usually the
button event that will lead to the popup-menu being displayed is the
last one that was processed.

Here's the patch:

=== modified file 'src/menu.c'
--- src/menu.c	2010-01-13 08:35:10 +0000
+++ src/menu.c	2010-03-21 15:57:51 +0000
@@ -61,6 +61,11 @@
 #define HAVE_BOXES 1
 #endif
 
+/* The timestamp of the last input event Emacs received from the X server.  */
+/* Defined in keyboard.c.  */
+extern unsigned long last_event_timestamp;
+
+
 extern Lisp_Object QCtoggle, QCradio;
 
 Lisp_Object menu_items;
@@ -1320,7 +1325,7 @@
 #else /* MSDOS and X11 */
   selection = xmenu_show (f, xpos, ypos, for_click,
 			  keymaps, title, &error_name,
-			  INTEGERP (timestamp) ? XUINT (timestamp) : 0);
+			  last_event_timestamp);
 #endif
 
   UNBLOCK_INPUT;

Regards

   Bernhard