Created
February 13, 2010 13:40
-
-
Save atr000/303457 to your computer and use it in GitHub Desktop.
setDisplay.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
gcc -o SetDisplay main.c -framework Cocoa | |
main.c | |
This tool sets the display resolution. This tool exists so that we | |
can set the display to a setting when the display manager says it can't, | |
mainly, when a Mac starts up and the monitor is off, or if the Mac is on | |
a KVM. | |
Copyright (c) 2006 University of Utah Student Computing Labs. | |
All Rights Reserved. | |
Permission to use, copy, modify, and distribute this software and | |
its documentation for any purpose and without fee is hereby granted, | |
provided that the above copyright notice appears in all copies and | |
that both that copyright notice and this permission notice appear | |
in supporting documentation, and that the name of The University | |
of Utah not be used in advertising or publicity pertaining to | |
distribution of the software without specific, written prior | |
permission. This software is supplied as is without expressed or | |
implied warranties of any kind. | |
USAGE: | |
You can give it arguments or not. If you don't give it arguments, it will use: | |
1024 x 768, 32 bits per pixel, and 75 htz | |
If you give it 4 arguments, they should be: width, height, bits per pixel, and refresh rate. | |
Like so: | |
SetDisplay 1024 768 32 75 | |
WARNING: | |
In testing garbage values, I did get this to tool to change the display so that absolutely | |
nothing displayed. I don't remember what I did to get that. And I can't duplicate it anymore. | |
I could remotely control it, so I could fix it. But beware, if you can't remotely control the | |
machine and fix bad display settings, well, you might have to resort to drastic measures to fix | |
a mac with bad display settings (like boot off of a different disk and delete the display | |
preferences. | |
In other words, this tool will change the display settings regardless of what the display | |
manager says is possible. So be careful!!! | |
*/ | |
#include <ApplicationServices/ApplicationServices.h> | |
#include <CoreFoundation/CoreFoundation.h> | |
#include <IOKit/IOKitLib.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define kMaxDisplays 16 | |
struct myMode | |
{ | |
size_t width; | |
size_t height; | |
size_t bitsPerPixel; | |
CGRefreshRate refresh; | |
}; | |
struct myMode myMode; | |
int numberForKey( CFDictionaryRef desc, CFStringRef key ) | |
{ | |
CFNumberRef value; | |
int num = 0; | |
if ( (value = CFDictionaryGetValue(desc, key)) == NULL ) | |
return 0; | |
CFNumberGetValue(value, kCFNumberIntType, &num); | |
return num; | |
} | |
void printDispDesc( CFDictionaryRef desc ) | |
{ | |
char * msg; | |
if ( CFDictionaryGetValue(desc, kCGDisplayModeUsableForDesktopGUI) == kCFBooleanTrue ) | |
msg = "Supports Aqua GUI"; | |
else | |
msg = "Not for Aqua GUI"; | |
printf( "\t%d x %d,\t%d BPP,\t%d Hz\t(%s)\n", | |
numberForKey(desc, kCGDisplayWidth), | |
numberForKey(desc, kCGDisplayHeight), | |
numberForKey(desc, kCGDisplayBitsPerPixel), | |
numberForKey(desc, kCGDisplayRefreshRate), | |
msg ); | |
} | |
static void setdisplay( CGDirectDisplayID dspy) | |
{ | |
CFDictionaryRef mode; | |
CFDictionaryRef originalMode; | |
boolean_t exactMatch; | |
CGError err; | |
CGDisplayConfigRef configRef; | |
originalMode = CGDisplayCurrentMode( dspy ); | |
if ( originalMode == NULL ) | |
{ | |
printf( "Display 0x%x is invalid\n", (unsigned int)dspy); | |
return; | |
} | |
printf( "Display 0x%x\n", (unsigned int)dspy); | |
/* | |
printf( "Available modes:\n"); | |
CFArrayRef modes = CGDisplayAvailableModes(dspy); | |
CFIndex c = CFArrayGetCount(modes); | |
int i; | |
for (i=0; i<c; i++) { | |
mode = CFArrayGetValueAtIndex(modes, i); | |
if (numberForKey(mode, kCGDisplayWidth) == myMode.width && | |
numberForKey(mode, kCGDisplayHeight) == myMode.height && | |
numberForKey(mode, kCGDisplayRefreshRate) == myMode.refresh && | |
numberForKey(mode, kCGDisplayBitsPerPixel) == myMode.bitsPerPixel) | |
printDispDesc( mode ); | |
} | |
*/ | |
printf( "Looking for %ld x %ld, %ld Bits Per Pixel\n", myMode.width, myMode.height, myMode.bitsPerPixel ); | |
mode = CGDisplayBestModeForParametersAndRefreshRate(dspy, myMode.bitsPerPixel, myMode.width, myMode.height, myMode.refresh, &exactMatch); | |
printf ("The chosen mode:\n"); | |
printDispDesc( mode ); | |
if( !exactMatch ) | |
{ | |
printf( "Not an exact match\n" ); | |
} | |
CGBeginDisplayConfiguration(&configRef); | |
err = CGConfigureDisplayMode(configRef, dspy, mode); | |
CGCompleteDisplayConfiguration( configRef, kCGConfigurePermanently ); | |
if ( err != CGDisplayNoErr ) | |
{ | |
printf( "Oops! Mode switch failed?!?? (%d)\n", err ); | |
} | |
} | |
int | |
main(int argc, const char *argv[]) | |
{ | |
CGDirectDisplayID displays[kMaxDisplays]; | |
CGDisplayCount numDisplays; | |
int i; | |
CGDisplayErr err; | |
if ( argc == 5 ) { | |
myMode.width = atoi(argv[1]); | |
myMode.height = atoi(argv[2]); | |
myMode.bitsPerPixel = atoi(argv[3]); | |
myMode.refresh = atoi(argv[4]); | |
} else { | |
myMode.width = 1024; | |
myMode.height = 768; | |
myMode.bitsPerPixel = 32; | |
myMode.refresh = 75; | |
} | |
printf( "Width: %d Height: %d BitsPerPixel: %d Refresh rate: %d\n", (int)myMode.width, (int)myMode.height, (int)myMode.bitsPerPixel, (int)myMode.refresh ); | |
err = CGGetOnlineDisplayList(kMaxDisplays, | |
displays, | |
&numDisplays); | |
if ( err != CGDisplayNoErr ) | |
{ | |
printf("Cannot get displays (%d)\n", err); | |
exit( 1 ); | |
} | |
printf( "%d online display(s) found\n", (int)numDisplays ); | |
for ( i = 0; i < numDisplays; ++i ) | |
{ | |
setdisplay(displays[i]); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment