configs  Documentation

/*
 * This small program sets the resolution to 3000x2000 in HiDPI mode.
 *
 * Unfortunately, this is necessary because macOS does not allow selecting 3:2
 * resolutions as used by some travel displays.
 *
 * Compile with:
 *   clang -framework CoreGraphics -framework Foundation Set3000x2000Res.m
 */

#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>

int
main()
{
	NSArray *allModes = (NSArray *)CGDisplayCopyAllDisplayModes(
	    CGMainDisplayID(), (CFDictionaryRef)@{
		(NSString *)kCGDisplayShowDuplicateLowResolutionModes: @YES
	});
	CGDisplayModeRef pickedMode = NULL;
	for (id object in allModes) {
		CGDisplayModeRef mode = (CGDisplayModeRef)object;

		if (CGDisplayModeGetWidth(mode) == 1500 &&
		    CGDisplayModeGetHeight(mode) == 1000 &&
		    CGDisplayModeGetRefreshRate(mode) == 60) {
			pickedMode = mode;
			break;
		}
	}

	if (pickedMode == NULL) {
		NSLog(@"Mode not found!");
		return 1;
	}

	CGDisplayConfigRef config;
	if (CGBeginDisplayConfiguration(&config) != kCGErrorSuccess) {
		NSLog(@"CGBeginDisplayConfiguration failed!");
		return 1;
	}

	if (CGConfigureDisplayWithDisplayMode(config, CGMainDisplayID(),
	    pickedMode, NULL) != kCGErrorSuccess) {
		NSLog(@"CGConfigureDisplayWithMode failed!");
		return 1;
	}

	if (CGCompleteDisplayConfiguration(config, kCGConfigurePermanently) !=
	    kCGErrorSuccess) {
		NSLog(@"CGCompleteDisplayConfiguration failed!");
		return 1;
	}

	return 0;
}