From 36e271cec6b41ec0a72bca5c875316dfe7fd83e8 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sun, 21 Dec 2014 13:27:04 -0500 Subject: [PATCH] Display Preferred Display Name. When listing display modes, show the display's 'prefferred name'. This is usually the make/model. --- Makefile | 2 +- cg_utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cg_utils.h | 7 +++++++ main.c | 4 +++- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 729681d..bbb70cf 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ ARCH_FLAGS=-arch i386 -arch x86_64 build: screenresolution screenresolution: main.c cg_utils.o version.h - $(CC) $(CPPFLAGS) $(CFLAGS) $(ARCH_FLAGS) -framework Foundation -framework ApplicationServices $< *.o -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(ARCH_FLAGS) -framework Foundation -framework ApplicationServices -framework IOKit $< *.o -o $@ %.o: %.c $(CC) $(CPPFLAGS) $(CFLAGS) $(ARCH_FLAGS) $< -c -o $@ diff --git a/cg_utils.c b/cg_utils.c index 4fef025..b5d828b 100644 --- a/cg_utils.c +++ b/cg_utils.c @@ -170,3 +170,59 @@ CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef *mode1Ptr, CGDisplay else return (width1 < width2) ? kCFCompareLessThan : kCFCompareGreaterThan; } + +char* convertCFStringToCString(CFStringRef toConvert) +{ + char* toReturn = ""; + + if (NULL != toConvert) + { + CFIndex length = CFStringGetLength(toConvert); + CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8); + char *buffer = (char *)malloc(maxSize); + + if (CFStringGetCString(toConvert, buffer, maxSize, kCFStringEncodingUTF8)) + { + toReturn = buffer; + } + } + + return toReturn; +} + +char* getPreferredDisplayName(CGDirectDisplayID displayID) +{ + char* name = "Unknown"; + + //TODO: 'CGDisplayIOServicePort' is deprecated (but still available) in OS X 10.9 + // I believe something else will come out by the time it is fully deprecated...or + // Apple will document a way of getting this additional display info while using + // CoreGraphics. + io_service_t displayServicePort = CGDisplayIOServicePort(displayID); + + if (displayServicePort) + { + CFDictionaryRef displayInfoDict = IODisplayCreateInfoDictionary(displayServicePort, kIODisplayOnlyPreferredName); + + if(displayInfoDict) + { + // this array will be populated with the localized names for the display (i.e. names of the + // display in different languages) + CFDictionaryRef namesForDisplay = CFDictionaryGetValue(displayInfoDict, CFSTR(kDisplayProductName)); + CFStringRef value; + + if (namesForDisplay) + { + // TODO: get this working with system's default locale...the rest of the program is in English + // for now, so it's not an utterly obtuse decision to stick with English. + name = convertCFStringToCString(CFDictionaryGetValue(namesForDisplay, CFSTR("en_US"))); + } + + CFRelease(displayInfoDict); + } + + IOObjectRelease(displayServicePort); + } + + return strdup(name); +} \ No newline at end of file diff --git a/cg_utils.h b/cg_utils.h index d050961..a9e155b 100644 --- a/cg_utils.h +++ b/cg_utils.h @@ -22,6 +22,12 @@ #define __CG_UTILS__H #include +#include + +/* +#include +#include +*/ // http://stackoverflow.com/questions/3060121/core-foundation-equivalent-for-nslog/3062319#3062319 #ifndef __OBJC__ @@ -43,5 +49,6 @@ unsigned int configureDisplay(CGDirectDisplayID display, unsigned int parseStringConfig(const char *string, struct config *out); size_t bitDepth(CGDisplayModeRef mode); CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef *mode1Ptr, CGDisplayModeRef *mode2Ptr, void *context); +char* getPreferredDisplayName(CGDirectDisplayID displayID); #endif \ No newline at end of file diff --git a/main.c b/main.c index 598ccb7..16e0bb2 100644 --- a/main.c +++ b/main.c @@ -135,6 +135,8 @@ unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum) { int numModes = 0; int i; + char* displayName = getPreferredDisplayName(display); + CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, NULL); if (allModes == NULL) { returncode = 0; @@ -159,7 +161,7 @@ unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum) { #ifndef LIST_DEBUG if(displayNum != 0) printf("\n\n"); - printf("Available Modes on Display %d\n", displayNum); + printf("Available Modes on Display %d (%s)\n", displayNum, displayName); #endif CGDisplayModeRef mode;