Existing users, log in.  New users, create a free account.  Lost password?

Mac OS X  |  Design / Graphics  |  CAD / Technical  |  cscreen  |  this is not a new thing guys...

cscreen

cscreen

command line app to switch screen resolution/depth

Version:  1.3

   [ Views: 721 ]

this is not a new thing guys...

Feedback Type:  Commentary

Contributed by: MonJab Tuesday, June 15 2004 @ 08:07 AM PDT

Product Platform: MacOSX

Used Product For: Over One Year

here's proof...
source code to do the same thing...

----START----

/*
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) 2002 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>

struct myMode
{
size_t width;
size_t height;
size_t bitsPerPixel;
CGRefreshRate refresh;
};

struct myMode myMode;

static void setdisplay( CGDirectDisplayID dspy)
{
CFDictionaryRef mode;
CFDictionaryRef originalMode;
boolean_t exactMatch;
CGDisplayErr err; // or is this suppose to be CGError???

CGDisplayConfigRef configRef;

originalMode = CGDisplayCurrentMode( dspy );
if ( originalMode == NULL )
{
printf( "Display is invalid\n" );
return;
}

printf( "Display 0x%x: Looking for %ld x %ld, %ld Bits Per Pixel\n", (unsigned int)dspy, myMode.width, myMode.height, myMode.bitsPerPixel );


mode = CGDisplayBestModeForParametersAndRefreshRate(dspy, myMode.bitsPerPixel, myMode.width, myMode.height, myMode.refresh, &exactMatch);

// mode = CGDisplayBestModeForParametersAndRefreshRateWithProperty(dspy, myMode.bitsPerPixel, myMode.width, myMode.height, myMode.refresh, kCGDisplayModeIsSafeForHardware, &exactMatch);

CGBeginDisplayConfiguration(&configRef);

err = CGConfigureDisplayMode(configRef, dspy, mode);

CGCompleteDisplayConfiguration( configRef, kCGConfigurePermanently );

if ( err != CGDisplayNoErr )
{
printf( "Oops! Mode switch failed?!?? (%d)\n", err );
}
}

#define kMaxDisplays 16

int
main(int argc, const char *argv[])
{
CGDirectDisplayID display[kMaxDisplays];
CGDisplayCount numDisplays;
CGDisplayCount 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 = 60;
}

printf( "Width: %d Height: %d BitsPerPixel: %d Refresh rate: %d\n", (int)myMode.width, (int)myMode.height, (int)myMode.bitsPerPixel, (int)myMode.refresh );

err = CGGetActiveDisplayList(kMaxDisplays,
display,
&numDisplays);
if ( err != CGDisplayNoErr )
{
printf("Cannot get displays (%d)\n", err);
exit( 1 );
}
printf( "%d displays found\n", (int)numDisplays );
for ( i = 0; i < numDisplays; ++i )
{
setdisplay(display[i]);
}
printf( "Done.\n" );

exit(0);
}





static 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;
}

static void printDesc( 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 );
}

----END----

Cheers...   

Rate this Commentary

Was this Commentary helpful? Yes | No

Comments

2 comments |

this is not a new thing guys... - lpye

True, it's not new. In case some wonder, no, I didn't have a look at the code before writing mine.

The history of cscreen goes back to a post on a message board (Mac OS X Hints IIRC) where someone asked for this functionality. I wrote the utility, mentioned it and posted to VT. All else has been added features based on user feedback.

I'm not entirely sure what the point of your comment was, though, or what exactly you were trying to accomplish. It's absolutely a simple app and anyone with an urge could quite easily code the same thing. To which, all I can say is...whatever.

Reply to This

Tuesday, June 15 2004 @ 09:00 AM PDT


this is not a new thing guys... - sjk

Probably trying to get the record for the longest VT post? ;-)

Reply to This

Tuesday, June 15 2004 @ 12:53 PM PDT