2017-02-26 16:00:41 +00:00
|
|
|
#include "sdl-keyboard.h"
|
|
|
|
|
|
|
|
#include "sdl-paddles.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
SDLKeyboard::SDLKeyboard(VMKeyboard *k) : PhysicalKeyboard(k)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLKeyboard::~SDLKeyboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int8_t actualKey;
|
|
|
|
bool shifted;
|
|
|
|
} keymapChar;
|
|
|
|
|
|
|
|
void SDLKeyboard::handleKeypress(SDL_KeyboardEvent *key)
|
|
|
|
{
|
|
|
|
bool releaseEvent = key->type == SDL_KEYUP;
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
if (key->type == SDL_KEYDOWN &&
|
|
|
|
key->keysym.sym == SDLK_F10) {
|
|
|
|
// Invoke BIOS
|
|
|
|
g_biosInterrupt = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
if ( (key->keysym.sym >= 'a' && key->keysym.sym <= 'z') ||
|
|
|
|
(key->keysym.sym >= '0' && key->keysym.sym <= '9') ||
|
|
|
|
key->keysym.sym == '-' ||
|
|
|
|
key->keysym.sym == '=' ||
|
|
|
|
key->keysym.sym == '[' ||
|
|
|
|
key->keysym.sym == '`' ||
|
|
|
|
key->keysym.sym == ']' ||
|
|
|
|
key->keysym.sym == '\\' ||
|
|
|
|
key->keysym.sym == ';' ||
|
|
|
|
key->keysym.sym == '\'' ||
|
|
|
|
key->keysym.sym == ',' ||
|
|
|
|
key->keysym.sym == '.' ||
|
|
|
|
key->keysym.sym == '/' ||
|
|
|
|
key->keysym.sym == ' ' ||
|
|
|
|
key->keysym.sym == 27 || // ESC
|
|
|
|
key->keysym.sym == 13 || // return
|
|
|
|
key->keysym.sym == 9) { // tab
|
|
|
|
|
|
|
|
// Simple keypresses
|
|
|
|
if (key->keysym.mod & (KMOD_LCTRL|KMOD_RCTRL)) {
|
|
|
|
key->keysym.sym -= ('a'-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (releaseEvent)
|
|
|
|
vmkeyboard->keyReleased(key->keysym.sym);
|
|
|
|
else
|
|
|
|
vmkeyboard->keyDepressed(key->keysym.sym);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete key
|
|
|
|
if (key->keysym.sym == 8) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_DEL);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_DEL);
|
2017-02-26 16:00:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//modifier handling
|
|
|
|
if (key->keysym.sym == SDLK_CAPSLOCK) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_LOCK);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_LOCK);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->keysym.sym == SDLK_LSHIFT ||
|
|
|
|
key->keysym.sym == SDLK_RSHIFT) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_LSHFT);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_LSHFT);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// arrows
|
|
|
|
if (key->keysym.sym == SDLK_LEFT) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_LARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_LARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
if (key->keysym.sym == SDLK_RIGHT) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_RARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_RARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->keysym.sym == SDLK_LEFT) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_LARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_LARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->keysym.sym == SDLK_UP) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_UARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_UARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->keysym.sym == SDLK_DOWN) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_DARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_DARR);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Paddles
|
|
|
|
if (key->keysym.sym == SDLK_LGUI) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_LA);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_LA);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->keysym.sym == SDLK_RGUI) {
|
|
|
|
if (releaseEvent)
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyReleased(PK_RA);
|
2017-02-26 16:00:41 +00:00
|
|
|
else
|
2018-02-07 15:20:26 +00:00
|
|
|
vmkeyboard->keyDepressed(PK_RA);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SDLKeyboard::maintainKeyboard()
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
if (SDL_PollEvent( &event )) {
|
|
|
|
|
|
|
|
// Handle keydown/keyup (and quit, incidentally)
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
// Don't handle repeats; we have our own repeat code
|
|
|
|
if (event.key.repeat == 0)
|
|
|
|
handleKeypress(&event.key);
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
// We are handling the SDL input loop, so need to pass this off to the paddles. :/
|
|
|
|
// FIXME: nasty rooting around in other objects and typecasting.
|
|
|
|
// FIXME: event.motion.state & SDL_BUTTON_LMASK, et al?
|
|
|
|
|
|
|
|
((SDLPaddles *)g_paddles)->gotMouseMovement(event.motion.x, event.motion.y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 15:20:26 +00:00
|
|
|
|
|
|
|
bool hasKeyPending;
|
|
|
|
uint8_t keyPending;
|
|
|
|
|
|
|
|
bool SDLKeyboard::kbhit()
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2020-12-28 19:23:47 +00:00
|
|
|
if (SDL_PollEvent( &event )) {
|
|
|
|
if (event.type == SDL_QUIT) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == SDL_KEYDOWN) {
|
|
|
|
SDL_KeyboardEvent *key = &event.key;
|
|
|
|
|
|
|
|
if ( (key->keysym.sym >= 'a' && key->keysym.sym <= 'z') ||
|
|
|
|
(key->keysym.sym >= '0' && key->keysym.sym <= '9') ||
|
|
|
|
key->keysym.sym == '-' ||
|
|
|
|
key->keysym.sym == '=' ||
|
|
|
|
key->keysym.sym == '[' ||
|
|
|
|
key->keysym.sym == '`' ||
|
|
|
|
key->keysym.sym == ']' ||
|
|
|
|
key->keysym.sym == '\\' ||
|
|
|
|
key->keysym.sym == ';' ||
|
|
|
|
key->keysym.sym == '\'' ||
|
|
|
|
key->keysym.sym == ',' ||
|
|
|
|
key->keysym.sym == '.' ||
|
|
|
|
key->keysym.sym == '/' ||
|
|
|
|
key->keysym.sym == ' ' ||
|
|
|
|
key->keysym.sym == 27 || // ESC
|
|
|
|
key->keysym.sym == 13 || // return
|
|
|
|
key->keysym.sym == 9) { // tab
|
|
|
|
keyPending = key->keysym.sym;
|
2018-02-07 15:20:26 +00:00
|
|
|
hasKeyPending = true;
|
2020-12-28 19:23:47 +00:00
|
|
|
} else {
|
|
|
|
switch (key->keysym.sym) {
|
|
|
|
case SDLK_UP:
|
|
|
|
keyPending = PK_UARR;
|
|
|
|
hasKeyPending = true;
|
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
|
|
|
keyPending = PK_DARR;
|
|
|
|
hasKeyPending = true;
|
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
keyPending = PK_RARR;
|
|
|
|
hasKeyPending = true;
|
|
|
|
break;
|
|
|
|
case SDLK_LEFT:
|
|
|
|
keyPending = PK_LARR;
|
|
|
|
hasKeyPending = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-07 15:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasKeyPending;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t SDLKeyboard::read()
|
|
|
|
{
|
|
|
|
// Meh
|
|
|
|
hasKeyPending = false;
|
|
|
|
return keyPending;
|
|
|
|
}
|
|
|
|
|
|
|
|
|