diff --git a/CMakeLists.txt b/CMakeLists.txt index 652ae3222..bea9db531 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,7 +247,6 @@ set(LIBFASTFETCH_SRC src/common/printing.c src/common/properties.c src/common/settings.c - src/detection/bluetooth/bluetooth.c src/detection/cpu/cpu.c src/detection/cpuusage/cpuusage.c src/detection/datetime/datetime.c diff --git a/src/detection/bluetooth/bluetooth.c b/src/detection/bluetooth/bluetooth.c deleted file mode 100644 index f254fe535..000000000 --- a/src/detection/bluetooth/bluetooth.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "bluetooth.h" - -#include "../internal.h" - -void ffDetectBluetoothImpl(const FFinstance* instance, FFBluetoothResult* bluetooth); - -const FFBluetoothResult* ffDetectBluetooth(const FFinstance* instance) -{ - FF_DETECTION_INTERNAL_GUARD(FFBluetoothResult, - ffStrbufInit(&result.error); - ffListInit(&result.devices, sizeof(FFBluetoothDevice)); - - ffDetectBluetoothImpl(instance, &result); - ) -} diff --git a/src/detection/bluetooth/bluetooth.h b/src/detection/bluetooth/bluetooth.h index ccd6ecd22..a2aad630f 100644 --- a/src/detection/bluetooth/bluetooth.h +++ b/src/detection/bluetooth/bluetooth.h @@ -14,12 +14,6 @@ typedef struct FFBluetoothDevice bool connected; } FFBluetoothDevice; -typedef struct FFBluetoothResult -{ - FFstrbuf error; - FFlist devices; // List of FFBluetoothDevice -} FFBluetoothResult; - -const FFBluetoothResult* ffDetectBluetooth(const FFinstance* instance); +const char* ffDetectBluetooth(const FFinstance* instance, FFlist* devices /* FFBluetoothDevice */); #endif diff --git a/src/detection/bluetooth/bluetooth_apple.m b/src/detection/bluetooth/bluetooth_apple.m index 8affb9074..061bf2cf3 100644 --- a/src/detection/bluetooth/bluetooth_apple.m +++ b/src/detection/bluetooth/bluetooth_apple.m @@ -2,18 +2,15 @@ #import -void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoothResult* bluetooth) +const char* ffDetectBluetooth(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* FFBluetoothDevice */) { NSArray* ioDevices = IOBluetoothDevice.pairedDevices; if(!ioDevices) - { - ffStrbufAppendS(&bluetooth->error, "IOBluetoothDevice.pairedDevices failed"); - return; - } + return "IOBluetoothDevice.pairedDevices failed"; for(IOBluetoothDevice* ioDevice in ioDevices) { - FFBluetoothDevice* device = ffListAdd(&bluetooth->devices); + FFBluetoothDevice* device = ffListAdd(devices); ffStrbufInitS(&device->name, ioDevice.name.UTF8String); ffStrbufInitS(&device->address, ioDevice.addressString.UTF8String); ffStrbufInit(&device->type); @@ -90,4 +87,6 @@ void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoo ffStrbufTrimRight(&device->type, ','); } } + + return NULL; } diff --git a/src/detection/bluetooth/bluetooth_linux.c b/src/detection/bluetooth/bluetooth_linux.c index bcfca6e7d..229a82a42 100644 --- a/src/detection/bluetooth/bluetooth_linux.c +++ b/src/detection/bluetooth/bluetooth_linux.c @@ -103,7 +103,7 @@ static void detectBluetoothProperty(FFDBusData* dbus, DBusMessageIter* iter, FFB } } -static void detectBluetoothObject(FFBluetoothResult* bluetooth, FFDBusData* dbus, DBusMessageIter* iter) +static void detectBluetoothObject(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter) { if(dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_DICT_ENTRY) return; @@ -129,7 +129,7 @@ static void detectBluetoothObject(FFBluetoothResult* bluetooth, FFDBusData* dbus DBusMessageIter arrayIter; dbus->lib->ffdbus_message_iter_recurse(&dictIter, &arrayIter); - FFBluetoothDevice* device = ffListAdd(&bluetooth->devices); + FFBluetoothDevice* device = ffListAdd(devices); ffStrbufInit(&device->name); ffStrbufInit(&device->address); ffStrbufInit(&device->type); @@ -147,11 +147,11 @@ static void detectBluetoothObject(FFBluetoothResult* bluetooth, FFDBusData* dbus ffStrbufDestroy(&device->name); ffStrbufDestroy(&device->address); ffStrbufDestroy(&device->type); - --bluetooth->devices.length; + --devices->length; } } -static void detectBluetoothRoot(FFBluetoothResult* bluetooth, FFDBusData* dbus, DBusMessageIter* iter) +static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter) { if(dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) return; @@ -161,12 +161,12 @@ static void detectBluetoothRoot(FFBluetoothResult* bluetooth, FFDBusData* dbus, while(true) { - detectBluetoothObject(bluetooth, dbus, &arrayIter); + detectBluetoothObject(devices, dbus, &arrayIter); FF_DBUS_ITER_CONTINUE(dbus, &arrayIter); } } -static const char* detectBluetooth(const FFinstance* instance, FFBluetoothResult* bluetooth) +static const char* detectBluetooth(const FFinstance* instance, FFlist* devices) { FFDBusData dbus; const char* error = ffDBusLoadData(instance, DBUS_BUS_SYSTEM, &dbus); @@ -184,7 +184,7 @@ static const char* detectBluetooth(const FFinstance* instance, FFBluetoothResult return "Failed to get root iterator of GetManagedObjects"; } - detectBluetoothRoot(bluetooth, &dbus, &rootIter); + detectBluetoothRoot(devices, &dbus, &rootIter); dbus.lib->ffdbus_message_unref(managedObjects); return NULL; @@ -192,12 +192,12 @@ static const char* detectBluetooth(const FFinstance* instance, FFBluetoothResult #endif -void ffDetectBluetoothImpl(const FFinstance* instance, FFBluetoothResult* bluetooth) +const char* ffDetectBluetooth(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* FFBluetoothDevice */) { #ifdef FF_HAVE_DBUS - ffStrbufAppendS(&bluetooth->error, detectBluetooth(instance, bluetooth)); + return detectBluetooth(instance, devices); #else FF_UNUSED(instance); - ffStrbufAppendS(&bluetooth->error, "Fastfetch was compiled without DBus support"); + return "Fastfetch was compiled without DBus support"; #endif } diff --git a/src/detection/bluetooth/bluetooth_nosupport.c b/src/detection/bluetooth/bluetooth_nosupport.c index f8cafe045..4fbc5353c 100644 --- a/src/detection/bluetooth/bluetooth_nosupport.c +++ b/src/detection/bluetooth/bluetooth_nosupport.c @@ -1,6 +1,6 @@ #include "bluetooth.h" -void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoothResult* bluetooth) +const char* ffDetectBluetooth(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothDevice */) { - ffStrbufAppendS(&bluetooth->error, "Bluetooth not supported on this platform"); + return "Not supported on this platform"; } diff --git a/src/detection/bluetooth/bluetooth_windows.c b/src/detection/bluetooth/bluetooth_windows.c index 76f0c46d9..698eb86db 100644 --- a/src/detection/bluetooth/bluetooth_windows.c +++ b/src/detection/bluetooth/bluetooth_windows.c @@ -7,7 +7,7 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpointer-sign" -void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoothResult* bluetooth) +const char* ffDetectBluetooth(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* FFBluetoothDevice */) { BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = { .fReturnConnected = TRUE, @@ -22,10 +22,10 @@ void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoo }; HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&btsp, &btdi); if(!hFind) - ffStrbufAppendS(&bluetooth->error, "BluetoothFindFirstDevice() failed or no devices found"); + return "BluetoothFindFirstDevice() failed or no devices found"; do { - FFBluetoothDevice* device = ffListAdd(&bluetooth->devices); + FFBluetoothDevice* device = ffListAdd(devices); ffStrbufInit(&device->name); ffStrbufInit(&device->address); ffStrbufInit(&device->type); @@ -110,6 +110,8 @@ void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoo ffStrbufTrimRight(&device->type, ','); } } while (BluetoothFindNextDevice(hFind, &btdi)); + + return NULL; } #pragma GCC diagnostic pop diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index 7d512cf2a..cf78eef7d 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -30,17 +30,18 @@ static void printDevice(FFinstance* instance, FFBluetoothOptions* options, const void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options) { - const FFBluetoothResult* bluetooth = ffDetectBluetooth(instance); + FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothDevice)); + const char* error = ffDetectBluetooth(instance, &devices); - if(bluetooth->error.length > 0) + if(error) { - ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "%s", bluetooth->error.chars); + ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "%s", error); return; } FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*)); - FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices) + FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices) { if(!device->connected && !options->showDisconnected) continue; @@ -59,6 +60,13 @@ void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options) uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); printDevice(instance, options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index); } + + FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices) + { + ffStrbufDestroy(&device->name); + ffStrbufDestroy(&device->type); + ffStrbufDestroy(&device->address); + } } void ffInitBluetoothOptions(FFBluetoothOptions* options)