Subject: bug#74423: Low level key events

From: Cecilio Pardo <[email-masked]>
Date: Sat, 23 Nov 2024 13:08:24 +0100

On 19/11/2024 0:49, Po Lu wrote:

> You are computing the modifier event to be generated incorrectly.  You
> should search the modifier map (dpyinfo->modmap) for columns containing
> received keysyms, and compare modifier bits derived from the rows where
> they appear against meta_mod_mask, shift_lock_mask, alt_mod_mask,
> super_mod_mask, and hyper_mod_mask in the display structure, or CtrlMask
> and ShiftMask.

Thanks for the guidance. Does this look ok?

Lisp_Object
x_get_modifier_for_keycode (struct x_display_info *dpyinfo,
			    int keycode)
{
#ifdef HAVE_XKB
   if (dpyinfo->xkb_desc)
     for (int mod = 0; mod < XkbNumModifiers; mod++)
       {
	int mask = (1 << mod);
	if (dpyinfo->xkb_desc->map->modmap[keycode] & mask)
	  {
	    if (mask == ShiftMask)
	      return Qshift;
	    if (mask == ControlMask)
	      return Qctrl;
	    if (mask == dpyinfo->meta_mod_mask)
	      return Qmeta;
	    if (mask == dpyinfo->alt_mod_mask)
	      return Qalt;
	    if (mask == dpyinfo->super_mod_mask)
	      return Qsuper;
	    if (mask == dpyinfo->hyper_mod_mask)
	      return Qhyper;
	  }
       }
#endif
   XModifierKeymap *map = dpyinfo->modmap;
   if (map)
     for (int mod = 0; mod < 8; mod++)
       {
	int mask = (1 << mod);
         for (int key = 0; key < map->max_keypermod; key++)
	  if (map->modifiermap[mod * map->max_keypermod + key] == keycode)
	    {
	      if (mask == ShiftMask)
		return Qshift;
	      if (mask == ControlMask)
		return Qctrl;
	      if (mask == dpyinfo->meta_mod_mask)
		return Qmeta;
	      if (mask == dpyinfo->alt_mod_mask)
		return Qalt;
	      if (mask == dpyinfo->super_mod_mask)
		return Qsuper;
	      if (mask == dpyinfo->hyper_mod_mask)
		return Qhyper;
	    }
       }
   return Qnil;
}