Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getMax to fetch maximum/native resolution #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cg_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,8 @@ unsigned int parseStringConfig(const char *string, struct config *out) {
return rc;
}

CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef *mode1Ptr, CGDisplayModeRef *mode2Ptr, void *context)
CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef mode1, CGDisplayModeRef mode2, void *context)
{
CGDisplayModeRef mode1 = (CGDisplayModeRef)mode1Ptr;
CGDisplayModeRef mode2 = (CGDisplayModeRef)mode2Ptr;

size_t width1 = CGDisplayModeGetWidth(mode1);
size_t width2 = CGDisplayModeGetWidth(mode2);

Expand Down
2 changes: 1 addition & 1 deletion cg_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ unsigned int configureDisplay(CGDirectDisplayID display,
int displayNum);
unsigned int parseStringConfig(const char *string, struct config *out);
size_t bitDepth(CGDisplayModeRef mode);
CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef *mode1Ptr, CGDisplayModeRef *mode2Ptr, void *context);
CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef mode1, CGDisplayModeRef mode2, void *context);

#endif
41 changes: 41 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum);
unsigned int listCurrentMode(CGDirectDisplayID display, int displayNum);
unsigned int listMaxSupported(CGDirectDisplayID display, int displayNum);

int main(int argc, const char *argv[]) {
// http://developer.apple.com/library/IOs/#documentation/CoreFoundation/Conceptual/CFStrings/Articles/MutableStrings.html
Expand Down Expand Up @@ -77,6 +78,10 @@ int main(int argc, const char *argv[]) {
if (!listCurrentMode(activeDisplays[d], d)) {
exitcode++;
}
} else if (strcmp(argv[1], "getMax") == 0) {
if (!listMaxSupported(activeDisplays[d], d)) {
exitcode++;
}
} else if (strcmp(argv[1], "list") == 0) {
if (!listAvailableModes(activeDisplays[d], d)) {
exitcode++;
Expand Down Expand Up @@ -131,6 +136,42 @@ unsigned int listCurrentMode(CGDirectDisplayID display, int displayNum) {
return returncode;
}

unsigned int listMaxSupported(CGDirectDisplayID display, int displayNum) {
unsigned int returncode = 1;
CGDisplayModeRef maxSupportedMode = CGDisplayCopyDisplayMode(display);

CFStringRef keys[1] = { kCGDisplayShowDuplicateLowResolutionModes };
CFBooleanRef values[1] = { kCFBooleanTrue };
CFDictionaryRef options = CFDictionaryCreate(
kCFAllocatorDefault, (const void**) keys, (const void**) values, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, options);
if (allModes == NULL) {
NSLog(CFSTR("Error: failed trying to look up modes for display %u"), displayNum);
}

CFIndex displayModeCount = CFArrayGetCount(allModes);
for (int i = 0; i < displayModeCount; ++i) {
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i);
CFComparisonResult result = _compareCFDisplayModes(mode, maxSupportedMode, NULL);
if (result == kCFCompareGreaterThan) {
maxSupportedMode = CGDisplayModeRetain(mode);
}
}

NSLog(CFSTR("Display %d: %ux%ux%u@%.0f"),
displayNum,
CGDisplayModeGetWidth(maxSupportedMode),
CGDisplayModeGetHeight(maxSupportedMode),
bitDepth(maxSupportedMode),
CGDisplayModeGetRefreshRate(maxSupportedMode));

CFRelease(allModes);
CFRelease(options);
CGDisplayModeRelease(maxSupportedMode);
return returncode;
}

unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum) {
unsigned int returncode = 1;
int numModes = 0;
Expand Down