Skip to content

Instantly share code, notes, and snippets.

@william-reed
Last active February 16, 2019 16:29
Show Gist options
  • Select an option

  • Save william-reed/ac2d6306291da351a3332b87e20b596f to your computer and use it in GitHub Desktop.

Select an option

Save william-reed/ac2d6306291da351a3332b87e20b596f to your computer and use it in GitHub Desktop.
reading EDID information on OSX
###############################
## main.c
###############################
#include <ApplicationServices/ApplicationServices.h>
#include <IOKit/graphics/IOGraphicsLib.h>
const UInt8 SERIAL = 0xFF;
const UInt8 DISPLAY_NAME = 0xFC;
const UInt8 UNSPECIFIED_TEXT = 0xFE;
int main() {
CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
io_iterator_t serialPortIterator = 0;
kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &serialPortIterator);
if (KERN_SUCCESS == kernResult && serialPortIterator != 0) {
io_object_t next;
do {
next = IOIteratorNext(serialPortIterator);
CFDictionaryRef dict = IODisplayCreateInfoDictionary(next, kIODisplayOnlyPreferredName);
CFDataRef data;
EDID *edid;
data = CFDictionaryGetValue(dict, CFSTR(kIODisplayEDIDKey));
if (!data || (CFDataGetLength(data) < sizeof(EDID)))
continue;
edid = (EDID *) CFDataGetBytePtr(data);
printf("EDID version\t\t%u.%u\n", edid->version, edid->revision);
// serial
printf("serial\t\t\t\t%.4s\n", edid->serialNumber);
printf("horiz screen size\t%u cm\n", edid->displayParams[1]);
printf("vert screen size\t%u cm\n", edid->displayParams[2]);
// any extra data?
for (int i = 0; i < 4; i++) {
EDIDGeneralDesc generalDesc = edid->descriptors[i].general;
UInt8 type = generalDesc.type;
switch (type) {
case SERIAL:
printf("serial\t\t\t\t");
break;
case DISPLAY_NAME:
printf("display name\t\t");
break;
case UNSPECIFIED_TEXT:
printf("unspecified text\t\t");
break;
default:
continue;
}
// print the actual data
printf("%.13s", generalDesc.data);
}
printf("\n====================\n");
} while (next != 0);
CFRelease(&next);
}
CFRelease(matching);
return 0;
}
###############################
## edid.h
###############################
//
// Help from:
// https://opensource.apple.com/source/IOKitUser/IOKitUser-67/graphics.subproj/IODisplayLib.c
// https://opensource.apple.com/source/IOKitUser/IOKitUser-67/graphics.subproj/IOGraphicsLibPrivate.h
//
#include <MacTypes.h>
#ifndef MONROT_EDID_H
#define MONROT_EDID_H
struct EDIDDetailedTimingDesc {
UInt16 clock;
UInt8 horizActive;
UInt8 horizBlanking;
UInt8 horizHigh;
UInt8 verticalActive;
UInt8 verticalBlanking;
UInt8 verticalHigh;
UInt8 horizSyncOffset;
UInt8 horizSyncWidth;
UInt8 verticalSyncOffsetWidth;
UInt8 syncHigh;
UInt8 horizImageSize;
UInt8 verticalImageSize;
UInt8 imageSizeHigh;
UInt8 horizBorder;
UInt8 verticalBorder;
UInt8 flags;
};
typedef struct EDIDDetailedTimingDesc EDIDDetailedTimingDesc;
struct EDIDGeneralDesc {
UInt16 flag1;
UInt8 flag2;
UInt8 type;
UInt8 flag3;
UInt8 data[13];
};
typedef struct EDIDGeneralDesc EDIDGeneralDesc;
union EDIDDesc {
EDIDDetailedTimingDesc timing;
EDIDGeneralDesc general;
};
typedef union EDIDDesc EDIDDesc;
struct EDID {
UInt8 header[8];
UInt8 vendorProduct[4];
UInt8 serialNumber[4];
UInt8 weekOfManufacture;
UInt8 yearOfManufacture;
UInt8 version;
UInt8 revision;
UInt8 displayParams[5];
UInt8 colorCharacteristics[10];
UInt8 establishedTimings[3];
UInt16 standardTimings[8];
EDIDDesc descriptors[4];
UInt8 extension;
UInt8 checksum;
};
typedef struct EDID EDID;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment