From b9c7324aa567b048b570a0fc9f206b9b470f020c Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Thu, 11 Mar 2021 22:15:48 +0100 Subject: [PATCH] resolution options --- completions/bash | 17 ++++++++++++- src/common.c | 13 +++++++--- src/fastfetch.c | 54 ++++++++++++++++++++++++++++++++-------- src/fastfetch.h | 16 ++++++++---- src/modules/packages.c | 4 +-- src/modules/resolution.c | 20 +++++++++++---- 6 files changed, 97 insertions(+), 27 deletions(-) diff --git a/completions/bash b/completions/bash index ef2911913..368f3d338 100644 --- a/completions/bash +++ b/completions/bash @@ -22,6 +22,11 @@ __fastfetch_complete_string() fi } +__fastfetch_complete_path() +{ + COMPREPLY=($(compgen -A file -- "$CURRENT_WORD")) +} + __fastfetch_complete_logo() { COMPREPLY=($(compgen -W "$(fastfetch --list-logos)" -- "$CURRENT_WORD")) @@ -32,6 +37,7 @@ __fastfetch_complete_option() local FF_OPTIONS_ALL=( "${FF_OPTIONS_BOOL[@]}" "${FF_OPTIONS_STRING[@]}" + "${FF_OPTIONS_PATH[@]}" "${FF_OPTIONS_LOGO[@]}" ) @@ -91,11 +97,12 @@ __fastfetch_completion() "--host-version" "--kernel-release" "--kernel-version" - "--shell-path" "--packages-combined" "--packages-combined-names" "--packages-pacman" "--packages-flatpak" + "--shell-path" + "--resolution-refreshrate" "--battery-manufacturer" "--battery-model" "--battery-technology" @@ -112,11 +119,17 @@ __fastfetch_completion() "-x" "--offsetx" "--packages-format" + "--resolution-format" "--battery-format" "--structure" "--set" ) + local FF_OPTIONS_PATH=( + "--resolution-libX11" + "--resolution-libXrandr" + ) + local FF_OPTIONS_LOGO=( "-l" "--logo" @@ -134,6 +147,8 @@ __fastfetch_completion() __fastfetch_complete_bool elif __fastfetch_previous_matches "${FF_OPTIONS_STRING[@]}"; then __fastfetch_complete_string + elif __fastfetch_previous_matches "${FF_OPTIONS_PATH[@]}"; then + __fastfetch_complete_path elif __fastfetch_previous_matches "${FF_OPTIONS_LOGO[@]}"; then __fastfetch_complete_logo else diff --git a/src/common.c b/src/common.c index cd58a1b33..3f0a65ee2 100644 --- a/src/common.c +++ b/src/common.c @@ -35,14 +35,19 @@ void ffDefaultConfig(FFconfig* config) config->kernelShowRelease = true; config->kernelShowVersion = false; - config->shellShowPath = false; - config->packagesCombined = false; config->packagesCombinedNames = true; - config->packagesPacman = true; - config->packagesFlatpak = true; + config->packagesShowPacman = true; + config->packagesShowFlatpak = true; config->packagesFormat[0] = '\0'; + config->shellShowPath = false; + + config->resolutionShowRefreshRate = true; + config->resolutionLibX11[0] = '\0'; + config->resolutionLibXrandr[0] = '\0'; + config->resolutionFormat[0] = '\0'; + config->batteryShowManufacturer = true; config->batteryShowModel = true; config->batteryShowTechnology = true; diff --git a/src/fastfetch.c b/src/fastfetch.c index 4f0ce9401..e509c5dfc 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -41,15 +41,20 @@ "# --kernel-release true\n" \ "# --kernel-version false\n" \ "\n" \ - "## Shell options:\n" \ - "# --shell-path false\n" \ - "\n" \ "## Packages options:\n" \ "# --packages-combined false\n" \ "# --packages-combined-names true\n" \ "# --packages-pacman true\n" \ "# --packages-flatpak true\n" \ "\n" \ + "## Shell options:\n" \ + "# --shell-path false\n" \ + "\n" \ + "## Resolution options:\n" \ + "# --resolution-refreshrate true\n" \ + "# --resolution-libX11 libX11.so\n"\ + "# --resolution-libXrandr libXrandr.so\n" \ + "\n" \ "## Battery options:\n" \ "# --battery-manufacturer true\n" \ "# --battery-model true\n" \ @@ -102,9 +107,6 @@ static inline void printHelp() " --kernel-release : Shows the release of the kernel\n" " --kernel-version : Shows the build version of the kernel\n" "\n" - "Shell options:\n" - " --shell-path : Show the full path of the shell\n" - "\n" "Packages options:\n" " --packages-combined : Show the sum of all packages\n" " --packages-combined-names : Show the names of the package managers after the sum if in packages-combined mode\n" @@ -112,6 +114,15 @@ static inline void printHelp() " --packages-flatpak : Count flatpak packages\n" " --packages-format : Provide the printf format string for packages output (+)\n" "\n" + "Shell options:\n" + " --shell-path : Show the full path of the shell\n" + "\n" + "Resolution optins:\n" + " --resolution-refreshrate : Show the refresh rate of the monitor\n" + " --resolution-libX11 : Set the path to the x11 library to load\n" + " --resolution-libXrandr : Set the path to the xrandr library to load\n" + " --resolution-format : Provide the printf format string for resolution output (+)\n" + "\n" "Battery options:\n" " --battery-manufacturer : Show the manufacturer of the battery, if possible\n" " --battery-model : Show the model of the battery, if possible\n" @@ -167,6 +178,19 @@ static inline void printCommandHelpPackagesFormat() ); } +static inline void printCommandHelpResolutionFormat() +{ + puts( + "usage: fastfetch --resolution-format \n" + "\n" + " is a string of maximum length 32, which is passed to printf as the format string.\n" + "the values passed to printf are in following order:\n" + "width (int), height (int), refreshRate (short)\n" + "if refresh rate is disabled, or could not be determined, zero is passed\n" + "The default value is something like \"%ix%i @ %dHz\"" + ); +} + static inline void printCommandHelp(const char* command) { if(strcasecmp(command, "c") == 0 || strcasecmp(command, "color") == 0) @@ -175,6 +199,8 @@ static inline void printCommandHelp(const char* command) printCommandHelpBatteryFormat(); else if(strcasecmp(command, "packages-format") == 0) printCommandHelpPackagesFormat(); + else if(strcasecmp(command, "resolution-format") == 0) + printCommandHelpResolutionFormat(); else printf("No specific help for command %s provided\n", command); } @@ -384,8 +410,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con instance->config.kernelShowRelease = optionParseBoolean(value); else if(strcasecmp(key, "--kernel-version") == 0) instance->config.kernelShowVersion = optionParseBoolean(value); - else if(strcasecmp(key, "--shell-path") == 0) - instance->config.shellShowPath = optionParseBoolean(value); else if(strcasecmp(key, "--packages-combined") == 0) instance->config.packagesCombined = optionParseBoolean(value); else if(strcasecmp(key, "--packages-combined-names") == 0) @@ -394,11 +418,21 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con instance->config.packagesCombinedNames = optionParseBoolean(value); } else if(strcasecmp(key, "--packages-pacman") == 0) - instance->config.packagesPacman = optionParseBoolean(value); + instance->config.packagesShowPacman = optionParseBoolean(value); else if(strcasecmp(key, "--packages-flatpak") == 0) - instance->config.packagesFlatpak = optionParseBoolean(value); + instance->config.packagesShowFlatpak = optionParseBoolean(value); else if(strcasecmp(key, "--packages-format") == 0) optionParseString(key, value, instance->config.packagesFormat, sizeof(instance->config.packagesFormat)); + else if(strcasecmp(key, "--shell-path") == 0) + instance->config.shellShowPath = optionParseBoolean(value); + else if(strcasecmp(key, "--resolution-refreshrate") == 0) + instance->config.resolutionShowRefreshRate = optionParseBoolean(value); + else if(strcasecmp(key, "--resolution-libX11") == 0) + optionParseString(key, value, instance->config.resolutionLibX11, sizeof(instance->config.resolutionLibX11)); + else if(strcasecmp(key, "--resolution-libXrandr") == 0) + optionParseString(key, value, instance->config.resolutionLibXrandr, sizeof(instance->config.resolutionLibXrandr)); + else if(strcasecmp(key, "--resolution-format") == 0) + optionParseString(key, value, instance->config.resolutionFormat, sizeof(instance->config.resolutionFormat)); else if(strcasecmp(key, "--battery-manufacturer") == 0) instance->config.batteryShowManufacturer = optionParseBoolean(value); else if(strcasecmp(key, "--battery-model") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 9b379f5dc..d5479046a 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -53,16 +53,22 @@ typedef struct FFconfig bool kernelShowRelease; bool kernelShowVersion; - //Shell - bool shellShowPath; - //Packages bool packagesCombined; bool packagesCombinedNames; - bool packagesPacman; - bool packagesFlatpak; + bool packagesShowPacman; + bool packagesShowFlatpak; char packagesFormat[32]; + //Shell + bool shellShowPath; + + //Resolution + bool resolutionShowRefreshRate; + char resolutionLibX11[64]; + char resolutionLibXrandr[64]; + char resolutionFormat[32]; + //Battery bool batteryShowManufacturer; bool batteryShowModel; diff --git a/src/modules/packages.c b/src/modules/packages.c index 9e501478a..4646d049a 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -25,8 +25,8 @@ static uint32_t get_num_dirs(const char* dirname) { void ffPrintPackages(FFinstance* instance) { - uint32_t pacman = instance->config.packagesPacman ? get_num_dirs("/var/lib/pacman/local") : 0; - uint32_t flatpak = instance->config.packagesFlatpak ? get_num_dirs("/var/lib/flatpak/app") : 0; + uint32_t pacman = instance->config.packagesShowPacman ? get_num_dirs("/var/lib/pacman/local") : 0; + uint32_t flatpak = instance->config.packagesShowFlatpak ? get_num_dirs("/var/lib/flatpak/app") : 0; uint32_t all = pacman + flatpak; diff --git a/src/modules/resolution.c b/src/modules/resolution.c index 1d1b5313d..645faeeb5 100644 --- a/src/modules/resolution.c +++ b/src/modules/resolution.c @@ -3,9 +3,13 @@ #include "X11/extensions/Xrandr.h" #include "dlfcn.h" -static short getCurrentRate(Display* display) +static short getCurrentRate(FFinstance* instance, Display* display) { - void* xrandr = dlopen("libXrandr.so", RTLD_LAZY); + void* xrandr; + if(instance->config.resolutionLibXrandr[0] != '\0') + xrandr = dlopen(instance->config.resolutionLibXrandr, RTLD_LAZY); + else + xrandr = dlopen("libXrandr.so", RTLD_LAZY); if(xrandr == NULL) return 0; @@ -39,7 +43,11 @@ void ffPrintResolution(FFinstance* instance) if(ffPrintCachedValue(instance, "Resolution")) return; - void* x11 = dlopen("libX11.so", RTLD_LAZY); + void* x11; + if(instance->config.resolutionLibX11[0] != '\0') + x11 = dlopen(instance->config.resolutionLibX11, RTLD_LAZY); + else + x11 = dlopen("libX11.so", RTLD_LAZY); if(x11 == NULL) { ffPrintError(instance, "Resolution", "dlopen(\"libX11.so\", RTLD_LAZY) == NULL"); @@ -62,13 +70,15 @@ void ffPrintResolution(FFinstance* instance) Screen* screen = DefaultScreenOfDisplay(display); - short currentRate = getCurrentRate(display); + short currentRate = instance->config.resolutionShowRefreshRate ? getCurrentRate(instance, display) : 0; dlclose(x11); char resolution[1024]; - if(currentRate == 0) + if(instance->config.resolutionFormat[0] != '\0') + sprintf(resolution, instance->config.resolutionFormat, screen->width, screen->height, currentRate); + else if(currentRate == 0) sprintf(resolution, "%ix%i", screen->width, screen->height); else sprintf(resolution, "%ix%i @ %dHz", screen->width, screen->height, currentRate);