MythTV  master
mythutilscocoa.mm
Go to the documentation of this file.
1 // MythTV
2 #include "mythutilscocoa.h"
3 
4 // OSX
5 #import <Cocoa/Cocoa.h>
6 #import <IOKit/graphics/IOGraphicsLib.h>
7 
8 CGDirectDisplayID GetOSXCocoaDisplay(void* View)
9 {
10  NSView *thisview = static_cast<NSView *>(View);
11  if (!thisview)
12  return 0;
13  NSScreen *screen = [[thisview window] screen];
14  if (!screen)
15  return 0;
16  NSDictionary* desc = [screen deviceDescription];
17  return (CGDirectDisplayID)[[desc objectForKey:@"NSScreenNumber"] intValue];
18 }
19 
20 static Boolean CFNumberEqualsUInt32(CFNumberRef Number, uint32_t Uint32)
21 {
22  if (Number == nullptr)
23  return (Uint32 == 0);
24  int64_t Int64;
25  if (!CFNumberGetValue(Number, kCFNumberSInt64Type, &Int64))
26  return false;
27  return Int64 == Uint32;
28 }
29 
30 QByteArray GetOSXEDID(CGDirectDisplayID Display)
31 {
32  QByteArray result;
33  if (!Display)
34  return result;
35 
36  uint32_t vendor = CGDisplayVendorNumber(Display);
37  uint32_t model = CGDisplayModelNumber(Display);
38  uint32_t serial = CGDisplaySerialNumber(Display);
39  CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
40 
41  io_iterator_t iter;
42  if (IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iter))
43  return result;
44 
45  io_service_t service = 0;
46  while ((service = IOIteratorNext(iter)) != 0)
47  {
48  CFDictionaryRef info = IODisplayCreateInfoDictionary(service, kIODisplayOnlyPreferredName);
49  CFNumberRef vendorID = static_cast<CFNumberRef>(CFDictionaryGetValue(info, CFSTR(kDisplayVendorID)));
50  CFNumberRef productID = static_cast<CFNumberRef>(CFDictionaryGetValue(info, CFSTR(kDisplayProductID)));
51  CFNumberRef serialNumber = static_cast<CFNumberRef>(CFDictionaryGetValue(info, CFSTR(kDisplaySerialNumber)));
52 
53  if (CFNumberEqualsUInt32(vendorID, vendor) &&
54  CFNumberEqualsUInt32(productID, model) &&
55  CFNumberEqualsUInt32(serialNumber, serial))
56  {
57  CFDataRef edid = static_cast<CFDataRef>(CFDictionaryGetValue(info, CFSTR(kIODisplayEDIDKey)));
58  if (edid)
59  {
60  const char* data = reinterpret_cast<const char*>(CFDataGetBytePtr(edid));
61  int length = CFDataGetLength(edid);
62  result = QByteArray(data, length);
63  }
64  CFRelease(info);
65  break;
66  }
67  CFRelease(info);
68  }
69  IOObjectRelease(iter);
70  return result;
71 }
CGDirectDisplayID GetOSXCocoaDisplay(void *View)
QByteArray GetOSXEDID(CGDirectDisplayID Display)
static Boolean CFNumberEqualsUInt32(CFNumberRef Number, uint32_t Uint32)