src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 05:08:33 +0000
branchSDL-1.3
changeset 1669 9857d21967bb
parent 1668 4da1ee79c9af
child 1670 eef792d31de8
permissions -rw-r--r--
The test programs compile again.
The dummy video driver is partially functional now.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* This file contains functions for backwards compatibility with SDL 1.2 */
    25 
    26 #include "SDL.h"
    27 
    28 #include "video/SDL_sysvideo.h"
    29 
    30 
    31 static SDL_WindowID window;
    32 static char *wm_title;
    33 
    34 char *
    35 SDL_AudioDriverName(char *namebuf, int maxlen)
    36 {
    37     const char *name = SDL_GetCurrentAudioDriver();
    38     if (name) {
    39         SDL_strlcpy(namebuf, name, maxlen);
    40         return namebuf;
    41     }
    42     return NULL;
    43 }
    44 
    45 char *
    46 SDL_VideoDriverName(char *namebuf, int maxlen)
    47 {
    48     const char *name = SDL_GetCurrentVideoDriver();
    49     if (name) {
    50         SDL_strlcpy(namebuf, name, maxlen);
    51         return namebuf;
    52     }
    53     return NULL;
    54 }
    55 
    56 int
    57 SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
    58 {
    59     int i, actual_bpp = 0;
    60 
    61     if (!SDL_GetVideoDevice()) {
    62         return 0;
    63     }
    64 
    65     if (!(flags & SDL_FULLSCREEN)) {
    66         return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
    67     }
    68 
    69     for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
    70         const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
    71         if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
    72             if (!mode->format) {
    73                 return bpp;
    74             }
    75             if (SDL_BITSPERPIXEL(mode->format) >= bpp) {
    76                 actual_bpp = SDL_BITSPERPIXEL(mode->format);
    77             }
    78         }
    79     }
    80     return actual_bpp;
    81 }
    82 
    83 SDL_Rect **
    84 SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
    85 {
    86     int i, nmodes;
    87     SDL_Rect **modes;
    88 
    89     if (!SDL_GetVideoDevice()) {
    90         return NULL;
    91     }
    92 
    93     if (!(flags & SDL_FULLSCREEN)) {
    94         return (SDL_Rect **) (-1);
    95     }
    96 
    97     /* Memory leak, but this is a compatibility function, who cares? */
    98     nmodes = 0;
    99     for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
   100         const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
   101         if (!mode->w || !mode->h) {
   102             return (SDL_Rect **) (-1);
   103         }
   104         if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
   105             continue;
   106         }
   107         if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
   108             && modes[nmodes - 1]->h == mode->h) {
   109             continue;
   110         }
   111 
   112         modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
   113         if (!modes) {
   114             return NULL;
   115         }
   116         modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
   117         if (!modes[nmodes]) {
   118             return NULL;
   119         }
   120         modes[nmodes]->x = 0;
   121         modes[nmodes]->y = 0;
   122         modes[nmodes]->w = mode->w;
   123         modes[nmodes]->h = mode->h;
   124         ++nmodes;
   125     }
   126     if (modes) {
   127         modes[nmodes] = NULL;
   128     }
   129     return modes;
   130 }
   131 
   132 static int (*orig_eventfilter) (const SDL_Event * event);
   133 
   134 static int
   135 SDL_CompatEventFilter(const SDL_Event * event)
   136 {
   137     SDL_Event fake;
   138 
   139     switch (event->type) {
   140     case SDL_WINDOWEVENT:
   141         switch (event->window.event) {
   142         case SDL_WINDOWEVENT_RESIZED:
   143             fake.type = SDL_VIDEORESIZE;
   144             fake.resize.w = event->window.data1;
   145             fake.resize.h = event->window.data2;
   146             SDL_PushEvent(&fake);
   147             break;
   148         case SDL_WINDOWEVENT_MINIMIZED:
   149             fake.type = SDL_ACTIVEEVENT;
   150             fake.active.gain = 0;
   151             fake.active.state = SDL_APPACTIVE;
   152             SDL_PushEvent(&fake);
   153             break;
   154         case SDL_WINDOWEVENT_RESTORED:
   155             fake.type = SDL_ACTIVEEVENT;
   156             fake.active.gain = 1;
   157             fake.active.state = SDL_APPACTIVE;
   158             SDL_PushEvent(&fake);
   159             break;
   160         case SDL_WINDOWEVENT_ENTER:
   161             fake.type = SDL_ACTIVEEVENT;
   162             fake.active.gain = 1;
   163             fake.active.state = SDL_APPMOUSEFOCUS;
   164             SDL_PushEvent(&fake);
   165             break;
   166         case SDL_WINDOWEVENT_LEAVE:
   167             fake.type = SDL_ACTIVEEVENT;
   168             fake.active.gain = 0;
   169             fake.active.state = SDL_APPMOUSEFOCUS;
   170             SDL_PushEvent(&fake);
   171             break;
   172         case SDL_WINDOWEVENT_FOCUS_GAINED:
   173             fake.type = SDL_ACTIVEEVENT;
   174             fake.active.gain = 1;
   175             fake.active.state = SDL_APPINPUTFOCUS;
   176             SDL_PushEvent(&fake);
   177             break;
   178         case SDL_WINDOWEVENT_FOCUS_LOST:
   179             fake.type = SDL_ACTIVEEVENT;
   180             fake.active.gain = 1;
   181             fake.active.state = SDL_APPINPUTFOCUS;
   182             SDL_PushEvent(&fake);
   183             break;
   184         }
   185     }
   186     return orig_eventfilter(event);
   187 }
   188 
   189 SDL_Surface *
   190 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
   191 {
   192     int (*filter) (const SDL_Event * event);
   193     const SDL_DisplayMode *desktop_mode;
   194     SDL_DisplayMode mode;
   195     int i;
   196     Uint32 window_flags;
   197     Uint32 desktop_format;
   198     Uint32 desired_format;
   199 
   200     if (!SDL_GetVideoDevice()) {
   201         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   202             return NULL;
   203         }
   204     }
   205 
   206     /* Destroy existing window */
   207     SDL_DestroyWindow(window);
   208 
   209     /* Set up the event filter */
   210     filter = SDL_GetEventFilter();
   211     if (filter != SDL_CompatEventFilter) {
   212         orig_eventfilter = filter;
   213     }
   214     SDL_SetEventFilter(SDL_CompatEventFilter);
   215 
   216     /* Create a new window */
   217     window_flags = SDL_WINDOW_SHOWN;
   218     if (flags & SDL_FULLSCREEN) {
   219         window_flags |= SDL_WINDOW_FULLSCREEN;
   220     }
   221     if (flags & SDL_OPENGL) {
   222         window_flags |= SDL_WINDOW_OPENGL;
   223     }
   224     if (flags & SDL_RESIZABLE) {
   225         window_flags |= SDL_WINDOW_RESIZABLE;
   226     }
   227     if (flags & SDL_NOFRAME) {
   228         window_flags |= SDL_WINDOW_BORDERLESS;
   229     }
   230     window = SDL_CreateWindow(wm_title, 0, 0, width, height, window_flags);
   231     if (!window) {
   232         return NULL;
   233     }
   234 
   235     /* Set up the desired display mode */
   236     desktop_mode = SDL_GetDesktopDisplayMode();
   237     desktop_format = desktop_mode->format;
   238     if (desktop_format && ((flags & SDL_ANYFORMAT)
   239                            || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
   240         desired_format = desktop_format;
   241     } else {
   242         switch (bpp) {
   243         case 0:
   244             if (desktop_format) {
   245                 desired_format = desktop_format;
   246             } else {
   247                 desired_format = SDL_PixelFormat_RGB888;
   248             }
   249             break;
   250         case 8:
   251             desired_format = SDL_PixelFormat_Index8;
   252             break;
   253         case 15:
   254             desired_format = SDL_PixelFormat_RGB555;
   255             break;
   256         case 16:
   257             desired_format = SDL_PixelFormat_RGB565;
   258             break;
   259         case 24:
   260             desired_format = SDL_PixelFormat_RGB24;
   261             break;
   262         case 32:
   263             desired_format = SDL_PixelFormat_RGB888;
   264             break;
   265         default:
   266             SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
   267             return NULL;
   268         }
   269     }
   270     mode.format = desired_format;
   271     mode.w = width;
   272     mode.h = height;
   273     mode.refresh_rate = 0;
   274 
   275     /* Set the desired display mode */
   276     if (flags & SDL_FULLSCREEN) {
   277         if (!SDL_GetClosestDisplayMode(&mode, &mode)) {
   278             return NULL;
   279         }
   280     } else {
   281         if (desktop_format) {
   282             mode.format = desktop_format;
   283         }
   284         if (desktop_mode->w && desktop_mode->h) {
   285             mode.w = desktop_mode->w;
   286             mode.h = desktop_mode->h;
   287         }
   288         mode.refresh_rate = desktop_mode->refresh_rate;
   289     }
   290     if (SDL_SetDisplayMode(&mode) < 0) {
   291         return NULL;
   292     }
   293 
   294     /* Create the display surface */
   295     return SDL_CreateWindowSurface(window, desired_format, flags);
   296 }
   297 
   298 SDL_Surface *
   299 SDL_GetVideoSurface(void)
   300 {
   301     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   302 
   303     return SDL_VideoSurface;
   304 }
   305 
   306 void
   307 SDL_WM_SetCaption(const char *title, const char *icon)
   308 {
   309     if (wm_title) {
   310         SDL_free(wm_title);
   311     } else {
   312         wm_title = SDL_strdup(title);
   313     }
   314     SDL_SetWindowTitle(window, wm_title);
   315 }
   316 
   317 void
   318 SDL_WM_GetCaption(char **title, char **icon)
   319 {
   320     if (title) {
   321         *title = wm_title;
   322     }
   323     if (icon) {
   324         *icon = "";
   325     }
   326 }
   327 
   328 void
   329 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
   330 {
   331     /* FIXME */
   332 }
   333 
   334 int
   335 SDL_WM_IconifyWindow(void)
   336 {
   337     SDL_MinimizeWindow(window);
   338 }
   339 
   340 int
   341 SDL_WM_ToggleFullScreen(SDL_Surface * surface)
   342 {
   343     return 0;
   344 }
   345 
   346 SDL_GrabMode
   347 SDL_WM_GrabInput(SDL_GrabMode mode)
   348 {
   349     if (mode != SDL_GRAB_QUERY) {
   350         SDL_SetWindowGrab(window, mode);
   351     }
   352     return (SDL_GrabMode) SDL_GetWindowGrab(window);
   353 }
   354 
   355 Uint8
   356 SDL_GetAppState(void)
   357 {
   358     Uint8 state = 0;
   359     Uint32 flags = 0;
   360 
   361     flags = SDL_GetWindowFlags(window);
   362     if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
   363         state |= SDL_APPACTIVE;
   364     }
   365     if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
   366         state |= SDL_APPINPUTFOCUS;
   367     }
   368     if (flags & SDL_WINDOW_MOUSE_FOCUS) {
   369         state |= SDL_APPMOUSEFOCUS;
   370     }
   371     return state;
   372 }
   373 
   374 const SDL_version *
   375 SDL_Linked_Version(void)
   376 {
   377     static SDL_version version;
   378     SDL_VERSION(&version);
   379     return &version;
   380 }
   381 
   382 int
   383 SDL_SetPalette(SDL_Surface * surface, int flags, SDL_Color * colors,
   384                int firstcolor, int ncolors)
   385 {
   386     SDL_SetColors(surface, colors, firstcolor, ncolors);
   387 }
   388 
   389 int
   390 SDL_GetWMInfo(SDL_SysWMinfo * info)
   391 {
   392     return SDL_GetWindowWMInfo(window, info);
   393 }
   394 
   395 /* vi: set ts=4 sw=4 expandtab: */