dosgame1/system/sdl/Keyboard.cpp

822 lines
20 KiB
C++

#include <SDL.h>
#include "../Keyboard.h"
Keyboard keyboard;
// TODO map unknown key codes
uint8_t mapSdlKeycode(SDL_Keycode keyCode) {
switch (keyCode) {
case SDLK_UNKNOWN:
return KeyUnknown;
case SDLK_RETURN:
return KeyUnknown;
case SDLK_ESCAPE:
return KeyEscape;
case SDLK_BACKSPACE:
return KeyBackspace;
case SDLK_TAB:
return KeyTab;
case SDLK_SPACE:
return KeySpace;
case SDLK_EXCLAIM:
return KeyUnknown;
case SDLK_QUOTEDBL:
return KeyUnknown;
case SDLK_HASH:
return KeyUnknown;
case SDLK_PERCENT:
return KeyUnknown;
case SDLK_DOLLAR:
return KeyUnknown;
case SDLK_AMPERSAND:
return KeyUnknown;
case SDLK_QUOTE:
return KeyUnknown;
case SDLK_LEFTPAREN:
return KeyUnknown;
case SDLK_RIGHTPAREN:
return KeyUnknown;
case SDLK_ASTERISK:
return KeyUnknown;
case SDLK_PLUS:
return KeyUnknown;
case SDLK_COMMA:
return KeyUnknown;
case SDLK_MINUS:
return KeyUnknown;
case SDLK_PERIOD:
return KeyUnknown;
case SDLK_SLASH:
return KeyUnknown;
case SDLK_0:
return Key0;
case SDLK_1:
return Key1;
case SDLK_2:
return Key2;
case SDLK_3:
return Key3;
case SDLK_4:
return Key4;
case SDLK_5:
return Key5;
case SDLK_6:
return Key6;
case SDLK_7:
return Key7;
case SDLK_8:
return Key8;
case SDLK_9:
return Key9;
case SDLK_COLON:
return KeyUnknown;
case SDLK_SEMICOLON:
return KeyUnknown;
case SDLK_LESS:
return KeyUnknown;
case SDLK_EQUALS:
return KeyUnknown;
case SDLK_GREATER:
return KeyUnknown;
case SDLK_QUESTION:
return KeyUnknown;
case SDLK_AT:
return KeyUnknown;
case SDLK_LEFTBRACKET:
return KeyUnknown;
case SDLK_BACKSLASH:
return KeyUnknown;
case SDLK_RIGHTBRACKET:
return KeyUnknown;
case SDLK_CARET:
return KeyUnknown;
case SDLK_UNDERSCORE:
return KeyUnknown;
case SDLK_BACKQUOTE:
return KeyUnknown;
case SDLK_a:
return KeyA;
case SDLK_b:
return KeyB;
case SDLK_c:
return KeyC;
case SDLK_d:
return KeyD;
case SDLK_e:
return KeyE;
case SDLK_f:
return KeyF;
case SDLK_g:
return KeyG;
case SDLK_h:
return KeyG;
case SDLK_i:
return KeyI;
case SDLK_j:
return KeyJ;
case SDLK_k:
return KeyK;
case SDLK_l:
return KeyL;
case SDLK_m:
return KeyM;
case SDLK_n:
return KeyN;
case SDLK_o:
return KeyO;
case SDLK_p:
return KeyP;
case SDLK_q:
return KeyQ;
case SDLK_r:
return KeyR;
case SDLK_s:
return KeyS;
case SDLK_t:
return KeyT;
case SDLK_u:
return KeyU;
case SDLK_v:
return KeyV;
case SDLK_w:
return KeyW;
case SDLK_x:
return KeyX;
case SDLK_y:
return KeyY;
case SDLK_z:
return KeyZ;
case SDLK_CAPSLOCK:
return KeyCapsLock;
case SDLK_F1:
return KeyF1;
case SDLK_F2:
return KeyF2;
case SDLK_F3:
return KeyF3;
case SDLK_F4:
return KeyF4;
case SDLK_F5:
return KeyF5;
case SDLK_F6:
return KeyF6;
case SDLK_F7:
return KeyF7;
case SDLK_F8:
return KeyF8;
case SDLK_F9:
return KeyF9;
case SDLK_F10:
return KeyF10;
case SDLK_F11:
return KeyF11;
case SDLK_F12:
return KeyF12;
case SDLK_PRINTSCREEN:
return KeyPrint;
case SDLK_SCROLLLOCK:
return KeyScrollLock;
case SDLK_PAUSE:
return KeyPause;
case SDLK_INSERT:
return KeyInsert;
case SDLK_HOME:
return KeyHome;
case SDLK_PAGEUP:
return KeyPageUp;
case SDLK_DELETE:
return KeyDelete;
case SDLK_END:
return KeyEnd;
case SDLK_PAGEDOWN:
return KeyPageDown;
case SDLK_RIGHT:
return KeyRight;
case SDLK_LEFT:
return KeyLeft;
case SDLK_DOWN:
return KeyDown;
case SDLK_UP:
return KeyUp;
case SDLK_NUMLOCKCLEAR:
return KeyUnknown;
case SDLK_KP_DIVIDE:
return KeyUnknown;
case SDLK_KP_MULTIPLY:
return KeyUnknown;
case SDLK_KP_MINUS:
return KeyUnknown;
case SDLK_KP_PLUS:
return KeyUnknown;
case SDLK_KP_ENTER:
return KeyUnknown;
case SDLK_KP_1:
return KeyUnknown;
case SDLK_KP_2:
return KeyUnknown;
case SDLK_KP_3:
return KeyUnknown;
case SDLK_KP_4:
return KeyUnknown;
case SDLK_KP_5:
return KeyUnknown;
case SDLK_KP_6:
return KeyUnknown;
case SDLK_KP_7:
return KeyUnknown;
case SDLK_KP_8:
return KeyUnknown;
case SDLK_KP_9:
return KeyUnknown;
case SDLK_KP_0:
return KeyUnknown;
case SDLK_KP_PERIOD:
return KeyUnknown;
case SDLK_APPLICATION:
return KeyUnknown;
case SDLK_POWER:
return KeyUnknown;
case SDLK_KP_EQUALS:
return KeyUnknown;
case SDLK_F13:
return KeyUnknown;
case SDLK_F14:
return KeyUnknown;
case SDLK_F15:
return KeyUnknown;
case SDLK_F16:
return KeyUnknown;
case SDLK_F17:
return KeyUnknown;
case SDLK_F18:
return KeyUnknown;
case SDLK_F19:
return KeyUnknown;
case SDLK_F20:
return KeyUnknown;
case SDLK_F21:
return KeyUnknown;
case SDLK_F22:
return KeyUnknown;
case SDLK_F23:
return KeyUnknown;
case SDLK_F24:
return KeyUnknown;
case SDLK_EXECUTE:
return KeyUnknown;
case SDLK_HELP:
return KeyUnknown;
case SDLK_MENU:
return KeyUnknown;
case SDLK_SELECT:
return KeyUnknown;
case SDLK_STOP:
return KeyUnknown;
case SDLK_AGAIN:
return KeyUnknown;
case SDLK_UNDO:
return KeyUnknown;
case SDLK_CUT:
return KeyUnknown;
case SDLK_COPY:
return KeyUnknown;
case SDLK_PASTE:
return KeyUnknown;
case SDLK_FIND:
return KeyUnknown;
case SDLK_MUTE:
return KeyUnknown;
case SDLK_VOLUMEUP:
return KeyUnknown;
case SDLK_VOLUMEDOWN:
return KeyUnknown;
case SDLK_KP_COMMA:
return KeyUnknown;
case SDLK_KP_EQUALSAS400:
return KeyUnknown;
case SDLK_ALTERASE:
return KeyUnknown;
case SDLK_SYSREQ:
return KeyUnknown;
case SDLK_CANCEL:
return KeyUnknown;
case SDLK_CLEAR:
return KeyUnknown;
case SDLK_PRIOR:
return KeyUnknown;
case SDLK_RETURN2:
return KeyUnknown;
case SDLK_SEPARATOR:
return KeyUnknown;
case SDLK_OUT:
return KeyUnknown;
case SDLK_OPER:
return KeyUnknown;
case SDLK_CLEARAGAIN:
return KeyUnknown;
case SDLK_CRSEL:
return KeyUnknown;
case SDLK_EXSEL:
return KeyUnknown;
case SDLK_KP_00:
return KeyUnknown;
case SDLK_KP_000:
return KeyUnknown;
case SDLK_THOUSANDSSEPARATOR:
return KeyUnknown;
case SDLK_DECIMALSEPARATOR:
return KeyUnknown;
case SDLK_CURRENCYUNIT:
return KeyUnknown;
case SDLK_CURRENCYSUBUNIT:
return KeyUnknown;
case SDLK_KP_LEFTPAREN:
return KeyUnknown;
case SDLK_KP_RIGHTPAREN:
return KeyUnknown;
case SDLK_KP_LEFTBRACE:
return KeyUnknown;
case SDLK_KP_RIGHTBRACE:
return KeyUnknown;
case SDLK_KP_TAB:
return KeyUnknown;
case SDLK_KP_BACKSPACE:
return KeyUnknown;
case SDLK_KP_A:
return KeyUnknown;
case SDLK_KP_B:
return KeyUnknown;
case SDLK_KP_C:
return KeyUnknown;
case SDLK_KP_D:
return KeyUnknown;
case SDLK_KP_E:
return KeyUnknown;
case SDLK_KP_F:
return KeyUnknown;
case SDLK_KP_XOR:
return KeyUnknown;
case SDLK_KP_POWER:
return KeyUnknown;
case SDLK_KP_PERCENT:
return KeyUnknown;
case SDLK_KP_LESS:
return KeyUnknown;
case SDLK_KP_GREATER:
return KeyUnknown;
case SDLK_KP_AMPERSAND:
return KeyUnknown;
case SDLK_KP_DBLAMPERSAND:
return KeyUnknown;
case SDLK_KP_VERTICALBAR:
return KeyUnknown;
case SDLK_KP_DBLVERTICALBAR:
return KeyUnknown;
case SDLK_KP_COLON:
return KeyUnknown;
case SDLK_KP_HASH:
return KeyUnknown;
case SDLK_KP_SPACE:
return KeyUnknown;
case SDLK_KP_AT:
return KeyUnknown;
case SDLK_KP_EXCLAM:
return KeyUnknown;
case SDLK_KP_MEMSTORE:
return KeyUnknown;
case SDLK_KP_MEMRECALL:
return KeyUnknown;
case SDLK_KP_MEMCLEAR:
return KeyUnknown;
case SDLK_KP_MEMADD:
return KeyUnknown;
case SDLK_KP_MEMSUBTRACT:
return KeyUnknown;
case SDLK_KP_MEMMULTIPLY:
return KeyUnknown;
case SDLK_KP_MEMDIVIDE:
return KeyUnknown;
case SDLK_KP_PLUSMINUS:
return KeyUnknown;
case SDLK_KP_CLEAR:
return KeyUnknown;
case SDLK_KP_CLEARENTRY:
return KeyUnknown;
case SDLK_KP_BINARY:
return KeyUnknown;
case SDLK_KP_OCTAL:
return KeyUnknown;
case SDLK_KP_DECIMAL:
return KeyUnknown;
case SDLK_KP_HEXADECIMAL:
return KeyUnknown;
case SDLK_LCTRL:
return KeyLeftCtrl;
case SDLK_LSHIFT:
return KeyLeftShift;
case SDLK_LALT:
return KeyLeftAlt;
case SDLK_LGUI:
return KeyUnknown;
case SDLK_RCTRL:
return KeyRightControl;
case SDLK_RSHIFT:
return KeyRightShift;
case SDLK_RALT:
return KeyRightAlt;
case SDLK_RGUI:
return KeyUnknown;
case SDLK_MODE:
return KeyUnknown;
case SDLK_AUDIONEXT:
return KeyUnknown;
case SDLK_AUDIOPREV:
return KeyUnknown;
case SDLK_AUDIOSTOP:
return KeyUnknown;
case SDLK_AUDIOPLAY:
return KeyUnknown;
case SDLK_AUDIOMUTE:
return KeyUnknown;
case SDLK_MEDIASELECT:
return KeyUnknown;
case SDLK_WWW:
return KeyUnknown;
case SDLK_MAIL:
return KeyUnknown;
case SDLK_CALCULATOR:
return KeyUnknown;
case SDLK_COMPUTER:
return KeyUnknown;
case SDLK_AC_SEARCH:
return KeyUnknown;
case SDLK_AC_HOME:
return KeyUnknown;
case SDLK_AC_BACK:
return KeyUnknown;
case SDLK_AC_FORWARD:
return KeyUnknown;
case SDLK_AC_STOP:
return KeyUnknown;
case SDLK_AC_REFRESH:
return KeyUnknown;
case SDLK_AC_BOOKMARKS:
return KeyUnknown;
case SDLK_BRIGHTNESSDOWN:
return KeyUnknown;
case SDLK_BRIGHTNESSUP:
return KeyUnknown;
case SDLK_DISPLAYSWITCH:
return KeyUnknown;
case SDLK_KBDILLUMTOGGLE:
return KeyUnknown;
case SDLK_KBDILLUMDOWN:
return KeyUnknown;
case SDLK_KBDILLUMUP:
return KeyUnknown;
case SDLK_EJECT:
return KeyUnknown;
case SDLK_SLEEP:
return KeyUnknown;
case SDLK_APP1:
return KeyUnknown;
case SDLK_APP2:
return KeyUnknown;
case SDLK_AUDIOREWIND:
return KeyUnknown;
case SDLK_AUDIOFASTFORWARD:
return KeyUnknown;
default: ;
}
return KeyUnknown;
}
static const char *keyNames[] = {
"<unknown:00>",
"KeyEscape", // = 0x01,
"Key1", // = 0x02,
"Key2", // = 0x03,
"Key3", // = 0x04,
"Key4", // = 0x05,
"Key5", // = 0x06,
"Key6", // = 0x07,
"Key7", // = 0x08,
"Key8", // = 0x09,
"Key9", // = 0x0a,
"Key0", // = 0x0b,
"KeyMinus", // = 0x0c,
"KeyEqual", // = 0x0d,
"KeyBackspace", // = 0x0e,
"KeyTab", // = 0x0f,
"KeyQ", // = 0x10,
"KeyW", // = 0x11,
"KeyE", // = 0x12,
"KeyR", // = 0x13,
"KeyT", // = 0x14,
"KeyY", // = 0x15,
"KeyU", // = 0x16,
"KeyI", // = 0x17,
"KeyO", // = 0x18,
"KeyP", // = 0x19,
"KeyBracketLeft", // = 0x1a,
"KeyBracketRight", // = 0x1b,
"KeyEnter", // = 0x1c,
"KeyLeftCtrl", // = 0x1d,
"KeyA", // = 0x1e,
"KeyS", // = 0x1f,
"KeyD", // = 0x20,
"KeyF", // = 0x21,
"KeyG", // = 0x22,
"KeyH", // = 0x23,
"KeyJ", // = 0x24,
"KeyK", // = 0x25,
"KeyL", // = 0x26,
"KeySemicolon", // = 0x27,
"KeyApostrophe", // = 0x28,
"KeyBacktick", // = 0x29,
"KeyLeftShift", // = 0x2a,
"KeyBackslash", // = 0x2b,
"KeyZ", // = 0x2c,
"KeyX", // = 0x2d,
"KeyC", // = 0x2e,
"KeyV", // = 0x2f,
"KeyB", // = 0x30,
"KeyN", // = 0x31,
"KeyM", // = 0x32,
"KeyComma", // = 0x33,
"KeyPeriod", // = 0x34,
"KeySlash", // = 0x35,
"KeyRightShift", // = 0x36,
"KeyKeypadMultiply", // = 0x37,
"KeyLeftAlt", // = 0x38,
"KeySpace", // = 0x39,
"KeyCapsLock", // = 0x3a,
"KeyF1", // = 0x3b,
"KeyF2", // = 0x3c,
"KeyF3", // = 0x3d,
"KeyF4", // = 0x3e,
"KeyF5", // = 0x3f,
"KeyF6", // = 0x40,
"KeyF7", // = 0x41,
"KeyF8", // = 0x42,
"KeyF9", // = 0x43,
"KeyF10", // = 0x44,
"KeyNumLock", // = 0x45,
"KeyScrollLock", // = 0x46,
"KeyKeypad7", // = 0x47,
"KeyKeypad8", // = 0x48,
"KeyKeypad9", // = 0x49,
"KeyKeypadMinus", // = 0x4a,
"KeyKeypad4", // = 0x4b,
"KeyKeypad5", // = 0x4c,
"KeyKeypad6", // = 0x4d,
"KeyKeypadPlus", // = 0x4e,
"KeyKeypad1", // = 0x4f,
"KeyKeypad2", // = 0x50,
"KeyKeypad3", // = 0x51,
"KeyKeypad0", // = 0x52,
"KeyKeypadPeriod", // = 0x53,
"<unknown:54>",
"<unknown:55>",
"<unknown:56>",
"KeyF11", // = 0x57,
"KeyF12", // = 0x58,
"<unknown:59>",
"<unknown:5a>",
"<unknown:5b>",
"<unknown:5c>",
"<unknown:5d>",
"<unknown:5e>",
"<unknown:5f>",
"<unknown:60>",
"<unknown:61>",
"<unknown:62>",
"<unknown:63>",
"<unknown:64>",
"<unknown:65>",
"<unknown:66>",
"<unknown:67>",
"<unknown:68>",
"<unknown:69>",
"<unknown:6a>",
"<unknown:6b>",
"<unknown:6c>",
"<unknown:6d>",
"<unknown:6e>",
"<unknown:6f>",
"<unknown:70>",
"<unknown:71>",
"<unknown:72>",
"<unknown:73>",
"<unknown:74>",
"<unknown:75>",
"<unknown:76>",
"<unknown:77>",
"<unknown:78>",
"<unknown:79>",
"<unknown:7a>",
"<unknown:7b>",
"<unknown:7c>",
"<unknown:7d>",
"<unknown:7e>",
"<unknown:7f>",
"<unknown:80>",
"<unknown:81>",
"<unknown:82>",
"<unknown:83>",
"<unknown:84>",
"<unknown:85>",
"<unknown:86>",
"<unknown:87>",
"<unknown:88>",
"<unknown:89>",
"<unknown:8a>",
"<unknown:8b>",
"<unknown:8c>",
"<unknown:8d>",
"<unknown:8e>",
"<unknown:8f>",
"KeyMediaPrev", // = 0x90,
"<unknown:91>",
"<unknown:92>",
"<unknown:93>",
"<unknown:94>",
"<unknown:95>",
"<unknown:96>",
"<unknown:97>",
"<unknown:98>",
"KeyMediaNext", // = 0x99,
"<unknown:9a>",
"<unknown:9b>",
"KeyKeypadEnter", // = 0x9c,
"KeyRightControl", // = 0x9d,
"<unknown:9e>",
"<unknown:9f>",
"KeyMediaMute", // = 0xa0,
"KeyMediaCalculator", // = 0xa1,
"KeyMediaPlay", // = 0xa2,
"<unknown:a3>",
"KeyMediaStop", // = 0xa4,
"<unknown:a5>",
"<unknown:a6>",
"<unknown:a7>",
"<unknown:a8>",
"<unknown:a9>",
"<unknown:aa>",
"<unknown:ab>",
"<unknown:ac>",
"<unknown:ad>",
"KeyMediaVolumeDown", // = 0xae,
"<unknown:af>",
"KeyMediaVolumeUp", // = 0xb0,
"<unknown:b1>",
"KeyMediaWww", // = 0xb2,
"<unknown:b3>",
"<unknown:b4>",
"KeyKeypadDivide", // = 0xb5,
"<unknown:b6>",
"<unknown:b7>",
"KeyRightAlt", // = 0xb8,
"<unknown:b9>",
"<unknown:ba>",
"<unknown:bb>",
"<unknown:bc>",
"<unknown:bd>",
"<unknown:be>",
"<unknown:bf>",
"<unknown:c0>",
"<unknown:c1>",
"<unknown:c2>",
"<unknown:c3>",
"<unknown:c4>",
"<unknown:c5>",
"<unknown:c6>",
"KeyHome", // = 0xc7,
"KeyUp", // = 0xc8,
"KeyPageUp", // = 0xc9,
"<unknown:ca>",
"KeyLeft", // = 0xcb,
"<unknown:cc>",
"KeyRight", // = 0xcd,
"<unknown:ce>",
"KeyEnd", // = 0xcf,
"KeyDown", // = 0xd0,
"KeyPageDown", // = 0xd1,
"KeyInsert", // = 0xd2,
"KeyDelete", // = 0xd3,
"<unknown:d4>",
"<unknown:d5>",
"<unknown:d6>",
"<unknown:d7>",
"<unknown:d8>",
"<unknown:d9>",
"<unknown:da>",
"KeyLeftGui", // = 0xdb,
"KeyRightGui", // = 0xdc,
"KeyApps", // = 0xdd,
"KeyAcpiPower", // = 0xde,
"KeyAcpiSleep", // = 0xdf,
"<unknown:e0>",
"<unknown:e1>",
"<unknown:e2>",
"KeyAcpiWake", // = 0xe3,
"<unknown:e4>",
"KeyMediaWwwSearch", // = 0xe5,
"KeyMediaWwwFavorites", // = 0xe6,
"KeyMediaWwwRefresh", // = 0xe7,
"KeyMediaWwwStop", // = 0xe8,
"KeyMediaWwwForward", // = 0xe9,
"KeyMediaWwwBack", // = 0xea,
"KeyMediaMyComputer", // = 0xeb,
"KeyMediaEmail", // = 0xec,
"KeyMediaSelect", // = 0xed,
"<unknown:ee>",
"<unknown:ef>",
"<unknown:f0>",
"<unknown:f1>",
"<unknown:f2>",
"<unknown:f3>",
"<unknown:f4>",
"<unknown:f5>",
"<unknown:f6>",
"<unknown:f7>",
"<unknown:f8>",
"<unknown:f9>",
"<unknown:fa>",
"<unknown:fb>",
"<unknown:fc>",
"<unknown:fd>",
"KeyPrint", // = 0xfe,
"KeyPause", // = 0xff,
};
void keyboardIsr() {
keyboard.Isr();
}
Keyboard::Keyboard() {
// TODO
}
Keyboard::~Keyboard() {
// TODO
}
Keyboard::KeyHandleFunction Keyboard::setKeyUpHandler(KeyHandleFunction handler) {
auto oldHandler = _keyUpHandler;
_keyUpHandler = handler;
return oldHandler;
}
Keyboard::KeyHandleFunction Keyboard::setKeyDownHandler(KeyHandleFunction handler) {
auto oldHandler = _keyDownHandler;
_keyDownHandler = handler;
return oldHandler;
}
Keyboard::KeyHandleFunction Keyboard::setKeyRepeatHandler(KeyHandleFunction handler) {
auto oldHandler = _keyRepeatHandler;
_keyRepeatHandler = handler;
return oldHandler;
}
const char *Keyboard::getKeyName(unsigned char keyCode) {
return keyNames[keyCode];
}
void Keyboard::Isr() {
// TODO
}
void Keyboard::keyDown(SDL_Keycode keyCode) {
auto c = mapSdlKeycode(keyCode);
if (keyState[c]) {
if (_keyRepeatHandler) _keyRepeatHandler(c);
} else {
keyState[c] = true;
if (_keyDownHandler) _keyDownHandler(c);
}
}
void Keyboard::keyUp(SDL_Keycode keyCode) {
auto c = mapSdlKeycode(keyCode);
keyState[c] = false;
if (_keyUpHandler) _keyUpHandler(c);
}