diff --git a/SourceX/DiabloUI/credits.cpp b/SourceX/DiabloUI/credits.cpp index 82dc9c731..42f6bb234 100644 --- a/SourceX/DiabloUI/credits.cpp +++ b/SourceX/DiabloUI/credits.cpp @@ -258,8 +258,6 @@ BOOL UiCreditsDialog(int a1) case SDL_MOUSEBUTTONDOWN: endMenu = true; break; - case SDL_QUIT: - exit(0); default: switch (GetMenuAction(event)) { case MenuAction::BACK: @@ -270,6 +268,7 @@ BOOL UiCreditsDialog(int a1) break; } } + UiHandleEvents(&event); } } while (!endMenu && !credits_renderer.Finished()); BlackPalette(); diff --git a/SourceX/DiabloUI/diabloui.cpp b/SourceX/DiabloUI/diabloui.cpp index 817b68453..f217ef856 100644 --- a/SourceX/DiabloUI/diabloui.cpp +++ b/SourceX/DiabloUI/diabloui.cpp @@ -15,6 +15,7 @@ #include "DiabloUI/fonts.h" #include "DiabloUI/button.h" #include "DiabloUI/dialogs.h" +#include "controls/controller.h" #ifdef __SWITCH__ // for virtual keyboard on Switch @@ -187,9 +188,6 @@ void selhero_CatToName(char *in_buf, char *out_buf, int cnt) bool UiFocusNavigation(SDL_Event *event) { - if (event->type == SDL_QUIT) - exit(0); - switch (GetMenuAction(*event)) { case MenuAction::SELECT: UiFocusNavigationSelect(); @@ -302,6 +300,19 @@ bool UiFocusNavigation(SDL_Event *event) return false; } +void UiHandleEvents(SDL_Event *event) +{ + if (event->type == SDL_QUIT) + exit(0); + +#ifndef USE_SDL1 + if (event->type == SDL_JOYDEVICEADDED || event->type == SDL_JOYDEVICEREMOVED) { + InitController(); + return; + } +#endif +} + void UiFocusNavigationSelect() { UiPlaySelectSound(); @@ -544,6 +555,7 @@ void UiPollAndRender() SDL_Event event; while (SDL_PollEvent(&event)) { UiFocusNavigation(&event); + UiHandleEvents(&event); } UiRenderItems(gUiItems, gUiItemCnt); DrawMouse(); diff --git a/SourceX/DiabloUI/diabloui.h b/SourceX/DiabloUI/diabloui.h index c8d4a3b3f..5c45e7ba5 100644 --- a/SourceX/DiabloUI/diabloui.h +++ b/SourceX/DiabloUI/diabloui.h @@ -41,6 +41,7 @@ extern void (*gfnSoundFunction)(char *file); bool IsInsideRect(const SDL_Event &event, const SDL_Rect &rect); void UiFadeIn(); bool UiFocusNavigation(SDL_Event *event); +void UiHandleEvents(SDL_Event *event); bool UiItemMouseEvents(SDL_Event *event, UiItem *items, std::size_t size); int GetCenterOffset(int w, int bw = 0); void LoadPalInMem(const PALETTEENTRY *pPal); diff --git a/SourceX/DiabloUI/dialogs.cpp b/SourceX/DiabloUI/dialogs.cpp index b68b21d4d..141afb4aa 100644 --- a/SourceX/DiabloUI/dialogs.cpp +++ b/SourceX/DiabloUI/dialogs.cpp @@ -262,8 +262,6 @@ void DialogLoop(UiItem *items, std::size_t num_items, UiItem *render_behind, std case SDL_MOUSEBUTTONUP: UiItemMouseEvents(&event, items, num_items); break; - case SDL_QUIT: - exit(0); default: switch (GetMenuAction(event)) { case MenuAction::BACK: @@ -275,6 +273,7 @@ void DialogLoop(UiItem *items, std::size_t num_items, UiItem *render_behind, std } break; } + UiHandleEvents(&event); } if (render_behind_size == 0) { diff --git a/SourceX/DiabloUI/progress.cpp b/SourceX/DiabloUI/progress.cpp index 78171a1fc..65e95a3df 100644 --- a/SourceX/DiabloUI/progress.cpp +++ b/SourceX/DiabloUI/progress.cpp @@ -108,9 +108,8 @@ BOOL UiProgressDialog(HWND window, char *msg, int enable, int (*fnfunc)(), int r case SDL_MOUSEBUTTONDOWN: endMenu = true; break; - case SDL_QUIT: - exit(0); } + UiHandleEvents(&event); } } BlackPalette(); diff --git a/SourceX/DiabloUI/title.cpp b/SourceX/DiabloUI/title.cpp index 6bdb43b64..31de2b250 100644 --- a/SourceX/DiabloUI/title.cpp +++ b/SourceX/DiabloUI/title.cpp @@ -50,9 +50,8 @@ void UiTitleDialog() case SDL_MOUSEBUTTONDOWN: endMenu = true; break; - case SDL_QUIT: - exit(0); } + UiHandleEvents(&event); } } BlackPalette(); diff --git a/SourceX/controls/devices/joystick.cpp b/SourceX/controls/devices/joystick.cpp index be6949d58..66046fb7e 100644 --- a/SourceX/controls/devices/joystick.cpp +++ b/SourceX/controls/devices/joystick.cpp @@ -260,8 +260,17 @@ int CurrentJoystickIndex() void InitJoystick() { - if (SDL_NumJoysticks() == 0) +#if HAS_KBCTRL == 1 + sgbControllerActive = true; +#endif + + if (SDL_NumJoysticks() == 0) { + current_joystick_index = -1; +#if HAS_KBCTRL == 0 + sgbControllerActive = false; +#endif return; + } // Get the first available controller. for (int i = 0; i < SDL_NumJoysticks(); ++i) { @@ -276,6 +285,7 @@ void InitJoystick() continue; } current_joystick_index = i; + sgbControllerActive = true; break; } } diff --git a/SourceX/controls/menu_controls.cpp b/SourceX/controls/menu_controls.cpp index daddf813f..cdbb65229 100644 --- a/SourceX/controls/menu_controls.cpp +++ b/SourceX/controls/menu_controls.cpp @@ -7,7 +7,9 @@ namespace dvl { MenuAction GetMenuAction(const SDL_Event &event) { const ControllerButtonEvent ctrl_event = ToControllerButtonEvent(event); - sgbControllerActive = true; + if (ctrl_event.button != ControllerButton::NONE) + sgbControllerActive = true; + if (!ctrl_event.up) { switch (ctrl_event.button) { case ControllerButton::IGNORE: @@ -36,10 +38,11 @@ MenuAction GetMenuAction(const SDL_Event &event) break; } } - if (ctrl_event.button == ControllerButton::NONE && (event.type < SDL_JOYAXISMOTION || event.type >= 0x700)) - sgbControllerActive = false; #if HAS_KBCTRL == 0 + if (event.type >= SDL_KEYDOWN && event.type < SDL_JOYAXISMOTION) + sgbControllerActive = false; + if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_UP: diff --git a/SourceX/miniwin/misc.cpp b/SourceX/miniwin/misc.cpp index ee6b1362b..dacf0a95d 100644 --- a/SourceX/miniwin/misc.cpp +++ b/SourceX/miniwin/misc.cpp @@ -128,9 +128,8 @@ bool SpawnWindow(LPCSTR lpWindowName, int nWidth, int nHeight) #ifdef USE_SDL1 SDL_EnableUNICODE(1); -#endif - InitController(); +#endif int upscale = 1; DvlIntSetting("upscale", &upscale); diff --git a/SourceX/miniwin/misc_msg.cpp b/SourceX/miniwin/misc_msg.cpp index 91cb3dcce..17d31191c 100644 --- a/SourceX/miniwin/misc_msg.cpp +++ b/SourceX/miniwin/misc_msg.cpp @@ -10,6 +10,7 @@ #include "controls/plrctrls.h" #include "controls/touch.h" #include "miniwin/ddraw.h" +#include "controls/controller.h" #ifdef __SWITCH__ #include "platform/switch/docking.h" @@ -480,6 +481,15 @@ WINBOOL PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilter } switch (e.type) { +#ifndef USE_SDL1 + case SDL_CONTROLLERDEVICEADDED: + case SDL_CONTROLLERDEVICEREMOVED: + break; + case SDL_JOYDEVICEADDED: + case SDL_JOYDEVICEREMOVED: + InitController(); + break; +#endif case SDL_QUIT: lpMsg->message = DVL_WM_QUIT; break;