From a51474d255b2a607c178ebdf7732716781b3bccf Mon Sep 17 00:00:00 2001 From: Daniel Dressler Date: Sat, 29 Jun 2013 19:15:55 -0700 Subject: [PATCH] Add SDL support for keycode pass through --- base/KeyCodeTranslationFromSDL.h | 32 ++++++++++++++++++++++++++ base/PCMain.cpp | 14 +++++++++++- input/input_state.cpp | 39 ++++++++++++++++++++++++++++++++ input/input_state.h | 3 +++ util/const_map.h | 24 ++++++++++++++++++++ 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 base/KeyCodeTranslationFromSDL.h create mode 100644 util/const_map.h diff --git a/base/KeyCodeTranslationFromSDL.h b/base/KeyCodeTranslationFromSDL.h new file mode 100644 index 0000000000..b9f443bc17 --- /dev/null +++ b/base/KeyCodeTranslationFromSDL.h @@ -0,0 +1,32 @@ +#include "util/const_map.h" +#include + +// TODO: fill out this with a full mapping +static const std::map KeyMapRawSDLtoNative = InitConstMap + (SDLK_p, KEYCODE_P) + (SDLK_o, KEYCODE_O) + (SDLK_i, KEYCODE_I) + (SDLK_u, KEYCODE_U) + (SDLK_y, KEYCODE_Y) + (SDLK_t, KEYCODE_T) + (SDLK_r, KEYCODE_R) + (SDLK_e, KEYCODE_E) + (SDLK_w, KEYCODE_W) + (SDLK_q, KEYCODE_Q) + (SDLK_l, KEYCODE_L) + (SDLK_k, KEYCODE_K) + (SDLK_j, KEYCODE_J) + (SDLK_h, KEYCODE_H) + (SDLK_g, KEYCODE_G) + (SDLK_f, KEYCODE_F) + (SDLK_d, KEYCODE_D) + (SDLK_s, KEYCODE_S) + (SDLK_a, KEYCODE_A) + (SDLK_m, KEYCODE_M) + (SDLK_n, KEYCODE_N) + (SDLK_b, KEYCODE_B) + (SDLK_v, KEYCODE_V) + (SDLK_c, KEYCODE_C) + (SDLK_x, KEYCODE_X) + (SDLK_z, KEYCODE_Z); + diff --git a/base/PCMain.cpp b/base/PCMain.cpp index 74b8d31f77..e9ab582470 100644 --- a/base/PCMain.cpp +++ b/base/PCMain.cpp @@ -14,6 +14,7 @@ #endif #include +#include #ifdef _WIN32 #include "SDL/SDL.h" #include "SDL/SDL_timer.h" @@ -32,9 +33,11 @@ #include "gfx_es2/glsl_program.h" #include "file/zip_read.h" #include "input/input_state.h" +#include "input/keycodes.h" +#include "base/KeyCodeTranslationFromSDL.h" #include "base/NativeApp.h" #include "net/resolve.h" - +#include "util/const_map.h" #if defined(MAEMO) || defined(PANDORA) #define EGL @@ -247,6 +250,7 @@ void LaunchEmail(const char *email_address) InputState input_state; + const int buttonMappings[14] = { #ifdef PANDORA SDLK_PAGEDOWN, //X => cross @@ -532,6 +536,14 @@ int main(int argc, char *argv[]) { if (event.key.keysym.sym == SDLK_ESCAPE) { quitRequested = 1; } + + KeyQueueAttemptTranslatedAdd(input_state.key_queue, + KeyMapRawSDLtoNative, + event.key.keysym.sym); + } else if (event.type == SDL_KEYUP) { + KeyQueueAttemptTranslatedRemove(input_state.key_queue, + KeyMapRawSDLtoNative, + event.key.keysym.sym); } else if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.button.button == SDL_BUTTON_LEFT) { input_state.pointer_x[0] = mx; diff --git a/input/input_state.cpp b/input/input_state.cpp index af84381b56..4846936781 100644 --- a/input/input_state.cpp +++ b/input/input_state.cpp @@ -1,4 +1,5 @@ #include "input_state.h" +#include #define max_queue MAX_KEYQUEUESIZE @@ -54,3 +55,41 @@ void KeyQueueBlank(int queue[]) for (int i = 0; i < max_queue; i++) queue[i] = 0; } + +int AttemptTranslate(const std::map trans_table, int plat_key) +{ + std::map::const_iterator iter; + iter = trans_table.find(plat_key); + if (iter == trans_table.end()) + return 0; + + return iter->second; +} + + +void KeyQueueAttemptTranslatedAdd(int queue[], + const std::map translation_table, + int platform_key) +{ + int key = AttemptTranslate(translation_table, platform_key); + + if (key == 0) { + std::cerr << "Warning: Platform key code translation table missing %d"; + return; + } + + KeyQueueAddKey(queue, key); +} + + +void KeyQueueAttemptTranslatedRemove(int queue[], + const std::map translation_table, + int platform_key) +{ + int key = AttemptTranslate(translation_table, platform_key); + + if (key == 0) + return; + + KeyQueueRemoveKey(queue, key); +} diff --git a/input/input_state.h b/input/input_state.h index fba97a4c02..1bb054bf2a 100644 --- a/input/input_state.h +++ b/input/input_state.h @@ -3,6 +3,7 @@ #include "math/lin/vec3.h" #include "base/mutex.h" #include "base/basictypes.h" +#include enum { PAD_BUTTON_A = 1, @@ -123,3 +124,5 @@ void KeyQueueAddKey(int [], int); void KeyQueueRemoveKey(int [], int); void KeyQueueCopyQueue(int src[], int dst[]); void KeyQueueBlank(int []); +void KeyQueueAttemptTranslatedAdd(int [], const std::map, int); +void KeyQueueAttemptTranslatedRemove(int [], const std::map, int); diff --git a/util/const_map.h b/util/const_map.h new file mode 100644 index 0000000000..6b1fe0e165 --- /dev/null +++ b/util/const_map.h @@ -0,0 +1,24 @@ +#pragma once + +template +class InitConstMap +{ +private: + std::map m_map; +public: + InitConstMap(const T& key, const U& val) + { + m_map[key] = val; + } + + InitConstMap& operator()(const T& key, const U& val) + { + m_map[key] = val; + return *this; + } + + operator std::map() + { + return m_map; + } +};