SDL_windowsmodes.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  20. #include "SDL_windowsvideo.h"
  21. #include "../../events/SDL_displayevents_c.h"
  22. #ifdef HAVE_DXGI1_6_H
  23. #define COBJMACROS
  24. #include <dxgi1_6.h>
  25. #endif
  26. // Windows CE compatibility
  27. #ifndef CDS_FULLSCREEN
  28. #define CDS_FULLSCREEN 0
  29. #endif
  30. // #define DEBUG_MODES
  31. // #define HIGHDPI_DEBUG_VERBOSE
  32. static void WIN_UpdateDisplayMode(SDL_VideoDevice *_this, LPCWSTR deviceName, DWORD index, SDL_DisplayMode *mode)
  33. {
  34. SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->internal;
  35. HDC hdc;
  36. data->DeviceMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS);
  37. // NOLINTNEXTLINE(bugprone-assignment-in-if-condition): No simple way to extract the assignment
  38. if (index == ENUM_CURRENT_SETTINGS && (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
  39. char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
  40. LPBITMAPINFO bmi;
  41. HBITMAP hbm;
  42. SDL_zeroa(bmi_data);
  43. bmi = (LPBITMAPINFO)bmi_data;
  44. bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  45. hbm = CreateCompatibleBitmap(hdc, 1, 1);
  46. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  47. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  48. DeleteObject(hbm);
  49. DeleteDC(hdc);
  50. if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
  51. switch (*(Uint32 *)bmi->bmiColors) {
  52. case 0x00FF0000:
  53. mode->format = SDL_PIXELFORMAT_XRGB8888;
  54. break;
  55. case 0x000000FF:
  56. mode->format = SDL_PIXELFORMAT_XBGR8888;
  57. break;
  58. case 0xF800:
  59. mode->format = SDL_PIXELFORMAT_RGB565;
  60. break;
  61. case 0x7C00:
  62. mode->format = SDL_PIXELFORMAT_XRGB1555;
  63. break;
  64. }
  65. } else if (bmi->bmiHeader.biCompression == BI_RGB) {
  66. if (bmi->bmiHeader.biBitCount == 24) {
  67. mode->format = SDL_PIXELFORMAT_RGB24;
  68. } else if (bmi->bmiHeader.biBitCount == 8) {
  69. mode->format = SDL_PIXELFORMAT_INDEX8;
  70. } else if (bmi->bmiHeader.biBitCount == 4) {
  71. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  72. }
  73. }
  74. } else if (mode->format == SDL_PIXELFORMAT_UNKNOWN) {
  75. // FIXME: Can we tell what this will be?
  76. if ((data->DeviceMode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
  77. switch (data->DeviceMode.dmBitsPerPel) {
  78. case 32:
  79. mode->format = SDL_PIXELFORMAT_XRGB8888;
  80. break;
  81. case 24:
  82. mode->format = SDL_PIXELFORMAT_RGB24;
  83. break;
  84. case 16:
  85. mode->format = SDL_PIXELFORMAT_RGB565;
  86. break;
  87. case 15:
  88. mode->format = SDL_PIXELFORMAT_XRGB1555;
  89. break;
  90. case 8:
  91. mode->format = SDL_PIXELFORMAT_INDEX8;
  92. break;
  93. case 4:
  94. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. static void *WIN_GetDXGIOutput(SDL_VideoDevice *_this, const WCHAR *DeviceName)
  101. {
  102. void *result = NULL;
  103. #ifdef HAVE_DXGI_H
  104. const SDL_VideoData *videodata = (const SDL_VideoData *)_this->internal;
  105. int nAdapter, nOutput;
  106. IDXGIAdapter *pDXGIAdapter;
  107. IDXGIOutput *pDXGIOutput;
  108. if (!videodata->pDXGIFactory) {
  109. return NULL;
  110. }
  111. nAdapter = 0;
  112. while (!result && SUCCEEDED(IDXGIFactory_EnumAdapters(videodata->pDXGIFactory, nAdapter, &pDXGIAdapter))) {
  113. nOutput = 0;
  114. while (!result && SUCCEEDED(IDXGIAdapter_EnumOutputs(pDXGIAdapter, nOutput, &pDXGIOutput))) {
  115. DXGI_OUTPUT_DESC outputDesc;
  116. if (SUCCEEDED(IDXGIOutput_GetDesc(pDXGIOutput, &outputDesc))) {
  117. if (SDL_wcscmp(outputDesc.DeviceName, DeviceName) == 0) {
  118. result = pDXGIOutput;
  119. }
  120. }
  121. if (pDXGIOutput != result) {
  122. IDXGIOutput_Release(pDXGIOutput);
  123. }
  124. nOutput++;
  125. }
  126. IDXGIAdapter_Release(pDXGIAdapter);
  127. nAdapter++;
  128. }
  129. #endif
  130. return result;
  131. }
  132. static void WIN_ReleaseDXGIOutput(void *dxgi_output)
  133. {
  134. #ifdef HAVE_DXGI_H
  135. IDXGIOutput *pDXGIOutput = (IDXGIOutput *)dxgi_output;
  136. if (pDXGIOutput) {
  137. IDXGIOutput_Release(pDXGIOutput);
  138. }
  139. #endif
  140. }
  141. static SDL_DisplayOrientation WIN_GetNaturalOrientation(DEVMODE *mode)
  142. {
  143. int width = mode->dmPelsWidth;
  144. int height = mode->dmPelsHeight;
  145. // Use unrotated width/height to guess orientation
  146. if (mode->dmDisplayOrientation == DMDO_90 || mode->dmDisplayOrientation == DMDO_270) {
  147. int temp = width;
  148. width = height;
  149. height = temp;
  150. }
  151. if (width >= height) {
  152. return SDL_ORIENTATION_LANDSCAPE;
  153. } else {
  154. return SDL_ORIENTATION_PORTRAIT;
  155. }
  156. }
  157. static SDL_DisplayOrientation WIN_GetDisplayOrientation(DEVMODE *mode)
  158. {
  159. if (WIN_GetNaturalOrientation(mode) == SDL_ORIENTATION_LANDSCAPE) {
  160. switch (mode->dmDisplayOrientation) {
  161. case DMDO_DEFAULT:
  162. return SDL_ORIENTATION_LANDSCAPE;
  163. case DMDO_90:
  164. return SDL_ORIENTATION_PORTRAIT;
  165. case DMDO_180:
  166. return SDL_ORIENTATION_LANDSCAPE_FLIPPED;
  167. case DMDO_270:
  168. return SDL_ORIENTATION_PORTRAIT_FLIPPED;
  169. default:
  170. return SDL_ORIENTATION_UNKNOWN;
  171. }
  172. } else {
  173. switch (mode->dmDisplayOrientation) {
  174. case DMDO_DEFAULT:
  175. return SDL_ORIENTATION_PORTRAIT;
  176. case DMDO_90:
  177. return SDL_ORIENTATION_LANDSCAPE_FLIPPED;
  178. case DMDO_180:
  179. return SDL_ORIENTATION_PORTRAIT_FLIPPED;
  180. case DMDO_270:
  181. return SDL_ORIENTATION_LANDSCAPE;
  182. default:
  183. return SDL_ORIENTATION_UNKNOWN;
  184. }
  185. }
  186. }
  187. static void WIN_GetRefreshRate(void *dxgi_output, DEVMODE *mode, int *numerator, int *denominator)
  188. {
  189. // We're not currently using DXGI to query display modes, so fake NTSC timings
  190. switch (mode->dmDisplayFrequency) {
  191. case 119:
  192. case 59:
  193. case 29:
  194. *numerator = (mode->dmDisplayFrequency + 1) * 1000;
  195. *denominator = 1001;
  196. break;
  197. default:
  198. *numerator = mode->dmDisplayFrequency;
  199. *denominator = 1;
  200. break;
  201. }
  202. #ifdef HAVE_DXGI_H
  203. if (dxgi_output) {
  204. IDXGIOutput *pDXGIOutput = (IDXGIOutput *)dxgi_output;
  205. DXGI_MODE_DESC modeToMatch;
  206. DXGI_MODE_DESC closestMatch;
  207. SDL_zero(modeToMatch);
  208. modeToMatch.Width = mode->dmPelsWidth;
  209. modeToMatch.Height = mode->dmPelsHeight;
  210. modeToMatch.RefreshRate.Numerator = *numerator;
  211. modeToMatch.RefreshRate.Denominator = *denominator;
  212. modeToMatch.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  213. if (SUCCEEDED(IDXGIOutput_FindClosestMatchingMode(pDXGIOutput, &modeToMatch, &closestMatch, NULL))) {
  214. *numerator = closestMatch.RefreshRate.Numerator;
  215. *denominator = closestMatch.RefreshRate.Denominator;
  216. }
  217. }
  218. #endif // HAVE_DXGI_H
  219. }
  220. static float WIN_GetContentScale(SDL_VideoDevice *_this, HMONITOR hMonitor)
  221. {
  222. const SDL_VideoData *videodata = (const SDL_VideoData *)_this->internal;
  223. int dpi = 0;
  224. if (videodata->GetDpiForMonitor) {
  225. UINT hdpi_uint, vdpi_uint;
  226. if (videodata->GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &hdpi_uint, &vdpi_uint) == S_OK) {
  227. dpi = (int)hdpi_uint;
  228. }
  229. }
  230. if (dpi == 0) {
  231. // Window 8.0 and below: same DPI for all monitors
  232. HDC hdc = GetDC(NULL);
  233. if (hdc) {
  234. dpi = GetDeviceCaps(hdc, LOGPIXELSX);
  235. ReleaseDC(NULL, hdc);
  236. }
  237. }
  238. if (dpi == 0) {
  239. // Safe default
  240. dpi = USER_DEFAULT_SCREEN_DPI;
  241. }
  242. return dpi / (float)USER_DEFAULT_SCREEN_DPI;
  243. }
  244. static bool WIN_GetDisplayMode(SDL_VideoDevice *_this, void *dxgi_output, HMONITOR hMonitor, LPCWSTR deviceName, DWORD index, SDL_DisplayMode *mode, SDL_DisplayOrientation *natural_orientation, SDL_DisplayOrientation *current_orientation)
  245. {
  246. SDL_DisplayModeData *data;
  247. DEVMODE devmode;
  248. devmode.dmSize = sizeof(devmode);
  249. devmode.dmDriverExtra = 0;
  250. if (!EnumDisplaySettingsW(deviceName, index, &devmode)) {
  251. return false;
  252. }
  253. data = (SDL_DisplayModeData *)SDL_malloc(sizeof(*data));
  254. if (!data) {
  255. return false;
  256. }
  257. SDL_zerop(mode);
  258. mode->internal = data;
  259. data->DeviceMode = devmode;
  260. mode->format = SDL_PIXELFORMAT_UNKNOWN;
  261. mode->w = data->DeviceMode.dmPelsWidth;
  262. mode->h = data->DeviceMode.dmPelsHeight;
  263. WIN_GetRefreshRate(dxgi_output, &data->DeviceMode, &mode->refresh_rate_numerator, &mode->refresh_rate_denominator);
  264. // Fill in the mode information
  265. WIN_UpdateDisplayMode(_this, deviceName, index, mode);
  266. if (natural_orientation) {
  267. *natural_orientation = WIN_GetNaturalOrientation(&devmode);
  268. }
  269. if (current_orientation) {
  270. *current_orientation = WIN_GetDisplayOrientation(&devmode);
  271. }
  272. return true;
  273. }
  274. static char *WIN_GetDisplayNameVista(SDL_VideoData *videodata, const WCHAR *deviceName)
  275. {
  276. DISPLAYCONFIG_PATH_INFO *paths = NULL;
  277. DISPLAYCONFIG_MODE_INFO *modes = NULL;
  278. char *result = NULL;
  279. UINT32 pathCount = 0;
  280. UINT32 modeCount = 0;
  281. UINT32 i;
  282. LONG rc;
  283. if (!videodata->GetDisplayConfigBufferSizes || !videodata->QueryDisplayConfig || !videodata->DisplayConfigGetDeviceInfo) {
  284. return NULL;
  285. }
  286. do {
  287. rc = videodata->GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount);
  288. if (rc != ERROR_SUCCESS) {
  289. goto WIN_GetDisplayNameVista_failed;
  290. }
  291. SDL_free(paths);
  292. SDL_free(modes);
  293. paths = (DISPLAYCONFIG_PATH_INFO *)SDL_malloc(sizeof(DISPLAYCONFIG_PATH_INFO) * pathCount);
  294. modes = (DISPLAYCONFIG_MODE_INFO *)SDL_malloc(sizeof(DISPLAYCONFIG_MODE_INFO) * modeCount);
  295. if ((!paths) || (!modes)) {
  296. goto WIN_GetDisplayNameVista_failed;
  297. }
  298. rc = videodata->QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, paths, &modeCount, modes, 0);
  299. } while (rc == ERROR_INSUFFICIENT_BUFFER);
  300. if (rc == ERROR_SUCCESS) {
  301. for (i = 0; i < pathCount; i++) {
  302. DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName;
  303. DISPLAYCONFIG_TARGET_DEVICE_NAME targetName;
  304. SDL_zero(sourceName);
  305. sourceName.header.adapterId = paths[i].targetInfo.adapterId;
  306. sourceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
  307. sourceName.header.size = sizeof(sourceName);
  308. sourceName.header.id = paths[i].sourceInfo.id;
  309. rc = videodata->DisplayConfigGetDeviceInfo(&sourceName.header);
  310. if (rc != ERROR_SUCCESS) {
  311. break;
  312. } else if (SDL_wcscmp(deviceName, sourceName.viewGdiDeviceName) != 0) {
  313. continue;
  314. }
  315. SDL_zero(targetName);
  316. targetName.header.adapterId = paths[i].targetInfo.adapterId;
  317. targetName.header.id = paths[i].targetInfo.id;
  318. targetName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
  319. targetName.header.size = sizeof(targetName);
  320. rc = videodata->DisplayConfigGetDeviceInfo(&targetName.header);
  321. if (rc == ERROR_SUCCESS) {
  322. result = WIN_StringToUTF8W(targetName.monitorFriendlyDeviceName);
  323. /* if we got an empty string, treat it as failure so we'll fallback
  324. to getting the generic name. */
  325. if (result && (*result == '\0')) {
  326. SDL_free(result);
  327. result = NULL;
  328. }
  329. }
  330. break;
  331. }
  332. }
  333. SDL_free(paths);
  334. SDL_free(modes);
  335. return result;
  336. WIN_GetDisplayNameVista_failed:
  337. SDL_free(result);
  338. SDL_free(paths);
  339. SDL_free(modes);
  340. return NULL;
  341. }
  342. #ifdef HAVE_DXGI1_6_H
  343. static bool WIN_GetMonitorDESC1(HMONITOR hMonitor, DXGI_OUTPUT_DESC1 *desc)
  344. {
  345. typedef HRESULT (WINAPI * PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
  346. PFN_CREATE_DXGI_FACTORY CreateDXGIFactoryFunc = NULL;
  347. SDL_SharedObject *hDXGIMod = NULL;
  348. bool found = false;
  349. hDXGIMod = SDL_LoadObject("dxgi.dll");
  350. if (hDXGIMod) {
  351. CreateDXGIFactoryFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(hDXGIMod, "CreateDXGIFactory1");
  352. }
  353. if (CreateDXGIFactoryFunc) {
  354. static const GUID SDL_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } };
  355. static const GUID SDL_IID_IDXGIOutput6 = { 0x068346e8, 0xaaec, 0x4b84, { 0xad, 0xd7, 0x13, 0x7f, 0x51, 0x3f, 0x77, 0xa1 } };
  356. IDXGIFactory1 *dxgiFactory;
  357. if (SUCCEEDED(CreateDXGIFactoryFunc(&SDL_IID_IDXGIFactory1, (void **)&dxgiFactory))) {
  358. IDXGIAdapter1 *dxgiAdapter;
  359. UINT adapter = 0;
  360. while (!found && SUCCEEDED(IDXGIFactory1_EnumAdapters1(dxgiFactory, adapter, &dxgiAdapter))) {
  361. IDXGIOutput *dxgiOutput;
  362. UINT output = 0;
  363. while (!found && SUCCEEDED(IDXGIAdapter1_EnumOutputs(dxgiAdapter, output, &dxgiOutput))) {
  364. IDXGIOutput6 *dxgiOutput6;
  365. if (SUCCEEDED(IDXGIOutput_QueryInterface(dxgiOutput, &SDL_IID_IDXGIOutput6, (void **)&dxgiOutput6))) {
  366. if (SUCCEEDED(IDXGIOutput6_GetDesc1(dxgiOutput6, desc))) {
  367. if (desc->Monitor == hMonitor) {
  368. found = true;
  369. }
  370. }
  371. IDXGIOutput6_Release(dxgiOutput6);
  372. }
  373. IDXGIOutput_Release(dxgiOutput);
  374. ++output;
  375. }
  376. IDXGIAdapter1_Release(dxgiAdapter);
  377. ++adapter;
  378. }
  379. IDXGIFactory2_Release(dxgiFactory);
  380. }
  381. }
  382. if (hDXGIMod) {
  383. SDL_UnloadObject(hDXGIMod);
  384. }
  385. return found;
  386. }
  387. static bool WIN_GetMonitorPathInfo(SDL_VideoData *videodata, HMONITOR hMonitor, DISPLAYCONFIG_PATH_INFO *path_info)
  388. {
  389. LONG result;
  390. MONITORINFOEXW view_info;
  391. UINT32 i;
  392. UINT32 num_path_array_elements = 0;
  393. UINT32 num_mode_info_array_elements = 0;
  394. DISPLAYCONFIG_PATH_INFO *path_infos = NULL, *new_path_infos;
  395. DISPLAYCONFIG_MODE_INFO *mode_infos = NULL, *new_mode_infos;
  396. bool found = false;
  397. if (!videodata->GetDisplayConfigBufferSizes || !videodata->QueryDisplayConfig || !videodata->DisplayConfigGetDeviceInfo) {
  398. return false;
  399. }
  400. SDL_zero(view_info);
  401. view_info.cbSize = sizeof(view_info);
  402. if (!GetMonitorInfoW(hMonitor, (MONITORINFO *)&view_info)) {
  403. goto done;
  404. }
  405. do {
  406. if (videodata->GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &num_path_array_elements, &num_mode_info_array_elements) != ERROR_SUCCESS) {
  407. SDL_free(path_infos);
  408. SDL_free(mode_infos);
  409. return false;
  410. }
  411. new_path_infos = (DISPLAYCONFIG_PATH_INFO *)SDL_realloc(path_infos, num_path_array_elements * sizeof(*path_infos));
  412. if (!new_path_infos) {
  413. goto done;
  414. }
  415. path_infos = new_path_infos;
  416. new_mode_infos = (DISPLAYCONFIG_MODE_INFO *)SDL_realloc(mode_infos, num_mode_info_array_elements * sizeof(*mode_infos));
  417. if (!new_mode_infos) {
  418. goto done;
  419. }
  420. mode_infos = new_mode_infos;
  421. result = videodata->QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &num_path_array_elements, path_infos, &num_mode_info_array_elements, mode_infos, NULL);
  422. } while (result == ERROR_INSUFFICIENT_BUFFER);
  423. if (result == ERROR_SUCCESS) {
  424. for (i = 0; i < num_path_array_elements; ++i) {
  425. DISPLAYCONFIG_SOURCE_DEVICE_NAME device_name;
  426. SDL_zero(device_name);
  427. device_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
  428. device_name.header.size = sizeof(device_name);
  429. device_name.header.adapterId = path_infos[i].sourceInfo.adapterId;
  430. device_name.header.id = path_infos[i].sourceInfo.id;
  431. if (videodata->DisplayConfigGetDeviceInfo(&device_name.header) == ERROR_SUCCESS) {
  432. if (SDL_wcscmp(view_info.szDevice, device_name.viewGdiDeviceName) == 0) {
  433. SDL_copyp(path_info, &path_infos[i]);
  434. found = true;
  435. break;
  436. }
  437. }
  438. }
  439. }
  440. done:
  441. SDL_free(path_infos);
  442. SDL_free(mode_infos);
  443. return found;
  444. }
  445. static float WIN_GetSDRWhitePoint(SDL_VideoDevice *_this, HMONITOR hMonitor)
  446. {
  447. DISPLAYCONFIG_PATH_INFO path_info;
  448. SDL_VideoData *videodata = _this->internal;
  449. float SDR_white_level = 1.0f;
  450. if (WIN_GetMonitorPathInfo(videodata, hMonitor, &path_info)) {
  451. DISPLAYCONFIG_SDR_WHITE_LEVEL white_level;
  452. SDL_zero(white_level);
  453. white_level.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL;
  454. white_level.header.size = sizeof(white_level);
  455. white_level.header.adapterId = path_info.targetInfo.adapterId;
  456. white_level.header.id = path_info.targetInfo.id;
  457. // WIN_GetMonitorPathInfo() succeeded: DisplayConfigGetDeviceInfo is not NULL
  458. if (videodata->DisplayConfigGetDeviceInfo(&white_level.header) == ERROR_SUCCESS &&
  459. white_level.SDRWhiteLevel > 0) {
  460. SDR_white_level = (white_level.SDRWhiteLevel / 1000.0f);
  461. }
  462. }
  463. return SDR_white_level;
  464. }
  465. static void WIN_GetHDRProperties(SDL_VideoDevice *_this, HMONITOR hMonitor, SDL_HDROutputProperties *HDR)
  466. {
  467. DXGI_OUTPUT_DESC1 desc;
  468. SDL_zerop(HDR);
  469. if (WIN_GetMonitorDESC1(hMonitor, &desc)) {
  470. if (desc.ColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020) {
  471. HDR->SDR_white_level = WIN_GetSDRWhitePoint(_this, hMonitor);
  472. HDR->HDR_headroom = (desc.MaxLuminance / 80.0f) / HDR->SDR_white_level;
  473. }
  474. }
  475. }
  476. #endif // HAVE_DXGI1_6_H
  477. static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONITORINFOEXW *info, int *display_index)
  478. {
  479. int i, index = *display_index;
  480. SDL_VideoDisplay display;
  481. SDL_DisplayData *displaydata;
  482. void *dxgi_output = NULL;
  483. SDL_DisplayMode mode;
  484. SDL_DisplayOrientation natural_orientation;
  485. SDL_DisplayOrientation current_orientation;
  486. float content_scale = WIN_GetContentScale(_this, hMonitor);
  487. #ifdef DEBUG_MODES
  488. SDL_Log("Display: %s\n", WIN_StringToUTF8W(info->szDevice));
  489. #endif
  490. dxgi_output = WIN_GetDXGIOutput(_this, info->szDevice);
  491. bool found = WIN_GetDisplayMode(_this, dxgi_output, hMonitor, info->szDevice, ENUM_CURRENT_SETTINGS, &mode, &natural_orientation, &current_orientation);
  492. WIN_ReleaseDXGIOutput(dxgi_output);
  493. if (!found) {
  494. return;
  495. }
  496. // Prevent adding duplicate displays. Do this after we know the display is
  497. // ready to be added to allow any displays that we can't fully query to be
  498. // removed
  499. for (i = 0; i < _this->num_displays; ++i) {
  500. SDL_DisplayData *internal = _this->displays[i]->internal;
  501. if (SDL_wcscmp(internal->DeviceName, info->szDevice) == 0) {
  502. bool moved = (index != i);
  503. bool changed_bounds = false;
  504. if (internal->state != DisplayRemoved) {
  505. // We've already enumerated this display, don't move it
  506. return;
  507. }
  508. if (index >= _this->num_displays) {
  509. // This should never happen due to the check above, but just in case...
  510. return;
  511. }
  512. if (moved) {
  513. SDL_VideoDisplay *tmp;
  514. tmp = _this->displays[index];
  515. _this->displays[index] = _this->displays[i];
  516. _this->displays[i] = tmp;
  517. i = index;
  518. }
  519. internal->MonitorHandle = hMonitor;
  520. internal->state = DisplayUnchanged;
  521. if (!_this->setting_display_mode) {
  522. SDL_VideoDisplay *existing_display = _this->displays[i];
  523. SDL_Rect bounds;
  524. SDL_ResetFullscreenDisplayModes(existing_display);
  525. SDL_SetDesktopDisplayMode(existing_display, &mode);
  526. if (WIN_GetDisplayBounds(_this, existing_display, &bounds) &&
  527. SDL_memcmp(&internal->bounds, &bounds, sizeof(bounds)) != 0) {
  528. changed_bounds = true;
  529. SDL_copyp(&internal->bounds, &bounds);
  530. }
  531. if (moved || changed_bounds) {
  532. SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_MOVED, 0, 0);
  533. }
  534. SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_ORIENTATION, current_orientation, 0);
  535. SDL_SetDisplayContentScale(existing_display, content_scale);
  536. #ifdef HAVE_DXGI1_6_H
  537. SDL_HDROutputProperties HDR;
  538. WIN_GetHDRProperties(_this, hMonitor, &HDR);
  539. SDL_SetDisplayHDRProperties(existing_display, &HDR);
  540. #endif
  541. }
  542. goto done;
  543. }
  544. }
  545. displaydata = (SDL_DisplayData *)SDL_calloc(1, sizeof(*displaydata));
  546. if (!displaydata) {
  547. return;
  548. }
  549. SDL_memcpy(displaydata->DeviceName, info->szDevice, sizeof(displaydata->DeviceName));
  550. displaydata->MonitorHandle = hMonitor;
  551. displaydata->state = DisplayAdded;
  552. SDL_zero(display);
  553. display.name = WIN_GetDisplayNameVista(_this->internal, info->szDevice);
  554. if (!display.name) {
  555. DISPLAY_DEVICEW device;
  556. SDL_zero(device);
  557. device.cb = sizeof(device);
  558. if (EnumDisplayDevicesW(info->szDevice, 0, &device, 0)) {
  559. display.name = WIN_StringToUTF8W(device.DeviceString);
  560. }
  561. }
  562. display.desktop_mode = mode;
  563. display.natural_orientation = natural_orientation;
  564. display.current_orientation = current_orientation;
  565. display.content_scale = content_scale;
  566. display.device = _this;
  567. display.internal = displaydata;
  568. WIN_GetDisplayBounds(_this, &display, &displaydata->bounds);
  569. #ifdef HAVE_DXGI1_6_H
  570. WIN_GetHDRProperties(_this, hMonitor, &display.HDR);
  571. #endif
  572. SDL_AddVideoDisplay(&display, false);
  573. SDL_free(display.name);
  574. done:
  575. *display_index += 1;
  576. }
  577. typedef struct _WIN_AddDisplaysData
  578. {
  579. SDL_VideoDevice *video_device;
  580. int display_index;
  581. bool want_primary;
  582. } WIN_AddDisplaysData;
  583. static BOOL CALLBACK WIN_AddDisplaysCallback(HMONITOR hMonitor,
  584. HDC hdcMonitor,
  585. LPRECT lprcMonitor,
  586. LPARAM dwData)
  587. {
  588. WIN_AddDisplaysData *data = (WIN_AddDisplaysData *)dwData;
  589. MONITORINFOEXW info;
  590. SDL_zero(info);
  591. info.cbSize = sizeof(info);
  592. if (GetMonitorInfoW(hMonitor, (LPMONITORINFO)&info) != 0) {
  593. const bool is_primary = ((info.dwFlags & MONITORINFOF_PRIMARY) == MONITORINFOF_PRIMARY);
  594. if (is_primary == data->want_primary) {
  595. WIN_AddDisplay(data->video_device, hMonitor, &info, &data->display_index);
  596. }
  597. }
  598. // continue enumeration
  599. return TRUE;
  600. }
  601. static void WIN_AddDisplays(SDL_VideoDevice *_this)
  602. {
  603. WIN_AddDisplaysData callback_data;
  604. callback_data.video_device = _this;
  605. callback_data.display_index = 0;
  606. callback_data.want_primary = true;
  607. EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
  608. callback_data.want_primary = false;
  609. EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
  610. }
  611. bool WIN_InitModes(SDL_VideoDevice *_this)
  612. {
  613. WIN_AddDisplays(_this);
  614. if (_this->num_displays == 0) {
  615. return SDL_SetError("No displays available");
  616. }
  617. return true;
  618. }
  619. bool WIN_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  620. {
  621. const SDL_DisplayData *data = display->internal;
  622. MONITORINFO minfo;
  623. BOOL rc;
  624. SDL_zero(minfo);
  625. minfo.cbSize = sizeof(MONITORINFO);
  626. rc = GetMonitorInfo(data->MonitorHandle, &minfo);
  627. if (!rc) {
  628. return SDL_SetError("Couldn't find monitor data");
  629. }
  630. rect->x = minfo.rcMonitor.left;
  631. rect->y = minfo.rcMonitor.top;
  632. rect->w = minfo.rcMonitor.right - minfo.rcMonitor.left;
  633. rect->h = minfo.rcMonitor.bottom - minfo.rcMonitor.top;
  634. return true;
  635. }
  636. bool WIN_GetDisplayUsableBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  637. {
  638. const SDL_DisplayData *data = display->internal;
  639. MONITORINFO minfo;
  640. BOOL rc;
  641. SDL_zero(minfo);
  642. minfo.cbSize = sizeof(MONITORINFO);
  643. rc = GetMonitorInfo(data->MonitorHandle, &minfo);
  644. if (!rc) {
  645. return SDL_SetError("Couldn't find monitor data");
  646. }
  647. rect->x = minfo.rcWork.left;
  648. rect->y = minfo.rcWork.top;
  649. rect->w = minfo.rcWork.right - minfo.rcWork.left;
  650. rect->h = minfo.rcWork.bottom - minfo.rcWork.top;
  651. return true;
  652. }
  653. bool WIN_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)
  654. {
  655. SDL_DisplayData *data = display->internal;
  656. void *dxgi_output;
  657. DWORD i;
  658. SDL_DisplayMode mode;
  659. dxgi_output = WIN_GetDXGIOutput(_this, data->DeviceName);
  660. for (i = 0;; ++i) {
  661. if (!WIN_GetDisplayMode(_this, dxgi_output, data->MonitorHandle, data->DeviceName, i, &mode, NULL, NULL)) {
  662. break;
  663. }
  664. if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
  665. // We don't support palettized modes now
  666. SDL_free(mode.internal);
  667. continue;
  668. }
  669. if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
  670. if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
  671. SDL_free(mode.internal);
  672. }
  673. } else {
  674. SDL_free(mode.internal);
  675. }
  676. }
  677. WIN_ReleaseDXGIOutput(dxgi_output);
  678. return true;
  679. }
  680. #ifdef DEBUG_MODES
  681. static void WIN_LogMonitor(SDL_VideoDevice *_this, HMONITOR mon)
  682. {
  683. const SDL_VideoData *vid_data = (const SDL_VideoData *)_this->internal;
  684. MONITORINFOEX minfo;
  685. UINT xdpi = 0, ydpi = 0;
  686. char *name_utf8;
  687. if (vid_data->GetDpiForMonitor) {
  688. vid_data->GetDpiForMonitor(mon, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
  689. }
  690. SDL_zero(minfo);
  691. minfo.cbSize = sizeof(minfo);
  692. GetMonitorInfo(mon, (LPMONITORINFO)&minfo);
  693. name_utf8 = WIN_StringToUTF8(minfo.szDevice);
  694. SDL_Log("WIN_LogMonitor: monitor \"%s\": dpi: %d windows screen coordinates: %d, %d, %dx%d",
  695. name_utf8,
  696. xdpi,
  697. minfo.rcMonitor.left,
  698. minfo.rcMonitor.top,
  699. minfo.rcMonitor.right - minfo.rcMonitor.left,
  700. minfo.rcMonitor.bottom - minfo.rcMonitor.top);
  701. SDL_free(name_utf8);
  702. }
  703. #endif
  704. bool WIN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  705. {
  706. SDL_DisplayData *displaydata = display->internal;
  707. SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->internal;
  708. LONG status;
  709. #ifdef DEBUG_MODES
  710. SDL_Log("WIN_SetDisplayMode: monitor state before mode change:");
  711. WIN_LogMonitor(_this, displaydata->MonitorHandle);
  712. #endif
  713. /* High-DPI notes:
  714. - ChangeDisplaySettingsEx always takes pixels.
  715. - e.g. if the display is set to 2880x1800 with 200% scaling in Display Settings
  716. - calling ChangeDisplaySettingsEx with a dmPelsWidth/Height other than 2880x1800 will
  717. change the monitor DPI to 96. (100% scaling)
  718. - calling ChangeDisplaySettingsEx with a dmPelsWidth/Height of 2880x1800 (or a NULL DEVMODE*) will
  719. reset the monitor DPI to 192. (200% scaling)
  720. NOTE: these are temporary changes in DPI, not modifications to the Control Panel setting. */
  721. if (mode->internal == display->desktop_mode.internal) {
  722. #ifdef DEBUG_MODES
  723. SDL_Log("WIN_SetDisplayMode: resetting to original resolution");
  724. #endif
  725. status = ChangeDisplaySettingsExW(displaydata->DeviceName, NULL, NULL, CDS_FULLSCREEN, NULL);
  726. } else {
  727. #ifdef DEBUG_MODES
  728. SDL_Log("WIN_SetDisplayMode: changing to %dx%d pixels", data->DeviceMode.dmPelsWidth, data->DeviceMode.dmPelsHeight);
  729. #endif
  730. status = ChangeDisplaySettingsExW(displaydata->DeviceName, &data->DeviceMode, NULL, CDS_FULLSCREEN, NULL);
  731. }
  732. if (status != DISP_CHANGE_SUCCESSFUL) {
  733. const char *reason = "Unknown reason";
  734. switch (status) {
  735. case DISP_CHANGE_BADFLAGS:
  736. reason = "DISP_CHANGE_BADFLAGS";
  737. break;
  738. case DISP_CHANGE_BADMODE:
  739. reason = "DISP_CHANGE_BADMODE";
  740. break;
  741. case DISP_CHANGE_BADPARAM:
  742. reason = "DISP_CHANGE_BADPARAM";
  743. break;
  744. case DISP_CHANGE_FAILED:
  745. reason = "DISP_CHANGE_FAILED";
  746. break;
  747. }
  748. return SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
  749. }
  750. #ifdef DEBUG_MODES
  751. SDL_Log("WIN_SetDisplayMode: monitor state after mode change:");
  752. WIN_LogMonitor(_this, displaydata->MonitorHandle);
  753. #endif
  754. EnumDisplaySettingsW(displaydata->DeviceName, ENUM_CURRENT_SETTINGS, &data->DeviceMode);
  755. WIN_UpdateDisplayMode(_this, displaydata->DeviceName, ENUM_CURRENT_SETTINGS, mode);
  756. return true;
  757. }
  758. void WIN_RefreshDisplays(SDL_VideoDevice *_this)
  759. {
  760. int i;
  761. // Mark all displays as potentially invalid to detect
  762. // entries that have actually been removed
  763. for (i = 0; i < _this->num_displays; ++i) {
  764. SDL_DisplayData *internal = _this->displays[i]->internal;
  765. internal->state = DisplayRemoved;
  766. }
  767. // Enumerate displays to add any new ones and mark still
  768. // connected entries as valid
  769. WIN_AddDisplays(_this);
  770. // Delete any entries still marked as invalid, iterate
  771. // in reverse as each delete takes effect immediately
  772. for (i = _this->num_displays - 1; i >= 0; --i) {
  773. SDL_VideoDisplay *display = _this->displays[i];
  774. SDL_DisplayData *internal = display->internal;
  775. if (internal->state == DisplayRemoved) {
  776. SDL_DelVideoDisplay(display->id, true);
  777. }
  778. }
  779. // Send events for any newly added displays
  780. for (i = 0; i < _this->num_displays; ++i) {
  781. SDL_VideoDisplay *display = _this->displays[i];
  782. SDL_DisplayData *internal = display->internal;
  783. if (internal->state == DisplayAdded) {
  784. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ADDED, 0, 0);
  785. }
  786. }
  787. }
  788. void WIN_QuitModes(SDL_VideoDevice *_this)
  789. {
  790. // All fullscreen windows should have restored modes by now
  791. }
  792. #endif // SDL_VIDEO_DRIVER_WINDOWS