Skip to content

Commit

Permalink
Add modifier to SDL key event (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Mar 26, 2021
1 parent 26ea032 commit 5deef26
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* Send low-level window closing event even if game uses SDL (#395)
* snd_pcm_writei() should block until all frames can be played
* Fix controller inputs when controller window has focus
* Add modifier to SDL key event (#405)

## [1.4.1] - 2021-01-02
### Added
Expand Down
2 changes: 2 additions & 0 deletions src/library/inputs/inputevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void generateKeyUpEvents(void)

SDL_Keysym keysym;
xkeysymToSDL(&keysym, old_game_ai.keyboard[i]);
keysym.mod = xkeyboardToSDLMod(game_ai.keyboard);
event2.key.keysym = keysym;

sdlEventQueue.insert(&event2);
Expand Down Expand Up @@ -232,6 +233,7 @@ void generateKeyDownEvents(void)

SDL_Keysym keysym;
xkeysymToSDL(&keysym, game_ai.keyboard[i]);
keysym.mod = xkeyboardToSDLMod(game_ai.keyboard);
event2.key.keysym = keysym;

sdlEventQueue.insert(&event2);
Expand Down
37 changes: 35 additions & 2 deletions src/library/inputs/keyboard_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "keyboard_helper.h"
#include "sdlkeyboardlayout.h"
#include <string.h>
#include <array>
//#include <X11/Xlib.h>
#include "../../external/keysymdef.h"

Expand Down Expand Up @@ -328,15 +329,47 @@ void xkeyboardToSDL1keyboard(const std::array<unsigned int,AllInputs::MAXKEYS>&
void xkeysymToSDL(SDL_Keysym *keysym, unsigned int xkeysym) {
keysym->sym = X11_TranslateKeysym(xkeysym);
keysym->scancode = GetScanFromKey(keysym->sym);
keysym->mod = KMOD_NONE; /* TODO: Add the modifier */
keysym->unused = 0;
}

void xkeysymToSDL1(SDL1::SDL_keysym *keysym, unsigned int xkeysym) {
keysym->sym = X11_Translate1Keysym(xkeysym);
keysym->scancode = GetScanFromKey1(keysym->sym);
keysym->mod = KMOD_NONE; /* TODO: Add the modifier */
keysym->unicode = 0;
}

struct ModTranslate {
unsigned int xmod;
SDL_Keymod sdlmod;
};

static std::array<ModTranslate, 10> mod_translate {{
{XK_Shift_L, KMOD_LSHIFT},
{XK_Shift_R, KMOD_RSHIFT},
{XK_Control_L, KMOD_LCTRL},
{XK_Control_R, KMOD_RCTRL},
{XK_Meta_L, KMOD_LGUI},
{XK_Meta_R, KMOD_RGUI},
{XK_Alt_L, KMOD_LALT},
{XK_Alt_R, KMOD_RALT},
{XK_Caps_Lock, KMOD_CAPS},
{XK_Shift_Lock, KMOD_NUM},
}};

SDL_Keymod xkeyboardToSDLMod(const std::array<unsigned int,AllInputs::MAXKEYS>& Xkeyboard) {
unsigned int mod = KMOD_NONE; // use int because "bitwise or" promotes to int
for (int i=0; i<AllInputs::MAXKEYS; i++) {
if (Xkeyboard[i]) {
for (int j=0; i<10; j++) {
if (Xkeyboard[i] == mod_translate[j].xmod) {
mod |= mod_translate[j].sdlmod;
break;
}
}
}
}

return static_cast<SDL_Keymod>(mod);
}

}
3 changes: 3 additions & 0 deletions src/library/inputs/keyboard_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void xkeyboardToSDL1keyboard(const std::array<unsigned int,AllInputs::MAXKEYS>&
void xkeysymToSDL(SDL_Keysym *keysym, unsigned int xkeysym);
void xkeysymToSDL1(SDL1::SDL_keysym *keysym, unsigned int xkeysym);

/* Build the SDL2 modifier from the keyboard input */
SDL_Keymod xkeyboardToSDLMod(const std::array<unsigned int,AllInputs::MAXKEYS>& Xkeyboard);

}

#endif
11 changes: 11 additions & 0 deletions src/library/inputs/sdlkeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ static Uint8 SDL1_keyboard[SDL1::SDLK_LAST] = {0};
return sdl::gameSDLWindow;
}

/* Override */ SDL_Keymod SDL_GetModState(void)
{
DEBUGLOGCALL(LCF_SDL | LCF_KEYBOARD);
return xkeyboardToSDLMod(game_ai.keyboard);
}

/* Override */ void SDL_SetModState(SDL_Keymod modstate)
{
DEBUGLOGCALL(LCF_SDL | LCF_KEYBOARD | LCF_TODO);
}

}
2 changes: 2 additions & 0 deletions src/library/inputs/sdlkeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace libtas {
OVERRIDE const Uint8* SDL_GetKeyboardState(int* numkeys); // SDL 2
OVERRIDE Uint8* SDL_GetKeyState(int* numkeys); // SDL 1
OVERRIDE SDL_Window* SDL_GetKeyboardFocus(void);
OVERRIDE SDL_Keymod SDL_GetModState(void);
OVERRIDE void SDL_SetModState(SDL_Keymod modstate);

}

Expand Down

0 comments on commit 5deef26

Please sign in to comment.