Skip to content

Instantly share code, notes, and snippets.

@tyfkda
Created September 25, 2018 03:48
Show Gist options
  • Save tyfkda/51bd043d4bbaf216a0c41282fbc0cd30 to your computer and use it in GitHub Desktop.
Save tyfkda/51bd043d4bbaf216a0c41282fbc0cd30 to your computer and use it in GitHub Desktop.
Resolution changer for Mac OSX
// http://apple.stackexchange.com/questions/139548/changing-display-resolution-on-retina-machines-while-in-command-line-mode
/*
* COMPILE:
* c++ setgetscreenres.m -framework ApplicationServices -o setgetscreenres
* USE:
* setgetscreenres 2048 1280 # 1440 900
*/
#include <ApplicationServices/ApplicationServices.h>
bool MyDisplaySwitchToMode (CGDirectDisplayID display, CFDictionaryRef mode);
int main (int argc, const char * argv[])
{
int h; // horizontal resolution
int v; // vertical resolution
CFDictionaryRef switchMode; // mode to switch to
CGDirectDisplayID mainDisplay; // ID of main display
CFDictionaryRef CGDisplayCurrentMode(CGDirectDisplayID display);
if (argc == 1) {
CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay);
CGSize screenSize = screenFrame.size;
printf("%g %g\n", screenSize.width, screenSize.height);
return 0;
}
if (argc != 3 || !(h = atoi(argv[1])) || !(v = atoi(argv[2])) ) {
fprintf(stderr, "ERROR: Use %s horres vertres\n", argv[0]);
return -1;
}
mainDisplay = CGMainDisplayID();
switchMode = CGDisplayBestModeForParameters(mainDisplay, 32, h, v, NULL);
if (! MyDisplaySwitchToMode(mainDisplay, switchMode)) {
fprintf(stderr, "Error changing resolution to %d %d\n", h, v);
return 1;
}
return 0;
}
bool MyDisplaySwitchToMode (CGDirectDisplayID display, CFDictionaryRef mode)
{
CGDisplayConfigRef config;
if (CGBeginDisplayConfiguration(&config) == kCGErrorSuccess) {
CGConfigureDisplayMode(config, display, mode);
CGCompleteDisplayConfiguration(config, kCGConfigureForSession );
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment