From 2267d827903c97a4f182be108bde0b67f94defb0 Mon Sep 17 00:00:00 2001 From: ptmaster Date: Sun, 22 Dec 2024 00:02:50 +0800 Subject: [PATCH] Fix Axis | season title | clean up --- src/bnusio.cpp | 57 ++++---- src/dllmain.cpp | 6 +- src/helpers.cpp | 16 ++- src/helpers.h | 5 +- src/patches/language.cpp | 28 +++- src/patches/versions/JPN39.cpp | 7 - src/poll.h | 4 +- src/pollasync.cpp | 251 ++++++++++++++------------------- 8 files changed, 180 insertions(+), 194 deletions(-) diff --git a/src/bnusio.cpp b/src/bnusio.cpp index 459d0fb..dcbb38f 100644 --- a/src/bnusio.cpp +++ b/src/bnusio.cpp @@ -62,7 +62,8 @@ u16 service_count = 0; bool inited = false; bool updateByCoin = false; HWND windowHandle = nullptr; -bool usePoll = false; +float axisThreshold = 0.6f; +u8 inputState = 0; HKL currentLayout; namespace bnusio { @@ -207,9 +208,8 @@ FUNCTION_PTR (i64, bnusio_ResetCoin_Original, PROC_ADDRESS ("bnusio_original.dll u16 __fastcall AnalogInputAxis (const u8 which) { - const u16 analogValue = static_cast (32768 * ControllerAxisIsDown (analogBindings[which])); - if (analogValue > 100) return analogValue; - else return 0; + u16 analogIn = (u16)(32768 * ControllerAxisIsDown (analogBindings[which])) + 1; + return analogIn > 100 ? analogIn : 0; } u16 __fastcall @@ -307,7 +307,7 @@ UpdateLoop () { if (IsButtonTapped (COIN_ADD)) coin_count++; if (IsButtonTapped (SERVICE)) service_count++; if (IsButtonTapped (TEST)) testEnabled = !testEnabled; - if (IsButtonTapped (EXIT)) { + if (exited == 0 && IsButtonTapped (EXIT)) { LogMessage (LogLevel::INFO, "Exit By Press Exit Button!"); exited += 1; testEnabled = 1; std::thread([](){patches::Plugins::Exit ();}).detach(); } @@ -369,33 +369,30 @@ Init () { const auto keyConfigPath = std::filesystem::current_path () / "keyconfig.toml"; const std::unique_ptr keyConfig_ptr (openConfig (keyConfigPath), toml_free); if (keyConfig_ptr) { - if (analogInput) usePoll = true; + if (analogInput) inputState |= (1 << 2); const toml_table_t *keyConfig = keyConfig_ptr.get (); - SetConfigValue (keyConfig, "EXIT", &EXIT, &usePoll); + SetConfigValue (keyConfig, "EXIT", &EXIT, &inputState); - SetConfigValue (keyConfig, "TEST", &TEST, &usePoll); - SetConfigValue (keyConfig, "SERVICE", &SERVICE, &usePoll); - SetConfigValue (keyConfig, "DEBUG_UP", &DEBUG_UP, &usePoll); - SetConfigValue (keyConfig, "DEBUG_DOWN", &DEBUG_DOWN, &usePoll); - SetConfigValue (keyConfig, "DEBUG_ENTER", &DEBUG_ENTER, &usePoll); + SetConfigValue (keyConfig, "TEST", &TEST, &inputState); + SetConfigValue (keyConfig, "SERVICE", &SERVICE, &inputState); + SetConfigValue (keyConfig, "DEBUG_UP", &DEBUG_UP, &inputState); + SetConfigValue (keyConfig, "DEBUG_DOWN", &DEBUG_DOWN, &inputState); + SetConfigValue (keyConfig, "DEBUG_ENTER", &DEBUG_ENTER, &inputState); - SetConfigValue (keyConfig, "COIN_ADD", &COIN_ADD, &usePoll); - SetConfigValue (keyConfig, "CARD_INSERT_1", &CARD_INSERT_1, &usePoll); - SetConfigValue (keyConfig, "CARD_INSERT_2", &CARD_INSERT_2, &usePoll); - SetConfigValue (keyConfig, "QR_DATA_READ", &QR_DATA_READ, &usePoll); - SetConfigValue (keyConfig, "QR_IMAGE_READ", &QR_IMAGE_READ, &usePoll); + SetConfigValue (keyConfig, "COIN_ADD", &COIN_ADD, &inputState); + SetConfigValue (keyConfig, "CARD_INSERT_1", &CARD_INSERT_1, &inputState); + SetConfigValue (keyConfig, "CARD_INSERT_2", &CARD_INSERT_2, &inputState); + SetConfigValue (keyConfig, "QR_DATA_READ", &QR_DATA_READ, &inputState); + SetConfigValue (keyConfig, "QR_IMAGE_READ", &QR_IMAGE_READ, &inputState); - SetConfigValue (keyConfig, "P1_LEFT_BLUE", &P1_LEFT_BLUE, &usePoll); - SetConfigValue (keyConfig, "P1_LEFT_RED", &P1_LEFT_RED, &usePoll); - SetConfigValue (keyConfig, "P1_RIGHT_RED", &P1_RIGHT_RED, &usePoll); - SetConfigValue (keyConfig, "P1_RIGHT_BLUE", &P1_RIGHT_BLUE, &usePoll); - SetConfigValue (keyConfig, "P2_LEFT_BLUE", &P2_LEFT_BLUE, &usePoll); - SetConfigValue (keyConfig, "P2_LEFT_RED", &P2_LEFT_RED, &usePoll); - SetConfigValue (keyConfig, "P2_RIGHT_RED", &P2_RIGHT_RED, &usePoll); - SetConfigValue (keyConfig, "P2_RIGHT_BLUE", &P2_RIGHT_BLUE, &usePoll); - if (!usePoll) { - LogMessage (LogLevel::WARN, "Only Keyboard config detected, disable poll!"); - } + SetConfigValue (keyConfig, "P1_LEFT_BLUE", &P1_LEFT_BLUE, &inputState); + SetConfigValue (keyConfig, "P1_LEFT_RED", &P1_LEFT_RED, &inputState); + SetConfigValue (keyConfig, "P1_RIGHT_RED", &P1_RIGHT_RED, &inputState); + SetConfigValue (keyConfig, "P1_RIGHT_BLUE", &P1_RIGHT_BLUE, &inputState); + SetConfigValue (keyConfig, "P2_LEFT_BLUE", &P2_LEFT_BLUE, &inputState); + SetConfigValue (keyConfig, "P2_LEFT_RED", &P2_LEFT_RED, &inputState); + SetConfigValue (keyConfig, "P2_RIGHT_RED", &P2_RIGHT_RED, &inputState); + SetConfigValue (keyConfig, "P2_RIGHT_BLUE", &P2_RIGHT_BLUE, &inputState); } if (!emulateUsio && !exists (std::filesystem::current_path () / "bnusio_original.dll")) { @@ -464,7 +461,7 @@ void Update () { if (!inited) { windowHandle = FindWindowA ("nuFoundation.Window", nullptr); - if (usePoll) InitializePoll (windowHandle); + InitializePoll (windowHandle); if (autoIme) { currentLayout = GetKeyboardLayout (0); auto engLayout = LoadKeyboardLayout (TEXT ("00000409"), KLF_ACTIVATE); @@ -474,7 +471,7 @@ Update () { patches::Plugins::Init (); inited = true; } - UpdatePoll (windowHandle, usePoll); + UpdatePoll (windowHandle); syncCV.notify_all (); } #endif diff --git a/src/dllmain.cpp b/src/dllmain.cpp index af4440a..1852129 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -151,9 +151,9 @@ DllMain (HMODULE module, const DWORD reason, LPVOID reserved) { InitializeLogger (GetLogLevel (logLevelStr), logToFile); patches::Timer::Init (); - #ifdef ASYNC_IO - InitializeKeyboard (); - #endif + // #ifdef ASYNC_IO + // InitializeKeyboard (); + // #endif LogMessage (LogLevel::INFO, "Loading config..."); diff --git a/src/helpers.cpp b/src/helpers.cpp index 8e5ec58..a557ef4 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -104,7 +104,7 @@ readConfigIntArray (const toml_table_t *table, const std::string &key, std::vect } std::wstring -replace (const std::wstring &orignStr, const std::wstring &oldStr, const std::wstring &newStr) { +replace (const std::wstring orignStr, const std::wstring &oldStr, const std::wstring &newStr) { size_t pos = 0; std::wstring tempStr = orignStr; const std::wstring::size_type newStrLen = newStr.length (); @@ -121,7 +121,7 @@ replace (const std::wstring &orignStr, const std::wstring &oldStr, const std::ws } std::string -replace (const std::string &orignStr, const std::string &oldStr, const std::string &newStr) { +replace (const std::string orignStr, const std::string &oldStr, const std::string &newStr) { size_t pos = 0; std::string tempStr = orignStr; const std::string::size_type newStrLen = newStr.length (); @@ -200,4 +200,16 @@ withFile (const char *fileName, std::function callback) { free (buffer); fclose (file); +} + +std::string +season () { + std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + std::tm* tm = std::localtime(&time); + int month = tm->tm_mon + 1; + return std::to_string (tm->tm_year + 1900) + ( + (month >= 3 && month <= 5) ? "spr" : + (month >= 6 && month <= 8) ? "sum" : + (month >= 9 && month <= 11) ? "aut" : "win" + ); } \ No newline at end of file diff --git a/src/helpers.h b/src/helpers.h index 7ed853a..e0b1eeb 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -217,10 +217,11 @@ bool readConfigBool (const toml_table_t *table, const std::string &key, bool not i64 readConfigInt (const toml_table_t *table, const std::string &key, i64 notFoundValue); std::string readConfigString (const toml_table_t *table, const std::string &key, const std::string ¬FoundValue); std::vector readConfigIntArray (const toml_table_t *table, const std::string &key, std::vector notFoundValue); -std::wstring replace (const std::wstring &orignStr, const std::wstring &oldStr, const std::wstring &newStr); -std::string replace (const std::string &orignStr, const std::string &oldStr, const std::string &newStr); +std::wstring replace (const std::wstring orignStr, const std::wstring &oldStr, const std::wstring &newStr); +std::string replace (const std::string orignStr, const std::string &oldStr, const std::string &newStr); const char *GameVersionToString (GameVersion version); const char *languageStr (int language); std::string ConvertWideToUtf8 (const std::wstring &wstr); bool AreAllBytesZero (const u8 *array, size_t offset, size_t length); void withFile (const char *fileName, std::function callback); +std::string season (); diff --git a/src/patches/language.cpp b/src/patches/language.cpp index abf9f3d..4edcb24 100644 --- a/src/patches/language.cpp +++ b/src/patches/language.cpp @@ -374,6 +374,8 @@ FAST_HOOK_DYNAMIC (u64, GraphicBaseGetLanguageType, u64 a1, void *a2) { return (u64)GetLanguageType (taiko_assign, language, a2); } +std::string *season_nulm = nullptr; +std::string *season_nutexb = nullptr; void Init () { switch (gameVersion) { @@ -416,12 +418,34 @@ Init () { language_patch.push_back (safetyhook::create_mid (ASLR (0x14012B8C8), [](SafetyHookContext &ctx) { InjectLanguageType (ctx.rbp + 0x180 + ctx.rax * 8, ctx.rdx); ctx.rip = ASLR (0x14012B8D0); })); // ScoreGetLanguageType LayeredFs::RegisterBefore ([&] (const std::string &originalFileName, const std::string ¤tFileName) -> std::string { - if (language != 4 || currentFileName.find ("\\lumen\\") == std::string::npos) return ""; - std::string fileName = currentFileName; + if (language != 4 || currentFileName.starts_with ("F:\\") || currentFileName.find ("\\lumen\\") == std::string::npos) return ""; + std::string fileName = std::string (currentFileName); fileName = replace (fileName, "\\lumen\\", "\\lumen_cn\\"); if (std::filesystem::exists (fileName)) return fileName; return ""; }); + + LayeredFs::RegisterBefore ([&] (const std::string &originalFileName, const std::string ¤tFileName) -> std::string { + if (language != 4 || currentFileName.starts_with ("F:\\")) return ""; + if (currentFileName.ends_with ("title.nulm")) { + std::string fileName = std::string (currentFileName); + if (season_nulm == nullptr) { + fileName = replace (fileName, "title.nulm", "title_season.nulm"); + if (std::filesystem::exists (fileName)) { + std::string nutexb = std::string (currentFileName); + nutexb = replace (nutexb, "title.nulm", "title_" + season () + ".nutexb"); + if (std::filesystem::exists (nutexb)) { + season_nulm = new std::string (fileName), season_nutexb = new std::string (nutexb); + return *season_nulm; + } + } + season_nulm = new std::string (""), season_nutexb = new std::string (""); + return ""; + } else return *season_nulm; + } else if (currentFileName.ends_with ("title.nutexb") && season_nutexb != nullptr) { + return *season_nutexb; + } else return ""; + }); } break; } diff --git a/src/patches/versions/JPN39.cpp b/src/patches/versions/JPN39.cpp index 028d3fc..fe56ef0 100644 --- a/src/patches/versions/JPN39.cpp +++ b/src/patches/versions/JPN39.cpp @@ -305,17 +305,12 @@ Init () { i32 yRes = 1080; bool vsync = false; bool unlockSongs = true; - bool chsPatch = false; - bool useLayeredfs = false; auto configPath = std::filesystem::current_path () / "config.toml"; std::unique_ptr config_ptr (openConfig (configPath), toml_free); if (config_ptr) { if (auto patches = openConfigSection (config_ptr.get (), "patches")) { unlockSongs = readConfigBool (patches, "unlock_songs", unlockSongs); - if (auto jpn39 = openConfigSection (patches, "jpn39")) { - chsPatch = readConfigBool (jpn39, "chs_patch", chsPatch); - } } if (auto graphics = openConfigSection (config_ptr.get (), "graphics")) { @@ -325,8 +320,6 @@ Init () { } vsync = readConfigBool (graphics, "vsync", vsync); } - - if (auto layeredfs = openConfigSection (config_ptr.get (), "layeredfs")) useLayeredfs = readConfigBool (layeredfs, "enabled", useLayeredfs); } // Hook to get AppAccessor and ComponentAccessor diff --git a/src/poll.h b/src/poll.h index 29e7d8c..90ec0ac 100644 --- a/src/poll.h +++ b/src/poll.h @@ -68,7 +68,7 @@ bool InitializePoll (HWND windowHandle); void DisposePoll (); void SetKeyboardButtons (); ConfigValue StringToConfigEnum (const char *value); -void SetConfigValue (const toml_table_t *table, const char *key, Keybindings *keybind, bool *usePoll); +void SetConfigValue (const toml_table_t *table, const char *key, Keybindings *keybind, u8 *inputState); InternalButtonState GetInternalButtonState (const Keybindings &bindings); void SetRumble (int left, int right, int length); @@ -108,7 +108,7 @@ bool ControllerAxisWasUp (const SDLAxis axis); bool ControllerAxisIsReleased (const SDLAxis axis); bool IsButtonReleased (const Keybindings &bindings); #else -void UpdatePoll (HWND windowHandle, bool useController); +void UpdatePoll (HWND windowHandle); void InitializeKeyboard (); void DisposeKeyboard (); #endif \ No newline at end of file diff --git a/src/pollasync.cpp b/src/pollasync.cpp index a16ea99..1366a33 100644 --- a/src/pollasync.cpp +++ b/src/pollasync.cpp @@ -7,6 +7,8 @@ extern bool jpLayout; extern int exited; +extern u8 inputState; +extern float axisThreshold; bool wndForeground = false; @@ -20,11 +22,12 @@ uint8_t controllerDiff[SDL_GAMEPAD_BUTTON_COUNT] = { 0 }; char currentMouseWheelDirection = 0; uint8_t mouseWheelCount[2] = { 0 }; uint8_t mouseWheelDiff[2] = { 0 }; -SDLAxisState currentControllerAxisState; +// SDLAxisState currentControllerAxisState; +float currentControllerAxisState[SDL_AXIS_MAX] = { 0.0 }; uint8_t controllerAxisCount[SDL_AXIS_MAX] = { 0 }; uint8_t controllerAxisDiff[SDL_AXIS_MAX] = { 0 }; -int maxCount = 2; +int maxCount = 1; SDL_Window *window; SDL_Gamepad *controllers[255]; @@ -40,7 +43,7 @@ SetKeyboardButtons () { } void -SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bind, bool *usePoll) { +SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bind, u8 *inputState) { const toml_array_t *array = toml_array_in (table, key); if (!array) { LogMessage (LogLevel::WARN, std::string (key) + ": Cannot find array"); @@ -59,6 +62,7 @@ SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bin switch (value.type) { case keycode: { + *inputState |= 1; for (int i = 0; i < std::size (key_bind->keycodes); i++) { if (key_bind->keycodes[i] == 0) { key_bind->keycodes[i] = value.keycode; @@ -68,7 +72,7 @@ SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bin break; } case button: { - *usePoll = true; + *inputState |= (1 << 2); for (int i = 0; i < std::size (key_bind->buttons); i++) { if (key_bind->buttons[i] == SDL_GAMEPAD_BUTTON_INVALID) { key_bind->buttons[i] = value.button; @@ -78,7 +82,7 @@ SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bin break; } case axis: { - *usePoll = true; + *inputState |= (1 << 2); for (int i = 0; i < std::size (key_bind->axis); i++) { if (key_bind->axis[i] == 0) { key_bind->axis[i] = value.axis; @@ -87,6 +91,7 @@ SetConfigValue (const toml_table_t *table, const char *key, Keybindings *key_bin } } case scroll: { + *inputState |= (1 << 1); for (int i = 0; i < std::size (key_bind->scroll); i++) { if (key_bind->scroll[i] == 0) { key_bind->scroll[i] = value.scroll; @@ -166,10 +171,16 @@ void KeyboardMainLoop() { HINSTANCE hInstance = GetModuleHandle(NULL); HWND hwnd = CreateInvisibleWindow(hInstance); if (!hwnd) LogMessage (LogLevel::ERROR, "Failed to create window!\n"); - keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, InputProc, nullptr, 0); - if (keyboardHook == nullptr) LogMessage (LogLevel::ERROR, "Failed to install keyboard hook!\n"); - mouseHook = SetWindowsHookEx(WH_MOUSE_LL, InputProc, nullptr, 0); - if (mouseHook == nullptr) LogMessage (LogLevel::ERROR, "Failed to install mouse hook!\n"); + if (inputState & 1) { + keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, InputProc, nullptr, 0); + if (keyboardHook == nullptr) LogMessage (LogLevel::ERROR, "Failed to install keyboard hook!\n"); + else LogMessage (LogLevel::INFO, "KeyboardLL hook installed!"); + } + if (inputState & (1 << 1)) { + mouseHook = SetWindowsHookEx(WH_MOUSE_LL, InputProc, nullptr, 0); + if (mouseHook == nullptr) LogMessage (LogLevel::ERROR, "Failed to install mouse hook!\n"); + else LogMessage (LogLevel::INFO, "MouseLL hook installed!"); + } if (maxCount > 2) LogMessage (LogLevel::ERROR, "CHEATING MODE! max count is set to {}", maxCount); LogMessage (LogLevel::WARN, "(experimental) Using Async IO!"); MSG msg; @@ -183,8 +194,9 @@ void KeyboardMainLoop() { void InitializeKeyboard () { - std::thread (KeyboardMainLoop).detach (); - // atexit (DisposeKeyboard); + if (inputState & (1 | 1 << 1)) { + std::thread (KeyboardMainLoop).detach (); + } } void @@ -202,64 +214,67 @@ CheckFlipped (uint32_t which, SDL_Gamepad *gamepad) { bool InitializePoll (HWND windowHandle) { - LogMessage (LogLevel::DEBUG, "InitializePoll"); - bool hasRumble = true; - wndForeground = windowHandle == GetForegroundWindow (); - SDL_SetMainReady (); + InitializeKeyboard (); + if (inputState & (1 << 2)) { + LogMessage (LogLevel::DEBUG, "InitializePoll"); + bool hasRumble = true; + wndForeground = windowHandle == GetForegroundWindow (); + SDL_SetMainReady (); - SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS4, "1"); - SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1"); - SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS5, "1"); - SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); + SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS4, "1"); + SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1"); + SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS5, "1"); + SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); - if (!SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO)) { - if (SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO)) { - hasRumble = false; - } else { - LogMessage (LogLevel::ERROR, std::string ("SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO): ") + SDL_GetError ()); - return false; - } - } - - auto configPath = std::filesystem::current_path () / "gamecontrollerdb.txt"; - if (SDL_AddGamepadMappingsFromFile (configPath.string ().c_str ()) == -1) - LogMessage (LogLevel::ERROR, "Cannot read gamecontrollerdb.txt"); - SDL_SetGamepadEventsEnabled (true); - SDL_SetJoystickEventsEnabled (true); - - int numJoysticks = 0; - auto* joyStickIds = SDL_GetJoysticks(&numJoysticks); - if (joyStickIds == nullptr) { - LogMessage (LogLevel::ERROR, "Error getting joystick ids"); - exit(1); - } - for (int i = 0; i < numJoysticks; i++) { - auto id = joyStickIds[i]; - if (!SDL_IsGamepad (id)) continue; - SDL_Gamepad *controller = SDL_OpenGamepad (id); - - if (!controller) { - LogMessage (LogLevel::WARN, std::string ("Could not open gamecontroller ") + SDL_GetGamepadNameForID (id) + ": " + SDL_GetError ()); - continue; + if (!SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO)) { + if (SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO)) { + hasRumble = false; + } else { + LogMessage (LogLevel::ERROR, std::string ("SDL_Init (SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_VIDEO): ") + SDL_GetError ()); + return false; + } } - LogMessage (LogLevel::DEBUG, "Init Controller connected!"); - CheckFlipped (id, controller); - controllers[id] = controller; - } - SDL_free (joyStickIds); + auto configPath = std::filesystem::current_path () / "gamecontrollerdb.txt"; + if (SDL_AddGamepadMappingsFromFile (configPath.string ().c_str ()) == -1) + LogMessage (LogLevel::ERROR, "Cannot read gamecontrollerdb.txt"); + SDL_SetGamepadEventsEnabled (true); + SDL_SetJoystickEventsEnabled (true); - SDL_PropertiesID props = SDL_CreateProperties(); - if(props == 0) { - LogMessage (LogLevel::ERROR, "Unable to create SDL_Properties"); - exit(1); - } - SDL_SetPointerProperty (props, SDL_PROP_WINDOW_CREATE_WIN32_HWND_POINTER, windowHandle); - window = SDL_CreateWindowWithProperties (props); - if (window == NULL) LogMessage (LogLevel::ERROR, std::string ("SDL_CreateWindowFrom (windowHandle): ") + SDL_GetError ()); + int numJoysticks = 0; + auto* joyStickIds = SDL_GetJoysticks(&numJoysticks); + if (joyStickIds == nullptr) { + LogMessage (LogLevel::ERROR, "Error getting joystick ids"); + exit(1); + } + for (int i = 0; i < numJoysticks; i++) { + auto id = joyStickIds[i]; + if (!SDL_IsGamepad (id)) continue; + SDL_Gamepad *controller = SDL_OpenGamepad (id); - atexit (DisposePoll); - return hasRumble; + if (!controller) { + LogMessage (LogLevel::WARN, std::string ("Could not open gamecontroller ") + SDL_GetGamepadNameForID (id) + ": " + SDL_GetError ()); + continue; + } + + LogMessage (LogLevel::DEBUG, "Init Controller connected!"); + CheckFlipped (id, controller); + controllers[id] = controller; + } + SDL_free (joyStickIds); + + SDL_PropertiesID props = SDL_CreateProperties(); + if(props == 0) { + LogMessage (LogLevel::ERROR, "Unable to create SDL_Properties"); + exit(1); + } + SDL_SetPointerProperty (props, SDL_PROP_WINDOW_CREATE_WIN32_HWND_POINTER, windowHandle); + window = SDL_CreateWindowWithProperties (props); + if (window == NULL) LogMessage (LogLevel::ERROR, std::string ("SDL_CreateWindowFrom (windowHandle): ") + SDL_GetError ()); + + atexit (DisposePoll); + return hasRumble; + } else return false; } bool @@ -285,23 +300,26 @@ CorrectButton (uint32_t which, uint8_t button) { } void -UpdatePoll (HWND windowHandle, bool useController) { +UpdatePoll (HWND windowHandle) { if (!CheckForegroundWindow (windowHandle)) return; - currentMouseState.ScrolledUp = false; - currentMouseState.ScrolledDown = false; - if (keyboardClean) { + if ((inputState & 1) && keyboardClean) { keyboardClean = false; for (int i=0; i<0xFF; i++) keyboardCount[i] -= (bool)keyboardDiff[i]; memset (keyboardDiff, 0, 0XFF); } - if (mouseWheelCount[0] > 0) mouseWheelCount[0] -= (bool)mouseWheelDiff[0]; - if (mouseWheelCount[1] > 0) mouseWheelCount[1] -= (bool)mouseWheelDiff[1]; - memset (mouseWheelDiff, 0, 2); + + if (inputState & (1 << 1)) { + currentMouseState.ScrolledUp = false; + currentMouseState.ScrolledDown = false; + if (mouseWheelCount[0] > 0) mouseWheelCount[0] -= (bool)mouseWheelDiff[0]; + if (mouseWheelCount[1] > 0) mouseWheelCount[1] -= (bool)mouseWheelDiff[1]; + memset (mouseWheelDiff, 0, 2); + } // GetCursorPos (¤tMouseState.Position); // ScreenToClient (windowHandle, ¤tMouseState.Position); - if (useController) { + if (inputState & (1 << 2)) { for (int i=0; i 0 ? 32767.0f : -32768.0f); - if (wndForeground || sensorValue == 0) { + if (wndForeground || sensorValue < axisThreshold) { switch (event.gaxis.axis) { - case SDL_GAMEPAD_AXIS_LEFTX: if (sensorValue < 0) { - currentControllerAxisState.LeftRight = 0.0f; - if (currentControllerAxisState.LeftLeft == 0.0f) { - controllerAxisCount[SDL_AXIS_LEFT_LEFT] = std::min (controllerAxisCount[SDL_AXIS_LEFT_LEFT] + 1, maxCount); + case SDL_GAMEPAD_AXIS_LEFTX: case SDL_GAMEPAD_AXIS_LEFTY: case SDL_GAMEPAD_AXIS_RIGHTX: case SDL_GAMEPAD_AXIS_RIGHTY: { + int direction = event.gaxis.axis * 2 + (event.gaxis.value < 0 ? 1 : 2); + int reverse = event.gaxis.axis * 2 + (event.gaxis.value < 0 ? 2 : 1); + currentControllerAxisState[reverse] = 0.0; + if (sensorValue > axisThreshold && currentControllerAxisState[direction] <= axisThreshold) { + LogMessage (LogLevel::DEBUG, "direction: {} value: {}", direction, sensorValue); + controllerAxisCount[direction] = std::min (controllerAxisCount[direction] + 1, maxCount); } - currentControllerAxisState.LeftLeft = -sensorValue; - } else { - currentControllerAxisState.LeftLeft = 0.0f; - if (currentControllerAxisState.LeftRight == 0.0f) { - controllerAxisCount[SDL_AXIS_LEFT_RIGHT] = std::min (controllerAxisCount[SDL_AXIS_LEFT_RIGHT] + 1, maxCount); - } - currentControllerAxisState.LeftRight = sensorValue; + currentControllerAxisState[direction] = sensorValue; } break; - case SDL_GAMEPAD_AXIS_LEFTY: if (sensorValue < 0) { - currentControllerAxisState.LeftDown = 0.0f; - if (currentControllerAxisState.LeftUp == 0.0f) { - controllerAxisCount[SDL_AXIS_LEFT_UP] = std::min (controllerAxisCount[SDL_AXIS_LEFT_UP] + 1, maxCount); + case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: { + int button = 5 + event.gaxis.axis; + if (sensorValue > axisThreshold && currentControllerAxisState[button] <= axisThreshold) { + controllerAxisCount[button] = std::min (controllerAxisCount[button] + 1, maxCount); } - currentControllerAxisState.LeftUp = -sensorValue; - } else { - currentControllerAxisState.LeftUp = 0.0f; - if (currentControllerAxisState.LeftDown == 0.0f) { - controllerAxisCount[SDL_AXIS_LEFT_DOWN] = std::min (controllerAxisCount[SDL_AXIS_LEFT_DOWN] + 1, maxCount); - } - currentControllerAxisState.LeftDown = sensorValue; + currentControllerAxisState[button] = sensorValue; } break; - case SDL_GAMEPAD_AXIS_RIGHTX: if (sensorValue < 0) { - currentControllerAxisState.RightRight = 0.0f; - if (currentControllerAxisState.RightLeft == 0.0f) { - controllerAxisCount[SDL_AXIS_RIGHT_LEFT] = std::min (controllerAxisCount[SDL_AXIS_RIGHT_LEFT] + 1, maxCount); - } - currentControllerAxisState.RightLeft = -sensorValue; - } else { - currentControllerAxisState.RightLeft = 0.0f; - if (currentControllerAxisState.RightRight == 0.0f) { - controllerAxisCount[SDL_AXIS_RIGHT_RIGHT] = std::min (controllerAxisCount[SDL_AXIS_RIGHT_RIGHT] + 1, maxCount); - } - currentControllerAxisState.RightRight = sensorValue; } break; - case SDL_GAMEPAD_AXIS_RIGHTY: if (sensorValue < 0) { - currentControllerAxisState.RightDown = 0.0f; - if (currentControllerAxisState.RightUp == 0.0f) { - controllerAxisCount[SDL_AXIS_RIGHT_UP] = std::min (controllerAxisCount[SDL_AXIS_RIGHT_UP] + 1, maxCount); - } - currentControllerAxisState.RightUp = -sensorValue; - } else { - currentControllerAxisState.RightUp = 0.0f; - if (currentControllerAxisState.RightDown == 0.0f) { - controllerAxisCount[SDL_AXIS_RIGHT_DOWN] = std::min (controllerAxisCount[SDL_AXIS_RIGHT_DOWN] + 1, maxCount); - } - currentControllerAxisState.RightDown = sensorValue; - } break; - case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: { - if (currentControllerAxisState.LTriggerDown == 0.0f) { - controllerAxisCount[SDL_AXIS_LTRIGGER_DOWN] = std::min (controllerAxisCount[SDL_AXIS_LTRIGGER_DOWN] + 1, maxCount); - } - currentControllerAxisState.LTriggerDown = sensorValue; - } break; - case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: { - if (currentControllerAxisState.RTriggerDown == 0.0f) { - controllerAxisCount[SDL_AXIS_RTRIGGER_DOWN] = std::min (controllerAxisCount[SDL_AXIS_RTRIGGER_DOWN] + 1, maxCount); - } - currentControllerAxisState.RTriggerDown = sensorValue; - } break; - } break; - } + } } } } @@ -520,19 +491,7 @@ ControllerButtonIsTapped (const SDL_GamepadButton button) { float ControllerAxisIsDown (const SDLAxis axis) { - switch (axis) { - case SDL_AXIS_LEFT_LEFT: return currentControllerAxisState.LeftLeft; - case SDL_AXIS_LEFT_RIGHT: return currentControllerAxisState.LeftRight; - case SDL_AXIS_LEFT_UP: return currentControllerAxisState.LeftUp; - case SDL_AXIS_LEFT_DOWN: return currentControllerAxisState.LeftDown; - case SDL_AXIS_RIGHT_LEFT: return currentControllerAxisState.RightLeft; - case SDL_AXIS_RIGHT_RIGHT: return currentControllerAxisState.RightRight; - case SDL_AXIS_RIGHT_UP: return currentControllerAxisState.RightUp; - case SDL_AXIS_RIGHT_DOWN: return currentControllerAxisState.RightDown; - case SDL_AXIS_LTRIGGER_DOWN: return currentControllerAxisState.LTriggerDown; - case SDL_AXIS_RTRIGGER_DOWN: return currentControllerAxisState.RTriggerDown; - default: return 0.0f; - } + return currentControllerAxisState[axis]; } bool