Subject: 29.0.60; Incoherent last_mouse_window (xterm.c) between XInput and XInput 2

From: Adrián Medraño Calvo <[email-masked]>
Date: Thu, 2 Feb 2023 23:28:01 +0100

For the implementation of `mouse-autoselect-window', the entered window
is remembered and then compared when an mouse motion event is received.
Up to Emacs 28, this value was stored in a static local variable named
`last_mouse_window' in the block handling MotionNotify within
`handle_one_xevent'.  With the introduction of XInput 2,
`handle_one_xevent' was adjusted to account for XI_Motion events, with
the oversight that a new local static `last_mouse_window' variable was
declared in the new handler block for XI_Motion.  As a result, when
Emacs receives motion events from XInput and XInput2 sources
alternatively the value of each `last_mouse_window’ differs, leading to
the window under the motion sometimes not being selected, as the
value `last_mouse_window' is out-of-date for the particular XInput
version.

This bug impacts the GNU ELPA package EXWM.  EXWM tries to extrapolate
applicable Emacs functionality to X window management; among others
assigns an Emacs buffer to each managed X window, and positions the X
window over the Emacs window in which the buffer is displayed.  When
`mouse-autoselect-windows’ and the user moves the mouse pointer over one
X window managed by EXWM, this X window receives the mouse events and not
Emacs, therefore the Emacs window is not selected.  In order to overcome
this, EXWM sends a synthetic MotionNotify event to Emacs.

The attached patch fixes the issue.

Thank you,
Adrián Medraño Calvo.

In GNU Emacs 29.0.60 (build 1, x86_64-apple-darwin22.2.0, cairo version
 1.17.6) of 2023-01-26 built on amcmac
Repository revision: f8c95d1a7681e861fc22d2a040cda0ddfe23eff4
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101006
System Description:  macOS 13.2

Configured using:
 'configure --prefix=/Volumes/sys/repos/emacs-cairo/rel
 --without-makeinfo --without-dbus --without-ns --with-x-toolkit=no
 --with-cairo
 PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/opt/freetype/lib/pkgconfig:/usr/local/opt/fontconfig/lib/pkgconfig:/usr/local/opt/ncurses/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig:/usr/local/Cellar/jpeg/9d/lib/pkgconfig:/usr/local/Cellar/libtiff/4.3.0/lib/pkgconfig:/usr/local/opt/harfbuzz/lib/pkgconfig/:/usr/local/opt/harfbuzz/lib/pkgconfig/
 'CFLAGS=-Og -g3' --without-jpeg --without-gif --without-xpm
 --without-tiff'

Configured features:
ACL CAIRO FREETYPE GLIB GMP GNUTLS GSETTINGS HARFBUZZ JSON LCMS2 LIBXML2
MODULES NOTIFY KQUEUE OLDXMENU PDUMPER PNG RSVG SQLITE3 THREADS WEBP X11
XDBE XIM XINPUT2 ZLIB

Important settings:
  value of $LC_ALL: en_US.UTF-8
  value of $LANG: en_US
  value of $XMODIFIERS: @im=exwm-xim
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date subr-x mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
cl-loaddefs cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils rmc iso-transl tooltip cconv eldoc paren electric
uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads kqueue lcms2 dynamic-setting
system-font-setting font-render-setting cairo xinput2 x multi-tty
make-network-process emacs)

Memory information:
((conses 16 38318 8147)
 (symbols 48 5108 0)
 (strings 32 12656 2131)
 (string-bytes 1 362604)
 (vectors 16 10252)
 (vector-slots 8 147251 15877)
 (floats 8 42 19)
 (intervals 56 263 0)
 (buffers 976 10))