Skip to content

Latest commit

 

History

History
441 lines (390 loc) · 14.2 KB

SDL_windowsmodes.c

File metadata and controls

441 lines (390 loc) · 14.2 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2016
Jan 2, 2016
3
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WINDOWS
#include "SDL_windowsvideo.h"
Jan 5, 2016
Jan 5, 2016
26
#include "../../../include/SDL_assert.h"
27
28
29
30
31
32
/* Windows CE compatibility */
#ifndef CDS_FULLSCREEN
#define CDS_FULLSCREEN 0
#endif
Jul 30, 2015
Jul 30, 2015
33
typedef struct _WIN_GetMonitorDPIData {
Oct 7, 2015
Oct 7, 2015
34
35
36
SDL_VideoData *vid_data;
SDL_DisplayMode *mode;
SDL_DisplayModeData *mode_data;
Jul 30, 2015
Jul 30, 2015
37
38
39
40
} WIN_GetMonitorDPIData;
static BOOL CALLBACK
WIN_GetMonitorDPI(HMONITOR hMonitor,
Oct 7, 2015
Oct 7, 2015
41
42
43
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData)
Jul 30, 2015
Jul 30, 2015
44
{
Oct 7, 2015
Oct 7, 2015
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
WIN_GetMonitorDPIData *data = (WIN_GetMonitorDPIData*) dwData;
UINT hdpi, vdpi;
if (data->vid_data->GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &hdpi, &vdpi) == S_OK &&
hdpi > 0 &&
vdpi > 0) {
float hsize, vsize;
data->mode_data->HorzDPI = (float)hdpi;
data->mode_data->VertDPI = (float)vdpi;
// Figure out the monitor size and compute the diagonal DPI.
hsize = data->mode->w / data->mode_data->HorzDPI;
vsize = data->mode->h / data->mode_data->VertDPI;
data->mode_data->DiagDPI = SDL_ComputeDiagonalDPI( data->mode->w,
data->mode->h,
hsize,
vsize );
// We can only handle one DPI per display mode so end the enumeration.
return FALSE;
}
// We didn't get DPI information so keep going.
return TRUE;
Jul 30, 2015
Jul 30, 2015
71
72
}
Jul 30, 2015
Jul 30, 2015
74
WIN_GetDisplayMode(_THIS, LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
Jul 30, 2015
Jul 30, 2015
76
SDL_VideoData *vid_data = (SDL_VideoData *) _this->driverdata;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
SDL_DisplayModeData *data;
DEVMODE devmode;
HDC hdc;
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);
Oct 7, 2015
Oct 7, 2015
95
96
97
98
99
data->ScaleX = 1.0f;
data->ScaleY = 1.0f;
data->DiagDPI = 0.0f;
data->HorzDPI = 0.0f;
data->VertDPI = 0.0f;
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Fill in the mode information */
mode->format = SDL_PIXELFORMAT_UNKNOWN;
mode->w = devmode.dmPelsWidth;
mode->h = devmode.dmPelsHeight;
mode->refresh_rate = devmode.dmDisplayFrequency;
mode->driverdata = data;
if (index == ENUM_CURRENT_SETTINGS
&& (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
LPBITMAPINFO bmi;
HBITMAP hbm;
Oct 7, 2015
Oct 7, 2015
113
114
115
116
117
118
119
120
121
122
123
124
int logical_width = GetDeviceCaps( hdc, HORZRES );
int logical_height = GetDeviceCaps( hdc, VERTRES );
data->ScaleX = (float)logical_width / devmode.dmPelsWidth;
data->ScaleY = (float)logical_height / devmode.dmPelsHeight;
mode->w = logical_width;
mode->h = logical_height;
// WIN_GetMonitorDPI needs mode->w and mode->h
// so only call after those are set.
if (vid_data->GetDpiForMonitor) {
WIN_GetMonitorDPIData dpi_data;
Jul 30, 2015
Jul 30, 2015
125
RECT monitor_rect;
Jul 30, 2015
Jul 30, 2015
126
Oct 7, 2015
Oct 7, 2015
127
128
129
dpi_data.vid_data = vid_data;
dpi_data.mode = mode;
dpi_data.mode_data = data;
Jul 30, 2015
Jul 30, 2015
130
131
132
133
monitor_rect.left = devmode.dmPosition.x;
monitor_rect.top = devmode.dmPosition.y;
monitor_rect.right = monitor_rect.left + 1;
monitor_rect.bottom = monitor_rect.top + 1;
Oct 7, 2015
Oct 7, 2015
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
EnumDisplayMonitors(NULL, &monitor_rect, WIN_GetMonitorDPI, (LPARAM)&dpi_data);
} else {
// We don't have the Windows 8.1 routine so just
// get system DPI.
data->HorzDPI = (float)GetDeviceCaps( hdc, LOGPIXELSX );
data->VertDPI = (float)GetDeviceCaps( hdc, LOGPIXELSY );
if (data->HorzDPI == data->VertDPI) {
data->DiagDPI = data->HorzDPI;
} else {
data->DiagDPI = SDL_ComputeDiagonalDPI( mode->w,
mode->h,
(float)GetDeviceCaps( hdc, HORZSIZE ) / 25.4f,
(float)GetDeviceCaps( hdc, VERTSIZE ) / 25.4f );
}
}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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:
mode->format = SDL_PIXELFORMAT_RGB888;
break;
case 0x000000FF:
mode->format = SDL_PIXELFORMAT_BGR888;
break;
case 0xF800:
mode->format = SDL_PIXELFORMAT_RGB565;
break;
case 0x7C00:
mode->format = SDL_PIXELFORMAT_RGB555;
break;
}
} else if (bmi->bmiHeader.biBitCount == 8) {
mode->format = SDL_PIXELFORMAT_INDEX8;
} else if (bmi->bmiHeader.biBitCount == 4) {
mode->format = SDL_PIXELFORMAT_INDEX4LSB;
}
Oct 7, 2015
Oct 7, 2015
179
} else {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* FIXME: Can we tell what this will be? */
if ((devmode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
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;
}
}
}
return SDL_TRUE;
}
static SDL_bool
Jul 30, 2015
Jul 30, 2015
208
WIN_AddDisplay(_THIS, LPTSTR DeviceName)
209
210
211
212
213
214
215
216
217
{
SDL_VideoDisplay display;
SDL_DisplayData *displaydata;
SDL_DisplayMode mode;
DISPLAY_DEVICE device;
#ifdef DEBUG_MODES
printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
#endif
Jul 30, 2015
Jul 30, 2015
218
if (!WIN_GetDisplayMode(_this, DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
return SDL_FALSE;
}
displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
if (!displaydata) {
return SDL_FALSE;
}
SDL_memcpy(displaydata->DeviceName, DeviceName,
sizeof(displaydata->DeviceName));
SDL_zero(display);
device.cb = sizeof(device);
if (EnumDisplayDevices(DeviceName, 0, &device, 0)) {
display.name = WIN_StringToUTF8(device.DeviceString);
}
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = displaydata;
SDL_AddVideoDisplay(&display);
SDL_free(display.name);
return SDL_TRUE;
}
int
WIN_InitModes(_THIS)
{
int pass;
DWORD i, j, count;
DISPLAY_DEVICE device;
device.cb = sizeof(device);
/* Get the primary display in the first pass */
for (pass = 0; pass < 2; ++pass) {
for (i = 0; ; ++i) {
TCHAR DeviceName[32];
if (!EnumDisplayDevices(NULL, i, &device, 0)) {
break;
}
if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
continue;
}
if (pass == 0) {
if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
continue;
}
} else {
if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
continue;
}
}
SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
#ifdef DEBUG_MODES
printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
#endif
count = 0;
for (j = 0; ; ++j) {
if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
break;
}
if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
continue;
}
if (pass == 0) {
if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
continue;
}
} else {
if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
continue;
}
}
Jul 30, 2015
Jul 30, 2015
292
count += WIN_AddDisplay(_this, device.DeviceName);
293
294
}
if (count == 0) {
Jul 30, 2015
Jul 30, 2015
295
WIN_AddDisplay(_this, DeviceName);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
}
}
}
if (_this->num_displays == 0) {
return SDL_SetError("No displays available");
}
return 0;
}
int
WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
{
SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->current_mode.driverdata;
rect->x = (int)SDL_ceil(data->DeviceMode.dmPosition.x * data->ScaleX);
rect->y = (int)SDL_ceil(data->DeviceMode.dmPosition.y * data->ScaleY);
rect->w = (int)SDL_ceil(data->DeviceMode.dmPelsWidth * data->ScaleX);
rect->h = (int)SDL_ceil(data->DeviceMode.dmPelsHeight * data->ScaleY);
return 0;
}
Jul 30, 2015
Jul 30, 2015
318
319
320
321
322
int
WIN_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
{
SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->current_mode.driverdata;
Oct 7, 2015
Oct 7, 2015
323
324
325
326
327
328
329
330
331
332
333
if (ddpi) {
*ddpi = data->DiagDPI;
}
if (hdpi) {
*hdpi = data->HorzDPI;
}
if (vdpi) {
*vdpi = data->VertDPI;
}
return data->DiagDPI != 0.0f ? 0 : -1;
Jul 30, 2015
Jul 30, 2015
334
335
}
Jan 5, 2016
Jan 5, 2016
336
337
338
339
340
341
342
343
344
345
int
WIN_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
{
const SDL_DisplayModeData *data = (const SDL_DisplayModeData *) display->current_mode.driverdata;
const DEVMODE *pDevMode = &data->DeviceMode;
POINT pt = {
/* !!! FIXME: no scale, right? */
(LONG) (pDevMode->dmPosition.x + (pDevMode->dmPelsWidth / 2)),
(LONG) (pDevMode->dmPosition.y + (pDevMode->dmPelsHeight / 2))
};
Jan 5, 2016
Jan 5, 2016
346
HMONITOR hmon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
Jan 5, 2016
Jan 5, 2016
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
MONITORINFO minfo;
const RECT *work;
BOOL rc = FALSE;
SDL_assert(hmon != NULL);
if (hmon != NULL) {
SDL_zero(minfo);
minfo.cbSize = sizeof (MONITORINFO);
rc = GetMonitorInfo(hmon, &minfo);
SDL_assert(rc);
}
if (!rc) {
return SDL_SetError("Couldn't find monitor data");
}
Jan 5, 2016
Jan 5, 2016
364
work = &minfo.rcWork;
Jan 5, 2016
Jan 5, 2016
365
366
367
368
369
370
371
372
rect->x = (int)SDL_ceil(work->left * data->ScaleX);
rect->y = (int)SDL_ceil(work->top * data->ScaleY);
rect->w = (int)SDL_ceil((work->right - work->left) * data->ScaleX);
rect->h = (int)SDL_ceil((work->bottom - work->top) * data->ScaleY);
return 0;
}
373
374
375
376
377
378
379
380
void
WIN_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
DWORD i;
SDL_DisplayMode mode;
for (i = 0;; ++i) {
Jul 30, 2015
Jul 30, 2015
381
if (!WIN_GetDisplayMode(_this, data->DeviceName, i, &mode)) {
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
break;
}
if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
/* We don't support palettized modes now */
SDL_free(mode.driverdata);
continue;
}
if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
if (!SDL_AddDisplayMode(display, &mode)) {
SDL_free(mode.driverdata);
}
} else {
SDL_free(mode.driverdata);
}
}
}
int
WIN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
LONG status;
if (mode->driverdata == display->desktop_mode.driverdata) {
status = ChangeDisplaySettingsEx(displaydata->DeviceName, NULL, NULL, 0, NULL);
} else {
status = ChangeDisplaySettingsEx(displaydata->DeviceName, &data->DeviceMode, NULL, CDS_FULLSCREEN, NULL);
}
if (status != DISP_CHANGE_SUCCESSFUL) {
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;
}
return SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
}
EnumDisplaySettings(displaydata->DeviceName, ENUM_CURRENT_SETTINGS, &data->DeviceMode);
return 0;
}
void
WIN_QuitModes(_THIS)
{
/* All fullscreen windows should have restored modes by now */
}
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */