ohmu
·
2026-10-15
1#include "displayserver.h"
2#include "common/apple/cf_helpers.h"
3#include "common/strutil.h"
4#include "common/edidHelper.h"
5#include "detection/os/os.h"
6
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10#include <CoreGraphics/CGDirectDisplay.h>
11#include <CoreVideo/CVDisplayLink.h>
12#include <IOKit/IOKitLib.h>
13
14#ifdef MAC_OS_X_VERSION_10_15
15// CoreDisplay_Display_* are thin wrappers around these: the CoreDisplay versions just dlsym the
16// SkyLight symbol and call it with the same display id (CoreDisplay_DisplayCreateInfoDictionary
17// only falls back to its own Mach IPC path when SkyLight is absent). Call SkyLight directly.
18[[clang::weak_import]] extern Boolean SLSDisplaySupportsHDRMode(CGDirectDisplayID display);
19[[clang::weak_import]] extern Boolean SLSDisplayIsHDRModeEnabled(CGDirectDisplayID display);
20[[clang::weak_import]] extern CFDictionaryRef SLSCopyDisplayInfoDictionary(CGDirectDisplayID display);
21#else
22 #include <IOKit/graphics/IOGraphicsLib.h>
23#endif
24
25// A CGDisplayModeRef keeps the IOKit mode description in a private CFDictionaryRef that
26// CoreGraphics never hands out (it only reads single keys out of it by itself), so there is no
27// public API for e.g. BitsPerSample. SkyLight exports a plain accessor for it instead, which is
28// the same value the private dictionary holds for the display's current mode:
29// {_CGSDisplayModeDescription=... "bitsPerPixel"i "bitsPerSample"i "samplesPerPixel"i ...}
30// Use that rather than reading CGDisplayModeRef's private layout.
31[[clang::weak_import]] extern int SLDisplayBitsPerSample(CGDirectDisplayID display);
32
33static void detectDisplays(FFDisplayServerResult* ds) {
34 CGDirectDisplayID screens[128];
35 uint32_t screenCount;
36 if (CGGetOnlineDisplayList(ARRAY_SIZE(screens), screens, &screenCount) != kCGErrorSuccess) {
37 return;
38 }
39
40 FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
41 for (uint32_t i = 0; i < screenCount; i++) {
42 CGDirectDisplayID screen = screens[i];
43 boolean_t builtin = CGDisplayIsBuiltin(screen);
44 CGDisplayModeRef mode = CGDisplayCopyDisplayMode(screen);
45 if (mode) {
46 // https://github.com/glfw/glfw/commit/aab08712dd8142b642e2042e7b7ba563acd07a45
47 double refreshRate = CGDisplayModeGetRefreshRate(mode);
48
49 if (refreshRate == 0) {
50#pragma clang diagnostic push
51#pragma clang diagnostic ignored "-Wdeprecated-declarations"
52 CVDisplayLinkRef link;
53 if (CVDisplayLinkCreateWithCGDisplay(screen, &link) == kCVReturnSuccess) {
54 const CVTime time = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(link);
55 if (!(time.flags & kCVTimeIsIndefinite)) {
56 refreshRate = time.timeScale / (double) time.timeValue; // 59.97...
57 }
58 CVDisplayLinkRelease(link);
59 }
60#pragma clang diagnostic pop
61 }
62
63 ffStrbufClear(&buffer);
64 FF_CFTYPE_AUTO_RELEASE CFDictionaryRef displayInfo = nullptr;
65#ifdef MAC_OS_X_VERSION_10_15
66 if (SLSCopyDisplayInfoDictionary) {
67 displayInfo = SLSCopyDisplayInfoDictionary(screen);
68 }
69#else
70 {
71 io_service_t servicePort = CGDisplayIOServicePort(screen);
72 displayInfo = IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName);
73 }
74#endif
75
76 uint32_t physicalWidth = 0, physicalHeight = 0;
77 uint32_t preferredWidth = 0, preferredHeight = 0;
78 double preferredRefreshRate = 0;
79 FF_STRBUF_AUTO_DESTROY serial = ffStrbufCreate();
80
81 if (displayInfo) {
82 // CGDisplayScreenSize reports invalid result for external displays on old Intel MacBook Pro
83 CFDataRef edidRef = (CFDataRef) CFDictionaryGetValue(displayInfo, CFSTR(kIODisplayEDIDKey));
84 if (edidRef && CFGetTypeID(edidRef) == CFDataGetTypeID()) {
85 const uint8_t* edidData = CFDataGetBytePtr(edidRef);
86 uint32_t edidLength = (uint32_t) CFDataGetLength(edidRef);
87 if (ffEdidIsValid(edidData, edidLength)) {
88 ffEdidGetPhysicalSize(edidData, &physicalWidth, &physicalHeight);
89 ffEdidGetSerial(edidData, &serial);
90 }
91 } else if (!builtin && ffCfDictGetString(displayInfo, CFSTR(kIODisplayLocationKey), &buffer) == nullptr) {
92 FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t ioDisplay = IORegistryEntryFromPath(MACH_PORT_NULL, buffer.chars);
93 ffStrbufClear(&buffer);
94 if (ioDisplay) {
95 FF_CFTYPE_AUTO_RELEASE CFDictionaryRef displayAttrRef = IORegistryEntryCreateCFProperty(ioDisplay, CFSTR("DisplayAttributes"), kCFAllocatorDefault, kNilOptions);
96 if (displayAttrRef && CFGetTypeID(displayAttrRef) == CFDictionaryGetTypeID()) {
97 CFDictionaryRef productAttrs;
98 if (ffCfDictGetDict(displayAttrRef, CFSTR("ProductAttributes"), &productAttrs) == nullptr) {
99 ffCfDictGetString(productAttrs, CFSTR("AlphanumericSerialNumber"), &serial);
100 ffCfDictGetString(productAttrs, CFSTR("ProductName"), &buffer);
101 }
102 }
103 }
104 }
105
106 if (!buffer.length) {
107 CFDictionaryRef productNames;
108 if (ffCfDictGetDict(displayInfo, CFSTR(kDisplayProductName), &productNames) == nullptr) {
109 ffCfDictGetString(productNames, CFSTR("en_US"), &buffer);
110 }
111 }
112
113 if (!physicalWidth || !physicalHeight) {
114 if (ffCfDictGetInt(displayInfo, CFSTR(kDisplayHorizontalImageSize), (int*) &physicalWidth) == nullptr) {
115 ffCfDictGetInt(displayInfo, CFSTR(kDisplayVerticalImageSize), (int*) &physicalHeight);
116 }
117 }
118
119 ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelWidth"), (int*) &preferredWidth);
120 ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelHeight"), (int*) &preferredHeight);
121 if (preferredWidth && preferredHeight) {
122 FF_CFTYPE_AUTO_RELEASE CFArrayRef allModes = CGDisplayCopyAllDisplayModes(screen, nullptr);
123 if (allModes) {
124 for (CFIndex i = 0, count = CFArrayGetCount(allModes); i < count; i++) {
125 CGDisplayModeRef modeInfo = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, i);
126 if (CGDisplayModeGetPixelWidth(modeInfo) == preferredWidth && CGDisplayModeGetPixelHeight(modeInfo) == preferredHeight) {
127 double rr = CGDisplayModeGetRefreshRate(modeInfo);
128 if (rr > preferredRefreshRate) {
129 preferredRefreshRate = rr;
130 }
131 break;
132 }
133 }
134 }
135 }
136 }
137
138 if ((!physicalWidth || !physicalHeight) && CGDisplayPrimaryDisplay(screen) == screen) // #1406
139 {
140 CGSize size = CGDisplayScreenSize(screen);
141 physicalWidth = (uint32_t) (size.width + 0.5);
142 physicalHeight = (uint32_t) (size.height + 0.5);
143 }
144
145 uint32_t pixelWidth = (uint32_t) CGDisplayModeGetPixelWidth(mode);
146 uint32_t pixelHeight = (uint32_t) CGDisplayModeGetPixelHeight(mode);
147
148 FFDisplayResult* display = ffdsAppendDisplay(ds,
149 pixelWidth,
150 pixelHeight,
151 refreshRate,
152 pixelHeight * 96 / (uint32_t) CGDisplayModeGetHeight(mode),
153 preferredWidth,
154 preferredHeight,
155 preferredRefreshRate,
156 (uint32_t) CGDisplayRotation(screen),
157 &buffer,
158 builtin ? FF_DISPLAY_TYPE_BUILTIN : FF_DISPLAY_TYPE_EXTERNAL,
159 CGDisplayIsMain(screen),
160 (uint64_t) screen,
161 physicalWidth,
162 physicalHeight,
163 "CoreGraphics");
164 if (display) {
165 if (SLDisplayBitsPerSample) {
166 // See the comment on SLDisplayBitsPerSample. Returns 0 for an unknown display.
167 int bitsPerSample = SLDisplayBitsPerSample(screen);
168 if (bitsPerSample > 0) {
169 display->bitDepth = (uint8_t) bitsPerSample;
170 }
171 }
172
173 if (display->type == FF_DISPLAY_TYPE_BUILTIN && displayInfo) {
174 display->hdrStatus = CFDictionaryContainsKey(displayInfo, CFSTR("ReferencePeakHDRLuminance"))
175 ? FF_DISPLAY_HDR_STATUS_SUPPORTED
176 : FF_DISPLAY_HDR_STATUS_UNSUPPORTED;
177 }
178#ifdef MAC_OS_X_VERSION_10_15
179 else if (SLSDisplaySupportsHDRMode) {
180 if (SLSDisplaySupportsHDRMode(screen)) {
181 display->hdrStatus = FF_DISPLAY_HDR_STATUS_SUPPORTED;
182 if (SLSDisplayIsHDRModeEnabled && SLSDisplayIsHDRModeEnabled(screen)) {
183 display->hdrStatus = FF_DISPLAY_HDR_STATUS_ENABLED;
184 }
185 } else {
186 display->hdrStatus = FF_DISPLAY_HDR_STATUS_UNSUPPORTED;
187 }
188 }
189#endif
190
191 if (serial.length) {
192 ffStrbufInitMove(&display->serial, &serial);
193 } else {
194 uint32_t int_serial = CGDisplaySerialNumber(screen);
195 if (int_serial != 0 && int_serial != 0xFFFFFFFF) {
196 ffStrbufSetF(&display->serial, "0x%08X", int_serial);
197 }
198 }
199
200 if (displayInfo) {
201 int value;
202 if (ffCfDictGetInt(displayInfo, CFSTR(kDisplayYearOfManufacture), &value) == nullptr) {
203 display->manufactureYear = (uint16_t) value;
204 }
205 if (ffCfDictGetInt(displayInfo, CFSTR(kDisplayWeekOfManufacture), &value) == nullptr) {
206 display->manufactureWeek = (uint16_t) value;
207 }
208 }
209 }
210 CGDisplayModeRelease(mode);
211 }
212 CGDisplayRelease(screen);
213 }
214}
215
216void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) {
217 {
218 FF_CFTYPE_AUTO_RELEASE CFMachPortRef port = CGWindowServerCreateServerPort();
219 if (port) {
220 ffStrbufSetStatic(&ds->wmProcessName, "WindowServer");
221 ffStrbufSetStatic(&ds->wmPrettyName, "Quartz Compositor");
222 }
223 }
224
225 detectDisplays(ds);
226}