Bluetooth: don't use singleton

This commit is contained in:
李通洲
2023-06-12 21:50:25 +08:00
parent fa99508a69
commit 88f354a872
8 changed files with 35 additions and 48 deletions
-1
View File
@@ -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
-15
View File
@@ -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);
)
}
+1 -7
View File
@@ -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
+5 -6
View File
@@ -2,18 +2,15 @@
#import <IOBluetooth/IOBluetooth.h>
void ffDetectBluetoothImpl(FF_MAYBE_UNUSED const FFinstance* instance, FFBluetoothResult* bluetooth)
const char* ffDetectBluetooth(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* FFBluetoothDevice */)
{
NSArray<IOBluetoothDevice*>* 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;
}
+10 -10
View File
@@ -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
}
@@ -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";
}
+5 -3
View File
@@ -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
+12 -4
View File
@@ -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)