Taming Snow Leopard: CGDisplaySwitchToMode deprecation

Last one.. (i hope) here’s a snippet that will flick the screen in a best match resolution using only 10.6 API.

- (void) setDisplay:(CGDirectDisplayID) dspy toMode: (struct screenMode) screenMode {
	
    CGDisplayModeRef mode;
    CGDisplayErr err;
    
    CGDisplayModeRef originalMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);    
	if ( originalMode == NULL ) {
		LLLog(@"LLScreenResolution.setDisplay display with id %d is invalid", (int)dspy);
        return;
    }
    
	// look for a matching displ
	LLLog(@"LLScreenResolution.setDisplay setting display 0x%x: looking for %ld x %ld, %ld Bits Per Pixel",
		  (unsigned int)dspy,
		  screenMode.width,
		  screenMode.height,
		  screenMode.bitsPerPixel );

	mode = [self bestMatchForMode:screenMode];
	

	if ((screenMode.width == CGDisplayModeGetWidth(mode)) && 
		(screenMode.height == CGDisplayModeGetHeight(mode)) &&
		(screenMode.bitsPerPixel == [self displayBitsPerPixelForMode:mode])) {
		LLLog(@"LLScreenResolution.setDisplay found an exact match, switching modes" );
	} else {
		LLLog(@"LLScreenResolution.setDisplay found a mode, switching modes" );
	}
	
	err = CGDisplaySetDisplayMode(dspy, mode, NULL);
	if ( err != CGDisplayNoErr ) {
		LLLog(@"LLScreenResolution.setDisplay Oops!  Mode switch failed?!?? (%d)", err );
		
		// try to reset
		err = CGDisplaySetDisplayMode(dspy, originalMode, NULL);
		if ( err != CGDisplayNoErr )
			LLLog(@"LLScreenResolution.setDisplay Oops!  Mode restore failed?!?? (%d)", err );
	}
	
	// check result
	struct screenMode currentMode = [self screenModeOnPrimairyDisplay];
	LLLog(@"LLScreenResolution.setDisplay display has been set to %ld x %ld, %ld Bits Per Pixel",
		  currentMode.width,
		  currentMode.height,
		  currentMode.bitsPerPixel);	
}

Advertisements
Tagged with: ,
Posted in Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories
%d bloggers like this: