Skip to content

Commit f30501f

Browse files
committed
Display (macOS): fix invalid physical size on old Intel Mac
1 parent bde6c3b commit f30501f

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/detection/displayserver/displayserver_apple.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "displayserver.h"
22
#include "util/apple/cf_helpers.h"
33
#include "util/stringUtils.h"
4+
#include "util/edidHelper.h"
45

56
#include <stdlib.h>
67
#include <string.h>
@@ -23,6 +24,7 @@ static void detectDisplays(FFDisplayServerResult* ds)
2324
if(CGGetOnlineDisplayList(sizeof(screens) / sizeof(screens[0]), screens, &screenCount) != kCGErrorSuccess)
2425
return;
2526

27+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
2628
for(uint32_t i = 0; i < screenCount; i++)
2729
{
2830
CGDirectDisplayID screen = screens[i];
@@ -44,7 +46,7 @@ static void detectDisplays(FFDisplayServerResult* ds)
4446
}
4547
}
4648

47-
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
49+
ffStrbufClear(&buffer);
4850
CFDictionaryRef FF_CFTYPE_AUTO_RELEASE displayInfo = NULL;
4951
#ifdef MAC_OS_X_VERSION_10_15
5052
if(CoreDisplay_DisplayCreateInfoDictionary)
@@ -55,14 +57,30 @@ static void detectDisplays(FFDisplayServerResult* ds)
5557
displayInfo = IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName);
5658
}
5759
#endif
60+
uint32_t physicalWidth = 0, physicalHeight = 0;
5861
if(displayInfo)
5962
{
6063
CFDictionaryRef productNames;
6164
if(!ffCfDictGetDict(displayInfo, CFSTR(kDisplayProductName), &productNames))
62-
ffCfDictGetString(productNames, CFSTR("en_US"), &name);
65+
ffCfDictGetString(productNames, CFSTR("en_US"), &buffer);
66+
67+
// CGDisplayScreenSize reports invalid result for external displays on old Intel MacBook Pro
68+
CFDataRef edidRef = (CFDataRef) CFDictionaryGetValue(displayInfo, CFSTR(kIODisplayEDIDKey));
69+
if (edidRef && CFGetTypeID(edidRef) == CFDataGetTypeID())
70+
{
71+
const uint8_t* edidData = CFDataGetBytePtr(edidRef);
72+
uint32_t edidLength = (uint32_t) CFDataGetLength(edidRef);
73+
if (edidLength >= 128)
74+
ffEdidGetPhysicalSize(edidData, &physicalWidth, &physicalHeight);
75+
}
6376
}
6477

65-
CGSize size = CGDisplayScreenSize(screen);
78+
if (!physicalWidth || !physicalHeight)
79+
{
80+
CGSize size = CGDisplayScreenSize(screen);
81+
physicalWidth = (uint32_t) (size.width + 0.5);
82+
physicalHeight = (uint32_t) (size.height + 0.5);
83+
}
6684

6785
FFDisplayResult* display = ffdsAppendDisplay(ds,
6886
(uint32_t)CGDisplayModeGetPixelWidth(mode),
@@ -71,12 +89,12 @@ static void detectDisplays(FFDisplayServerResult* ds)
7189
(uint32_t)CGDisplayModeGetWidth(mode),
7290
(uint32_t)CGDisplayModeGetHeight(mode),
7391
(uint32_t)CGDisplayRotation(screen),
74-
&name,
92+
&buffer,
7593
CGDisplayIsBuiltin(screen) ? FF_DISPLAY_TYPE_BUILTIN : FF_DISPLAY_TYPE_EXTERNAL,
7694
CGDisplayIsMain(screen),
7795
(uint64_t)screen,
78-
(uint32_t) (size.width + 0.5),
79-
(uint32_t) (size.height + 0.5)
96+
physicalWidth,
97+
physicalHeight
8098
);
8199
if (display)
82100
{
@@ -97,16 +115,20 @@ static void detectDisplays(FFDisplayServerResult* ds)
97115
{
98116
if (CoreDisplay_Display_SupportsHDRMode(screen))
99117
{
100-
if (CoreDisplay_Display_IsHDRModeEnabled)
101-
{
102-
display->hdrStatus = CoreDisplay_Display_IsHDRModeEnabled(screen)
103-
? FF_DISPLAY_HDR_STATUS_ENABLED
104-
: FF_DISPLAY_HDR_STATUS_SUPPORTED;
105-
}
106-
else
107-
display->hdrStatus = FF_DISPLAY_HDR_STATUS_SUPPORTED;
118+
display->hdrStatus = FF_DISPLAY_HDR_STATUS_SUPPORTED;
119+
if (CoreDisplay_Display_IsHDRModeEnabled && CoreDisplay_Display_IsHDRModeEnabled(screen))
120+
display->hdrStatus = FF_DISPLAY_HDR_STATUS_ENABLED;
108121
}
122+
else
123+
display->hdrStatus = FF_DISPLAY_HDR_STATUS_UNSUPPORTED;
109124
}
125+
126+
display->serial = CGDisplaySerialNumber(screen);
127+
int value;
128+
if (ffCfDictGetInt(displayInfo, CFSTR(kDisplayYearOfManufacture), &value) == NULL)
129+
display->manufactureYear = (uint16_t) value;
130+
if (ffCfDictGetInt(displayInfo, CFSTR(kDisplayWeekOfManufacture), &value) == NULL)
131+
display->manufactureWeek = (uint16_t) value;
110132
}
111133
CGDisplayModeRelease(mode);
112134
}

0 commit comments

Comments
 (0)