From 3a2db0135d93b5f12fd42f04db3b6ad9d40834d3 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 24 Jan 2015 23:42:12 +0100 Subject: SDL: Refactor WM specific functionality into SdlGraphicsManager. --- backends/events/sdl/sdl-events.cpp | 15 ++++----- backends/events/sdl/sdl-events.h | 5 --- backends/events/symbiansdl/symbiansdl-events.cpp | 4 ++- backends/graphics/dinguxsdl/dinguxsdl-graphics.cpp | 2 +- .../linuxmotosdl/linuxmotosdl-graphics.cpp | 2 +- backends/graphics/openglsdl/openglsdl-graphics.cpp | 4 +-- backends/graphics/sdl/sdl-graphics.cpp | 31 ++++++++++++++++++ backends/graphics/sdl/sdl-graphics.h | 38 ++++++++++++++++++++++ .../graphics/surfacesdl/surfacesdl-graphics.cpp | 8 ++--- backends/platform/sdl/sdl.cpp | 5 ++- 10 files changed, 88 insertions(+), 26 deletions(-) diff --git a/backends/events/sdl/sdl-events.cpp b/backends/events/sdl/sdl-events.cpp index 284e0970fd..d6d0731202 100644 --- a/backends/events/sdl/sdl-events.cpp +++ b/backends/events/sdl/sdl-events.cpp @@ -173,7 +173,9 @@ void SdlEventSource::handleKbdMouse() { _km.y_down_count = 1; } - SDL_WarpMouse((Uint16)_km.x, (Uint16)_km.y); + if (_graphicsManager) { + _graphicsManager->warpMouseInWindow((Uint16)_km.x, (Uint16)_km.y); + } } } } @@ -429,7 +431,9 @@ bool SdlEventSource::handleKeyDown(SDL_Event &ev, Common::Event &event) { // Ctrl-m toggles mouse capture if (event.kbd.hasFlags(Common::KBD_CTRL) && ev.key.keysym.sym == 'm') { - toggleMouseGrab(); + if (_graphicsManager) { + _graphicsManager->toggleMouseGrab(); + } return false; } @@ -752,13 +756,6 @@ bool SdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) { return false; } -void SdlEventSource::toggleMouseGrab() { - if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) - SDL_WM_GrabInput(SDL_GRAB_ON); - else - SDL_WM_GrabInput(SDL_GRAB_OFF); -} - void SdlEventSource::resetKeyboadEmulation(int16 x_max, int16 y_max) { _km.x_max = x_max; _km.y_max = y_max; diff --git a/backends/events/sdl/sdl-events.h b/backends/events/sdl/sdl-events.h index a1b6d5ec3c..7001e41db1 100644 --- a/backends/events/sdl/sdl-events.h +++ b/backends/events/sdl/sdl-events.h @@ -49,11 +49,6 @@ public: */ virtual void resetKeyboadEmulation(int16 x_max, int16 y_max); - /** - * Toggles mouse input grab - */ - virtual void toggleMouseGrab(); - protected: /** @name Keyboard mouse emulation * Disabled by fingolfin 2004-12-18. diff --git a/backends/events/symbiansdl/symbiansdl-events.cpp b/backends/events/symbiansdl/symbiansdl-events.cpp index 36018f1024..97361e3df0 100644 --- a/backends/events/symbiansdl/symbiansdl-events.cpp +++ b/backends/events/symbiansdl/symbiansdl-events.cpp @@ -133,7 +133,9 @@ bool SymbianSdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) { _currentZone = 0; event.type = Common::EVENT_MOUSEMOVE; processMouseEvent(event, _mouseXZone[_currentZone], _mouseYZone[_currentZone]); - SDL_WarpMouse(event.mouse.x, event.mouse.y); + if (_graphicsManager) { + _graphicsManager->warpMouseInWindow(event.mouse.x, event.mouse.y); + } } return true; diff --git a/backends/graphics/dinguxsdl/dinguxsdl-graphics.cpp b/backends/graphics/dinguxsdl/dinguxsdl-graphics.cpp index 343efa4da6..975e34a314 100644 --- a/backends/graphics/dinguxsdl/dinguxsdl-graphics.cpp +++ b/backends/graphics/dinguxsdl/dinguxsdl-graphics.cpp @@ -122,7 +122,7 @@ void DINGUXSdlGraphicsManager::initSize(uint w, uint h) { if (w > 320 || h > 240) { setGraphicsMode(GFX_HALF); setGraphicsModeIntern(); - _eventSource->toggleMouseGrab(); + toggleMouseGrab(); } _transactionDetails.sizeChanged = true; diff --git a/backends/graphics/linuxmotosdl/linuxmotosdl-graphics.cpp b/backends/graphics/linuxmotosdl/linuxmotosdl-graphics.cpp index 22b271ae1a..f8ab9930d7 100644 --- a/backends/graphics/linuxmotosdl/linuxmotosdl-graphics.cpp +++ b/backends/graphics/linuxmotosdl/linuxmotosdl-graphics.cpp @@ -134,7 +134,7 @@ void LinuxmotoSdlGraphicsManager::initSize(uint w, uint h) { if (w > 320 || h > 240) { setGraphicsMode(GFX_HALF); setGraphicsModeIntern(); - _eventSource->toggleMouseGrab(); + toggleMouseGrab(); } _transactionDetails.sizeChanged = true; diff --git a/backends/graphics/openglsdl/openglsdl-graphics.cpp b/backends/graphics/openglsdl/openglsdl-graphics.cpp index b028cd5b1a..fc2956755d 100644 --- a/backends/graphics/openglsdl/openglsdl-graphics.cpp +++ b/backends/graphics/openglsdl/openglsdl-graphics.cpp @@ -108,7 +108,7 @@ void OpenGLSdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable) case OSystem::kFeatureIconifyWindow: if (enable) { - SDL_WM_IconifyWindow(); + iconifyWindow(); } break; @@ -229,7 +229,7 @@ void OpenGLSdlGraphicsManager::notifyMousePos(Common::Point mouse) { } void OpenGLSdlGraphicsManager::setInternalMousePosition(int x, int y) { - SDL_WarpMouse(x, y); + warpMouseInWindow(x, y); } bool OpenGLSdlGraphicsManager::loadVideoMode(uint requestedWidth, uint requestedHeight, const Graphics::PixelFormat &format) { diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index b5e49fa397..d42c88f5d6 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -22,7 +22,9 @@ #include "backends/graphics/sdl/sdl-graphics.h" +#include "backends/platform/sdl/sdl-sys.h" #include "backends/events/sdl/sdl-events.h" +#include "common/textconsole.h" SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source) : _eventSource(source) { @@ -38,3 +40,32 @@ void SdlGraphicsManager::activateManager() { void SdlGraphicsManager::deactivateManager() { _eventSource->setGraphicsManager(0); } + +void SdlGraphicsManager::setWindowCaption(const Common::String &caption) { + SDL_WM_SetCaption(caption.c_str(), caption.c_str()); +} + +void SdlGraphicsManager::setWindowIcon(SDL_Surface *icon) { + SDL_WM_SetIcon(icon, NULL); + SDL_FreeSurface(icon); +} + +void SdlGraphicsManager::toggleMouseGrab() { + if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) { + SDL_WM_GrabInput(SDL_GRAB_ON); + } else { + SDL_WM_GrabInput(SDL_GRAB_OFF); + } +} + +bool SdlGraphicsManager::hasMouseFocus() const { + return (SDL_GetAppState() & SDL_APPMOUSEFOCUS); +} + +void SdlGraphicsManager::warpMouseInWindow(uint x, uint y) { + SDL_WarpMouse(x, y); +} + +void SdlGraphicsManager::iconifyWindow() { + SDL_WM_IconifyWindow(); +} diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 3ef540708a..216fc2d200 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -25,7 +25,9 @@ #include "backends/graphics/graphics.h" +#include "backends/platform/sdl/sdl-sys.h" #include "common/rect.h" +#include "common/str.h" class SdlEventSource; @@ -91,6 +93,42 @@ public: */ virtual void notifyMousePos(Common::Point mouse) = 0; + /** + * Change the caption of the window. + * + * @param caption New window caption in UTF-8 encoding. + */ + void setWindowCaption(const Common::String &caption); + + /** + * Attach an icon to the window. + * + * @param icon The surface to use as icon. SdlGraphicsManager takes + * ownership over it. + */ + void setWindowIcon(SDL_Surface *icon); + + /** + * Toggle mouse grab state. This decides whether the cursor can leave the + * window or not. + */ + void toggleMouseGrab(); + + /** + * Check whether the application has mouse focus. + */ + bool hasMouseFocus() const; + + /** + * Warp the mouse to the specified position in window coordinates. + */ + void warpMouseInWindow(uint x, uint y); + + /** + * Iconifies the window. + */ + void iconifyWindow(); + protected: SdlEventSource *_eventSource; }; diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index 7f3c99fcea..583c85e446 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -235,7 +235,7 @@ void SurfaceSdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable) break; case OSystem::kFeatureIconifyWindow: if (enable) - SDL_WM_IconifyWindow(); + iconifyWindow(); break; default: break; @@ -1710,7 +1710,7 @@ void SurfaceSdlGraphicsManager::warpMouse(int x, int y) { int y1 = y; // Don't change actual mouse position, when mouse is outside of our window (in case of windowed mode) - if (!(SDL_GetAppState( ) & SDL_APPMOUSEFOCUS)) { + if (!hasMouseFocus()) { setMousePos(x, y); // but change game cursor position return; } @@ -1720,9 +1720,9 @@ void SurfaceSdlGraphicsManager::warpMouse(int x, int y) { if (_mouseCurState.x != x || _mouseCurState.y != y) { if (!_overlayVisible) - SDL_WarpMouse(x * _videoMode.scaleFactor, y1 * _videoMode.scaleFactor); + warpMouseInWindow(x * _videoMode.scaleFactor, y1 * _videoMode.scaleFactor); else - SDL_WarpMouse(x, y1); + warpMouseInWindow(x, y1); // SDL_WarpMouse() generates a mouse movement event, so // setMousePos() would be called eventually. However, the diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index 4dc5929dab..6a2643b048 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -314,7 +314,7 @@ void OSystem_SDL::setWindowCaption(const char *caption) { } } - SDL_WM_SetCaption(cap.c_str(), cap.c_str()); + dynamic_cast(_graphicsManager)->setWindowCaption(cap); } void OSystem_SDL::quit() { @@ -477,8 +477,7 @@ void OSystem_SDL::setupIcon() { if (!sdl_surf) { warning("SDL_CreateRGBSurfaceFrom(icon) failed"); } - SDL_WM_SetIcon(sdl_surf, NULL); - SDL_FreeSurface(sdl_surf); + dynamic_cast(_graphicsManager)->setWindowIcon(sdl_surf); free(icon); } -- cgit v1.2.3