[PATCH 8/9] Prevent key mapping breaking non-recoverably

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


PC key mapping with modifiers breaks down if mapping is detected, but
key up is never handled.  This can happen at least in two cases:

* PC window management catches the modifier, so that modifier up event
  never gets delivered, e.g. KMOD_LALT + some key on Gnome desktop.
  This can be fixed by user just by pressing the same modifier without
  any other key

* Key mapping file maps same PC key (scancode) multiple times, with
  different modifiers, and user presses that key with both modifiers
  being kept down.  This is nor recoverable without emulation reboot
  (which re-parses key mapping file)

This commit rejects latter specifications from the keymap file.

This issue happens because ST key is stored to KeysDown[], indexed
only by SDL scancode & taken from there in Keymap_KeyUp(), and because
ST modifier support required up & down status for ST keys to be
counted (not just true / false).
---
 src/keymap.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/keymap.c b/src/keymap.c
index 1e177653..5276be1b 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -567,6 +567,7 @@ static bool HostSpecToKeymap(const char *spec, KeyMapping* mapping)
 	char buf[64], *token, *saveptr, *endptr;
 	SDL_Scancode scancode = 0;
 	SDL_Keymod mods = 0;
+	int i;
 
 	if (!spec)
 		return false;
@@ -607,6 +608,27 @@ static bool HostSpecToKeymap(const char *spec, KeyMapping* mapping)
 			return false;
 		}
 	}
+
+	/* Because KeysDown[] is indexed by PC scancode, KeysDown[]
+	 * is used in Keymap_KeyUp() instead of Keymap_RemapKeyToSTKey(),
+	 * and ST modifier support requires KeyStates to be tracked by
+	 * count, multiple modifier mappings for the same scancode can
+	 * break mapping handling non-recoverably in some corner-cases.
+	 *
+	 * This prevents such mappings.
+	 */
+	for (i = 0; i < ARRAY_SIZE(LoadedKeymap); i++)
+	{
+		SDL_Scancode check = LoadedKeymap[i].pc.scancode;
+		if (!check)
+			break; /* end */
+
+		if (scancode == check && mods && LoadedKeymap[i].pc.mods)
+		{
+			Log_Printf(LOG_ERROR, "PC/SDL scancode %d specified already with modifier(s)\n", scancode);
+			return false;
+		}
+	}
 	mapping->pc.scancode = scancode;
 	mapping->pc.mods = mods;
 	return (scancode > 0);
-- 
2.30.2


--------------7873925CF10FCA11A2667F70
Content-Type: text/x-patch; charset=UTF-8;
 name="0007-Support-for-specifying-PC-SDL-key-modifiers-in-the-m.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0007-Support-for-specifying-PC-SDL-key-modifiers-in-the-m.pa";
 filename*1="tch"



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/