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