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 
00043 SdlWindow::~SdlWindow() {
00044 #if SDL_VERSION_ATLEAST(2, 0, 0)
00045     destroyWindow();
00046 #endif
00047 }
00048 
00049 void SdlWindow::setupIcon() {
00050     int x, y, w, h, ncols, nbytes, i;
00051     unsigned int rgba[256];
00052     unsigned int *icon;
00053 
00054     if (sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes) != 4) {
00055         warning("Wrong format of scummvm_icon[0] (%s)", scummvm_icon[0]);
00056 
00057         return;
00058     }
00059     if ((w > 512) || (h > 512) || (ncols > 255) || (nbytes > 1)) {
00060         warning("Could not load the built-in icon (%d %d %d %d)", w, h, ncols, nbytes);
00061         return;
00062     }
00063     icon = (unsigned int*)malloc(w*h*sizeof(unsigned int));
00064     if (!icon) {
00065         warning("Could not allocate temp storage for the built-in icon");
00066         return;
00067     }
00068 
00069     for (i = 0; i < ncols; i++) {
00070         unsigned char code;
00071         char color[32];
00072         memset(color, 0, sizeof(color));
00073         unsigned int col;
00074         if (sscanf(scummvm_icon[1 + i], "%c c %s", &code, color) != 2) {
00075             warning("Wrong format of scummvm_icon[%d] (%s)", 1 + i, scummvm_icon[1 + i]);
00076         }
00077         if (!strcmp(color, "None"))
00078             col = 0x00000000;
00079         else if (!strcmp(color, "black"))
00080             col = 0xFF000000;
00081         else if (color[0] == '#') {
00082             if (sscanf(color + 1, "%06x", &col) != 1) {
00083                 warning("Wrong format of color (%s)", color + 1);
00084             }
00085             col |= 0xFF000000;
00086         } else {
00087             warning("Could not load the built-in icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]);
00088             free(icon);
00089             return;
00090         }
00091 
00092         rgba[code] = col;
00093     }
00094     for (y = 0; y < h; y++) {
00095         const char *line = scummvm_icon[1 + ncols + y];
00096         for (x = 0; x < w; x++) {
00097             icon[x + w * y] = rgba[(int)line[x]];
00098         }
00099     }
00100 
00101     SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, w, h, 32, w * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
00102     if (!sdl_surf) {
00103         warning("SDL_CreateRGBSurfaceFrom(icon) failed");
00104     }
00105 
00106 #if SDL_VERSION_ATLEAST(2, 0, 0)
00107     if (_window) {
00108         SDL_SetWindowIcon(_window, sdl_surf);
00109     }
00110 #else
00111     SDL_WM_SetIcon(sdl_surf, NULL);
00112 #endif
00113 
00114     SDL_FreeSurface(sdl_surf);
00115     free(icon);
00116 }
00117 
00118 void SdlWindow::setWindowCaption(const Common::String &caption) {
00119 #if SDL_VERSION_ATLEAST(2, 0, 0)
00120     _windowCaption = caption;
00121     if (_window) {
00122         SDL_SetWindowTitle(_window, caption.c_str());
00123     }
00124 #else
00125     SDL_WM_SetCaption(caption.c_str(), caption.c_str());
00126 #endif
00127 }
00128 
00129 void SdlWindow::toggleMouseGrab() {
00130 #if SDL_VERSION_ATLEAST(2, 0, 0)
00131     if (_window) {
00132         _inputGrabState = SDL_GetWindowGrab(_window) == SDL_FALSE;
00133         SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE);
00134     }
00135 #else
00136     if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
00137         SDL_WM_GrabInput(SDL_GRAB_ON);
00138         _inputGrabState = true;
00139     } else {
00140         SDL_WM_GrabInput(SDL_GRAB_OFF);
00141         _inputGrabState = false;
00142     }
00143 #endif
00144 }
00145 
00146 bool SdlWindow::hasMouseFocus() const {
00147 #if SDL_VERSION_ATLEAST(2, 0, 0)
00148     if (_window) {
00149         return (SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_FOCUS);
00150     } else {
00151         return false;
00152     }
00153 #else
00154     return (SDL_GetAppState() & SDL_APPMOUSEFOCUS);
00155 #endif
00156 }
00157 
00158 bool SdlWindow::warpMouseInWindow(int x, int y) {
00159     if (hasMouseFocus()) {
00160 #if SDL_VERSION_ATLEAST(2, 0, 0)
00161         if (_window) {
00162             SDL_WarpMouseInWindow(_window, x, y);
00163             return true;
00164         }
00165 #else
00166         SDL_WarpMouse(x, y);
00167         return true;
00168 #endif
00169     }
00170 
00171     return false;
00172 }
00173 
00174 void SdlWindow::iconifyWindow() {
00175 #if SDL_VERSION_ATLEAST(2, 0, 0)
00176     if (_window) {
00177         SDL_MinimizeWindow(_window);
00178     }
00179 #else
00180     SDL_WM_IconifyWindow();
00181 #endif
00182 }
00183 
00184 bool SdlWindow::getSDLWMInformation(SDL_SysWMinfo *info) const {
00185     SDL_VERSION(&info->version);
00186 #if SDL_VERSION_ATLEAST(2, 0, 0)
00187     return SDL_GetWindowWMInfo(_window, info);
00188 #else
00189     return SDL_GetWMInfo(info);
00190 #endif
00191 }
00192 
00193 #if SDL_VERSION_ATLEAST(2, 0, 0)
00194 SDL_Surface *copySDLSurface(SDL_Surface *src) {
00195     const bool locked = SDL_MUSTLOCK(src) == SDL_TRUE;
00196 
00197     if (locked) {
00198         if (SDL_LockSurface(src) != 0) {
00199             return nullptr;
00200         }
00201     }
00202 
00203     SDL_Surface *res = SDL_CreateRGBSurfaceFrom(src->pixels,
00204                            src->w, src->h, src->format->BitsPerPixel,
00205                            src->pitch, src->format->Rmask, src->format->Gmask,
00206                            src->format->Bmask, src->format->Amask);
00207 
00208     if (locked) {
00209         SDL_UnlockSurface(src);
00210     }
00211 
00212     return res;
00213 }
00214 
00215 bool SdlWindow::createOrUpdateWindow(int width, int height, uint32 flags) {
00216     if (_inputGrabState) {
00217         flags |= SDL_WINDOW_INPUT_GRABBED;
00218     }
00219 
00220     // SDL_WINDOW_RESIZABLE can also be updated without recreating the window
00221     // starting with SDL 2.0.5, but it is not treated as updateable here
00222     // because:
00223     // 1. It is currently only changed in conjunction with the SDL_WINDOW_OPENGL
00224     //    flag, so the window will always be recreated anyway when changing
00225     //    resizability; and
00226     // 2. Users (particularly on Windows) will sometimes swap older SDL DLLs
00227     //    to avoid bugs, which would be impossible if the feature was enabled
00228     //    at compile time using SDL_VERSION_ATLEAST.
00229     const uint32 updateableFlagsMask = fullscreenMask | SDL_WINDOW_INPUT_GRABBED;
00230 
00231     const uint32 oldNonUpdateableFlags = _lastFlags & ~updateableFlagsMask;
00232     const uint32 newNonUpdateableFlags = flags & ~updateableFlagsMask;
00233 
00234     const uint32 fullscreenFlags = flags & fullscreenMask;
00235 
00236     // This is terrible, but there is no way in SDL to get information on the
00237     // maximum bounds of a window with decoration, and SDL is too dumb to make
00238     // sure the window's surface doesn't grow beyond the display bounds, which
00239     // can easily happen with 3x scalers. There is a function in SDL to get the
00240     // window decoration size, but it only exists starting in SDL 2.0.5, which
00241     // is a buggy release on some platforms so we can't safely use 2.0.5+
00242     // features since some users replace the SDL dynamic library with 2.0.4, and
00243     // the documentation says it only works on X11 anyway, which means it is
00244     // basically worthless. So we'll just try to keep things closeish to the
00245     // maximum for now.
00246     SDL_DisplayMode displayMode;
00247     SDL_GetDesktopDisplayMode(0, &displayMode);
00248     if (!fullscreenFlags) {
00249         displayMode.w -= 20;
00250         displayMode.h -= 30;
00251     }
00252 
00253     if (width > displayMode.w) {
00254         width = displayMode.w;
00255     }
00256 
00257     if (height > displayMode.h) {
00258         height = displayMode.h;
00259     }
00260 
00261     if (!_window || oldNonUpdateableFlags != newNonUpdateableFlags) {
00262         destroyWindow();
00263         _window = SDL_CreateWindow(_windowCaption.c_str(), _lastX,
00264                                    _lastY, width, height, flags);
00265         if (_window) {
00266             setupIcon();
00267         }
00268     } else {
00269         if (fullscreenFlags) {
00270             SDL_DisplayMode fullscreenMode;
00271             fullscreenMode.w = width;
00272             fullscreenMode.h = height;
00273             fullscreenMode.driverdata = nullptr;
00274             fullscreenMode.format = 0;
00275             fullscreenMode.refresh_rate = 0;
00276             SDL_SetWindowDisplayMode(_window, &fullscreenMode);
00277         } else {
00278             SDL_SetWindowSize(_window, width, height);
00279         }
00280 
00281         SDL_SetWindowFullscreen(_window, fullscreenFlags);
00282     }
00283 
00284     const bool shouldGrab = (flags & SDL_WINDOW_INPUT_GRABBED) || fullscreenFlags;
00285     SDL_SetWindowGrab(_window, shouldGrab ? SDL_TRUE : SDL_FALSE);
00286 
00287     if (!_window) {
00288         return false;
00289     }
00290 
00291 #if defined(MACOSX)
00292     // macOS windows with the flag SDL_WINDOW_FULLSCREEN_DESKTOP exiting their fullscreen space
00293     // ignore the size set by SDL_SetWindowSize while they were in fullscreen mode.
00294     // Instead, they revert back to their previous windowed mode size.
00295     // This is a bug in SDL2: https://bugzilla.libsdl.org/show_bug.cgi?id=3719.
00296     // TODO: Remove the call to SDL_SetWindowSize below once the SDL bug is fixed.
00297 
00298     // In some cases at this point there may be a pending SDL resize event with the old size.
00299     // This happens for example if we destroyed the window, or when switching between windowed
00300     // and fullscreen modes. If we changed the window size here, this pending event will have the
00301     // old (and incorrect) size. To avoid any issue we call SDL_SetWindowSize() to generate another
00302     // resize event (SDL_WINDOWEVENT_SIZE_CHANGED) so that the last resize event we receive has
00303     // the correct size. This fixes for exmample bug #9971: SDL2: Fullscreen to RTL launcher resolution
00304     SDL_SetWindowSize(_window, width, height);
00305 #endif
00306 
00307     _lastFlags = flags;
00308 
00309     return true;
00310 }
00311 
00312 void SdlWindow::destroyWindow() {
00313     if (_window) {
00314         if (!(_lastFlags & fullscreenMask)) {
00315             SDL_GetWindowPosition(_window, &_lastX, &_lastY);
00316         }
00317         SDL_DestroyWindow(_window);
00318         _window = nullptr;
00319     }
00320 }
00321 #endif


Generated on Sat May 18 2019 05:01:20 for ResidualVM by doxygen 1.7.1
curved edge   curved edge