From d9a022ffff15d9ecfd565fa5b53f800930837ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 9 Jan 2023 18:59:02 +0800 Subject: [PATCH] Brightness: add new module (macOS) It turns out that merging brightness module into resolution module is a bad idea. --- CHANGELOG.md | 1 + CMakeLists.txt | 6 ++ README.md | 2 +- src/common/format.c | 2 + src/common/format.h | 1 + src/common/init.c | 1 + src/data/config_user.txt | 3 + src/detection/brightness/brightness.h | 17 +++++ src/detection/brightness/brightness_apple.c | 33 ++++++++++ .../brightness/brightness_nosupport.c | 6 ++ src/fastfetch.c | 3 + src/fastfetch.h | 4 +- src/flashfetch.c | 1 + src/modules/brightness.c | 62 +++++++++++++++++++ 14 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/detection/brightness/brightness.h create mode 100644 src/detection/brightness/brightness_apple.c create mode 100644 src/detection/brightness/brightness_nosupport.c create mode 100644 src/modules/brightness.c diff --git a/CHANGELOG.md b/CHANGELOG.md index f11a7e894..fc2eed5a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Features: * Look for config files in `~/Library/Preferences` (macOS) * Add `--list-config-paths` option which list search paths of config files * Add `--list-data-paths` option which list search paths for presets and logos +* Add `Brightness` module support Logos: * Raspbian (@IamNoRobot, #373) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb39d390a..3a1f122a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,6 +254,7 @@ set(LIBFASTFETCH_SRC src/modules/battery.c src/modules/bios.c src/modules/board.c + src/modules/brightness.c src/modules/break.c src/modules/chassis.c src/modules/colors.c @@ -309,6 +310,7 @@ if(LINUX) src/detection/battery/battery_linux.c src/detection/bios/bios_linux.c src/detection/board/board_linux.c + src/detection/brightness/brightness_nosupport.c src/detection/chassis/chassis_linux.c src/detection/cpu/cpu_linux.c src/detection/cpuUsage/cpuUsage_linux.c @@ -348,6 +350,7 @@ elseif(ANDROID) src/detection/battery/battery_nosupport.c src/detection/bios/bios_nosupport.c src/detection/board/board_nosupport.c + src/detection/brightness/brightness_nosupport.c src/detection/chassis/chassis_nosupport.c src/detection/cpu/cpu_linux.c src/detection/cursor/cursor_nosupport.c @@ -382,6 +385,7 @@ elseif(BSD) src/detection/battery/battery_nosupport.c src/detection/bios/bios_bsd.c src/detection/board/board_nosupport.c + src/detection/brightness/brightness_nosupport.c src/detection/chassis/chassis_nosupport.c src/detection/cpu/cpu_bsd.c src/detection/cpuUsage/cpuUsage_bsd.c @@ -422,6 +426,7 @@ elseif(APPLE) src/detection/battery/battery_apple.c src/detection/bios/bios_apple.c src/detection/board/board_nosupport.c + src/detection/brightness/brightness_apple.c src/detection/chassis/chassis_nosupport.c src/detection/cpu/cpu_apple.c src/detection/cpuUsage/cpuUsage_apple.c @@ -458,6 +463,7 @@ elseif(WIN32) src/detection/battery/battery_windows.c src/detection/bios/bios_windows.c src/detection/board/board_windows.c + src/detection/brightness/brightness_nosupport.c src/detection/chassis/chassis_nosupport.c src/detection/cpu/cpu_windows.c src/detection/cpuUsage/cpuUsage_windows.c diff --git a/README.md b/README.md index 0957e7ec6..78312f6a8 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ All categories not listed here should work without needing a specific implementa ##### Available Modules ``` -Title, Separator, OS, Host, Bios, Board, Kernel, Uptime, Processes, Packages, Shell, Resolution, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Swap, Disk, Battery, Power Adapter, Player, Media, Vulkan, OpenGL, OpenCL, LocalIP, PublicIP, Wifi, DateTime, Date, Time, Locale, Colors, Break, Custom +Title, Separator, OS, Host, Bios, Board, Kernel, Uptime, Processes, Packages, Shell, Resolution, Brightness, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Swap, Disk, Battery, Power Adapter, Player, Media, Vulkan, OpenGL, OpenCL, LocalIP, PublicIP, Wifi, DateTime, Date, Time, Locale, Colors, Break, Custom ``` ##### Builtin logos diff --git a/src/common/format.c b/src/common/format.c index b491713cf..1a4a688c5 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -19,6 +19,8 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg) ffStrbufAppendS(buffer, (const char*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_STRBUF) ffStrbufAppend(buffer, (FFstrbuf*)formatarg->value); + else if(formatarg->type == FF_FORMAT_ARG_TYPE_FLOAT) + ffStrbufAppendF(buffer, "%f", *(float*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_DOUBLE) ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_BOOL) diff --git a/src/common/format.h b/src/common/format.h index d860dbd29..cc7e29e6d 100644 --- a/src/common/format.h +++ b/src/common/format.h @@ -12,6 +12,7 @@ typedef enum FFformatargtype FF_FORMAT_ARG_TYPE_INT, FF_FORMAT_ARG_TYPE_STRING, FF_FORMAT_ARG_TYPE_STRBUF, + FF_FORMAT_ARG_TYPE_FLOAT, FF_FORMAT_ARG_TYPE_DOUBLE, FF_FORMAT_ARG_TYPE_LIST, FF_FORMAT_ARG_TYPE_BOOL diff --git a/src/common/init.c b/src/common/init.c index 3cf2287bd..59aee5fb1 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -247,6 +247,7 @@ static void defaultConfig(FFinstance* instance) initModuleArg(&instance->config.host); initModuleArg(&instance->config.bios); initModuleArg(&instance->config.board); + initModuleArg(&instance->config.brightness); initModuleArg(&instance->config.chassis); initModuleArg(&instance->config.kernel); initModuleArg(&instance->config.uptime); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index a76e24686..2afeba65e 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -230,6 +230,7 @@ #--packages-key Packages #--shell-key Shell #--resolution-key Resolution {1} +#--brightness-key Brightness ({1}) #--de-key DE #--wm-key WM #--wm-theme-key WM Theme @@ -274,6 +275,7 @@ #--packages-format #--shell-format #--resolution-format +#--brightness-format #--de-format #--wm-format #--wm-theme-format @@ -317,6 +319,7 @@ #--packages-error #--shell-error #--resolution-error +#--brightness-error #--de-error #--wm-error #--wm-theme-error diff --git a/src/detection/brightness/brightness.h b/src/detection/brightness/brightness.h new file mode 100644 index 000000000..d65843947 --- /dev/null +++ b/src/detection/brightness/brightness.h @@ -0,0 +1,17 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_brightness_brightness +#define FF_INCLUDED_detection_brightness_brightness + +#include "fastfetch.h" +#include "util/FFlist.h" + +typedef struct FFBrightnessResult +{ + FFstrbuf name; + float value; +} FFBrightnessResult; + +const char* ffDetectBrightness(FFlist* result); // list of FFBrightnessResult + +#endif diff --git a/src/detection/brightness/brightness_apple.c b/src/detection/brightness/brightness_apple.c new file mode 100644 index 000000000..e3a93a5ea --- /dev/null +++ b/src/detection/brightness/brightness_apple.c @@ -0,0 +1,33 @@ +#include "brightness.h" + +#include + +extern int DisplayServicesGetBrightness(CGDirectDisplayID display, float *brightness) __attribute__((weak_import)); + +const char* ffDetectBrightness(FFlist* result) +{ + if(DisplayServicesGetBrightness == NULL) + return "DisplayServices function DisplayServicesGetBrightness is not available"; + + CGDirectDisplayID screens[128]; + uint32_t screenCount; + if(CGGetOnlineDisplayList(sizeof(screens) / sizeof(screens[0]), screens, &screenCount) != kCGErrorSuccess) + return "CGGetOnlineDisplayList() failed"; + + for(uint32_t i = 0; i < screenCount; i++) + { + CGDirectDisplayID screen = screens[i]; + float brightness; + if(DisplayServicesGetBrightness(screen, &brightness) != kCGErrorSuccess) + continue; + + FFBrightnessResult* display = (FFBrightnessResult*) ffListAdd(result); + if(CGDisplayIsBuiltin(screen)) + ffStrbufInitS(&display->name, "built-in"); + else + ffStrbufInitF(&display->name, "external-%u", (unsigned) screen); + display->value = brightness * 100; + } + + return NULL; +} diff --git a/src/detection/brightness/brightness_nosupport.c b/src/detection/brightness/brightness_nosupport.c new file mode 100644 index 000000000..5661c180f --- /dev/null +++ b/src/detection/brightness/brightness_nosupport.c @@ -0,0 +1,6 @@ +#include "brightness.h" + +const char* ffDetectBrightness(FF_UNUSED_PARAM FFlist* result) +{ + return "Not supported on this platform"; +} diff --git a/src/fastfetch.c b/src/fastfetch.c index 8de17b9f0..ad63f26a3 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1146,6 +1146,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(optionParseModuleArgs(key, value, "packages", &instance->config.packages)) {} else if(optionParseModuleArgs(key, value, "shell", &instance->config.shell)) {} else if(optionParseModuleArgs(key, value, "resolution", &instance->config.resolution)) {} + else if(optionParseModuleArgs(key, value, "brightness", &instance->config.brightness)) {} else if(optionParseModuleArgs(key, value, "de", &instance->config.de)) {} else if(optionParseModuleArgs(key, value, "wifi", &instance->config.wifi)) {} else if(optionParseModuleArgs(key, value, "wm", &instance->config.wm)) {} @@ -1362,6 +1363,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintBios(instance); else if(strcasecmp(line, "board") == 0) ffPrintBoard(instance); + else if(strcasecmp(line, "brightness") == 0) + ffPrintBrightness(instance); else if(strcasecmp(line, "chassis") == 0) ffPrintChassis(instance); else if(strcasecmp(line, "kernel") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index fa1b11d09..0572ef294 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -106,8 +106,9 @@ typedef struct FFconfig FFModuleArgs os; FFModuleArgs host; FFModuleArgs bios; - FFModuleArgs chassis; FFModuleArgs board; + FFModuleArgs brightness; + FFModuleArgs chassis; FFModuleArgs kernel; FFModuleArgs uptime; FFModuleArgs processes; @@ -273,6 +274,7 @@ void ffPrintProcesses(FFinstance* instance); void ffPrintPackages(FFinstance* instance); void ffPrintShell(FFinstance* instance); void ffPrintResolution(FFinstance* instance); +void ffPrintBrightness(FFinstance* instance); void ffPrintDesktopEnvironment(FFinstance* instance); void ffPrintWM(FFinstance* instance); void ffPrintWMTheme(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index 196e94bb5..86729ace6 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -31,6 +31,7 @@ int main(int argc, char** argv) ffPrintPackages(&instance); ffPrintShell(&instance); ffPrintResolution(&instance); + // ffPrintBrightness(&instance); ffPrintDesktopEnvironment(&instance); ffPrintWM(&instance); ffPrintWMTheme(&instance); diff --git a/src/modules/brightness.c b/src/modules/brightness.c new file mode 100644 index 000000000..edede6543 --- /dev/null +++ b/src/modules/brightness.c @@ -0,0 +1,62 @@ +#include "fastfetch.h" +#include "common/printing.h" +#include "detection/brightness/brightness.h" + +#define FF_BRIGHTNESS_MODULE_NAME "Brightness" +#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2 + +void ffPrintBrightness(FFinstance* instance) +{ + FFlist result; + ffListInit(&result, sizeof(FFBrightnessResult)); + const char* error = ffDetectBrightness(&result); + + if(error) + { + ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "%s", error); + goto exit; + } + + if(result.length == 0) + { + ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "No result is detected."); + goto exit; + } + + FFstrbuf key; + ffStrbufInit(&key); + + FF_LIST_FOR_EACH(FFBrightnessResult, item, result) + { + if(instance->config.brightness.key.length == 0) + { + ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars); + } + else + { + ffParseFormatString(&key, &instance->config.brightness.key, 1, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &item->name} + }); + } + + if(instance->config.brightness.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + printf("%.0f%%\n", item->value); + } + else + { + ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.brightness.outputFormat, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_STRBUF, &item->value}, + {FF_FORMAT_ARG_TYPE_FLOAT, &item->name} + }); + } + + ffStrbufDestroy(&item->name); + } + + ffStrbufDestroy(&key); + +exit: + ffListDestroy(&result); +}