Re: [hatari-devel] Suggestion for enhanced keymap table format

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


Hi !


What's the deal with SDL v1 vs SDL v2 ? Any reason why SDL1 support should stay ?

Sorry I had actually started making some changes to have a bit more consistency in the code style and I feel it would be to revert that now.

I'm sending your this work in progress so you can have an idea.

You can drop the attached keymap.c in src . I attached a keymap for the French keyboard.

Essentially the keymap is now an KeyMapping array.

You can define the SDL scancode and one modifier, and the list of scancodes that this should produce. So we can send ALT or SHIFT-ALT for example, in addition to the key to press.

I added an optional mask so the mods of the PC key pressed can be masked out, so to make the rules more generic, but I'm not sure if that's really useful.

The attached code (kind of) works well for my French keyboard (at least I can produce all characters except § which I never use), but there are some problems as I'm probably confusing the TOS when sending a SHIFT/ALT scancode when that same key was already pressed. Also problem when we have a shifted key on the PC but unshifted on the Atari (§). I'll have to better keep track of what modifiers are already pressed so I don't "press" them again, and don't release them.


Vinz


On 31/10/2020 00:22, Eero Tamminen wrote:
Hi,

Thomas is probably best person to comment on this,
but until we've removed support for SDL v1, these
kind of changes should support both SDL v1 and v2.

Regardless of that, could you send your PoC changes as patch to list?

(Just the relevant changes, no extra white space
changes etc.)


Keymap file format change isn't necessarily much
of a problem, if you could provide also (shell or
python) script that can convert between the
formats.

    - Eero

On 10/31/20 12:28 AM, Vincent Barrilliot wrote:
Dear Hatari developers,

I am new to Hatari development. I am French, 43 and I also follow EmuTOS and FreeMiNT development although I listen and learn at the moment.

You have been doing a fantastic job with Hatari, thank you for such a great piece of software !

As a French keyboard user, I am suffering from the inconvenience of the "alt-shift" keys which are not well supported currently, and I have decided to do something about it.

I have made a proof of concept where I modified keymap.c with the following improvement:

The keymap is now an array of KeyMappings structs which have the SDL_keysym on one hand, and a list of ST scancodes on the other hand.

By pressing AltGr-' on my French PC keyboard, Hatari sends scancodes of shift-alt-# to its "ikbd", and an opening curly brace is correctly output in e.g. STeno or EmuCON.

So this works and should enable to bind any key+modifiers from the PC to any sequence of scancodes on the ST side, solving that old problem for good.

Now I need to be able to make that configurable and enhance the format of the keymap configuration file to support this and I would your advice for this.

Obviously I could make something that works with scancodes numbers only, but it would break the current "symbolic" mapping which although not perfect, is easy to use and understand.

I've had the following ideas:

1 Use some "section" delimiter in the config file so to indicate to the parser how to interpret following the lines.

SYMBOLIC

Y,44

Z,21

SCANCODE

21|200,2a|38|1a

(we could also allow usage of modifier names or ST modifier names to make things more friendly, ie 21|KMOD_RALT,LSHIFT|ALTERNATE|1a)

2 Introduce some prefix to indicate to the parser what is we're describing. Eg s for symbolic, r for raw (scancode).

sY,44

r21|KMOD_RALT,LSHIFT|ALTERNATE|r1a

But that would break backward compatibility with exiting keymaps...


None of that sounds great... By default I'd go with the scancode because it's the most robust. There is only a handful of keys to "override" anyway so we wouldn't compete with /etc/termcap's complexity. We can also ship a few standard keymaps. But then what do we do with existing user keymaps...?

Thanks for any idea!


Vincent B






# Keymap. This format allows you to associate SDL key descriptions with
# scancodes simulating the corresponding keys pressed on the emulated ST.
#
# Format is the following:
# <host_description>,<guest_description>
#
# <host_description> is the scancode the key pressed (which you can see
# when running hatari with the "--trace keymap" option. Optionally you can
# add a SDL key modifier. These are described in
# https://wiki.libsdl.org/SDL_Keymod#Related_Functions.
# You can also add a modifier mask. This mask will be bitwise "AND"ed to the
# actual key pressed before it is compared with items from this keymap.
# This allows you make mappings work regardless of the status of other
# modifiers (e.g. NumLock).
# All these (scancode, modifiers, mask) must appear in this order and be
# separated by a pipe |.
#
# <guest_description> is the pipe-separated ordered list of scancodes to send
# to the ST (3 max). For convenience you can use LSHIFT, RSHIFT and
# ALTERNATE in lieu of 2a,36,38 respectively.
# You can find scancodes in many Atari documentation places, e.g.
# https://tho-otto.de/keyboards/
# or https://freemint.github.io/tos.hyp/en/scancode.html
#
# Notes:
# * All scan codes must be in hexadecimal.
# * The parser is not very smart so don't add spaces etc.

04,1e
# ,
10,32
# ?
10|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|33
10|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|33
# a
14,10
# z
1a,11
# w
1d,2c
# ~
1f|KMOD_RALT|KMOD_RALT,LSHIFT|ALTERNATE|2b
# #
20|KMOD_RALT|KMOD_RALT,2b
# {
21|KMOD_RALT|KMOD_RALT,2a|ALTERNATE|1a
# [
22|KMOD_RALT|KMOD_RALT,ALTERNATE|1a
# |
23|KMOD_RALT|KMOD_RALT,LSHIFT|2b
# 6
23|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|07
23|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|07
# -
23,0d
# 7
24|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|08
24|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|08
# `
24|KMOD_RALT|KMOD_RALT,29
# 8
25|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|09
25|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|09
# \
25|KMOD_RALT|KMOD_RALT,ALTERNATE|28
# _
25,LSHIFT|0d
# ^
26|KMOD_RALT|KMOD_RALT,1a
# @
27|KMOD_RALT|KMOD_RALT,ALTERNATE|2b
# °
2d|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|0c
2d|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|0c
# ]
2d|KMOD_RALT|KMOD_RALT,ALTERNATE|1b
# + (près de efface)
2e|KMOD_SHIFT|KMOD_SHIFT,RSHIFT|35
# }
2e|KMOD_RALT|KMOD_RALT,LSHIFT|ALTERNATE|1b
# =
2e,35
# ¨
2f|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|1a
2f|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|1a
# ^
2f,1a
# £
30|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|1b
30|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|1b
# $
30,1b
# * (pas pavé numérique)
31,RSHIFT|1b
# m
33,27
# %
34|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|28
34|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|28
# ù
34,28
# ;
36,33
# . (pas pavé numérique)
36|KMOD_LSHIFT|KMOD_LSHIFT,LSHIFT|33
36|KMOD_RSHIFT|KMOD_RSHIFT,RSHIFT|33
# §
ALTERNATE|KMOD_LSHIFT|KMOD_LSHIFT,07
ALTERNATE|KMOD_RSHIFT|KMOD_RSHIFT,07
/*
  Hatari - keymap.c

  This file is distributed under the GNU General Public License, version 2
  or at your option any later version. Read the file gpl.txt for details.

  Here we process a key press and the remapping of the scancodes.
*/
const char Keymap_fileid[] = "Hatari keymap.c : " __DATE__ " " __TIME__;

#include <ctype.h>
#include "main.h"
#include "keymap.h"
#include "configuration.h"
#include "file.h"
#include "ikbd.h"
#include "joy.h"
#include "shortcut.h"
#include "str.h"
#include "screen.h"
#include "debugui.h"
#include "log.h"


#if !WITH_SDL2
/* This table is used to translate a symbolic keycode to the (SDL) scancode */
static Uint8 SdlSymToSdlScan[SDLK_LAST];
#endif


/* Key mappings: pair a SDL definition with a list of keys we have to press on the ST */
#define MAX_ST_SCANCODES 3 /* Max is alt-shift-something, so 3 chars */
struct KeyMapping
{
	SDL_keysym SdlKeysym;
	SDL_Keymod modmask;
	uint8_t	   STScanCodesLength;
	uint8_t    STScanCodes[MAX_ST_SCANCODES]; /* Starts with the count of scancodes, followed by scancodes. */
};
static struct KeyMapping LoadedKeyMap[KBD_MAX_SCANCODE];

/* List of ST scan codes to NOT de-bounce when running in maximum speed */
static const char DebounceExtendedKeys[] =
{
	0x1d,  /* CTRL */
	0x2a,  /* Left SHIFT */
	0x01,  /* ESC */
	0x38,  /* ALT */
	0x36,  /* Right SHIFT */
	0      /* term */
};

/* Helper functions for parsing the keymap file */
static int HostSpecToSDLKeysym(char *spec, struct KeyMapping* result);
static int GuestSpecToSTScanCodes(char *spec, struct KeyMapping *result);
static SDL_Keymod SDLKeymodFromName(const char *name);

/*-----------------------------------------------------------------------*/
/**
 * Initialization.
 */
void Keymap_Init(void)
{
#if !WITH_SDL2
	memset(SdlSymToSdlScan, 0, sizeof(SdlSymToSdlScan));      /* Clear array */
#endif
	Keymap_LoadRemapFile(ConfigureParams.Keyboard.szMappingFileName);
}

/**
 * Map SDL symbolic key to ST scan code
 */
static uint8_t Keymap_SymbolicToStScanCode(SDL_keysym* pKeySym)
{
	char code;

	switch (pKeySym->sym)
	{
	 case SDLK_BACKSPACE: code = 0x0E; break;
	 case SDLK_TAB: code = 0x0F; break;
	 case SDLK_CLEAR: code = 0x47; break;
	 case SDLK_RETURN: code = 0x1C; break;
	 case SDLK_ESCAPE: code = 0x01; break;
	 case SDLK_SPACE: code = 0x39; break;
	 case SDLK_EXCLAIM: code = 0x09; break;     /* on azerty? */
	 case SDLK_QUOTEDBL: code = 0x04; break;    /* on azerty? */
	 case SDLK_HASH: code = 0x29; break;
	 case SDLK_DOLLAR: code = 0x1b; break;      /* on azerty */
	 case SDLK_AMPERSAND: code = 0x02; break;   /* on azerty? */
	 case SDLK_QUOTE: code = 0x28; break;
	 case SDLK_LEFTPAREN: code = 0x63; break;
	 case SDLK_RIGHTPAREN: code = 0x64; break;
	 case SDLK_ASTERISK: code = 0x66; break;
	 case SDLK_PLUS: code = 0x1B; break;
	 case SDLK_COMMA: code = 0x33; break;
	 case SDLK_MINUS: code = 0x35; break;
	 case SDLK_PERIOD: code = 0x34; break;
	 case SDLK_SLASH: code = 0x35; break;
	 case SDLK_0: code = 0x0B; break;
	 case SDLK_1: code = 0x02; break;
	 case SDLK_2: code = 0x03; break;
	 case SDLK_3: code = 0x04; break;
	 case SDLK_4: code = 0x05; break;
	 case SDLK_5: code = 0x06; break;
	 case SDLK_6: code = 0x07; break;
	 case SDLK_7: code = 0x08; break;
	 case SDLK_8: code = 0x09; break;
	 case SDLK_9: code = 0x0A; break;
	 case SDLK_COLON: code = 0x34; break;
	 case SDLK_SEMICOLON: code = 0x27; break;
	 case SDLK_LESS: code = 0x60; break;
	 case SDLK_EQUALS: code = 0x0D; break;
	 case SDLK_GREATER : code = 0x34; break;
	 case SDLK_QUESTION: code = 0x35; break;
	 case SDLK_AT: code = 0x28; break;
	 case SDLK_LEFTBRACKET: code = 0x63; break;
	 case SDLK_BACKSLASH: code = 0x2B; break;     /* Might be 0x60 for UK keyboards */
	 case SDLK_RIGHTBRACKET: code = 0x64; break;
	 case SDLK_CARET: code = 0x2B; break;
	 case SDLK_UNDERSCORE: code = 0x0C; break;
	 case SDLK_BACKQUOTE: code = 0x52; break;
	 case SDLK_a: code = 0x1E; break;
	 case SDLK_b: code = 0x30; break;
	 case SDLK_c: code = 0x2E; break;
	 case SDLK_d: code = 0x20; break;
	 case SDLK_e: code = 0x12; break;
	 case SDLK_f: code = 0x21; break;
	 case SDLK_g: code = 0x22; break;
	 case SDLK_h: code = 0x23; break;
	 case SDLK_i: code = 0x17; break;
	 case SDLK_j: code = 0x24; break;
	 case SDLK_k: code = 0x25; break;
	 case SDLK_l: code = 0x26; break;
	 case SDLK_m: code = 0x32; break;
	 case SDLK_n: code = 0x31; break;
	 case SDLK_o: code = 0x18; break;
	 case SDLK_p: code = 0x19; break;
	 case SDLK_q: code = 0x10; break;
	 case SDLK_r: code = 0x13; break;
	 case SDLK_s: code = 0x1F; break;
	 case SDLK_t: code = 0x14; break;
	 case SDLK_u: code = 0x16; break;
	 case SDLK_v: code = 0x2F; break;
	 case SDLK_w: code = 0x11; break;
	 case SDLK_x: code = 0x2D; break;
	 case SDLK_y: code = 0x15; break;
	 case SDLK_z: code = 0x2C; break;
	 case SDLK_DELETE: code = 0x53; break;
	 /* End of ASCII mapped keysyms */
#if WITH_SDL2
	 case 180: code = 0x0D; break;
	 case 223: code = 0x0C; break;
	 case 228: code = 0x28; break;
	 case 246: code = 0x27; break;
	 case 252: code = 0x1A; break;
#else /* !WITH_SDL2 */
	 case SDLK_WORLD_0: code = 0x0d; break;
	 case SDLK_WORLD_1: code = 0x0c; break;
	 case SDLK_WORLD_2: code = 0x1a; break;
	 case SDLK_WORLD_3: code = 0x28; break;
	 case SDLK_WORLD_4: code = 0x27; break;
	 case SDLK_WORLD_20: code = 0x0D; break;
	 case SDLK_WORLD_63: code = 0x0C; break;
	 case SDLK_WORLD_68: code = 0x28; break;
	 case SDLK_WORLD_86: code = 0x27; break;
	 case SDLK_WORLD_92: code = 0x1A; break;
#endif /* !WITH_SDL2 */
	 /* Numeric keypad: */
	 case SDLK_KP0: code = 0x70; break;
	 case SDLK_KP1: code = 0x6D; break;
	 case SDLK_KP2: code = 0x6E; break;
	 case SDLK_KP3: code = 0x6F; break;
	 case SDLK_KP4: code = 0x6A; break;
	 case SDLK_KP5: code = 0x6B; break;
	 case SDLK_KP6: code = 0x6C; break;
	 case SDLK_KP7: code = 0x67; break;
	 case SDLK_KP8: code = 0x68; break;
	 case SDLK_KP9: code = 0x69; break;
	 case SDLK_KP_PERIOD: code = 0x71; break;
	 case SDLK_KP_DIVIDE: code = 0x65; break;
	 case SDLK_KP_MULTIPLY: code = 0x66; break;
	 case SDLK_KP_MINUS: code = 0x4A; break;
	 case SDLK_KP_PLUS: code = 0x4E; break;
	 case SDLK_KP_ENTER: code = 0x72; break;
	 case SDLK_KP_EQUALS: code = 0x61; break;
	 /* Arrows + Home/End pad */
	 case SDLK_UP: code = 0x48; break;
	 case SDLK_DOWN: code = 0x50; break;
	 case SDLK_RIGHT: code = 0x4D; break;
	 case SDLK_LEFT: code = 0x4B; break;
	 case SDLK_INSERT: code = 0x52; break;
	 case SDLK_HOME: code = 0x47; break;
	 case SDLK_END: code = 0x61; break;
	 case SDLK_PAGEUP: code = 0x63; break;
	 case SDLK_PAGEDOWN: code = 0x64; break;
	 /* Function keys */
	 case SDLK_F1: code = 0x3B; break;
	 case SDLK_F2: code = 0x3C; break;
	 case SDLK_F3: code = 0x3D; break;
	 case SDLK_F4: code = 0x3E; break;
	 case SDLK_F5: code = 0x3F; break;
	 case SDLK_F6: code = 0x40; break;
	 case SDLK_F7: code = 0x41; break;
	 case SDLK_F8: code = 0x42; break;
	 case SDLK_F9: code = 0x43; break;
	 case SDLK_F10: code = 0x44; break;
	 case SDLK_F11: code = 0x62; break;
	 case SDLK_F12: code = 0x61; break;
	 case SDLK_F13: code = 0x62; break;
	 /* Key state modifier keys */
	 case SDLK_CAPSLOCK: code = 0x3A; break;
	 case SDLK_SCROLLOCK: code = 0x61; break;
	 case SDLK_RSHIFT: code = 0x36; break;
	 case SDLK_LSHIFT: code = 0x2A; break;
	 case SDLK_RCTRL: code = 0x1D; break;
	 case SDLK_LCTRL: code = 0x1D; break;
	 case SDLK_RALT: code = 0x38; break;
	 case SDLK_LALT: code = 0x38; break;
	 /* Miscellaneous function keys */
	 case SDLK_HELP: code = 0x62; break;
	 case SDLK_PRINT: code = 0x62; break;
	 case SDLK_UNDO: code = 0x61; break;
	 default: code = -1;
	}

	return code;
}


#if WITH_SDL2

/**
 * Remap SDL scancode key to ST Scan code - this is the version for SDL2
 */
static uint8_t Keymap_PcToStScanCode(SDL_keysym* pKeySym)
{
	switch (pKeySym->scancode)
	{
	 case SDL_SCANCODE_A: return 0x1e;
	 case SDL_SCANCODE_B: return 0x30;
	 case SDL_SCANCODE_C: return 0x2e;
	 case SDL_SCANCODE_D: return 0x20;
	 case SDL_SCANCODE_E: return 0x12;
	 case SDL_SCANCODE_F: return 0x21;
	 case SDL_SCANCODE_G: return 0x22;
	 case SDL_SCANCODE_H: return 0x23;
	 case SDL_SCANCODE_I: return 0x17;
	 case SDL_SCANCODE_J: return 0x24;
	 case SDL_SCANCODE_K: return 0x25;
	 case SDL_SCANCODE_L: return 0x26;
	 case SDL_SCANCODE_M: return 0x32;
	 case SDL_SCANCODE_N: return 0x31;
	 case SDL_SCANCODE_O: return 0x18;
	 case SDL_SCANCODE_P: return 0x19;
	 case SDL_SCANCODE_Q: return 0x10;
	 case SDL_SCANCODE_R: return 0x13;
	 case SDL_SCANCODE_S: return 0x1f;
	 case SDL_SCANCODE_T: return 0x14;
	 case SDL_SCANCODE_U: return 0x16;
	 case SDL_SCANCODE_V: return 0x2f;
	 case SDL_SCANCODE_W: return 0x11;
	 case SDL_SCANCODE_X: return 0x2d;
	 case SDL_SCANCODE_Y: return 0x15;
	 case SDL_SCANCODE_Z: return 0x2c;
	 case SDL_SCANCODE_1: return 0x02;
	 case SDL_SCANCODE_2: return 0x03;
	 case SDL_SCANCODE_3: return 0x04;
	 case SDL_SCANCODE_4: return 0x05;
	 case SDL_SCANCODE_5: return 0x06;
	 case SDL_SCANCODE_6: return 0x07;
	 case SDL_SCANCODE_7: return 0x08;
	 case SDL_SCANCODE_8: return 0x09;
	 case SDL_SCANCODE_9: return 0x0a;
	 case SDL_SCANCODE_0: return 0x0b;
	 case SDL_SCANCODE_RETURN: return 0x1c;
	 case SDL_SCANCODE_ESCAPE: return 0x01;
	 case SDL_SCANCODE_BACKSPACE: return 0x0e;
	 case SDL_SCANCODE_TAB: return 0x0f;
	 case SDL_SCANCODE_SPACE: return 0x39;
	 case SDL_SCANCODE_MINUS: return 0x0c;
	 case SDL_SCANCODE_EQUALS: return 0x0d;
	 case SDL_SCANCODE_LEFTBRACKET: return 0x1a;
	 case SDL_SCANCODE_RIGHTBRACKET: return 0x1b;
	 case SDL_SCANCODE_BACKSLASH: return 0x29;  /* for 0x60 see NONUSBACKSLASH */
	 case SDL_SCANCODE_NONUSHASH: return 0x2b;
	 case SDL_SCANCODE_SEMICOLON: return 0x27;
	 case SDL_SCANCODE_APOSTROPHE: return 0x28;
	 case SDL_SCANCODE_GRAVE: return 0x2b;      /* ok? */
	 case SDL_SCANCODE_COMMA: return 0x33;
	 case SDL_SCANCODE_PERIOD: return 0x34;
	 case SDL_SCANCODE_SLASH: return 0x35;
	 case SDL_SCANCODE_CAPSLOCK: return 0x3a;
	 case SDL_SCANCODE_F1: return 0x3b;
	 case SDL_SCANCODE_F2: return 0x3c;
	 case SDL_SCANCODE_F3: return 0x3d;
	 case SDL_SCANCODE_F4: return 0x3e;
	 case SDL_SCANCODE_F5: return 0x3f;
	 case SDL_SCANCODE_F6: return 0x40;
	 case SDL_SCANCODE_F7: return 0x41;
	 case SDL_SCANCODE_F8: return 0x42;
	 case SDL_SCANCODE_F9: return 0x43;
	 case SDL_SCANCODE_F10: return 0x44;
	 case SDL_SCANCODE_F11: return 0x62;
	 case SDL_SCANCODE_F12: return 0x61;
	 case SDL_SCANCODE_PRINTSCREEN: return 0x62;
	 case SDL_SCANCODE_SCROLLLOCK: return 0x61;
	 case SDL_SCANCODE_PAUSE: return 0x61;
	 case SDL_SCANCODE_INSERT: return 0x52;
	 case SDL_SCANCODE_HOME: return 0x47;
	 case SDL_SCANCODE_PAGEUP: return 0x63;
	 case SDL_SCANCODE_DELETE: return 0x53;
	 case SDL_SCANCODE_END: return 0x2b;
	 case SDL_SCANCODE_PAGEDOWN: return 0x64;
	 case SDL_SCANCODE_RIGHT: return 0x4d;
	 case SDL_SCANCODE_LEFT: return 0x4b;
	 case SDL_SCANCODE_DOWN: return 0x50;
	 case SDL_SCANCODE_UP: return 0x48;
	 case SDL_SCANCODE_NUMLOCKCLEAR: return 0x64;
	 case SDL_SCANCODE_KP_DIVIDE: return 0x65;
	 case SDL_SCANCODE_KP_MULTIPLY: return 0x66;
	 case SDL_SCANCODE_KP_MINUS: return 0x4a;
	 case SDL_SCANCODE_KP_PLUS: return 0x4e;
	 case SDL_SCANCODE_KP_ENTER: return 0x72;
	 case SDL_SCANCODE_KP_1: return 0x6d;
	 case SDL_SCANCODE_KP_2: return 0x6e;
	 case SDL_SCANCODE_KP_3: return 0x6f;
	 case SDL_SCANCODE_KP_4: return 0x6a;
	 case SDL_SCANCODE_KP_5: return 0x6b;
	 case SDL_SCANCODE_KP_6: return 0x6c;
	 case SDL_SCANCODE_KP_7: return 0x67;
	 case SDL_SCANCODE_KP_8: return 0x68;
	 case SDL_SCANCODE_KP_9: return 0x69;
	 case SDL_SCANCODE_KP_0: return 0x70;
	 case SDL_SCANCODE_KP_PERIOD: return 0x71;
	 case SDL_SCANCODE_NONUSBACKSLASH: return 0x60;
	 //case SDL_SCANCODE_APPLICATION: return ;
	 case SDL_SCANCODE_KP_EQUALS: return 0x63;
	 case SDL_SCANCODE_F13: return 0x63;
	 case SDL_SCANCODE_F14: return 0x64;
	 case SDL_SCANCODE_HELP: return 0x62;
	 case SDL_SCANCODE_UNDO: return 0x61;
	 case SDL_SCANCODE_KP_COMMA: return 0x71;
	 case SDL_SCANCODE_CLEAR: return 0x47;
	 case SDL_SCANCODE_RETURN2: return 0x1c;
	 case SDL_SCANCODE_KP_LEFTPAREN: return 0x63;
	 case SDL_SCANCODE_KP_RIGHTPAREN: return 0x64;
	 case SDL_SCANCODE_KP_LEFTBRACE: return 0x63;
	 case SDL_SCANCODE_KP_RIGHTBRACE: return 0x64;
	 case SDL_SCANCODE_KP_TAB: return 0x0f;
	 case SDL_SCANCODE_KP_BACKSPACE: return 0x0e;
	 case SDL_SCANCODE_KP_COLON: return 0x33;
	 case SDL_SCANCODE_KP_HASH: return 0x0c;
	 case SDL_SCANCODE_KP_SPACE: return 0x39;
	 case SDL_SCANCODE_KP_CLEAR: return 0x47;
	 case SDL_SCANCODE_LCTRL: return 0x1d;
	 case SDL_SCANCODE_LSHIFT: return 0x2a;
	 case SDL_SCANCODE_LALT: return 0x38;
	 case SDL_SCANCODE_RCTRL: return 0x1d;
	 case SDL_SCANCODE_RSHIFT: return 0x36;
	 default:
		if (!pKeySym->scancode && pKeySym->sym)
		{
			/* assume SimulateKey
			 * -> KeyUp/Down
			 *    -> Remap (with scancode mode configured)
			 *       -> PcToStScanCode
			 */
			return Keymap_SymbolicToStScanCode(pKeySym);
		}
		Log_Printf(LOG_WARN, "Unhandled scancode 0x%x!\n", pKeySym->scancode);
		return -1;
	}
}

#else /* !WITH_SDL2 */

/**
 * Heuristic analysis to find out the obscure scancode offset.
 * Some keys like 'z' can't be used for detection since they are on different
 * locations on "qwertz" and "azerty" keyboards.
 * This clever code has originally been taken from the emulator Aranym. (cheers!)
 */
static int Keymap_FindScanCodeOffset(SDL_keysym* keysym)
{
	int offset = -1;    /* uninitialized scancode offset */
	int scanPC = keysym->scancode;

	if (scanPC == 0)
		return -1; /* Ignore illegal scancode */

	switch (keysym->sym)
	{
	 case SDLK_ESCAPE: offset = scanPC - 0x01; break;
	 case SDLK_1:  offset = scanPC - 0x02; break;
	 case SDLK_2:  offset = scanPC - 0x03; break;
	 case SDLK_3:  offset = scanPC - 0x04; break;
	 case SDLK_4:  offset = scanPC - 0x05; break;
	 case SDLK_5:  offset = scanPC - 0x06; break;
	 case SDLK_6:  offset = scanPC - 0x07; break;
	 case SDLK_7:  offset = scanPC - 0x08; break;
	 case SDLK_8:  offset = scanPC - 0x09; break;
	 case SDLK_9:  offset = scanPC - 0x0a; break;
	 case SDLK_0:  offset = scanPC - 0x0b; break;
	 case SDLK_BACKSPACE: offset = scanPC - 0x0e; break;
	 case SDLK_TAB:    offset = scanPC - 0x0f; break;
	 case SDLK_RETURN: offset = scanPC - 0x1c; break;
	 case SDLK_SPACE:  offset = scanPC - 0x39; break;
	 /*case SDLK_q:  offset = scanPC - 0x10; break;*/  /* different on azerty */
	 /*case SDLK_w:  offset = scanPC - 0x11; break;*/  /* different on azerty */
	 case SDLK_e:  offset = scanPC - 0x12; break;
	 case SDLK_r:  offset = scanPC - 0x13; break;
	 case SDLK_t:  offset = scanPC - 0x14; break;
	 /*case SDLK_y:  offset = scanPC - 0x15; break;*/  /* different on qwertz */
	 case SDLK_u:  offset = scanPC - 0x16; break;
	 case SDLK_i:  offset = scanPC - 0x17; break;
	 case SDLK_o:  offset = scanPC - 0x18; break;
	 case SDLK_p:  offset = scanPC - 0x19; break;
	 /*case SDLK_a:  offset = scanPC - 0x1e; break;*/  /* different on azerty */
	 case SDLK_s:  offset = scanPC - 0x1f; break;
	 case SDLK_d:  offset = scanPC - 0x20; break;
	 case SDLK_f:  offset = scanPC - 0x21; break;
	 case SDLK_g:  offset = scanPC - 0x22; break;
	 case SDLK_h:  offset = scanPC - 0x23; break;
	 case SDLK_j:  offset = scanPC - 0x24; break;
	 case SDLK_k:  offset = scanPC - 0x25; break;
	 case SDLK_l:  offset = scanPC - 0x26; break;
	 /*case SDLK_z:  offset = scanPC - 0x2c; break;*/  /* different on qwertz and azerty */
	 case SDLK_x:  offset = scanPC - 0x2d; break;
	 case SDLK_c:  offset = scanPC - 0x2e; break;
	 case SDLK_v:  offset = scanPC - 0x2f; break;
	 case SDLK_b:  offset = scanPC - 0x30; break;
	 case SDLK_n:  offset = scanPC - 0x31; break;
	 /*case SDLK_m:  offset = scanPC - 0x32; break;*/  /* different on azerty */
	 case SDLK_CAPSLOCK:  offset = scanPC - 0x3a; break;
	 case SDLK_LSHIFT: offset = scanPC - 0x2a; break;
	 case SDLK_LCTRL: offset = scanPC - 0x1d; break;
	 case SDLK_LALT: offset = scanPC - 0x38; break;
	 case SDLK_F1:  offset = scanPC - 0x3b; break;
	 case SDLK_F2:  offset = scanPC - 0x3c; break;
	 case SDLK_F3:  offset = scanPC - 0x3d; break;
	 case SDLK_F4:  offset = scanPC - 0x3e; break;
	 case SDLK_F5:  offset = scanPC - 0x3f; break;
	 case SDLK_F6:  offset = scanPC - 0x40; break;
	 case SDLK_F7:  offset = scanPC - 0x41; break;
	 case SDLK_F8:  offset = scanPC - 0x42; break;
	 case SDLK_F9:  offset = scanPC - 0x43; break;
	 case SDLK_F10: offset = scanPC - 0x44; break;
	 default:  break;
	}

	if (offset != -1)
	{
		Log_Printf(LOG_WARN, "Detected scancode offset = %d (key: '%s' with scancode $%02x)\n",
		        offset, SDL_GetKeyName(keysym->sym), scanPC);
	}

	return offset;
}


/**
 * Map PC scancode to ST scancode.
 * This code was heavily inspired by the emulator Aranym. (cheers!)
 */
static uint8_t Keymap_PcToStScanCode(SDL_keysym* keysym)
{
	static int offset = -1;    /* uninitialized scancode offset */

	/* We sometimes enter here with an illegal (=0) scancode, so we keep
	 * track of the right scancodes in a table and then use a value from there.
	 */
	if (keysym->scancode != 0)
	{
		SdlSymToSdlScan[keysym->sym] = keysym->scancode;
	}
	else
	{
		keysym->scancode = SdlSymToSdlScan[keysym->sym];
		if (keysym->scancode == 0)
			Log_Printf(LOG_WARN, "Key scancode is 0!\n");
	}

	switch (keysym->sym)
	{
	 /* Numeric Pad */
	 /* note that the numbers are handled in Keymap_GetKeyPadScanCode()! */
	 case SDLK_KP_DIVIDE:   return 0x65;  /* Numpad / */
	 case SDLK_KP_MULTIPLY: return 0x66;  /* NumPad * */
	 case SDLK_KP_MINUS:    return 0x4a;  /* NumPad - */
	 case SDLK_KP_PLUS:     return 0x4e;  /* NumPad + */
	 case SDLK_KP_PERIOD:   return 0x71;  /* NumPad . */
	 case SDLK_KP_ENTER:    return 0x72;  /* NumPad Enter */

	 /* Special Keys */
	 case SDLK_PRINT:    	return 0x62;  /* Help */
	 case SDLK_SCROLLOCK: 	return 0x61; /* Undo */
	 case SDLK_PAGEUP:   	return 0x63;  /* Keypad ( */
	 case SDLK_PAGEDOWN: 	return 0x64;  /* Keypad ) */
	 case SDLK_HOME:     	return 0x47;  /* Home */
	 case SDLK_END:      	return 0x60;  /* End => "<>" on German Atari kbd */
	 case SDLK_UP:       	return 0x48;  /* Arrow Up */
	 case SDLK_LEFT:    	return 0x4b;  /* Arrow Left */
	 case SDLK_RIGHT:    	return 0x4d;  /* Arrow Right */
	 case SDLK_DOWN:     	return 0x50;  /* Arrow Down */
	 case SDLK_INSERT:   	return 0x52;  /* Insert */
	 case SDLK_DELETE:   	return 0x53;  /* Delete */
	 case SDLK_LESS:     	return 0x60;  /* "<" */

	 /* Map Right Alt/Alt Gr/Control to the Atari keys */
	 case SDLK_RCTRL:  	return 0x1d;  /* Control */
	 case SDLK_RALT:   	return 0x38;  /* Alternate */

	 default:
		/* Process remaining keys: assume that it's PC101 keyboard
		 * and that it is compatible with Atari ST keyboard (basically
		 * same scancodes but on different platforms with different
		 * base offset (framebuffer = 0, X11 = 8).
		 * Try to detect the offset using a little bit of black magic.
		 * If offset is known then simply pass the scancode. */
		if (offset == -1)
		{
			offset = Keymap_FindScanCodeOffset(keysym);
		}

		if (offset >= 0)
		{
			/* offset is defined so pass the scancode directly */
			return (keysym->scancode - offset);
		}
		else
		{
			/* Failed to detect offset, so use default value 8 */
			Log_Printf(LOG_WARN, "Offset detection failed with "
			        "key '%s', scancode = 0x%02x, symcode =  0x%02x\n",
			        SDL_GetKeyName(keysym->sym), keysym->scancode, keysym->sym);
			return (keysym->scancode - 8);
		}
		break;
	}
}

#endif	/* !WITH_SDL2 */


/**
 * Remap a keypad key to ST scan code. We use a separate function for this
 * so that we can easily toggle between number and cursor mode with the
 * numlock key.
 */
static char Keymap_GetKeyPadScanCode(SDL_keysym* pKeySym)
{
	if (SDL_GetModState() & KMOD_NUM)
	{
		switch (pKeySym->sym)
		{
		 case SDLK_KP1:  return 0x6d;  /* NumPad 1 */
		 case SDLK_KP2:  return 0x6e;  /* NumPad 2 */
		 case SDLK_KP3:  return 0x6f;  /* NumPad 3 */
		 case SDLK_KP4:  return 0x6a;  /* NumPad 4 */
		 case SDLK_KP5:  return 0x6b;  /* NumPad 5 */
		 case SDLK_KP6:  return 0x6c;  /* NumPad 6 */
		 case SDLK_KP7:  return 0x67;  /* NumPad 7 */
		 case SDLK_KP8:  return 0x68;  /* NumPad 8 */
		 case SDLK_KP9:  return 0x69;  /* NumPad 9 */
		 default:  break;
		}
	}
	else
	{
		switch (pKeySym->sym)
		{
		 case SDLK_KP1:  return 0x6d;  /* NumPad 1 */
		 case SDLK_KP2:  return 0x50;  /* Cursor down */
		 case SDLK_KP3:  return 0x6f;  /* NumPad 3 */
		 case SDLK_KP4:  return 0x4b;  /* Cursor left */
		 case SDLK_KP5:  return 0x50;  /* Cursor down (again?) */
		 case SDLK_KP6:  return 0x4d;  /* Cursor right */
		 case SDLK_KP7:  return 0x52;  /* Insert - good for Dungeon Master */
		 case SDLK_KP8:  return 0x48;  /* Cursor up */
		 case SDLK_KP9:  return 0x47;  /* Home - again for Dungeon Master */
		 default:  break;
		}
	}

	return -1;
}


static int InputMatchesKeyMapping(SDL_keysym* keySym, struct KeyMapping *mapping)
{
	return keySym->scancode == mapping->SdlKeysym.scancode
		&& (keySym->mod & mapping->modmask) == mapping->SdlKeysym.mod;
}

/**
 * Remap SDL Key to ST Scan code
 * Receives the pressed key from SDL, and a buffer to fill with ST keyboard scan codes to simulate.
 * Returns the number of scan codes filled in the buffer.
 */
static uint8_t Keymap_RemapKeyToSTScanCodes(SDL_keysym* pKeySym, uint8_t STScanCodes[])
{
	/* Check for keypad first so we can handle numlock */
	if (ConfigureParams.Keyboard.nKeymapType != KEYMAP_LOADED)
	{
		if (pKeySym->sym >= SDLK_KP1 && pKeySym->sym <= SDLK_KP9)
		{
			STScanCodes[0] = Keymap_GetKeyPadScanCode(pKeySym);
			return 1;
		}
	}

	/* Remap from PC scancodes? */
	if (ConfigureParams.Keyboard.nKeymapType == KEYMAP_SCANCODE)
	{
		STScanCodes[0] = Keymap_PcToStScanCode(pKeySym);
		return 1;
	}

	/* Use loaded keymap? */
	if (ConfigureParams.Keyboard.nKeymapType == KEYMAP_LOADED)
	{
		int i;
		for (i = 0; i < KBD_MAX_SCANCODE; i++)
		{
			struct KeyMapping *mapping = &LoadedKeyMap[i];

			if (mapping->SdlKeysym.scancode == 0)
				break; /* End of table */

			if (!InputMatchesKeyMapping(pKeySym, mapping))
				continue;

			for (int j=0 ; j < mapping->STScanCodesLength ; j++)
				STScanCodes[j] = mapping->STScanCodes[j];

			LOG_TRACE(TRACE_KEYMAP,"Keymap found: %02x %02x %02x (%d bytes)\n",STScanCodes[0],STScanCodes[1],STScanCodes[2],mapping->STScanCodesLength);
			return mapping->STScanCodesLength;
		}
	}

	/* Fall back to symbolic mapping */
	STScanCodes[0] = Keymap_SymbolicToStScanCode(pKeySym);
	LOG_TRACE(TRACE_KEYMAP,"Symbolic mapping: %02x\n",STScanCodes[0]);
	return 1;
}


/*-----------------------------------------------------------------------*/
/**
 * Load keyboard remap file
 */
void Keymap_LoadRemapFile(const char *pszFileName)
{
	char mapLine[1024];
	int hostSpecIsOk, guestSpecIsOk;
	FILE *in;
	int idx = 0;
	int lineNumber = 0; /* For logging purposes            */
	char *token;
	char *saveptr;		/* For saving strtok_r's state, because host/guest analyses may also use strtok */
	char hostSpec[50]; 	/* Host (PC) keys specification    */
	char guestSpec[50];	/* Guest's (ST) keys specification */
	const char invalidSpecificationMessage[] = "Keymap_LoadRemapFile: '%s' not a valid specification at line %d\n";

	Log_Printf(LOG_DEBUG, "Keymap_LoadRemapFile: Loading '%s'\n", pszFileName);

	/* Initialize table with default values */
	memset(LoadedKeyMap, 0, sizeof(LoadedKeyMap));

	if (strlen(pszFileName) == 0)
		return;

	/* Attempt to load file */
	if (!File_Exists(pszFileName))
	{
		Log_Printf(LOG_DEBUG, "Keymap_LoadRemapFile: '%s' not a file\n", pszFileName);
		return;
	}
	in = fopen(pszFileName, "r");
	if (!in)
	{
		Log_Printf(LOG_ERROR, "Keymap_LoadRemapFile: failed to open keymap file '%s'\n", pszFileName);
		return;
	}

	while (!feof(in) && idx < KBD_MAX_SCANCODE)
	{
		/* Read line from file */
		if (fgets(mapLine, sizeof(mapLine), in) == NULL)
			break;

		++lineNumber;

		LOG_TRACE(TRACE_KEYMAP, "Keymap_LoadRemapFile: Analysing line '%s'\n", mapLine);

		/* Remove white-space from start of line */
		Str_Trim(mapLine);

		/* Ignore empty line and comments */
		if (strlen(mapLine) == 0 || mapLine[0] == ';' || mapLine[0] == '#')
			continue;

		/* Cut out the values between host and guest parts */
		token = strtok_r(mapLine, ",", &saveptr);
		if (token == NULL)
			goto invalidSpecificationError;

		/* Get the host's key specification */
		strcpy(hostSpec,token);
		Str_Trim(hostSpec);
		if (strlen(hostSpec) == 0)
			goto invalidSpecificationError;
		hostSpecIsOk = HostSpecToSDLKeysym(hostSpec, &LoadedKeyMap[idx]);

		/* Get the guest (ST) specification */
		token = strtok_r(NULL, "\n", &saveptr);

		if (token == NULL)
			continue;
		strcpy(guestSpec,token);
		Str_Trim(guestSpec);
		if (strlen(guestSpec) == 0)
			goto invalidSpecificationError;
		guestSpecIsOk = GuestSpecToSTScanCodes(guestSpec, &LoadedKeyMap[idx]);

		/* Store into remap table, check both value within range */
		if (guestSpecIsOk && hostSpecIsOk)
		{
			LOG_TRACE(TRACE_KEYMAP,"keymap from file: host %s --> guest %s\n", hostSpec, guestSpec);
			idx += 1;
		}
		else
		{
			Log_Printf(LOG_WARN, "Could not parse keymap specification: %s\n", mapLine);
		}

		continue;

invalidSpecificationError:
			Log_Printf(LOG_ERROR, invalidSpecificationMessage, pszFileName, lineNumber);
	}

	fclose(in);
}

static int HostSpecToSDLKeysym(char *spec, struct KeyMapping* result)
{
	char buf[100];
	char *token;
	SDL_Scancode scancode;

	/* Prepare buffer */
	strcpy(buf, spec);

	/* Prepare for early returns */
	result->SdlKeysym.mod = 0;
	result->modmask = 0;

	/* Scancode part */
	token = strtok(buf,"|");
	if (token != NULL && (scancode = strtol(token, NULL, 16)))
		result->SdlKeysym.scancode = scancode;
	else
		return 0;

	/* Modifier part */
	token = strtok(NULL,"|");
	if (token != NULL)
	{
		/* We have a modifier specified */
		result->SdlKeysym.mod = SDLKeymodFromName(token);

		/* "modifier mask" part */
		token = strtok(NULL,"|");
		if (token != NULL)
			result->modmask = SDLKeymodFromName(token);
	}

	return -1; /* Success */
}


static int GuestSpecToSTScanCodes(char *spec, struct KeyMapping* mapping)
{
	/* Analyses the guest (Atari ST keyboard) specification from the table file */
	char buf[40];
	char *start;
	char *separator;
	uint8_t *scancodes = mapping->STScanCodes; /* Alias for readability */
	uint8_t scancode;
	int i = 0;

	strcpy(buf, spec);
	separator = spec-1;
	do
	{
		start = separator+1;
		separator = strchr(start, '|');
		if (separator != NULL)
			*separator = '\0';

		if (strlen(start) <= 2)
		{
			scancode = strtol(start, NULL, 16);
		}
		else
		{
			/* Scancode may be expressed as LSHIFT,RSHIFT,ALTERNATE for user convenience */
			if (strcmp(start, "LSHIFT") == 0)
				scancode = 0x2a;
			else if (strcmp(start, "RSHIFT") == 0)
				scancode = 0x36;
			else if (strcmp(start, "ALTERNATE") == 0)
				scancode = 0x38;
			else
			{
				Log_Printf(LOG_ERROR, "GuestSpecToSTScanCodes: Cannot understand scancode '%s'\n", start);
				i = 0; /* Error out */
				break;
			}
		}

		scancodes[i++] = scancode;
	} while (separator != NULL);


	LOG_TRACE(TRACE_KEYMAP, "GetSpecTOSTScanCodes: %s made %d bytes: %02x %02x %02x\n",
		spec, i, scancodes[0], scancodes[1], scancodes[2]);

	mapping->STScanCodesLength = i;

	return i > 0;
}


/*-----------------------------------------------------------------------*/
/**
 * Scan list of keys to NOT de-bounce when running in maximum speed, eg ALT,SHIFT,CTRL etc...
 * @return true if key requires de-bouncing
 */
static bool Keymap_DebounceSTKey(char STScanCode)
{
	int i=0;

	/* Are we in fast forward, and have disabled key repeat? */
	if ((ConfigureParams.System.bFastForward == true)
	    && (ConfigureParams.Keyboard.bDisableKeyRepeat))
	{
		/* We should de-bounce all non extended keys,
		 * e.g. leave ALT, SHIFT, CTRL etc... held */
		while (DebounceExtendedKeys[i])
		{
			if (STScanCode == DebounceExtendedKeys[i])
				return false;
			i++;
		}

		/* De-bounce key */
		return true;
	}

	/* Do not de-bounce key */
	return false;
}


/*-----------------------------------------------------------------------*/
/**
 * Debounce any PC key held down if running with key repeat disabled.
 * This is called each ST frame, so keys get held down for one VBL which
 * is enough for 68000 code to scan.
 */
void Keymap_DebounceAllKeys(void)
{
	uint8_t nScanCode;

	/* Return if we aren't in fast forward or have not disabled key repeat */
	if ((ConfigureParams.System.bFastForward == false)
	        || (!ConfigureParams.Keyboard.bDisableKeyRepeat))
	{
		return;
	}

	/* Now run through each key looking for ones held down */
	for (nScanCode = 1; nScanCode <= KBD_MAX_SCANCODE; nScanCode++)
	{
		/* Is key held? */
		if (Keyboard.KeyStates[nScanCode])
		{
			/* Does this require de-bouncing? */
			if (Keymap_DebounceSTKey(nScanCode))
			{
				IKBD_PressSTKey(nScanCode, false);
				Keyboard.KeyStates[nScanCode] = false;
			}
		}
	}

}


static int IsKeyTranslatable(SDL_Keycode symkey)
{
	/* Ignore modifier keys that are not passed to the ST */
	switch (symkey)
	{
		case SDLK_RALT:
	 	case SDLK_LMETA:
	 	case SDLK_RMETA:
	 	case SDLK_MODE:
	 	case SDLK_NUMLOCK:
			return 0;
	}
	return -1;
}


/*-----------------------------------------------------------------------*/
/**
 * User pressed a key down
 */
void Keymap_KeyDown(SDL_keysym *sdlkey)
{
	uint8_t stScanCodes[3];
	uint8_t stScanCodesLength;
	int symkey = sdlkey->sym;
	int modkey = sdlkey->mod;

	LOG_TRACE(TRACE_KEYMAP, "Keymap_KeyDown: sym=%i scancode=0x%02x mod=0x%02x name='%s'\n",
	          symkey, sdlkey->scancode, modkey, Keymap_GetKeyName(symkey));

	if (ShortCut_CheckKeys(modkey, symkey, true))
		return;

	/* If using joystick emulation via keyboard, DON'T send keys to keyboard processor!!!
	 * Some games use keyboard as pause! */
	if (Joy_KeyDown(symkey, modkey))
		return;

	if (!IsKeyTranslatable(symkey))
		return;

	stScanCodesLength = Keymap_RemapKeyToSTScanCodes(sdlkey, stScanCodes);
	if (stScanCodesLength > 0)
	{
		int i;
		for (i = 0; i < stScanCodesLength ; i++)
		{
			uint8_t scanCode = stScanCodes[i];

			if (!Keyboard.KeyStates[scanCode])
			{
				/* Set down */
				Keyboard.KeyStates[scanCode] = true;
				IKBD_PressSTKey(scanCode, true);
			}
		}
	}
}


/*-----------------------------------------------------------------------*/
/**
 * User released a key
 */
void Keymap_KeyUp(SDL_keysym *sdlkey)
{
	uint8_t STScanCodes[3];
	uint8_t STScanCodesLength;
	int symkey = sdlkey->sym;
	int modkey = sdlkey->mod;

	/* LOG_TRACE(TRACE_KEYMAP, "Keymap_KeyUp: sym=%i scancode=0x%02x mod=0x%02x name='%s'\n",
	          symkey, sdlkey->scancode, modkey, Keymap_GetKeyName(symkey));*/

	/* Ignore short-cut keys here */
	if (ShortCut_CheckKeys(modkey, symkey, false))
		return;

	/* If using keyboard emulation, DON'T send keys to keyboard processor!!!
	 * Some games use keyboard as pause! */
	if (Joy_KeyUp(symkey, modkey))
		return;

	/* Handle special keys */
	if (!IsKeyTranslatable(symkey))
		return;

	STScanCodesLength = Keymap_RemapKeyToSTScanCodes(sdlkey, STScanCodes);
	/* Release key (only if was pressed) */
	if (STScanCodesLength > 0)
	{
		int iScanCode;
		for (iScanCode = 0; iScanCode < STScanCodesLength ; iScanCode++)
		{
			uint8_t scanCode = STScanCodes[iScanCode];

			/* Set up */
			Keyboard.KeyStates[scanCode] = false;
			IKBD_PressSTKey(scanCode, false);
		}
	}
}

/*-----------------------------------------------------------------------*/
/**
 * Simulate press or release of a key corresponding to given character
 */
void Keymap_SimulateCharacter(char asckey, bool press)
{
	SDL_keysym sdlkey;

	sdlkey.mod = KMOD_NONE;
	sdlkey.scancode = 0;
	if (isupper((unsigned char)asckey))
	{
		if (press)
		{
			sdlkey.sym = SDLK_LSHIFT;
			Keymap_KeyDown(&sdlkey);
		}
		sdlkey.sym = tolower((unsigned char)asckey);
		sdlkey.mod = KMOD_LSHIFT;
	}
	else
	{
		sdlkey.sym = asckey;
	}

	if (press)
	{
		Keymap_KeyDown(&sdlkey);
	}
	else
	{
		Keymap_KeyUp(&sdlkey);
		if (isupper((unsigned char)asckey))
		{
			sdlkey.sym = SDLK_LSHIFT;
			Keymap_KeyUp(&sdlkey);
		}
	}
}


#if WITH_SDL2

int Keymap_GetKeyFromName(const char *name)
{
	return SDL_GetKeyFromName(name);
}

const char *Keymap_GetKeyName(int keycode)
{
	if (!keycode)
		return "";

	return SDL_GetKeyName(keycode);
}

static SDL_Keymod SDLKeymodFromName(const char *name) {
	struct {
		SDL_Keymod mod;
		const char *name;
	} const keymodNames[] = {
		{ KMOD_NONE, "KMOD_NONE" },
		{ KMOD_LSHIFT, "KMOD_LSHIFT" },
		{ KMOD_RSHIFT, "KMOD_RSHIFT" },
		{ KMOD_LCTRL, "KMOD_LCTRL" },
		{ KMOD_RCTRL, "KMOD_RCTRL" },
		{ KMOD_LALT, "KMOD_LALT" },
		{ KMOD_RALT, "KMOD_RALT" },
		{ KMOD_LGUI, "KMOD_LGUI" },
		{ KMOD_RGUI, "KMOD_RGUI" },
		{ KMOD_NUM, "KMOD_NUM" },
		{ KMOD_CAPS, "KMOD_CAPS" },
		{ KMOD_MODE, "KMOD_MODE" },
		{ KMOD_CTRL, "KMOD_CTRL" },
		{ KMOD_SHIFT, "KMOD_SHIFT" },
		{ KMOD_ALT, "KMOD_ALT" },
		{ KMOD_GUI, "KMOD_GUI" },
		{ 0/*whatever*/, NULL }};

	int i;
	for (i = 0; keymodNames[i].name != NULL; i++)
	{
		if (strcmp(name, keymodNames[i].name) == 0)
			return keymodNames[i].mod;
	};

	LOG_TRACE(TRACE_KEYMAP, "SDLKeymodFromName: Didn't find SDL_Keymod \"%s\", defaulting to KMOD_NONE.\n", name);

	return KMOD_NONE;
}

#else	/* !WITH_SDL2 */

static struct {
	int code;
	const char *name;
} const sdl_keytab[] = {
	{ SDLK_BACKSPACE, "Backspace" },
	{ SDLK_TAB, "Tab" },
	{ SDLK_CLEAR, "Clear" },
	{ SDLK_RETURN, "Return" },
	{ SDLK_PAUSE, "Pause" },
	{ SDLK_ESCAPE, "Escape" },
	{ SDLK_SPACE, "Space" },
	{ SDLK_EXCLAIM, "!" },
	{ SDLK_QUOTEDBL, "\"" },
	{ SDLK_HASH, "#" },
	{ SDLK_DOLLAR, "$" },
	{ SDLK_AMPERSAND, "&" },
	{ SDLK_QUOTE, "'" },
	{ SDLK_LEFTPAREN, "(" },
	{ SDLK_RIGHTPAREN, ")" },
	{ SDLK_ASTERISK, "*" },
	{ SDLK_PLUS, "+" },
	{ SDLK_COMMA, "," },
	{ SDLK_MINUS, "-" },
	{ SDLK_PERIOD, "." },
	{ SDLK_SLASH, "/" },
	{ SDLK_0, "0" },
	{ SDLK_1, "1" },
	{ SDLK_2, "2" },
	{ SDLK_3, "3" },
	{ SDLK_4, "4" },
	{ SDLK_5, "5" },
	{ SDLK_6, "6" },
	{ SDLK_7, "7" },
	{ SDLK_8, "8" },
	{ SDLK_9, "9" },
	{ SDLK_COLON, ":" },
	{ SDLK_SEMICOLON, ";" },
	{ SDLK_LESS, "<" },
	{ SDLK_EQUALS, "=" },
	{ SDLK_GREATER, ">" },
	{ SDLK_QUESTION, "?" },
	{ SDLK_AT, "@" },
	{ SDLK_LEFTBRACKET, "[" },
	{ SDLK_BACKSLASH, "\\" },
	{ SDLK_RIGHTBRACKET, "]" },
	{ SDLK_CARET, "^" },
	{ SDLK_UNDERSCORE, "_" },
	{ SDLK_BACKQUOTE, "`" },
	{ SDLK_a, "A" },
	{ SDLK_b, "B" },
	{ SDLK_c, "C" },
	{ SDLK_d, "D" },
	{ SDLK_e, "E" },
	{ SDLK_f, "F" },
	{ SDLK_g, "G" },
	{ SDLK_h, "H" },
	{ SDLK_i, "I" },
	{ SDLK_j, "J" },
	{ SDLK_k, "K" },
	{ SDLK_l, "L" },
	{ SDLK_m, "M" },
	{ SDLK_n, "N" },
	{ SDLK_o, "O" },
	{ SDLK_p, "P" },
	{ SDLK_q, "Q" },
	{ SDLK_r, "R" },
	{ SDLK_s, "S" },
	{ SDLK_t, "T" },
	{ SDLK_u, "U" },
	{ SDLK_v, "V" },
	{ SDLK_w, "W" },
	{ SDLK_x, "X" },
	{ SDLK_y, "Y" },
	{ SDLK_z, "Z" },
	{ SDLK_DELETE, "Delete" },
	{ SDLK_KP0, "Keypad 0" },
	{ SDLK_KP1, "Keypad 1" },
	{ SDLK_KP2, "Keypad 2" },
	{ SDLK_KP3, "Keypad 3" },
	{ SDLK_KP4, "Keypad 4" },
	{ SDLK_KP5, "Keypad 5" },
	{ SDLK_KP6, "Keypad 6" },
	{ SDLK_KP7, "Keypad 7" },
	{ SDLK_KP8, "Keypad 8" },
	{ SDLK_KP9, "Keypad 9" },
	{ SDLK_KP_PERIOD, "Keypad ." },
	{ SDLK_KP_DIVIDE, "Keypad /" },
	{ SDLK_KP_MULTIPLY, "Keypad *" },
	{ SDLK_KP_MINUS, "Keypad -" },
	{ SDLK_KP_PLUS, "Keypad +" },
	{ SDLK_KP_ENTER, "Keypad Enter" },
	{ SDLK_KP_EQUALS, "Keypad =" },
	{ SDLK_UP, "Up" },
	{ SDLK_DOWN, "Down" },
	{ SDLK_RIGHT, "Right" },
	{ SDLK_LEFT, "Left" },
	{ SDLK_INSERT, "Insert" },
	{ SDLK_HOME, "Home" },
	{ SDLK_END, "End" },
	{ SDLK_PAGEUP, "PageUp" },
	{ SDLK_PAGEDOWN, "PageDown" },
	{ SDLK_F1, "F1" },
	{ SDLK_F2, "F2" },
	{ SDLK_F3, "F3" },
	{ SDLK_F4, "F4" },
	{ SDLK_F5, "F5" },
	{ SDLK_F6, "F6" },
	{ SDLK_F7, "F7" },
	{ SDLK_F8, "F8" },
	{ SDLK_F9, "F9" },
	{ SDLK_F10, "F10" },
	{ SDLK_F11, "F11" },
	{ SDLK_F12, "F12" },
	{ SDLK_F13, "F13" },
	{ SDLK_F14, "F14" },
	{ SDLK_F15, "F15" },
	{ SDLK_NUMLOCK, "Numlock" },
	{ SDLK_CAPSLOCK, "CapsLock" },
	{ SDLK_SCROLLOCK, "ScrollLock" },
	{ SDLK_RSHIFT, "Right Shift" },
	{ SDLK_LSHIFT, "Left Shift" },
	{ SDLK_RCTRL, "Right Ctrl" },
	{ SDLK_LCTRL, "Left Ctrl" },
	{ SDLK_RALT, "Right Alt" },
	{ SDLK_LALT, "Left Alt" },
	{ SDLK_RMETA, "Right GUI" },
	{ SDLK_LMETA, "Left GUI" },
	{ SDLK_MODE, "ModeSwitch" },
	{ SDLK_HELP, "Help" },
	{ SDLK_PRINT, "PrintScreen" },
	{ SDLK_SYSREQ, "SysReq" },
	{ SDLK_BREAK, "Cancel" },
	{ SDLK_MENU, "Menu" },
	{ SDLK_POWER, "Power" },
	{ SDLK_UNDO, "Undo" },

	{ SDLK_WORLD_1, "¡" },	/* 161 */
	{ SDLK_WORLD_2, "¢" },	/* 162 */
	{ SDLK_WORLD_3, "£" },	/* 163 */
	{ SDLK_WORLD_4, "¤" },	/* 164 */
	{ SDLK_WORLD_5, "¥" },	/* 165 */
	{ SDLK_WORLD_6, "¦" },	/* 166 */
	{ SDLK_WORLD_7, "§" },	/* 167 */
	{ SDLK_WORLD_8, "¨" },	/* 168 */
	{ SDLK_WORLD_9, "©" },	/* 169 */
	{ SDLK_WORLD_10, "ª" },	/* 170 */
	{ SDLK_WORLD_11, "«" },	/* 171 */
	{ SDLK_WORLD_12, "¬" },	/* 172 */
	{ SDLK_WORLD_14, "®" },	/* 174 */
	{ SDLK_WORLD_15, "¯" },	/* 175 */
	{ SDLK_WORLD_16, "°" },	/* 176 */
	{ SDLK_WORLD_17, "±" },	/* 177 */
	{ SDLK_WORLD_18, "²" },	/* 178 */
	{ SDLK_WORLD_19, "³" },	/* 179 */
	{ SDLK_WORLD_20, "´" },	/* 180 */
	{ SDLK_WORLD_21, "µ" },	/* 181 */
	{ SDLK_WORLD_22, "¶" },	/* 182 */
	{ SDLK_WORLD_23, "·" },	/* 183 */
	{ SDLK_WORLD_24, "¸" },	/* 184 */
	{ SDLK_WORLD_25, "¹" },	/* 185 */
	{ SDLK_WORLD_26, "º" },	/* 186 */
	{ SDLK_WORLD_27, "»" },	/* 187 */
	{ SDLK_WORLD_28, "¼" },	/* 188 */
	{ SDLK_WORLD_29, "½" },	/* 189 */
	{ SDLK_WORLD_30, "¾" },	/* 190 */
	{ SDLK_WORLD_31, "¿" },	/* 191 */
	{ SDLK_WORLD_32, "À" },	/* 192 */
	{ SDLK_WORLD_33, "Á" },	/* 193 */
	{ SDLK_WORLD_34, "Â" },	/* 194 */
	{ SDLK_WORLD_35, "Ã" },	/* 195 */
	{ SDLK_WORLD_36, "Ä" },	/* 196 */
	{ SDLK_WORLD_37, "Å" },	/* 197 */
	{ SDLK_WORLD_38, "Æ" },	/* 198 */
	{ SDLK_WORLD_39, "Ç" },	/* 199 */
	{ SDLK_WORLD_40, "È" },	/* 200 */
	{ SDLK_WORLD_41, "É" },	/* 201 */
	{ SDLK_WORLD_42, "Ê" },	/* 202 */
	{ SDLK_WORLD_43, "Ë" },	/* 203 */
	{ SDLK_WORLD_44, "Ì" },	/* 204 */
	{ SDLK_WORLD_45, "Í" },	/* 205 */
	{ SDLK_WORLD_46, "Î" },	/* 206 */
	{ SDLK_WORLD_47, "Ï" },	/* 207 */
	{ SDLK_WORLD_48, "Ð" },	/* 208 */
	{ SDLK_WORLD_49, "Ñ" },	/* 209 */
	{ SDLK_WORLD_50, "Ò" },	/* 210 */
	{ SDLK_WORLD_51, "Ó" },	/* 211 */
	{ SDLK_WORLD_52, "Ô" },	/* 212 */
	{ SDLK_WORLD_53, "Õ" },	/* 213 */
	{ SDLK_WORLD_54, "Ö" },	/* 214 */
	{ SDLK_WORLD_55, "×" },	/* 215 */
	{ SDLK_WORLD_56, "Ø" },	/* 216 */
	{ SDLK_WORLD_57, "Ù" },	/* 217 */
	{ SDLK_WORLD_58, "Ú" },	/* 218 */
	{ SDLK_WORLD_59, "Û" },	/* 219 */
	{ SDLK_WORLD_60, "Ü" },	/* 220 */
	{ SDLK_WORLD_61, "Ý" },	/* 221 */
	{ SDLK_WORLD_62, "Þ" },	/* 222 */
	{ SDLK_WORLD_63, "ß" },	/* 223 */
	{ SDLK_WORLD_64, "à" },	/* 224 */
	{ SDLK_WORLD_65, "á" },	/* 225 */
	{ SDLK_WORLD_66, "â" },	/* 226 */
	{ SDLK_WORLD_67, "ã" },	/* 227 */
	{ SDLK_WORLD_68, "ä" },	/* 228 */
	{ SDLK_WORLD_69, "å" },	/* 229 */
	{ SDLK_WORLD_70, "æ" },	/* 230 */
	{ SDLK_WORLD_71, "ç" },	/* 231 */
	{ SDLK_WORLD_72, "è" },	/* 232 */
	{ SDLK_WORLD_73, "é" },	/* 233 */
	{ SDLK_WORLD_74, "ê" },	/* 234 */
	{ SDLK_WORLD_75, "ë" },	/* 235 */
	{ SDLK_WORLD_76, "ì" },	/* 236 */
	{ SDLK_WORLD_77, "í" },	/* 237 */
	{ SDLK_WORLD_78, "î" },	/* 238 */
	{ SDLK_WORLD_79, "ï" },	/* 239 */
	{ SDLK_WORLD_80, "ð" },	/* 240 */
	{ SDLK_WORLD_81, "ñ" },	/* 241 */
	{ SDLK_WORLD_82, "ò" },	/* 242 */
	{ SDLK_WORLD_83, "ó" },	/* 243 */
	{ SDLK_WORLD_84, "ô" },	/* 244 */
	{ SDLK_WORLD_85, "õ" },	/* 245 */
	{ SDLK_WORLD_86, "ö" },	/* 246 */
	{ SDLK_WORLD_87, "÷" },	/* 247 */
	{ SDLK_WORLD_88, "ø" },	/* 248 */
	{ SDLK_WORLD_89, "ù" },	/* 249 */
	{ SDLK_WORLD_90, "ú" },	/* 250 */
	{ SDLK_WORLD_91, "û" },	/* 251 */
	{ SDLK_WORLD_92, "ü" },	/* 252 */
	{ SDLK_WORLD_93, "ý" },	/* 253 */
	{ SDLK_WORLD_94, "þ" },	/* 254 */
	{ SDLK_WORLD_95, "ÿ" },	/* 255 */

	{ -1, NULL }
};

int Keymap_GetKeyFromName(const char *name)
{
	int i;

	if (!name[0])
		return 0;

	for (i = 0; sdl_keytab[i].name != NULL; i++)
	{
		if (strcasecmp(name, sdl_keytab[i].name) == 0)
			return sdl_keytab[i].code;
	}

	return 0;
}

const char *Keymap_GetKeyName(int keycode)
{
	int i;

	if (keycode == 0)
		return "";

	for (i = 0; sdl_keytab[i].name != NULL; i++)
	{
		if (keycode == sdl_keytab[i].code)
			return sdl_keytab[i].name;
	}

	return "";
}

#endif /* !WITH_SDL2 */


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