comparison src/video/windows/SDL_windowsmodes.c @ 5062:e8916fe9cfc8

Fixed bug #925 Changed "win32" to "windows"
author Sam Lantinga <slouken@libsdl.org>
date Thu, 20 Jan 2011 18:04:05 -0800
parents src/video/win32/SDL_win32modes.c@e1664f94f026
children c2539ff054c8
comparison
equal deleted inserted replaced
5061:9e9940eae455 5062:e8916fe9cfc8
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 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 #include "SDL_windowsvideo.h"
25
26
27 static SDL_bool
28 WIN_GetDisplayMode(LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
29 {
30 SDL_DisplayModeData *data;
31 DEVMODE devmode;
32 HDC hdc;
33
34 devmode.dmSize = sizeof(devmode);
35 devmode.dmDriverExtra = 0;
36 if (!EnumDisplaySettings(deviceName, index, &devmode)) {
37 return SDL_FALSE;
38 }
39
40 data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
41 if (!data) {
42 return SDL_FALSE;
43 }
44 data->DeviceMode = devmode;
45 data->DeviceMode.dmFields =
46 (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY |
47 DM_DISPLAYFLAGS);
48
49 /* Fill in the mode information */
50 mode->format = SDL_PIXELFORMAT_UNKNOWN;
51 mode->w = devmode.dmPelsWidth;
52 mode->h = devmode.dmPelsHeight;
53 mode->refresh_rate = devmode.dmDisplayFrequency;
54 mode->driverdata = data;
55 #ifdef _WIN32_WCE
56 /* In WinCE EnumDisplaySettings(ENUM_CURRENT_SETTINGS) doesn't take the user defined orientation
57 into account but GetSystemMetrics does. */
58 if (index == ENUM_CURRENT_SETTINGS) {
59 mode->w = GetSystemMetrics(SM_CXSCREEN);
60 mode->h = GetSystemMetrics(SM_CYSCREEN);
61 }
62 #endif
63
64 /* WinCE has no GetDIBits, therefore we can't use it to get the display format */
65 #ifndef _WIN32_WCE
66 if (index == ENUM_CURRENT_SETTINGS
67 && (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
68 char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
69 LPBITMAPINFO bmi;
70 HBITMAP hbm;
71
72 SDL_zero(bmi_data);
73 bmi = (LPBITMAPINFO) bmi_data;
74 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
75
76 hbm = CreateCompatibleBitmap(hdc, 1, 1);
77 GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
78 GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
79 DeleteObject(hbm);
80 DeleteDC(hdc);
81 if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
82 switch (*(Uint32 *) bmi->bmiColors) {
83 case 0x00FF0000:
84 mode->format = SDL_PIXELFORMAT_RGB888;
85 break;
86 case 0x000000FF:
87 mode->format = SDL_PIXELFORMAT_BGR888;
88 break;
89 case 0xF800:
90 mode->format = SDL_PIXELFORMAT_RGB565;
91 break;
92 case 0x7C00:
93 mode->format = SDL_PIXELFORMAT_RGB555;
94 break;
95 }
96 } else if (bmi->bmiHeader.biBitCount == 8) {
97 mode->format = SDL_PIXELFORMAT_INDEX8;
98 } else if (bmi->bmiHeader.biBitCount == 4) {
99 mode->format = SDL_PIXELFORMAT_INDEX4LSB;
100 }
101 } else
102 #endif /* _WIN32_WCE */
103 {
104 /* FIXME: Can we tell what this will be? */
105 if ((devmode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
106 switch (devmode.dmBitsPerPel) {
107 case 32:
108 mode->format = SDL_PIXELFORMAT_RGB888;
109 break;
110 case 24:
111 mode->format = SDL_PIXELFORMAT_RGB24;
112 break;
113 case 16:
114 mode->format = SDL_PIXELFORMAT_RGB565;
115 break;
116 case 15:
117 mode->format = SDL_PIXELFORMAT_RGB555;
118 break;
119 case 8:
120 mode->format = SDL_PIXELFORMAT_INDEX8;
121 break;
122 case 4:
123 mode->format = SDL_PIXELFORMAT_INDEX4LSB;
124 break;
125 }
126 }
127 }
128 return SDL_TRUE;
129 }
130
131 static SDL_bool
132 WIN_AddDisplay(LPTSTR DeviceName)
133 {
134 SDL_VideoDisplay display;
135 SDL_DisplayData *displaydata;
136 SDL_DisplayMode mode;
137
138 #ifdef DEBUG_MODES
139 printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
140 #endif
141 if (!WIN_GetDisplayMode(DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
142 return SDL_FALSE;
143 }
144
145 displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
146 if (!displaydata) {
147 return SDL_FALSE;
148 }
149 SDL_memcpy(displaydata->DeviceName, DeviceName,
150 sizeof(displaydata->DeviceName));
151
152 SDL_zero(display);
153 display.desktop_mode = mode;
154 display.current_mode = mode;
155 display.driverdata = displaydata;
156 SDL_AddVideoDisplay(&display);
157 return SDL_TRUE;
158 }
159
160 int
161 WIN_InitModes(_THIS)
162 {
163 DWORD i, j, count;
164 DISPLAY_DEVICE device;
165
166 device.cb = sizeof(device);
167 for (i = 0;; ++i) {
168 TCHAR DeviceName[32];
169
170 if (!EnumDisplayDevices(NULL, i, &device, 0)) {
171 break;
172 }
173 if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
174 continue;
175 }
176 SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
177 #ifdef DEBUG_MODES
178 printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
179 #endif
180 count = 0;
181 for (j = 0;; ++j) {
182 if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
183 break;
184 }
185 if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
186 continue;
187 }
188 count += WIN_AddDisplay(device.DeviceName);
189 }
190 if (count == 0) {
191 WIN_AddDisplay(DeviceName);
192 }
193 }
194 if (_this->num_displays == 0) {
195 SDL_SetError("No displays available");
196 return -1;
197 }
198 return 0;
199 }
200
201 int
202 WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
203 {
204 SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->desktop_mode.driverdata;
205
206 #ifdef _WIN32_WCE
207 // WINCE: DEVMODE.dmPosition not found, or may be mingw32ce bug
208 rect->x = 0;
209 rect->y = 0;
210 rect->w = display->windows->w;
211 rect->h = display->windows->h;
212 #else
213 rect->x = (int)data->DeviceMode.dmPosition.x;
214 rect->y = (int)data->DeviceMode.dmPosition.y;
215 rect->w = data->DeviceMode.dmPelsWidth;
216 rect->h = data->DeviceMode.dmPelsHeight;
217 #endif
218 return 0;
219 }
220
221 void
222 WIN_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
223 {
224 SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
225 DWORD i;
226 SDL_DisplayMode mode;
227
228 for (i = 0;; ++i) {
229 if (!WIN_GetDisplayMode(data->DeviceName, i, &mode)) {
230 break;
231 }
232 if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
233 if (!SDL_AddDisplayMode(display, &mode)) {
234 SDL_free(mode.driverdata);
235 }
236 }
237 }
238 }
239
240 int
241 WIN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
242 {
243 SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
244 SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
245 LONG status;
246
247 #ifdef _WIN32_WCE
248 /* TODO: implement correctly.
249 On my Asus MyPAL, if I execute the code below
250 I get DISP_CHANGE_BADFLAGS and the Titlebar of the fullscreen window stays
251 visible ... (SDL_RaiseWindow() would fix that one) */
252 return 0;
253 #endif
254
255 status =
256 ChangeDisplaySettingsEx(displaydata->DeviceName, &data->DeviceMode,
257 NULL, CDS_FULLSCREEN, NULL);
258 if (status == DISP_CHANGE_SUCCESSFUL) {
259 return 0;
260 } else {
261 const char *reason = "Unknown reason";
262 switch (status) {
263 case DISP_CHANGE_BADFLAGS:
264 reason = "DISP_CHANGE_BADFLAGS";
265 break;
266 case DISP_CHANGE_BADMODE:
267 reason = "DISP_CHANGE_BADMODE";
268 break;
269 case DISP_CHANGE_BADPARAM:
270 reason = "DISP_CHANGE_BADPARAM";
271 break;
272 case DISP_CHANGE_FAILED:
273 reason = "DISP_CHANGE_FAILED";
274 break;
275 }
276 SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
277 return -1;
278 }
279 }
280
281 void
282 WIN_QuitModes(_THIS)
283 {
284 ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
285 }
286
287 /* vi: set ts=4 sw=4 expandtab: */