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