From b182e6156b21254c5d44a24eee1e626d16ee481e Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Fri, 7 Aug 2026 03:17:07 +0000
Subject: [PATCH 22/38] displayserver (Apple): use legacy CGS mode API under
 CG_LEGACY_API

CGDisplayCopyDisplayMode()/CGDisplayModeGetRefreshRate() and friends
aren't available before macOS 10.6; on those targets (or __ppc__)
retrieve mode info through the private CGSGetCurrentDisplayMode()/
CGSGetDisplayModeDescriptionOfLength() pair instead, and use
CGWindowServerCFMachPort() in place of CGWindowServerCreateServerPort()
before 10.8.

Ported from https://github.com/macos-powerpc/powerpc-ports/blob/main/sysutils/fastfetch/files/0012-Misc-fixes-for-legacy-systems.patch
---
 .../displayserver/displayserver_apple.c       | 64 ++++++++++++++++++-
 1 file changed, 62 insertions(+), 2 deletions(-)

diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c
index 94c86dfb6..4e341330d 100644
--- a/src/detection/displayserver/displayserver_apple.c
+++ b/src/detection/displayserver/displayserver_apple.c
@@ -7,6 +7,35 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+
+#ifdef CG_LEGACY_API
+
+#include "common/sysctl.h"
+#include <ApplicationServices/ApplicationServices.h>
+
+typedef union
+{
+    uint8_t rawData[0xDC];
+    struct
+    {
+        uint32_t mode;
+        uint32_t flags;     // 0x4
+        uint32_t width;     // 0x8
+        uint32_t height;    // 0xC
+        uint32_t depth;     // 0x10
+        uint32_t dc2[42];
+        uint16_t dc3;
+        uint16_t freq;      // 0xBC
+        uint32_t dc4[4];
+        float density;      // 0xD0
+    } derived;
+} modes_D4;
+
+void CGSGetCurrentDisplayMode(CGDirectDisplayID display, int* modeNum);
+void CGSGetDisplayModeDescriptionOfLength(CGDirectDisplayID display, int idx, modes_D4* mode, int length);
+
+#endif
+
 #include <CoreGraphics/CGDirectDisplay.h>
 #include <CoreVideo/CVDisplayLink.h>
 #include <IOKit/IOKitLib.h>
@@ -28,11 +57,23 @@ static void detectDisplays(FFDisplayServerResult* ds) {
 
     FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
     for (uint32_t i = 0; i < screenCount; i++) {
+#ifdef CG_LEGACY_API
+        int modeID;
+        CGSGetCurrentDisplayMode(screens[i], &modeID);
+        modes_D4 mode;
+        CGSGetDisplayModeDescriptionOfLength(screens[i], modeID, &mode, 0xD4);
+#endif
         CGDirectDisplayID screen = screens[i];
         boolean_t builtin = CGDisplayIsBuiltin(screen);
+#ifndef CG_LEGACY_API
         CGDisplayModeRef mode = CGDisplayCopyDisplayMode(screen);
-        if (mode) {
+        if (mode)
+#endif
+        {
             // https://github.com/glfw/glfw/commit/aab08712dd8142b642e2042e7b7ba563acd07a45
+#ifdef CG_LEGACY_API
+            double refreshRate = ffdsParseRefreshRate(mode.derived.freq);
+#else
             double refreshRate = CGDisplayModeGetRefreshRate(mode);
 
             if (refreshRate == 0) {
@@ -48,6 +89,7 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                 }
 #pragma clang diagnostic pop
             }
+#endif
 
             ffStrbufClear(&buffer);
             FF_CFTYPE_AUTO_RELEASE CFDictionaryRef displayInfo = nullptr;
@@ -107,6 +149,7 @@ static void detectDisplays(FFDisplayServerResult* ds) {
 
                 ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelWidth"), (int*) &preferredWidth);
                 ffCfDictGetInt(displayInfo, CFSTR("kCGDisplayPixelHeight"), (int*) &preferredHeight);
+#ifndef CG_LEGACY_API
                 if (preferredWidth && preferredHeight) {
                     FF_CFTYPE_AUTO_RELEASE CFArrayRef allModes = CGDisplayCopyAllDisplayModes(screen, nullptr);
                     if (allModes) {
@@ -122,6 +165,7 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                         }
                     }
                 }
+#endif
             }
 
             if ((!physicalWidth || !physicalHeight) && CGDisplayPrimaryDisplay(screen) == screen) // #1406
@@ -131,14 +175,22 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                 physicalHeight = (uint32_t) (size.height + 0.5);
             }
 
+#ifdef CG_LEGACY_API
+            uint32_t pixelWidth = mode.derived.width;
+            uint32_t pixelHeight = mode.derived.height;
+            // This will result in DPI always being reported as 96:
+            uint32_t modeHeight = mode.derived.height;
+#else
             uint32_t pixelWidth = (uint32_t) CGDisplayModeGetPixelWidth(mode);
             uint32_t pixelHeight = (uint32_t) CGDisplayModeGetPixelHeight(mode);
+            uint32_t modeHeight = (uint32_t) CGDisplayModeGetHeight(mode);
+#endif
 
             FFDisplayResult* display = ffdsAppendDisplay(ds,
                 pixelWidth,
                 pixelHeight,
                 refreshRate,
-                pixelHeight * 96 / (uint32_t) CGDisplayModeGetHeight(mode),
+                pixelHeight * 96 / modeHeight,
                 preferredWidth,
                 preferredHeight,
                 preferredRefreshRate,
@@ -151,6 +203,7 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                 physicalHeight,
                 "CoreGraphics");
             if (display) {
+#ifndef CG_LEGACY_API
 #ifndef MAC_OS_X_VERSION_10_11
                 FF_CFTYPE_AUTO_RELEASE CFStringRef pe = CGDisplayModeCopyPixelEncoding(mode);
                 if (pe) {
@@ -165,6 +218,7 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                     ffCfDictGetInt(dict, kCGDisplayBitsPerSample, &bitDepth);
                     display->bitDepth = (uint8_t) bitDepth;
                 }
+#endif
 #endif
 
                 if (display->type == FF_DISPLAY_TYPE_BUILTIN && displayInfo) {
@@ -204,7 +258,9 @@ static void detectDisplays(FFDisplayServerResult* ds) {
                     }
                 }
             }
+#ifndef CG_LEGACY_API
             CGDisplayModeRelease(mode);
+#endif
         }
         CGDisplayRelease(screen);
     }
@@ -212,7 +268,11 @@ static void detectDisplays(FFDisplayServerResult* ds) {
 
 void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) {
     {
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
         FF_CFTYPE_AUTO_RELEASE CFMachPortRef port = CGWindowServerCreateServerPort();
+#else
+        FF_CFTYPE_AUTO_RELEASE CFMachPortRef port = CGWindowServerCFMachPort();
+#endif
         if (port) {
             ffStrbufSetStatic(&ds->wmProcessName, "WindowServer");
             ffStrbufSetStatic(&ds->wmPrettyName, "Quartz Compositor");
-- 
2.43.0

