src/video/win32/SDL_win32modes.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 2149 eba4fd03b4f6
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_win32video.h"
slouken@1895
    25
slouken@1895
    26
slouken@1895
    27
static SDL_bool
slouken@1895
    28
WIN_GetDisplayMode(LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
slouken@1895
    29
{
slouken@1895
    30
    SDL_DisplayModeData *data;
slouken@1895
    31
    DEVMODE devmode;
slouken@1895
    32
    HDC hdc;
slouken@1895
    33
slouken@1895
    34
    devmode.dmSize = sizeof(devmode);
slouken@1895
    35
    devmode.dmDriverExtra = 0;
slouken@1895
    36
    if (!EnumDisplaySettings(deviceName, index, &devmode)) {
slouken@1895
    37
        return SDL_FALSE;
slouken@1895
    38
    }
slouken@1895
    39
slouken@1895
    40
    data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
slouken@1895
    41
    if (!data) {
slouken@1895
    42
        return SDL_FALSE;
slouken@1895
    43
    }
slouken@1895
    44
    SDL_memcpy(data->DeviceName, deviceName, sizeof(data->DeviceName));
slouken@1895
    45
    data->DeviceMode = devmode;
slouken@1895
    46
    data->DeviceMode.dmFields =
slouken@1895
    47
        (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY |
slouken@1895
    48
         DM_DISPLAYFLAGS);
slouken@1895
    49
slouken@1895
    50
    /* Fill in the mode information */
slouken@1965
    51
    mode->format = SDL_PIXELFORMAT_UNKNOWN;
slouken@1895
    52
    mode->w = devmode.dmPelsWidth;
slouken@1895
    53
    mode->h = devmode.dmPelsHeight;
slouken@1895
    54
    mode->refresh_rate = devmode.dmDisplayFrequency;
slouken@1895
    55
    mode->driverdata = data;
slouken@1895
    56
slouken@1913
    57
    if (index == ENUM_CURRENT_SETTINGS
slouken@1913
    58
        && (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
slouken@1895
    59
        char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
slouken@1895
    60
        LPBITMAPINFO bmi;
slouken@1895
    61
        HBITMAP hbm;
slouken@1895
    62
slouken@1895
    63
        SDL_zero(bmi_data);
slouken@1895
    64
        bmi = (LPBITMAPINFO) bmi_data;
slouken@1895
    65
        bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
slouken@1895
    66
slouken@1895
    67
        hbm = CreateCompatibleBitmap(hdc, 1, 1);
slouken@1895
    68
        GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
slouken@1895
    69
        GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
slouken@1895
    70
        DeleteObject(hbm);
slouken@1895
    71
        DeleteDC(hdc);
slouken@1895
    72
        if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
slouken@1895
    73
            switch (*(Uint32 *) bmi->bmiColors) {
slouken@1895
    74
            case 0x00FF0000:
slouken@1965
    75
                mode->format = SDL_PIXELFORMAT_RGB888;
slouken@1895
    76
                break;
slouken@1895
    77
            case 0x000000FF:
slouken@1965
    78
                mode->format = SDL_PIXELFORMAT_BGR888;
slouken@1895
    79
                break;
slouken@1895
    80
            case 0xF800:
slouken@1965
    81
                mode->format = SDL_PIXELFORMAT_RGB565;
slouken@1895
    82
                break;
slouken@1895
    83
            case 0x7C00:
slouken@1965
    84
                mode->format = SDL_PIXELFORMAT_RGB555;
slouken@1895
    85
                break;
slouken@1895
    86
            }
slouken@1895
    87
        } else if (bmi->bmiHeader.biBitCount == 8) {
slouken@1965
    88
            mode->format = SDL_PIXELFORMAT_INDEX8;
slouken@1895
    89
        }
slouken@1895
    90
    } else {
slouken@1913
    91
        /* FIXME: Can we tell what this will be? */
slouken@1895
    92
        switch (devmode.dmBitsPerPel) {
slouken@1895
    93
        case 32:
slouken@1965
    94
            mode->format = SDL_PIXELFORMAT_RGB888;
slouken@1895
    95
            break;
slouken@1895
    96
        case 24:
slouken@1965
    97
            mode->format = SDL_PIXELFORMAT_RGB24;
slouken@1895
    98
            break;
slouken@1895
    99
        case 16:
slouken@1965
   100
            mode->format = SDL_PIXELFORMAT_RGB565;
slouken@1895
   101
            break;
slouken@1895
   102
        case 15:
slouken@1965
   103
            mode->format = SDL_PIXELFORMAT_RGB555;
slouken@1895
   104
            break;
slouken@1895
   105
        case 8:
slouken@1965
   106
            mode->format = SDL_PIXELFORMAT_INDEX8;
slouken@1895
   107
            break;
slouken@1895
   108
        }
slouken@1895
   109
    }
slouken@1895
   110
    return SDL_TRUE;
slouken@1895
   111
}
slouken@1895
   112
slouken@2149
   113
static SDL_bool
slouken@2119
   114
WIN_AddDisplay(LPTSTR DeviceName)
slouken@2119
   115
{
slouken@2119
   116
    SDL_VideoDisplay display;
slouken@2119
   117
    SDL_DisplayData *displaydata;
slouken@2119
   118
    SDL_DisplayMode mode;
slouken@2119
   119
slouken@2119
   120
#ifdef DEBUG_MODES
slouken@2119
   121
    printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
slouken@2119
   122
#endif
slouken@2119
   123
    if (!WIN_GetDisplayMode(DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
slouken@2149
   124
        return SDL_FALSE;
slouken@2119
   125
    }
slouken@2119
   126
slouken@2119
   127
    displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
slouken@2119
   128
    if (!displaydata) {
slouken@2149
   129
        return SDL_FALSE;
slouken@2119
   130
    }
slouken@2119
   131
    SDL_memcpy(displaydata->DeviceName, DeviceName,
slouken@2119
   132
               sizeof(displaydata->DeviceName));
slouken@2119
   133
slouken@2119
   134
    SDL_zero(display);
slouken@2119
   135
    display.desktop_mode = mode;
slouken@2119
   136
    display.current_mode = mode;
slouken@2119
   137
    display.driverdata = displaydata;
slouken@2119
   138
    SDL_AddVideoDisplay(&display);
slouken@2149
   139
    return SDL_TRUE;
slouken@2119
   140
}
slouken@2119
   141
slouken@1895
   142
void
slouken@1895
   143
WIN_InitModes(_THIS)
slouken@1895
   144
{
slouken@1895
   145
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2149
   146
    DWORD i, j, count;
slouken@1895
   147
    DISPLAY_DEVICE device;
slouken@1895
   148
slouken@1895
   149
    device.cb = sizeof(device);
slouken@1895
   150
    for (i = 0;; ++i) {
slouken@1895
   151
        TCHAR DeviceName[32];
slouken@1895
   152
slouken@1895
   153
        if (!EnumDisplayDevices(NULL, i, &device, 0)) {
slouken@1895
   154
            break;
slouken@1895
   155
        }
slouken@1895
   156
        if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
slouken@1895
   157
            continue;
slouken@1895
   158
        }
slouken@1895
   159
        SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
slouken@1895
   160
#ifdef DEBUG_MODES
slouken@1895
   161
        printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
slouken@1895
   162
#endif
slouken@2149
   163
        count = 0;
slouken@1895
   164
        for (j = 0;; ++j) {
slouken@1895
   165
            if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
slouken@1895
   166
                break;
slouken@1895
   167
            }
slouken@1895
   168
            if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
slouken@1895
   169
                continue;
slouken@1895
   170
            }
slouken@2149
   171
            count += WIN_AddDisplay(device.DeviceName);
slouken@2119
   172
        }
slouken@2149
   173
        if (count == 0) {
slouken@2119
   174
            WIN_AddDisplay(DeviceName);
slouken@1895
   175
        }
slouken@1895
   176
    }
slouken@1895
   177
}
slouken@1895
   178
slouken@1895
   179
void
slouken@1895
   180
WIN_GetDisplayModes(_THIS)
slouken@1895
   181
{
slouken@1895
   182
    SDL_DisplayData *data = (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
slouken@1895
   183
    DWORD i;
slouken@1895
   184
    SDL_DisplayMode mode;
slouken@1895
   185
slouken@1895
   186
    for (i = 0;; ++i) {
slouken@1895
   187
        if (!WIN_GetDisplayMode(data->DeviceName, i, &mode)) {
slouken@1895
   188
            break;
slouken@1895
   189
        }
slouken@1895
   190
        if (!SDL_AddDisplayMode(_this->current_display, &mode)) {
slouken@1895
   191
            SDL_free(mode.driverdata);
slouken@1895
   192
        }
slouken@1895
   193
    }
slouken@1895
   194
}
slouken@1895
   195
slouken@1895
   196
int
slouken@1895
   197
WIN_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
slouken@1895
   198
{
slouken@1895
   199
    SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
slouken@1895
   200
    LONG status;
slouken@1895
   201
slouken@1895
   202
    status =
slouken@1895
   203
        ChangeDisplaySettingsEx(data->DeviceName, &data->DeviceMode, NULL,
slouken@1895
   204
                                CDS_FULLSCREEN, NULL);
slouken@1895
   205
    if (status == DISP_CHANGE_SUCCESSFUL) {
slouken@1895
   206
        return 0;
slouken@1895
   207
    } else {
slouken@1895
   208
        const char *reason = "Unknown reason";
slouken@1895
   209
        switch (status) {
slouken@1895
   210
        case DISP_CHANGE_BADFLAGS:
slouken@1895
   211
            reason = "DISP_CHANGE_BADFLAGS";
slouken@1895
   212
            break;
slouken@1895
   213
        case DISP_CHANGE_BADMODE:
slouken@1895
   214
            reason = "DISP_CHANGE_BADMODE";
slouken@1895
   215
            break;
slouken@1895
   216
        case DISP_CHANGE_BADPARAM:
slouken@1895
   217
            reason = "DISP_CHANGE_BADPARAM";
slouken@1895
   218
            break;
slouken@1895
   219
        case DISP_CHANGE_FAILED:
slouken@1895
   220
            reason = "DISP_CHANGE_FAILED";
slouken@1895
   221
            break;
slouken@1895
   222
        }
slouken@1895
   223
        SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
slouken@1895
   224
        return -1;
slouken@1895
   225
    }
slouken@1895
   226
}
slouken@1895
   227
slouken@1895
   228
void
slouken@1895
   229
WIN_QuitModes(_THIS)
slouken@1895
   230
{
slouken@1895
   231
    ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
slouken@1895
   232
}
slouken@1895
   233
slouken@1895
   234
/* vi: set ts=4 sw=4 expandtab: */