From c810980b4290627cbdad86d2d28ec526c9fe78fd Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 24 Aug 2024 17:31:53 +0100 Subject: [PATCH] Further SDL3 compatibility changes. Signed-off-by: Andrea Odetti --- source/frontends/sdl/imgui/sdlimguiframe.cpp | 2 +- .../frontends/sdl/renderer/sdlrendererframe.h | 2 +- source/frontends/sdl/sdirectsound.cpp | 2 +- source/frontends/sdl/sdlcompat.cpp | 8 +++----- source/frontends/sdl/sdlcompat.h | 8 +++++++- source/frontends/sdl/sdlframe.cpp | 20 +++++++++++++------ source/frontends/sdl/utils.cpp | 8 ++++---- source/frontends/sdl/utils.h | 2 +- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/source/frontends/sdl/imgui/sdlimguiframe.cpp b/source/frontends/sdl/imgui/sdlimguiframe.cpp index 07fedff60..68fc5a959 100644 --- a/source/frontends/sdl/imgui/sdlimguiframe.cpp +++ b/source/frontends/sdl/imgui/sdlimguiframe.cpp @@ -293,7 +293,7 @@ namespace sa2 { const size_t modifiers = getCanonicalModifiers(key); - switch (key.keysym.sym) + switch (SA2_KEY_CODE(key)) { case SDLK_F8: { diff --git a/source/frontends/sdl/renderer/sdlrendererframe.h b/source/frontends/sdl/renderer/sdlrendererframe.h index 5d1bbb476..fc2b36996 100644 --- a/source/frontends/sdl/renderer/sdlrendererframe.h +++ b/source/frontends/sdl/renderer/sdlrendererframe.h @@ -22,7 +22,7 @@ namespace sa2 private: - static constexpr SDL_PixelFormatEnum ourPixelFormat = SDL_PIXELFORMAT_ARGB8888; + static constexpr PixelFormat_t ourPixelFormat = SDL_PIXELFORMAT_ARGB8888; Renderer_Rect_t myRect; int myPitch; diff --git a/source/frontends/sdl/sdirectsound.cpp b/source/frontends/sdl/sdirectsound.cpp index 22a5e28c3..830ebe17c 100644 --- a/source/frontends/sdl/sdirectsound.cpp +++ b/source/frontends/sdl/sdirectsound.cpp @@ -140,7 +140,7 @@ namespace myAudioSpec.freq = mySampleRate; myAudioSpec.format = AUDIO_S16LSB; myAudioSpec.channels = myChannels; - myAudioStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &myAudioSpec, staticAudioCallback3, this); + myAudioStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &myAudioSpec, staticAudioCallback3, this); if (myAudioStream) { myAudioDevice = SDL_GetAudioStreamDevice(myAudioStream); diff --git a/source/frontends/sdl/sdlcompat.cpp b/source/frontends/sdl/sdlcompat.cpp index 9eedf9e1d..235b8dfaa 100644 --- a/source/frontends/sdl/sdlcompat.cpp +++ b/source/frontends/sdl/sdlcompat.cpp @@ -13,12 +13,11 @@ namespace sa2 int getNumJoysticks() { int count = 0; - SDL_JoystickID* joy = SDL_GetJoysticks(&count); + const SDL_JoystickID* joy = SDL_GetJoysticks(&count); if (!joy) { throw std::runtime_error(decorateSDLError("SDL_GetJoysticks")); } - SDL_free(joy); return count; } @@ -35,11 +34,10 @@ namespace sa2 const SDL_DisplayMode * getCurrentDisplayMode() { int count = 0; - SDL_DisplayID * displays = SDL_GetDisplays(&count); + const SDL_DisplayID * displays = SDL_GetDisplays(&count); if (displays) { const SDL_DisplayMode * mode = SDL_GetCurrentDisplayMode(*displays); - SDL_free(displays); return mode; } throw std::runtime_error(decorateSDLError("SDL_GetDisplays")); @@ -78,7 +76,7 @@ namespace sa2 const char * name = index >= 0 ? SDL_GetRenderDriver(index) : nullptr; SDL_PropertiesID props = SDL_CreateProperties(); - SDL_SetProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); + SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); SDL_SetBooleanProperty(props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, SDL_TRUE); if (index >= 0) { diff --git a/source/frontends/sdl/sdlcompat.h b/source/frontends/sdl/sdlcompat.h index f230421a3..45f25509b 100644 --- a/source/frontends/sdl/sdlcompat.h +++ b/source/frontends/sdl/sdlcompat.h @@ -17,9 +17,12 @@ #define SA2_CONTROLLER_BUTTON(e) e.gbutton.button #define SA2_DROP_FILE(d) d.data #define SA2_RENDERER_LOGICAL_SIZE(r, w, h) SDL_RenderSetLogicalSize(r, w, h, SDL_LOGICAL_PRESENTATION_STRETCH, SDL_SCALEMODE_NEAREST) - #define SA2_IMAGE_BITS(s) s->format->bits_per_pixel + #define SA2_IMAGE_BITS(s) SDL_GetPixelFormatDetails(s->format)->bits_per_pixel + #define SA2_KEY_CODE(e) e.key + #define SA2_KEY_MOD(e) e.mod typedef SDL_FRect Renderer_Rect_t; + typedef SDL_PixelFormat PixelFormat_t; #else #include #include @@ -33,8 +36,11 @@ #define SA2_DROP_FILE(d) d.file #define SA2_RENDERER_LOGICAL_SIZE(r, w, h) SDL_RenderSetLogicalSize(r, w, h) #define SA2_IMAGE_BITS(s) s->format->BitsPerPixel + #define SA2_KEY_CODE(e) e.keysym.sym + #define SA2_KEY_MOD(e) e.keysym.mod typedef SDL_Rect Renderer_Rect_t; + typedef SDL_PixelFormatEnum PixelFormat_t; #endif namespace common2 diff --git a/source/frontends/sdl/sdlframe.cpp b/source/frontends/sdl/sdlframe.cpp index 21e030f06..3b59ae2fb 100644 --- a/source/frontends/sdl/sdlframe.cpp +++ b/source/frontends/sdl/sdlframe.cpp @@ -46,7 +46,7 @@ namespace // but this makes it impossible to detect CTRL-ASCII... more to follow BYTE ch = 0; - switch (key.keysym.sym) + switch (SA2_KEY_CODE(key)) { case SDLK_RETURN: case SDLK_KP_ENTER: @@ -96,10 +96,10 @@ namespace // CAPS is forced when the emulator starts // until is used the first time const bool capsLock = forceCapsLock || (SDL_GetModState() & KMOD_CAPS); - const bool upper = capsLock || (key.keysym.mod & KMOD_SHIFT); + const bool upper = capsLock || (SA2_KEY_MOD(key) & KMOD_SHIFT); - ch = (key.keysym.sym - SDLK_a) + 0x01; - if (key.keysym.mod & KMOD_CTRL) + ch = (SA2_KEY_CODE(key) - SDLK_a) + 0x01; + if (SA2_KEY_MOD(key) & KMOD_CTRL) { // ok } @@ -455,7 +455,7 @@ namespace sa2 if (!key.repeat) { const size_t modifiers = getCanonicalModifiers(key); - switch (key.keysym.sym) + switch (SA2_KEY_CODE(key)) { case SDLK_F12: { @@ -581,12 +581,20 @@ namespace sa2 } else if (modifiers == KMOD_SHIFT) { +#ifdef SA2_SDL3 + const char * text = SDL_GetClipboardText(); + if (text) + { + addTextToBuffer(text); + } +#else char * text = SDL_GetClipboardText(); if (text) { addTextToBuffer(text); SDL_free(text); } +#endif } else if (modifiers == KMOD_CTRL) { @@ -614,7 +622,7 @@ namespace sa2 void SDLFrame::ProcessKeyUp(const SDL_KeyboardEvent & key) { - switch (key.keysym.sym) + switch (SA2_KEY_CODE(key)) { case SDLK_LALT: { diff --git a/source/frontends/sdl/utils.cpp b/source/frontends/sdl/utils.cpp index dad99a2e8..74109b099 100644 --- a/source/frontends/sdl/utils.cpp +++ b/source/frontends/sdl/utils.cpp @@ -73,7 +73,7 @@ namespace sa2 void printRendererInfo(std::ostream & os, const std::shared_ptr & ren, - const SDL_PixelFormatEnum pixelFormat, + const PixelFormat_t pixelFormat, const int selectedDriver) { const size_t n = SDL_GetNumRenderDrivers(); @@ -154,15 +154,15 @@ namespace sa2 // simplification of the handling of left and right version of the modifiers // so we can use equality == comparisons size_t modifiers = KMOD_NONE; - if (key.keysym.mod & KMOD_CTRL) + if (SA2_KEY_MOD(key) & KMOD_CTRL) { modifiers |= KMOD_CTRL; } - if (key.keysym.mod & KMOD_SHIFT) + if (SA2_KEY_MOD(key) & KMOD_SHIFT) { modifiers |= KMOD_SHIFT; } - if (key.keysym.mod & KMOD_ALT) + if (SA2_KEY_MOD(key) & KMOD_ALT) { modifiers |= KMOD_ALT; } diff --git a/source/frontends/sdl/utils.h b/source/frontends/sdl/utils.h index b7a45cbb4..6320e13c7 100644 --- a/source/frontends/sdl/utils.h +++ b/source/frontends/sdl/utils.h @@ -14,7 +14,7 @@ namespace sa2 void printRendererInfo(std::ostream & os, const std::shared_ptr & ren, - const SDL_PixelFormatEnum pixelFormat, + const PixelFormat_t pixelFormat, const int selectedDriver); bool show_yes_no_dialog(const std::shared_ptr & win,