diff --git a/src/common/FFlist.h b/src/common/FFlist.h index 5cafb48cc..b380f6cf7 100644 --- a/src/common/FFlist.h +++ b/src/common/FFlist.h @@ -11,46 +11,49 @@ typedef struct FFlist { uint8_t* data; - uint32_t elementSize; uint32_t length; uint32_t capacity; } FFlist; -void* ffListAdd(FFlist* list); +void* ffListAdd(FFlist* list, uint32_t elementSize); // Removes the first element, and copy its value to `*result` -bool ffListShift(FFlist* list, void* result); +bool ffListShift(FFlist* list, uint32_t elementSize, void* result); // Removes the last element, and copy its value to `*result` -bool ffListPop(FFlist* list, void* result); +bool ffListPop(FFlist* list, uint32_t elementSize, void* result); -static inline void ffListInit(FFlist* list, uint32_t elementSize) { - assert(elementSize > 0); - list->elementSize = elementSize; +static inline void ffListInit(FFlist* list) { list->capacity = 0; list->length = 0; list->data = NULL; } static inline void ffListInitA(FFlist* list, uint32_t elementSize, uint32_t capacity) { - ffListInit(list, elementSize); + ffListInit(list); list->capacity = capacity; - list->data = __builtin_expect(capacity == 0, 0) ? NULL : (uint8_t*) malloc((size_t) list->capacity * list->elementSize); + list->data = __builtin_expect(capacity == 0, 0) ? NULL : (uint8_t*) malloc((size_t) capacity * elementSize); } -static inline FFlist ffListCreate(uint32_t elementSize) { +FF_A_NODISCARD static inline FFlist ffListCreate() { FFlist result; - ffListInit(&result, elementSize); + ffListInit(&result); return result; } -static inline void* ffListGet(const FFlist* list, uint32_t index) { - assert(list->capacity > index); - return list->data + (index * list->elementSize); +FF_A_NODISCARD static inline FFlist ffListCreateA(uint32_t elementSize, uint32_t capacity) { + FFlist result; + ffListInitA(&result, elementSize, capacity); + return result; } -FF_A_NODISCARD static inline uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool (*compFunc)(const void*, const void*)) { +FF_A_NODISCARD static inline void* ffListGet(const FFlist* list, uint32_t elementSize, uint32_t index) { + assert(list->capacity > index); + return list->data + (index * elementSize); +} + +FF_A_NODISCARD static inline uint32_t ffListFirstIndexComp(const FFlist* list, uint32_t elementSize, void* compElement, bool (*compFunc)(const void*, const void*)) { for (uint32_t i = 0; i < list->length; i++) { - if (compFunc(ffListGet(list, i), compElement)) { + if (compFunc(ffListGet(list, elementSize, i), compElement)) { return i; } } @@ -58,24 +61,23 @@ FF_A_NODISCARD static inline uint32_t ffListFirstIndexComp(const FFlist* list, v return list->length; } -static inline bool ffListContains(const FFlist* list, void* compElement, bool (*compFunc)(const void*, const void*)) { - return ffListFirstIndexComp(list, compElement, compFunc) != list->length; +FF_A_NODISCARD static inline bool ffListContains(const FFlist* list, uint32_t elementSize, void* compElement, bool (*compFunc)(const void*, const void*)) { + return ffListFirstIndexComp(list, elementSize, compElement, compFunc) != list->length; } -static inline void ffListSort(FFlist* list, int (*compar)(const void*, const void*)) { - qsort(list->data, list->length, list->elementSize, compar); +static inline void ffListSort(FFlist* list, uint32_t elementSize, int (*compar)(const void*, const void*)) { + qsort(list->data, list->length, elementSize, compar); } // Move the contents of `src` into `list`, and left `src` empty static inline void ffListInitMove(FFlist* list, FFlist* src) { if (src) { - list->elementSize = src->elementSize; list->capacity = src->capacity; list->length = src->length; list->data = src->data; - ffListInit(src, list->elementSize); + ffListInit(src); } else { - ffListInit(list, 0); + ffListInit(list); } } @@ -94,35 +96,29 @@ static inline void ffListClear(FFlist* list) { list->length = 0; } -static inline void ffListReserve(FFlist* list, uint32_t newCapacity) { +static inline void ffListReserve(FFlist* list, uint32_t elementSize, uint32_t newCapacity) { if (__builtin_expect(newCapacity <= list->capacity, false)) { return; } - list->data = (uint8_t*) realloc(list->data, (size_t) newCapacity * list->elementSize); + list->data = (uint8_t*) realloc(list->data, (size_t) newCapacity * elementSize); list->capacity = newCapacity; } #define FF_LIST_FOR_EACH(itemType, itemVarName, listVar) \ - assert(sizeof(itemType) == (listVar).elementSize); \ for (itemType* itemVarName = (itemType*) (listVar).data; \ itemVarName - (itemType*) (listVar).data < (intptr_t) (listVar).length; \ ++itemVarName) #define FF_LIST_AUTO_DESTROY FFlist FF_A_CLEANUP(ffListDestroy) -#define FF_LIST_GET(itemType, listVar, index) \ - ({ \ - assert(sizeof(itemType) == (listVar).elementSize); \ - assert((listVar).capacity > (index)); \ - (itemType*) (listVar).data + (index); \ +#define FF_LIST_GET(itemType, listVar, index) \ + ({ \ + assert((listVar).capacity > (index)); \ + (itemType*) (listVar).data + (index); \ }) -#define FF_LIST_ADD(itemType, listVar) \ - ({ \ - assert(sizeof(itemType) == (listVar).elementSize); \ - (itemType*) ffListAdd(&(listVar)); \ - }) +#define FF_LIST_ADD(itemType, listVar) (itemType*) ffListAdd(&(listVar), (uint32_t) sizeof(itemType)) #define FF_LIST_FIRST(itemType, listVar) FF_LIST_GET(itemType, listVar, 0) #define FF_LIST_LAST(itemType, listVar) \ @@ -130,3 +126,16 @@ static inline void ffListReserve(FFlist* list, uint32_t newCapacity) { assert((listVar).length > 0); \ FF_LIST_GET(itemType, listVar, ((listVar).length - 1)); \ }) + +#define FF_LIST_CONTAINS(listVar, pCompElement, compFunc) \ + ({ \ + typedef __typeof__(*(pCompElement)) compElementType; \ + typedef bool compFuncType(const compElementType*, const compElementType*); \ + static_assert(__builtin_types_compatible_p(__typeof__(compFunc), compFuncType), "In compatible callback function"); \ + ffListContains(&(listVar), (uint32_t) sizeof(*(pCompElement)), (pCompElement), (bool (*)(const void*, const void*)) compFunc); \ + }) + +#define FF_LIST_SHIFT(listVar, pResult) \ + ffListShift(&(listVar), (uint32_t) sizeof(*(pResult)), (pResult)) +#define FF_LIST_POP(listVar, pResult) \ + ffListPop(&(listVar), (uint32_t) sizeof(*(pResult)), (pResult)) diff --git a/src/common/impl/FFPlatform.c b/src/common/impl/FFPlatform.c index 532e46271..2e4d523a9 100644 --- a/src/common/impl/FFPlatform.c +++ b/src/common/impl/FFPlatform.c @@ -6,8 +6,8 @@ void ffPlatformInit(FFPlatform* platform) { ffStrbufInit(&platform->homeDir); ffStrbufInit(&platform->cacheDir); - ffListInit(&platform->configDirs, sizeof(FFstrbuf)); - ffListInit(&platform->dataDirs, sizeof(FFstrbuf)); + ffListInit(&platform->configDirs); + ffListInit(&platform->dataDirs); ffStrbufInit(&platform->exePath); ffStrbufInit(&platform->cwd); @@ -77,8 +77,8 @@ void ffPlatformPathAddAbsolute(FFlist* dirs, const char* path) { FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateS(path); ffStrbufEnsureEndsWithC(&buffer, '/'); - if (!ffListContains(dirs, &buffer, (void*) ffStrbufEqual)) { - ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer); + if (!FF_LIST_CONTAINS(*dirs, &buffer, ffStrbufEqual)) { + ffStrbufInitMove(FF_LIST_ADD(FFstrbuf, *dirs), &buffer); } } @@ -87,7 +87,7 @@ void ffPlatformPathAddHome(FFlist* dirs, const FFPlatform* platform, const char* ffStrbufAppend(&buffer, &platform->homeDir); ffStrbufAppendS(&buffer, suffix); ffStrbufEnsureEndsWithC(&buffer, '/'); - if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !ffListContains(dirs, &buffer, (void*) ffStrbufEqual)) { - ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer); + if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !FF_LIST_CONTAINS(*dirs, &buffer, ffStrbufEqual)) { + ffStrbufInitMove(FF_LIST_ADD(FFstrbuf, *dirs), &buffer); } } diff --git a/src/common/impl/FFPlatform_windows.c b/src/common/impl/FFPlatform_windows.c index 13a687168..b4ed2b1ce 100644 --- a/src/common/impl/FFPlatform_windows.c +++ b/src/common/impl/FFPlatform_windows.c @@ -77,8 +77,8 @@ static void platformPathAddKnownFolder(FFlist* dirs, REFKNOWNFOLDERID folderId) CoTaskMemFree(pPath); ffStrbufReplaceAllC(&buffer, '\\', '/'); ffStrbufEnsureEndsWithC(&buffer, '/'); - if (!ffListContains(dirs, &buffer, (void*) ffStrbufEqual)) { - ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer); + if (!FF_LIST_CONTAINS(*dirs, &buffer, ffStrbufEqual)) { + ffStrbufInitMove(FF_LIST_ADD(FFstrbuf, *dirs), &buffer); } } } @@ -98,8 +98,8 @@ static void platformPathAddEnvSuffix(FFlist* dirs, const char* env, const char* ffStrbufEnsureEndsWithC(&buffer, '/'); } - if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !ffListContains(dirs, &buffer, (void*) ffStrbufEqual)) { - ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer); + if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !FF_LIST_CONTAINS(*dirs, &buffer, ffStrbufEqual)) { + ffStrbufInitMove(FF_LIST_ADD(FFstrbuf, *dirs), &buffer); } } diff --git a/src/common/impl/FFlist.c b/src/common/impl/FFlist.c index 3f98cd06f..74df1e468 100644 --- a/src/common/impl/FFlist.c +++ b/src/common/impl/FFlist.c @@ -3,32 +3,32 @@ #include #include -void* ffListAdd(FFlist* list) { +void* ffListAdd(FFlist* list, uint32_t elementSize) { if (list->length == list->capacity) { - ffListReserve(list, list->capacity == 0 ? FF_LIST_DEFAULT_ALLOC : list->capacity * 2); + ffListReserve(list, elementSize, list->capacity == 0 ? FF_LIST_DEFAULT_ALLOC : list->capacity * 2); } ++list->length; - return ffListGet(list, list->length - 1); + return ffListGet(list, elementSize, list->length - 1); } -bool ffListShift(FFlist* list, void* result) { +bool ffListShift(FFlist* list, uint32_t elementSize, void* result) { if (list->length == 0) { return false; } - memcpy(result, list->data, list->elementSize); - memmove(list->data, list->data + list->elementSize, (size_t) list->elementSize * (list->length - 1)); + memcpy(result, list->data, elementSize); + memmove(list->data, list->data + elementSize, (size_t) elementSize * (list->length - 1)); --list->length; return true; } -bool ffListPop(FFlist* list, void* result) { +bool ffListPop(FFlist* list, uint32_t elementSize, void* result) { if (list->length == 0) { return false; } - memcpy(result, ffListGet(list, list->length - 1), list->elementSize); + memcpy(result, ffListGet(list, elementSize, list->length - 1), elementSize); --list->length; return true; } diff --git a/src/common/impl/font.c b/src/common/impl/font.c index 56f4a1316..b754c4c18 100644 --- a/src/common/impl/font.c +++ b/src/common/impl/font.c @@ -12,7 +12,7 @@ void ffFontInit(FFfont* font) { ffStrbufInit(&font->pretty); ffStrbufInit(&font->name); ffStrbufInit(&font->size); - ffListInit(&font->styles, sizeof(FFstrbuf)); + ffListInit(&font->styles); } static void strbufAppendNSExcludingC(FFstrbuf* strbuf, uint32_t length, const char* value, char exclude) { @@ -93,7 +93,7 @@ void ffFontInitQt(FFfont* font, const char* data) { data++; if (isalpha(*data)) { do { - FFstrbuf* style = (FFstrbuf*) ffListAdd(&font->styles); + FFstrbuf* style = FF_LIST_ADD(FFstrbuf, font->styles); ffStrbufInit(style); data = ffStrbufAppendSUntilC(style, data, ' '); if (data) { @@ -152,7 +152,7 @@ static void fontPangoParseWord(const char** data, FFfont* font, FFstrbuf* altern ffStrStartsWithIgnCase(wordStart, "Condensed") || ffStrStartsWithIgnCase(wordStart, "Expanded")) { if (alternativeBuffer == NULL) { - alternativeBuffer = (FFstrbuf*) ffListAdd(&font->styles); + alternativeBuffer = FF_LIST_ADD(FFstrbuf, font->styles); ffStrbufInit(alternativeBuffer); } @@ -271,7 +271,7 @@ void ffFontInitXlfd(FFfont* font, const char* xlfd) { { // ignore "normal" (case-insensitive) if (!(length == 6 && ffStrStartsWithIgnCase(pstart, "normal"))) { - FFstrbuf* style = (FFstrbuf*) ffListAdd(&font->styles); + FFstrbuf* style = FF_LIST_ADD(FFstrbuf, font->styles); ffStrbufInitNS(style, length, pstart); } } diff --git a/src/common/impl/smbios.c b/src/common/impl/smbios.c index af9c95a9e..abb9fe291 100644 --- a/src/common/impl/smbios.c +++ b/src/common/impl/smbios.c @@ -232,18 +232,17 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() { if (!ffAppendFileBuffer("/sys/firmware/dmi/tables/DMI", &buffer)) # elif defined(__OpenBSD__) { - FF_DEBUG("Using OpenBSD /var/run/dmesg.boot implementation"); - char dmesg[8192]; - ssize_t size = ffReadFileData("/var/run/dmesg.boot", sizeof(dmesg), dmesg); - if (size <= 0) { - goto fallback; - } - char* line = memmem(dmesg, sizeof(dmesg), "\nbios0 at mainbios0: SMBIOS rev. ", strlen("\nbios0 at mainbios0: SMBIOS rev. ")); - if (!line) { - goto fallback; - } - line += strlen("\nbios0 at mainbios0: SMBIOS rev. "); - + FF_DEBUG("Using OpenBSD /var/run/dmesg.boot implementation"); + char dmesg[8192]; + ssize_t size = ffReadFileData("/var/run/dmesg.boot", sizeof(dmesg), dmesg); + if (size <= 0) { + goto fallback; + } + char* line = memmem(dmesg, sizeof(dmesg), "\nbios0 at mainbios0: SMBIOS rev. ", strlen("\nbios0 at mainbios0: SMBIOS rev. ")); + if (!line) { + goto fallback; + } + line += strlen("\nbios0 at mainbios0: SMBIOS rev. "); } # endif { diff --git a/src/common/windows/registry.c b/src/common/windows/registry.c index 30cd68ce6..b2d6611d7 100644 --- a/src/common/windows/registry.c +++ b/src/common/windows/registry.c @@ -178,14 +178,6 @@ static bool processRegValue(const FFRegValueArg* arg, const ULONG regType, const } FFlist* list = (FFlist*) arg->value; - if (list->elementSize != sizeof(FFstrbuf)) { - if (error) { - FF_STRBUF_AUTO_DESTROY nameA = arg->name ? ffStrbufCreateWS(arg->name) : ffStrbufCreateStatic("(default)"); - ffStrbufAppendF(error, "ffRegReadValues(%s) type mismatch: expected list of strbuf for REG_MULTI_SZ", nameA.chars); - } - return false; - } - ffListClear(list); for ( diff --git a/src/detection/battery/battery_android.c b/src/detection/battery/battery_android.c index 2884bb0c1..4c52d3a77 100644 --- a/src/detection/battery/battery_android.c +++ b/src/detection/battery/battery_android.c @@ -31,7 +31,7 @@ static const char* parseTermuxApi(FFBatteryOptions* options, FFlist* results) { return "Battery info result is not a JSON object"; } - FFBatteryResult* battery = ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); battery->temperature = FF_BATTERY_TEMP_UNSET; battery->cycleCount = 0; battery->timeRemaining = -1; @@ -92,7 +92,7 @@ static const char* parseDumpsys(FFBatteryOptions* options, FFlist* results) { } ffStrbufClear(&temp); - FFBatteryResult* battery = ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); battery->temperature = FF_BATTERY_TEMP_UNSET; battery->cycleCount = 0; battery->timeRemaining = -1; diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index 4023898b8..c22aa6d43 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -32,7 +32,7 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results) { bool boolValue; - FFBatteryResult* battery = ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); battery->temperature = FF_BATTERY_TEMP_UNSET; ffStrbufInit(&battery->manufacturer); ffStrbufInit(&battery->modelName); diff --git a/src/detection/battery/battery_bsd.c b/src/detection/battery/battery_bsd.c index 01863cedb..336a17e06 100644 --- a/src/detection/battery/battery_bsd.c +++ b/src/detection/battery/battery_bsd.c @@ -34,7 +34,7 @@ const char* ffDetectBattery(FF_A_UNUSED FFBatteryOptions* options, FFlist* resul continue; } - FFBatteryResult* battery = ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); battery->temperature = FF_BATTERY_TEMP_UNSET; battery->cycleCount = 0; ffStrbufInit(&battery->manufacturer); diff --git a/src/detection/battery/battery_haiku.c b/src/detection/battery/battery_haiku.c index b4463d07d..aa620e6e5 100644 --- a/src/detection/battery/battery_haiku.c +++ b/src/detection/battery/battery_haiku.c @@ -25,7 +25,7 @@ const char* parseBattery(int dfd, const char* battId, FFlist* results) { return "Skipped"; } - FFBatteryResult* battery = (FFBatteryResult*) ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); ffStrbufInitS(&battery->modelName, extended.model_number); ffStrbufInitS(&battery->manufacturer, extended.oem_info); ffStrbufInit(&battery->manufactureDate); diff --git a/src/detection/battery/battery_linux.c b/src/detection/battery/battery_linux.c index affe5dc03..13fc4d05c 100644 --- a/src/detection/battery/battery_linux.c +++ b/src/detection/battery/battery_linux.c @@ -60,7 +60,7 @@ static bool parseBattery(int dfd, const char* id, FFBatteryOptions* options, FFl return false; } - FFBatteryResult* result = ffListAdd(results); + FFBatteryResult* result = FF_LIST_ADD(FFBatteryResult, *results); ffStrbufInit(&result->manufacturer); ffStrbufInit(&result->modelName); ffStrbufInit(&result->technology); diff --git a/src/detection/battery/battery_nbsd.c b/src/detection/battery/battery_nbsd.c index 05bd2e39c..f24907c5e 100644 --- a/src/detection/battery/battery_nbsd.c +++ b/src/detection/battery/battery_nbsd.c @@ -74,7 +74,7 @@ const char* ffDetectBattery(FF_A_UNUSED FFBatteryOptions* options, FFlist* resul } if (max > 0) { - FFBatteryResult* battery = ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); battery->temperature = FF_BATTERY_TEMP_UNSET; battery->cycleCount = 0; ffStrbufInit(&battery->manufacturer); diff --git a/src/detection/battery/battery_obsd.c b/src/detection/battery/battery_obsd.c index dba707b0f..ee664ce69 100644 --- a/src/detection/battery/battery_obsd.c +++ b/src/detection/battery/battery_obsd.c @@ -23,7 +23,7 @@ const char* ffDetectBattery(FF_A_UNUSED FFBatteryOptions* options, FFlist* resul return NULL; } - FFBatteryResult* battery = (FFBatteryResult*) ffListAdd(result); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *result); battery->temperature = FF_BATTERY_TEMP_UNSET; battery->cycleCount = 0; battery->timeRemaining = -1; diff --git a/src/detection/battery/battery_windows.c b/src/detection/battery/battery_windows.c index 27319f2db..de94d75a8 100644 --- a/src/detection/battery/battery_windows.c +++ b/src/detection/battery/battery_windows.c @@ -63,7 +63,7 @@ static const char* detectWithCmApi(FFBatteryOptions* options, FFlist* results) { continue; } - FFBatteryResult* battery = (FFBatteryResult*) ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); if (memcmp(bi.Chemistry, "PbAc", 4) == 0) { ffStrbufInitStatic(&battery->technology, "Lead Acid"); @@ -259,7 +259,7 @@ static const char* detectWithNtApi(FF_A_UNUSED FFBatteryOptions* options, FFlist SYSTEM_BATTERY_STATE info; if (NT_SUCCESS(NtPowerInformation(SystemBatteryState, NULL, 0, &info, sizeof(info))) && info.BatteryPresent) { - FFBatteryResult* battery = (FFBatteryResult*) ffListAdd(results); + FFBatteryResult* battery = FF_LIST_ADD(FFBatteryResult, *results); ffStrbufInit(&battery->modelName); ffStrbufInit(&battery->manufacturer); ffStrbufInit(&battery->manufactureDate); diff --git a/src/detection/bluetooth/bluetooth_apple.m b/src/detection/bluetooth/bluetooth_apple.m index e7d3abd39..9cbf732a5 100644 --- a/src/detection/bluetooth/bluetooth_apple.m +++ b/src/detection/bluetooth/bluetooth_apple.m @@ -21,7 +21,7 @@ const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FF if (!options->showDisconnected && !ioDevice.isConnected) continue; - FFBluetoothResult* device = ffListAdd(devices); + FFBluetoothResult* device = FF_LIST_ADD(FFBluetoothResult, *devices); ffStrbufInitS(&device->name, ioDevice.name.UTF8String); ffStrbufInitS(&device->address, ioDevice.addressString.UTF8String); ffStrbufReplaceAllC(&device->address, '-', ':'); diff --git a/src/detection/bluetooth/bluetooth_bsd.c b/src/detection/bluetooth/bluetooth_bsd.c index 5c97a61cc..db9f23cf7 100644 --- a/src/detection/bluetooth/bluetooth_bsd.c +++ b/src/detection/bluetooth/bluetooth_bsd.c @@ -4,7 +4,7 @@ #include static int enumDev(FF_A_UNUSED int sockfd, struct bt_devinfo const* dev, FFlist* devices) { - FFBluetoothResult* device = ffListAdd(devices); + FFBluetoothResult* device = FF_LIST_ADD(FFBluetoothResult, *devices); ffStrbufInitS(&device->name, #if __FreeBSD__ bt_devremote_name_gen(dev->devname, &dev->bdaddr) diff --git a/src/detection/bluetooth/bluetooth_haiku.cpp b/src/detection/bluetooth/bluetooth_haiku.cpp index a9ce35950..8ed52c5ac 100644 --- a/src/detection/bluetooth/bluetooth_haiku.cpp +++ b/src/detection/bluetooth/bluetooth_haiku.cpp @@ -17,7 +17,7 @@ const char* ffDetectBluetooth(FF_A_UNUSED FFBluetoothOptions* options, FFlist* d BString devClass; dev->GetDeviceClass().DumpDeviceClass(devClass); - FFBluetoothResult* device = (FFBluetoothResult*) ffListAdd(devices); + FFBluetoothResult* device = FF_LIST_ADD(FFBluetoothResult, *devices); ffStrbufInitS(&device->name, dev->GetFriendlyName()); ffStrbufInitS(&device->address, bdaddrUtils::ToString(dev->GetBluetoothAddress()).String()); ffStrbufInitS(&device->type, devClass.String()); diff --git a/src/detection/bluetooth/bluetooth_linux.c b/src/detection/bluetooth/bluetooth_linux.c index 1f10968a6..79ac59c7b 100644 --- a/src/detection/bluetooth/bluetooth_linux.c +++ b/src/detection/bluetooth/bluetooth_linux.c @@ -151,7 +151,7 @@ static FFBluetoothResult* detectBluetoothObject(FFlist* devices, FFDBusData* dbu DBusMessageIter arrayIter; dbus->lib->ffdbus_message_iter_recurse(&dictIter, &arrayIter); - FFBluetoothResult* device = ffListAdd(devices); + FFBluetoothResult* device = FF_LIST_ADD(FFBluetoothResult, *devices); ffStrbufInit(&device->name); ffStrbufInit(&device->address); ffStrbufInit(&device->type); @@ -165,7 +165,7 @@ static FFBluetoothResult* detectBluetoothObject(FFlist* devices, FFDBusData* dbu return device; } -static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter, int32_t connectedCount) { +static void detectBluetoothRoot(FFBluetoothOptions* options, FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter, int32_t connectedCount) { if (dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) { return; } @@ -177,13 +177,17 @@ static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIt FFBluetoothResult* device = detectBluetoothObject(devices, dbus, &arrayIter); if (device) { - if (device->name.length == 0 || (connectedCount > 0 && !device->connected)) { + if (!options->showDisconnected && !device->connected) { ffStrbufDestroy(&device->name); ffStrbufDestroy(&device->address); ffStrbufDestroy(&device->type); --devices->length; } + if (device->name.length == 0) { + ffStrbufSetStatic(&device->name, "Unknown Device"); + } + if (device->connected && --connectedCount == 0) { break; } @@ -191,7 +195,7 @@ static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIt } while (dbus->lib->ffdbus_message_iter_next(&arrayIter)); } -static const char* detectBluetooth(FFlist* devices, int32_t connectedCount) { +static const char* detectBluetooth(FFBluetoothOptions* options, FFlist* devices, int32_t connectedCount) { FF_DBUS_AUTO_DESTROY_DATA FFDBusData dbus = {}; const char* error = ffDBusLoadData(DBUS_BUS_SYSTEM, &dbus); if (error) { @@ -209,7 +213,7 @@ static const char* detectBluetooth(FFlist* devices, int32_t connectedCount) { return "Failed to get root iterator of GetManagedObjects"; } - detectBluetoothRoot(devices, &dbus, &rootIter, connectedCount); + detectBluetoothRoot(options, devices, &dbus, &rootIter, connectedCount); dbus.lib->ffdbus_message_unref(managedObjects); return NULL; @@ -234,7 +238,7 @@ static uint32_t connectedDevices(void) { #endif -const char* ffDetectBluetooth(FF_A_UNUSED FFBluetoothOptions* options, FF_A_UNUSED FFlist* devices /* FFBluetoothResult */) { +const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FFBluetoothResult */) { #ifdef FF_HAVE_DBUS int32_t connectedCount = -1; if (!options->showDisconnected) { @@ -244,8 +248,9 @@ const char* ffDetectBluetooth(FF_A_UNUSED FFBluetoothOptions* options, FF_A_UNUS } } - return detectBluetooth(devices, connectedCount); + return detectBluetooth(options, devices, connectedCount); #else + FF_UNUSED(options, devices); return "Fastfetch was compiled without DBus support"; #endif } diff --git a/src/detection/bluetooth/bluetooth_windows.c b/src/detection/bluetooth/bluetooth_windows.c index 8cfd78053..bc148609b 100644 --- a/src/detection/bluetooth/bluetooth_windows.c +++ b/src/detection/bluetooth/bluetooth_windows.c @@ -30,7 +30,11 @@ const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FF } do { - FFBluetoothResult* device = ffListAdd(devices); + if (!options->showDisconnected && !btdi.fConnected) { + continue; + } + + FFBluetoothResult* device = FF_LIST_ADD(FFBluetoothResult, *devices); ffStrbufInitWS(&device->name, btdi.szName); ffStrbufInitF(&device->address, "%02X:%02X:%02X:%02X:%02X:%02X", btdi.Address.rgBytes[5], btdi.Address.rgBytes[4], btdi.Address.rgBytes[3], btdi.Address.rgBytes[2], btdi.Address.rgBytes[1], btdi.Address.rgBytes[0]); ffStrbufInit(&device->type); diff --git a/src/detection/bluetoothradio/bluetoothradio_apple.m b/src/detection/bluetoothradio/bluetoothradio_apple.m index 63af39d9c..c5154abf1 100644 --- a/src/detection/bluetoothradio/bluetoothradio_apple.m +++ b/src/detection/bluetoothradio/bluetoothradio_apple.m @@ -34,7 +34,7 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */) for (IOBluetoothHostController* ctrl in ctrls) { - FFBluetoothRadioResult* device = ffListAdd(devices); + FFBluetoothRadioResult* device = FF_LIST_ADD(FFBluetoothRadioResult, *devices); ffStrbufInitS(&device->name, ctrl.nameAsString.UTF8String); ffStrbufInitS(&device->address, ctrl.addressAsString.UTF8String); ffStrbufInitStatic(&device->vendor, "Apple"); diff --git a/src/detection/bluetoothradio/bluetoothradio_linux.c b/src/detection/bluetoothradio/bluetoothradio_linux.c index 83027c95d..faedfe721 100644 --- a/src/detection/bluetoothradio/bluetoothradio_linux.c +++ b/src/detection/bluetoothradio/bluetoothradio_linux.c @@ -126,7 +126,7 @@ static const char* detectBluetooth(FFlist* devices) { continue; } - FFBluetoothRadioResult* device = ffListAdd(devices); + FFBluetoothRadioResult* device = FF_LIST_ADD(FFBluetoothRadioResult, *devices); ffStrbufInit(&device->name); ffStrbufInit(&device->address); ffStrbufInitStatic(&device->vendor, "Unknown"); diff --git a/src/detection/bluetoothradio/bluetoothradio_windows.c b/src/detection/bluetoothradio/bluetoothradio_windows.c index 8977b05ab..f0b55d455 100644 --- a/src/detection/bluetoothradio/bluetoothradio_windows.c +++ b/src/detection/bluetoothradio/bluetoothradio_windows.c @@ -77,7 +77,7 @@ const char* ffDetectBluetoothRadio(FFlist* devices /* FFBluetoothRadioResult */) continue; } - FFBluetoothRadioResult* device = ffListAdd(devices); + FFBluetoothRadioResult* device = FF_LIST_ADD(FFBluetoothRadioResult, *devices); ffStrbufInitS(&device->name, blri.localInfo.name); BLUETOOTH_ADDRESS_STRUCT addr = { .ullLong = blri.localInfo.address }; diff --git a/src/detection/brightness/brightness_apple.c b/src/detection/brightness/brightness_apple.c index 55b1a2dd1..f7ffb6b5e 100644 --- a/src/detection/brightness/brightness_apple.c +++ b/src/detection/brightness/brightness_apple.c @@ -34,7 +34,7 @@ static const char* detectWithDisplayServices(const FFDisplayServerResult* displa if (display->type == FF_DISPLAY_TYPE_BUILTIN || display->type == FF_DISPLAY_TYPE_UNKNOWN) { float value; if (DisplayServicesGetBrightness((CGDirectDisplayID) display->id, &value) == kCGErrorSuccess) { - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); brightness->current = value; brightness->max = 1; brightness->min = 0; @@ -103,7 +103,7 @@ static const char* detectWithDdcci(FF_A_UNUSED const FFDisplayServerResult* disp uint32_t current = ((uint32_t) i2cOut[8] << 8u) + (uint32_t) i2cOut[9]; uint32_t max = ((uint32_t) i2cOut[6] << 8u) + (uint32_t) i2cOut[7]; - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); brightness->max = max; brightness->min = 0; brightness->current = current; @@ -214,7 +214,7 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F uint32_t current = ((uint32_t) i2cOut[8] << 8u) + (uint32_t) i2cOut[9]; uint32_t max = ((uint32_t) i2cOut[6] << 8u) + (uint32_t) i2cOut[7]; - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); brightness->max = max; brightness->min = 0; brightness->current = current; diff --git a/src/detection/brightness/brightness_bsd.c b/src/detection/brightness/brightness_bsd.c index 68bd9f1b3..818737159 100644 --- a/src/detection/brightness/brightness_bsd.c +++ b/src/detection/brightness/brightness_bsd.c @@ -26,7 +26,7 @@ const char* ffDetectBrightness(FF_A_UNUSED FFBrightnessOptions* options, FFlist* continue; } - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); ffStrbufInit(&brightness->name); brightness->max = BACKLIGHTMAXLEVELS; diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index 784ffb1b6..2bbf7fbc6 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -36,7 +36,7 @@ static const char* detectWithBacklight(FFlist* result) { ffStrbufAppendS(&backlightDir, entry->d_name); ffStrbufAppendS(&backlightDir, "/max_brightness"); if (ffReadFileBuffer(backlightDir.chars, &buffer)) { - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); ffStrbufSubstrBeforeLastC(&backlightDir, '/'); ffStrbufAppendS(&backlightDir, "/device"); ffStrbufInitA(&brightness->name, PATH_MAX); @@ -146,7 +146,7 @@ static const char* detectWithDdcci(FF_A_UNUSED FFBrightnessOptions* options, FFl int current = VALREC_CUR_VAL(vcpValue), max = VALREC_MAX_VAL(vcpValue); ffddca_free_any_vcp_value(vcpValue); - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); brightness->max = max; brightness->min = 0; brightness->current = current; diff --git a/src/detection/brightness/brightness_nbsd.c b/src/detection/brightness/brightness_nbsd.c index b61d66abb..afd665604 100644 --- a/src/detection/brightness/brightness_nbsd.c +++ b/src/detection/brightness/brightness_nbsd.c @@ -14,7 +14,7 @@ const char* ffDetectBrightness(FF_A_UNUSED FFBrightnessOptions* options, FFlist* continue; } - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); ffStrbufInitF(&brightness->name, "acpiout%d", i); brightness->max = 100; diff --git a/src/detection/brightness/brightness_obsd.c b/src/detection/brightness/brightness_obsd.c index 13122a2da..102a8c256 100644 --- a/src/detection/brightness/brightness_obsd.c +++ b/src/detection/brightness/brightness_obsd.c @@ -31,7 +31,7 @@ const char* ffDetectBrightness(FF_A_UNUSED FFBrightnessOptions* options, FFlist* continue; } - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); ffStrbufInitF(&brightness->name, "ttyC%c", i); brightness->max = param.max; diff --git a/src/detection/brightness/brightness_windows.cpp b/src/detection/brightness/brightness_windows.cpp index ec8e37020..43e78a82f 100644 --- a/src/detection/brightness/brightness_windows.cpp +++ b/src/detection/brightness/brightness_windows.cpp @@ -43,7 +43,7 @@ static const char* detectWithWmi(FFlist* result) { while (FFWmiRecord record = query.next()) { if (FFWmiVariant vtValue = record.get(L"CurrentBrightness")) { - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); brightness->max = 100; brightness->min = 0; brightness->current = vtValue.get(); @@ -90,7 +90,7 @@ static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, F if (NT_SUCCESS(ffGetPhysicalMonitors(&deviceName, 1, &monitorCount, &physicalMonitor)) && monitorCount >= 1) { DWORD curr = 0, max = 0; if (NT_SUCCESS(ffDDCCIGetVCPFeature(physicalMonitor, 0x10 /* luminance */, NULL, &curr, &max))) { - FFBrightnessResult* brightness = (FFBrightnessResult*) ffListAdd(result); + FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result); if (display->name.length > 0) { ffStrbufInitCopy(&brightness->name, &display->name); } else { diff --git a/src/detection/btrfs/btrfs_linux.c b/src/detection/btrfs/btrfs_linux.c index edfb90deb..41914d709 100644 --- a/src/detection/btrfs/btrfs_linux.c +++ b/src/detection/btrfs/btrfs_linux.c @@ -138,7 +138,7 @@ const char* ffDetectBtrfs(FFlist* result) { continue; } - FFBtrfsResult* item = ffListAdd(result); + FFBtrfsResult* item = FF_LIST_ADD(FFBtrfsResult, *result); (*item) = (FFBtrfsResult) { .uuid = ffStrbufCreateNS(uuidLen, entry->d_name), .name = ffStrbufCreate(), diff --git a/src/detection/camera/camera_android.c b/src/detection/camera/camera_android.c index 0622e668d..b09314d39 100644 --- a/src/detection/camera/camera_android.c +++ b/src/detection/camera/camera_android.c @@ -33,7 +33,7 @@ const char* ffDetectCamera(FF_A_UNUSED FFlist* result) { yyjson_val* device; size_t idx, max; yyjson_arr_foreach (root, idx, max, device) { - FFCameraResult* camera = (FFCameraResult*) ffListAdd(result); + FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result); { const char* facing = yyjson_get_str(yyjson_obj_get(device, "facing")); if (facing) { diff --git a/src/detection/camera/camera_apple.m b/src/detection/camera/camera_apple.m index a7d48d2fc..aa5c1cf91 100644 --- a/src/detection/camera/camera_apple.m +++ b/src/detection/camera/camera_apple.m @@ -37,7 +37,7 @@ const char* ffDetectCamera(FFlist* result) for (AVCaptureDevice* device in session.devices) { - FFCameraResult* camera = (FFCameraResult*) ffListAdd(result); + FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result); ffStrbufInitS(&camera->name, device.localizedName.UTF8String); ffStrbufInitS(&camera->vendor, device.manufacturer.UTF8String); ffStrbufInitS(&camera->id, device.uniqueID.UTF8String); diff --git a/src/detection/camera/camera_linux.c b/src/detection/camera/camera_linux.c index fa2dd03ff..71b446111 100644 --- a/src/detection/camera/camera_linux.c +++ b/src/detection/camera/camera_linux.c @@ -39,7 +39,7 @@ const char* ffDetectCamera(FFlist* result) { continue; } - FFCameraResult* camera = (FFCameraResult*) ffListAdd(result); + FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result); ffStrbufInitS(&camera->name, (const char*) cap.card); ffStrbufInit(&camera->vendor); ffStrbufInitS(&camera->id, (const char*) cap.bus_info); diff --git a/src/detection/camera/camera_windows.cpp b/src/detection/camera/camera_windows.cpp index ceb08a7df..e1fa90e07 100644 --- a/src/detection/camera/camera_windows.cpp +++ b/src/detection/camera/camera_windows.cpp @@ -48,7 +48,7 @@ extern "C" const char* ffDetectCamera(FF_A_UNUSED FFlist* result) { continue; } - FFCameraResult* camera = (FFCameraResult*) ffListAdd(result); + FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result); ffStrbufInitNWS(&camera->name, length, buffer); ffStrbufInit(&camera->colorspace); ffStrbufInit(&camera->vendor); diff --git a/src/detection/command/command.c b/src/detection/command/command.c index 112fb0ba0..28c6f4b72 100644 --- a/src/detection/command/command.c +++ b/src/detection/command/command.c @@ -8,9 +8,7 @@ typedef struct FFCommandResultBundle { } FFCommandResultBundle; // FIFO, non-thread-safe list of running commands -static FFlist commandQueue = { - .elementSize = sizeof(FFCommandResultBundle), -}; +static FFlist commandQueue; static const char* spawnProcess(FFCommandOptions* options, FFProcessHandle* handle) { if (options->text.length == 0) { @@ -32,7 +30,7 @@ bool ffPrepareCommand(FFCommandOptions* options) { return false; } - FFCommandResultBundle* bundle = ffListAdd(&commandQueue); + FFCommandResultBundle* bundle = FF_LIST_ADD(FFCommandResultBundle, commandQueue); bundle->error = spawnProcess(options, &bundle->handle); return true; @@ -42,7 +40,7 @@ const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result) { FFCommandResultBundle bundle = {}; if (!options->parallel) { bundle.error = spawnProcess(options, &bundle.handle); - } else if (!ffListShift(&commandQueue, &bundle)) { + } else if (!FF_LIST_SHIFT(commandQueue, &bundle)) { return "[BUG] command queue is empty"; } diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index 8c51d4334..cdd502473 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -776,7 +776,7 @@ static const char* detectPhysicalCores(FFCPUResult* cpu) { uint64_t pkgLow = 0, pkgHigh = 0; struct dirent* entry; - FF_LIST_AUTO_DESTROY cpuList = ffListCreate(sizeof(uint32_t)); + FF_LIST_AUTO_DESTROY cpuList = ffListCreate(); while ((entry = readdir(dir)) != NULL) { if (entry->d_type != DT_DIR || !ffStrStartsWith(entry->d_name, "cpu") || !ffCharIsDigit(entry->d_name[strlen("cpu")])) { continue; @@ -826,7 +826,7 @@ static const char* detectPhysicalCores(FFCPUResult* cpu) { } } if (!found) { - *(uint32_t*) ffListAdd(&cpuList) = coreId; + *FF_LIST_ADD(uint32_t, cpuList) = coreId; } p = strchr(pend, ','); diff --git a/src/detection/cpucache/cpucache.h b/src/detection/cpucache/cpucache.h index 5d512cd12..287154bbc 100644 --- a/src/detection/cpucache/cpucache.h +++ b/src/detection/cpucache/cpucache.h @@ -33,7 +33,7 @@ static inline FFCPUCache* ffCPUCacheAddItem(FFCPUCacheResult* result, uint32_t l } } - FFCPUCache* item = (FFCPUCache*) ffListAdd(cacheLevel); + FFCPUCache* item = FF_LIST_ADD(FFCPUCache, *cacheLevel); *item = (FFCPUCache) { .size = size, .num = 1, diff --git a/src/detection/cpuusage/cpuusage.c b/src/detection/cpuusage/cpuusage.c index 3013e99f7..a78ae7b13 100644 --- a/src/detection/cpuusage/cpuusage.c +++ b/src/detection/cpuusage/cpuusage.c @@ -8,19 +8,19 @@ static FFlist cpuTimes1; static uint64_t startTime; void ffPrepareCPUUsage(void) { - if (cpuTimes1.elementSize != 0) { + if (startTime != 0) { return; // Already prepared } - ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo)); + ffListInit(&cpuTimes1); ffGetCpuUsageInfo(&cpuTimes1); startTime = ffTimeGetNow(); } const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result) { const char* error = NULL; - if (cpuTimes1.elementSize == 0) { - ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo)); + if (startTime == 0) { + ffListInit(&cpuTimes1); error = ffGetCpuUsageInfo(&cpuTimes1); if (error) { return error; @@ -37,7 +37,7 @@ const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result) { return "No CPU cores found"; } - FF_LIST_AUTO_DESTROY cpuTimes2 = ffListCreate(sizeof(FFCpuUsageInfo)); + FF_LIST_AUTO_DESTROY cpuTimes2 = ffListCreate(); uint32_t retryCount = 0; retry: @@ -65,7 +65,7 @@ retry: for (uint32_t i = 0; i < cpuTimes1.length; ++i) { FFCpuUsageInfo* cpuTime1 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes1, i); FFCpuUsageInfo* cpuTime2 = FF_LIST_GET(FFCpuUsageInfo, cpuTimes2, i); - *(double*) ffListAdd(result) = (double) (cpuTime2->inUseAll - cpuTime1->inUseAll) / (double) (cpuTime2->totalAll - cpuTime1->totalAll) * 100; + *FF_LIST_ADD(double, *result) = (double) (cpuTime2->inUseAll - cpuTime1->inUseAll) / (double) (cpuTime2->totalAll - cpuTime1->totalAll) * 100; cpuTime1->inUseAll = cpuTime2->inUseAll; cpuTime1->totalAll = cpuTime2->totalAll; } diff --git a/src/detection/cpuusage/cpuusage_apple.c b/src/detection/cpuusage/cpuusage_apple.c index a3e9aa67f..50a8b15b4 100644 --- a/src/detection/cpuusage/cpuusage_apple.c +++ b/src/detection/cpuusage/cpuusage_apple.c @@ -21,7 +21,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { integer_t inUse = cpuInfo[CPU_STATE_MAX * i + CPU_STATE_USER] + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_SYSTEM] + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_NICE]; integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE]; - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = (uint64_t) inUse, .totalAll = (uint64_t) total, diff --git a/src/detection/cpuusage/cpuusage_bsd.c b/src/detection/cpuusage/cpuusage_bsd.c index 73cd9fd57..6989380b1 100644 --- a/src/detection/cpuusage/cpuusage_bsd.c +++ b/src/detection/cpuusage/cpuusage_bsd.c @@ -47,7 +47,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { uint64_t inUse = cpTime[CP_USER] + cpTime[CP_NICE] + cpTime[CP_SYS] + cpTime[CP_INTR]; uint64_t total = inUse + cpTime[CP_IDLE]; - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = inUse, .totalAll = total, diff --git a/src/detection/cpuusage/cpuusage_haiku.c b/src/detection/cpuusage/cpuusage_haiku.c index 3089b8549..f4d3c632d 100644 --- a/src/detection/cpuusage/cpuusage_haiku.c +++ b/src/detection/cpuusage/cpuusage_haiku.c @@ -18,7 +18,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { uint64_t uptime = (uint64_t) system_time(); for (uint32_t i = 0; i < sysInfo.cpu_count; ++i) { - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); info->inUseAll = (uint64_t) cpuInfo[i].active_time; info->totalAll = uptime; } diff --git a/src/detection/cpuusage/cpuusage_linux.c b/src/detection/cpuusage/cpuusage_linux.c index 6d447a59b..fd2f43bb8 100644 --- a/src/detection/cpuusage/cpuusage_linux.c +++ b/src/detection/cpuusage/cpuusage_linux.c @@ -31,7 +31,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { uint64_t inUse = user + nice + system + irq + softirq; uint64_t total = inUse + idle + iowait; - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = inUse, .totalAll = total, diff --git a/src/detection/cpuusage/cpuusage_sunos.c b/src/detection/cpuusage/cpuusage_sunos.c index 4f24dfdc2..813d74d24 100644 --- a/src/detection/cpuusage/cpuusage_sunos.c +++ b/src/detection/cpuusage/cpuusage_sunos.c @@ -28,7 +28,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes) { uint64_t inUse = cs.cpu_sysinfo.cpu[CPU_USER] + cs.cpu_sysinfo.cpu[CPU_KERNEL]; uint64_t total = inUse + cs.cpu_sysinfo.cpu[CPU_IDLE] + cs.cpu_sysinfo.cpu[CPU_WAIT]; - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = inUse, .totalAll = total, diff --git a/src/detection/cpuusage/cpuusage_windows.c b/src/detection/cpuusage/cpuusage_windows.c index d2f984b84..c23c28ac1 100644 --- a/src/detection/cpuusage/cpuusage_windows.c +++ b/src/detection/cpuusage/cpuusage_windows.c @@ -28,7 +28,7 @@ static const char* getInfoByNqsi(FFlist* cpuTimes) { uint64_t inUse = (uint64_t) (coreInfo->UserTime.QuadPart + coreInfo->KernelTime.QuadPart); uint64_t total = inUse + (uint64_t) coreInfo->IdleTime.QuadPart; - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = inUse, .totalAll = total, @@ -138,7 +138,7 @@ static const char* getInfoByPerflib(FFlist* cpuTimes) { return "Counter \"% Processor Utility\" are not supported"; } - FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes); *info = (FFCpuUsageInfo) { .inUseAll = processorUtility, .totalAll = utilityBase, diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index 758a5c0cb..78f82084b 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -18,7 +18,7 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) { // For example for /boot/efi/bootmgr we need to check /boot/efi before /boot // Note that we sort alphabetically here for a better ordering when printing the list, // so the check must be done in reverse order - ffListSort(disks, (void*) compareDisks); + ffListSort(disks, sizeof(FFDisk), (void*) compareDisks); FF_LIST_FOR_EACH (FFDisk, disk, *disks) { if (disk->bytesTotal == 0) { disk->type |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 02d632019..438a01ceb 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -176,7 +176,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { } #endif - FFDisk* disk = ffListAdd(disks); + FFDisk* disk = FF_LIST_ADD(FFDisk, *disks); disk->bytesTotal = (uint64_t) fs->f_blocks * (uint64_t) fs->f_bsize; disk->bytesFree = (uint64_t) fs->f_bfree * (uint64_t) fs->f_bsize; diff --git a/src/detection/disk/disk_haiku.cpp b/src/detection/disk/disk_haiku.cpp index b6b37d91a..094d8c045 100644 --- a/src/detection/disk/disk_haiku.cpp +++ b/src/detection/disk/disk_haiku.cpp @@ -36,7 +36,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { continue; } - FFDisk* disk = (FFDisk*) ffListAdd(disks); + FFDisk* disk = FF_LIST_ADD(FFDisk, *disks); disk->bytesTotal = (uint64_t) fs.total_blocks * (uint64_t) fs.block_size; disk->bytesFree = (uint64_t) fs.free_blocks * (uint64_t) fs.block_size; diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 7a004b5de..7dcf6ecee 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -308,7 +308,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { } // We have a valid device, add it to the list - FFDisk* disk = ffListAdd(disks); + FFDisk* disk = FF_LIST_ADD(FFDisk, *disks); disk->type = FF_DISK_VOLUME_TYPE_NONE; // detect mountFrom diff --git a/src/detection/disk/disk_sunos.c b/src/detection/disk/disk_sunos.c index 9dd64a1a2..e2382f4fb 100644 --- a/src/detection/disk/disk_sunos.c +++ b/src/detection/disk/disk_sunos.c @@ -131,7 +131,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { } // We have a valid device, add it to the list - FFDisk* disk = ffListAdd(disks); + FFDisk* disk = FF_LIST_ADD(FFDisk, *disks); disk->type = FF_DISK_VOLUME_TYPE_NONE; ffStrbufInitS(&disk->mountFrom, device.mnt_special); ffStrbufInitS(&disk->mountpoint, device.mnt_mountp); diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index 89c78d20b..0b3c93d3e 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -66,7 +66,7 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) { } } - FFDisk* disk = ffListAdd(disks); + FFDisk* disk = FF_LIST_ADD(FFDisk, *disks); disk->filesUsed = 0; disk->filesTotal = 0; diff --git a/src/detection/diskio/diskio.c b/src/detection/diskio/diskio.c index cbf25e19e..6ff66be44 100644 --- a/src/detection/diskio/diskio.c +++ b/src/detection/diskio/diskio.c @@ -16,7 +16,7 @@ void ffPrepareDiskIO(FFDiskIOOptions* options) { return; // Already prepared } - ffListInit(&ioCounters1, sizeof(FFDiskIOResult)); + ffListInit(&ioCounters1); ffDiskIOGetIoCounters(&ioCounters1, options); time1 = ffTimeGetNow(); } @@ -33,7 +33,7 @@ const char* ffDetectDiskIO(FFlist* result, FFDiskIOOptions* options) { } if (time1 == 0) { - ffListInit(&ioCounters1, sizeof(FFDiskIOResult)); + ffListInit(&ioCounters1); error = ffDiskIOGetIoCounters(&ioCounters1, options); if (error) { return error; diff --git a/src/detection/diskio/diskio_apple.c b/src/detection/diskio/diskio_apple.c index 6ebad86ab..dc2890701 100644 --- a/src/detection/diskio/diskio_apple.c +++ b/src/detection/diskio/diskio_apple.c @@ -37,7 +37,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitS(&device->name, deviceName); ffStrbufInit(&device->devPath); diff --git a/src/detection/diskio/diskio_bsd.c b/src/detection/diskio/diskio_bsd.c index 3b8b11d80..ffca1235b 100644 --- a/src/detection/diskio/diskio_bsd.c +++ b/src/detection/diskio/diskio_bsd.c @@ -57,7 +57,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name); device->bytesRead = snapIter->bytes[DEVSTAT_READ]; device->readCount = snapIter->operations[DEVSTAT_READ]; @@ -102,7 +102,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitS(&device->name, deviceName); ffStrbufInitF(&device->devPath, "/dev/%s", deviceName); device->bytesRead = current->bytes_read; diff --git a/src/detection/diskio/diskio_linux.c b/src/detection/diskio/diskio_linux.c index 90c374405..a0ee44e99 100644 --- a/src/detection/diskio/diskio_linux.c +++ b/src/detection/diskio/diskio_linux.c @@ -64,7 +64,7 @@ static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* res } } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitMove(&device->name, &name); ffStrbufInitF(&device->devPath, "/dev/%s", devName); device->bytesRead = sectorRead * 512; diff --git a/src/detection/diskio/diskio_nbsd.c b/src/detection/diskio/diskio_nbsd.c index ec156267a..71f0bfa08 100644 --- a/src/detection/diskio/diskio_nbsd.c +++ b/src/detection/diskio/diskio_nbsd.c @@ -37,7 +37,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitNS(&device->devPath, (uint32_t) (end - path), path); ffStrbufInitS(&device->name, st->name); device->bytesRead = st->rbytes; diff --git a/src/detection/diskio/diskio_obsd.c b/src/detection/diskio/diskio_obsd.c index 34a58f438..cab0abe05 100644 --- a/src/detection/diskio/diskio_obsd.c +++ b/src/detection/diskio/diskio_obsd.c @@ -26,7 +26,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInitF(&device->devPath, "/dev/%s", st->ds_name); ffStrbufInitS(&device->name, st->ds_name); device->bytesRead = st->ds_rbytes; diff --git a/src/detection/diskio/diskio_sunos.c b/src/detection/diskio/diskio_sunos.c index 8149e9dcb..0a1db093c 100644 --- a/src/detection/diskio/diskio_sunos.c +++ b/src/detection/diskio/diskio_sunos.c @@ -29,7 +29,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { continue; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); ffStrbufInit(&device->devPath); // unlike other platforms, `/dev/ks_name` is not available ffStrbufInitS(&device->name, ks->ks_name); device->bytesRead = kio.nread; diff --git a/src/detection/diskio/diskio_windows.c b/src/detection/diskio/diskio_windows.c index 52be20340..80ad94e3d 100644 --- a/src/detection/diskio/diskio_windows.c +++ b/src/detection/diskio/diskio_windows.c @@ -29,7 +29,7 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIO return true; } - FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result); STORAGE_DEVICE_DESCRIPTOR* sdd = (STORAGE_DEVICE_DESCRIPTOR*) sddBuffer; ffStrbufInit(&device->name); diff --git a/src/detection/displayserver/displayserver.c b/src/detection/displayserver/displayserver.c index 40f7c1261..58c8c8912 100644 --- a/src/detection/displayserver/displayserver.c +++ b/src/detection/displayserver/displayserver.c @@ -21,7 +21,7 @@ FFDisplayResult* ffdsAppendDisplay( return NULL; } - FFDisplayResult* display = (FFDisplayResult*) ffListAdd(&result->displays); + FFDisplayResult* display = FF_LIST_ADD(FFDisplayResult, result->displays); display->width = width; display->height = height; display->refreshRate = refreshRate; @@ -52,13 +52,15 @@ void ffConnectDisplayServerImpl(FFDisplayServerResult* ds); const FFDisplayServerResult* ffConnectDisplayServer() { static FFDisplayServerResult result; - if (result.displays.elementSize == 0) { + static bool initialized = false; + if (!initialized) { + initialized = true; ffStrbufInit(&result.wmProcessName); ffStrbufInit(&result.wmPrettyName); ffStrbufInit(&result.wmProtocolName); ffStrbufInit(&result.deProcessName); ffStrbufInit(&result.dePrettyName); - ffListInit(&result.displays, sizeof(FFDisplayResult)); + ffListInit(&result.displays); ffConnectDisplayServerImpl(&result); } return &result; diff --git a/src/detection/displayserver/linux/wayland/kde-output.c b/src/detection/displayserver/linux/wayland/kde-output.c index 2c5d2a906..8d502c45c 100644 --- a/src/detection/displayserver/linux/wayland/kde-output.c +++ b/src/detection/displayserver/linux/wayland/kde-output.c @@ -44,7 +44,7 @@ static void waylandKdeModeListener(void* data, FF_A_UNUSED struct kde_output_dev return; } - WaylandKdeMode* newMode = ffListAdd((FFlist*) wldata->internal); + WaylandKdeMode* newMode = FF_LIST_ADD(WaylandKdeMode, *(FFlist*) wldata->internal); *newMode = (WaylandKdeMode) { .pMode = mode }; // Strangely, the listener is called only in this function, but not in `waylandKdeCurrentModeListener` @@ -188,7 +188,7 @@ const char* ffWaylandHandleKdeOutput(WaylandData* wldata, struct wl_registry* re return "Failed to create kde_output_device_v2"; } - FF_LIST_AUTO_DESTROY modes = ffListCreate(sizeof(WaylandKdeMode)); + FF_LIST_AUTO_DESTROY modes = ffListCreate(); WaylandDisplay display = { .parent = wldata, .transform = WL_OUTPUT_TRANSFORM_NORMAL, diff --git a/src/detection/displayserver/linux/wayland/zwlr-output.c b/src/detection/displayserver/linux/wayland/zwlr-output.c index d24142a2c..9521dff94 100644 --- a/src/detection/displayserver/linux/wayland/zwlr-output.c +++ b/src/detection/displayserver/linux/wayland/zwlr-output.c @@ -50,7 +50,7 @@ static void waylandZwlrModeListener(void* data, FF_A_UNUSED struct zwlr_output_h return; } - WaylandZwlrMode* newMode = ffListAdd((FFlist*) wldata->internal); + WaylandZwlrMode* newMode = FF_LIST_ADD(WaylandZwlrMode, *(FFlist*) wldata->internal); *newMode = (WaylandZwlrMode) { .pMode = mode }; // Strangely, the listener is called only in this function, but not in `waylandZwlrCurrentModeListener` @@ -118,7 +118,7 @@ static const struct zwlr_output_head_v1_listener headListener = { static void waylandHandleZwlrHead(void* data, FF_A_UNUSED struct zwlr_output_manager_v1* zwlr_output_manager_v1, struct zwlr_output_head_v1* head) { WaylandData* wldata = data; - FF_LIST_AUTO_DESTROY modes = ffListCreate(sizeof(WaylandZwlrMode)); + FF_LIST_AUTO_DESTROY modes = ffListCreate(); WaylandDisplay display = { .parent = wldata, .transform = WL_OUTPUT_TRANSFORM_NORMAL, diff --git a/src/detection/dns/dns_apple.c b/src/detection/dns/dns_apple.c index 10ea519b8..2facd8fc9 100644 --- a/src/detection/dns/dns_apple.c +++ b/src/detection/dns/dns_apple.c @@ -48,7 +48,7 @@ static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FF continue; } - FFstrbuf* item = (FFstrbuf*) ffListAdd(results); + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); ffStrbufInitS(item, nameserver); ffStrbufTrimRightSpace(item); FF_DEBUG("Found DNS server: %s", item->chars); @@ -85,7 +85,7 @@ const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) { } // Add to results - FFstrbuf* item = (FFstrbuf*) ffListAdd(results); + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); ffStrbufInitMove(item, &buffer); FF_DEBUG("Found DNS server on macOS: %s", item->chars); } diff --git a/src/detection/dns/dns_linux.c b/src/detection/dns/dns_linux.c index 6897c9c69..ecdb5f1c2 100644 --- a/src/detection/dns/dns_linux.c +++ b/src/detection/dns/dns_linux.c @@ -51,7 +51,7 @@ static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FF continue; } - FFstrbuf* item = (FFstrbuf*) ffListAdd(results); + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); ffStrbufInitS(item, nameserver); ffStrbufTrimRightSpace(item); FF_DEBUG("Found DNS server: %s", item->chars); diff --git a/src/detection/dns/dns_windows.c b/src/detection/dns/dns_windows.c index 5c3c05f04..b28eefb9e 100644 --- a/src/detection/dns/dns_windows.c +++ b/src/detection/dns/dns_windows.c @@ -46,7 +46,7 @@ const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) { } for (IP_ADAPTER_DNS_SERVER_ADDRESS_XP* ifa = adapter->FirstDnsServerAddress; ifa; ifa = ifa->Next) { - FFstrbuf* item = (FFstrbuf*) ffListAdd(results); + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); if (ifa->Address.lpSockaddr->sa_family == AF_INET) { SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr; ffStrbufInitA(item, INET_ADDRSTRLEN); diff --git a/src/detection/gamepad/gamepad_apple.c b/src/detection/gamepad/gamepad_apple.c index b10b04b7f..c262c8668 100644 --- a/src/detection/gamepad/gamepad_apple.c +++ b/src/detection/gamepad/gamepad_apple.c @@ -6,7 +6,7 @@ #include static void enumSet(IOHIDDeviceRef value, FFlist* results) { - FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(results); + FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *results); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); device->battery = 0; diff --git a/src/detection/gamepad/gamepad_bsd.c b/src/detection/gamepad/gamepad_bsd.c index b39446a03..99addca76 100644 --- a/src/detection/gamepad/gamepad_bsd.c +++ b/src/detection/gamepad/gamepad_bsd.c @@ -49,7 +49,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) { struct usb_device_info di; if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) { - FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices); ffStrbufInitS(&device->serial, di.udi_serial); ffStrbufInitF(&device->name, "%s %s", di.udi_vendor, di.udi_product); device->battery = 0; diff --git a/src/detection/gamepad/gamepad_haiku.cpp b/src/detection/gamepad/gamepad_haiku.cpp index 8051f19fd..50b43a043 100644 --- a/src/detection/gamepad/gamepad_haiku.cpp +++ b/src/detection/gamepad/gamepad_haiku.cpp @@ -8,7 +8,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) { for (int32 i = 0, n = js.CountDevices(); i < n; ++i) { char name[B_OS_NAME_LENGTH]; if (js.GetDeviceName(i, name) == B_OK) { - FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices); ffStrbufInit(&device->serial); ffStrbufInitS(&device->name, name); device->battery = 0; diff --git a/src/detection/gamepad/gamepad_linux.c b/src/detection/gamepad/gamepad_linux.c index f7c9276ba..c55ba7343 100644 --- a/src/detection/gamepad/gamepad_linux.c +++ b/src/detection/gamepad/gamepad_linux.c @@ -4,7 +4,7 @@ static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) { uint32_t baseLen = path->length; - FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices); ffStrbufInit(&device->serial); ffStrbufInitMove(&device->name, name); device->battery = 0; diff --git a/src/detection/gamepad/gamepad_windows.c b/src/detection/gamepad/gamepad_windows.c index 93e6495cc..9cbb5618e 100644 --- a/src/detection/gamepad/gamepad_windows.c +++ b/src/detection/gamepad/gamepad_windows.c @@ -113,7 +113,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) { continue; } - FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + FFGamepadDevice* device = FF_LIST_ADD(FFGamepadDevice, *devices); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); device->battery = 0; diff --git a/src/detection/gpu/gpu.c b/src/detection/gpu/gpu.c index ef8652c1f..1062d90ad 100644 --- a/src/detection/gpu/gpu.c +++ b/src/detection/gpu/gpu.c @@ -90,7 +90,7 @@ const char* detectByOpenGL(FFlist* gpus) { FF_DEBUG("OpenGL detection returns: %s", error ?: "success"); if (!error) { - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); gpu->type = FF_GPU_TYPE_UNKNOWN; ffStrbufInitMove(&gpu->vendor, &result.vendor); ffStrbufInitMove(&gpu->name, &result.renderer); diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index bc613be74..dec6770b0 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -112,7 +112,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) { continue; } - FFGPUResult* gpu = ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); gpu->index = FF_GPU_INDEX_UNSET; ffStrbufInit(&gpu->memoryType); gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; diff --git a/src/detection/gpu/gpu_bsd.c b/src/detection/gpu/gpu_bsd.c index 57e97cbad..6957346bd 100644 --- a/src/detection/gpu/gpu_bsd.c +++ b/src/detection/gpu/gpu_bsd.c @@ -56,7 +56,7 @@ static const char* detectByDrm(const FFGPUOptions* options, FFlist* gpus) { const char* path = dev->nodes[DRM_NODE_PRIMARY]; - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInit(&gpu->vendor); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); @@ -179,7 +179,7 @@ static const char* detectByPci(const FFGPUOptions* options, FFlist* gpus) { continue; // Likely an auxiliary display controller (#2034) } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString(pc->pc_vendor)); ffStrbufInit(&gpu->name); ffStrbufInitS(&gpu->driver, pc->pd_name); diff --git a/src/detection/gpu/gpu_gnu.c b/src/detection/gpu/gpu_gnu.c index f3089a9aa..27c774b20 100644 --- a/src/detection/gpu/gpu_gnu.c +++ b/src/detection/gpu/gpu_gnu.c @@ -117,7 +117,7 @@ const char* ffDetectGPUImpl(FF_A_UNUSED const FFGPUOptions* options, FFlist* gpu uint16_t vendorId = data[PCI_VENDOR_ID] | (data[PCI_VENDOR_ID + 1] << 8); uint16_t deviceId = data[PCI_DEVICE_ID] | (data[PCI_DEVICE_ID + 1] << 8); - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString(vendorId)); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); diff --git a/src/detection/gpu/gpu_haiku.c b/src/detection/gpu/gpu_haiku.c index e4257a105..f75f03980 100644 --- a/src/detection/gpu/gpu_haiku.c +++ b/src/detection/gpu/gpu_haiku.c @@ -24,7 +24,7 @@ const char* ffDetectGPUImpl(FF_A_UNUSED const FFGPUOptions* options, FFlist* gpu continue; // Likely an auxiliary display controller (#2034) } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString(dev.vendor_id)); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index dfb595dc9..d636a7c96 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -392,7 +392,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf return "Likely an auxiliary display controller"; // #2034 } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString((uint16_t) vendorId)); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); @@ -528,7 +528,7 @@ static const char* detectOf(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, co ++name; } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); gpu->index = FF_GPU_INDEX_UNSET; gpu->deviceId = 0; ffStrbufInit(&gpu->name); diff --git a/src/detection/gpu/gpu_nbsd.c b/src/detection/gpu/gpu_nbsd.c index eb90e7be9..4f5dab3c9 100644 --- a/src/detection/gpu/gpu_nbsd.c +++ b/src/detection/gpu/gpu_nbsd.c @@ -83,7 +83,7 @@ const char* ffDetectGPUImpl(FF_A_UNUSED const FFGPUOptions* options, FFlist* gpu continue; // Likely an auxiliary display controller (#2034) } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString(PCI_VENDOR(pciid))); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); diff --git a/src/detection/gpu/gpu_obsd.c b/src/detection/gpu/gpu_obsd.c index b116580e4..dabadedd1 100644 --- a/src/detection/gpu/gpu_obsd.c +++ b/src/detection/gpu/gpu_obsd.c @@ -72,7 +72,7 @@ const char* ffDetectGPUImpl(FF_A_UNUSED const FFGPUOptions* options, FFlist* gpu continue; // Likely an auxiliary display controller (#2034) } - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitStatic(&gpu->vendor, ffGPUGetVendorString(PCI_VENDOR(pciid))); ffStrbufInit(&gpu->name); ffStrbufInit(&gpu->driver); diff --git a/src/detection/gpu/gpu_sunos.c b/src/detection/gpu/gpu_sunos.c index 3ea3fd329..f739b6207 100644 --- a/src/detection/gpu/gpu_sunos.c +++ b/src/detection/gpu/gpu_sunos.c @@ -7,7 +7,7 @@ static int walkDevTree(di_node_t node, FF_A_UNUSED di_minor_t minor, FFlist* gpu int* vendorId; int* deviceId; if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "vendor-id", &vendorId) > 0 && di_prop_lookup_ints(DDI_DEV_T_ANY, node, "device-id", &deviceId) > 0) { - FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus); ffStrbufInitS(&gpu->vendor, ffGPUGetVendorString((uint16_t) *vendorId)); ffStrbufInit(&gpu->name); ffStrbufInitS(&gpu->driver, di_driver_name(node)); diff --git a/src/detection/gpu/gpu_windows.c b/src/detection/gpu/gpu_windows.c index 07259d9a4..f0cd1d000 100644 --- a/src/detection/gpu/gpu_windows.c +++ b/src/detection/gpu/gpu_windows.c @@ -32,13 +32,15 @@ static bool queryDeviceIdsFallback(D3DKMT_ADAPTERADDRESS adapterAddress, D3DKMT_ } static FFlist deviceIdsCache; + static bool initialized; typedef struct { D3DKMT_DEVICE_IDS deviceIds; D3DKMT_ADAPTERADDRESS adapterAddress; } CacheEntry; - if (deviceIdsCache.elementSize == 0) { - ffListInit(&deviceIdsCache, sizeof(CacheEntry)); + if (!initialized) { + initialized = true; + ffListInit(&deviceIdsCache); ULONG devIdListSize = 0; if (CM_Get_Device_ID_List_SizeW(&devIdListSize, GUID_DEVCLASS_DISPLAY_STRING, CM_GETIDLIST_FILTER_CLASS | CM_GETIDLIST_FILTER_PRESENT) != CR_SUCCESS || devIdListSize <= 1) { diff --git a/src/detection/keyboard/keyboard_apple.c b/src/detection/keyboard/keyboard_apple.c index 2a230aed2..e2ecc37e4 100644 --- a/src/detection/keyboard/keyboard_apple.c +++ b/src/detection/keyboard/keyboard_apple.c @@ -6,7 +6,7 @@ #include static void enumSet(IOHIDDeviceRef value, FFlist* results) { - FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(results); + FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *results); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); diff --git a/src/detection/keyboard/keyboard_bsd.c b/src/detection/keyboard/keyboard_bsd.c index f18a79515..eee6d3a52 100644 --- a/src/detection/keyboard/keyboard_bsd.c +++ b/src/detection/keyboard/keyboard_bsd.c @@ -18,7 +18,7 @@ static const char* detectByIoctl(FFlist* devices) { return "ioctl(KDGKBINFO) failed"; } - FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(devices); + FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices); switch (kbdInfo.kb_type) { case KB_84: @@ -69,7 +69,7 @@ static const char* detectByUsbhid(FFlist* devices) { struct usb_device_info di; if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) { - FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(devices); + FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices); ffStrbufInitS(&device->serial, di.udi_serial); ffStrbufInitS(&device->name, di.udi_product); } diff --git a/src/detection/keyboard/keyboard_haiku.cpp b/src/detection/keyboard/keyboard_haiku.cpp index 644dbc87b..dc1d28a0e 100644 --- a/src/detection/keyboard/keyboard_haiku.cpp +++ b/src/detection/keyboard/keyboard_haiku.cpp @@ -18,7 +18,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) { continue; } - FFKeyboardDevice* item = (FFKeyboardDevice*) ffListAdd(devices); + FFKeyboardDevice* item = FF_LIST_ADD(FFKeyboardDevice, *devices); ffStrbufInit(&item->serial); ffStrbufInitS(&item->name, device->Name()); } diff --git a/src/detection/keyboard/keyboard_linux.c b/src/detection/keyboard/keyboard_linux.c index 3d0630ca9..343cc57eb 100644 --- a/src/detection/keyboard/keyboard_linux.c +++ b/src/detection/keyboard/keyboard_linux.c @@ -83,7 +83,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) { case '\0': // End of device entry; add to list if it has a name. if (device.name.length > 0) { - FFKeyboardDevice* added = (FFKeyboardDevice*) ffListAdd(devices); + FFKeyboardDevice* added = FF_LIST_ADD(FFKeyboardDevice, *devices); ffStrbufInitMove(&added->name, &device.name); ffStrbufInitMove(&added->serial, &device.serial); } diff --git a/src/detection/keyboard/keyboard_windows.c b/src/detection/keyboard/keyboard_windows.c index 0789623f8..272f632e5 100644 --- a/src/detection/keyboard/keyboard_windows.c +++ b/src/detection/keyboard/keyboard_windows.c @@ -43,7 +43,7 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */) { continue; } - FFKeyboardDevice* device = (FFKeyboardDevice*) ffListAdd(devices); + FFKeyboardDevice* device = FF_LIST_ADD(FFKeyboardDevice, *devices); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 997e2ef38..2a116704e 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -172,8 +172,10 @@ static FFLocalIpIpv6Type getIpv6Type(struct ifaddrs* ifa) { return result; #elif __linux__ static FFlist addresses = {}; - if (addresses.elementSize == 0) { - ffListInit(&addresses, sizeof(struct in6_addr)); + static bool initialized = false; + if (!initialized) { + initialized = true; + ffListInit(&addresses); FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); if (!ffReadFileBuffer("/proc/net/if_inet6", &buffer)) { return result; @@ -182,7 +184,7 @@ static FFLocalIpIpv6Type getIpv6Type(struct ifaddrs* ifa) { char* line = NULL; size_t len = 0; while (ffStrbufGetline(&line, &len, &buffer)) { - struct in6_addr* entry = (struct in6_addr*) ffListAdd(&addresses); + struct in6_addr* entry = FF_LIST_ADD(struct in6_addr, addresses); uint8_t flags; if (sscanf(line, "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 "%2" SCNx8 " %*s %*s %*s %" SCNx8 " %*s", &entry->s6_addr[0], &entry->s6_addr[1], &entry->s6_addr[2], &entry->s6_addr[3], &entry->s6_addr[4], &entry->s6_addr[5], &entry->s6_addr[6], &entry->s6_addr[7], &entry->s6_addr[8], &entry->s6_addr[9], &entry->s6_addr[10], &entry->s6_addr[11], &entry->s6_addr[12], &entry->s6_addr[13], &entry->s6_addr[14], &entry->s6_addr[15], &flags) != 17 || (!IN6_IS_ADDR_GLOBAL(entry) && !IN6_IS_ADDR_UNIQUE_LOCAL(entry)) || @@ -284,7 +286,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) { FF_DEBUG("Successfully retrieved interface addresses"); - FF_LIST_AUTO_DESTROY adapters = ffListCreate(sizeof(FFAdapter)); + FF_LIST_AUTO_DESTROY adapters = ffListCreate(); for (struct ifaddrs* ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next) { if (!ifa->ifa_addr) { @@ -341,11 +343,11 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) { } } if (!adapter) { - adapter = ffListAdd(&adapters); + adapter = FF_LIST_ADD(FFAdapter, adapters); *adapter = (FFAdapter) { .mac = ifa, - .ipv4 = ffListCreate(sizeof(struct ifaddrs*)), - .ipv6 = ffListCreate(sizeof(struct ifaddrs*)), + .ipv4 = ffListCreate(), + .ipv6 = ffListCreate(), }; FF_DEBUG("Created new adapter entry for interface %s", ifa->ifa_name); } diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index d2311865b..6a7e0e79b 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -114,7 +114,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) { processedCount++; FF_DEBUG("Creating result item for adapter %u ('%s')", (unsigned) adapter->IfIndex, name.chars); - FFLocalIpResult* item = (FFLocalIpResult*) ffListAdd(results); + FFLocalIpResult* item = FF_LIST_ADD(FFLocalIpResult, *results); ffStrbufInitMove(&item->name, &name); ffStrbufInit(&item->ipv4); ffStrbufInit(&item->ipv6); diff --git a/src/detection/mouse/mouse_apple.c b/src/detection/mouse/mouse_apple.c index 8df2a9255..2c5b2d7e3 100644 --- a/src/detection/mouse/mouse_apple.c +++ b/src/detection/mouse/mouse_apple.c @@ -6,7 +6,7 @@ #include static void enumSet(IOHIDDeviceRef value, FFlist* results) { - FFMouseDevice* device = (FFMouseDevice*) ffListAdd(results); + FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *results); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); diff --git a/src/detection/mouse/mouse_bsd.c b/src/detection/mouse/mouse_bsd.c index be0a73dae..f7d0ace7f 100644 --- a/src/detection/mouse/mouse_bsd.c +++ b/src/detection/mouse/mouse_bsd.c @@ -41,7 +41,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) { struct usb_device_info di; if (ioctl(fd, USB_GET_DEVICEINFO, &di) != -1) { - FFMouseDevice* device = (FFMouseDevice*) ffListAdd(devices); + FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices); ffStrbufInitS(&device->serial, di.udi_serial); ffStrbufInitS(&device->name, di.udi_product); } diff --git a/src/detection/mouse/mouse_haiku.cpp b/src/detection/mouse/mouse_haiku.cpp index c89cac281..5835c8ec2 100644 --- a/src/detection/mouse/mouse_haiku.cpp +++ b/src/detection/mouse/mouse_haiku.cpp @@ -18,7 +18,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) { continue; } - FFMouseDevice* item = (FFMouseDevice*) ffListAdd(devices); + FFMouseDevice* item = FF_LIST_ADD(FFMouseDevice, *devices); ffStrbufInit(&item->serial); ffStrbufInitS(&item->name, device->Name()); } diff --git a/src/detection/mouse/mouse_linux.c b/src/detection/mouse/mouse_linux.c index 500e65015..a608f88aa 100644 --- a/src/detection/mouse/mouse_linux.c +++ b/src/detection/mouse/mouse_linux.c @@ -28,7 +28,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) { ffStrbufTrimRightSpace(&name); ffStrbufSubstrBefore(&path, path.length - 4); - FFMouseDevice* device = (FFMouseDevice*) ffListAdd(devices); + FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices); ffStrbufInitMove(&device->name, &name); ffStrbufInit(&device->serial); diff --git a/src/detection/mouse/mouse_windows.c b/src/detection/mouse/mouse_windows.c index ade28f1c9..3f2370a9a 100644 --- a/src/detection/mouse/mouse_windows.c +++ b/src/detection/mouse/mouse_windows.c @@ -42,7 +42,7 @@ const char* ffDetectMouse(FFlist* devices /* List of FFMouseDevice */) { continue; } - FFMouseDevice* device = (FFMouseDevice*) ffListAdd(devices); + FFMouseDevice* device = FF_LIST_ADD(FFMouseDevice, *devices); ffStrbufInit(&device->serial); ffStrbufInit(&device->name); diff --git a/src/detection/netio/netio.c b/src/detection/netio/netio.c index 8cb71e1de..c6b9da61b 100644 --- a/src/detection/netio/netio.c +++ b/src/detection/netio/netio.c @@ -14,7 +14,7 @@ void ffPrepareNetIO(FFNetIOOptions* options) { return; // Already prepared } - ffListInit(&ioCounters1, sizeof(FFNetIOResult)); + ffListInit(&ioCounters1); ffNetIOGetIoCounters(&ioCounters1, options); time1 = ffTimeGetNow(); } @@ -31,7 +31,7 @@ const char* ffDetectNetIO(FFlist* result, FFNetIOOptions* options) { } if (time1 == 0) { - ffListInit(&ioCounters1, sizeof(FFNetIOResult)); + ffListInit(&ioCounters1); error = ffNetIOGetIoCounters(&ioCounters1, options); if (error) { return error; diff --git a/src/detection/netio/netio_apple.c b/src/detection/netio/netio_apple.c index 72e021a79..125cfd90c 100644 --- a/src/detection/netio/netio_apple.c +++ b/src/detection/netio/netio_apple.c @@ -36,7 +36,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { continue; } - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); *counters = (FFNetIOResult) { .name = ffStrbufCreateS(mibdata->ifmd_name), .txBytes = mibdata->ifmd_data.ifi_obytes, diff --git a/src/detection/netio/netio_bsd.c b/src/detection/netio/netio_bsd.c index 9bfa65053..83c1b6ba0 100644 --- a/src/detection/netio/netio_bsd.c +++ b/src/detection/netio/netio_bsd.c @@ -39,7 +39,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { continue; } - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); *counters = (FFNetIOResult) { .name = ffStrbufCreateNS(sdl->sdl_nlen, sdl->sdl_data), .txBytes = ifm->ifm_data.ifi_obytes, diff --git a/src/detection/netio/netio_haiku.cpp b/src/detection/netio/netio_haiku.cpp index 352ec4440..88a123d1d 100644 --- a/src/detection/netio/netio_haiku.cpp +++ b/src/detection/netio/netio_haiku.cpp @@ -33,7 +33,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { continue; } - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); *counters = (FFNetIOResult) { .name = ffStrbufCreateS(interface.Name()), .defaultRoute = defaultRoute, diff --git a/src/detection/netio/netio_linux.c b/src/detection/netio/netio_linux.c index 72add989f..aa48c0114 100644 --- a/src/detection/netio/netio_linux.c +++ b/src/detection/netio/netio_linux.c @@ -18,7 +18,7 @@ static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, i return; } - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); ffStrbufInitS(&counters->name, ifName); counters->defaultRoute = isDefaultRoute; diff --git a/src/detection/netio/netio_sunos.c b/src/detection/netio/netio_sunos.c index ff32f6d4f..553634a33 100644 --- a/src/detection/netio/netio_sunos.c +++ b/src/detection/netio/netio_sunos.c @@ -37,7 +37,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { continue; } - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); kstat_named_t* wbytes = (kstat_named_t*) kstat_data_lookup(ks, "obytes64"); kstat_named_t* rbytes = (kstat_named_t*) kstat_data_lookup(ks, "rbytes64"); diff --git a/src/detection/netio/netio_windows.c b/src/detection/netio/netio_windows.c index 3fb422870..3c6433425 100644 --- a/src/detection/netio/netio_windows.c +++ b/src/detection/netio/netio_windows.c @@ -51,7 +51,7 @@ const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { MIB_IF_ROW2 ifRow = { .InterfaceIndex = adapter->IfIndex }; if (GetIfEntry2(&ifRow) == NO_ERROR) { - FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result); + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); *counters = (FFNetIOResult) { .name = ffStrbufCreateMove(&name), .txBytes = ifRow.OutOctets, diff --git a/src/detection/opencl/opencl.c b/src/detection/opencl/opencl.c index 94ef321c5..e76a4b89b 100644 --- a/src/detection/opencl/opencl.c +++ b/src/detection/opencl/opencl.c @@ -87,7 +87,7 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) { continue; } - FFGPUResult* gpu = ffListAdd(&result->gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, result->gpus); ffStrbufInitS(&gpu->name, buffer); ffStrbufInit(&gpu->vendor); ffStrbufInit(&gpu->driver); @@ -198,12 +198,14 @@ static const char* detectOpenCL(FFOpenCLResult* result) { FFOpenCLResult* ffDetectOpenCL(void) { static FFOpenCLResult result; + static bool initialized; - if (result.gpus.elementSize == 0) { + if (!initialized) { + initialized = true; ffStrbufInit(&result.version); ffStrbufInit(&result.name); ffStrbufInit(&result.vendor); - ffListInit(&result.gpus, sizeof(FFGPUResult)); + ffListInit(&result.gpus); #ifdef FF_HAVE_OPENCL result.error = detectOpenCL(&result); diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 1fae83125..9f8be1454 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -130,7 +130,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) type |= FF_PHYSICALDISK_TYPE_UNUSED; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInit(&device->serial); ffStrbufInit(&device->revision); ffStrbufInitMove(&device->name, &deviceName); diff --git a/src/detection/physicaldisk/physicaldisk_bsd.c b/src/detection/physicaldisk/physicaldisk_bsd.c index daa1bd11a..e87d249c4 100644 --- a/src/detection/physicaldisk/physicaldisk_bsd.c +++ b/src/detection/physicaldisk/physicaldisk_bsd.c @@ -78,7 +78,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) continue; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name); ffStrbufInitMove(&device->serial, &identifier); ffStrbufTrimSpace(&device->serial); diff --git a/src/detection/physicaldisk/physicaldisk_haiku.c b/src/detection/physicaldisk/physicaldisk_haiku.c index 3219d9a0a..89799a2e1 100644 --- a/src/detection/physicaldisk/physicaldisk_haiku.c +++ b/src/detection/physicaldisk/physicaldisk_haiku.c @@ -62,7 +62,7 @@ static const char* searchRawDeviceFile(FFstrbuf* path, const char* diskType, FFl type |= FF_PHYSICALDISK_TYPE_UNUSED; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitS(&device->name, name); ffStrbufInitCopy(&device->devPath, path); ffStrbufInit(&device->serial); diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 267baf7d0..c06f8dc42 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -97,7 +97,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption ffStrbufSetS(&name, devName); } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitMove(&device->name, &name); ffStrbufInitF(&device->devPath, "/dev/%s", devName); ffStrbufInit(&device->serial); diff --git a/src/detection/physicaldisk/physicaldisk_nbsd.c b/src/detection/physicaldisk/physicaldisk_nbsd.c index 29b48017d..acef9ac80 100644 --- a/src/detection/physicaldisk/physicaldisk_nbsd.c +++ b/src/detection/physicaldisk/physicaldisk_nbsd.c @@ -17,35 +17,58 @@ static inline const char* retstsToStr(uint8_t retsts) { switch (retsts) { - case SCCMD_OK: return "OK"; - case SCCMD_TIMEOUT: return "TIMEOUT"; - case SCCMD_BUSY: return "BUSY"; - case SCCMD_SENSE: return "SENSE"; - case SCCMD_UNKNOWN: return "UNKNOWN"; - default: return "?"; + case SCCMD_OK: + return "OK"; + case SCCMD_TIMEOUT: + return "TIMEOUT"; + case SCCMD_BUSY: + return "BUSY"; + case SCCMD_SENSE: + return "SENSE"; + case SCCMD_UNKNOWN: + return "UNKNOWN"; + default: + return "?"; } } #ifndef NDEBUG static inline const char* senseKeyToStr(uint8_t key) { switch (key) { - case 0x0: return "No Sense"; - case 0x1: return "Recovered Error"; - case 0x2: return "Not Ready"; - case 0x3: return "Medium Error"; - case 0x4: return "Hardware Error"; - case 0x5: return "Illegal Request"; - case 0x6: return "Unit Attention"; - case 0x7: return "Data Protect"; - case 0x8: return "Blank Check"; - case 0x9: return "Vendor Specific"; - case 0xA: return "Copy Aborted"; - case 0xB: return "Aborted Command"; - case 0xC: return "Equal"; - case 0xD: return "Volume Overflow"; - case 0xE: return "Miscompare"; - case 0xF: return "Completed"; - default: return "Unknown"; + case 0x0: + return "No Sense"; + case 0x1: + return "Recovered Error"; + case 0x2: + return "Not Ready"; + case 0x3: + return "Medium Error"; + case 0x4: + return "Hardware Error"; + case 0x5: + return "Illegal Request"; + case 0x6: + return "Unit Attention"; + case 0x7: + return "Data Protect"; + case 0x8: + return "Blank Check"; + case 0x9: + return "Vendor Specific"; + case 0xA: + return "Copy Aborted"; + case 0xB: + return "Aborted Command"; + case 0xC: + return "Equal"; + case 0xD: + return "Volume Overflow"; + case 0xE: + return "Miscompare"; + case 0xF: + return "Completed"; + default: + return "Unknown"; } } @@ -67,8 +90,9 @@ static void logScsiSense(const char* diskName, const char* operation, const scsi FF_STRBUF_AUTO_DESTROY rawSense = ffStrbufCreate(); for (size_t i = 0; i < req->senselen_used; ++i) { - if (i) + if (i) { ffStrbufAppendC(&rawSense, ' '); + } ffStrbufAppendF(&rawSense, "%02X", req->sense[i]); } @@ -81,11 +105,10 @@ static void logScsiSense(const char* diskName, const char* operation, const scsi senseKeyToStr(senseKey), asc, ascq, - rawSense.length ? rawSense.chars : "empty" - ); + rawSense.length ? rawSense.chars : "empty"); } #else -#define logScsiSense(...) ((void)0) +# define logScsiSense(...) ((void) 0) #endif const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) { @@ -188,7 +211,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) type |= FF_PHYSICALDISK_TYPE_UNUSED; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitS(&device->name, devType ?: dl.d_packname); ffStrbufInitS(&device->devPath, devPath); ffStrbufInit(&device->serial); diff --git a/src/detection/physicaldisk/physicaldisk_obsd.c b/src/detection/physicaldisk/physicaldisk_obsd.c index e181f54d0..2958c631a 100644 --- a/src/detection/physicaldisk/physicaldisk_obsd.c +++ b/src/detection/physicaldisk/physicaldisk_obsd.c @@ -14,35 +14,58 @@ static inline const char* retstsToStr(uint8_t retsts) { switch (retsts) { - case SCCMD_OK: return "OK"; - case SCCMD_TIMEOUT: return "TIMEOUT"; - case SCCMD_BUSY: return "BUSY"; - case SCCMD_SENSE: return "SENSE"; - case SCCMD_UNKNOWN: return "UNKNOWN"; - default: return "?"; + case SCCMD_OK: + return "OK"; + case SCCMD_TIMEOUT: + return "TIMEOUT"; + case SCCMD_BUSY: + return "BUSY"; + case SCCMD_SENSE: + return "SENSE"; + case SCCMD_UNKNOWN: + return "UNKNOWN"; + default: + return "?"; } } #ifndef NDEBUG static inline const char* senseKeyToStr(uint8_t key) { switch (key) { - case 0x0: return "No Sense"; - case 0x1: return "Recovered Error"; - case 0x2: return "Not Ready"; - case 0x3: return "Medium Error"; - case 0x4: return "Hardware Error"; - case 0x5: return "Illegal Request"; - case 0x6: return "Unit Attention"; - case 0x7: return "Data Protect"; - case 0x8: return "Blank Check"; - case 0x9: return "Vendor Specific"; - case 0xA: return "Copy Aborted"; - case 0xB: return "Aborted Command"; - case 0xC: return "Equal"; - case 0xD: return "Volume Overflow"; - case 0xE: return "Miscompare"; - case 0xF: return "Completed"; - default: return "Unknown"; + case 0x0: + return "No Sense"; + case 0x1: + return "Recovered Error"; + case 0x2: + return "Not Ready"; + case 0x3: + return "Medium Error"; + case 0x4: + return "Hardware Error"; + case 0x5: + return "Illegal Request"; + case 0x6: + return "Unit Attention"; + case 0x7: + return "Data Protect"; + case 0x8: + return "Blank Check"; + case 0x9: + return "Vendor Specific"; + case 0xA: + return "Copy Aborted"; + case 0xB: + return "Aborted Command"; + case 0xC: + return "Equal"; + case 0xD: + return "Volume Overflow"; + case 0xE: + return "Miscompare"; + case 0xF: + return "Completed"; + default: + return "Unknown"; } } @@ -64,8 +87,9 @@ static void logScsiSense(const char* diskName, const char* operation, const scsi FF_STRBUF_AUTO_DESTROY rawSense = ffStrbufCreate(); for (size_t i = 0; i < req->senselen_used; ++i) { - if (i) + if (i) { ffStrbufAppendC(&rawSense, ' '); + } ffStrbufAppendF(&rawSense, "%02X", req->sense[i]); } @@ -78,11 +102,10 @@ static void logScsiSense(const char* diskName, const char* operation, const scsi senseKeyToStr(senseKey), asc, ascq, - rawSense.length ? rawSense.chars : "empty" - ); + rawSense.length ? rawSense.chars : "empty"); } #else -#define logScsiSense(...) ((void)0) +# define logScsiSense(...) ((void) 0) #endif const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) { @@ -155,7 +178,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) type |= FF_PHYSICALDISK_TYPE_UNUSED; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitS(&device->name, dl.d_packname); ffStrbufInitS(&device->devPath, devPath); ffStrbufInit(&device->serial); @@ -181,7 +204,6 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) }; if (ioctl(f, SCIOCCOMMAND, &req) == 0) { if (req.retsts == SCCMD_OK) { - ffStrbufClear(&device->name); ffStrbufAppendNS(&device->name, (uint32_t) ARRAY_SIZE(inquiry.vendor), inquiry.vendor); ffStrbufTrimRight(&device->name, '\0'); diff --git a/src/detection/physicaldisk/physicaldisk_sunos.c b/src/detection/physicaldisk/physicaldisk_sunos.c index 3cd3a51cb..93952c295 100644 --- a/src/detection/physicaldisk/physicaldisk_sunos.c +++ b/src/detection/physicaldisk/physicaldisk_sunos.c @@ -43,7 +43,7 @@ static int walkDevTree(di_node_t node, di_minor_t minor, struct FFWalkTreeBundle } } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInitMove(&device->name, &name); ffStrbufInitF(&device->devPath, "/devices%s", di_devfs_path(node)); ffStrbufInit(&device->serial); diff --git a/src/detection/physicaldisk/physicaldisk_windows.c b/src/detection/physicaldisk/physicaldisk_windows.c index c674c6b9d..5d4f7f743 100644 --- a/src/detection/physicaldisk/physicaldisk_windows.c +++ b/src/detection/physicaldisk/physicaldisk_windows.c @@ -204,7 +204,7 @@ static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* s return "Name prefix mismatch"; } - FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); + FFPhysicalDiskResult* device = FF_LIST_ADD(FFPhysicalDiskResult, *result); ffStrbufInit(&device->serial); ffStrbufInit(&device->revision); ffStrbufInitMove(&device->name, &name); diff --git a/src/detection/physicalmemory/physicalmemory.h b/src/detection/physicalmemory/physicalmemory.h index 6f683803a..d4c851171 100644 --- a/src/detection/physicalmemory/physicalmemory.h +++ b/src/detection/physicalmemory/physicalmemory.h @@ -17,6 +17,6 @@ typedef struct FFPhysicalMemoryResult { bool ecc; } FFPhysicalMemoryResult; -const char* ffDetectPhysicalMemory(FFlist* result); // list of FFPhysicalMemoryResult +const char* ffDetectPhysicalMemory(FFPhysicalMemoryOptions* options, FFlist* result); // list of FFPhysicalMemoryResult void FFPhysicalMemoryUpdateVendorString(FFPhysicalMemoryResult* device); diff --git a/src/detection/physicalmemory/physicalmemory_apple.m b/src/detection/physicalmemory/physicalmemory_apple.m index 36eab6d36..b9d2c267e 100644 --- a/src/detection/physicalmemory/physicalmemory_apple.m +++ b/src/detection/physicalmemory/physicalmemory_apple.m @@ -19,7 +19,7 @@ static void appendDevice( NSString* speed, bool ecc) { - FFPhysicalMemoryResult* device = ffListAdd(result); + FFPhysicalMemoryResult* device = FF_LIST_ADD(FFPhysicalMemoryResult, *result); ffStrbufInitS(&device->type, type.UTF8String); ffStrbufInit(&device->formFactor); ffStrbufInitS(&device->locator, locator.UTF8String); @@ -133,7 +133,7 @@ FF_A_UNUSED static const char* detectFromIokit(FFlist* result) if (!dramType || !dramSize || !dramVendor) return "IORegistryEntryCreateCFProperty() failed"; - FFPhysicalMemoryResult* device = ffListAdd(result); + FFPhysicalMemoryResult* device = FF_LIST_ADD(FFPhysicalMemoryResult, *result); ffStrbufInit(&device->type); ffStrbufInit(&device->formFactor); ffStrbufInit(&device->locator); @@ -152,7 +152,7 @@ FF_A_UNUSED static const char* detectFromIokit(FFlist* result) return NULL; } -const char* ffDetectPhysicalMemory(FFlist* result) +const char* ffDetectPhysicalMemory(FF_A_UNUSED FFPhysicalMemoryOptions* options, FFlist* result) { #if __aarch64__ if (detectFromIokit(result) == NULL) diff --git a/src/detection/physicalmemory/physicalmemory_linux.c b/src/detection/physicalmemory/physicalmemory_linux.c index 91573b5ab..02df7897a 100644 --- a/src/detection/physicalmemory/physicalmemory_linux.c +++ b/src/detection/physicalmemory/physicalmemory_linux.c @@ -64,7 +64,7 @@ typedef struct FFSmbiosMemoryDevice { static_assert(offsetof(FFSmbiosMemoryDevice, RcdRevisionNumber) == 0x62, "FFSmbiosMemoryDevice: Wrong struct alignment"); -const char* ffDetectPhysicalMemory(FFlist* result) { +const char* ffDetectPhysicalMemory(FFPhysicalMemoryOptions* options, FFlist* result) { const FFSmbiosHeaderTable* smbiosTable = ffGetSmbiosHeaderTable(); if (!smbiosTable) { return "Failed to get SMBIOS data"; @@ -84,7 +84,11 @@ const char* ffDetectPhysicalMemory(FFlist* result) { const char* strings = (const char*) data + data->Header.Length; bool installed = data->Size != 0; - FFPhysicalMemoryResult* device = ffListAdd(result); + if (!installed && !options->showEmptySlots) { + continue; + } + + FFPhysicalMemoryResult* device = FF_LIST_ADD(FFPhysicalMemoryResult, *result); ffStrbufInit(&device->type); ffStrbufInit(&device->formFactor); ffStrbufInit(&device->locator); diff --git a/src/detection/poweradapter/poweradapter_apple.c b/src/detection/poweradapter/poweradapter_apple.c index 557afcd69..9a5008abd 100644 --- a/src/detection/poweradapter/poweradapter_apple.c +++ b/src/detection/poweradapter/poweradapter_apple.c @@ -8,7 +8,7 @@ const char* ffDetectPowerAdapter(FFlist* results) { FF_CFTYPE_AUTO_RELEASE CFDictionaryRef details = IOPSCopyExternalPowerAdapterDetails(); if (details && CFDictionaryContainsKey(details, CFSTR(kIOPSPowerAdapterWattsKey))) { - FFPowerAdapterResult* adapter = ffListAdd(results); + FFPowerAdapterResult* adapter = FF_LIST_ADD(FFPowerAdapterResult, *results); ffStrbufInit(&adapter->name); ffStrbufInit(&adapter->description); diff --git a/src/detection/poweradapter/poweradapter_linux.c b/src/detection/poweradapter/poweradapter_linux.c index f0423cc5e..747e5b42a 100644 --- a/src/detection/poweradapter/poweradapter_linux.c +++ b/src/detection/poweradapter/poweradapter_linux.c @@ -46,7 +46,7 @@ static void parsePowerAdapter(int dfd, const char* id, FFlist* results) { return; } - FFPowerAdapterResult* result = ffListAdd(results); + FFPowerAdapterResult* result = FF_LIST_ADD(FFPowerAdapterResult, *results); ffStrbufInit(&result->name); ffStrbufInit(&result->description); result->watts = (int) (ffStrbufToDouble(&tmpBuffer, 0) / 1e6 + 0.5); diff --git a/src/detection/sound/sound.h b/src/detection/sound/sound.h index 16344f215..f6aa80d1b 100644 --- a/src/detection/sound/sound.h +++ b/src/detection/sound/sound.h @@ -1,6 +1,7 @@ #pragma once #include "fastfetch.h" +#include "modules/sound/option.h" #define FF_SOUND_VOLUME_UNKNOWN 255 @@ -13,4 +14,4 @@ typedef struct FFSoundDevice { bool active; } FFSoundDevice; -const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */); +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */); diff --git a/src/detection/sound/sound_apple.c b/src/detection/sound/sound_apple.c index f38067387..49d87b105 100644 --- a/src/detection/sound/sound_apple.c +++ b/src/detection/sound/sound_apple.c @@ -8,7 +8,7 @@ # define kAudioObjectPropertyElementMain kAudioObjectPropertyElementMaster #endif -const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */) { AudioDeviceID mainDeviceId; UInt32 dataSize = sizeof(mainDeviceId); if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &(AudioObjectPropertyAddress) { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeOutput, kAudioObjectPropertyElementMain }, 0, NULL, &dataSize, &mainDeviceId) != kAudioHardwareNoError) { @@ -16,9 +16,14 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { } AudioObjectID deviceIds[32] = {}; - dataSize = sizeof(deviceIds); - if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &(AudioObjectPropertyAddress) { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeOutput, kAudioObjectPropertyElementMain }, 0, NULL, &dataSize, &deviceIds) != kAudioHardwareNoError) { - return "AudioObjectGetPropertyData(kAudioHardwarePropertyDevices) failed"; + if (options->soundType & FF_SOUND_TYPE_MAIN) { + deviceIds[0] = mainDeviceId; + dataSize = sizeof(mainDeviceId); + } else { + dataSize = sizeof(deviceIds); + if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &(AudioObjectPropertyAddress) { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeOutput, kAudioObjectPropertyElementMain }, 0, NULL, &dataSize, &deviceIds) != kAudioHardwareNoError) { + return "AudioObjectGetPropertyData(kAudioHardwarePropertyDevices) failed"; + } } for (uint32_t index = 0, length = dataSize / sizeof(*deviceIds); index < length; ++index) { @@ -41,9 +46,17 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { } } - FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices); + uint32_t active = true; + dataSize = sizeof(active); + if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress) { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeOutput, kAudioObjectPropertyElementMain }, 0, NULL, &dataSize, &active) == kAudioHardwareNoError) { + if ((options->soundType & FF_SOUND_TYPE_ACTIVE) && !active) { + continue; + } + } + + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); device->main = deviceId == mainDeviceId; - device->active = false; + device->active = !!active; device->volume = FF_SOUND_VOLUME_UNKNOWN; ffStrbufInit(&device->identifier); ffStrbufInit(&device->name); @@ -71,12 +84,6 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { muted = false; // Device may not support volume control } - uint32_t active; - dataSize = sizeof(active); - if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress) { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeOutput, kAudioObjectPropertyElementMain }, 0, NULL, &dataSize, &active) == kAudioHardwareNoError) { - device->active = !!active; - } - if (muted) { device->volume = 0; } else { diff --git a/src/detection/sound/sound_bsd.c b/src/detection/sound/sound_bsd.c index 68f3b4f44..1b85db319 100644 --- a/src/detection/sound/sound_bsd.c +++ b/src/detection/sound/sound_bsd.c @@ -6,7 +6,7 @@ #include #include -const char* ffDetectSound(FFlist* devices) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) { #ifndef __NetBSD__ int defaultDev = ffSysctlGetInt("hw.snd.default_unit", -1); if (defaultDev == -1) { @@ -32,6 +32,11 @@ const char* ffDetectSound(FFlist* devices) { struct oss_sysinfo info = { .nummixers = 9 }; for (int idev = 0; idev <= info.nummixers; ++idev) { + bool isMain = idev == defaultDev; + if ((options->soundType & FF_SOUND_TYPE_MAIN) && !isMain) { + continue; + } + path[strlen("/dev/mixer")] = (char) ('0' + idev); FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { @@ -68,7 +73,7 @@ const char* ffDetectSound(FFlist* devices) { continue; } - FFSoundDevice* device = ffListAdd(devices); + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); ffStrbufInitS(&device->identifier, path); ffStrbufInitF(&device->name, "%s %s", ci.longname, ci.hw_info); ffStrbufTrimRightSpace(&device->name); @@ -79,7 +84,7 @@ const char* ffDetectSound(FFlist* devices) { #endif ((uint8_t) volume /*left*/ + (uint8_t) (volume >> 8) /*right*/) / 2; device->active = true; - device->main = defaultDev == idev; + device->main = isMain; } return NULL; diff --git a/src/detection/sound/sound_haiku.cpp b/src/detection/sound/sound_haiku.cpp index 8dc8ef8eb..717977a55 100644 --- a/src/detection/sound/sound_haiku.cpp +++ b/src/detection/sound/sound_haiku.cpp @@ -7,7 +7,7 @@ extern "C" { #include #include -const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { +const char* ffDetectSound(FF_A_UNUSED FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */) { BMediaRoster* roster = BMediaRoster::Roster(); media_node mediaNode; live_node_info liveInfo; @@ -17,7 +17,7 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { return NULL; } - FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices); + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); ffStrbufInit(&device->identifier); if (roster->GetDormantNodeFor(mediaNode, &dormantInfo) == B_OK) { ffStrbufAppendS(&device->identifier, dormantInfo.name); diff --git a/src/detection/sound/sound_linux.c b/src/detection/sound/sound_linux.c index 73a5128df..3e4833446 100644 --- a/src/detection/sound/sound_linux.c +++ b/src/detection/sound/sound_linux.c @@ -4,23 +4,40 @@ # include "common/library.h" # include -static void paSinkInfoCallback(pa_context* c, const pa_sink_info* i, int eol, void* userdata) { - FF_UNUSED(c); +struct DetectionInfoBundle { + FFstrbuf serverName; + FFstrbuf defaultDeviceId; + FFlist* result; + FFSoundOptions* options; +}; +static void paSinkInfoCallback(FF_A_UNUSED pa_context* c, const pa_sink_info* i, int eol, void* userdata) { if (eol > 0 || !i) { return; } - FFSoundDevice* device = ffListAdd(userdata); + struct DetectionInfoBundle* bundle = userdata; + + bool isMain = ffStrbufEqualS(&bundle->defaultDeviceId, i->name); + if ((bundle->options->soundType & FF_SOUND_TYPE_MAIN) && !isMain) { + return; + } + + bool isActive = i->active_port && i->active_port->available != PA_PORT_AVAILABLE_NO; + if ((bundle->options->soundType & FF_SOUND_TYPE_ACTIVE) && !isActive) { + return; + } + + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *bundle->result); ffStrbufInitS(&device->identifier, i->name); - ffStrbufInitStatic(&device->platformApi, "PulseAudio"); + ffStrbufInitCopy(&device->platformApi, &bundle->serverName); ffStrbufTrimRightSpace(&device->identifier); ffStrbufInitS(&device->name, i->description); ffStrbufTrimRightSpace(&device->name); ffStrbufTrimLeft(&device->name, ' '); device->volume = i->mute ? 0 : (uint8_t) ((i->volume.values[0] * 100 + PA_VOLUME_NORM / 2 /*round*/) / PA_VOLUME_NORM); - device->active = i->active_port && i->active_port->available != PA_PORT_AVAILABLE_NO; - device->main = false; + device->active = isActive; + device->main = isMain; } static void paServerInfoCallback(FF_A_UNUSED pa_context* c, const pa_server_info* i, void* userdata) { @@ -28,22 +45,20 @@ static void paServerInfoCallback(FF_A_UNUSED pa_context* c, const pa_server_info return; } - FF_STRBUF_AUTO_DESTROY api = ffStrbufCreate(); + struct DetectionInfoBundle* bundle = userdata; + const char* realServer = strstr(i->server_name, "(on "); if (realServer) { - ffStrbufSetS(&api, realServer + strlen("(on ")); - ffStrbufTrimRight(&api, ')'); + ffStrbufSetS(&bundle->serverName, realServer + strlen("(on ")); + ffStrbufTrimRight(&bundle->serverName, ')'); } else { - ffStrbufSetF(&api, "%s %s", i->server_name, i->server_version); + ffStrbufSetF(&bundle->serverName, "%s %s", i->server_name, i->server_version); } - FF_LIST_FOR_EACH (FFSoundDevice, device, *(FFlist*) userdata) { - device->main = ffStrbufEqualS(&device->identifier, i->default_sink_name); - ffStrbufSet(&device->platformApi, &api); - } + ffStrbufSetS(&bundle->defaultDeviceId, i->default_sink_name); } -static const char* detectSound(FFlist* devices) { +static const char* detectSound(FFSoundOptions* options, FFlist* devices) { FF_LIBRARY_LOAD_MESSAGE(pulse, "libpulse" FF_LIBRARY_EXTENSION, 0) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_mainloop_new) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_mainloop_get_api) @@ -92,7 +107,23 @@ static const char* detectSound(FFlist* devices) { ffpa_mainloop_iterate(mainloop, 1, NULL); } - pa_operation* operation = ffpa_context_get_sink_info_list(context, paSinkInfoCallback, devices); + struct DetectionInfoBundle bundle = { + .serverName = ffStrbufCreate(), + .defaultDeviceId = ffStrbufCreate(), + .result = devices, + .options = options, + }; + + { + pa_operation* operation = ffpa_context_get_server_info(context, paServerInfoCallback, &bundle); + while (ffpa_operation_get_state(operation) == PA_OPERATION_RUNNING) { + ffpa_mainloop_iterate(mainloop, 1, NULL); + } + + ffpa_operation_unref(operation); + } + + pa_operation* operation = ffpa_context_get_sink_info_list(context, paSinkInfoCallback, &bundle); if (!operation) { ffpa_context_unref(context); ffpa_mainloop_free(mainloop); @@ -105,14 +136,6 @@ static const char* detectSound(FFlist* devices) { ffpa_operation_unref(operation); - operation = ffpa_context_get_server_info(context, paServerInfoCallback, devices); - if (operation) { - while (ffpa_operation_get_state(operation) == PA_OPERATION_RUNNING) { - ffpa_mainloop_iterate(mainloop, 1, NULL); - } - - ffpa_operation_unref(operation); - } ffpa_context_unref(context); ffpa_mainloop_free(mainloop); @@ -121,11 +144,11 @@ static const char* detectSound(FFlist* devices) { #endif // FF_HAVE_PULSE -const char* ffDetectSound(FFlist* devices) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) { #ifdef FF_HAVE_PULSE - return detectSound(devices); + return detectSound(options, devices); #else - FF_UNUSED(devices); + FF_UNUSED(options, devices); return "Fastfetch was built without libpulse support"; #endif } diff --git a/src/detection/sound/sound_nbsd.c b/src/detection/sound/sound_nbsd.c index a65aa82f7..592dce9f5 100644 --- a/src/detection/sound/sound_nbsd.c +++ b/src/detection/sound/sound_nbsd.c @@ -7,7 +7,7 @@ #include #include -const char* ffDetectSound(FFlist* devices) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) { int defaultDev; { char audiop[12]; @@ -24,6 +24,11 @@ const char* ffDetectSound(FFlist* devices) { char path[] = "/dev/audio0"; for (int idev = 0; idev < 9; ++idev) { + bool isMain = idev == defaultDev; + if ((options->soundType & FF_SOUND_TYPE_MAIN) && !isMain) { + continue; + } + path[strlen("/dev/audio")] = (char) ('0' + idev); FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { @@ -40,14 +45,14 @@ const char* ffDetectSound(FFlist* devices) { continue; } - FFSoundDevice* device = ffListAdd(devices); + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); ffStrbufInitS(&device->identifier, path); ffStrbufInitS(&device->name, ad.name); ffStrbufTrimRightSpace(&device->name); ffStrbufInitF(&device->platformApi, "%s", "SunAudio"); device->volume = (uint8_t) ((ai.play.gain * 100 + AUDIO_MAX_GAIN / 2) / AUDIO_MAX_GAIN); device->active = true; - device->main = defaultDev == idev; + device->main = isMain; } return NULL; diff --git a/src/detection/sound/sound_obsd.c b/src/detection/sound/sound_obsd.c index cedfe5f30..b66985ba6 100644 --- a/src/detection/sound/sound_obsd.c +++ b/src/detection/sound/sound_obsd.c @@ -56,7 +56,7 @@ static void enumerate_props(FFSoundDeviceBundle* bundle, struct sioctl_desc* des } } -const char* ffDetectSound(FFlist* devices) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) { FF_A_CLEANUP(close_hdl) struct sioctl_hdl* hdl = sioctl_open(SIO_DEVANY, SIOCTL_READ, 0); if (!hdl) { return "sio_open() failed"; @@ -71,7 +71,7 @@ const char* ffDetectSound(FFlist* devices) { return "Unexpected sioctl_ondesc() result"; } - FFSoundDevice* device = ffListAdd(devices); + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); ffStrbufInitS(&device->name, bundle.name); ffStrbufInitS(&device->identifier, SIO_DEVANY); ffStrbufInitStatic(&device->platformApi, "sndio"); diff --git a/src/detection/sound/sound_sunos.c b/src/detection/sound/sound_sunos.c index 6ce6c358c..85362e50d 100644 --- a/src/detection/sound/sound_sunos.c +++ b/src/detection/sound/sound_sunos.c @@ -11,7 +11,7 @@ # include "audio_oss_sunos.h" #endif -const char* ffDetectSound(FFlist* devices) { +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) { int defaultDev; { char mixerp[12]; @@ -33,6 +33,11 @@ const char* ffDetectSound(FFlist* devices) { // The implementation is very different from *BSD's. They call it OSS4 for (int idev = 0; idev < info.nummixers; ++idev) { + bool isMain = idev == defaultDev; + if ((options->soundType & FF_SOUND_TYPE_MAIN) && !isMain) { + continue; + } + path[strlen("/dev/mixer")] = (char) ('0' + idev); FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { @@ -53,6 +58,10 @@ const char* ffDetectSound(FFlist* devices) { continue; } + if (options->soundType == FF_SOUND_TYPE_ACTIVE && !mi.enabled) { + continue; + } + int volume = -1; for (int iext = 0; iext < mi.nrext; ++iext) { struct oss_mixext me = { .dev = mi.dev, .ctrl = iext }; @@ -73,7 +82,7 @@ const char* ffDetectSound(FFlist* devices) { continue; } - FFSoundDevice* device = ffListAdd(devices); + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); ffStrbufInitS(&device->identifier, path); char buf[16]; int bufLen = snprintf(buf, ARRAY_SIZE(buf), "\n%d: ", mi.dev); @@ -93,7 +102,7 @@ const char* ffDetectSound(FFlist* devices) { ffStrbufInitF(&device->platformApi, "%s %s", info.product, info.version); device->volume = (uint8_t) volume; device->active = !!mi.enabled; - device->main = defaultDev == idev; + device->main = isMain; } return NULL; diff --git a/src/detection/sound/sound_windows.cpp b/src/detection/sound/sound_windows.cpp index 0ae1ed0eb..116581daa 100644 --- a/src/detection/sound/sound_windows.cpp +++ b/src/detection/sound/sound_windows.cpp @@ -19,7 +19,56 @@ static void ffCoTaskMemFreeWrapper(void* pptr) { } #define FF_COTASK_AUTO_FREE FF_A_CLEANUP(ffCoTaskMemFreeWrapper) -const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { +static const char* detectSoundDevice(FFlist* devices /* List of FFSoundDevice */, IMMDevice* immDevice, LPWSTR mainDeviceId) { + LPWSTR FF_COTASK_AUTO_FREE immDeviceId = NULL; + if (FAILED(immDevice->GetId(&immDeviceId))) { + return "immDevice->GetId() failed"; + } + + IPropertyStore* FF_AUTO_RELEASE_COM_OBJECT immPropStore; + if (FAILED(immDevice->OpenPropertyStore(STGM_READ, &immPropStore))) { + return "immDevice->OpenPropertyStore() failed"; + } + + DWORD immState; + if (FAILED(immDevice->GetState(&immState))) { + return "immDevice->GetState() failed"; + } + + FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices); + device->main = !mainDeviceId || wcscmp(immDeviceId, mainDeviceId) == 0; + device->active = !!(immState & DEVICE_STATE_ACTIVE); + device->volume = FF_SOUND_VOLUME_UNKNOWN; + ffStrbufInitWS(&device->identifier, immDeviceId); + ffStrbufInit(&device->name); + ffStrbufInitStatic(&device->platformApi, "Core Audio APIs"); + + { + FFPropVariant friendlyName; + if (SUCCEEDED(immPropStore->GetValue(PKEY_Device_FriendlyName, &friendlyName))) { + ffStrbufSetWSV(&device->name, friendlyName.get()); + } else if (SUCCEEDED(immPropStore->GetValue(PKEY_Device_DeviceDesc, &friendlyName))) { + ffStrbufSetWSV(&device->name, friendlyName.get()); + } else { + ffStrbufSetStatic(&device->name, "Unknown Device"); + } + } + + IAudioEndpointVolume* FF_AUTO_RELEASE_COM_OBJECT immEndpointVolume; + if (SUCCEEDED(immDevice->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**) &immEndpointVolume))) { + BOOL muted; + if (FAILED(immEndpointVolume->GetMute(&muted)) || !muted) { + FLOAT volume; + if (SUCCEEDED(immEndpointVolume->GetMasterVolumeLevelScalar(&volume))) { + device->volume = (uint8_t) (volume * 100 + 0.5); + } + } + } + + return NULL; +} + +const char* ffDetectSound(FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */) { const char* error = ffInitCom(); if (error) { return error; @@ -40,6 +89,10 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { return "GetDefaultAudioEndpoint() failed"; } + if (options->soundType & FF_SOUND_TYPE_MAIN) { + return detectSoundDevice(devices, pDefaultDevice, NULL); + } + if (FAILED(pDefaultDevice->GetId(&mainDeviceId))) { return "pDefaultDevice->GetId() failed"; } @@ -47,7 +100,7 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { IMMDeviceCollection* FF_AUTO_RELEASE_COM_OBJECT pDevices = NULL; - if (FAILED(pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE | DEVICE_STATE_DISABLED, &pDevices))) { + if (FAILED(pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE | (options->soundType & FF_SOUND_TYPE_ACTIVE ? 0 : DEVICE_STATE_DISABLED), &pDevices))) { return "EnumAudioEndpoints() failed"; } @@ -62,46 +115,7 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { continue; } - LPWSTR FF_COTASK_AUTO_FREE immDeviceId = NULL; - if (FAILED(immDevice->GetId(&immDeviceId))) { - continue; - } - - IPropertyStore* FF_AUTO_RELEASE_COM_OBJECT immPropStore; - if (FAILED(immDevice->OpenPropertyStore(STGM_READ, &immPropStore))) { - continue; - } - - DWORD immState; - if (FAILED(immDevice->GetState(&immState))) { - continue; - } - - FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices); - device->main = wcscmp(mainDeviceId, immDeviceId) == 0; - device->active = !!(immState & DEVICE_STATE_ACTIVE); - device->volume = FF_SOUND_VOLUME_UNKNOWN; - ffStrbufInitWS(&device->identifier, immDeviceId); - ffStrbufInit(&device->name); - ffStrbufInitStatic(&device->platformApi, "Core Audio APIs"); - - { - FFPropVariant friendlyName; - if (SUCCEEDED(immPropStore->GetValue(PKEY_Device_FriendlyName, &friendlyName))) { - ffStrbufSetWSV(&device->name, friendlyName.get()); - } - } - - IAudioEndpointVolume* FF_AUTO_RELEASE_COM_OBJECT immEndpointVolume; - if (SUCCEEDED(immDevice->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**) &immEndpointVolume))) { - BOOL muted; - if (FAILED(immEndpointVolume->GetMute(&muted)) || !muted) { - FLOAT volume; - if (SUCCEEDED(immEndpointVolume->GetMasterVolumeLevelScalar(&volume))) { - device->volume = (uint8_t) (volume * 100 + 0.5); - } - } - } + detectSoundDevice(devices, immDevice, mainDeviceId); } return NULL; diff --git a/src/detection/swap/swap_apple.c b/src/detection/swap/swap_apple.c index d53148e7b..40dda4900 100644 --- a/src/detection/swap/swap_apple.c +++ b/src/detection/swap/swap_apple.c @@ -20,7 +20,7 @@ const char* ffDetectSwap(FFlist* result) { } } - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitStatic(&swap->name, xsw.xsu_encrypted ? "Encrypted" : "Normal"); swap->bytesTotal = xsw.xsu_total; swap->bytesUsed = xsw.xsu_used; diff --git a/src/detection/swap/swap_bsd.c b/src/detection/swap/swap_bsd.c index b3e8d65b6..a54cd5252 100644 --- a/src/detection/swap/swap_bsd.c +++ b/src/detection/swap/swap_bsd.c @@ -10,7 +10,7 @@ static void addSwapEntry(FFlist* result, struct xswdev* xsw, uint32_t pageSize) return; } - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); if (xsw->xsw_dev == NODEV) { ffStrbufInitStatic(&swap->name, "[NFS]"); } else { diff --git a/src/detection/swap/swap_haiku.c b/src/detection/swap/swap_haiku.c index 3fe7e7ff2..8a3b08cbe 100644 --- a/src/detection/swap/swap_haiku.c +++ b/src/detection/swap/swap_haiku.c @@ -10,7 +10,7 @@ const char* ffDetectSwap(FFlist* result) { } uint32_t pageSize = instance.state.platform.sysinfo.pageSize; - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitStatic(&swap->name, "System"); void* kvms = load_driver_settings("virtual_memory"); // /boot/home/config/settings/kernel/drivers/virtual_memory if (kvms) { diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index b0fbe32e5..b31f268bf 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -26,7 +26,7 @@ static const char* detectByProcMeminfo(FFlist* result) { swapFree = strtoul(token + strlen("SwapFree:"), NULL, 10); } - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitStatic(&swap->name, "Total"); swap->bytesTotal = swapTotal * 1024lu; swap->bytesUsed = (swapTotal - swapFree) * 1024lu; @@ -54,7 +54,7 @@ static const char* detectByProcSwaps(FFlist* result) { } uint32_t nameLen = (uint32_t) strnlen(name, sizeof(name)); - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitA(&swap->name, nameLen); for (size_t i = 0; i < nameLen; ++i) { if (name[i] == '\\') { diff --git a/src/detection/swap/swap_obsd.c b/src/detection/swap/swap_obsd.c index 04afe8053..1296aeb27 100644 --- a/src/detection/swap/swap_obsd.c +++ b/src/detection/swap/swap_obsd.c @@ -24,7 +24,7 @@ const char* ffDetectSwap(FFlist* result) { for (int i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitS(&swap->name, swdev[i].se_path); swap->bytesUsed = (uint64_t) swdev[i].se_inuse * DEV_BSIZE; swap->bytesTotal = (uint64_t) swdev[i].se_nblks * DEV_BSIZE; diff --git a/src/detection/swap/swap_sunos.c b/src/detection/swap/swap_sunos.c index 323b4decc..fa3260d50 100644 --- a/src/detection/swap/swap_sunos.c +++ b/src/detection/swap/swap_sunos.c @@ -22,7 +22,7 @@ const char* ffDetectSwap(FFlist* result) { uint32_t pageSize = instance.state.platform.sysinfo.pageSize; for (int i = 0; i < size; ++i) { - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitS(&swap->name, table->swt_ent[i].ste_path); swap->bytesTotal = (uint64_t) table->swt_ent[i].ste_pages * pageSize; swap->bytesUsed = swap->bytesTotal - (uint64_t) table->swt_ent[i].ste_free * pageSize; diff --git a/src/detection/swap/swap_windows.c b/src/detection/swap/swap_windows.c index 558feff5c..e96298bdc 100644 --- a/src/detection/swap/swap_windows.c +++ b/src/detection/swap/swap_windows.c @@ -20,7 +20,7 @@ const char* ffDetectSwap(FFlist* result) { uint32_t pageSize = instance.state.platform.sysinfo.pageSize; for (SYSTEM_PAGEFILE_INFORMATION* current = pstart;; current = (SYSTEM_PAGEFILE_INFORMATION*) ((uint8_t*) current + current->NextEntryOffset)) { - FFSwapResult* swap = ffListAdd(result); + FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result); ffStrbufInitNWS(&swap->name, current->FileName.Length / sizeof(wchar_t), current->FileName.Buffer); if (ffStrbufStartsWithS(&swap->name, "\\??\\")) { ffStrbufSubstrAfter(&swap->name, strlen("\\??\\") - 1); diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index a23b8938b..cfb786522 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -259,7 +259,7 @@ static void detectWarp(FFTerminalFontResult* terminalFont) { ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); - FFstrbuf* fontWeight = (FFstrbuf*) ffListAdd(&terminalFont->font.styles); + FFstrbuf* fontWeight = FF_LIST_ADD(FFstrbuf, terminalFont->font.styles); ffStrbufInit(fontWeight); if (ffRegReadStrbuf(key, L"FontWeight", fontWeight, NULL)) { ffStrbufTrim(fontWeight, '"'); diff --git a/src/detection/users/users_obsd.c b/src/detection/users/users_obsd.c index 4c957a57f..97a22f039 100644 --- a/src/detection/users/users_obsd.c +++ b/src/detection/users/users_obsd.c @@ -27,7 +27,7 @@ next: } } - FFUserResult* user = (FFUserResult*) ffListAdd(users); + FFUserResult* user = FF_LIST_ADD(FFUserResult, *users); ffStrbufInitS(&user->name, n.ut_name); ffStrbufInitS(&user->hostName, n.ut_host); ffStrbufInitS(&user->sessionName, n.ut_line); diff --git a/src/detection/users/users_windows.c b/src/detection/users/users_windows.c index ca5c52611..4cde44e31 100644 --- a/src/detection/users/users_windows.c +++ b/src/detection/users/users_windows.c @@ -27,7 +27,7 @@ const char* ffDetectUsers(FFUsersOptions* options, FFlist* users) { continue; } - FFUserResult* user = (FFUserResult*) ffListAdd(users); + FFUserResult* user = FF_LIST_ADD(FFUserResult, *users); ffStrbufInitMove(&user->name, &userName); ffStrbufInitWS(&user->hostName, session->pHostName); ffStrbufInitWS(&user->sessionName, session->pSessionName); diff --git a/src/detection/vulkan/vulkan.c b/src/detection/vulkan/vulkan.c index 8116bc6c9..6ec0a4860 100644 --- a/src/detection/vulkan/vulkan.c +++ b/src/detection/vulkan/vulkan.c @@ -243,7 +243,7 @@ static const char* detectVulkan(FFVulkanResult* result) { } } - FFGPUResult* gpu = ffListAdd(&result->gpus); + FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, result->gpus); ffStrbufInitF(&gpu->platformApi, "Vulkan %u.%u.%u", deviceAPIVersion.major, deviceAPIVersion.minor, deviceAPIVersion.patch); gpu->deviceId = physicalDeviceProperties.properties.deviceID; @@ -304,14 +304,16 @@ static const char* detectVulkan(FFVulkanResult* result) { FFVulkanResult* ffDetectVulkan(void) { static FFVulkanResult result; + static bool initialized; - if (result.gpus.elementSize == 0) { + if (!initialized) { FF_DEBUG("Initializing Vulkan detection cache"); + initialized = true; ffStrbufInit(&result.driver); ffStrbufInit(&result.apiVersion); ffStrbufInit(&result.conformanceVersion); ffStrbufInit(&result.instanceVersion); - ffListInit(&result.gpus, sizeof(FFGPUResult)); + ffListInit(&result.gpus); #ifdef FF_HAVE_VULKAN result.error = detectVulkan(&result); diff --git a/src/detection/wifi/wifi_android.c b/src/detection/wifi/wifi_android.c index cdcc1eb09..c4537da07 100644 --- a/src/detection/wifi/wifi_android.c +++ b/src/detection/wifi/wifi_android.c @@ -30,7 +30,7 @@ const char* ffDetectWifi(FFlist* result) { return "Wifi info result is not a JSON object"; } - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInit(&item->inf.description); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_apple.m b/src/detection/wifi/wifi_apple.m index 05318e7c2..21411335e 100644 --- a/src/detection/wifi/wifi_apple.m +++ b/src/detection/wifi/wifi_apple.m @@ -21,7 +21,7 @@ const char* ffDetectWifi(FFlist* result) for (CWInterface* inf in interfaces) { - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInit(&item->inf.description); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_bsd.c b/src/detection/wifi/wifi_bsd.c index 7c3f7d79d..865a0a80f 100644 --- a/src/detection/wifi/wifi_bsd.c +++ b/src/detection/wifi/wifi_bsd.c @@ -25,7 +25,7 @@ const char* ffDetectWifi(FFlist* result) { continue; } - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInitS(&item->inf.description, i->if_name); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_linux.c b/src/detection/wifi/wifi_linux.c index 4181d0cdb..eeb6b5b69 100644 --- a/src/detection/wifi/wifi_linux.c +++ b/src/detection/wifi/wifi_linux.c @@ -475,7 +475,7 @@ const char* ffDetectWifi(FF_A_UNUSED FFlist* result) { } FF_DEBUG("Found wifi interface: %s", i->if_name); - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInitS(&item->inf.description, i->if_name); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_nbsd.c b/src/detection/wifi/wifi_nbsd.c index a70f2a857..b7dbe131e 100644 --- a/src/detection/wifi/wifi_nbsd.c +++ b/src/detection/wifi/wifi_nbsd.c @@ -35,7 +35,7 @@ const char* ffDetectWifi(FFlist* result) { continue; } - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInitS(&item->inf.description, i->if_name); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_obsd.c b/src/detection/wifi/wifi_obsd.c index 06b6603ac..2fa4cd75c 100644 --- a/src/detection/wifi/wifi_obsd.c +++ b/src/detection/wifi/wifi_obsd.c @@ -25,7 +25,7 @@ const char* ffDetectWifi(FFlist* result) { continue; } - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInitS(&item->inf.description, i->if_name); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/wifi/wifi_windows.c b/src/detection/wifi/wifi_windows.c index 8969f09ef..125c314cc 100644 --- a/src/detection/wifi/wifi_windows.c +++ b/src/detection/wifi/wifi_windows.c @@ -67,7 +67,7 @@ const char* ffDetectWifi(FFlist* result) { for (uint32_t index = 0; index < ifList->dwNumberOfItems; ++index) { WLAN_INTERFACE_INFO* ifInfo = (WLAN_INTERFACE_INFO*) &ifList->InterfaceInfo[index]; - FFWifiResult* item = (FFWifiResult*) ffListAdd(result); + FFWifiResult* item = FF_LIST_ADD(FFWifiResult, *result); ffStrbufInitWS(&item->inf.description, ifInfo->strInterfaceDescription); ffStrbufInit(&item->inf.status); ffStrbufInit(&item->conn.status); diff --git a/src/detection/zpool/zpool.c b/src/detection/zpool/zpool.c index 0780a0941..ece62a4dd 100644 --- a/src/detection/zpool/zpool.c +++ b/src/detection/zpool/zpool.c @@ -50,7 +50,7 @@ static inline void cleanLibzfs(FFZfsData* data) { static int enumZpoolCallback(zpool_handle_t* zpool, void* param) { FFZfsData* data = (FFZfsData*) param; zprop_source_t source; - FFZpoolResult* item = ffListAdd(data->result); + FFZpoolResult* item = FF_LIST_ADD(FFZpoolResult, *data->result); char buf[1024]; if (data->ffzpool_get_prop(zpool, data->props.name, buf, ARRAY_SIZE(buf), &source, false) == 0) { ffStrbufInitS(&item->name, buf); diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index 6865d256a..0039ae307 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -166,7 +166,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin } bool ffPrintBattery(FFBatteryOptions* options) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBattery(options, &results); @@ -233,7 +233,7 @@ void ffGenerateBatteryJsonConfig(FFBatteryOptions* options, yyjson_mut_doc* doc, } bool ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBattery(options, &results); if (error) { diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index 32138d4af..2894c5464 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -56,7 +56,7 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de } bool ffPrintBluetooth(FFBluetoothOptions* options) { - FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof(FFBluetoothResult)); + FF_LIST_AUTO_DESTROY devices = ffListCreate(); const char* error = ffDetectBluetooth(options, &devices); if (error) { @@ -64,23 +64,14 @@ bool ffPrintBluetooth(FFBluetoothOptions* options) { return false; } - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*)); - - FF_LIST_FOR_EACH (FFBluetoothResult, device, devices) { - if (!device->connected && !options->showDisconnected) { - continue; - } - - *(FFBluetoothResult**) ffListAdd(&filtered) = device; - } - - if (filtered.length == 0) { + if (devices.length == 0) { ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found"); } - for (uint32_t i = 0; i < filtered.length; i++) { - uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); - printDevice(options, *FF_LIST_GET(FFBluetoothResult*, filtered, i), index); + uint8_t i = 1; + FF_LIST_FOR_EACH (FFBluetoothResult, device, devices) { + printDevice(options, device, devices.length == 0 ? 0 : i); + ++i; } FF_LIST_FOR_EACH (FFBluetoothResult, device, devices) { @@ -121,7 +112,7 @@ void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc* } bool ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBluetooth(options, &results); if (error) { diff --git a/src/modules/bluetoothradio/bluetoothradio.c b/src/modules/bluetoothradio/bluetoothradio.c index c5668e892..fcbd799f5 100644 --- a/src/modules/bluetoothradio/bluetoothradio.c +++ b/src/modules/bluetoothradio/bluetoothradio.c @@ -89,7 +89,7 @@ static void printDevice(FFBluetoothRadioOptions* options, const FFBluetoothRadio } bool ffPrintBluetoothRadio(FFBluetoothRadioOptions* options) { - FF_LIST_AUTO_DESTROY radios = ffListCreate(sizeof(FFBluetoothRadioResult)); + FF_LIST_AUTO_DESTROY radios = ffListCreate(); const char* error = ffDetectBluetoothRadio(&radios); if (error) { @@ -140,7 +140,7 @@ void ffGenerateBluetoothRadioJsonConfig(FFBluetoothRadioOptions* options, yyjson } bool ffGenerateBluetoothRadioJsonResult(FF_A_UNUSED FFBluetoothRadioOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothRadioResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBluetoothRadio(&results); if (error) { diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index 3ed12607f..699779365 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -6,7 +6,7 @@ #include "modules/brightness/brightness.h" bool ffPrintBrightness(FFBrightnessOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectBrightness(options, &result); @@ -141,7 +141,7 @@ void ffGenerateBrightnessJsonConfig(FFBrightnessOptions* options, yyjson_mut_doc } bool ffGenerateBrightnessJsonResult(FF_A_UNUSED FFBrightnessOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectBrightness(options, &result); diff --git a/src/modules/btrfs/btrfs.c b/src/modules/btrfs/btrfs.c index 8498b6dff..30cadd346 100644 --- a/src/modules/btrfs/btrfs.c +++ b/src/modules/btrfs/btrfs.c @@ -95,7 +95,7 @@ static void printBtrfs(FFBtrfsOptions* options, FFBtrfsResult* result, uint8_t i } bool ffPrintBtrfs(FFBtrfsOptions* options) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBtrfsResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBtrfs(&results); @@ -147,7 +147,7 @@ void ffGenerateBtrfsJsonConfig(FFBtrfsOptions* options, yyjson_mut_doc* doc, yyj } bool ffGenerateBtrfsJsonResult(FF_A_UNUSED FFBtrfsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBtrfsResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectBtrfs(&results); if (error) { diff --git a/src/modules/camera/camera.c b/src/modules/camera/camera.c index 0df33685e..fbabfa329 100644 --- a/src/modules/camera/camera.c +++ b/src/modules/camera/camera.c @@ -33,7 +33,7 @@ static void printDevice(FFCameraOptions* options, const FFCameraResult* device, } bool ffPrintCamera(FFCameraOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectCamera(&result); if (error) { @@ -77,7 +77,7 @@ void ffGenerateCameraJsonConfig(FFCameraOptions* options, yyjson_mut_doc* doc, y } bool ffGenerateCameraJsonResult(FF_A_UNUSED FFCameraOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectCamera(&result); if (error) { diff --git a/src/modules/cpucache/cpucache.c b/src/modules/cpucache/cpucache.c index 6f9f9bea1..0224a21e7 100644 --- a/src/modules/cpucache/cpucache.c +++ b/src/modules/cpucache/cpucache.c @@ -104,10 +104,10 @@ bool ffPrintCPUCache(FFCPUCacheOptions* options) { bool success = false; FFCPUCacheResult result = { .caches = { - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), + ffListCreate(), + ffListCreate(), + ffListCreate(), + ffListCreate(), }, }; @@ -161,10 +161,10 @@ bool ffGenerateCPUCacheJsonResult(FF_A_UNUSED FFCPUCacheOptions* options, yyjson bool success = false; FFCPUCacheResult result = { .caches = { - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), - ffListCreate(sizeof(FFCPUCache)), + ffListCreate(), + ffListCreate(), + ffListCreate(), + ffListCreate(), }, }; diff --git a/src/modules/cpuusage/cpuusage.c b/src/modules/cpuusage/cpuusage.c index 90dd8c5fa..81960473b 100644 --- a/src/modules/cpuusage/cpuusage.c +++ b/src/modules/cpuusage/cpuusage.c @@ -8,7 +8,7 @@ #define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage" bool ffPrintCPUUsage(FFCPUUsageOptions* options) { - FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); + FF_LIST_AUTO_DESTROY percentages = ffListCreate(); const char* error = ffGetCpuUsageResult(options, &percentages); if (error) { @@ -154,7 +154,7 @@ void ffGenerateCPUUsageJsonConfig(FFCPUUsageOptions* options, yyjson_mut_doc* do } bool ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); + FF_LIST_AUTO_DESTROY percentages = ffListCreate(); const char* error = ffGetCpuUsageResult(options, &percentages); if (error) { diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index cf3dcefcc..b472feeea 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -183,7 +183,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index } bool ffPrintDisk(FFDiskOptions* options) { - FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof(FFDisk)); + FF_LIST_AUTO_DESTROY disks = ffListCreate(); const char* error = ffDetectDisks(options, &disks); if (error) { @@ -358,7 +358,7 @@ void ffGenerateDiskJsonConfig(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso } bool ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof(FFDisk)); + FF_LIST_AUTO_DESTROY disks = ffListCreate(); const char* error = ffDetectDisks(options, &disks); if (error) { diff --git a/src/modules/diskio/diskio.c b/src/modules/diskio/diskio.c index 9c3a25cc2..f008db01a 100644 --- a/src/modules/diskio/diskio.c +++ b/src/modules/diskio/diskio.c @@ -26,7 +26,7 @@ static void formatKey(const FFDiskIOOptions* options, FFDiskIOResult* dev, uint3 } bool ffPrintDiskIO(FFDiskIOOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFDiskIOResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectDiskIO(&result, options); if (error) { @@ -34,7 +34,7 @@ bool ffPrintDiskIO(FFDiskIOOptions* options) { return false; } - ffListSort(&result, (const void*) sortDevices); + ffListSort(&result, sizeof(FFDiskIOResult), (const void*) sortDevices); uint32_t index = 0; FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -131,7 +131,7 @@ void ffGenerateDiskIOJsonConfig(FFDiskIOOptions* options, yyjson_mut_doc* doc, y } bool ffGenerateDiskIOJsonResult(FFDiskIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFDiskIOResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectDiskIO(&result, options); if (error) { diff --git a/src/modules/display/display.c b/src/modules/display/display.c index 582555874..07b5356ba 100644 --- a/src/modules/display/display.c +++ b/src/modules/display/display.c @@ -24,7 +24,7 @@ bool ffPrintDisplay(FFDisplayOptions* options) { } if (options->order != FF_DISPLAY_ORDER_NONE) { - ffListSort((FFlist*) &dsResult->displays, (void*) (options->order == FF_DISPLAY_ORDER_ASC ? sortByNameAsc : sortByNameDesc)); + ffListSort((FFlist*) &dsResult->displays, sizeof(FFDisplayResult), (void*) (options->order == FF_DISPLAY_ORDER_ASC ? sortByNameAsc : sortByNameDesc)); } if (options->compactType != FF_DISPLAY_COMPACT_TYPE_NONE) { diff --git a/src/modules/dns/dns.c b/src/modules/dns/dns.c index 992f2c55a..7612fcac9 100644 --- a/src/modules/dns/dns.c +++ b/src/modules/dns/dns.c @@ -5,7 +5,7 @@ #include "modules/dns/dns.h" bool ffPrintDNS(FFDNSOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectDNS(options, &result); @@ -101,7 +101,7 @@ void ffGenerateDNSJsonConfig(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_ } bool ffGenerateDNSJsonResult(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectDNS(options, &result); diff --git a/src/modules/gamepad/gamepad.c b/src/modules/gamepad/gamepad.c index 8a8f37e35..f11fc4768 100644 --- a/src/modules/gamepad/gamepad.c +++ b/src/modules/gamepad/gamepad.c @@ -49,7 +49,7 @@ static void printDevice(FFGamepadOptions* options, const FFGamepadDevice* device } bool ffPrintGamepad(FFGamepadOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectGamepad(&result); @@ -63,7 +63,7 @@ bool ffPrintGamepad(FFGamepadOptions* options) { return false; } - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFGamepadDevice*)); + FF_LIST_AUTO_DESTROY filtered = ffListCreate(); FF_LIST_FOR_EACH (FFGamepadDevice, device, result) { bool ignored = false; FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) { @@ -73,7 +73,7 @@ bool ffPrintGamepad(FFGamepadOptions* options) { } } if (!ignored) { - FFGamepadDevice** ptr = ffListAdd(&filtered); + FFGamepadDevice** ptr = FF_LIST_ADD(FFGamepadDevice*, filtered); *ptr = device; } } @@ -111,7 +111,7 @@ void ffParseGamepadJsonObject(FFGamepadOptions* options, yyjson_val* module) { size_t eidx, emax; yyjson_arr_foreach (val, eidx, emax, elem) { if (yyjson_is_str(elem)) { - FFstrbuf* strbuf = ffListAdd(&options->ignores); + FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores); ffStrbufInitJsonVal(strbuf, elem); } } @@ -139,7 +139,7 @@ void ffGenerateGamepadJsonConfig(FFGamepadOptions* options, yyjson_mut_doc* doc, } bool ffGenerateGamepadJsonResult(FF_A_UNUSED FFGamepadOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectGamepad(&result); @@ -175,7 +175,7 @@ bool ffGenerateGamepadJsonResult(FF_A_UNUSED FFGamepadOptions* options, yyjson_m void ffInitGamepadOptions(FFGamepadOptions* options) { ffOptionInitModuleArg(&options->moduleArgs, "󰺵"); - ffListInit(&options->ignores, sizeof(FFstrbuf)); + ffListInit(&options->ignores); options->percent = (FFPercentageModuleConfig) { 50, 20, 0 }; } diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index 54a9433a6..2a323e797 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -165,15 +165,14 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu } bool ffPrintGPU(FFGPUOptions* options) { - FF_LIST_AUTO_DESTROY gpus = ffListCreate(sizeof(FFGPUResult)); + FF_LIST_AUTO_DESTROY gpus = ffListCreate(); const char* error = ffDetectGPU(options, &gpus); if (error) { ffPrintError(FF_GPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); return false; } - FF_LIST_AUTO_DESTROY selectedGPUs; - ffListInitA(&selectedGPUs, sizeof(const FFGPUResult*), gpus.length); + FF_LIST_AUTO_DESTROY selectedGPUs = ffListCreateA(sizeof(const FFGPUResult*), gpus.length); FF_LIST_FOR_EACH (FFGPUResult, gpu, gpus) { if (gpu->type == FF_GPU_TYPE_UNKNOWN && options->hideType == FF_GPU_TYPE_UNKNOWN) { @@ -188,11 +187,13 @@ bool ffPrintGPU(FFGPUOptions* options) { continue; } - *(const FFGPUResult**) ffListAdd(&selectedGPUs) = gpu; + *FF_LIST_ADD(const FFGPUResult*, selectedGPUs) = gpu; } - for (uint32_t i = 0; i < selectedGPUs.length; i++) { - printGPUResult(options, selectedGPUs.length == 1 ? 0 : (uint8_t) (i + 1), *FF_LIST_GET(const FFGPUResult*, selectedGPUs, i)); + uint32_t i = 0; + FF_LIST_FOR_EACH (const FFGPUResult*, pgpu, selectedGPUs) { + printGPUResult(options, selectedGPUs.length == 1 ? 0 : (uint8_t) (i + 1), *pgpu); + ++i; } if (selectedGPUs.length == 0) { @@ -318,7 +319,7 @@ void ffGenerateGPUJsonConfig(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ } bool ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY gpus = ffListCreate(sizeof(FFGPUResult)); + FF_LIST_AUTO_DESTROY gpus = ffListCreate(); const char* error = ffDetectGPU(options, &gpus); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); diff --git a/src/modules/keyboard/keyboard.c b/src/modules/keyboard/keyboard.c index 524b3469e..2a96854fc 100644 --- a/src/modules/keyboard/keyboard.c +++ b/src/modules/keyboard/keyboard.c @@ -18,7 +18,7 @@ static void printDevice(FFKeyboardOptions* options, const FFKeyboardDevice* devi } bool ffPrintKeyboard(FFKeyboardOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectKeyboard(&result); @@ -32,7 +32,7 @@ bool ffPrintKeyboard(FFKeyboardOptions* options) { return false; } - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFKeyboardDevice*)); + FF_LIST_AUTO_DESTROY filtered = ffListCreate(); FF_LIST_FOR_EACH (FFKeyboardDevice, device, result) { bool ignored = false; FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) { @@ -42,7 +42,7 @@ bool ffPrintKeyboard(FFKeyboardOptions* options) { } } if (!ignored) { - FFKeyboardDevice** ptr = ffListAdd(&filtered); + FFKeyboardDevice** ptr = FF_LIST_ADD(FFKeyboardDevice*, filtered); *ptr = device; } } @@ -77,7 +77,7 @@ void ffParseKeyboardJsonObject(FFKeyboardOptions* options, yyjson_val* module) { size_t eidx, emax; yyjson_arr_foreach (val, eidx, emax, elem) { if (yyjson_is_str(elem)) { - FFstrbuf* strbuf = ffListAdd(&options->ignores); + FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores); ffStrbufInitJsonVal(strbuf, elem); } } @@ -100,7 +100,7 @@ void ffGenerateKeyboardJsonConfig(FFKeyboardOptions* options, yyjson_mut_doc* do } bool ffGenerateKeyboardJsonResult(FF_A_UNUSED FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectKeyboard(&result); @@ -136,7 +136,7 @@ bool ffGenerateKeyboardJsonResult(FF_A_UNUSED FFKeyboardOptions* options, yyjson void ffInitKeyboardOptions(FFKeyboardOptions* options) { ffOptionInitModuleArg(&options->moduleArgs, ""); - ffListInit(&options->ignores, sizeof(FFstrbuf)); + ffListInit(&options->ignores); } void ffDestroyKeyboardOptions(FFKeyboardOptions* options) { diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 012b754fc..aa1bdf292 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -95,7 +95,7 @@ static void printIp(FFLocalIpResult* ip, bool markDefaultRoute, FFstrbuf* buffer } bool ffPrintLocalIp(FFLocalIpOptions* options) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectLocalIps(options, &results); @@ -109,7 +109,7 @@ bool ffPrintLocalIp(FFLocalIpOptions* options) { return false; } - ffListSort(&results, (const void*) sortIps); + ffListSort(&results, sizeof(FFLocalIpResult), (const void*) sortIps); FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); @@ -351,7 +351,7 @@ void ffGenerateLocalIpJsonConfig(FFLocalIpOptions* options, yyjson_mut_doc* doc, } bool ffGenerateLocalIpJsonResult(FF_A_UNUSED FFLocalIpOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectLocalIps(options, &results); diff --git a/src/modules/mouse/mouse.c b/src/modules/mouse/mouse.c index cbe52475f..e03b13f32 100644 --- a/src/modules/mouse/mouse.c +++ b/src/modules/mouse/mouse.c @@ -18,7 +18,7 @@ static void printDevice(FFMouseOptions* options, const FFMouseDevice* device, ui } bool ffPrintMouse(FFMouseOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFMouseDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectMouse(&result); @@ -32,7 +32,7 @@ bool ffPrintMouse(FFMouseOptions* options) { return false; } - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFMouseDevice*)); + FF_LIST_AUTO_DESTROY filtered = ffListCreate(); FF_LIST_FOR_EACH (FFMouseDevice, device, result) { bool ignored = false; FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) { @@ -42,7 +42,7 @@ bool ffPrintMouse(FFMouseOptions* options) { } } if (!ignored) { - FFMouseDevice** ptr = ffListAdd(&filtered); + FFMouseDevice** ptr = FF_LIST_ADD(FFMouseDevice*, filtered); *ptr = device; } } @@ -80,7 +80,7 @@ void ffParseMouseJsonObject(FFMouseOptions* options, yyjson_val* module) { size_t eidx, emax; yyjson_arr_foreach (val, eidx, emax, elem) { if (yyjson_is_str(elem)) { - FFstrbuf* strbuf = ffListAdd(&options->ignores); + FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores); ffStrbufInitJsonVal(strbuf, elem); } } @@ -103,7 +103,7 @@ void ffGenerateMouseJsonConfig(FFMouseOptions* options, yyjson_mut_doc* doc, yyj } bool ffGenerateMouseJsonResult(FF_A_UNUSED FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFMouseDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectMouse(&result); @@ -139,7 +139,7 @@ bool ffGenerateMouseJsonResult(FF_A_UNUSED FFMouseOptions* options, yyjson_mut_d void ffInitMouseOptions(FFMouseOptions* options) { ffOptionInitModuleArg(&options->moduleArgs, "󰍽"); - ffListInit(&options->ignores, sizeof(FFstrbuf)); + ffListInit(&options->ignores); } void ffDestroyMouseOptions(FFMouseOptions* options) { diff --git a/src/modules/netio/netio.c b/src/modules/netio/netio.c index dc63a17ad..00d58dffa 100644 --- a/src/modules/netio/netio.c +++ b/src/modules/netio/netio.c @@ -29,7 +29,7 @@ static void formatKey(const FFNetIOOptions* options, FFNetIOResult* inf, uint32_ } bool ffPrintNetIO(FFNetIOOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectNetIO(&result, options); if (error) { @@ -37,7 +37,7 @@ bool ffPrintNetIO(FFNetIOOptions* options) { return false; } - ffListSort(&result, (const void*) sortInfs); + ffListSort(&result, sizeof(FFNetIOResult), (const void*) sortInfs); uint32_t index = 0; FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -148,7 +148,7 @@ void ffGenerateNetIOJsonConfig(FFNetIOOptions* options, yyjson_mut_doc* doc, yyj } bool ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectNetIO(&result, options); if (error) { diff --git a/src/modules/physicaldisk/physicaldisk.c b/src/modules/physicaldisk/physicaldisk.c index ed7b678a4..803e01442 100644 --- a/src/modules/physicaldisk/physicaldisk.c +++ b/src/modules/physicaldisk/physicaldisk.c @@ -26,7 +26,7 @@ static void formatKey(const FFPhysicalDiskOptions* options, FFPhysicalDiskResult } bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectPhysicalDisk(&result, options); if (error) { @@ -34,7 +34,7 @@ bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { return false; } - ffListSort(&result, (const void*) sortDevices); + ffListSort(&result, sizeof(FFPhysicalDiskResult), (const void*) sortDevices); uint32_t index = 0; FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -181,7 +181,7 @@ void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut } bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectPhysicalDisk(&result, options); if (error) { diff --git a/src/modules/physicalmemory/physicalmemory.c b/src/modules/physicalmemory/physicalmemory.c index 48319f85c..d4bee7db5 100644 --- a/src/modules/physicalmemory/physicalmemory.c +++ b/src/modules/physicalmemory/physicalmemory.c @@ -7,8 +7,8 @@ #define FF_PHYSICALMEMORY_DISPLAY_NAME "Physical Memory" bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult)); - const char* error = ffDetectPhysicalMemory(&result); + FF_LIST_AUTO_DESTROY result = ffListCreate(); + const char* error = ffDetectPhysicalMemory(options, &result); if (error) { ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); @@ -16,35 +16,21 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) { } if (result.length == 0) { - ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No physical memory detected"); - return false; - } - - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFPhysicalMemoryResult*)); - FF_LIST_FOR_EACH (FFPhysicalMemoryResult, device, result) { - if (!options->showEmptySlots && !device->installed) { - continue; - } - - *(FFPhysicalMemoryResult**) ffListAdd(&filtered) = device; - } - - if (filtered.length == 0) { - ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No installed physical memory detected"); + ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No physical memory devices detected"); return false; } FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate(); - for (uint32_t i = 0; i < filtered.length; ++i) { - FFPhysicalMemoryResult* device = *FF_LIST_GET(FFPhysicalMemoryResult*, filtered, i); + uint32_t i = 0; + FF_LIST_FOR_EACH (FFPhysicalMemoryResult, device, result) { ffStrbufClear(&prettySize); if (device->installed) { ffSizeAppendNum(device->size, &prettySize); } if (options->moduleArgs.outputFormat.length == 0) { - ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, filtered.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, result.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); if (device->installed) { fputs(prettySize.chars, stdout); @@ -88,6 +74,8 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) { FF_ARG(device->installed, "is-installed"), })); } + + i++; } FF_LIST_FOR_EACH (FFPhysicalMemoryResult, device, result) { @@ -124,9 +112,9 @@ void ffGeneratePhysicalMemoryJsonConfig(FFPhysicalMemoryOptions* options, yyjson yyjson_mut_obj_add_bool(doc, module, "showEmptySlots", options->showEmptySlots); } -bool ffGeneratePhysicalMemoryJsonResult(FF_A_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult)); - const char* error = ffDetectPhysicalMemory(&result); +bool ffGeneratePhysicalMemoryJsonResult(FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + FF_LIST_AUTO_DESTROY result = ffListCreate(); + const char* error = ffDetectPhysicalMemory(options, &result); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); diff --git a/src/modules/poweradapter/poweradapter.c b/src/modules/poweradapter/poweradapter.c index 3467e7629..03d405372 100644 --- a/src/modules/poweradapter/poweradapter.c +++ b/src/modules/poweradapter/poweradapter.c @@ -7,7 +7,7 @@ #define FF_POWERADAPTER_DISPLAY_NAME "Power Adapter" bool ffPrintPowerAdapter(FFPowerAdapterOptions* options) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFPowerAdapterResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectPowerAdapter(&results); @@ -70,7 +70,7 @@ void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* m } bool ffGeneratePowerAdapterJsonResult(FF_A_UNUSED FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFPowerAdapterResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectPowerAdapter(&results); diff --git a/src/modules/sound/option.h b/src/modules/sound/option.h index cb8a6f1a4..989256913 100644 --- a/src/modules/sound/option.h +++ b/src/modules/sound/option.h @@ -4,14 +4,16 @@ #include "common/percent.h" typedef enum FF_A_PACKED FFSoundType { - FF_SOUND_TYPE_MAIN, - FF_SOUND_TYPE_ACTIVE, - FF_SOUND_TYPE_ALL, + FF_SOUND_TYPE_NONE = 0, + FF_SOUND_TYPE_MAIN = 1 << 0, + FF_SOUND_TYPE_ACTIVE = 1 << 1, } FFSoundType; typedef struct FFSoundOptions { FFModuleArgs moduleArgs; + // Reports matched device only, otherwise reports all devices + // NOTE: for FF_SOUND_TYPE_NONE, reports all devices FFSoundType soundType; FFPercentageModuleConfig percent; } FFSoundOptions; diff --git a/src/modules/sound/sound.c b/src/modules/sound/sound.c index f917fb7be..34286baf1 100644 --- a/src/modules/sound/sound.c +++ b/src/modules/sound/sound.c @@ -65,50 +65,25 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui bool ffPrintSound(FFSoundOptions* options) { bool success = false; - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); - const char* error = ffDetectSound(&result); + const char* error = ffDetectSound(options, &result); if (error) { ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - goto exit; + return false; } - { - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFSoundDevice*)); - - FF_LIST_FOR_EACH (FFSoundDevice, device, result) { - switch (options->soundType) { - case FF_SOUND_TYPE_MAIN: - if (!device->main) { - continue; - } - break; - case FF_SOUND_TYPE_ACTIVE: - if (!device->active) { - continue; - } - break; - case FF_SOUND_TYPE_ALL: - break; - } - - *(FFSoundDevice**) ffListAdd(&filtered) = device; - } - - if (filtered.length == 0) { - ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No active sound devices found"); - goto exit; - } - - uint8_t index = 1; - FF_LIST_FOR_EACH (FFSoundDevice*, device, filtered) { - printDevice(options, *device, filtered.length == 1 ? 0 : index++); - } + if (result.length == 0) { + ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No matched sound devices found"); + return false; + } + + uint8_t index = 1; + FF_LIST_FOR_EACH (FFSoundDevice, device, result) { + printDevice(options, device, result.length == 1 ? 0 : index++); } - success = true; -exit: FF_LIST_FOR_EACH (FFSoundDevice, device, result) { ffStrbufDestroy(&device->identifier); ffStrbufDestroy(&device->name); @@ -131,7 +106,7 @@ void ffParseSoundJsonObject(FFSoundOptions* options, yyjson_val* module) { const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { { "main", FF_SOUND_TYPE_MAIN }, { "active", FF_SOUND_TYPE_ACTIVE }, - { "all", FF_SOUND_TYPE_ALL }, + { "all", FF_SOUND_TYPE_NONE }, // Don't filter devices {}, }); if (error) { @@ -160,7 +135,7 @@ void ffGenerateSoundJsonConfig(FFSoundOptions* options, yyjson_mut_doc* doc, yyj case FF_SOUND_TYPE_ACTIVE: yyjson_mut_obj_add_str(doc, module, "soundType", "active"); break; - case FF_SOUND_TYPE_ALL: + case FF_SOUND_TYPE_NONE: yyjson_mut_obj_add_str(doc, module, "soundType", "all"); break; } @@ -168,9 +143,9 @@ void ffGenerateSoundJsonConfig(FFSoundOptions* options, yyjson_mut_doc* doc, yyj ffPercentGenerateJsonConfig(doc, module, options->percent); } -bool ffGenerateSoundJsonResult(FF_A_UNUSED FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice)); - const char* error = ffDetectSound(&result); +bool ffGenerateSoundJsonResult(FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + FF_LIST_AUTO_DESTROY result = ffListCreate(); + const char* error = ffDetectSound(options, &result); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); diff --git a/src/modules/swap/swap.c b/src/modules/swap/swap.c index e73b7dd3f..a612b413d 100644 --- a/src/modules/swap/swap.c +++ b/src/modules/swap/swap.c @@ -86,7 +86,7 @@ void printSwap(FFSwapOptions* options, uint8_t index, uint32_t totalCount, FFSwa } bool ffPrintSwap(FFSwapOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSwapResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectSwap(&result); if (error) { @@ -148,7 +148,7 @@ void ffGenerateSwapJsonConfig(FFSwapOptions* options, yyjson_mut_doc* doc, yyjso } bool ffGenerateSwapJsonResult(FF_A_UNUSED FFSwapOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSwapResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectSwap(&result); if (error) { diff --git a/src/modules/users/users.c b/src/modules/users/users.c index 4db235f0d..b920ccdea 100644 --- a/src/modules/users/users.c +++ b/src/modules/users/users.c @@ -8,7 +8,7 @@ #pragma GCC diagnostic ignored "-Wformat" // warning: unknown conversion type character 'F' in format bool ffPrintUsers(FFUsersOptions* options) { - FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFUserResult)); + FF_LIST_AUTO_DESTROY users = ffListCreate(); const char* error = ffDetectUsers(options, &users); @@ -134,7 +134,7 @@ void ffGenerateUsersJsonConfig(FFUsersOptions* options, yyjson_mut_doc* doc, yyj } bool ffGenerateUsersJsonResult(FFUsersOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFUserResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectUsers(options, &results); diff --git a/src/modules/wifi/wifi.c b/src/modules/wifi/wifi.c index 6b48df6c8..b6005a3f7 100644 --- a/src/modules/wifi/wifi.c +++ b/src/modules/wifi/wifi.c @@ -5,7 +5,7 @@ #include "modules/wifi/wifi.h" bool ffPrintWifi(FFWifiOptions* options) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectWifi(&result); if (error) { @@ -150,7 +150,7 @@ void ffGenerateWifiJsonConfig(FFWifiOptions* options, yyjson_mut_doc* doc, yyjso } bool ffGenerateWifiJsonResult(FF_A_UNUSED FFWifiOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(); const char* error = ffDetectWifi(&result); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); diff --git a/src/modules/zpool/zpool.c b/src/modules/zpool/zpool.c index b8f587261..c0e2cd39f 100644 --- a/src/modules/zpool/zpool.c +++ b/src/modules/zpool/zpool.c @@ -100,7 +100,7 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i } bool ffPrintZpool(FFZpoolOptions* options) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectZpool(&results); @@ -149,7 +149,7 @@ void ffGenerateZpoolJsonConfig(FFZpoolOptions* options, yyjson_mut_doc* doc, yyj } bool ffGenerateZpoolJsonResult(FF_A_UNUSED FFZpoolOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(); const char* error = ffDetectZpool(&results); if (error) { diff --git a/src/options/display.c b/src/options/display.c index 57d0629d9..5a97c4aaa 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -474,7 +474,8 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va yyjson_val* item; size_t idx, max; yyjson_arr_foreach (val, idx, max, item) { - ffStrbufInitJsonVal(ffListAdd(&options->constants), item); + FFstrbuf* buffer = FF_LIST_ADD(FFstrbuf, options->constants); + ffStrbufInitJsonVal(buffer, item); } } else if (unsafe_yyjson_equals_str(key, "freq")) { if (!yyjson_is_obj(val)) { @@ -814,7 +815,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options) { options->fractionNdigits = 2; options->fractionTrailingZeros = FF_FRACTION_TRAILING_ZEROS_TYPE_DEFAULT; - ffListInit(&options->constants, sizeof(FFstrbuf)); + ffListInit(&options->constants); } void ffOptionsDestroyDisplay(FFOptionsDisplay* options) { diff --git a/tests/format.c b/tests/format.c index 868586adb..b29b6be79 100644 --- a/tests/format.c +++ b/tests/format.c @@ -112,9 +112,9 @@ int main(void) { #ifndef _WIN32 // Windows doesn't have setenv { - ffListInit(&instance.config.display.constants, sizeof(FFstrbuf)); - ffStrbufInitStatic(ffListAdd(&instance.config.display.constants), "CONST1"); - ffStrbufInitStatic(ffListAdd(&instance.config.display.constants), "CONST2"); + ffListInit(&instance.config.display.constants); + ffStrbufInitStatic(FF_LIST_ADD(FFstrbuf, instance.config.display.constants), "CONST1"); + ffStrbufInitStatic(FF_LIST_ADD(FFstrbuf, instance.config.display.constants), "CONST2"); setenv("FF_TEST", "ENVVAR", 1); VERIFY("output({$FF_TEST})", "", "output(ENVVAR)"); VERIFY("output({$1})", "", "output(CONST1)"); diff --git a/tests/list.c b/tests/list.c index e43b88cac..c0ead67de 100644 --- a/tests/list.c +++ b/tests/list.c @@ -10,7 +10,7 @@ __attribute__((__noreturn__)) static void testFailed(const FFlist* list, const c fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr); fprintf(stderr, "[%d] %s, list:", lineNo, expression); for (uint32_t i = 0; i < list->length; ++i) { - fprintf(stderr, "%u ", *(uint32_t*) ffListGet(list, i)); + fprintf(stderr, "%u ", *FF_LIST_GET(uint32_t, *list, i)); } fputc('\n', stderr); fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr); @@ -18,8 +18,8 @@ __attribute__((__noreturn__)) static void testFailed(const FFlist* list, const c exit(1); } -static bool numEqualsAdapter(const void* first, const void* second) { - return *(uint32_t*) first == *(uint32_t*) second; +static bool numEqualsAdapter(const uint32_t* first, const uint32_t* second) { + return *first == *second; } #define VERIFY(expression) \ @@ -31,9 +31,8 @@ int main(void) { // initA - ffListInit(&list, sizeof(uint32_t)); + ffListInit(&list); - VERIFY(list.elementSize == sizeof(uint32_t)); VERIFY(list.capacity == 0); VERIFY(list.length == 0); @@ -43,18 +42,17 @@ int main(void) { } // shift - VERIFY(!ffListShift(&list, &n)); + VERIFY(!FF_LIST_SHIFT(list, &n)); VERIFY(list.length == 0); // pop - VERIFY(!ffListPop(&list, &n)); + VERIFY(!FF_LIST_POP(list, &n)); VERIFY(list.length == 0); // add for (uint32_t i = 1; i <= FF_LIST_DEFAULT_ALLOC + 1; ++i) { - *(uint32_t*) ffListAdd(&list) = i; + *FF_LIST_ADD(uint32_t, list) = i; - VERIFY(list.elementSize == sizeof(uint32_t)); VERIFY(list.length == i); if (i <= FF_LIST_DEFAULT_ALLOC) { @@ -63,8 +61,8 @@ int main(void) { VERIFY(list.capacity == FF_LIST_DEFAULT_ALLOC * 2); } - VERIFY(*(uint32_t*) ffListGet(&list, 0) == 1); - VERIFY(*(uint32_t*) ffListGet(&list, i - 1) == i); + VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 1); + VERIFY(*FF_LIST_GET(uint32_t, list, i - 1) == i); } VERIFY(list.length == FF_LIST_DEFAULT_ALLOC + 1); @@ -77,40 +75,38 @@ int main(void) { // ffListFirstIndexComp n = 10; - VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == 9); + VERIFY(ffListFirstIndexComp(&list, sizeof(n), &n, (void*) numEqualsAdapter) == 9); n = 999; - VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == list.length); + VERIFY(ffListFirstIndexComp(&list, sizeof(n), &n, (void*) numEqualsAdapter) == list.length); // ffListContains n = 10; - VERIFY(ffListContains(&list, &n, numEqualsAdapter)); + VERIFY(FF_LIST_CONTAINS(list, &n, numEqualsAdapter)); n = 999; - VERIFY(!ffListContains(&list, &n, numEqualsAdapter)); + VERIFY(!FF_LIST_CONTAINS(list, &n, numEqualsAdapter)); // shift - VERIFY(ffListShift(&list, &n)); + VERIFY(FF_LIST_SHIFT(list, &n)); VERIFY(n == 1); VERIFY(list.length == FF_LIST_DEFAULT_ALLOC); - VERIFY(*(uint32_t*) ffListGet(&list, 0) == 2); - VERIFY(*(uint32_t*) ffListGet(&list, list.length - 1) == FF_LIST_DEFAULT_ALLOC + 1); + VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 2); + VERIFY(*FF_LIST_GET(uint32_t, list, list.length - 1) == FF_LIST_DEFAULT_ALLOC + 1); // pop - VERIFY(ffListPop(&list, &n)); + VERIFY(FF_LIST_POP(list, &n)); VERIFY(n == FF_LIST_DEFAULT_ALLOC + 1); VERIFY(list.length == FF_LIST_DEFAULT_ALLOC - 1); - VERIFY(*(uint32_t*) ffListGet(&list, 0) == 2); - VERIFY(*(uint32_t*) ffListGet(&list, list.length - 1) == FF_LIST_DEFAULT_ALLOC); + VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 2); + VERIFY(*FF_LIST_GET(uint32_t, list, list.length - 1) == FF_LIST_DEFAULT_ALLOC); // Destroy ffListDestroy(&list); - VERIFY(list.elementSize == sizeof(uint32_t)); VERIFY(list.capacity == 0); VERIFY(list.length == 0); { - FF_LIST_AUTO_DESTROY test = ffListCreate(1); - VERIFY(test.elementSize == 1); + FF_LIST_AUTO_DESTROY test = ffListCreate(); VERIFY(test.capacity == 0); VERIFY(test.length == 0); }