From f426ba6c36f4d825e427535514eb68855c64c2d1 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Sun, 10 Mar 2019 16:25:25 +0000 Subject: SDL: Move fullscreen and screenshot event handling to SdlGraphicsManager (#1522) --- backends/graphics/openglsdl/openglsdl-graphics.cpp | 79 ++++---------------- backends/graphics/openglsdl/openglsdl-graphics.h | 4 +- backends/graphics/sdl/sdl-graphics.cpp | 84 ++++++++++++++++++++++ backends/graphics/sdl/sdl-graphics.h | 13 +++- .../graphics/surfacesdl/surfacesdl-graphics.cpp | 72 ++----------------- backends/graphics/surfacesdl/surfacesdl-graphics.h | 9 +-- 6 files changed, 121 insertions(+), 140 deletions(-) diff --git a/backends/graphics/openglsdl/openglsdl-graphics.cpp b/backends/graphics/openglsdl/openglsdl-graphics.cpp index ef3b25e98c..a7e7cb1ba0 100644 --- a/backends/graphics/openglsdl/openglsdl-graphics.cpp +++ b/backends/graphics/openglsdl/openglsdl-graphics.cpp @@ -461,6 +461,10 @@ void OpenGLSdlGraphicsManager::handleResizeImpl(const int width, const int heigh SdlGraphicsManager::handleResizeImpl(width, height); } +bool OpenGLSdlGraphicsManager::saveScreenshot(const Common::String &filename) const { + return OpenGLGraphicsManager::saveScreenshot(filename); +} + bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) { // In case we request a fullscreen mode we will use the mode the user // has chosen last time or the biggest mode available. @@ -621,68 +625,13 @@ bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) { bool OpenGLSdlGraphicsManager::notifyEvent(const Common::Event &event) { switch (event.type) { case Common::EVENT_KEYUP: - return isHotkey(event); - - case Common::EVENT_KEYDOWN: - if (event.kbd.hasFlags(Common::KBD_ALT)) { - if ( event.kbd.keycode == Common::KEYCODE_RETURN - || event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER) { - // Alt-Return and Alt-Enter toggle full screen mode - beginGFXTransaction(); - setFeatureState(OSystem::kFeatureFullscreenMode, !getFeatureState(OSystem::kFeatureFullscreenMode)); - endGFXTransaction(); - -#ifdef USE_OSD - if (getFeatureState(OSystem::kFeatureFullscreenMode)) { - displayMessageOnOSD(_("Fullscreen mode")); - } else { - displayMessageOnOSD(_("Windowed mode")); - } -#endif - return true; - } + if (isHotkey(event)) + return true; - // Alt-s creates a screenshot - if (event.kbd.keycode == Common::KEYCODE_s) { - Common::String filename; - - Common::String screenshotsPath; - OSystem_SDL *sdl_g_system = dynamic_cast(g_system); - if (sdl_g_system) - screenshotsPath = sdl_g_system->getScreenshotsPath(); - - for (int n = 0;; n++) { - SDL_RWops *file; - -#ifdef USE_PNG - filename = Common::String::format("scummvm%05d.png", n); -#else - filename = Common::String::format("scummvm%05d.bmp", n); -#endif - - file = SDL_RWFromFile((screenshotsPath + filename).c_str(), "r"); - - if (!file) - break; - SDL_RWclose(file); - } - - if (saveScreenshot(screenshotsPath + filename)) { - if (screenshotsPath.empty()) - debug("Saved screenshot '%s' in current directory", filename.c_str()); - else - debug("Saved screenshot '%s' in directory '%s'", filename.c_str(), screenshotsPath.c_str()); - } else { - if (screenshotsPath.empty()) - warning("Could not save screenshot in current directory"); - else - warning("Could not save screenshot in directory '%s'", screenshotsPath.c_str()); - } - - return true; - } + break; - } else if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) { + case Common::EVENT_KEYDOWN: + if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) { if ( event.kbd.keycode == Common::KEYCODE_PLUS || event.kbd.keycode == Common::KEYCODE_MINUS || event.kbd.keycode == Common::KEYCODE_KP_PLUS || event.kbd.keycode == Common::KEYCODE_KP_MINUS) { // Ctrl+Alt+Plus/Minus Increase/decrease the size @@ -833,16 +782,14 @@ bool OpenGLSdlGraphicsManager::notifyEvent(const Common::Event &event) { // Fall through default: - return false; + break; } + + return SdlGraphicsManager::notifyEvent(event); } bool OpenGLSdlGraphicsManager::isHotkey(const Common::Event &event) const { - if (event.kbd.hasFlags(Common::KBD_ALT)) { - return event.kbd.keycode == Common::KEYCODE_RETURN - || event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER - || event.kbd.keycode == Common::KEYCODE_s; - } else if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) { + if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) { return event.kbd.keycode == Common::KEYCODE_PLUS || event.kbd.keycode == Common::KEYCODE_MINUS || event.kbd.keycode == Common::KEYCODE_KP_PLUS || event.kbd.keycode == Common::KEYCODE_KP_MINUS || event.kbd.keycode == Common::KEYCODE_a diff --git a/backends/graphics/openglsdl/openglsdl-graphics.h b/backends/graphics/openglsdl/openglsdl-graphics.h index b6ea496575..e30c2b8e49 100644 --- a/backends/graphics/openglsdl/openglsdl-graphics.h +++ b/backends/graphics/openglsdl/openglsdl-graphics.h @@ -30,7 +30,7 @@ #include "common/array.h" #include "common/events.h" -class OpenGLSdlGraphicsManager : public OpenGL::OpenGLGraphicsManager, public SdlGraphicsManager, public Common::EventObserver { +class OpenGLSdlGraphicsManager : public OpenGL::OpenGLGraphicsManager, public SdlGraphicsManager { public: OpenGLSdlGraphicsManager(uint desktopWidth, uint desktopHeight, SdlEventSource *eventSource, SdlWindow *window); virtual ~OpenGLSdlGraphicsManager(); @@ -72,6 +72,8 @@ protected: virtual void handleResizeImpl(const int width, const int height) override; + virtual bool saveScreenshot(const Common::String &filename) const override; + virtual int getGraphicsModeScale(int mode) const override { return 1; } private: diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index 1e37310ca2..767ce24949 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -22,10 +22,14 @@ #include "backends/graphics/sdl/sdl-graphics.h" #include "backends/platform/sdl/sdl-sys.h" +#include "backends/platform/sdl/sdl.h" #include "backends/events/sdl/sdl-events.h" #include "common/config-manager.h" #include "common/textconsole.h" #include "graphics/scaler/aspect.h" +#ifdef USE_OSD +#include "common/translation.h" +#endif SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source, SdlWindow *window) : _eventSource(source), _window(window), _hwScreen(nullptr) @@ -265,3 +269,83 @@ bool SdlGraphicsManager::createOrUpdateWindow(int width, int height, const Uint3 return true; } #endif + +void SdlGraphicsManager::saveScreenshot() { + Common::String filename; + + Common::String screenshotsPath; + OSystem_SDL *sdl_g_system = dynamic_cast(g_system); + if (sdl_g_system) + screenshotsPath = sdl_g_system->getScreenshotsPath(); + + for (int n = 0;; n++) { + SDL_RWops *file; + +#ifdef USE_PNG + filename = Common::String::format("scummvm%05d.png", n); +#else + filename = Common::String::format("scummvm%05d.bmp", n); +#endif + + file = SDL_RWFromFile((screenshotsPath + filename).c_str(), "r"); + + if (!file) + break; + SDL_RWclose(file); + } + + if (saveScreenshot(screenshotsPath + filename)) { + if (screenshotsPath.empty()) + debug("Saved screenshot '%s' in current directory", filename.c_str()); + else + debug("Saved screenshot '%s' in directory '%s'", filename.c_str(), screenshotsPath.c_str()); + } else { + if (screenshotsPath.empty()) + warning("Could not save screenshot in current directory"); + else + warning("Could not save screenshot in directory '%s'", screenshotsPath.c_str()); + } +} + +bool SdlGraphicsManager::notifyEvent(const Common::Event &event) { + switch ((int)event.type) { + case Common::EVENT_KEYDOWN: + // Alt-Return and Alt-Enter toggle full screen mode + if (event.kbd.hasFlags(Common::KBD_ALT) && + (event.kbd.keycode == Common::KEYCODE_RETURN || + event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER)) { + beginGFXTransaction(); + setFeatureState(OSystem::kFeatureFullscreenMode, !getFeatureState(OSystem::kFeatureFullscreenMode)); + endGFXTransaction(); +#ifdef USE_OSD + if (getFeatureState(OSystem::kFeatureFullscreenMode)) + displayMessageOnOSD(_("Fullscreen mode")); + else + displayMessageOnOSD(_("Windowed mode")); +#endif + return true; + } + + // Alt-S: Create a screenshot + if (event.kbd.hasFlags(Common::KBD_ALT) && event.kbd.keycode == 's') { + saveScreenshot(); + return true; + } + + break; + + case Common::EVENT_KEYUP: + if (event.kbd.hasFlags(Common::KBD_ALT)) { + return event.kbd.keycode == Common::KEYCODE_RETURN + || event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER + || event.kbd.keycode == Common::KEYCODE_s; + } + + break; + + default: + break; + } + + return false; +} diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 526306270b..d16402f57c 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -26,14 +26,19 @@ #include "backends/graphics/windowed.h" #include "backends/platform/sdl/sdl-window.h" +#include "common/events.h" #include "common/rect.h" class SdlEventSource; +#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__) +#define USE_OSD 1 +#endif + /** * Base class for a SDL based graphics manager. */ -class SdlGraphicsManager : virtual public WindowedGraphicsManager { +class SdlGraphicsManager : virtual public WindowedGraphicsManager, public Common::EventObserver { public: SdlGraphicsManager(SdlEventSource *source, SdlWindow *window); virtual ~SdlGraphicsManager() {} @@ -91,6 +96,12 @@ public: virtual bool showMouse(const bool visible) override; + virtual bool saveScreenshot(const Common::String &filename) const { return false; } + void saveScreenshot(); + + // Override from Common::EventObserver + virtual bool notifyEvent(const Common::Event &event) override; + /** * A (subset) of the graphic manager's state. This is used when switching * between different SDL graphic managers at runtime. diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index 37965d057e..22db3bd5e3 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -25,7 +25,6 @@ #if defined(SDL_BACKEND) #include "backends/graphics/surfacesdl/surfacesdl-graphics.h" #include "backends/events/sdl/sdl-events.h" -#include "backends/platform/sdl/sdl.h" #include "common/config-manager.h" #include "common/mutex.h" #include "common/textconsole.h" @@ -1419,7 +1418,7 @@ void SurfaceSdlGraphicsManager::internUpdateScreen() { _cursorNeedsRedraw = false; } -bool SurfaceSdlGraphicsManager::saveScreenshot(const char *filename) { +bool SurfaceSdlGraphicsManager::saveScreenshot(const Common::String &filename) const { assert(_hwScreen != NULL); Common::StackLock lock(_graphicsMutex); @@ -1484,7 +1483,7 @@ bool SurfaceSdlGraphicsManager::saveScreenshot(const char *filename) { return success; #else - return SDL_SaveBMP(_hwScreen, filename) == 0; + return SDL_SaveBMP(_hwScreen, filename.c_str()) == 0; #endif } @@ -2672,69 +2671,9 @@ bool SurfaceSdlGraphicsManager::isScalerHotkey(const Common::Event &event) { return false; } -void SurfaceSdlGraphicsManager::toggleFullScreen() { - beginGFXTransaction(); - setFullscreenMode(!_videoMode.fullscreen); - endGFXTransaction(); -#ifdef USE_OSD - if (_videoMode.fullscreen) - displayMessageOnOSD(_("Fullscreen mode")); - else - displayMessageOnOSD(_("Windowed mode")); -#endif -} - bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) { switch ((int)event.type) { case Common::EVENT_KEYDOWN: - // Alt-Return and Alt-Enter toggle full screen mode - if (event.kbd.hasFlags(Common::KBD_ALT) && - (event.kbd.keycode == Common::KEYCODE_RETURN || - event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER)) { - toggleFullScreen(); - return true; - } - - // Alt-S: Create a screenshot - if (event.kbd.hasFlags(Common::KBD_ALT) && event.kbd.keycode == 's') { - Common::String filename; - - Common::String screenshotsPath; - OSystem_SDL *sdl_g_system = dynamic_cast(g_system); - if (sdl_g_system) - screenshotsPath = sdl_g_system->getScreenshotsPath(); - - for (int n = 0;; n++) { - SDL_RWops *file; - -#ifdef USE_PNG - filename = Common::String::format("scummvm%05d.png", n); -#else - filename = Common::String::format("scummvm%05d.bmp", n); -#endif - - file = SDL_RWFromFile((screenshotsPath + filename).c_str(), "r"); - - if (!file) - break; - SDL_RWclose(file); - } - - if (saveScreenshot((screenshotsPath + filename).c_str())) { - if (screenshotsPath.empty()) - debug("Saved screenshot '%s' in current directory", filename.c_str()); - else - debug("Saved screenshot '%s' in directory '%s'", filename.c_str(), screenshotsPath.c_str()); - } else { - if (screenshotsPath.empty()) - warning("Could not save screenshot in current directory"); - else - warning("Could not save screenshot in directory '%s'", screenshotsPath.c_str()); - } - - return true; - } - // Ctrl-Alt- will change the GFX mode if (event.kbd.hasFlags(Common::KBD_CTRL|Common::KBD_ALT)) { if (handleScalerHotkeys(event.kbd.keycode)) @@ -2744,13 +2683,16 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) { break; case Common::EVENT_KEYUP: - return isScalerHotkey(event); + if (isScalerHotkey(event)) + return true; + + break; default: break; } - return false; + return SdlGraphicsManager::notifyEvent(event); } void SurfaceSdlGraphicsManager::notifyVideoExpose() { diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h index 3315533d80..3db2ac6709 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.h +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h @@ -39,10 +39,6 @@ #define USE_SDL_DEBUG_FOCUSRECT #endif -#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__) -#define USE_OSD 1 -#endif - enum { GFX_NORMAL = 0, GFX_DOUBLESIZE = 1, @@ -74,7 +70,7 @@ public: /** * SDL graphics manager */ -class SurfaceSdlGraphicsManager : public SdlGraphicsManager, public Common::EventObserver { +class SurfaceSdlGraphicsManager : public SdlGraphicsManager { public: SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window); virtual ~SurfaceSdlGraphicsManager(); @@ -389,14 +385,13 @@ protected: virtual void setAspectRatioCorrection(bool enable); void setFilteringMode(bool enable); - virtual bool saveScreenshot(const char *filename); + virtual bool saveScreenshot(const Common::String &filename) const; virtual void setGraphicsModeIntern(); private: void setFullscreenMode(bool enable); bool handleScalerHotkeys(Common::KeyCode key); bool isScalerHotkey(const Common::Event &event); - void toggleFullScreen(); /** * Converts the given point from the overlay's coordinate space to the -- cgit v1.2.3