From df84721b5587fcd678ed59073dcdb5e1a44a1a10 Mon Sep 17 00:00:00 2001 From: CSoellinger Date: Wed, 1 Feb 2023 10:01:47 +0100 Subject: [PATCH 01/15] fix: add another mutter detection for linux mint 21.1 (cinnamon) --- src/detection/displayserver/linux/wmde.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index 33d03a20a..aaad2b938 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -70,7 +70,8 @@ static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* proce strcasecmp(processName, "gnome-shell") == 0 || strcasecmp(processName, "gnome shell") == 0 || strcasecmp(processName, "gnome-session-binary") == 0 || - strcasecmp(processName, "Mutter") == 0 + strcasecmp(processName, "Mutter") == 0 || + strcasecmp(processName, "Mutter (Muffin)") == 0 ) ffStrbufSetS(&result->wmPrettyName, FF_WM_PRETTY_MUTTER); else if( strcasecmp(processName, "cinnamon-session") == 0 || From 7a2826358ba350d090c5f64f7510d9c408fd33c5 Mon Sep 17 00:00:00 2001 From: CSoellinger Date: Wed, 1 Feb 2023 10:08:08 +0100 Subject: [PATCH 02/15] fix: spell correction --- src/detection/terminalfont/terminalfont.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/detection/terminalfont/terminalfont.c b/src/detection/terminalfont/terminalfont.c index 1d3503da2..d4c1359ad 100644 --- a/src/detection/terminalfont/terminalfont.c +++ b/src/detection/terminalfont/terminalfont.c @@ -385,7 +385,7 @@ const FFTerminalFontResult* ffDetectTerminalFont(const FFinstance* instance) const FFTerminalShellResult* terminalShell = ffDetectTerminalShell(instance); if(terminalShell->terminalProcessName.length == 0) - ffStrbufAppendS(&result.error, "Terminal font needs successfull terminal detection"); + ffStrbufAppendS(&result.error, "Terminal font needs successful terminal detection"); else if(!detectTerminalFontCommon(instance, terminalShell, &result)) ffDetectTerminalFontPlatform(instance, terminalShell, &result); From 7ee4a648d00fdb5a256e3e15cef99158c57fa1e3 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 1 Feb 2023 10:43:24 +0100 Subject: [PATCH 03/15] Small fix to muffin theme detection --- src/detection/wmtheme/wmtheme_linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/detection/wmtheme/wmtheme_linux.c b/src/detection/wmtheme/wmtheme_linux.c index d87757567..87a8437c1 100644 --- a/src/detection/wmtheme/wmtheme_linux.c +++ b/src/detection/wmtheme/wmtheme_linux.c @@ -104,11 +104,13 @@ static bool detectMuffin(FFinstance* instance, FFstrbuf* themeOrError) ffStrbufAppendS(themeOrError, theme); return true; } + if(theme == NULL) { - ffStrbufAppendS(themeOrError, theme); + ffStrbufAppendS(themeOrError, name); return true; } + ffStrbufAppendF(themeOrError, "%s (%s)", name, theme); return true; } From 55da030e2a5fcbb6eb55652b9a3bb90b77f1ae9c Mon Sep 17 00:00:00 2001 From: CSoellinger Date: Wed, 1 Feb 2023 10:50:31 +0100 Subject: [PATCH 04/15] fix: move mutter(muffin) detection to get FF_WM_PRETTY_MUFFIN back --- src/detection/displayserver/linux/wmde.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index aaad2b938..71b1d1474 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -70,12 +70,12 @@ static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* proce strcasecmp(processName, "gnome-shell") == 0 || strcasecmp(processName, "gnome shell") == 0 || strcasecmp(processName, "gnome-session-binary") == 0 || - strcasecmp(processName, "Mutter") == 0 || - strcasecmp(processName, "Mutter (Muffin)") == 0 + strcasecmp(processName, "Mutter") == 0 ) ffStrbufSetS(&result->wmPrettyName, FF_WM_PRETTY_MUTTER); else if( strcasecmp(processName, "cinnamon-session") == 0 || - strcasecmp(processName, "Muffin") == 0 + strcasecmp(processName, "Muffin") == 0 || + strcasecmp(processName, "Mutter (Muffin)") == 0 ) ffStrbufSetS(&result->wmPrettyName, FF_WM_PRETTY_MUFFIN); else if(strcasecmp(processName, "sway") == 0) ffStrbufSetS(&result->wmPrettyName, FF_WM_PRETTY_SWAY); From 1b23c9a030f15f921c5ff54f8622204a18248dfe Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 1 Feb 2023 11:14:54 +0100 Subject: [PATCH 05/15] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c7af9af4..98e1839c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Features: Bugfixes: * Fix a rare case that fails to detect terminal +* Fix Muffin detection (@Zerogiven, #412) Other: From 619ec921ee2dba595c308464cb3458f61d395f67 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 1 Feb 2023 11:16:01 +0100 Subject: [PATCH 06/15] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98e1839c3..e6cc76664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ Features: Bugfixes: * Fix a rare case that fails to detect terminal -* Fix Muffin detection (@Zerogiven, #412) +* Fix Muffin detection (@Zerogiven, #411) Other: From a74cbc5781f36e1bcf4aa95391190b7e13f008ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 13:59:21 +0800 Subject: [PATCH 07/15] Sound: fix memleak, update config file --- src/common/init.c | 1 + src/data/config_user.txt | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index 0125c4b75..42734a1a3 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -385,6 +385,7 @@ static void destroyConfig(FFinstance* instance) destroyModuleArg(&instance->config.openCL); destroyModuleArg(&instance->config.users); destroyModuleArg(&instance->config.bluetooth); + destroyModuleArg(&instance->config.sound); ffStrbufDestroy(&instance->config.libPCI); ffStrbufDestroy(&instance->config.libVulkan); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index e4fcef289..3938e0783 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -255,9 +255,9 @@ # Sound show all option # Sets if all sound devices should be printed -# Must be either true or false -# Default is false ( only main device should be printed ). -#--sound-show-all false +# Must be either main, active or all. Default is main +# Default is main. +#--sound-type main # Percentage output type option # Applies to all modules that prints percentage values. Currently memory, swap, disk, battery and CPU usage are supported. From 7fc2c4dcf1e5afd3ae9dc891f1b4778ded3a6af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 14:01:39 +0800 Subject: [PATCH 08/15] Gamepad: init support for Windows --- CMakeLists.txt | 7 ++++ presets/all | 2 +- src/common/init.c | 2 + src/data/config_user.txt | 3 ++ src/detection/gamepad/gamepad.h | 16 ++++++++ src/detection/gamepad/gamepad_nosupport.c | 6 +++ src/detection/gamepad/gamepad_windows.cpp | 32 +++++++++++++++ src/fastfetch.c | 10 +++++ src/fastfetch.h | 2 + src/modules/gamepad.c | 48 +++++++++++++++++++++++ 10 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/detection/gamepad/gamepad.h create mode 100644 src/detection/gamepad/gamepad_nosupport.c create mode 100644 src/detection/gamepad/gamepad_windows.cpp create mode 100644 src/modules/gamepad.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a2e5c3957..3a97a77fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,6 +274,7 @@ set(LIBFASTFETCH_SRC src/modules/gpu.c src/modules/host.c src/modules/icons.c + src/modules/gamepad.c src/modules/kernel.c src/modules/locale.c src/modules/localip.c @@ -335,6 +336,7 @@ if(LINUX) src/detection/gtk.c src/detection/host/host_linux.c src/detection/localip/localip_linux.c + src/detection/gamepad/gamepad_nosupport.c src/detection/media/media_linux.c src/detection/memory/memory_linux.c src/detection/opengl/opengl_linux.c @@ -374,6 +376,7 @@ elseif(ANDROID) src/detection/gpu/gpu_nosupport.c src/detection/host/host_android.c src/detection/localip/localip_linux.c + src/detection/gamepad/gamepad_nosupport.c src/detection/media/media_nosupport.c src/detection/memory/memory_linux.c src/detection/opengl/opengl_linux.c @@ -419,6 +422,7 @@ elseif(BSD) src/detection/gtk.c src/detection/host/host_bsd.c src/detection/localip/localip_linux.c + src/detection/gamepad/gamepad_nosupport.c src/detection/media/media_linux.c src/detection/memory/memory_bsd.c src/detection/opengl/opengl_linux.c @@ -460,6 +464,7 @@ elseif(APPLE) src/detection/gpu/gpu_apple.c src/detection/host/host_apple.c src/detection/localip/localip_linux.c + src/detection/gamepad/gamepad_nosupport.c src/detection/media/media_apple.m src/detection/memory/memory_apple.c src/detection/opengl/opengl_apple.c @@ -500,6 +505,7 @@ elseif(WIN32) src/detection/gpu/gpu_windows.cpp src/detection/host/host_windows.c src/detection/localip/localip_windows.c + src/detection/gamepad/gamepad_windows.cpp src/detection/media/media_nosupport.c src/detection/memory/memory_windows.c src/detection/opengl/opengl_windows.c @@ -712,6 +718,7 @@ elseif(WIN32) PRIVATE "version" PRIVATE "setupapi" PRIVATE "dxgi" + PRIVATE "dinput8" PRIVATE "wtsapi32" PRIVATE "powrprof" ) diff --git a/presets/all b/presets/all index dfcaccc44..e8b10e197 100644 --- a/presets/all +++ b/presets/all @@ -1 +1 @@ ---structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Weather:Break:Colors +--structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Gamepad:Weather:Break:Colors diff --git a/src/common/init.c b/src/common/init.c index 42734a1a3..592ba17df 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -117,6 +117,7 @@ static void defaultConfig(FFinstance* instance) initModuleArg(&instance->config.users); initModuleArg(&instance->config.bluetooth); initModuleArg(&instance->config.sound); + initModuleArg(&instance->config.gamepad); ffStrbufInitA(&instance->config.libPCI, 0); ffStrbufInitA(&instance->config.libVulkan, 0); @@ -386,6 +387,7 @@ static void destroyConfig(FFinstance* instance) destroyModuleArg(&instance->config.users); destroyModuleArg(&instance->config.bluetooth); destroyModuleArg(&instance->config.sound); + destroyModuleArg(&instance->config.gamepad); ffStrbufDestroy(&instance->config.libPCI); ffStrbufDestroy(&instance->config.libVulkan); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index 3938e0783..62aef8ad1 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -309,6 +309,7 @@ #--users-key Users #--bluetooth-key Bluetooth #--sound-key Sound +#--gamepad-key Gamepad # Format options: # Sets the format string for module values. @@ -355,6 +356,7 @@ #--users-format #--bluetooth-format #--sound-format +#--gamepad-format # Error options: # Sets the format string to use if an error occured @@ -401,6 +403,7 @@ #--users-error #--bluetooth-error #--sound-error +#--gamepad-error # Library options: # Sets an user specific path to a library to load. diff --git a/src/detection/gamepad/gamepad.h b/src/detection/gamepad/gamepad.h new file mode 100644 index 000000000..1def1b430 --- /dev/null +++ b/src/detection/gamepad/gamepad.h @@ -0,0 +1,16 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_gamepad_gamepad +#define FF_INCLUDED_detection_gamepad_gamepad + +#include "fastfetch.h" + +typedef struct FFGamepadDevice +{ + FFstrbuf identifier; + FFstrbuf name; +} FFGamepadDevice; + +const char* ffDetectGamepad(const FFinstance* instance, FFlist* devices /* List of FFGamepadDevice */); + +#endif diff --git a/src/detection/gamepad/gamepad_nosupport.c b/src/detection/gamepad/gamepad_nosupport.c new file mode 100644 index 000000000..d921d3552 --- /dev/null +++ b/src/detection/gamepad/gamepad_nosupport.c @@ -0,0 +1,6 @@ +#include "gamepad.h" + +const char* ffDetectGamepad(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_UNUSED FFlist* devices /* List of FFGamepadDevice */) +{ + return "Not supported on this platform"; +} diff --git a/src/detection/gamepad/gamepad_windows.cpp b/src/detection/gamepad/gamepad_windows.cpp new file mode 100644 index 000000000..c30ef9fe4 --- /dev/null +++ b/src/detection/gamepad/gamepad_windows.cpp @@ -0,0 +1,32 @@ +extern "C" { + #include "gamepad.h" +} + +#include +#define DIRECTINPUT_VERSION 0x0800 +#include + +CALLBACK BOOL DIEnumDevicesCallback(LPCDIDEVICEINSTANCEA lpddi, LPVOID pvRef) +{ + FFlist* devices = (FFlist*) pvRef; + + FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + ffStrbufInitF(&device->identifier, "{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}", + lpddi->guidInstance.Data1, lpddi->guidInstance.Data2, lpddi->guidInstance.Data3, + lpddi->guidInstance.Data4[0], lpddi->guidInstance.Data4[1], lpddi->guidInstance.Data4[2], lpddi->guidInstance.Data4[3], + lpddi->guidInstance.Data4[4], lpddi->guidInstance.Data4[5], lpddi->guidInstance.Data4[6], lpddi->guidInstance.Data4[7]); + ffStrbufInitS(&device->name, lpddi->tszInstanceName); + + return DIENUM_CONTINUE; +} + +extern "C" +const char* ffDetectGamepad(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* List of FFGamepadDevice */) +{ + IDirectInput8A* dinput; + if (!SUCCEEDED(DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8A, (void**)&dinput, NULL))) + return "DirectInput8Create() failed"; + dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, devices, DIEDFL_ALLDEVICES); + dinput->Release(); + return NULL; +} diff --git a/src/fastfetch.c b/src/fastfetch.c index ded937466..fb6d0fc27 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -463,6 +463,13 @@ static inline void printCommandHelp(const char* command) "Identifier" ); } + else if(strcasecmp(command, "gamepad-format") == 0) + { + constructAndPrintCommandHelpFormat("gamepad", "{1}", 1, + "Name", + "Identifier" + ); + } else fprintf(stderr, "No specific help for command %s provided\n", command); } @@ -1166,6 +1173,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(optionParseModuleArgs(key, value, "users", &instance->config.users)) {} else if(optionParseModuleArgs(key, value, "bluetooth", &instance->config.bluetooth)) {} else if(optionParseModuleArgs(key, value, "sound", &instance->config.sound)) {} + else if(optionParseModuleArgs(key, value, "gamepad", &instance->config.gamepad)) {} /////////////////// //Library options// @@ -1478,6 +1486,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintBluetooth(instance); else if(strcasecmp(line, "sound") == 0) ffPrintSound(instance); + else if(strcasecmp(line, "gamepad") == 0) + ffPrintGamepad(instance); else ffPrintErrorString(instance, line, 0, NULL, NULL, ""); } diff --git a/src/fastfetch.h b/src/fastfetch.h index 0d57902c6..b625611b1 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -148,6 +148,7 @@ typedef struct FFconfig FFModuleArgs users; FFModuleArgs bluetooth; FFModuleArgs sound; + FFModuleArgs gamepad; FFstrbuf libPCI; FFstrbuf libVulkan; @@ -326,5 +327,6 @@ void ffPrintUsers(FFinstance* instance); void ffPrintCommand(FFinstance* instance); void ffPrintBluetooth(FFinstance* instance); void ffPrintSound(FFinstance* instance); +void ffPrintGamepad(FFinstance* instance); #endif diff --git a/src/modules/gamepad.c b/src/modules/gamepad.c new file mode 100644 index 000000000..7b935a972 --- /dev/null +++ b/src/modules/gamepad.c @@ -0,0 +1,48 @@ +#include "common/printing.h" +#include "detection/gamepad/gamepad.h" + +#define FF_GAMEPAD_MODULE_NAME "Gamepad" +#define FF_GAMEPAD_NUM_FORMAT_ARGS 2 + +static void printDevice(FFinstance* instance, const FFGamepadDevice* device, uint8_t index) +{ + if(instance->config.sound.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_GAMEPAD_MODULE_NAME, index, &instance->config.sound.key); + ffStrbufPutTo(&device->name, stdout); + } + else + { + ffPrintFormat(instance, FF_GAMEPAD_MODULE_NAME, index, &instance->config.sound, FF_GAMEPAD_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_STRBUF, &device->name}, + {FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier}, + }); + } +} + +void ffPrintGamepad(FFinstance* instance) +{ + FF_LIST_AUTO_DESTROY result; + ffListInit(&result, sizeof(FFGamepadDevice)); + const char* error = ffDetectGamepad(instance, &result); + + if(error) + { + ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &instance->config.sound, "%s", error); + return; + } + + if(!result.length) + { + ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &instance->config.sound, "No devices detected"); + return; + } + + uint8_t index = 0; + FF_LIST_FOR_EACH(FFGamepadDevice, device, result) + { + printDevice(instance, device, result.length > 1 ? ++index : 0); + ffStrbufDestroy(&device->identifier); + ffStrbufDestroy(&device->name); + } +} From 0b8e8ad4f2067313d677d3c05b65b404670a8838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 20:23:46 +0800 Subject: [PATCH 09/15] Gamepad (macOS): add support --- CMakeLists.txt | 2 +- src/detection/gamepad/gamepad_apple.c | 41 +++++++++++++++++++++++++++ src/util/apple/cf_helpers.h | 13 +++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/detection/gamepad/gamepad_apple.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a97a77fe..27853f760 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -464,7 +464,7 @@ elseif(APPLE) src/detection/gpu/gpu_apple.c src/detection/host/host_apple.c src/detection/localip/localip_linux.c - src/detection/gamepad/gamepad_nosupport.c + src/detection/gamepad/gamepad_apple.c src/detection/media/media_apple.m src/detection/memory/memory_apple.c src/detection/opengl/opengl_apple.c diff --git a/src/detection/gamepad/gamepad_apple.c b/src/detection/gamepad/gamepad_apple.c new file mode 100644 index 000000000..154f1c587 --- /dev/null +++ b/src/detection/gamepad/gamepad_apple.c @@ -0,0 +1,41 @@ +#include "gamepad.h" +#include "util/apple/cf_helpers.h" + +#include +#include + +static void enumSet(const void* value, void *context) +{ + CFStringRef product = IOHIDDeviceGetProperty((IOHIDDeviceRef) value, CFSTR(kIOHIDProductKey)); + CFStringRef serialNumber = IOHIDDeviceGetProperty((IOHIDDeviceRef) value, CFSTR(kIOHIDSerialNumberKey)); + FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd((FFlist*) context); + ffStrbufInit(&device->identifier); + ffCfStrGetString(serialNumber, &device->identifier); + ffStrbufInit(&device->name); + ffCfStrGetString(product, &device->name); +} + +const char* ffDetectGamepad(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* List of FFGamepadDevice */) +{ + IOHIDManagerRef FF_CFTYPE_AUTO_RELEASE manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); + if (IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone) != kIOReturnSuccess) + return "IOHIDManagerOpen() failed"; + + CFNumberRef FF_CFTYPE_AUTO_RELEASE genericDesktop = ffCfCreateInt(kHIDPage_GenericDesktop); + CFNumberRef FF_CFTYPE_AUTO_RELEASE gamePad = ffCfCreateInt(kHIDUsage_GD_GamePad); + CFDictionaryRef FF_CFTYPE_AUTO_RELEASE matching = CFDictionaryCreate(kCFAllocatorDefault, (const void **)(CFStringRef[]){ + CFSTR(kIOHIDDeviceUsagePageKey), + CFSTR(kIOHIDDeviceUsageKey) + }, (const void **)(CFNumberRef[]){ + genericDesktop, + gamePad + }, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + IOHIDManagerSetDeviceMatching(manager, matching); + + CFSetRef FF_CFTYPE_AUTO_RELEASE set = IOHIDManagerCopyDevices(manager); + if (set) + CFSetApplyFunction(set, &enumSet, devices); + IOHIDManagerClose(manager, 0); + + return NULL; +} diff --git a/src/util/apple/cf_helpers.h b/src/util/apple/cf_helpers.h index d3cb66d43..52755bb8b 100644 --- a/src/util/apple/cf_helpers.h +++ b/src/util/apple/cf_helpers.h @@ -13,4 +13,17 @@ const char* ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result) const char* ffCfDictGetInt(CFDictionaryRef dict, CFStringRef key, int* result); const char* ffCfDictGetDict(CFDictionaryRef dict, CFStringRef key, CFDictionaryRef* result); +static inline CFNumberRef ffCfCreateInt(int value) +{ + return CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &value); +} + +static inline void cfReleaseWrapper(void* type) +{ + if (*(CFTypeRef*) type) + CFRelease(*(CFTypeRef*) type); +} + +#define FF_CFTYPE_AUTO_RELEASE __attribute__((__cleanup__(cfReleaseWrapper))) + #endif From b49b583e640f818e190b61b7c968cca7232ff6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 21:28:42 +0800 Subject: [PATCH 10/15] Gamepad (Linux): add support --- CMakeLists.txt | 2 +- src/detection/gamepad/gamepad_linux.c | 42 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/detection/gamepad/gamepad_linux.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 27853f760..40b013dde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -336,7 +336,7 @@ if(LINUX) src/detection/gtk.c src/detection/host/host_linux.c src/detection/localip/localip_linux.c - src/detection/gamepad/gamepad_nosupport.c + src/detection/gamepad/gamepad_linux.c src/detection/media/media_linux.c src/detection/memory/memory_linux.c src/detection/opengl/opengl_linux.c diff --git a/src/detection/gamepad/gamepad_linux.c b/src/detection/gamepad/gamepad_linux.c new file mode 100644 index 000000000..ce61a5711 --- /dev/null +++ b/src/detection/gamepad/gamepad_linux.c @@ -0,0 +1,42 @@ +#include "gamepad.h" +#include "common/io/io.h" + +#include +#include + +const char* ffDetectGamepad(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* List of FFGamepadDevice */) +{ + DIR* dirp = opendir("/sys/class/input/"); + if(dirp == NULL) + return "opendir(\"/sys/class/input/\") == NULL"; + + FF_STRBUF_AUTO_DESTROY path; + ffStrbufInitS(&path, "/sys/class/input/"); + uint32_t baseLen = path.length; + + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(strncmp(entry->d_name, "js", 2) != 0) + continue; + if(!isdigit(entry->d_name[2])) + continue; + + ffStrbufAppendS(&path, entry->d_name); + ffStrbufAppendS(&path, "/device/name"); + + if (ffPathExists(path.chars, FF_PATHTYPE_FILE)) + { + FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + ffStrbufInitS(&device->identifier, entry->d_name); + ffStrbufInit(&device->name); + ffAppendFileBuffer(path.chars, &device->name); + } + + ffStrbufSubstrBefore(&path, baseLen); + } + + closedir(dirp); + + return NULL; +} From 79c9c19b1acc81605ba63813677d35ade238910f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 21:47:35 +0800 Subject: [PATCH 11/15] Doc: document gamepad module support --- CHANGELOG.md | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6cc76664..674e5b3f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Features: * Bluetooth module * Sound module +* Gamepad module * Support colored percentage numbers output Bugfixes: diff --git a/README.md b/README.md index 34ba5e726..192c498c3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ All categories not listed here should work without needing a specific implementa ##### Available Modules ``` -Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, GPU, Host, Icons, Kernel, Locale, LocalIP, Media, Memory, OpenCL, OpenGL, Packages, Player, Power Adapter, Processes, PublicIP, Separator, OS, Shell, Sound, Swap, Terminal, Terminal Font, Theme, Time, Title, Uptime, Vulkan, Wifi, WM, WMTheme +Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, Gamepad, GPU, Host, Icons, Kernel, Locale, LocalIP, Media, Memory, OpenCL, OpenGL, Packages, Player, Power Adapter, Processes, PublicIP, Separator, OS, Shell, Sound, Swap, Terminal, Terminal Font, Theme, Time, Title, Uptime, Vulkan, Wifi, WM, WMTheme ``` ##### Builtin logos From dc5da3c3b33c17d7b05746de489ba71e544e2fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 1 Feb 2023 21:54:45 +0800 Subject: [PATCH 12/15] Flashfetch: add missing modules --- src/flashfetch.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/flashfetch.c b/src/flashfetch.c index 0c24732e3..8737c8fcf 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -63,6 +63,9 @@ int main(int argc, char** argv) //ffPrintOpenCL(&instance); //ffPrintUsers(&instance); //ffPrintWeather(&instance); + //ffPrintBluetooth(&instance); + //ffPrintSound(&instance); + //ffPrintGamepad(&instance); ffPrintBreak(&instance); ffPrintColors(&instance); From a063cd4feb0f3a67a569332cc7b0969920340afc Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 1 Feb 2023 15:56:25 +0100 Subject: [PATCH 13/15] Correctly handle zfs pools #413 --- CMakeLists.txt | 1 + src/common/dbus.c | 1 + src/common/format.c | 1 + src/detection/cursor/cursor_linux.c | 1 + src/detection/disk/disk_linux.c | 12 ++++++++ src/detection/displayserver/linux/wmde.c | 1 + src/detection/displayserver/linux/xlib.c | 1 + src/detection/gpu/gpu_linux.c | 1 + src/detection/os/os_linux.c | 1 + .../terminalfont/terminalfont_linux.c | 1 + .../terminalshell/terminalshell_linux.c | 4 +-- src/detection/vulkan.c | 1 + src/detection/wmtheme/wmtheme.h | 2 ++ src/detection/wmtheme/wmtheme_linux.c | 4 +-- src/fastfetch.c | 3 +- src/modules/opencl.c | 1 + src/util/FFstrbuf.c | 11 ------- src/util/FFstrbuf.h | 2 -- src/util/platform/FFPlatform.c | 1 + src/util/platform/FFPlatform_unix.c | 2 +- src/util/stringUtils.c | 29 +++++++++++++++++++ src/util/stringUtils.h | 12 ++++++++ 22 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 src/util/stringUtils.c create mode 100644 src/util/stringUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a2e5c3957..fcff26c2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -307,6 +307,7 @@ set(LIBFASTFETCH_SRC src/util/FFstrbuf.c src/util/FFvaluestore.c src/util/platform/FFPlatform.c + src/util/stringUtils.c ) if(LINUX) diff --git a/src/common/dbus.c b/src/common/dbus.c index 1d2840b2f..f974b39d9 100644 --- a/src/common/dbus.c +++ b/src/common/dbus.c @@ -3,6 +3,7 @@ #ifdef FF_HAVE_DBUS #include "common/thread.h" +#include "util/stringUtils.h" static bool loadLibSymbols(const FFinstance* instance, FFDBusLibrary* lib) { diff --git a/src/common/format.c b/src/common/format.c index 1a4a688c5..7394822f8 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -2,6 +2,7 @@ #include "common/format.h" #include "common/parsing.h" #include "util/textModifier.h" +#include "util/stringUtils.h" #include diff --git a/src/detection/cursor/cursor_linux.c b/src/detection/cursor/cursor_linux.c index ab7104dea..88366c935 100644 --- a/src/detection/cursor/cursor_linux.c +++ b/src/detection/cursor/cursor_linux.c @@ -5,6 +5,7 @@ #include "common/settings.h" #include "detection/gtk.h" #include "detection/displayserver/displayserver.h" +#include "util/stringUtils.h" #include diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index ec7a26abf..c15eea812 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -1,5 +1,7 @@ #include "disk.h" +#include "util/stringUtils.h" + #include #include #include @@ -19,6 +21,10 @@ static bool isPhysicalDevice(const char* device) if(strcmp(device, "drvfs") == 0) return true; + //ZFS root pool. The format is rpool/// + if(strncmp(device, "rpool/", 6) == 0) + return true; + //Pseudo filesystems don't have a device in /dev const char* devPrefix = "/dev/"; if(strncmp(device, devPrefix, strlen(devPrefix)) != 0) @@ -141,6 +147,7 @@ static bool isSubvolume(const FFlist* devices) { const FFstrbuf* currentDevie = ffListGet(devices, devices->length - 1); + //Filter all disks which device was already found. This catches BTRFS subvolumes. for(uint32_t i = 0; i < devices->length - 1; i++) { const FFstrbuf* otherDevice = ffListGet(devices, i); @@ -149,6 +156,11 @@ static bool isSubvolume(const FFlist* devices) return true; } + //ZFS subvolumes: rpool///. + //Test if the third slash is present. + if(strncmp(currentDevie->chars, "rpool/", 6) == 0 && ffStrHasNChars(currentDevie->chars, '/', 3)) + return true; + return false; } diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index 71b1d1474..3c40b2e11 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -3,6 +3,7 @@ #include "common/properties.h" #include "common/parsing.h" #include "common/processing.h" +#include "util/stringUtils.h" #include #include diff --git a/src/detection/displayserver/linux/xlib.c b/src/detection/displayserver/linux/xlib.c index e6fb8b930..a804dd29a 100644 --- a/src/detection/displayserver/linux/xlib.c +++ b/src/detection/displayserver/linux/xlib.c @@ -3,6 +3,7 @@ #ifdef FF_HAVE_X11 #include "common/library.h" #include "common/parsing.h" +#include "util/stringUtils.h" #include typedef struct X11PropertyData diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index d34807df9..549d8ed40 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -6,6 +6,7 @@ #include "common/properties.h" #include "common/parsing.h" #include "detection/temps/temps_linux.h" +#include "util/stringUtils.h" #include #include #include diff --git a/src/detection/os/os_linux.c b/src/detection/os/os_linux.c index 45a0f5cec..063e8455b 100644 --- a/src/detection/os/os_linux.c +++ b/src/detection/os/os_linux.c @@ -1,6 +1,7 @@ #include "os.h" #include "common/properties.h" #include "common/parsing.h" +#include "util/stringUtils.h" #include #include diff --git a/src/detection/terminalfont/terminalfont_linux.c b/src/detection/terminalfont/terminalfont_linux.c index 1d06d09c1..b83724160 100644 --- a/src/detection/terminalfont/terminalfont_linux.c +++ b/src/detection/terminalfont/terminalfont_linux.c @@ -4,6 +4,7 @@ #include "common/parsing.h" #include "detection/terminalshell/terminalshell.h" #include "detection/displayserver/displayserver.h" +#include "util/stringUtils.h" static const char* getSystemMonospaceFont(const FFinstance* instance) { diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 1a2875508..460b98a09 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -1,9 +1,9 @@ -#include "fastfetch.h" +#include "terminalshell.h" #include "common/io/io.h" #include "common/parsing.h" #include "common/processing.h" #include "common/thread.h" -#include "terminalshell.h" +#include "util/stringUtils.h" #include #include diff --git a/src/detection/vulkan.c b/src/detection/vulkan.c index c61e6f8f8..b6ccd0698 100644 --- a/src/detection/vulkan.c +++ b/src/detection/vulkan.c @@ -7,6 +7,7 @@ #include "common/library.h" #include "common/io/io.h" #include "common/parsing.h" +#include "util/stringUtils.h" #include #include diff --git a/src/detection/wmtheme/wmtheme.h b/src/detection/wmtheme/wmtheme.h index e1475006f..33a15dd0f 100644 --- a/src/detection/wmtheme/wmtheme.h +++ b/src/detection/wmtheme/wmtheme.h @@ -3,6 +3,8 @@ #ifndef FASTFETCH_INCLUDED_detection_wmtheme #define FASTFETCH_INCLUDED_detection_wmtheme +#include "fastfetch.h" + bool ffDetectWmTheme(FFinstance* instance, FFstrbuf* themeOrError); #endif diff --git a/src/detection/wmtheme/wmtheme_linux.c b/src/detection/wmtheme/wmtheme_linux.c index 87a8437c1..8d2a079f6 100644 --- a/src/detection/wmtheme/wmtheme_linux.c +++ b/src/detection/wmtheme/wmtheme_linux.c @@ -1,10 +1,10 @@ -#include "fastfetch.h" +#include "wmtheme.h" #include "common/properties.h" #include "common/parsing.h" #include "common/settings.h" #include "detection/gtk.h" #include "detection/displayserver/displayserver.h" -#include "wmtheme.h" +#include "util/stringUtils.h" static bool detectWMThemeFromConfigFile(FFinstance* instance, const char* configFile, const char* themeRegex, const char* defaultValue, FFstrbuf* themeOrError) { diff --git a/src/fastfetch.c b/src/fastfetch.c index ded937466..c107f4768 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1,9 +1,10 @@ #include "fastfetch.h" -#include "util/FFvaluestore.h" #include "common/printing.h" #include "common/parsing.h" #include "common/io/io.h" #include "common/time.h" +#include "util/FFvaluestore.h" +#include "util/stringUtils.h" #include #include diff --git a/src/modules/opencl.c b/src/modules/opencl.c index debfcd45e..18e826317 100644 --- a/src/modules/opencl.c +++ b/src/modules/opencl.c @@ -7,6 +7,7 @@ #if defined(FF_HAVE_OPENCL) || defined(__APPLE__) #include "common/library.h" #include "common/parsing.h" +#include "util/stringUtils.h" #include #define CL_TARGET_OPENCL_VERSION 100 diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 375c53aaa..94f436d51 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -3,17 +3,6 @@ #include #include -bool ffStrSet(const char* str) -{ - if(str == NULL) - return false; - - while(isspace(*str)) - str++; - - return *str != '\0'; -} - static char* CHAR_NULL_PTR = ""; void ffStrbufInitA(FFstrbuf* strbuf, uint32_t allocate) diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 7342cb607..7755225a7 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -18,8 +18,6 @@ #define strcasestr StrStrIA #endif -bool ffStrSet(const char* str); - #define FASTFETCH_STRBUF_DEFAULT_ALLOC 32 typedef struct FFstrbuf diff --git a/src/util/platform/FFPlatform.c b/src/util/platform/FFPlatform.c index d549b6501..2a880aed1 100644 --- a/src/util/platform/FFPlatform.c +++ b/src/util/platform/FFPlatform.c @@ -1,4 +1,5 @@ #include "FFPlatform_private.h" +#include "util/stringUtils.h" void ffPlatformInit(FFPlatform* platform) { diff --git a/src/util/platform/FFPlatform_unix.c b/src/util/platform/FFPlatform_unix.c index 65dad8d8b..6d65defcb 100644 --- a/src/util/platform/FFPlatform_unix.c +++ b/src/util/platform/FFPlatform_unix.c @@ -1,5 +1,5 @@ #include "FFPlatform_private.h" - +#include "util/stringUtils.h" #include "fastfetch_config.h" #include diff --git a/src/util/stringUtils.c b/src/util/stringUtils.c new file mode 100644 index 000000000..703c81a45 --- /dev/null +++ b/src/util/stringUtils.c @@ -0,0 +1,29 @@ +#include "stringUtils.h" + +#include +#include + +bool ffStrSet(const char* str) +{ + if(str == NULL) + return false; + + while(isspace(*str)) + str++; + + return *str != '\0'; +} + +bool ffStrHasNChars(const char* str, char c, uint32_t n) +{ + for(uint32_t i = 0; i < n; i++) + { + char* current = strchr(str, c); + if(current == NULL) + return false; + + str = current + 1; + } + + return true; +} diff --git a/src/util/stringUtils.h b/src/util/stringUtils.h new file mode 100644 index 000000000..7c2475353 --- /dev/null +++ b/src/util/stringUtils.h @@ -0,0 +1,12 @@ +#pragma once + +#ifndef FF_INCLUDED_util_stringUtils_h +#define FF_INCLUDED_util_stringUtils_h + +#include +#include + +bool ffStrSet(const char* str); +bool ffStrHasNChars(const char* str, char c, uint32_t n); + +#endif From 159f4a3a3c60c972c584dc61e4302e842e9f7590 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 1 Feb 2023 18:35:06 +0100 Subject: [PATCH 14/15] Fix build on apple --- src/detection/packages/packages_apple.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/detection/packages/packages_apple.c b/src/detection/packages/packages_apple.c index b8bc8a682..eaaf5f2fa 100644 --- a/src/detection/packages/packages_apple.c +++ b/src/detection/packages/packages_apple.c @@ -1,5 +1,6 @@ #include "packages.h" #include "common/parsing.h" +#include "util/stringUtils.h" #include From 8153c8b82c730becb4b740224c7ba465a1d241b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 2 Feb 2023 17:50:22 +0800 Subject: [PATCH 15/15] LocalIP: support `--localip-compact-type` option (#408) --- CHANGELOG.md | 3 +- completions/bash | 1 + src/common/init.c | 1 + src/data/help.txt | 1 + src/fastfetch.c | 10 ++++ src/fastfetch.h | 9 ++++ src/modules/localip.c | 109 ++++++++++++++++++++++++++++-------------- 7 files changed, 97 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 674e5b3f3..8ae796c35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ Features: * Bluetooth module * Sound module * Gamepad module -* Support colored percentage numbers output +* Support colored percentage numbers output (#409) +* Support `--localip-compact-type` option (#408) Bugfixes: diff --git a/completions/bash b/completions/bash index 93386be34..5533d45a2 100644 --- a/completions/bash +++ b/completions/bash @@ -186,6 +186,7 @@ __fastfetch_completion() "--localip-show-ipv6" "--localip-show-loop" "--localip-name-prefix" + "--localip-compact-type" "--escape-bedrock" "--pipe" "--title-fqdn" diff --git a/src/common/init.c b/src/common/init.c index 592ba17df..cd1105534 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -175,6 +175,7 @@ static void defaultConfig(FFinstance* instance) instance->config.localIpShowIpV6 = false; instance->config.localIpShowLoop = false; ffStrbufInit(&instance->config.localIpNamePrefix); + instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE; instance->config.publicIpTimeout = 0; ffStrbufInit(&instance->config.publicIpUrl); diff --git a/src/data/help.txt b/src/data/help.txt index 3eeaf662b..b8cf324f7 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -124,6 +124,7 @@ Module specific options: --localip-show-ipv6 : Show IPv6 addresses in local ip module. Default is false --localip-show-loop : Show loop back addresses (127.0.0.1) in local ip module. Default is false --localip-name-prefix : Show IPs with given name prefix only. Default is empty + --localip-compact-type : Show IPs in one line. Should be either none (disable one-line mode), default (don't sort the result), v4first (print ipv4 first) and v6first. Default is none --public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0) --public-ip-url: The URL of public IP detection server to be used. --weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0) diff --git a/src/fastfetch.c b/src/fastfetch.c index a977d3c47..594c5c87b 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1290,6 +1290,16 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con instance->config.localIpShowLoop = optionParseBoolean(value); else if(strcasecmp(key, "--localip-name-prefix") == 0) optionParseString(key, value, &instance->config.localIpNamePrefix); + else if(strcasecmp(key, "--localip-compact-type") == 0) + { + optionParseEnum(key, value, &instance->config.localIpCompactType, + "none", FF_LOCALIP_COMPACT_TYPE_NONE, + "default", FF_LOCALIP_COMPACT_TYPE_DEFAULT, + "v4first", FF_LOCALIP_COMPACT_TYPE_V4FIRST, + "v6first", FF_LOCALIP_COMPACT_TYPE_V6FIRST, + NULL + ); + } else if(strcasecmp(key, "--os-file") == 0) optionParseString(key, value, &instance->config.osFile); else if(strcasecmp(key, "--player-name") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index b625611b1..f31562208 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -41,6 +41,14 @@ typedef enum FFSoundType FF_SOUND_TYPE_ALL, } FFSoundType; +typedef enum FFLocalIpCompactType +{ + FF_LOCALIP_COMPACT_TYPE_NONE, + FF_LOCALIP_COMPACT_TYPE_DEFAULT, + FF_LOCALIP_COMPACT_TYPE_V4FIRST, + FF_LOCALIP_COMPACT_TYPE_V6FIRST, +} FFLocalIpCompactType; + typedef enum FFBinaryPrefixType { FF_BINARY_PREFIX_TYPE_IEC, // 1024 Bytes = 1 KiB, 1024 KiB = 1 MiB, ... (standard) @@ -204,6 +212,7 @@ typedef struct FFconfig bool localIpShowIpV4; bool localIpShowIpV6; FFstrbuf localIpNamePrefix; + FFLocalIpCompactType localIpCompactType; FFstrbuf publicIpUrl; uint32_t publicIpTimeout; diff --git a/src/modules/localip.c b/src/modules/localip.c index 3a445c5e5..d751cbdba 100644 --- a/src/modules/localip.c +++ b/src/modules/localip.c @@ -5,9 +5,25 @@ #define FF_LOCALIP_MODULE_NAME "Local IP" #define FF_LOCALIP_NUM_FORMAT_ARGS 2 +static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right) +{ + if (left->ipv6 != right->ipv6) + return left->ipv6 == false ? -1 : 1; + + return ffStrbufComp(&left->addr, &right->addr); +} + +static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right) +{ + if (left->ipv6 != right->ipv6) + return left->ipv6 == true ? -1 : 1; + + return ffStrbufComp(&left->addr, &right->addr); +} + void ffPrintLocalIp(FFinstance* instance) { - FFlist results; + FF_LIST_AUTO_DESTROY results; ffListInit(&results, sizeof(FFLocalIpResult)); const char* error = ffDetectLocalIps(instance, &results); @@ -15,56 +31,77 @@ void ffPrintLocalIp(FFinstance* instance) if(error) { ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "%s", error); - goto exit; + return; } if(results.length == 0) { ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "Failed to detect any IPs"); - goto exit; + return; } - FFstrbuf key; - ffStrbufInit(&key); - - for(uint32_t i = 0; i < results.length; ++i) + if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE) { - FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(&results, i); + ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key); - if(instance->config.localIP.key.length == 0) + switch (instance->config.localIpCompactType) { - if(ip->name.length) - ffStrbufSetF(&key, FF_LOCALIP_MODULE_NAME " (%*s)", ip->name.length, ip->name.chars); - else - ffStrbufSetS(&key, FF_LOCALIP_MODULE_NAME); - } - else - { - ffStrbufClear(&key); - ffParseFormatString(&key, &instance->config.localIP.key, 1, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &ip->name} - }); + case FF_LOCALIP_COMPACT_TYPE_V4FIRST: + ffListSort(&results, (void*) sortIpsV4First); + break; + case FF_LOCALIP_COMPACT_TYPE_V6FIRST: + ffListSort(&results, (void*) sortIpsV6First); + break; + default: + break; } - if(instance->config.localIP.outputFormat.length == 0) + uint32_t index = 0; + FF_LIST_FOR_EACH(FFLocalIpResult, ip, results) { - ffPrintLogoAndKey(instance, key.chars, 0, NULL); - ffStrbufPutTo(&ip->addr, stdout); + if (index++ > 0) + putchar(' '); + ffStrbufWriteTo(&ip->addr, stdout); } - else - { - ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr}, - {FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"} - }); - } - - ffStrbufDestroy(&ip->name); - ffStrbufDestroy(&ip->addr); + putchar('\n'); } + else + { + FF_STRBUF_AUTO_DESTROY key; + ffStrbufInit(&key); - ffStrbufDestroy(&key); + FF_LIST_FOR_EACH(FFLocalIpResult, ip, results) + { + if(instance->config.localIP.key.length == 0) + { + if(ip->name.length) + ffStrbufSetF(&key, FF_LOCALIP_MODULE_NAME " (%*s)", ip->name.length, ip->name.chars); + else + ffStrbufSetS(&key, FF_LOCALIP_MODULE_NAME); + } + else + { + ffStrbufClear(&key); + ffParseFormatString(&key, &instance->config.localIP.key, 1, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &ip->name} + }); + } -exit: - ffListDestroy(&results); + if(instance->config.localIP.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + ffStrbufPutTo(&ip->addr, stdout); + } + else + { + ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr}, + {FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"} + }); + } + + ffStrbufDestroy(&ip->name); + ffStrbufDestroy(&ip->addr); + } + } }