SDL_windowsmodes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_VIDEO_DRIVER_WINDOWS) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  20. #include "SDL_windowsvideo.h"
  21. #include "../../events/SDL_displayevents_c.h"
  22. /* Windows CE compatibility */
  23. #ifndef CDS_FULLSCREEN
  24. #define CDS_FULLSCREEN 0
  25. #endif
  26. /* #define DEBUG_MODES */
  27. /* #define HIGHDPI_DEBUG_VERBOSE */
  28. static void WIN_UpdateDisplayMode(SDL_VideoDevice *_this, LPCWSTR deviceName, DWORD index, SDL_DisplayMode *mode)
  29. {
  30. SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
  31. HDC hdc;
  32. data->DeviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS);
  33. /* NOLINTNEXTLINE(bugprone-assignment-in-if-condition): No simple way to extract the assignment */
  34. if (index == ENUM_CURRENT_SETTINGS && (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
  35. char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
  36. LPBITMAPINFO bmi;
  37. HBITMAP hbm;
  38. SDL_zeroa(bmi_data);
  39. bmi = (LPBITMAPINFO)bmi_data;
  40. bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  41. hbm = CreateCompatibleBitmap(hdc, 1, 1);
  42. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  43. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  44. DeleteObject(hbm);
  45. DeleteDC(hdc);
  46. if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
  47. switch (*(Uint32 *)bmi->bmiColors) {
  48. case 0x00FF0000:
  49. mode->format = SDL_PIXELFORMAT_RGB888;
  50. break;
  51. case 0x000000FF:
  52. mode->format = SDL_PIXELFORMAT_BGR888;
  53. break;
  54. case 0xF800:
  55. mode->format = SDL_PIXELFORMAT_RGB565;
  56. break;
  57. case 0x7C00:
  58. mode->format = SDL_PIXELFORMAT_RGB555;
  59. break;
  60. }
  61. } else if (bmi->bmiHeader.biBitCount == 8) {
  62. mode->format = SDL_PIXELFORMAT_INDEX8;
  63. } else if (bmi->bmiHeader.biBitCount == 4) {
  64. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  65. }
  66. } else if (mode->format == SDL_PIXELFORMAT_UNKNOWN) {
  67. /* FIXME: Can we tell what this will be? */
  68. if ((data->DeviceMode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
  69. switch (data->DeviceMode.dmBitsPerPel) {
  70. case 32:
  71. mode->format = SDL_PIXELFORMAT_RGB888;
  72. break;
  73. case 24:
  74. mode->format = SDL_PIXELFORMAT_RGB24;
  75. break;
  76. case 16:
  77. mode->format = SDL_PIXELFORMAT_RGB565;
  78. break;
  79. case 15:
  80. mode->format = SDL_PIXELFORMAT_RGB555;
  81. break;
  82. case 8:
  83. mode->format = SDL_PIXELFORMAT_INDEX8;
  84. break;
  85. case 4:
  86. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. static SDL_DisplayOrientation WIN_GetDisplayOrientation(DEVMODE *mode)
  93. {
  94. int width = mode->dmPelsWidth;
  95. int height = mode->dmPelsHeight;
  96. /* Use unrotated width/height to guess orientation */
  97. if (mode->dmDisplayOrientation == DMDO_90 || mode->dmDisplayOrientation == DMDO_270) {
  98. int temp = width;
  99. width = height;
  100. height = temp;
  101. }
  102. if (width >= height) {
  103. switch (mode->dmDisplayOrientation) {
  104. case DMDO_DEFAULT:
  105. return SDL_ORIENTATION_LANDSCAPE;
  106. case DMDO_90:
  107. return SDL_ORIENTATION_PORTRAIT;
  108. case DMDO_180:
  109. return SDL_ORIENTATION_LANDSCAPE_FLIPPED;
  110. case DMDO_270:
  111. return SDL_ORIENTATION_PORTRAIT_FLIPPED;
  112. default:
  113. return SDL_ORIENTATION_UNKNOWN;
  114. }
  115. } else {
  116. switch (mode->dmDisplayOrientation) {
  117. case DMDO_DEFAULT:
  118. return SDL_ORIENTATION_PORTRAIT;
  119. case DMDO_90:
  120. return SDL_ORIENTATION_LANDSCAPE_FLIPPED;
  121. case DMDO_180:
  122. return SDL_ORIENTATION_PORTRAIT_FLIPPED;
  123. case DMDO_270:
  124. return SDL_ORIENTATION_LANDSCAPE;
  125. default:
  126. return SDL_ORIENTATION_UNKNOWN;
  127. }
  128. }
  129. }
  130. static float WIN_GetRefreshRate(DEVMODE *mode)
  131. {
  132. /* We're not currently using DXGI to query display modes, so fake NTSC timings */
  133. switch (mode->dmDisplayFrequency) {
  134. case 119:
  135. case 59:
  136. case 29:
  137. return ((100 * (mode->dmDisplayFrequency + 1) * 1000) / 1001) / 100.0f;
  138. default:
  139. return (float)mode->dmDisplayFrequency;
  140. }
  141. }
  142. static float WIN_GetContentScale(SDL_VideoDevice *_this, HMONITOR hMonitor)
  143. {
  144. const SDL_VideoData *videodata = (const SDL_VideoData *)_this->driverdata;
  145. int dpi = 0;
  146. if (videodata->GetDpiForMonitor) {
  147. UINT hdpi_uint, vdpi_uint;
  148. if (videodata->GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &hdpi_uint, &vdpi_uint) == S_OK) {
  149. dpi = (int)hdpi_uint;
  150. }
  151. }
  152. if (dpi == 0) {
  153. /* Window 8.0 and below: same DPI for all monitors */
  154. HDC hdc = GetDC(NULL);
  155. if (hdc) {
  156. dpi = GetDeviceCaps(hdc, LOGPIXELSX);
  157. ReleaseDC(NULL, hdc);
  158. }
  159. }
  160. if (dpi == 0) {
  161. /* Safe default */
  162. dpi = 96;
  163. }
  164. return dpi / 96.0f;
  165. }
  166. static SDL_bool WIN_GetDisplayMode(SDL_VideoDevice *_this, HMONITOR hMonitor, LPCWSTR deviceName, DWORD index, SDL_DisplayMode *mode, SDL_DisplayOrientation *orientation)
  167. {
  168. SDL_DisplayModeData *data;
  169. DEVMODE devmode;
  170. devmode.dmSize = sizeof(devmode);
  171. devmode.dmDriverExtra = 0;
  172. if (!EnumDisplaySettingsW(deviceName, index, &devmode)) {
  173. return SDL_FALSE;
  174. }
  175. data = (SDL_DisplayModeData *)SDL_malloc(sizeof(*data));
  176. if (data == NULL) {
  177. return SDL_FALSE;
  178. }
  179. SDL_zerop(mode);
  180. mode->driverdata = data;
  181. data->DeviceMode = devmode;
  182. mode->format = SDL_PIXELFORMAT_UNKNOWN;
  183. mode->w = data->DeviceMode.dmPelsWidth;
  184. mode->h = data->DeviceMode.dmPelsHeight;
  185. mode->refresh_rate = WIN_GetRefreshRate(&data->DeviceMode);
  186. /* Fill in the mode information */
  187. WIN_UpdateDisplayMode(_this, deviceName, index, mode);
  188. if (orientation) {
  189. *orientation = WIN_GetDisplayOrientation(&devmode);
  190. }
  191. return SDL_TRUE;
  192. }
  193. /* The win32 API calls in this function require Windows Vista or later. */
  194. /* *INDENT-OFF* */ /* clang-format off */
  195. typedef LONG (WINAPI *SDL_WIN32PROC_GetDisplayConfigBufferSizes)(UINT32 flags, UINT32* numPathArrayElements, UINT32* numModeInfoArrayElements);
  196. typedef LONG (WINAPI *SDL_WIN32PROC_QueryDisplayConfig)(UINT32 flags, UINT32* numPathArrayElements, DISPLAYCONFIG_PATH_INFO* pathArray, UINT32* numModeInfoArrayElements, DISPLAYCONFIG_MODE_INFO* modeInfoArray, DISPLAYCONFIG_TOPOLOGY_ID* currentTopologyId);
  197. typedef LONG (WINAPI *SDL_WIN32PROC_DisplayConfigGetDeviceInfo)(DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket);
  198. /* *INDENT-ON* */ /* clang-format on */
  199. static char *WIN_GetDisplayNameVista(const WCHAR *deviceName)
  200. {
  201. void *dll;
  202. SDL_WIN32PROC_GetDisplayConfigBufferSizes pGetDisplayConfigBufferSizes;
  203. SDL_WIN32PROC_QueryDisplayConfig pQueryDisplayConfig;
  204. SDL_WIN32PROC_DisplayConfigGetDeviceInfo pDisplayConfigGetDeviceInfo;
  205. DISPLAYCONFIG_PATH_INFO *paths = NULL;
  206. DISPLAYCONFIG_MODE_INFO *modes = NULL;
  207. char *retval = NULL;
  208. UINT32 pathCount = 0;
  209. UINT32 modeCount = 0;
  210. UINT32 i;
  211. LONG rc;
  212. dll = SDL_LoadObject("USER32.DLL");
  213. if (dll == NULL) {
  214. return NULL;
  215. }
  216. pGetDisplayConfigBufferSizes = (SDL_WIN32PROC_GetDisplayConfigBufferSizes)SDL_LoadFunction(dll, "GetDisplayConfigBufferSizes");
  217. pQueryDisplayConfig = (SDL_WIN32PROC_QueryDisplayConfig)SDL_LoadFunction(dll, "QueryDisplayConfig");
  218. pDisplayConfigGetDeviceInfo = (SDL_WIN32PROC_DisplayConfigGetDeviceInfo)SDL_LoadFunction(dll, "DisplayConfigGetDeviceInfo");
  219. if (pGetDisplayConfigBufferSizes == NULL || pQueryDisplayConfig == NULL || pDisplayConfigGetDeviceInfo == NULL) {
  220. goto WIN_GetDisplayNameVista_failed;
  221. }
  222. do {
  223. rc = pGetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount);
  224. if (rc != ERROR_SUCCESS) {
  225. goto WIN_GetDisplayNameVista_failed;
  226. }
  227. SDL_free(paths);
  228. SDL_free(modes);
  229. paths = (DISPLAYCONFIG_PATH_INFO *)SDL_malloc(sizeof(DISPLAYCONFIG_PATH_INFO) * pathCount);
  230. modes = (DISPLAYCONFIG_MODE_INFO *)SDL_malloc(sizeof(DISPLAYCONFIG_MODE_INFO) * modeCount);
  231. if ((paths == NULL) || (modes == NULL)) {
  232. goto WIN_GetDisplayNameVista_failed;
  233. }
  234. rc = pQueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, paths, &modeCount, modes, 0);
  235. } while (rc == ERROR_INSUFFICIENT_BUFFER);
  236. if (rc == ERROR_SUCCESS) {
  237. for (i = 0; i < pathCount; i++) {
  238. DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName;
  239. DISPLAYCONFIG_TARGET_DEVICE_NAME targetName;
  240. SDL_zero(sourceName);
  241. sourceName.header.adapterId = paths[i].targetInfo.adapterId;
  242. sourceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
  243. sourceName.header.size = sizeof(sourceName);
  244. sourceName.header.id = paths[i].sourceInfo.id;
  245. rc = pDisplayConfigGetDeviceInfo(&sourceName.header);
  246. if (rc != ERROR_SUCCESS) {
  247. break;
  248. } else if (SDL_wcscmp(deviceName, sourceName.viewGdiDeviceName) != 0) {
  249. continue;
  250. }
  251. SDL_zero(targetName);
  252. targetName.header.adapterId = paths[i].targetInfo.adapterId;
  253. targetName.header.id = paths[i].targetInfo.id;
  254. targetName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
  255. targetName.header.size = sizeof(targetName);
  256. rc = pDisplayConfigGetDeviceInfo(&targetName.header);
  257. if (rc == ERROR_SUCCESS) {
  258. retval = WIN_StringToUTF8W(targetName.monitorFriendlyDeviceName);
  259. /* if we got an empty string, treat it as failure so we'll fallback
  260. to getting the generic name. */
  261. if (retval && (*retval == '\0')) {
  262. SDL_free(retval);
  263. retval = NULL;
  264. }
  265. }
  266. break;
  267. }
  268. }
  269. SDL_free(paths);
  270. SDL_free(modes);
  271. SDL_UnloadObject(dll);
  272. return retval;
  273. WIN_GetDisplayNameVista_failed:
  274. SDL_free(retval);
  275. SDL_free(paths);
  276. SDL_free(modes);
  277. SDL_UnloadObject(dll);
  278. return NULL;
  279. }
  280. static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONITORINFOEXW *info, int *display_index, SDL_bool send_event)
  281. {
  282. int i, index = *display_index;
  283. SDL_DisplayID displayID;
  284. SDL_VideoDisplay display;
  285. SDL_DisplayData *displaydata;
  286. SDL_DisplayMode mode;
  287. SDL_DisplayOrientation orientation;
  288. float content_scale = WIN_GetContentScale(_this, hMonitor);
  289. #ifdef DEBUG_MODES
  290. SDL_Log("Display: %s\n", WIN_StringToUTF8W(info->szDevice));
  291. #endif
  292. if (!WIN_GetDisplayMode(_this, hMonitor, info->szDevice, ENUM_CURRENT_SETTINGS, &mode, &orientation)) {
  293. return;
  294. }
  295. // Prevent adding duplicate displays. Do this after we know the display is
  296. // ready to be added to allow any displays that we can't fully query to be
  297. // removed
  298. for (i = 0; i < _this->num_displays; ++i) {
  299. SDL_DisplayData *driverdata = _this->displays[i].driverdata;
  300. if (SDL_wcscmp(driverdata->DeviceName, info->szDevice) == 0) {
  301. SDL_bool moved = (index != i);
  302. SDL_bool changed_bounds = SDL_FALSE;
  303. if (moved) {
  304. SDL_VideoDisplay tmp;
  305. SDL_assert(index < _this->num_displays);
  306. SDL_memcpy(&tmp, &_this->displays[index], sizeof(tmp));
  307. SDL_memcpy(&_this->displays[index], &_this->displays[i], sizeof(tmp));
  308. SDL_memcpy(&_this->displays[i], &tmp, sizeof(tmp));
  309. i = index;
  310. }
  311. driverdata->MonitorHandle = hMonitor;
  312. driverdata->IsValid = SDL_TRUE;
  313. if (!_this->setting_display_mode) {
  314. SDL_VideoDisplay *existing_display = &_this->displays[i];
  315. SDL_Rect bounds;
  316. SDL_ResetFullscreenDisplayModes(existing_display);
  317. SDL_SetDesktopDisplayMode(existing_display, &mode);
  318. if (WIN_GetDisplayBounds(_this, existing_display, &bounds) == 0 &&
  319. SDL_memcmp(&driverdata->bounds, &bounds, sizeof(bounds)) != 0) {
  320. changed_bounds = SDL_TRUE;
  321. SDL_copyp(&driverdata->bounds, &bounds);
  322. }
  323. if (moved || changed_bounds) {
  324. SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_MOVED, 0);
  325. }
  326. SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_ORIENTATION, orientation);
  327. SDL_SetDisplayContentScale(existing_display, content_scale);
  328. }
  329. goto done;
  330. }
  331. }
  332. displaydata = (SDL_DisplayData *)SDL_calloc(1, sizeof(*displaydata));
  333. if (displaydata == NULL) {
  334. return;
  335. }
  336. SDL_memcpy(displaydata->DeviceName, info->szDevice, sizeof(displaydata->DeviceName));
  337. displaydata->MonitorHandle = hMonitor;
  338. displaydata->IsValid = SDL_TRUE;
  339. SDL_zero(display);
  340. display.name = WIN_GetDisplayNameVista(info->szDevice);
  341. if (display.name == NULL) {
  342. DISPLAY_DEVICEW device;
  343. SDL_zero(device);
  344. device.cb = sizeof(device);
  345. if (EnumDisplayDevicesW(info->szDevice, 0, &device, 0)) {
  346. display.name = WIN_StringToUTF8W(device.DeviceString);
  347. }
  348. }
  349. display.desktop_mode = mode;
  350. display.orientation = orientation;
  351. display.content_scale = content_scale;
  352. display.device = _this;
  353. display.driverdata = displaydata;
  354. WIN_GetDisplayBounds(_this, &display, &displaydata->bounds);
  355. displayID = SDL_AddVideoDisplay(&display, send_event);
  356. SDL_assert(SDL_GetDisplayIndex(displayID) == *display_index);
  357. SDL_free(display.name);
  358. done:
  359. *display_index += 1;
  360. }
  361. typedef struct _WIN_AddDisplaysData
  362. {
  363. SDL_VideoDevice *video_device;
  364. int display_index;
  365. SDL_bool send_event;
  366. SDL_bool want_primary;
  367. } WIN_AddDisplaysData;
  368. static BOOL CALLBACK WIN_AddDisplaysCallback(HMONITOR hMonitor,
  369. HDC hdcMonitor,
  370. LPRECT lprcMonitor,
  371. LPARAM dwData)
  372. {
  373. WIN_AddDisplaysData *data = (WIN_AddDisplaysData *)dwData;
  374. MONITORINFOEXW info;
  375. SDL_zero(info);
  376. info.cbSize = sizeof(info);
  377. if (GetMonitorInfoW(hMonitor, (LPMONITORINFO)&info) != 0) {
  378. const SDL_bool is_primary = ((info.dwFlags & MONITORINFOF_PRIMARY) == MONITORINFOF_PRIMARY);
  379. if (is_primary == data->want_primary) {
  380. WIN_AddDisplay(data->video_device, hMonitor, &info, &data->display_index, data->send_event);
  381. }
  382. }
  383. // continue enumeration
  384. return TRUE;
  385. }
  386. static void WIN_AddDisplays(SDL_VideoDevice *_this, SDL_bool send_event)
  387. {
  388. WIN_AddDisplaysData callback_data;
  389. callback_data.video_device = _this;
  390. callback_data.display_index = 0;
  391. callback_data.send_event = send_event;
  392. callback_data.want_primary = SDL_TRUE;
  393. EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
  394. callback_data.want_primary = SDL_FALSE;
  395. EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
  396. }
  397. int WIN_InitModes(SDL_VideoDevice *_this)
  398. {
  399. WIN_AddDisplays(_this, SDL_FALSE);
  400. if (_this->num_displays == 0) {
  401. return SDL_SetError("No displays available");
  402. }
  403. return 0;
  404. }
  405. int WIN_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  406. {
  407. const SDL_DisplayData *data = display->driverdata;
  408. MONITORINFO minfo;
  409. BOOL rc;
  410. SDL_zero(minfo);
  411. minfo.cbSize = sizeof(MONITORINFO);
  412. rc = GetMonitorInfo(data->MonitorHandle, &minfo);
  413. if (!rc) {
  414. return SDL_SetError("Couldn't find monitor data");
  415. }
  416. rect->x = minfo.rcMonitor.left;
  417. rect->y = minfo.rcMonitor.top;
  418. rect->w = minfo.rcMonitor.right - minfo.rcMonitor.left;
  419. rect->h = minfo.rcMonitor.bottom - minfo.rcMonitor.top;
  420. return 0;
  421. }
  422. int WIN_GetDisplayUsableBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  423. {
  424. const SDL_DisplayData *data = display->driverdata;
  425. MONITORINFO minfo;
  426. BOOL rc;
  427. SDL_zero(minfo);
  428. minfo.cbSize = sizeof(MONITORINFO);
  429. rc = GetMonitorInfo(data->MonitorHandle, &minfo);
  430. if (!rc) {
  431. return SDL_SetError("Couldn't find monitor data");
  432. }
  433. rect->x = minfo.rcWork.left;
  434. rect->y = minfo.rcWork.top;
  435. rect->w = minfo.rcWork.right - minfo.rcWork.left;
  436. rect->h = minfo.rcWork.bottom - minfo.rcWork.top;
  437. return 0;
  438. }
  439. int WIN_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)
  440. {
  441. SDL_DisplayData *data = display->driverdata;
  442. DWORD i;
  443. SDL_DisplayMode mode;
  444. for (i = 0;; ++i) {
  445. if (!WIN_GetDisplayMode(_this, data->MonitorHandle, data->DeviceName, i, &mode, NULL)) {
  446. break;
  447. }
  448. if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
  449. /* We don't support palettized modes now */
  450. SDL_free(mode.driverdata);
  451. continue;
  452. }
  453. if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
  454. if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
  455. SDL_free(mode.driverdata);
  456. }
  457. } else {
  458. SDL_free(mode.driverdata);
  459. }
  460. }
  461. return 0;
  462. }
  463. #ifdef DEBUG_MODES
  464. static void WIN_LogMonitor(SDL_VideoDevice *_this, HMONITOR mon)
  465. {
  466. const SDL_VideoData *vid_data = (const SDL_VideoData *)_this->driverdata;
  467. MONITORINFOEX minfo;
  468. UINT xdpi = 0, ydpi = 0;
  469. char *name_utf8;
  470. if (vid_data->GetDpiForMonitor) {
  471. vid_data->GetDpiForMonitor(mon, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
  472. }
  473. SDL_zero(minfo);
  474. minfo.cbSize = sizeof(minfo);
  475. GetMonitorInfo(mon, (LPMONITORINFO)&minfo);
  476. name_utf8 = WIN_StringToUTF8(minfo.szDevice);
  477. SDL_Log("WIN_LogMonitor: monitor \"%s\": dpi: %d windows screen coordinates: %d, %d, %dx%d",
  478. name_utf8,
  479. xdpi,
  480. minfo.rcMonitor.left,
  481. minfo.rcMonitor.top,
  482. minfo.rcMonitor.right - minfo.rcMonitor.left,
  483. minfo.rcMonitor.bottom - minfo.rcMonitor.top);
  484. SDL_free(name_utf8);
  485. }
  486. #endif
  487. int WIN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  488. {
  489. SDL_DisplayData *displaydata = display->driverdata;
  490. SDL_DisplayModeData *data = mode->driverdata;
  491. LONG status;
  492. #ifdef DEBUG_MODES
  493. SDL_Log("WIN_SetDisplayMode: monitor state before mode change:");
  494. WIN_LogMonitor(_this, displaydata->MonitorHandle);
  495. #endif
  496. /* High-DPI notes:
  497. - ChangeDisplaySettingsEx always takes pixels.
  498. - e.g. if the display is set to 2880x1800 with 200% scaling in Display Settings
  499. - calling ChangeDisplaySettingsEx with a dmPelsWidth/Height other than 2880x1800 will
  500. change the monitor DPI to 96. (100% scaling)
  501. - calling ChangeDisplaySettingsEx with a dmPelsWidth/Height of 2880x1800 (or a NULL DEVMODE*) will
  502. reset the monitor DPI to 192. (200% scaling)
  503. NOTE: these are temporary changes in DPI, not modifications to the Control Panel setting. */
  504. if (mode->driverdata == display->desktop_mode.driverdata) {
  505. #ifdef DEBUG_MODES
  506. SDL_Log("WIN_SetDisplayMode: resetting to original resolution");
  507. #endif
  508. status = ChangeDisplaySettingsExW(displaydata->DeviceName, NULL, NULL, CDS_FULLSCREEN, NULL);
  509. } else {
  510. #ifdef DEBUG_MODES
  511. SDL_Log("WIN_SetDisplayMode: changing to %dx%d pixels", data->DeviceMode.dmPelsWidth, data->DeviceMode.dmPelsHeight);
  512. #endif
  513. status = ChangeDisplaySettingsExW(displaydata->DeviceName, &data->DeviceMode, NULL, CDS_FULLSCREEN, NULL);
  514. }
  515. if (status != DISP_CHANGE_SUCCESSFUL) {
  516. const char *reason = "Unknown reason";
  517. switch (status) {
  518. case DISP_CHANGE_BADFLAGS:
  519. reason = "DISP_CHANGE_BADFLAGS";
  520. break;
  521. case DISP_CHANGE_BADMODE:
  522. reason = "DISP_CHANGE_BADMODE";
  523. break;
  524. case DISP_CHANGE_BADPARAM:
  525. reason = "DISP_CHANGE_BADPARAM";
  526. break;
  527. case DISP_CHANGE_FAILED:
  528. reason = "DISP_CHANGE_FAILED";
  529. break;
  530. }
  531. return SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
  532. }
  533. #ifdef DEBUG_MODES
  534. SDL_Log("WIN_SetDisplayMode: monitor state after mode change:");
  535. WIN_LogMonitor(_this, displaydata->MonitorHandle);
  536. #endif
  537. EnumDisplaySettingsW(displaydata->DeviceName, ENUM_CURRENT_SETTINGS, &data->DeviceMode);
  538. WIN_UpdateDisplayMode(_this, displaydata->DeviceName, ENUM_CURRENT_SETTINGS, mode);
  539. return 0;
  540. }
  541. void WIN_RefreshDisplays(SDL_VideoDevice *_this)
  542. {
  543. int i;
  544. // Mark all displays as potentially invalid to detect
  545. // entries that have actually been removed
  546. for (i = 0; i < _this->num_displays; ++i) {
  547. SDL_DisplayData *driverdata = _this->displays[i].driverdata;
  548. driverdata->IsValid = SDL_FALSE;
  549. }
  550. // Enumerate displays to add any new ones and mark still
  551. // connected entries as valid
  552. WIN_AddDisplays(_this, SDL_TRUE);
  553. // Delete any entries still marked as invalid, iterate
  554. // in reverse as each delete takes effect immediately
  555. for (i = _this->num_displays - 1; i >= 0; --i) {
  556. SDL_VideoDisplay *display = &_this->displays[i];
  557. SDL_DisplayData *driverdata = display->driverdata;
  558. if (driverdata->IsValid == SDL_FALSE) {
  559. SDL_DelVideoDisplay(display->id, SDL_TRUE);
  560. }
  561. }
  562. }
  563. void WIN_QuitModes(SDL_VideoDevice *_this)
  564. {
  565. /* All fullscreen windows should have restored modes by now */
  566. }
  567. #endif /* SDL_VIDEO_DRIVER_WINDOWS */