ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

sdl-window.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00024 
00025 #include "backends/platform/sdl/sdl-window.h"
00026 
00027 #include "common/textconsole.h"
00028 
00029 #include "icons/residualvm.xpm"
00030 
00031 #if SDL_VERSION_ATLEAST(2, 0, 0)
00032 static const uint32 fullscreenMask = SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN;
00033 #endif
00034 
00035 SdlWindow::SdlWindow()
00036 #if SDL_VERSION_ATLEAST(2, 0, 0)
00037     : _window(nullptr), _inputGrabState(false), _windowCaption("ResidualVM"),
00038     _lastFlags(0), _lastX(SDL_WINDOWPOS_UNDEFINED), _lastY(SDL_WINDOWPOS_UNDEFINED)
00039 #endif
00040     {
00041 
00042 #if !SDL_VERSION_ATLEAST(2, 0, 0)
00043     // Query the desktop resolution. We simply hope nothing tried to change
00044     // the resolution so far.
00045     const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
00046     if (videoInfo && videoInfo->current_w > 0 && videoInfo->current_h > 0) {
00047         _desktopRes = Common::Rect(videoInfo->current_w, videoInfo->current_h);
00048     }
00049 #endif
00050 }
00051 
00052 SdlWindow::~SdlWindow() {
00053 #if SDL_VERSION_ATLEAST(2, 0, 0)
00054     destroyWindow();
00055 #endif
00056 }
00057 
00058 void SdlWindow::setupIcon() {
00059     int x, y, w, h, ncols, nbytes, i;
00060     unsigned int rgba[256];
00061     unsigned int *icon;
00062 
00063     if (sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes) != 4) {
00064         warning("Wrong format of scummvm_icon[0] (%s)", scummvm_icon[0]);
00065 
00066         return;
00067     }
00068     if ((w > 512) || (h > 512) || (ncols > 255) || (nbytes > 1)) {
00069         warning("Could not load the built-in icon (%d %d %d %d)", w, h, ncols, nbytes);
00070         return;
00071     }
00072     icon = (unsigned int*)malloc(w*h*sizeof(unsigned int));
00073     if (!icon) {
00074         warning("Could not allocate temp storage for the built-in icon");
00075         return;
00076     }
00077 
00078     for (i = 0; i < ncols; i++) {
00079         unsigned char code;
00080         char color[32];
00081         memset(color, 0, sizeof(color));
00082         unsigned int col;
00083         if (sscanf(scummvm_icon[1 + i], "%c c %s", &code, color) != 2) {
00084             warning("Wrong format of scummvm_icon[%d] (%s)", 1 + i, scummvm_icon[1 + i]);
00085         }
00086         if (!strcmp(color, "None"))
00087             col = 0x00000000;
00088         else if (!strcmp(color, "black"))
00089             col = 0xFF000000;
00090         else if (!strcmp(color, "gray20"))
00091             col = 0xFF333333;
00092         else if (color[0] == '#') {
00093             if (sscanf(color + 1, "%06x", &col) != 1) {
00094                 warning("Wrong format of color (%s)", color + 1);
00095             }
00096             col |= 0xFF000000;
00097         } else {
00098             warning("Could not load the built-in icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]);
00099             free(icon);
00100             return;
00101         }
00102 
00103         rgba[code] = col;
00104     }
00105     for (y = 0; y < h; y++) {
00106         const char *line = scummvm_icon[1 + ncols + y];
00107         for (x = 0; x < w; x++) {
00108             icon[x + w * y] = rgba[(int)line[x]];
00109         }
00110     }
00111 
00112     SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, w, h, 32, w * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
00113     if (!sdl_surf) {
00114         warning("SDL_CreateRGBSurfaceFrom(icon) failed");
00115     }
00116 
00117 #if SDL_VERSION_ATLEAST(2, 0, 0)
00118     if (_window) {
00119         SDL_SetWindowIcon(_window, sdl_surf);
00120     }
00121 #else
00122     SDL_WM_SetIcon(sdl_surf, NULL);
00123 #endif
00124 
00125     SDL_FreeSurface(sdl_surf);
00126     free(icon);
00127 }
00128 
00129 void SdlWindow::setWindowCaption(const Common::String &caption) {
00130 #if SDL_VERSION_ATLEAST(2, 0, 0)
00131     _windowCaption = caption;
00132     if (_window) {
00133         SDL_SetWindowTitle(_window, caption.c_str());
00134     }
00135 #else
00136     SDL_WM_SetCaption(caption.c_str(), caption.c_str());
00137 #endif
00138 }
00139 
00140 void SdlWindow::toggleMouseGrab() {
00141 #if SDL_VERSION_ATLEAST(2, 0, 0)
00142     if (_window) {
00143         _inputGrabState = SDL_GetWindowGrab(_window) == SDL_FALSE;
00144         SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE);
00145     }
00146 #else
00147     if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
00148         SDL_WM_GrabInput(SDL_GRAB_ON);
00149         _inputGrabState = true;
00150     } else {
00151         SDL_WM_GrabInput(SDL_GRAB_OFF);
00152         _inputGrabState = false;
00153     }
00154 #endif
00155 }
00156 
00157 bool SdlWindow::hasMouseFocus() const {
00158 #if SDL_VERSION_ATLEAST(2, 0, 0)
00159     if (_window) {
00160         return (SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_FOCUS);
00161     } else {
00162         return false;
00163     }
00164 #else
00165     return (SDL_GetAppState() & SDL_APPMOUSEFOCUS);
00166 #endif
00167 }
00168 
00169 bool SdlWindow::warpMouseInWindow(int x, int y) {
00170     if (hasMouseFocus()) {
00171 #if SDL_VERSION_ATLEAST(2, 0, 0)
00172         if (_window) {
00173             SDL_WarpMouseInWindow(_window, x, y);
00174             return true;
00175         }
00176 #else
00177         SDL_WarpMouse(x, y);
00178         return true;
00179 #endif
00180     }
00181 
00182     return false;
00183 }
00184 
00185 void SdlWindow::iconifyWindow() {
00186 #if SDL_VERSION_ATLEAST(2, 0, 0)
00187     if (_window) {
00188         SDL_MinimizeWindow(_window);
00189     }
00190 #else
00191     SDL_WM_IconifyWindow();
00192 #endif
00193 }
00194 
00195 bool SdlWindow::getSDLWMInformation(SDL_SysWMinfo *info) const {
00196     SDL_VERSION(&info->version);
00197 #if SDL_VERSION_ATLEAST(2, 0, 0)
00198     return _window ? (SDL_GetWindowWMInfo(_window, info) == SDL_TRUE) : false;
00199 #else
00200     return SDL_GetWMInfo(info);
00201 #endif
00202 }
00203 
00204 Common::Rect SdlWindow::getDesktopResolution() {
00205 #if SDL_VERSION_ATLEAST(2, 0, 0)
00206     int displayIndex = _window ? SDL_GetWindowDisplayIndex(_window) : 0;
00207     SDL_DisplayMode displayMode;
00208     if (!SDL_GetDesktopDisplayMode(displayIndex, &displayMode)) {
00209         _desktopRes = Common::Rect(displayMode.w, displayMode.h);
00210     }
00211 #endif
00212 
00213     return _desktopRes;
00214 }
00215 
00216 
00217 #if SDL_VERSION_ATLEAST(2, 0, 0)
00218 SDL_Surface *copySDLSurface(SDL_Surface *src) {
00219     const bool locked = SDL_MUSTLOCK(src) == SDL_TRUE;
00220 
00221     if (locked) {
00222         if (SDL_LockSurface(src) != 0) {
00223             return nullptr;
00224         }
00225     }
00226 
00227     SDL_Surface *res = SDL_CreateRGBSurfaceFrom(src->pixels,
00228                            src->w, src->h, src->format->BitsPerPixel,
00229                            src->pitch, src->format->Rmask, src->format->Gmask,
00230                            src->format->Bmask, src->format->Amask);
00231 
00232     if (locked) {
00233         SDL_UnlockSurface(src);
00234     }
00235 
00236     return res;
00237 }
00238 
00239 bool SdlWindow::createOrUpdateWindow(int width, int height, uint32 flags) {
00240     if (_inputGrabState) {
00241         flags |= SDL_WINDOW_INPUT_GRABBED;
00242     }
00243 
00244     // SDL_WINDOW_RESIZABLE can also be updated without recreating the window
00245     // starting with SDL 2.0.5, but it is not treated as updateable here
00246     // because:
00247     // 1. It is currently only changed in conjunction with the SDL_WINDOW_OPENGL
00248     //    flag, so the window will always be recreated anyway when changing
00249     //    resizability; and
00250     // 2. Users (particularly on Windows) will sometimes swap older SDL DLLs
00251     //    to avoid bugs, which would be impossible if the feature was enabled
00252     //    at compile time using SDL_VERSION_ATLEAST.
00253     const uint32 updateableFlagsMask = fullscreenMask | SDL_WINDOW_INPUT_GRABBED;
00254 
00255     const uint32 oldNonUpdateableFlags = _lastFlags & ~updateableFlagsMask;
00256     const uint32 newNonUpdateableFlags = flags & ~updateableFlagsMask;
00257 
00258     const uint32 fullscreenFlags = flags & fullscreenMask;
00259 
00260     // This is terrible, but there is no way in SDL to get information on the
00261     // maximum bounds of a window with decoration, and SDL is too dumb to make
00262     // sure the window's surface doesn't grow beyond the display bounds, which
00263     // can easily happen with 3x scalers. There is a function in SDL to get the
00264     // window decoration size, but it only exists starting in SDL 2.0.5, which
00265     // is a buggy release on some platforms so we can't safely use 2.0.5+
00266     // features since some users replace the SDL dynamic library with 2.0.4, and
00267     // the documentation says it only works on X11 anyway, which means it is
00268     // basically worthless. So we'll just try to keep things closeish to the
00269     // maximum for now.
00270     SDL_DisplayMode displayMode;
00271     SDL_GetDesktopDisplayMode(0, &displayMode);
00272     if (!fullscreenFlags) {
00273         displayMode.w -= 20;
00274         displayMode.h -= 30;
00275     }
00276 
00277     if (width > displayMode.w) {
00278         width = displayMode.w;
00279     }
00280 
00281     if (height > displayMode.h) {
00282         height = displayMode.h;
00283     }
00284 
00285     if (!_window || oldNonUpdateableFlags != newNonUpdateableFlags) {
00286         destroyWindow();
00287         _window = SDL_CreateWindow(_windowCaption.c_str(), _lastX,
00288                                    _lastY, width, height, flags);
00289         if (_window) {
00290             setupIcon();
00291         }
00292     } else {
00293         if (fullscreenFlags) {
00294             SDL_DisplayMode fullscreenMode;
00295             fullscreenMode.w = width;
00296             fullscreenMode.h = height;
00297             fullscreenMode.driverdata = nullptr;
00298             fullscreenMode.format = 0;
00299             fullscreenMode.refresh_rate = 0;
00300             SDL_SetWindowDisplayMode(_window, &fullscreenMode);
00301         } else {
00302             SDL_SetWindowSize(_window, width, height);
00303         }
00304 
00305         SDL_SetWindowFullscreen(_window, fullscreenFlags);
00306     }
00307 
00308     const bool shouldGrab = (flags & SDL_WINDOW_INPUT_GRABBED) || fullscreenFlags;
00309     SDL_SetWindowGrab(_window, shouldGrab ? SDL_TRUE : SDL_FALSE);
00310 
00311     if (!_window) {
00312         return false;
00313     }
00314 
00315 #if defined(MACOSX)
00316     // macOS windows with the flag SDL_WINDOW_FULLSCREEN_DESKTOP exiting their fullscreen space
00317     // ignore the size set by SDL_SetWindowSize while they were in fullscreen mode.
00318     // Instead, they revert back to their previous windowed mode size.
00319     // This is a bug in SDL2: https://bugzilla.libsdl.org/show_bug.cgi?id=3719.
00320     // TODO: Remove the call to SDL_SetWindowSize below once the SDL bug is fixed.
00321 
00322     // In some cases at this point there may be a pending SDL resize event with the old size.
00323     // This happens for example if we destroyed the window, or when switching between windowed
00324     // and fullscreen modes. If we changed the window size here, this pending event will have the
00325     // old (and incorrect) size. To avoid any issue we call SDL_SetWindowSize() to generate another
00326     // resize event (SDL_WINDOWEVENT_SIZE_CHANGED) so that the last resize event we receive has
00327     // the correct size. This fixes for exmample bug #9971: SDL2: Fullscreen to RTL launcher resolution
00328     SDL_SetWindowSize(_window, width, height);
00329 #endif
00330 
00331     _lastFlags = flags;
00332 
00333     return true;
00334 }
00335 
00336 void SdlWindow::destroyWindow() {
00337     if (_window) {
00338         if (!(_lastFlags & fullscreenMask)) {
00339             SDL_GetWindowPosition(_window, &_lastX, &_lastY);
00340         }
00341         SDL_DestroyWindow(_window);
00342         _window = nullptr;
00343     }
00344 }
00345 #endif


Generated on Sat Aug 8 2020 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge