Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift remap fix #169

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/cap32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ byte *membank_config[8][4];
FILE *pfileObject;
FILE *pfoPrinter;

// Store shifted keys for correct remapping where shift release state is
// different from shift state when pressed. SDL1 scancodes are uint8 values but
// SDL2 has extended scancodes with max value of 284. Future proof this array
// with that number.
static constexpr int kSDL2_Scancode_Max = 284;
static bool shifted_keys[kSDL2_Scancode_Max + 1] = {};

#ifdef DEBUG
dword dwDebugFlag = 0;
FILE *pfoDebug = nullptr;
Expand Down Expand Up @@ -2086,7 +2093,11 @@ int cap32_main (int argc, char **argv)
switch (event.type) {
case SDL_KEYDOWN:
{
dword cpc_key = CPC.InputMapper->CPCkeyFromKeysym(event.key.keysym);
auto shift_state = static_cast<bool>(event.key.keysym.mod & KMOD_SHIFT);
// store shift state of physical keys for remapping if released after shift
shifted_keys[event.key.keysym.scancode] = shift_state;

dword cpc_key = CPC.InputMapper->CPCkeyFromKeysym(event.key.keysym, shift_state);
if (!(cpc_key & MOD_EMU_KEY)) {
applyKeypress(cpc_key, keyboard_matrix, true);
}
Expand All @@ -2095,7 +2106,12 @@ int cap32_main (int argc, char **argv)

case SDL_KEYUP:
{
dword cpc_key = CPC.InputMapper->CPCkeyFromKeysym(event.key.keysym);
bool shift_state = static_cast<bool>(event.key.keysym.mod & KMOD_SHIFT);
auto scancode = event.key.keysym.scancode;
if (shift_state != shifted_keys[scancode]) {
shift_state = shifted_keys[scancode];
}
dword cpc_key = CPC.InputMapper->CPCkeyFromKeysym(event.key.keysym, shift_state);
if (!(cpc_key & MOD_EMU_KEY)) {
applyKeypress(cpc_key, keyboard_matrix, false);
}
Expand Down
12 changes: 6 additions & 6 deletions src/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,14 +1402,14 @@ void InputMapper::init()
}
}

dword InputMapper::CPCkeyFromKeysym(SDL_keysym keysym) {
dword InputMapper::CPCkeyFromKeysym(SDL_keysym keysym, bool apply_shift /*= false*/) {
dword sdl_key = keysym.sym;

if (keysym.mod & KMOD_SHIFT) sdl_key |= MOD_PC_SHIFT;
if (keysym.mod & KMOD_CTRL) sdl_key |= MOD_PC_CTRL;
if (keysym.mod & KMOD_MODE) sdl_key |= MOD_PC_MODE;
if (keysym.mod & KMOD_META) sdl_key |= MOD_PC_META;
if (keysym.mod & KMOD_ALT) sdl_key |= MOD_PC_ALT;
if (apply_shift) sdl_key |= MOD_PC_SHIFT;
if (keysym.mod & KMOD_CTRL) sdl_key |= MOD_PC_CTRL;
if (keysym.mod & KMOD_MODE) sdl_key |= MOD_PC_MODE;
if (keysym.mod & KMOD_META) sdl_key |= MOD_PC_META;
if (keysym.mod & KMOD_ALT) sdl_key |= MOD_PC_ALT;
// Ignore sticky modifiers (MOD_PC_NUM and MOD_PC_CAPS)

auto cpc_key = CPCkeysFromSDLkeysym.find(sdl_key);
Expand Down
4 changes: 2 additions & 2 deletions src/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ class InputMapper {

public:
InputMapper(t_CPC *CPC);
bool load_layout(const std::string& filename);
bool load_layout(const std::string& filename);
void init();
dword CPCkeyFromKeysym(SDL_keysym keysym);
dword CPCkeyFromKeysym(SDL_keysym keysym, bool apply_shift = false);
dword CPCkeyFromJoystickButton(SDL_JoyButtonEvent jbutton);
void CPCkeyFromJoystickAxis(SDL_JoyAxisEvent jaxis, dword *cpc_key, bool &release);
std::list<SDL_Event> StringToEvents(std::string toTranslate);
Expand Down
6 changes: 3 additions & 3 deletions test/InputMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ TEST_F(InputMapperTest, Keymapping)
// Exclaim
keysym.sym = SDLK_1;
keysym.mod = KMOD_LSHIFT;
ASSERT_EQ(0x80 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym));
ASSERT_EQ(0x80 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym, true));

CPC.kbd_layout ="keymap_uk_linux.map";
CPC.keyboard = 0;
CPC.InputMapper->init();
// Pound
keysym.sym = SDLK_3;
keysym.mod = KMOD_RSHIFT;
ASSERT_EQ(0x30 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym));
ASSERT_EQ(0x30 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym, true));

CPC.kbd_layout ="keymap_fr_win.map";
CPC.keyboard = 1;
Expand All @@ -130,7 +130,7 @@ TEST_F(InputMapperTest, Keymapping)
// N Tilde
keysym.sym = SDLK_WORLD_81;
keysym.mod = KMOD_LSHIFT;
ASSERT_EQ(0x35 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym));
ASSERT_EQ(0x35 | MOD_CPC_SHIFT, CPC.InputMapper->CPCkeyFromKeysym(keysym, true));


}