SDL_windowsmodes.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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 SDL_VIDEO_DRIVER_WINDOWS
  20. #include "SDL_windowsvideo.h"
  21. /* Windows CE compatibility */
  22. #ifndef CDS_FULLSCREEN
  23. #define CDS_FULLSCREEN 0
  24. #endif
  25. static SDL_bool
  26. WIN_GetDisplayMode(LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
  27. {
  28. SDL_DisplayModeData *data;
  29. DEVMODE devmode;
  30. HDC hdc;
  31. devmode.dmSize = sizeof(devmode);
  32. devmode.dmDriverExtra = 0;
  33. if (!EnumDisplaySettings(deviceName, index, &devmode)) {
  34. return SDL_FALSE;
  35. }
  36. data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
  37. if (!data) {
  38. return SDL_FALSE;
  39. }
  40. data->DeviceMode = devmode;
  41. data->DeviceMode.dmFields =
  42. (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY |
  43. DM_DISPLAYFLAGS);
  44. data->ScaleX = 1.0f;
  45. data->ScaleY = 1.0f;
  46. /* Fill in the mode information */
  47. mode->format = SDL_PIXELFORMAT_UNKNOWN;
  48. mode->w = devmode.dmPelsWidth;
  49. mode->h = devmode.dmPelsHeight;
  50. mode->refresh_rate = devmode.dmDisplayFrequency;
  51. mode->driverdata = data;
  52. if (index == ENUM_CURRENT_SETTINGS
  53. && (hdc = CreateDC(deviceName, NULL, NULL, NULL)) != NULL) {
  54. char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
  55. LPBITMAPINFO bmi;
  56. HBITMAP hbm;
  57. int logical_width = GetDeviceCaps( hdc, HORZRES );
  58. int logical_height = GetDeviceCaps( hdc, VERTRES );
  59. data->ScaleX = (float)logical_width / devmode.dmPelsWidth;
  60. data->ScaleY = (float)logical_height / devmode.dmPelsHeight;
  61. mode->w = logical_width;
  62. mode->h = logical_height;
  63. SDL_zero(bmi_data);
  64. bmi = (LPBITMAPINFO) bmi_data;
  65. bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  66. hbm = CreateCompatibleBitmap(hdc, 1, 1);
  67. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  68. GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
  69. DeleteObject(hbm);
  70. DeleteDC(hdc);
  71. if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
  72. switch (*(Uint32 *) bmi->bmiColors) {
  73. case 0x00FF0000:
  74. mode->format = SDL_PIXELFORMAT_RGB888;
  75. break;
  76. case 0x000000FF:
  77. mode->format = SDL_PIXELFORMAT_BGR888;
  78. break;
  79. case 0xF800:
  80. mode->format = SDL_PIXELFORMAT_RGB565;
  81. break;
  82. case 0x7C00:
  83. mode->format = SDL_PIXELFORMAT_RGB555;
  84. break;
  85. }
  86. } else if (bmi->bmiHeader.biBitCount == 8) {
  87. mode->format = SDL_PIXELFORMAT_INDEX8;
  88. } else if (bmi->bmiHeader.biBitCount == 4) {
  89. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  90. }
  91. } else {
  92. /* FIXME: Can we tell what this will be? */
  93. if ((devmode.dmFields & DM_BITSPERPEL) == DM_BITSPERPEL) {
  94. switch (devmode.dmBitsPerPel) {
  95. case 32:
  96. mode->format = SDL_PIXELFORMAT_RGB888;
  97. break;
  98. case 24:
  99. mode->format = SDL_PIXELFORMAT_RGB24;
  100. break;
  101. case 16:
  102. mode->format = SDL_PIXELFORMAT_RGB565;
  103. break;
  104. case 15:
  105. mode->format = SDL_PIXELFORMAT_RGB555;
  106. break;
  107. case 8:
  108. mode->format = SDL_PIXELFORMAT_INDEX8;
  109. break;
  110. case 4:
  111. mode->format = SDL_PIXELFORMAT_INDEX4LSB;
  112. break;
  113. }
  114. }
  115. }
  116. return SDL_TRUE;
  117. }
  118. static SDL_bool
  119. WIN_AddDisplay(LPTSTR DeviceName)
  120. {
  121. SDL_VideoDisplay display;
  122. SDL_DisplayData *displaydata;
  123. SDL_DisplayMode mode;
  124. DISPLAY_DEVICE device;
  125. #ifdef DEBUG_MODES
  126. printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
  127. #endif
  128. if (!WIN_GetDisplayMode(DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
  129. return SDL_FALSE;
  130. }
  131. displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
  132. if (!displaydata) {
  133. return SDL_FALSE;
  134. }
  135. SDL_memcpy(displaydata->DeviceName, DeviceName,
  136. sizeof(displaydata->DeviceName));
  137. SDL_zero(display);
  138. device.cb = sizeof(device);
  139. if (EnumDisplayDevices(DeviceName, 0, &device, 0)) {
  140. display.name = WIN_StringToUTF8(device.DeviceString);
  141. }
  142. display.desktop_mode = mode;
  143. display.current_mode = mode;
  144. display.driverdata = displaydata;
  145. SDL_AddVideoDisplay(&display);
  146. SDL_free(display.name);
  147. return SDL_TRUE;
  148. }
  149. int
  150. WIN_InitModes(_THIS)
  151. {
  152. int pass;
  153. DWORD i, j, count;
  154. DISPLAY_DEVICE device;
  155. device.cb = sizeof(device);
  156. /* Get the primary display in the first pass */
  157. for (pass = 0; pass < 2; ++pass) {
  158. for (i = 0; ; ++i) {
  159. TCHAR DeviceName[32];
  160. if (!EnumDisplayDevices(NULL, i, &device, 0)) {
  161. break;
  162. }
  163. if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
  164. continue;
  165. }
  166. if (pass == 0) {
  167. if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
  168. continue;
  169. }
  170. } else {
  171. if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
  172. continue;
  173. }
  174. }
  175. SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
  176. #ifdef DEBUG_MODES
  177. printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
  178. #endif
  179. count = 0;
  180. for (j = 0; ; ++j) {
  181. if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
  182. break;
  183. }
  184. if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
  185. continue;
  186. }
  187. if (pass == 0) {
  188. if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
  189. continue;
  190. }
  191. } else {
  192. if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
  193. continue;
  194. }
  195. }
  196. count += WIN_AddDisplay(device.DeviceName);
  197. }
  198. if (count == 0) {
  199. WIN_AddDisplay(DeviceName);
  200. }
  201. }
  202. }
  203. if (_this->num_displays == 0) {
  204. return SDL_SetError("No displays available");
  205. }
  206. return 0;
  207. }
  208. int
  209. WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
  210. {
  211. SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->current_mode.driverdata;
  212. rect->x = (int)SDL_ceil(data->DeviceMode.dmPosition.x * data->ScaleX);
  213. rect->y = (int)SDL_ceil(data->DeviceMode.dmPosition.y * data->ScaleY);
  214. rect->w = (int)SDL_ceil(data->DeviceMode.dmPelsWidth * data->ScaleX);
  215. rect->h = (int)SDL_ceil(data->DeviceMode.dmPelsHeight * data->ScaleY);
  216. return 0;
  217. }
  218. void
  219. WIN_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
  220. {
  221. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  222. DWORD i;
  223. SDL_DisplayMode mode;
  224. for (i = 0;; ++i) {
  225. if (!WIN_GetDisplayMode(data->DeviceName, i, &mode)) {
  226. break;
  227. }
  228. if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
  229. /* We don't support palettized modes now */
  230. SDL_free(mode.driverdata);
  231. continue;
  232. }
  233. if (mode.format != SDL_PIXELFORMAT_UNKNOWN) {
  234. if (!SDL_AddDisplayMode(display, &mode)) {
  235. SDL_free(mode.driverdata);
  236. }
  237. } else {
  238. SDL_free(mode.driverdata);
  239. }
  240. }
  241. }
  242. int
  243. WIN_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
  244. {
  245. SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
  246. SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
  247. LONG status;
  248. if (mode->driverdata == display->desktop_mode.driverdata) {
  249. status = ChangeDisplaySettingsEx(displaydata->DeviceName, NULL, NULL, 0, NULL);
  250. } else {
  251. status = ChangeDisplaySettingsEx(displaydata->DeviceName, &data->DeviceMode, NULL, CDS_FULLSCREEN, NULL);
  252. }
  253. if (status != DISP_CHANGE_SUCCESSFUL) {
  254. const char *reason = "Unknown reason";
  255. switch (status) {
  256. case DISP_CHANGE_BADFLAGS:
  257. reason = "DISP_CHANGE_BADFLAGS";
  258. break;
  259. case DISP_CHANGE_BADMODE:
  260. reason = "DISP_CHANGE_BADMODE";
  261. break;
  262. case DISP_CHANGE_BADPARAM:
  263. reason = "DISP_CHANGE_BADPARAM";
  264. break;
  265. case DISP_CHANGE_FAILED:
  266. reason = "DISP_CHANGE_FAILED";
  267. break;
  268. }
  269. return SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
  270. }
  271. EnumDisplaySettings(displaydata->DeviceName, ENUM_CURRENT_SETTINGS, &data->DeviceMode);
  272. return 0;
  273. }
  274. void
  275. WIN_QuitModes(_THIS)
  276. {
  277. /* All fullscreen windows should have restored modes by now */
  278. }
  279. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  280. /* vi: set ts=4 sw=4 expandtab: */