This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_windowsmodes.c
293 lines (264 loc) · 8.58 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
24
#include "SDL_windowsvideo.h"
25
26
27
28
29
/* Windows CE compatibility */
#ifndef CDS_FULLSCREEN
#define CDS_FULLSCREEN 0
#endif
30
31
32
33
34
35
static SDL_bool
WIN_GetDisplayMode(LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
{
SDL_DisplayModeData *data;
DEVMODE devmode;
36
#ifndef _WIN32_WCE
37
HDC hdc;
38
#endif
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
devmode.dmSize = sizeof(devmode);
devmode.dmDriverExtra = 0;
if (!EnumDisplaySettings(deviceName, index, &devmode)) {
return SDL_FALSE;
}
data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
if (!data) {
return SDL_FALSE;
}
data->DeviceMode = devmode;
data->DeviceMode.dmFields =
(DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY |
DM_DISPLAYFLAGS);
/* Fill in the mode information */
56
mode->format = SDL_PIXELFORMAT_UNKNOWN;
57
58
59
60
mode->w = devmode.dmPelsWidth;
mode->h = devmode.dmPelsHeight;
mode->refresh_rate = devmode.dmDisplayFrequency;
mode->driverdata = data;
61
62
#ifdef _WIN32_WCE
/* In WinCE EnumDisplaySettings(ENUM_CURRENT_SETTINGS) doesn't take the user defined orientation
63
into account but GetSystemMetrics does. */
64
65
66
if (index == ENUM_CURRENT_SETTINGS) {
mode->w = GetSystemMetrics(SM_CXSCREEN);
mode->h = GetSystemMetrics(SM_CYSCREEN);
67
68
}
#endif
69
70
71
/* WinCE has no GetDIBits, therefore we can't use it to get the display format */
#ifndef _WIN32_WCE
72
73
if (index == ENUM_CURRENT_SETTINGS
&& (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
LPBITMAPINFO bmi;
HBITMAP hbm;
SDL_zero(bmi_data);
bmi = (LPBITMAPINFO) bmi_data;
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
hbm = CreateCompatibleBitmap(hdc, 1, 1);
GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
DeleteObject(hbm);
DeleteDC(hdc);
if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
switch (*(Uint32 *) bmi->bmiColors) {
case 0x00FF0000:
90
mode->format = SDL_PIXELFORMAT_RGB888;
91
92
break;
case 0x000000FF:
93
mode->format = SDL_PIXELFORMAT_BGR888;
94
95
break;
case 0xF800:
96
mode->format = SDL_PIXELFORMAT_RGB565;
97
98
break;
case 0x7C00:
99
mode->format = SDL_PIXELFORMAT_RGB555;
100
101
102
break;
}
} else if (bmi->bmiHeader.biBitCount == 8) {
103
mode->format = SDL_PIXELFORMAT_INDEX8;
104
105
} else if (bmi->bmiHeader.biBitCount == 4) {
mode->format = SDL_PIXELFORMAT_INDEX4LSB;
106
}
107
108
109
} else
#endif /* _WIN32_WCE */
{
110
/* FIXME: Can we tell what this will be? */
111
if ((devmode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
switch (devmode.dmBitsPerPel) {
case 32:
mode->format = SDL_PIXELFORMAT_RGB888;
break;
case 24:
mode->format = SDL_PIXELFORMAT_RGB24;
break;
case 16:
mode->format = SDL_PIXELFORMAT_RGB565;
break;
case 15:
mode->format = SDL_PIXELFORMAT_RGB555;
break;
case 8:
mode->format = SDL_PIXELFORMAT_INDEX8;
break;
case 4:
mode->format = SDL_PIXELFORMAT_INDEX4LSB;
break;
}
132
}
133
134
135
136
}
return SDL_TRUE;
}
137
static SDL_bool
138
139
140
141
142
143
144
145
146
147
WIN_AddDisplay(LPTSTR DeviceName)
{
SDL_VideoDisplay display;
SDL_DisplayData *displaydata;
SDL_DisplayMode mode;
#ifdef DEBUG_MODES
printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
#endif
if (!WIN_GetDisplayMode(DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
148
return SDL_FALSE;
149
150
151
152
}
displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
if (!displaydata) {
153
return SDL_FALSE;
154
155
156
157
158
159
160
161
162
}
SDL_memcpy(displaydata->DeviceName, DeviceName,
sizeof(displaydata->DeviceName));
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = displaydata;
SDL_AddVideoDisplay(&display);
163
return SDL_TRUE;
164
165
}
166
int
167
168
WIN_InitModes(_THIS)
{
169
DWORD i, j, count;
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
DISPLAY_DEVICE device;
device.cb = sizeof(device);
for (i = 0;; ++i) {
TCHAR DeviceName[32];
if (!EnumDisplayDevices(NULL, i, &device, 0)) {
break;
}
if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
continue;
}
SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
#ifdef DEBUG_MODES
printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
#endif
186
count = 0;
187
188
189
190
191
192
193
for (j = 0;; ++j) {
if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
break;
}
if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
continue;
}
194
count += WIN_AddDisplay(device.DeviceName);
195
}
196
if (count == 0) {
197
WIN_AddDisplay(DeviceName);
198
199
}
}
200
201
202
203
204
if (_this->num_displays == 0) {
SDL_SetError("No displays available");
return -1;
}
return 0;
205
206
}
207
208
209
210
211
int
WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
{
SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->desktop_mode.driverdata;
212
213
214
215
216
217
218
#ifdef _WIN32_WCE
// WINCE: DEVMODE.dmPosition not found, or may be mingw32ce bug
rect->x = 0;
rect->y = 0;
rect->w = display->windows->w;
rect->h = display->windows->h;
#else
219
220
221
222
rect->x = (int)data->DeviceMode.dmPosition.x;
rect->y = (int)data->DeviceMode.dmPosition.y;
rect->w = data->DeviceMode.dmPelsWidth;
rect->h = data->DeviceMode.dmPelsHeight;
223
#endif
224
225
226
return 0;
}
227
void
228
WIN_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
229
{
230
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
231
232
233
234
235
236
237
DWORD i;
SDL_DisplayMode mode;
for (i = 0;; ++i) {
if (!WIN_GetDisplayMode(data->DeviceName, i, &mode)) {
break;
}
238
if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
239
if (!SDL_AddDisplayMode(display, &mode)) {
240
SDL_free(mode.driverdata);
241
}
242
}
243
244
245
246
}
}
int
247
WIN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
248
{
249
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
250
251
SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
LONG status;
252
253
254
255
256
#ifdef _WIN32_WCE
/* TODO: implement correctly.
On my Asus MyPAL, if I execute the code below
I get DISP_CHANGE_BADFLAGS and the Titlebar of the fullscreen window stays
257
visible ... (SDL_RaiseWindow() would fix that one) */
258
259
return 0;
#endif
260
261
status =
262
263
ChangeDisplaySettingsEx(displaydata->DeviceName, &data->DeviceMode,
NULL, CDS_FULLSCREEN, NULL);
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
if (status == DISP_CHANGE_SUCCESSFUL) {
return 0;
} else {
const char *reason = "Unknown reason";
switch (status) {
case DISP_CHANGE_BADFLAGS:
reason = "DISP_CHANGE_BADFLAGS";
break;
case DISP_CHANGE_BADMODE:
reason = "DISP_CHANGE_BADMODE";
break;
case DISP_CHANGE_BADPARAM:
reason = "DISP_CHANGE_BADPARAM";
break;
case DISP_CHANGE_FAILED:
reason = "DISP_CHANGE_FAILED";
break;
}
SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
return -1;
}
}
void
WIN_QuitModes(_THIS)
{
ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
}
/* vi: set ts=4 sw=4 expandtab: */