/*
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 );*/
                        
        printf( "%dx%d %d %d\n",
                        numberForKey(desc, kCGDisplayWidth),
                        numberForKey(desc, kCGDisplayHeight),
                        numberForKey(desc, kCGDisplayBitsPerPixel),
                        numberForKey(desc, kCGDisplayRefreshRate));
}

static void setdisplay( CGDirectDisplayID dspy, int doSwitch)
{
        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("Current Resolution: ");
				printDispDesc(originalMode);
        
        //printf( "Available modes:\n");
        CFArrayRef modes = CGDisplayAvailableModes(dspy);
        CFIndex c = CFArrayGetCount(modes);
        //printf("Num modes: %i\n",c);
        int i;
        for (i=0; i<c; i++) {
        				//printf("Step %i\n", i);
                mode = CFArrayGetValueAtIndex(modes, i);
                printDispDesc( mode );
                /*
                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" );
        }

				if(doSwitch == 1)
				{
	        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;
        int doSwitch; // Track whether or not we switch resolutions.
        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]);
                doSwitch = 1;
        } else {
                myMode.width = 1024;
                myMode.height = 768;
                myMode.bitsPerPixel = 32;
                myMode.refresh = 75;
                doSwitch = 0;
        }

        //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], doSwitch);
        }

        exit(0);
}