From 2675d6ea434fdd56534ed0028a0c2ae3347dfeec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 26 May 2023 18:40:13 +0200 Subject: [PATCH] Input event and device enums (#17514) * Switch deviceID from int to enum InputDeviceID, globally * Switch axisId to enum InputAxis * Change int keycodes to InputKeyCode where it makes sense. * SDL input buildfix * SDL keycode buildfix * Switch on enum warning fixes * Qt keycode buildfix * iOS keycode buildfix * UWP keycode buildfix * More iOS buildfix * More iOS buildfix * Update DinputDevice.cpp --- Common/Input/InputState.cpp | 16 ++++----- Common/Input/InputState.h | 64 +++++++++++---------------------- Common/Input/KeyCodes.h | 8 +++-- Common/UI/Root.cpp | 8 +++-- Common/UI/ScrollView.cpp | 4 +++ Common/UI/View.cpp | 12 +++++-- Common/UI/View.h | 9 ++--- Common/VR/PPSSPPVR.cpp | 12 +++---- Core/KeyMap.cpp | 19 +++++----- Core/KeyMap.h | 12 +++---- Core/KeyMapDefaults.cpp | 2 +- Qt/NKCodeFromQt.h | 3 +- Qt/QtMain.cpp | 6 ++-- SDL/NKCodeFromSDL.h | 3 +- SDL/SDLJoystick.cpp | 8 +++-- SDL/SDLJoystick.h | 2 +- SDL/SDLMain.cpp | 2 +- UI/GameSettingsScreen.cpp | 6 ++-- UI/GameSettingsScreen.h | 2 +- UI/NativeApp.cpp | 4 +-- UI/TiltAnalogSettingsScreen.cpp | 1 + UWP/NKCodeFromWindowsSystem.cpp | 4 +-- UWP/NKCodeFromWindowsSystem.h | 2 +- UWP/PPSSPP_UWPMain.cpp | 2 +- Windows/DinputDevice.cpp | 6 ++-- Windows/RawInput.cpp | 8 ++--- Windows/XinputDevice.cpp | 6 ++-- android/jni/app-android.cpp | 12 +++---- ios/SmartKeyboardMap.cpp | 4 +-- ios/SmartKeyboardMap.hpp | 7 ++-- ios/ViewController.mm | 4 +-- unittest/UnitTest.cpp | 4 +-- 32 files changed, 129 insertions(+), 133 deletions(-) diff --git a/Common/Input/InputState.cpp b/Common/Input/InputState.cpp index 3db6dc137c..b12507d3c9 100644 --- a/Common/Input/InputState.cpp +++ b/Common/Input/InputState.cpp @@ -39,7 +39,7 @@ std::vector confirmKeys; std::vector cancelKeys; std::vector tabLeftKeys; std::vector tabRightKeys; -static std::unordered_map uiFlipAnalogY; +static std::unordered_map uiFlipAnalogY; static void AppendKeys(std::vector &keys, const std::vector &newKeys) { for (auto iter = newKeys.begin(); iter != newKeys.end(); ++iter) { @@ -69,11 +69,11 @@ void SetTabLeftRightKeys(const std::vector &tabLeft, const std::ve tabRightKeys = tabRight; } -void SetAnalogFlipY(std::unordered_map flipYByDeviceId) { +void SetAnalogFlipY(std::unordered_map flipYByDeviceId) { uiFlipAnalogY = flipYByDeviceId; } -int GetAnalogYDirection(int deviceId) { +int GetAnalogYDirection(InputDeviceID deviceId) { auto configured = uiFlipAnalogY.find(deviceId); if (configured != uiFlipAnalogY.end()) return configured->second; @@ -84,8 +84,8 @@ int GetAnalogYDirection(int deviceId) { InputMapping InputMapping::FromConfigString(const std::string &str) { std::vector parts; SplitString(str, '-', parts); - int deviceId = atoi(parts[0].c_str()); - int keyCode = atoi(parts[1].c_str()); + InputDeviceID deviceId = (InputDeviceID)(atoi(parts[0].c_str())); + InputKeyCode keyCode = (InputKeyCode)atoi(parts[1].c_str()); InputMapping mapping; mapping.deviceId = deviceId; @@ -94,15 +94,15 @@ InputMapping InputMapping::FromConfigString(const std::string &str) { } std::string InputMapping::ToConfigString() const { - return StringFromFormat("%d-%d", deviceId, keyCode); + return StringFromFormat("%d-%d", (int)deviceId, keyCode); } void InputMapping::FormatDebug(char *buffer, size_t bufSize) const { if (IsAxis()) { int direction; int axis = Axis(&direction); - snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", deviceId, axis, direction); + snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", (int)deviceId, axis, direction); } else { - snprintf(buffer, bufSize, "Device: %d Key: %d", deviceId, keyCode); + snprintf(buffer, bufSize, "Device: %d Key: %d", (int)deviceId, keyCode); } } diff --git a/Common/Input/InputState.h b/Common/Input/InputState.h index 0d71bf5c3c..840a8ae1d4 100644 --- a/Common/Input/InputState.h +++ b/Common/Input/InputState.h @@ -12,7 +12,7 @@ // Default device IDs -enum { +enum InputDeviceID { DEVICE_ID_ANY = -1, // Represents any device ID DEVICE_ID_DEFAULT = 0, // Old Android DEVICE_ID_KEYBOARD = 1, // PC keyboard, android keyboards @@ -38,43 +38,15 @@ enum { DEVICE_ID_TOUCH = 42, }; +inline InputDeviceID operator +(InputDeviceID deviceID, int addend) { + return (InputDeviceID)((int)deviceID + addend); +} + //number of contiguous generic joypad IDs const int MAX_NUM_PADS = 10; const char *GetDeviceName(int deviceId); -enum { - PAD_BUTTON_A = 1, - PAD_BUTTON_B = 2, - PAD_BUTTON_X = 4, - PAD_BUTTON_Y = 8, - PAD_BUTTON_LBUMPER = 16, - PAD_BUTTON_RBUMPER = 32, - PAD_BUTTON_START = 64, - PAD_BUTTON_SELECT = 128, - PAD_BUTTON_UP = 256, - PAD_BUTTON_DOWN = 512, - PAD_BUTTON_LEFT = 1024, - PAD_BUTTON_RIGHT = 2048, - - PAD_BUTTON_MENU = 4096, - PAD_BUTTON_BACK = 8192, - - // For Qt - PAD_BUTTON_JOY_UP = 1<<14, - PAD_BUTTON_JOY_DOWN = 1<<15, - PAD_BUTTON_JOY_LEFT = 1<<16, - PAD_BUTTON_JOY_RIGHT = 1<<17, - - PAD_BUTTON_LEFT_THUMB = 1 << 18, // Click left thumb stick on X360 - PAD_BUTTON_RIGHT_THUMB = 1 << 19, // Click right thumb stick on X360 - - PAD_BUTTON_LEFT_TRIGGER = 1 << 21, // Click left thumb stick on X360 - PAD_BUTTON_RIGHT_TRIGGER = 1 << 22, // Click left thumb stick on X360 - - PAD_BUTTON_FASTFORWARD = 1 << 20, // Click Tab to unthrottle -}; - #ifndef MAX_KEYQUEUESIZE #define MAX_KEYQUEUESIZE 20 #endif @@ -98,18 +70,18 @@ private: return AXIS_BIND_NKCODE_START + axisId * 2 + (direction < 0 ? 1 : 0); } public: - InputMapping() : deviceId(0), keyCode(0) {} + InputMapping() : deviceId(DEVICE_ID_DEFAULT), keyCode(0) {} // From a key mapping - InputMapping(int _deviceId, int key) : deviceId(_deviceId), keyCode(key) {} + InputMapping(InputDeviceID _deviceId, int key) : deviceId(_deviceId), keyCode(key) {} // From an axis - InputMapping(int _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) { + InputMapping(InputDeviceID _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) { _dbg_assert_(direction != 0); } static InputMapping FromConfigString(const std::string &str); std::string ToConfigString() const; - int deviceId; + InputDeviceID deviceId; int keyCode; // Can also represent an axis with direction, if encoded properly. bool IsAxis() const { @@ -195,15 +167,19 @@ enum { struct KeyInput { KeyInput() {} - KeyInput(int devId, int code, int fl) : deviceId(devId), keyCode(code), flags(fl) {} - int deviceId; - int keyCode; // Android keycodes are the canonical keycodes, everyone else map to them. + KeyInput(InputDeviceID devId, InputKeyCode code, int fl) : deviceId(devId), keyCode(code), flags(fl) {} + KeyInput(InputDeviceID devId, int unicode) : deviceId(devId), unicodeChar(unicode), flags(KEY_CHAR) {} + InputDeviceID deviceId; + union { + InputKeyCode keyCode; // Android keycodes are the canonical keycodes, everyone else map to them. + int unicodeChar; // for KEY_CHAR + }; int flags; }; struct AxisInput { - int deviceId; - int axisId; // Android axis Ids are the canonical ones. + InputDeviceID deviceId; + InputAxis axisId; float value; }; @@ -219,5 +195,5 @@ void SetConfirmCancelKeys(const std::vector &confirm, const std::v void SetTabLeftRightKeys(const std::vector &tabLeft, const std::vector &tabRight); // 0 means unknown (attempt autodetect), -1 means flip, 1 means original direction. -void SetAnalogFlipY(std::unordered_map flipYByDeviceId); -int GetAnalogYDirection(int deviceId); +void SetAnalogFlipY(std::unordered_map flipYByDeviceId); +int GetAnalogYDirection(InputDeviceID deviceId); diff --git a/Common/Input/KeyCodes.h b/Common/Input/KeyCodes.h index 690d4c2c08..dc2b9897ce 100644 --- a/Common/Input/KeyCodes.h +++ b/Common/Input/KeyCodes.h @@ -1,6 +1,7 @@ #pragma once -typedef enum _keycode_t { +// These mostly match Android keycodes. +enum InputKeyCode { NKCODE_BUTTON_CROSS = 23, // trackpad or X button(Xperia Play) is pressed NKCODE_BUTTON_CROSS_PS3 = 96, // PS3 X button is pressed NKCODE_BUTTON_CIRCLE = 1004, // Special custom keycode generated from 'O' button by our java code. Or 'O' button if Alt is pressed (TODO) @@ -259,9 +260,10 @@ typedef enum _keycode_t { NKCODE_EXT_ROTATION_RIGHT = 1114, NKCODE_MAX -} keycode_t; +}; -enum AndroidJoystickAxis { +// These mostly match Android axis IDs. +enum InputAxis { // Field descriptor #15 I JOYSTICK_AXIS_X = 0, JOYSTICK_AXIS_Y = 1, diff --git a/Common/UI/Root.cpp b/Common/UI/Root.cpp index ccd1f67352..cb72afaa13 100644 --- a/Common/UI/Root.cpp +++ b/Common/UI/Root.cpp @@ -175,8 +175,8 @@ static int frameCount; // completely broken input where the original keypresses have deviceId = 10 and the repeats // have deviceId = 0. struct HeldKey { - int key; - int deviceId; + InputKeyCode key; + InputDeviceID deviceId; double triggerTime; // Ignores startTime @@ -250,6 +250,8 @@ bool KeyEvent(const KeyInput &key, ViewGroup *root) { case NKCODE_VOLUME_MUTE: retval = false; break; + default: + break; } return retval; @@ -318,7 +320,7 @@ bool AxisEvent(const AxisInput &axis, ViewGroup *root) { // Cannot use the remapper since this is for the menu, so we provide our own // axis->button emulation here. - auto GenerateKeyFromAxis = [&](DirState old, DirState cur, keycode_t neg_key, keycode_t pos_key) { + auto GenerateKeyFromAxis = [&](DirState old, DirState cur, InputKeyCode neg_key, InputKeyCode pos_key) { if (old == cur) return; if (old == DirState::POS) { diff --git a/Common/UI/ScrollView.cpp b/Common/UI/ScrollView.cpp index 991f1c25db..25bad50c10 100644 --- a/Common/UI/ScrollView.cpp +++ b/Common/UI/ScrollView.cpp @@ -112,6 +112,8 @@ bool ScrollView::Key(const KeyInput &input) { case DEVICE_ID_XR_CONTROLLER_RIGHT: scrollSpeed = 50; break; + default: + break; } if (input.flags & KEY_DOWN) { @@ -127,6 +129,8 @@ bool ScrollView::Key(const KeyInput &input) { case NKCODE_EXT_MOUSEWHEEL_DOWN: ScrollRelative(scrollSpeed); break; + default: + break; } } return ViewGroup::Key(input); diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index d8fa404447..f9a232ecd8 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -349,7 +349,7 @@ bool Clickable::Key(const KeyInput &key) { down_ = false; ret = true; } - } else if (IsEscapeKey(key)) { + } else if (down_ && IsEscapeKey(key)) { down_ = false; } } @@ -1190,6 +1190,8 @@ bool TextEdit::Key(const KeyInput &input) { case NKCODE_BACK: case NKCODE_ESCAPE: return false; + default: + break; } if (ctrlDown_) { @@ -1227,6 +1229,8 @@ bool TextEdit::Key(const KeyInput &input) { case NKCODE_Z: text_ = undo_; break; + default: + break; } } @@ -1244,6 +1248,8 @@ bool TextEdit::Key(const KeyInput &input) { case NKCODE_CTRL_RIGHT: ctrlDown_ = false; break; + default: + break; } } @@ -1369,7 +1375,7 @@ bool Slider::Key(const KeyInput &input) { } } -bool Slider::ApplyKey(int keyCode) { +bool Slider::ApplyKey(InputKeyCode keyCode) { switch (keyCode) { case NKCODE_DPAD_LEFT: case NKCODE_MINUS: @@ -1497,7 +1503,7 @@ bool SliderFloat::Key(const KeyInput &input) { } } -bool SliderFloat::ApplyKey(int keyCode) { +bool SliderFloat::ApplyKey(InputKeyCode keyCode) { switch (keyCode) { case NKCODE_DPAD_LEFT: case NKCODE_MINUS: diff --git a/Common/UI/View.h b/Common/UI/View.h index f857d837ae..a683cb9474 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -19,6 +19,7 @@ #include "Common/Math/lin/matrix4x4.h" #include "Common/Math/math_util.h" #include "Common/Math/geom2d.h" +#include "Common/Input/KeyCodes.h" #include "Common/Common.h" @@ -619,7 +620,7 @@ public: Event OnChange; private: - bool ApplyKey(int keyCode); + bool ApplyKey(InputKeyCode keyCode); int *value_; bool showPercent_; @@ -629,7 +630,7 @@ private: float paddingRight_; int step_; int repeat_ = 0; - int repeatCode_ = 0; + InputKeyCode repeatCode_ = NKCODE_UNKNOWN; }; class SliderFloat : public Clickable { @@ -649,7 +650,7 @@ public: Event OnChange; private: - bool ApplyKey(int keyCode); + bool ApplyKey(InputKeyCode keyCode); float *value_; float minValue_; @@ -657,7 +658,7 @@ private: float paddingLeft_; float paddingRight_; int repeat_; - int repeatCode_ = 0; + InputKeyCode repeatCode_ = NKCODE_UNKNOWN; }; // Basic button that modifies a bitfield based on the pressed status. Supports multitouch. diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index 99e8d311bf..f4a4000a17 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -46,7 +46,7 @@ enum VRMirroring { static VRAppMode appMode = VR_MENU_MODE; static std::map > pspAxis; -static std::map pspKeys; +static std::map pspKeys; // key can be virtual, so not using the enum. static int vr3DGeometryCount = 0; static long vrCompat[VR_COMPAT_MAX]; @@ -69,11 +69,11 @@ VR button mapping struct ButtonMapping { ovrButton ovr; - int keycode; + InputKeyCode keycode; bool pressed; int repeat; - ButtonMapping(int keycode, ovrButton ovr) { + ButtonMapping(InputKeyCode keycode, ovrButton ovr) { this->keycode = keycode; this->ovr = ovr; pressed = false; @@ -106,7 +106,7 @@ static std::vector rightControllerMapping = { ButtonMapping(NKCODE_ENTER, ovrButton_Trigger), }; -static const int controllerIds[] = {DEVICE_ID_XR_CONTROLLER_LEFT, DEVICE_ID_XR_CONTROLLER_RIGHT}; +static const InputDeviceID controllerIds[] = {DEVICE_ID_XR_CONTROLLER_LEFT, DEVICE_ID_XR_CONTROLLER_RIGHT}; static std::vector controllerMapping[2] = { leftControllerMapping, rightControllerMapping @@ -223,7 +223,7 @@ void SetVRAppMode(VRAppMode mode) { void UpdateVRInput(bool haptics, float dp_xscale, float dp_yscale) { //axis - if (pspKeys[VIRTKEY_VR_CAMERA_ADJUST]) { + if (pspKeys[(int)VIRTKEY_VR_CAMERA_ADJUST]) { AxisInput axis = {}; for (int j = 0; j < 2; j++) { XrVector2f joystick = IN_VRGetJoystickState(j); @@ -515,7 +515,7 @@ bool UpdateVRKeys(const KeyInput &key) { pspKeys[VIRTKEY_VR_CAMERA_ADJUST] = false; for (auto& pspKey : pspKeys) { if (pspKey.second) { - keyUp.keyCode = pspKey.first; + keyUp.keyCode = (InputKeyCode)pspKey.first; NativeKey(keyUp); } } diff --git a/Core/KeyMap.cpp b/Core/KeyMap.cpp index 0957fed33c..5683e218bb 100644 --- a/Core/KeyMap.cpp +++ b/Core/KeyMap.cpp @@ -45,8 +45,8 @@ KeyMapping g_controllerMap; // Incremented on modification, so we know when to update menus. int g_controllerMapGeneration = 0; std::set g_seenPads; -std::map g_padNames; -std::set g_seenDeviceIds; +std::map g_padNames; +std::set g_seenDeviceIds; // Utility for UI navigation void SingleInputMappingFromPspButton(int btn, std::vector *mappings, bool ignoreMouse) { @@ -120,8 +120,8 @@ void UpdateNativeMenuKeys() { SetConfirmCancelKeys(confirmKeys, cancelKeys); SetTabLeftRightKeys(tabLeft, tabRight); - std::unordered_map flipYByDeviceId; - for (int deviceId : g_seenDeviceIds) { + std::unordered_map flipYByDeviceId; + for (InputDeviceID deviceId : g_seenDeviceIds) { auto analogs = MappedAxesForDevice(deviceId); flipYByDeviceId[deviceId] = analogs.leftY.direction; } @@ -441,6 +441,7 @@ const KeyMap_IntStrPair psp_button_names[] = { {CTRL_NOTE, "Note"}, }; +// key here can be other things than InputKeyCode. static std::string FindName(int key, const KeyMap_IntStrPair list[], size_t size) { for (size_t i = 0; i < size; i++) { if (list[i].key == key) @@ -449,7 +450,7 @@ static std::string FindName(int key, const KeyMap_IntStrPair list[], size_t size return StringFromFormat("%02x?", key); } -std::string GetKeyName(int keyCode) { +std::string GetKeyName(InputKeyCode keyCode) { return FindName(keyCode, key_names, ARRAY_SIZE(key_names)); } @@ -532,7 +533,7 @@ bool PspButtonHasMappings(int btn) { return !iter->second.empty(); } -MappedAnalogAxes MappedAxesForDevice(int deviceId) { +MappedAnalogAxes MappedAxesForDevice(InputDeviceID deviceId) { MappedAnalogAxes result{}; // Find the axisId mapped for a specific virtual button. @@ -578,7 +579,7 @@ void RemoveButtonMapping(int btn) { } } -bool IsKeyMapped(int device, int key) { +bool IsKeyMapped(InputDeviceID device, int key) { std::lock_guard guard(g_controllerMapLock); for (auto &iter : g_controllerMap) { for (auto &mappedKey : iter.second) { @@ -778,7 +779,7 @@ bool HasBuiltinController(const std::string &name) { return IsOuya(name) || IsXperiaPlay(name) || IsNvidiaShield(name) || IsMOQII7S(name) || IsRetroid(name); } -void NotifyPadConnected(int deviceId, const std::string &name) { +void NotifyPadConnected(InputDeviceID deviceId, const std::string &name) { g_seenPads.insert(name); g_padNames[deviceId] = name; } @@ -814,7 +815,7 @@ const std::set &GetSeenPads() { return g_seenPads; } -std::string PadName(int deviceId) { +std::string PadName(InputDeviceID deviceId) { auto it = g_padNames.find(deviceId); if (it != g_padNames.end()) return it->second; diff --git a/Core/KeyMap.h b/Core/KeyMap.h index 41c2e1c403..660c907652 100644 --- a/Core/KeyMap.h +++ b/Core/KeyMap.h @@ -155,7 +155,7 @@ namespace KeyMap { // Once the multimappings are inserted here, they must not be empty. // If one would be, delete the whole entry from the map instead. // This is automatically handled by SetInputMapping. - extern std::set g_seenDeviceIds; + extern std::set g_seenDeviceIds; extern int g_controllerMapGeneration; // Key & Button names struct KeyMap_IntStrPair { @@ -164,7 +164,7 @@ namespace KeyMap { }; // Use if you need to display the textual name - std::string GetKeyName(int keyCode); + std::string GetKeyName(InputKeyCode keyCode); std::string GetKeyOrAxisName(const InputMapping &mapping); std::string GetAxisName(int axisId); std::string GetPspButtonName(int btn); @@ -189,7 +189,7 @@ namespace KeyMap { // Return false if bind was a duplicate and got removed bool ReplaceSingleKeyMapping(int btn, int index, MultiInputMapping key); - MappedAnalogAxes MappedAxesForDevice(int deviceId); + MappedAnalogAxes MappedAxesForDevice(InputDeviceID deviceId); void LoadFromIni(IniFile &iniFile); void SaveToIni(IniFile &iniFile); @@ -202,7 +202,7 @@ namespace KeyMap { void UpdateNativeMenuKeys(); - void NotifyPadConnected(int deviceId, const std::string &name); + void NotifyPadConnected(InputDeviceID deviceId, const std::string &name); bool IsNvidiaShield(const std::string &name); bool IsNvidiaShieldTV(const std::string &name); bool IsXperiaPlay(const std::string &name); @@ -212,10 +212,10 @@ namespace KeyMap { bool HasBuiltinController(const std::string &name); const std::set &GetSeenPads(); - std::string PadName(int deviceId); + std::string PadName(InputDeviceID deviceId); void AutoConfForPad(const std::string &name); - bool IsKeyMapped(int device, int key); + bool IsKeyMapped(InputDeviceID device, int key); bool HasChanged(int &prevGeneration); } diff --git a/Core/KeyMapDefaults.cpp b/Core/KeyMapDefaults.cpp index 63c4294305..3c08f2c94c 100644 --- a/Core/KeyMapDefaults.cpp +++ b/Core/KeyMapDefaults.cpp @@ -338,7 +338,7 @@ static const DefMappingStruct defaultVRRightController[] = { {VIRTKEY_AXIS_X_MAX, NKCODE_DPAD_RIGHT}, }; -static void SetDefaultKeyMap(int deviceId, const DefMappingStruct *array, size_t count, bool replace) { +static void SetDefaultKeyMap(InputDeviceID deviceId, const DefMappingStruct *array, size_t count, bool replace) { for (size_t i = 0; i < count; i++) { if (array[i].direction == 0) SetInputMapping(array[i].pspKey, MultiInputMapping(InputMapping(deviceId, array[i].keyOrAxis)), replace); diff --git a/Qt/NKCodeFromQt.h b/Qt/NKCodeFromQt.h index b53b09ffff..dfc67beb0d 100644 --- a/Qt/NKCodeFromQt.h +++ b/Qt/NKCodeFromQt.h @@ -1,10 +1,11 @@ #pragma once #include "Common/Data/Collections/ConstMap.h" +#include "Common/Input/KeyCodes.h" #include // TODO: Add any missing keys -static const std::map KeyMapRawQttoNative = InitConstMap +static const std::map KeyMapRawQttoNative = InitConstMap (Qt::Key_P, NKCODE_P) (Qt::Key_O, NKCODE_O) (Qt::Key_I, NKCODE_I) diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 5d65a83a0f..288e2cce42 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -633,7 +633,7 @@ bool MainUI::event(QEvent *e) { { auto qtKeycode = ((QKeyEvent*)e)->key(); auto iter = KeyMapRawQttoNative.find(qtKeycode); - int nativeKeycode = 0; + InputKeyCode nativeKeycode = NKCODE_UNKNOWN; if (iter != KeyMapRawQttoNative.end()) { nativeKeycode = iter->second; NativeKey(KeyInput(DEVICE_ID_KEYBOARD, nativeKeycode, KEY_DOWN)); @@ -652,8 +652,8 @@ bool MainUI::event(QEvent *e) { default: if (str.size()) { int pos = 0; - int code = u8_nextchar(str.c_str(), &pos); - NativeKey(KeyInput(DEVICE_ID_KEYBOARD, code, KEY_CHAR)); + int unicode = u8_nextchar(str.c_str(), &pos); + NativeKey(KeyInput(DEVICE_ID_KEYBOARD, unicode)); } break; } diff --git a/SDL/NKCodeFromSDL.h b/SDL/NKCodeFromSDL.h index bd184eef01..bcc13ff367 100644 --- a/SDL/NKCodeFromSDL.h +++ b/SDL/NKCodeFromSDL.h @@ -1,10 +1,11 @@ #pragma once #include "Common/Data/Collections/ConstMap.h" +#include "Common/Input/KeyCodes.h" #include // TODO: Add any missing keys -static const std::map KeyMapRawSDLtoNative = InitConstMap +static const std::map KeyMapRawSDLtoNative = InitConstMap (SDLK_UNKNOWN, NKCODE_UNKNOWN) (SDLK_p, NKCODE_P) (SDLK_o, NKCODE_O) diff --git a/SDL/SDLJoystick.cpp b/SDL/SDLJoystick.cpp index 2f6b47171d..f51335912f 100644 --- a/SDL/SDLJoystick.cpp +++ b/SDL/SDLJoystick.cpp @@ -88,7 +88,8 @@ void SDLJoystick::setUpController(int deviceIndex) { controllers.push_back(controller); controllerDeviceMap[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller))] = deviceIndex; cout << "found control pad: " << SDL_GameControllerName(controller) << ", loading mapping: "; - KeyMap::NotifyPadConnected(deviceIndex, std::string(pszGUID) + ": " + SDL_GameControllerName(controller)); + // NOTE: The case to InputDeviceID here is wrong, we should do some kind of lookup. + KeyMap::NotifyPadConnected((InputDeviceID)deviceIndex, std::string(pszGUID) + ": " + SDL_GameControllerName(controller)); auto mapping = SDL_GameControllerMapping(controller); if (mapping == NULL) { //cout << "FAILED" << endl; @@ -116,7 +117,7 @@ void SDLJoystick::registerEventHandler() { registeredAsEventHandler = true; } -keycode_t SDLJoystick::getKeycodeForButton(SDL_GameControllerButton button) { +InputKeyCode SDLJoystick::getKeycodeForButton(SDL_GameControllerButton button) { switch (button) { case SDL_CONTROLLER_BUTTON_DPAD_UP: return NKCODE_DPAD_UP; @@ -182,7 +183,8 @@ void SDLJoystick::ProcessInput(SDL_Event &event){ break; case SDL_CONTROLLERAXISMOTION: AxisInput axis; - axis.axisId = event.caxis.axis; + // TODO: Can we really cast axis events like that? Do they match? + axis.axisId = (InputAxis)event.caxis.axis; axis.value = event.caxis.value / 32767.0f; if (axis.value > 1.0f) axis.value = 1.0f; if (axis.value < -1.0f) axis.value = -1.0f; diff --git a/SDL/SDLJoystick.h b/SDL/SDLJoystick.h index d7494f8296..1046a5f283 100644 --- a/SDL/SDLJoystick.h +++ b/SDL/SDLJoystick.h @@ -25,7 +25,7 @@ public: private: void setUpController(int deviceIndex); void setUpControllers(); - keycode_t getKeycodeForButton(SDL_GameControllerButton button); + InputKeyCode getKeycodeForButton(SDL_GameControllerButton button); int getDeviceIndex(int instanceId); bool registeredAsEventHandler; std::vector controllers; diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 4262d2c571..c49df319a7 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -1069,7 +1069,7 @@ int main(int argc, char *argv[]) { int c = u8_nextchar(event.text.text, &pos); KeyInput key; key.flags = KEY_CHAR; - key.keyCode = c; + key.unicodeChar = c; key.deviceId = DEVICE_ID_KEYBOARD; NativeKey(key); break; diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index ab15e6137d..817e8ee7d1 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -2063,7 +2063,7 @@ void HostnameSelectScreen::CreatePopupContents(UI::ViewGroup *parent) { progressView_->SetVisibility(UI::V_GONE); } -void HostnameSelectScreen::SendEditKey(int keyCode, int flags) { +void HostnameSelectScreen::SendEditKey(InputKeyCode keyCode, int flags) { auto oldView = UI::GetFocusedView(); UI::SetFocusedView(addrView_); KeyInput fakeKey{ DEVICE_ID_KEYBOARD, keyCode, KEY_DOWN | flags }; @@ -2074,13 +2074,13 @@ void HostnameSelectScreen::SendEditKey(int keyCode, int flags) { UI::EventReturn HostnameSelectScreen::OnNumberClick(UI::EventParams &e) { std::string text = e.v ? e.v->Tag() : ""; if (text.length() == 1 && text[0] >= '0' && text[0] <= '9') { - SendEditKey(text[0], KEY_CHAR); + SendEditKey((InputKeyCode)text[0], KEY_CHAR); // ASCII for digits match keycodes. } return UI::EVENT_DONE; } UI::EventReturn HostnameSelectScreen::OnPointClick(UI::EventParams &e) { - SendEditKey('.', KEY_CHAR); + SendEditKey(NKCODE_PERIOD, KEY_CHAR); return UI::EVENT_DONE; } diff --git a/UI/GameSettingsScreen.h b/UI/GameSettingsScreen.h index 882b60263c..a5c20c50c4 100644 --- a/UI/GameSettingsScreen.h +++ b/UI/GameSettingsScreen.h @@ -214,7 +214,7 @@ protected: private: void ResolverThread(); - void SendEditKey(int keyCode, int flags = 0); + void SendEditKey(InputKeyCode keyCode, int flags = 0); UI::EventReturn OnNumberClick(UI::EventParams &e); UI::EventReturn OnPointClick(UI::EventParams &e); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 22c8543d68..f5f6b0e508 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1147,9 +1147,9 @@ void NativeRender(GraphicsContext *graphicsContext) { } void HandleGlobalMessage(const std::string &msg, const std::string &value) { - int nextInputDeviceID = -1; + InputDeviceID nextInputDeviceID = DEVICE_ID_ANY; if (msg == "inputDeviceConnectedID") { - nextInputDeviceID = parseLong(value); + nextInputDeviceID = (InputDeviceID)parseLong(value); } else if (msg == "inputDeviceConnected") { KeyMap::NotifyPadConnected(nextInputDeviceID, value); diff --git a/UI/TiltAnalogSettingsScreen.cpp b/UI/TiltAnalogSettingsScreen.cpp index 73cb24e37d..f4b9234f80 100644 --- a/UI/TiltAnalogSettingsScreen.cpp +++ b/UI/TiltAnalogSettingsScreen.cpp @@ -145,6 +145,7 @@ void TiltAnalogSettingsScreen::axis(const AxisInput &axis) { case JOYSTICK_AXIS_ACCELEROMETER_X: down_.x = axis.value; break; case JOYSTICK_AXIS_ACCELEROMETER_Y: down_.y = axis.value; break; case JOYSTICK_AXIS_ACCELEROMETER_Z: down_.z = axis.value; break; + default: break; } } } diff --git a/UWP/NKCodeFromWindowsSystem.cpp b/UWP/NKCodeFromWindowsSystem.cpp index 01c6aeda21..c7d6c5d769 100644 --- a/UWP/NKCodeFromWindowsSystem.cpp +++ b/UWP/NKCodeFromWindowsSystem.cpp @@ -3,7 +3,7 @@ using namespace Windows::System; -std::map virtualKeyCodeToNKCode{ +std::map virtualKeyCodeToNKCode{ { VirtualKey::A, NKCODE_A }, { VirtualKey::B, NKCODE_B }, { VirtualKey::C, NKCODE_C }, @@ -107,4 +107,4 @@ std::map virtualKeyCodeToNKCode{ //{ VK_OEM_102, NKCODE_EXT_PIPE }, //{ VK_LBUTTON, NKCODE_EXT_MOUSEBUTTON_1 }, //{ VK_RBUTTON, NKCODE_EXT_MOUSEBUTTON_2 },; -}; \ No newline at end of file +}; diff --git a/UWP/NKCodeFromWindowsSystem.h b/UWP/NKCodeFromWindowsSystem.h index 94905fb699..230b5a986f 100644 --- a/UWP/NKCodeFromWindowsSystem.h +++ b/UWP/NKCodeFromWindowsSystem.h @@ -4,4 +4,4 @@ #include "Common/Input/KeyCodes.h" -extern std::map virtualKeyCodeToNKCode; \ No newline at end of file +extern std::map virtualKeyCodeToNKCode; diff --git a/UWP/PPSSPP_UWPMain.cpp b/UWP/PPSSPP_UWPMain.cpp index 904906fbb9..0e8a80b38e 100644 --- a/UWP/PPSSPP_UWPMain.cpp +++ b/UWP/PPSSPP_UWPMain.cpp @@ -251,7 +251,7 @@ void PPSSPP_UWPMain::OnKeyUp(int scanCode, Windows::System::VirtualKey virtualKe } void PPSSPP_UWPMain::OnMouseWheel(float delta) { - int key = NKCODE_EXT_MOUSEWHEEL_UP; + InputKeyCode key = NKCODE_EXT_MOUSEWHEEL_UP; if (delta < 0) { key = NKCODE_EXT_MOUSEWHEEL_DOWN; } else if (delta == 0) { diff --git a/Windows/DinputDevice.cpp b/Windows/DinputDevice.cpp index 43a2340f27..b9ec91a391 100644 --- a/Windows/DinputDevice.cpp +++ b/Windows/DinputDevice.cpp @@ -39,7 +39,7 @@ std::vector DinputDevice::devices; bool DinputDevice::needsCheck_ = true; // In order from 0. There can be 128, but most controllers do not have that many. -static const int dinput_buttons[] = { +static const InputKeyCode dinput_buttons[] = { NKCODE_BUTTON_1, NKCODE_BUTTON_2, NKCODE_BUTTON_3, @@ -203,11 +203,11 @@ DinputDevice::~DinputDevice() { } } -void SendNativeAxis(int deviceId, int value, int &lastValue, int axisId) { +void SendNativeAxis(InputDeviceID deviceId, int value, int &lastValue, InputAxis axisId) { AxisInput axis; axis.deviceId = deviceId; axis.axisId = axisId; - axis.value = (float)value / 10000.0f; // Convert axis to normalised float + axis.value = (float)value * (1.0f / 10000.0f); // Convert axis to normalised float NativeAxis(axis); lastValue = value; diff --git a/Windows/RawInput.cpp b/Windows/RawInput.cpp index db8b8518c2..9f5fc3a190 100644 --- a/Windows/RawInput.cpp +++ b/Windows/RawInput.cpp @@ -57,7 +57,7 @@ #endif namespace WindowsRawInput { - static std::set keyboardKeysDown; + static std::set keyboardKeysDown; static void *rawInputBuffer; static size_t rawInputBufferSize; static bool menuActive; @@ -68,7 +68,7 @@ namespace WindowsRawInput { // TODO: More keys need to be added, but this is more than // a fair start. - static std::map windowsTransTable = { + static std::map windowsTransTable = { { 'A', NKCODE_A }, { 'B', NKCODE_B }, { 'C', NKCODE_C }, @@ -213,7 +213,7 @@ namespace WindowsRawInput { return menuActive; } - static int GetTrueVKey(const RAWKEYBOARD &kb) { + static InputKeyCode GetTrueVKey(const RAWKEYBOARD &kb) { int vKey = kb.VKey; switch (kb.VKey) { case VK_SHIFT: @@ -285,7 +285,7 @@ namespace WindowsRawInput { LRESULT ProcessChar(HWND hWnd, WPARAM wParam, LPARAM lParam) { KeyInput key; - key.keyCode = (int)wParam; // Note that this is NOT a NKCODE but a Unicode character! + key.unicodeChar = (int)wParam; // Note that this is NOT a NKCODE but a Unicode character! key.flags = KEY_CHAR; key.deviceId = DEVICE_ID_KEYBOARD; NativeKey(key); diff --git a/Windows/XinputDevice.cpp b/Windows/XinputDevice.cpp index 3e82015921..d8c3e0e898 100644 --- a/Windows/XinputDevice.cpp +++ b/Windows/XinputDevice.cpp @@ -126,7 +126,7 @@ static void UnloadXInputDLL() {} // Permanent map. Actual mapping happens elsewhere. -static const struct {int from, to;} xinput_ctrl_map[] = { +static const struct { int from; InputKeyCode to; } xinput_ctrl_map[] = { {XINPUT_GAMEPAD_A, NKCODE_BUTTON_A}, {XINPUT_GAMEPAD_B, NKCODE_BUTTON_B}, {XINPUT_GAMEPAD_X, NKCODE_BUTTON_X}, @@ -220,8 +220,8 @@ void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATIO ApplyVibration(pad, vibration); AxisInput axis; - axis.deviceId = DEVICE_ID_XINPUT_0 + pad; - auto sendAxis = [&](AndroidJoystickAxis axisId, float value) { + axis.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad); + auto sendAxis = [&](InputAxis axisId, float value) { axis.axisId = axisId; axis.value = value; NativeAxis(axis); diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 1084252a14..9f135c72ec 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1181,8 +1181,8 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_touch extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_keyDown(JNIEnv *, jclass, jint deviceId, jint key, jboolean isRepeat) { KeyInput keyInput; - keyInput.deviceId = deviceId; - keyInput.keyCode = key; + keyInput.deviceId = (InputDeviceID)deviceId; + keyInput.keyCode = (InputKeyCode)key; keyInput.flags = KEY_DOWN; if (isRepeat) { keyInput.flags |= KEY_IS_REPEAT; @@ -1192,8 +1192,8 @@ extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_keyDown(JNIEnv *, jclass, j extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_keyUp(JNIEnv *, jclass, jint deviceId, jint key) { KeyInput keyInput; - keyInput.deviceId = deviceId; - keyInput.keyCode = key; + keyInput.deviceId = (InputDeviceID)deviceId; + keyInput.keyCode = (InputKeyCode)key; keyInput.flags = KEY_UP; return NativeKey(keyInput); } @@ -1204,8 +1204,8 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_joystickAxis( return; AxisInput axis; - axis.axisId = axisId; - axis.deviceId = deviceId; + axis.axisId = (InputAxis)axisId; + axis.deviceId = (InputDeviceID)deviceId; axis.value = value; NativeAxis(axis); diff --git a/ios/SmartKeyboardMap.cpp b/ios/SmartKeyboardMap.cpp index 43d8e5b46e..a61fc7da36 100644 --- a/ios/SmartKeyboardMap.cpp +++ b/ios/SmartKeyboardMap.cpp @@ -9,7 +9,7 @@ #include "SmartKeyboardMap.hpp" #include "Common/Input/KeyCodes.h" -int getSmartKeyboardMap(int keycode) { +InputKeyCode getSmartKeyboardMap(int keycode) { switch(keycode) { case 4: return NKCODE_A; case 5: return NKCODE_B; @@ -73,6 +73,6 @@ int getSmartKeyboardMap(int keycode) { case 229: return NKCODE_SHIFT_RIGHT; case 230: return NKCODE_META_RIGHT; case 231: return NKCODE_ALT_RIGHT; - default: return 0; + default: return NKCODE_UNKNOWN; } } diff --git a/ios/SmartKeyboardMap.hpp b/ios/SmartKeyboardMap.hpp index bf6a3654a9..fcaa22d966 100644 --- a/ios/SmartKeyboardMap.hpp +++ b/ios/SmartKeyboardMap.hpp @@ -6,9 +6,8 @@ // // -#ifndef SmartKeyboardMap_hpp -#define SmartKeyboardMap_hpp +#pragma once -int getSmartKeyboardMap(int keycode); +#include "Common/Input/KeyCodes.h" -#endif /* SmartKeyboardMap_hpp */ +InputKeyCode getSmartKeyboardMap(int keycode); diff --git a/ios/ViewController.mm b/ios/ViewController.mm index 0215dacf68..5f125990af 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -105,7 +105,7 @@ static CameraHelper *cameraHelper; static LocationHelper *locationHelper; @interface ViewController () { - std::map iCadeToKeyMap; + std::map iCadeToKeyMap; } @property (nonatomic, strong) EAGLContext* context; @@ -567,7 +567,7 @@ int ToTouchID(UITouch *uiTouch, bool allowAllocate) { } } -- (void)controllerButtonPressed:(BOOL)pressed keyCode:(keycode_t)keyCode +- (void)controllerButtonPressed:(BOOL)pressed keyCode:(InputKeyCode)keyCode { KeyInput key; key.deviceId = DEVICE_ID_PAD_0; diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 4b4b1b6e50..7980486af1 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -910,10 +910,10 @@ static bool TestDepthMath() { bool TestInputMapping() { InputMapping mapping; - mapping.deviceId = 10; + mapping.deviceId = DEVICE_ID_PAD_0; mapping.keyCode = 20; InputMapping mapping2; - mapping2.deviceId = 18; + mapping2.deviceId = DEVICE_ID_PAD_8; mapping2.keyCode = 38; std::string cfg = mapping.ToConfigString();