Skip to content

Commit

Permalink
Native DisplayMode: Expect to receive instruction when to capture
Browse files Browse the repository at this point in the history
The portable DisplayMode tells the native code when to capture displays
and when not to.
  • Loading branch information
skyjake committed Mar 20, 2012
1 parent ad5084a commit 977df28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
4 changes: 1 addition & 3 deletions doomsday/engine/mac/include/displaymode_macx.h
Expand Up @@ -36,9 +36,7 @@ void DisplayMode_Native_GetMode(int index, DisplayMode* mode);

void DisplayMode_Native_GetCurrentMode(DisplayMode* mode);

int DisplayMode_Native_CaptureScreen(int capture);

int DisplayMode_Native_Change(const DisplayMode* mode);
int DisplayMode_Native_Change(const DisplayMode* mode, boolean shouldCapture);

void DisplayMode_Native_Shutdown(void);

Expand Down
43 changes: 18 additions & 25 deletions doomsday/engine/mac/src/displaymode_macx.mm
Expand Up @@ -63,7 +63,6 @@ static DisplayMode modeFromDict(CFDictionaryRef dict)

static std::vector<DisplayMode> displayModes;
static std::vector<CFDictionaryRef> displayDicts;
static CFDictionaryRef originalDisplayDict;
static CFDictionaryRef currentDisplayDict;

void DisplayMode_Native_Init(void)
Expand All @@ -77,18 +76,10 @@ void DisplayMode_Native_Init(void)
displayModes.push_back(modeFromDict(dict));
displayDicts.push_back(dict);
}
originalDisplayDict = currentDisplayDict = (CFDictionaryRef) CGDisplayCurrentMode(kCGDirectMainDisplay);
currentDisplayDict = (CFDictionaryRef) CGDisplayCurrentMode(kCGDirectMainDisplay);
}

static void releaseDisplays()
{
if(CGDisplayIsCaptured(kCGDirectMainDisplay))
{
CGReleaseAllDisplays();
}
}

int DisplayMode_Native_CaptureScreen(int capture)
static bool captureDisplays(int capture)
{
if(capture && !CGDisplayIsCaptured(kCGDirectMainDisplay))
{
Expand All @@ -102,10 +93,14 @@ int DisplayMode_Native_CaptureScreen(int capture)
return true;
}

static void releaseDisplays()
{
captureDisplays(false);
}

void DisplayMode_Native_Shutdown(void)
{
displayModes.clear();

releaseDisplays();
}

Expand Down Expand Up @@ -140,14 +135,14 @@ static int findIndex(const DisplayMode* mode)
return -1; // Invalid mode.
}

int DisplayMode_Native_Change(const DisplayMode* mode)
int DisplayMode_Native_Change(const DisplayMode* mode, boolean shouldCapture)
{
const CGDisplayFadeInterval fadeTime = .5f;

assert(mode);
assert(findIndex(mode) >= 0); // mode must be an enumerated one

// Fade all displays to black.
// Fade all displays to black (blocks until faded).
CGDisplayFadeReservationToken token;
CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
CGDisplayFade(token, fadeTime, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, true /* wait */);
Expand All @@ -156,17 +151,20 @@ int DisplayMode_Native_Change(const DisplayMode* mode)
bool wasPreviouslyCaptured = CGDisplayIsCaptured(kCGDirectMainDisplay);
CGDisplayErr result = kCGErrorSuccess;

if(!DisplayMode_Native_CaptureScreen(true))
CFDictionaryRef newModeDict = displayDicts[findIndex(mode)];

// Capture displays if instructed to do so.
if(shouldCapture && !captureDisplays(true))
{
result = kCGErrorFailure;
}

if(result == kCGErrorSuccess)
if(result == kCGErrorSuccess && currentDisplayDict != newModeDict)
{
qDebug() << "Changing to native mode" << findIndex(mode);

// Try to change.
result = CGDisplaySwitchToMode(kCGDirectMainDisplay, displayDicts[findIndex(mode)]);
result = CGDisplaySwitchToMode(kCGDirectMainDisplay, newModeDict);
if(result != kCGErrorSuccess)
{
// Oh no!
Expand All @@ -179,19 +177,14 @@ int DisplayMode_Native_Change(const DisplayMode* mode)
}
}

/*
DisplayMode dm = modeFromDict(currentDisplayDict);
qDebug() << "Native current is now" << dm.width << dm.height;
*/

// Fade back to normal.
CGDisplayFade(token, 2*fadeTime, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, false);
CGReleaseDisplayFadeReservation(token);

if(currentDisplayDict == originalDisplayDict)
// Release display capture if instructed to do so.
if(!shouldCapture)
{
// Returned to the original mode, don't capture.
DisplayMode_Native_CaptureScreen(false);
captureDisplays(false);
}

return result == kCGErrorSuccess;
Expand Down

0 comments on commit 977df28

Please sign in to comment.