From 4f807f26a952e3e6ff60f2ded44cb3a0ad66f4d9 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 20 Dec 2024 11:07:09 +0800 Subject: [PATCH] Bluetooth (Linux): improve performance when `showDisconnected == false` If no connected devices found, no dbus calls will be emitted Fix #1450 --- src/detection/bluetooth/bluetooth.h | 2 +- src/detection/bluetooth/bluetooth_apple.m | 2 +- src/detection/bluetooth/bluetooth_linux.c | 22 ++++++++++++++++++- src/detection/bluetooth/bluetooth_nosupport.c | 2 +- src/detection/bluetooth/bluetooth_windows.c | 2 +- src/modules/bluetooth/bluetooth.c | 6 ++--- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/detection/bluetooth/bluetooth.h b/src/detection/bluetooth/bluetooth.h index 0702abbdb..57bf9ef3a 100644 --- a/src/detection/bluetooth/bluetooth.h +++ b/src/detection/bluetooth/bluetooth.h @@ -11,4 +11,4 @@ typedef struct FFBluetoothResult bool connected; } FFBluetoothResult; -const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */); +const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FFBluetoothResult */); diff --git a/src/detection/bluetooth/bluetooth_apple.m b/src/detection/bluetooth/bluetooth_apple.m index 4f29deb56..dfd2d0efc 100644 --- a/src/detection/bluetooth/bluetooth_apple.m +++ b/src/detection/bluetooth/bluetooth_apple.m @@ -10,7 +10,7 @@ @property (nonatomic) uint8_t batteryPercentSingle; @end -const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */) +const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FFBluetoothResult */) { NSArray* ioDevices = IOBluetoothDevice.pairedDevices; if(!ioDevices) diff --git a/src/detection/bluetooth/bluetooth_linux.c b/src/detection/bluetooth/bluetooth_linux.c index b7ae5a839..ea1f09719 100644 --- a/src/detection/bluetooth/bluetooth_linux.c +++ b/src/detection/bluetooth/bluetooth_linux.c @@ -3,6 +3,7 @@ #ifdef FF_HAVE_DBUS #include "common/dbus.h" +#include "common/io/io.h" /* Example dbus reply, striped to only the relevant parts: array [ //root @@ -192,11 +193,30 @@ static const char* detectBluetooth(FFlist* devices) return NULL; } +static bool hasConnectedDevices(void) +{ + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/bluetooth"); + if(dirp == NULL) + return false; + + struct dirent* entry; + while ((entry = readdir(dirp)) != NULL) + { + if (strchr(entry->d_name, ':') != NULL) // ignore connected devices + return true; + } + + return false; +} + #endif -const char* ffDetectBluetooth(FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothResult */) +const char* ffDetectBluetooth(FFBluetoothOptions* options, FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothResult */) { #ifdef FF_HAVE_DBUS + if (!options->showDisconnected && !hasConnectedDevices()) + return NULL; + return detectBluetooth(devices); #else return "Fastfetch was compiled without DBus support"; diff --git a/src/detection/bluetooth/bluetooth_nosupport.c b/src/detection/bluetooth/bluetooth_nosupport.c index 8db5a465c..c119ecb95 100644 --- a/src/detection/bluetooth/bluetooth_nosupport.c +++ b/src/detection/bluetooth/bluetooth_nosupport.c @@ -1,6 +1,6 @@ #include "bluetooth.h" -const char* ffDetectBluetooth(FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothResult */) +const char* ffDetectBluetooth(FF_MAYBE_UNUSED FFBluetoothOptions* options, FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothResult */) { return "Not supported on this platform"; } diff --git a/src/detection/bluetooth/bluetooth_windows.c b/src/detection/bluetooth/bluetooth_windows.c index 80b83837a..d59a31623 100644 --- a/src/detection/bluetooth/bluetooth_windows.c +++ b/src/detection/bluetooth/bluetooth_windows.c @@ -7,7 +7,7 @@ #pragma GCC diagnostic ignored "-Wpointer-sign" -const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */) +const char* ffDetectBluetooth(FFBluetoothOptions* options, FFlist* devices /* FFBluetoothResult */) { // Actually bluetoothapis.dll, but it's missing on Windows 7 FF_LIBRARY_LOAD(bluetoothapis, "dlopen bthprops.cpl failed", "bthprops.cpl", 1) diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index fe3357d4f..7563b1d54 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -59,7 +59,7 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de void ffPrintBluetooth(FFBluetoothOptions* options) { FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothResult)); - const char* error = ffDetectBluetooth(&devices); + const char* error = ffDetectBluetooth(options, &devices); if(error) { @@ -155,11 +155,11 @@ void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc* ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent); } -void ffGenerateBluetoothJsonResult(FF_MAYBE_UNUSED FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothResult)); - const char* error = ffDetectBluetooth(&results); + const char* error = ffDetectBluetooth(options, &results); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error);