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

Display Preferred Display Name. #19

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand Down
56 changes: 56 additions & 0 deletions cg_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 7 additions & 0 deletions cg_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
#define __CG_UTILS__H

#include <ApplicationServices/ApplicationServices.h>
#include <IOKit/graphics/IOGraphicsLib.h>

/*
#include <CoreVideo/CVBase.h>
#include <CoreVideo/CVDisplayLink.h>
*/

// http://stackoverflow.com/questions/3060121/core-foundation-equivalent-for-nslog/3062319#3062319
#ifndef __OBJC__
Expand All @@ -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
4 changes: 3 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down