DisplayServer: support brightness detection (macOS)

This commit is contained in:
李通洲 2023-01-07 19:46:56 +08:00
parent 3e2c344417
commit 959540f17c
9 changed files with 40 additions and 15 deletions

View File

@ -652,6 +652,7 @@ if(APPLE)
PRIVATE "-framework Cocoa"
PRIVATE "-framework CoreWLAN"
PRIVATE "-weak_framework MediaRemote -F /System/Library/PrivateFrameworks"
PRIVATE "-weak_framework DisplayServices -F /System/Library/PrivateFrameworks"
)
elseif(WIN32)
target_compile_definitions(libfastfetch PRIVATE -D_WIN32_WINNT=0x0601)

View File

@ -54,6 +54,7 @@ The following libraries are used if present at runtime:
### macOS
* [`MediaRemote`](https://iphonedev.wiki/index.php/MediaRemote.framework): Need for Media detection. It's a private framework provided by newer macOS system.
* [`DisplayServices`](https://developer.apple.com/forums/thread/666383#663154022): Need for screen brightness detection. It's a private framework provided by newer macOS system.
* [`MoltenVK`](https://github.com/KhronosGroup/MoltenVK): Vulkan driver for macOS. [`molten-vk`](https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/molten-vk.rb)
* [`libmagickcore` (ImageMagick)](https://www.imagemagick.org/): Images in terminal using sixel graphics protocol. [`imagemagick`](https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/imagemagick.rb)
* [`libchafa`](https://github.com/hpjansson/chafa): Image output as ascii art. [`chafa`](https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/chafa.rb)

View File

@ -19,7 +19,7 @@ uint32_t ffdsParseRefreshRate(int32_t refreshRate)
return (uint32_t) refreshRate;
}
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate)
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate, int32_t brightness)
{
if(width == 0 || height == 0)
return false;
@ -28,6 +28,7 @@ bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_
resolution->width = width;
resolution->height = height;
resolution->refreshRate = refreshRate;
resolution->brightness = brightness;
return true;
}

View File

@ -10,6 +10,7 @@ typedef struct FFResolutionResult
uint32_t width;
uint32_t height;
uint32_t refreshRate;
int32_t brightness;
} FFResolutionResult;
typedef struct FFDisplayServerResult
@ -27,6 +28,6 @@ const FFDisplayServerResult* ffConnectDisplayServer(const FFinstance* instance);
//Used internal
uint32_t ffdsParseRefreshRate(int32_t refreshRate);
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate);
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate, int32_t brightness);
#endif

View File

@ -7,6 +7,8 @@
#include <assert.h>
#include <CoreGraphics/CGDirectDisplay.h>
extern int DisplayServicesGetBrightness(CGDirectDisplayID display, float *brightness) __attribute__((weak_import));
static void detectResolution(FFDisplayServerResult* ds)
{
CGDirectDisplayID screens[128];
@ -20,10 +22,15 @@ static void detectResolution(FFDisplayServerResult* ds)
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(screen);
if(mode)
{
float brightness;
if(DisplayServicesGetBrightness == NULL || DisplayServicesGetBrightness(screen, &brightness) != kCGErrorSuccess)
brightness = -1;
ffdsAppendResolution(ds,
(uint32_t)CGDisplayModeGetWidth(mode),
(uint32_t)CGDisplayModeGetHeight(mode),
(uint32_t)CGDisplayModeGetRefreshRate(mode)
(uint32_t)CGDisplayModeGetRefreshRate(mode),
(int32_t)(brightness * 100)
);
CGDisplayModeRelease(mode);
}

View File

@ -34,7 +34,7 @@ void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* ins
if(EnumDisplaySettingsW(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &devMode) == 0)
continue;
ffdsAppendResolution(ds, devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmDisplayFrequency);
ffdsAppendResolution(ds, devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmDisplayFrequency, -1);
}
//https://github.com/hykilpikonna/hyfetch/blob/master/neofetch#L2067

View File

@ -119,7 +119,8 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result)
result,
(uint32_t) iterator.data->width_in_pixels,
(uint32_t) iterator.data->height_in_pixels,
0
0,
-1
);
ffxcb_screen_next(&iterator);
}
@ -184,7 +185,8 @@ static bool xcbRandrHandleModeInfo(XcbRandrData* data, xcb_randr_mode_info_t* mo
data->result,
(uint32_t) modeInfo->width,
(uint32_t) modeInfo->height,
refreshRate == 0 ? data->defaultRefreshRate : refreshRate
refreshRate == 0 ? data->defaultRefreshRate : refreshRate,
-1
);
}
@ -219,7 +221,8 @@ static bool xcbRandrHandleCrtc(XcbRandrData* data, xcb_randr_crtc_t crtc)
data->result,
(uint32_t) crtcInfoReply->width,
(uint32_t) crtcInfoReply->height,
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
free(crtcInfoReply);
@ -262,7 +265,8 @@ static bool xcbRandrHandleMonitor(XcbRandrData* data, xcb_randr_monitor_info_t*
data->result,
(uint32_t) monitor->width,
(uint32_t) monitor->height,
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
}
@ -320,7 +324,8 @@ static void xcbRandrHandleScreen(XcbRandrData* data, xcb_screen_t* screen)
data->result,
(uint32_t) screen->width_in_pixels,
(uint32_t) screen->height_in_pixels,
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
}

View File

@ -83,7 +83,8 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result)
result,
(uint32_t) screen->width,
(uint32_t) screen->height,
0
0,
-1
);
}
@ -142,7 +143,8 @@ static bool xrandrHandleModeInfo(XrandrData* data, XRRModeInfo* modeInfo)
data->result,
(uint32_t) modeInfo->width,
(uint32_t) modeInfo->height,
refreshRate == 0 ? data->defaultRefreshRate : refreshRate
refreshRate == 0 ? data->defaultRefreshRate : refreshRate,
-1
);
}
@ -171,7 +173,8 @@ static bool xrandrHandleCrtc(XrandrData* data, RRCrtc crtc)
data->result,
(uint32_t) crtcInfo->width,
(uint32_t) crtcInfo->height,
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
data->ffXRRFreeCrtcInfo(crtcInfo);
@ -205,7 +208,8 @@ static bool xrandrHandleMonitor(XrandrData* data, XRRMonitorInfo* monitorInfo)
data->result,
(uint32_t) monitorInfo->width,
(uint32_t) monitorInfo->height,
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
}
@ -257,7 +261,8 @@ static void xrandrHandleScreen(XrandrData* data, Screen* screen)
data->result,
(uint32_t) WidthOfScreen(screen),
(uint32_t) HeightOfScreen(screen),
data->defaultRefreshRate
data->defaultRefreshRate,
-1
);
}

View File

@ -27,6 +27,9 @@ void ffPrintResolution(FFinstance* instance)
if(result->refreshRate > 0)
printf(" @ %iHz", result->refreshRate);
if(result->brightness >= 0)
printf(" (☀️ %d%%)", result->brightness);
putchar('\n');
}
else
@ -34,7 +37,8 @@ void ffPrintResolution(FFinstance* instance)
ffPrintFormat(instance, FF_RESOLUTION_MODULE_NAME, moduleIndex, &instance->config.resolution, FF_RESOLUTION_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_INT, &result->width},
{FF_FORMAT_ARG_TYPE_INT, &result->height},
{FF_FORMAT_ARG_TYPE_INT, &result->refreshRate}
{FF_FORMAT_ARG_TYPE_INT, &result->refreshRate},
{FF_FORMAT_ARG_TYPE_INT, &result->brightness},
});
}
}