The general code handles restoring video mode/gamma/etc. when the window loses focus. SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sun, 09 Jul 2006 18:09:16 +0000
branchSDL-1.3
changeset 1734f7c667ded87d
parent 1733 0b1070f2f94d
child 1735 8dd28c4ef746
The general code handles restoring video mode/gamma/etc. when the window loses focus.
Support for changing fullscreen/windowed mode in progress.
include/SDL_video.h
src/SDL_compat.c
src/events/SDL_windowevents.c
src/video/SDL_gamma.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/win32/SDL_win32events.c
src/video/win32/SDL_win32window.c
     1.1 --- a/include/SDL_video.h	Sun Jul 09 09:02:26 2006 +0000
     1.2 +++ b/include/SDL_video.h	Sun Jul 09 18:09:16 2006 +0000
     1.3 @@ -125,7 +125,7 @@
     1.4      SDL_WINDOW_MINIMIZED = 0x00000020,          /**< minimized */
     1.5      SDL_WINDOW_MAXIMIZED = 0x00000040,          /**< maximized */
     1.6      SDL_WINDOW_INPUT_GRABBED = 0x00000100,      /**< window has grabbed input focus */
     1.7 -    SDL_WINDOW_KEYBOARD_FOCUS = 0x00000200,     /**< window has keyboard focus */
     1.8 +    SDL_WINDOW_INPUT_FOCUS = 0x00000200,        /**< window has input focus */
     1.9      SDL_WINDOW_MOUSE_FOCUS = 0x00000400,        /**< window has mouse focus */
    1.10  } SDL_WindowFlags;
    1.11  
    1.12 @@ -734,7 +734,7 @@
    1.13  /**
    1.14   * \fn void SDL_RaiseWindow(SDL_WindowID windowID)
    1.15   *
    1.16 - * \brief Raise the window so it's above other windows.
    1.17 + * \brief Raise the window above other windows and set the input focus.
    1.18   */
    1.19  extern DECLSPEC void SDLCALL SDL_RaiseWindow(SDL_WindowID windowID);
    1.20  
     2.1 --- a/src/SDL_compat.c	Sun Jul 09 09:02:26 2006 +0000
     2.2 +++ b/src/SDL_compat.c	Sun Jul 09 18:09:16 2006 +0000
     2.3 @@ -699,7 +699,18 @@
     2.4  int
     2.5  SDL_WM_ToggleFullScreen(SDL_Surface * surface)
     2.6  {
     2.7 -    return 0;
     2.8 +    if (SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN) {
     2.9 +        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 0) < 0) {
    2.10 +            return 0;
    2.11 +        }
    2.12 +        SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
    2.13 +    } else {
    2.14 +        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 1) < 0) {
    2.15 +            return 0;
    2.16 +        }
    2.17 +        SDL_PublicSurface->flags |= SDL_FULLSCREEN;
    2.18 +    }
    2.19 +    return 1;
    2.20  }
    2.21  
    2.22  SDL_GrabMode
    2.23 @@ -727,7 +738,7 @@
    2.24      if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
    2.25          state |= SDL_APPACTIVE;
    2.26      }
    2.27 -    if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
    2.28 +    if (flags & SDL_WINDOW_INPUT_FOCUS) {
    2.29          state |= SDL_APPINPUTFOCUS;
    2.30      }
    2.31      if (flags & SDL_WINDOW_MOUSE_FOCUS) {
     3.1 --- a/src/events/SDL_windowevents.c	Sun Jul 09 09:02:26 2006 +0000
     3.2 +++ b/src/events/SDL_windowevents.c	Sun Jul 09 18:09:16 2006 +0000
     3.3 @@ -100,17 +100,17 @@
     3.4          window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
     3.5          break;
     3.6      case SDL_WINDOWEVENT_FOCUS_GAINED:
     3.7 -        if (window->flags & SDL_WINDOW_KEYBOARD_FOCUS) {
     3.8 +        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
     3.9              return 0;
    3.10          }
    3.11 -        window->flags |= SDL_WINDOW_KEYBOARD_FOCUS;
    3.12 +        window->flags |= SDL_WINDOW_INPUT_FOCUS;
    3.13          SDL_OnWindowFocusGained(window);
    3.14          break;
    3.15      case SDL_WINDOWEVENT_FOCUS_LOST:
    3.16 -        if (!(window->flags & SDL_WINDOW_KEYBOARD_FOCUS)) {
    3.17 +        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
    3.18              return 0;
    3.19          }
    3.20 -        window->flags &= ~SDL_WINDOW_KEYBOARD_FOCUS;
    3.21 +        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
    3.22          SDL_OnWindowFocusLost(window);
    3.23          break;
    3.24      }
     4.1 --- a/src/video/SDL_gamma.c	Sun Jul 09 09:02:26 2006 +0000
     4.2 +++ b/src/video/SDL_gamma.c	Sun Jul 09 18:09:16 2006 +0000
     4.3 @@ -152,8 +152,12 @@
     4.4      /* Try to set the gamma ramp in the driver */
     4.5      succeeded = -1;
     4.6      if (_this && _this->SetDisplayGammaRamp) {
     4.7 -        succeeded =
     4.8 -            _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
     4.9 +        if (SDL_GetFocusWindow()) {
    4.10 +            succeeded =
    4.11 +                _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
    4.12 +        } else {
    4.13 +            succeeded = 0;
    4.14 +        }
    4.15      } else {
    4.16          SDL_SetError("Gamma ramp manipulation not supported");
    4.17      }
     5.1 --- a/src/video/SDL_sysvideo.h	Sun Jul 09 09:02:26 2006 +0000
     5.2 +++ b/src/video/SDL_sysvideo.h	Sun Jul 09 18:09:16 2006 +0000
     5.3 @@ -124,9 +124,9 @@
     5.4      void *driverdata;
     5.5  };
     5.6  #define FULLSCREEN_VISIBLE(W) \
     5.7 -    ((W->flags & SDL_WINDOW_FULLSCREEN) && \
     5.8 -     (W->flags & SDL_WINDOW_SHOWN) && \
     5.9 -     !(W->flags & SDL_WINDOW_MINIMIZED))
    5.10 +    (((W)->flags & SDL_WINDOW_FULLSCREEN) && \
    5.11 +     ((W)->flags & SDL_WINDOW_SHOWN) && \
    5.12 +     !((W)->flags & SDL_WINDOW_MINIMIZED))
    5.13  
    5.14  /* Define the SDL display structure
    5.15     This corresponds to physical monitors attached to the system.
    5.16 @@ -425,6 +425,7 @@
    5.17  extern void SDL_OnWindowHidden(SDL_Window * window);
    5.18  extern void SDL_OnWindowFocusGained(SDL_Window * window);
    5.19  extern void SDL_OnWindowFocusLost(SDL_Window * window);
    5.20 +extern SDL_WindowID SDL_GetFocusWindow(void);
    5.21  
    5.22  #endif /* _SDL_sysvideo_h */
    5.23  
     6.1 --- a/src/video/SDL_video.c	Sun Jul 09 09:02:26 2006 +0000
     6.2 +++ b/src/video/SDL_video.c	Sun Jul 09 18:09:16 2006 +0000
     6.3 @@ -712,6 +712,7 @@
     6.4                                    SDL_WINDOW_MAXIMIZED |
     6.5                                    SDL_WINDOW_MINIMIZED |
     6.6                                    SDL_WINDOW_INPUT_GRABBED);
     6.7 +    SDL_VideoDisplay *display;
     6.8      SDL_Window window;
     6.9      int num_windows;
    6.10      SDL_Window *windows;
    6.11 @@ -724,14 +725,8 @@
    6.12      SDL_zero(window);
    6.13      window.id = _this->next_object_id++;
    6.14      window.title = title ? SDL_strdup(title) : NULL;
    6.15 -    if (flags & SDL_WINDOW_FULLSCREEN) {
    6.16 -        const SDL_DisplayMode *mode = &SDL_CurrentDisplay.current_mode;
    6.17 -        window.x = (mode->w - w) / 2;
    6.18 -        window.y = (mode->h - h) / 2;
    6.19 -    } else {
    6.20 -        window.x = x;
    6.21 -        window.y = y;
    6.22 -    }
    6.23 +    window.x = x;
    6.24 +    window.y = y;
    6.25      window.w = w;
    6.26      window.h = h;
    6.27      window.flags = (flags & allowed_flags);
    6.28 @@ -744,10 +739,10 @@
    6.29          return 0;
    6.30      }
    6.31  
    6.32 -    num_windows = SDL_CurrentDisplay.num_windows;
    6.33 +    display = &SDL_CurrentDisplay;
    6.34 +    num_windows = display->num_windows;
    6.35      windows =
    6.36 -        SDL_realloc(SDL_CurrentDisplay.windows,
    6.37 -                    (num_windows + 1) * sizeof(*windows));
    6.38 +        SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
    6.39      if (!windows) {
    6.40          if (_this->DestroyWindow) {
    6.41              _this->DestroyWindow(_this, &window);
    6.42 @@ -758,8 +753,20 @@
    6.43          return 0;
    6.44      }
    6.45      windows[num_windows] = window;
    6.46 -    SDL_CurrentDisplay.windows = windows;
    6.47 -    SDL_CurrentDisplay.num_windows++;
    6.48 +    display->windows = windows;
    6.49 +    display->num_windows++;
    6.50 +
    6.51 +    if (FULLSCREEN_VISIBLE(&window)) {
    6.52 +        /* Hide any other fullscreen windows */
    6.53 +        int i;
    6.54 +        for (i = 0; i < display->num_windows; ++i) {
    6.55 +            SDL_Window *other = &display->windows[i];
    6.56 +            if (other->id != window.id && FULLSCREEN_VISIBLE(other)) {
    6.57 +                SDL_MinimizeWindow(other->id);
    6.58 +            }
    6.59 +        }
    6.60 +        SDL_SetDisplayMode(display->fullscreen_mode);
    6.61 +    }
    6.62  
    6.63      return window.id;
    6.64  }
    6.65 @@ -767,6 +774,7 @@
    6.66  SDL_WindowID
    6.67  SDL_CreateWindowFrom(const void *data)
    6.68  {
    6.69 +    SDL_VideoDisplay *display;
    6.70      SDL_Window window;
    6.71      int num_windows;
    6.72      SDL_Window *windows;
    6.73 @@ -785,10 +793,10 @@
    6.74          return 0;
    6.75      }
    6.76  
    6.77 -    num_windows = SDL_CurrentDisplay.num_windows;
    6.78 +    display = &SDL_CurrentDisplay;
    6.79 +    num_windows = display->num_windows;
    6.80      windows =
    6.81 -        SDL_realloc(SDL_CurrentDisplay.windows,
    6.82 -                    (num_windows + 1) * sizeof(*windows));
    6.83 +        SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
    6.84      if (!windows) {
    6.85          if (_this->DestroyWindow) {
    6.86              _this->DestroyWindow(_this, &window);
    6.87 @@ -799,8 +807,8 @@
    6.88          return 0;
    6.89      }
    6.90      windows[num_windows] = window;
    6.91 -    SDL_CurrentDisplay.windows = windows;
    6.92 -    SDL_CurrentDisplay.num_windows++;
    6.93 +    display->windows = windows;
    6.94 +    display->num_windows++;
    6.95  
    6.96      return window.id;
    6.97  }
    6.98 @@ -1062,6 +1070,48 @@
    6.99      }
   6.100  }
   6.101  
   6.102 +int
   6.103 +SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
   6.104 +{
   6.105 +    SDL_Window *window = SDL_GetWindowFromID(windowID);
   6.106 +
   6.107 +    if (!window) {
   6.108 +        return -1;
   6.109 +    }
   6.110 +
   6.111 +    if (fullscreen) {
   6.112 +        fullscreen = SDL_WINDOW_FULLSCREEN;
   6.113 +    }
   6.114 +    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
   6.115 +        return 0;
   6.116 +    }
   6.117 +
   6.118 +    if (fullscreen) {
   6.119 +        window->flags |= SDL_WINDOW_FULLSCREEN;
   6.120 +
   6.121 +        if (FULLSCREEN_VISIBLE(window)) {
   6.122 +            SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   6.123 +
   6.124 +            /* Hide any other fullscreen windows */
   6.125 +            int i;
   6.126 +            for (i = 0; i < display->num_windows; ++i) {
   6.127 +                SDL_Window *other = &display->windows[i];
   6.128 +                if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
   6.129 +                    SDL_MinimizeWindow(other->id);
   6.130 +                }
   6.131 +            }
   6.132 +
   6.133 +            SDL_SetDisplayMode(display->fullscreen_mode);
   6.134 +        }
   6.135 +    } else {
   6.136 +        window->flags &= ~SDL_WINDOW_FULLSCREEN;
   6.137 +
   6.138 +        if (FULLSCREEN_VISIBLE(window)) {
   6.139 +            SDL_SetDisplayMode(NULL);
   6.140 +        }
   6.141 +    }
   6.142 +}
   6.143 +
   6.144  void
   6.145  SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
   6.146  {
   6.147 @@ -1077,7 +1127,7 @@
   6.148          window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   6.149      }
   6.150  
   6.151 -    if (_this->SetWindowGrab) {
   6.152 +    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
   6.153          _this->SetWindowGrab(_this, window);
   6.154      }
   6.155  }
   6.156 @@ -1115,6 +1165,9 @@
   6.157      if (display->gamma && _this->SetDisplayGammaRamp) {
   6.158          _this->SetDisplayGammaRamp(_this, display->gamma);
   6.159      }
   6.160 +    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
   6.161 +        _this->SetWindowGrab(_this, window);
   6.162 +    }
   6.163  }
   6.164  
   6.165  void
   6.166 @@ -1123,12 +1176,36 @@
   6.167      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   6.168  
   6.169      if (window->flags & SDL_WINDOW_FULLSCREEN) {
   6.170 +        SDL_MinimizeWindow(window->id);
   6.171          SDL_SetDisplayMode(NULL);
   6.172 -        SDL_MinimizeWindow(window->id);
   6.173      }
   6.174      if (display->gamma && _this->SetDisplayGammaRamp) {
   6.175          _this->SetDisplayGammaRamp(_this, display->saved_gamma);
   6.176      }
   6.177 +    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
   6.178 +        _this->SetWindowGrab(_this, window);
   6.179 +    }
   6.180 +}
   6.181 +
   6.182 +SDL_WindowID
   6.183 +SDL_GetFocusWindow(void)
   6.184 +{
   6.185 +    SDL_VideoDisplay *display;
   6.186 +    int i;
   6.187 +
   6.188 +    if (!_this) {
   6.189 +        return 0;
   6.190 +    }
   6.191 +
   6.192 +    display = &SDL_CurrentDisplay;
   6.193 +    for (i = 0; i < display->num_windows; ++i) {
   6.194 +        SDL_Window *window = &display->windows[i];
   6.195 +
   6.196 +        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
   6.197 +            return window->id;
   6.198 +        }
   6.199 +    }
   6.200 +    return 0;
   6.201  }
   6.202  
   6.203  void
   6.204 @@ -1140,6 +1217,9 @@
   6.205          return;
   6.206      }
   6.207  
   6.208 +    /* Restore video mode, etc. */
   6.209 +    SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   6.210 +
   6.211      for (i = 0; i < _this->num_displays; ++i) {
   6.212          SDL_VideoDisplay *display = &_this->displays[i];
   6.213          for (j = 0; j < display->num_windows; ++j) {
   6.214 @@ -1147,16 +1227,6 @@
   6.215              if (window->id != windowID) {
   6.216                  continue;
   6.217              }
   6.218 -            if (window->flags & SDL_WINDOW_FULLSCREEN) {
   6.219 -                SDL_SetDisplayMode(NULL);
   6.220 -            }
   6.221 -            if (display->gamma && _this->SetDisplayGammaRamp) {
   6.222 -                _this->SetDisplayGammaRamp(_this, display->saved_gamma);
   6.223 -            }
   6.224 -            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   6.225 -                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   6.226 -                _this->SetWindowGrab(_this, window);
   6.227 -            }
   6.228              if (window->renderer) {
   6.229                  SDL_DestroyRenderer(window->id);
   6.230              }
     7.1 --- a/src/video/win32/SDL_win32events.c	Sun Jul 09 09:02:26 2006 +0000
     7.2 +++ b/src/video/win32/SDL_win32events.c	Sun Jul 09 18:09:16 2006 +0000
     7.3 @@ -453,26 +453,11 @@
     7.4                  }
     7.5                  if (keyboard && keyboard->focus != data->windowID) {
     7.6                      SDL_SetKeyboardFocus(index, data->windowID);
     7.7 -
     7.8 -                    if (SDL_GetWindowFlags(data->windowID) &
     7.9 -                        SDL_WINDOW_INPUT_GRABBED) {
    7.10 -                        RECT rect;
    7.11 -
    7.12 -                        GetClientRect(hwnd, &rect);
    7.13 -                        ClientToScreen(hwnd, (LPPOINT) & rect);
    7.14 -                        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
    7.15 -                        ClipCursor(&rect);
    7.16 -                    }
    7.17                  }
    7.18                  /* FIXME: Update keyboard state */
    7.19              } else {
    7.20                  if (keyboard && keyboard->focus == data->windowID) {
    7.21                      SDL_SetKeyboardFocus(index, 0);
    7.22 -
    7.23 -                    if (SDL_GetWindowFlags(data->windowID) &
    7.24 -                        SDL_WINDOW_INPUT_GRABBED) {
    7.25 -                        ClipCursor(NULL);
    7.26 -                    }
    7.27                  }
    7.28                  if (minimized) {
    7.29                      SDL_SendWindowEvent(data->windowID,
    7.30 @@ -782,7 +767,7 @@
    7.31  
    7.32              window_flags = SDL_GetWindowFlags(data->windowID);
    7.33              if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
    7.34 -                (window_flags & SDL_WINDOW_KEYBOARD_FOCUS)) {
    7.35 +                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
    7.36                  ClipCursor(&rect);
    7.37              }
    7.38  
     8.1 --- a/src/video/win32/SDL_win32window.c	Sun Jul 09 09:02:26 2006 +0000
     8.2 +++ b/src/video/win32/SDL_win32window.c	Sun Jul 09 18:09:16 2006 +0000
     8.3 @@ -109,7 +109,7 @@
     8.4      }
     8.5      if (GetFocus() == hwnd) {
     8.6          int index = data->videodata->keyboard;
     8.7 -        window->flags |= SDL_WINDOW_KEYBOARD_FOCUS;
     8.8 +        window->flags |= SDL_WINDOW_INPUT_FOCUS;
     8.9          SDL_SetKeyboardFocus(index, data->windowID);
    8.10  
    8.11          if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
    8.12 @@ -390,20 +390,15 @@
    8.13  {
    8.14      HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    8.15  
    8.16 -    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
    8.17 -        if (window->flags & SDL_WINDOW_KEYBOARD_FOCUS) {
    8.18 -            RECT rect;
    8.19 -            GetClientRect(hwnd, &rect);
    8.20 -            ClientToScreen(hwnd, (LPPOINT) & rect);
    8.21 -            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
    8.22 -            ClipCursor(&rect);
    8.23 -        } else {
    8.24 -            SetFocus(hwnd);
    8.25 -        }
    8.26 +    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
    8.27 +        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
    8.28 +        RECT rect;
    8.29 +        GetClientRect(hwnd, &rect);
    8.30 +        ClientToScreen(hwnd, (LPPOINT) & rect);
    8.31 +        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
    8.32 +        ClipCursor(&rect);
    8.33      } else {
    8.34 -        if (window->flags & SDL_WINDOW_KEYBOARD_FOCUS) {
    8.35 -            ClipCursor(NULL);
    8.36 -        }
    8.37 +        ClipCursor(NULL);
    8.38      }
    8.39  }
    8.40