SDL_cocoamodes.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #ifdef SDL_VIDEO_DRIVER_COCOA
  20. #include "SDL_cocoavideo.h"
  21. /* We need this for IODisplayCreateInfoDictionary and kIODisplayOnlyPreferredName */
  22. #include <IOKit/graphics/IOGraphicsLib.h>
  23. /* We need this for CVDisplayLinkGetNominalOutputVideoRefreshPeriod */
  24. #include <CoreVideo/CVBase.h>
  25. #include <CoreVideo/CVDisplayLink.h>
  26. /* This gets us MAC_OS_X_VERSION_MIN_REQUIRED... */
  27. #include <AvailabilityMacros.h>
  28. #ifndef MAC_OS_X_VERSION_10_13
  29. #define NSAppKitVersionNumber10_12 1504
  30. #endif
  31. #if (IOGRAPHICSTYPES_REV < 40)
  32. #define kDisplayModeNativeFlag 0x02000000
  33. #endif
  34. static int CG_SetError(const char *prefix, CGDisplayErr result)
  35. {
  36. const char *error;
  37. switch (result) {
  38. case kCGErrorFailure:
  39. error = "kCGErrorFailure";
  40. break;
  41. case kCGErrorIllegalArgument:
  42. error = "kCGErrorIllegalArgument";
  43. break;
  44. case kCGErrorInvalidConnection:
  45. error = "kCGErrorInvalidConnection";
  46. break;
  47. case kCGErrorInvalidContext:
  48. error = "kCGErrorInvalidContext";
  49. break;
  50. case kCGErrorCannotComplete:
  51. error = "kCGErrorCannotComplete";
  52. break;
  53. case kCGErrorNotImplemented:
  54. error = "kCGErrorNotImplemented";
  55. break;
  56. case kCGErrorRangeCheck:
  57. error = "kCGErrorRangeCheck";
  58. break;
  59. case kCGErrorTypeCheck:
  60. error = "kCGErrorTypeCheck";
  61. break;
  62. case kCGErrorInvalidOperation:
  63. error = "kCGErrorInvalidOperation";
  64. break;
  65. case kCGErrorNoneAvailable:
  66. error = "kCGErrorNoneAvailable";
  67. break;
  68. default:
  69. error = "Unknown Error";
  70. break;
  71. }
  72. return SDL_SetError("%s: %s", prefix, error);
  73. }
  74. static NSScreen *GetNSScreenForDisplayID(CGDirectDisplayID displayID)
  75. {
  76. NSArray *screens = [NSScreen screens];
  77. /* !!! FIXME: maybe track the NSScreen in SDL_DisplayData? */
  78. for (NSScreen *screen in screens) {
  79. const CGDirectDisplayID thisDisplay = (CGDirectDisplayID)[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue];
  80. if (thisDisplay == displayID) {
  81. return screen;
  82. }
  83. }
  84. return nil;
  85. }
  86. static float GetDisplayModeRefreshRate(CGDisplayModeRef vidmode, CVDisplayLinkRef link)
  87. {
  88. double refreshRate = CGDisplayModeGetRefreshRate(vidmode);
  89. /* CGDisplayModeGetRefreshRate can return 0 (eg for built-in displays). */
  90. if (refreshRate == 0 && link != NULL) {
  91. CVTime time = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(link);
  92. if ((time.flags & kCVTimeIsIndefinite) == 0 && time.timeValue != 0) {
  93. refreshRate = (double)time.timeScale / time.timeValue;
  94. }
  95. }
  96. return (int)(refreshRate * 100) / 100.0f;
  97. }
  98. static SDL_bool HasValidDisplayModeFlags(CGDisplayModeRef vidmode)
  99. {
  100. uint32_t ioflags = CGDisplayModeGetIOFlags(vidmode);
  101. /* Filter out modes which have flags that we don't want. */
  102. if (ioflags & (kDisplayModeNeverShowFlag | kDisplayModeNotGraphicsQualityFlag)) {
  103. return SDL_FALSE;
  104. }
  105. /* Filter out modes which don't have flags that we want. */
  106. if (!(ioflags & kDisplayModeValidFlag) || !(ioflags & kDisplayModeSafeFlag)) {
  107. return SDL_FALSE;
  108. }
  109. return SDL_TRUE;
  110. }
  111. static Uint32 GetDisplayModePixelFormat(CGDisplayModeRef vidmode)
  112. {
  113. /* This API is deprecated in 10.11 with no good replacement (as of 10.15). */
  114. CFStringRef fmt = CGDisplayModeCopyPixelEncoding(vidmode);
  115. Uint32 pixelformat = SDL_PIXELFORMAT_UNKNOWN;
  116. if (CFStringCompare(fmt, CFSTR(IO32BitDirectPixels),
  117. kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  118. pixelformat = SDL_PIXELFORMAT_ARGB8888;
  119. } else if (CFStringCompare(fmt, CFSTR(IO16BitDirectPixels),
  120. kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  121. pixelformat = SDL_PIXELFORMAT_ARGB1555;
  122. } else if (CFStringCompare(fmt, CFSTR(kIO30BitDirectPixels),
  123. kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  124. pixelformat = SDL_PIXELFORMAT_ARGB2101010;
  125. } else {
  126. /* ignore 8-bit and such for now. */
  127. }
  128. CFRelease(fmt);
  129. return pixelformat;
  130. }
  131. static SDL_bool GetDisplayMode(SDL_VideoDevice *_this, CGDisplayModeRef vidmode, SDL_bool vidmodeCurrent, CFArrayRef modelist, CVDisplayLinkRef link, SDL_DisplayMode *mode)
  132. {
  133. SDL_DisplayModeData *data;
  134. bool usableForGUI = CGDisplayModeIsUsableForDesktopGUI(vidmode);
  135. size_t width = CGDisplayModeGetWidth(vidmode);
  136. size_t height = CGDisplayModeGetHeight(vidmode);
  137. size_t pixelW = width;
  138. size_t pixelH = height;
  139. uint32_t ioflags = CGDisplayModeGetIOFlags(vidmode);
  140. float refreshrate = GetDisplayModeRefreshRate(vidmode, link);
  141. Uint32 format = GetDisplayModePixelFormat(vidmode);
  142. bool interlaced = (ioflags & kDisplayModeInterlacedFlag) != 0;
  143. CFMutableArrayRef modes;
  144. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  145. return SDL_FALSE;
  146. }
  147. /* Don't fail the current mode based on flags because this could prevent Cocoa_InitModes from
  148. * succeeding if the current mode lacks certain flags (esp kDisplayModeSafeFlag). */
  149. if (!vidmodeCurrent && !HasValidDisplayModeFlags(vidmode)) {
  150. return SDL_FALSE;
  151. }
  152. modes = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  153. CFArrayAppendValue(modes, vidmode);
  154. /* If a list of possible display modes is passed in, use it to filter out
  155. * modes that have duplicate sizes. We don't just rely on SDL's higher level
  156. * duplicate filtering because this code can choose what properties are
  157. * preferred, and it can add CGDisplayModes to the DisplayModeData's list of
  158. * modes to try (see comment below for why that's necessary). */
  159. pixelW = CGDisplayModeGetPixelWidth(vidmode);
  160. pixelH = CGDisplayModeGetPixelHeight(vidmode);
  161. if (modelist != NULL) {
  162. CFIndex modescount = CFArrayGetCount(modelist);
  163. int i;
  164. for (i = 0; i < modescount; i++) {
  165. size_t otherW, otherH, otherpixelW, otherpixelH;
  166. float otherrefresh;
  167. Uint32 otherformat;
  168. bool otherGUI;
  169. CGDisplayModeRef othermode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modelist, i);
  170. uint32_t otherioflags = CGDisplayModeGetIOFlags(othermode);
  171. if (CFEqual(vidmode, othermode)) {
  172. continue;
  173. }
  174. if (!HasValidDisplayModeFlags(othermode)) {
  175. continue;
  176. }
  177. otherW = CGDisplayModeGetWidth(othermode);
  178. otherH = CGDisplayModeGetHeight(othermode);
  179. otherpixelW = CGDisplayModeGetPixelWidth(othermode);
  180. otherpixelH = CGDisplayModeGetPixelHeight(othermode);
  181. otherrefresh = GetDisplayModeRefreshRate(othermode, link);
  182. otherformat = GetDisplayModePixelFormat(othermode);
  183. otherGUI = CGDisplayModeIsUsableForDesktopGUI(othermode);
  184. /* Ignore this mode if it's interlaced and there's a non-interlaced
  185. * mode in the list with the same properties.
  186. */
  187. if (interlaced && ((otherioflags & kDisplayModeInterlacedFlag) == 0) && width == otherW && height == otherH && pixelW == otherpixelW && pixelH == otherpixelH && refreshrate == otherrefresh && format == otherformat && usableForGUI == otherGUI) {
  188. CFRelease(modes);
  189. return SDL_FALSE;
  190. }
  191. /* Ignore this mode if it's not usable for desktop UI and its
  192. * properties are equal to another GUI-capable mode in the list.
  193. */
  194. if (width == otherW && height == otherH && pixelW == otherpixelW && pixelH == otherpixelH && !usableForGUI && otherGUI && refreshrate == otherrefresh && format == otherformat) {
  195. CFRelease(modes);
  196. return SDL_FALSE;
  197. }
  198. /* If multiple modes have the exact same properties, they'll all
  199. * go in the list of modes to try when SetDisplayMode is called.
  200. * This is needed because kCGDisplayShowDuplicateLowResolutionModes
  201. * (which is used to expose highdpi display modes) can make the
  202. * list of modes contain duplicates (according to their properties
  203. * obtained via public APIs) which don't work with SetDisplayMode.
  204. * Those duplicate non-functional modes *do* have different pixel
  205. * formats according to their internal data structure viewed with
  206. * NSLog, but currently no public API can detect that.
  207. * https://bugzilla.libsdl.org/show_bug.cgi?id=4822
  208. *
  209. * As of macOS 10.15.0, those duplicates have the exact same
  210. * properties via public APIs in every way (even their IO flags and
  211. * CGDisplayModeGetIODisplayModeID is the same), so we could test
  212. * those for equality here too, but I'm intentionally not doing that
  213. * in case there are duplicate modes with different IO flags or IO
  214. * display mode IDs in the future. In that case I think it's better
  215. * to try them all in SetDisplayMode than to risk one of them being
  216. * correct but it being filtered out by SDL_AddFullscreenDisplayMode
  217. * as being a duplicate.
  218. */
  219. if (width == otherW && height == otherH && pixelW == otherpixelW && pixelH == otherpixelH && usableForGUI == otherGUI && refreshrate == otherrefresh && format == otherformat) {
  220. CFArrayAppendValue(modes, othermode);
  221. }
  222. }
  223. }
  224. SDL_zerop(mode);
  225. data = (SDL_DisplayModeData *)SDL_malloc(sizeof(*data));
  226. if (!data) {
  227. CFRelease(modes);
  228. return SDL_FALSE;
  229. }
  230. data->modes = modes;
  231. mode->format = format;
  232. mode->w = (int)width;
  233. mode->h = (int)height;
  234. mode->pixel_density = (float)pixelW / width;
  235. mode->refresh_rate = refreshrate;
  236. mode->driverdata = data;
  237. return SDL_TRUE;
  238. }
  239. static char *Cocoa_GetDisplayName(CGDirectDisplayID displayID)
  240. {
  241. /* This API is deprecated in 10.9 with no good replacement (as of 10.15). */
  242. io_service_t servicePort = CGDisplayIOServicePort(displayID);
  243. CFDictionaryRef deviceInfo = IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName);
  244. NSDictionary *localizedNames = [(__bridge NSDictionary *)deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
  245. char *displayName = NULL;
  246. if ([localizedNames count] > 0) {
  247. displayName = SDL_strdup([[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] UTF8String]);
  248. }
  249. CFRelease(deviceInfo);
  250. return displayName;
  251. }
  252. static void Cocoa_GetHDRProperties(CGDirectDisplayID displayID, SDL_HDROutputProperties *HDR)
  253. {
  254. HDR->SDR_white_level = 1.0f;
  255. HDR->HDR_headroom = 1.0f;
  256. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101500 /* Added in the 10.15 SDK */
  257. if (@available(macOS 10.15, *)) {
  258. NSScreen *screen = GetNSScreenForDisplayID(displayID);
  259. if (screen) {
  260. if (screen.maximumExtendedDynamicRangeColorComponentValue > 1.0f) {
  261. HDR->HDR_headroom = screen.maximumExtendedDynamicRangeColorComponentValue;
  262. } else {
  263. HDR->HDR_headroom = screen.maximumPotentialExtendedDynamicRangeColorComponentValue;
  264. }
  265. }
  266. }
  267. #endif
  268. }
  269. void Cocoa_InitModes(SDL_VideoDevice *_this)
  270. {
  271. @autoreleasepool {
  272. CGDisplayErr result;
  273. CGDirectDisplayID *displays;
  274. CGDisplayCount numDisplays;
  275. SDL_bool isstack;
  276. int pass, i;
  277. result = CGGetOnlineDisplayList(0, NULL, &numDisplays);
  278. if (result != kCGErrorSuccess) {
  279. CG_SetError("CGGetOnlineDisplayList()", result);
  280. return;
  281. }
  282. displays = SDL_small_alloc(CGDirectDisplayID, numDisplays, &isstack);
  283. result = CGGetOnlineDisplayList(numDisplays, displays, &numDisplays);
  284. if (result != kCGErrorSuccess) {
  285. CG_SetError("CGGetOnlineDisplayList()", result);
  286. SDL_small_free(displays, isstack);
  287. return;
  288. }
  289. /* Pick up the primary display in the first pass, then get the rest */
  290. for (pass = 0; pass < 2; ++pass) {
  291. for (i = 0; i < numDisplays; ++i) {
  292. SDL_VideoDisplay display;
  293. SDL_DisplayData *displaydata;
  294. SDL_DisplayMode mode;
  295. CGDisplayModeRef moderef = NULL;
  296. CVDisplayLinkRef link = NULL;
  297. if (pass == 0) {
  298. if (!CGDisplayIsMain(displays[i])) {
  299. continue;
  300. }
  301. } else {
  302. if (CGDisplayIsMain(displays[i])) {
  303. continue;
  304. }
  305. }
  306. if (CGDisplayMirrorsDisplay(displays[i]) != kCGNullDirectDisplay) {
  307. continue;
  308. }
  309. moderef = CGDisplayCopyDisplayMode(displays[i]);
  310. if (!moderef) {
  311. continue;
  312. }
  313. displaydata = (SDL_DisplayData *)SDL_malloc(sizeof(*displaydata));
  314. if (!displaydata) {
  315. CGDisplayModeRelease(moderef);
  316. continue;
  317. }
  318. displaydata->display = displays[i];
  319. CVDisplayLinkCreateWithCGDisplay(displays[i], &link);
  320. SDL_zero(display);
  321. /* this returns a strdup'ed string */
  322. display.name = Cocoa_GetDisplayName(displays[i]);
  323. if (!GetDisplayMode(_this, moderef, SDL_TRUE, NULL, link, &mode)) {
  324. CVDisplayLinkRelease(link);
  325. CGDisplayModeRelease(moderef);
  326. SDL_free(display.name);
  327. SDL_free(displaydata);
  328. continue;
  329. }
  330. CVDisplayLinkRelease(link);
  331. CGDisplayModeRelease(moderef);
  332. Cocoa_GetHDRProperties(displaydata->display, &display.HDR);
  333. display.desktop_mode = mode;
  334. display.driverdata = displaydata;
  335. SDL_AddVideoDisplay(&display, SDL_FALSE);
  336. SDL_free(display.name);
  337. }
  338. }
  339. SDL_small_free(displays, isstack);
  340. }
  341. }
  342. void Cocoa_UpdateDisplays(SDL_VideoDevice *_this)
  343. {
  344. SDL_HDROutputProperties HDR;
  345. int i;
  346. for (i = 0; i < _this->num_displays; ++i) {
  347. SDL_VideoDisplay *display = _this->displays[i];
  348. SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
  349. Cocoa_GetHDRProperties(displaydata->display, &HDR);
  350. SDL_SetDisplayHDRProperties(display, &HDR);
  351. }
  352. }
  353. int Cocoa_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  354. {
  355. SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
  356. CGRect cgrect;
  357. cgrect = CGDisplayBounds(displaydata->display);
  358. rect->x = (int)cgrect.origin.x;
  359. rect->y = (int)cgrect.origin.y;
  360. rect->w = (int)cgrect.size.width;
  361. rect->h = (int)cgrect.size.height;
  362. return 0;
  363. }
  364. int Cocoa_GetDisplayUsableBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  365. {
  366. SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
  367. NSScreen *screen = GetNSScreenForDisplayID(displaydata->display);
  368. if (screen == nil) {
  369. return SDL_SetError("Couldn't get NSScreen for display");
  370. }
  371. {
  372. const NSRect frame = [screen visibleFrame];
  373. rect->x = (int)frame.origin.x;
  374. rect->y = (int)(CGDisplayPixelsHigh(kCGDirectMainDisplay) - frame.origin.y - frame.size.height);
  375. rect->w = (int)frame.size.width;
  376. rect->h = (int)frame.size.height;
  377. }
  378. return 0;
  379. }
  380. int Cocoa_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)
  381. {
  382. SDL_DisplayData *data = (SDL_DisplayData *)display->driverdata;
  383. CVDisplayLinkRef link = NULL;
  384. CFArrayRef modes;
  385. CFDictionaryRef dict = NULL;
  386. const CFStringRef dictkeys[] = { kCGDisplayShowDuplicateLowResolutionModes };
  387. const CFBooleanRef dictvalues[] = { kCFBooleanTrue };
  388. CVDisplayLinkCreateWithCGDisplay(data->display, &link);
  389. /* By default, CGDisplayCopyAllDisplayModes will only get a subset of the
  390. * system's available modes. For example on a 15" 2016 MBP, users can
  391. * choose 1920x1080@2x in System Preferences but it won't show up here,
  392. * unless we specify the option below.
  393. * The display modes returned by CGDisplayCopyAllDisplayModes are also not
  394. * high dpi-capable unless this option is set.
  395. * macOS 10.15 also seems to have a bug where entering, exiting, and
  396. * re-entering exclusive fullscreen with a low dpi display mode can cause
  397. * the content of the screen to move up, which this setting avoids:
  398. * https://bugzilla.libsdl.org/show_bug.cgi?id=4822
  399. */
  400. dict = CFDictionaryCreate(NULL,
  401. (const void **)dictkeys,
  402. (const void **)dictvalues,
  403. 1,
  404. &kCFCopyStringDictionaryKeyCallBacks,
  405. &kCFTypeDictionaryValueCallBacks);
  406. modes = CGDisplayCopyAllDisplayModes(data->display, dict);
  407. if (dict) {
  408. CFRelease(dict);
  409. }
  410. if (modes) {
  411. CFIndex i;
  412. const CFIndex count = CFArrayGetCount(modes);
  413. for (i = 0; i < count; i++) {
  414. CGDisplayModeRef moderef = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i);
  415. SDL_DisplayMode mode;
  416. if (GetDisplayMode(_this, moderef, SDL_FALSE, modes, link, &mode)) {
  417. if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
  418. CFRelease(((SDL_DisplayModeData *)mode.driverdata)->modes);
  419. SDL_free(mode.driverdata);
  420. }
  421. }
  422. }
  423. CFRelease(modes);
  424. }
  425. CVDisplayLinkRelease(link);
  426. return 0;
  427. }
  428. static CGError SetDisplayModeForDisplay(CGDirectDisplayID display, SDL_DisplayModeData *data)
  429. {
  430. /* SDL_DisplayModeData can contain multiple CGDisplayModes to try (with
  431. * identical properties), some of which might not work. See GetDisplayMode.
  432. */
  433. CGError result = kCGErrorFailure;
  434. for (CFIndex i = 0; i < CFArrayGetCount(data->modes); i++) {
  435. CGDisplayModeRef moderef = (CGDisplayModeRef)CFArrayGetValueAtIndex(data->modes, i);
  436. result = CGDisplaySetDisplayMode(display, moderef, NULL);
  437. if (result == kCGErrorSuccess) {
  438. /* If this mode works, try it first next time. */
  439. CFArrayExchangeValuesAtIndices(data->modes, i, 0);
  440. break;
  441. }
  442. }
  443. return result;
  444. }
  445. int Cocoa_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  446. {
  447. SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
  448. SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
  449. CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
  450. CGError result = kCGErrorSuccess;
  451. b_inModeTransition = SDL_TRUE;
  452. /* Fade to black to hide resolution-switching flicker */
  453. if (CGAcquireDisplayFadeReservation(5, &fade_token) == kCGErrorSuccess) {
  454. CGDisplayFade(fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
  455. }
  456. if (data == display->desktop_mode.driverdata) {
  457. /* Restoring desktop mode */
  458. SetDisplayModeForDisplay(displaydata->display, data);
  459. } else {
  460. /* Do the physical switch */
  461. result = SetDisplayModeForDisplay(displaydata->display, data);
  462. }
  463. /* Fade in again (asynchronously) */
  464. if (fade_token != kCGDisplayFadeReservationInvalidToken) {
  465. CGDisplayFade(fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
  466. CGReleaseDisplayFadeReservation(fade_token);
  467. }
  468. b_inModeTransition = SDL_FALSE;
  469. if (result != kCGErrorSuccess) {
  470. CG_SetError("CGDisplaySwitchToMode()", result);
  471. return -1;
  472. }
  473. return 0;
  474. }
  475. void Cocoa_QuitModes(SDL_VideoDevice *_this)
  476. {
  477. int i, j;
  478. for (i = 0; i < _this->num_displays; ++i) {
  479. SDL_VideoDisplay *display = _this->displays[i];
  480. SDL_DisplayModeData *mode;
  481. if (display->current_mode->driverdata != display->desktop_mode.driverdata) {
  482. Cocoa_SetDisplayMode(_this, display, &display->desktop_mode);
  483. }
  484. mode = (SDL_DisplayModeData *)display->desktop_mode.driverdata;
  485. CFRelease(mode->modes);
  486. for (j = 0; j < display->num_fullscreen_modes; j++) {
  487. mode = (SDL_DisplayModeData *)display->fullscreen_modes[j].driverdata;
  488. CFRelease(mode->modes);
  489. }
  490. }
  491. }
  492. #endif /* SDL_VIDEO_DRIVER_COCOA */