Bluetooth (Windows): remove hard dep of bthprops

Also fix resource leaks and better error message

Ref: https://github.com/fastfetch-cli/fastfetch/issues/623#issuecomment-1815682290
This commit is contained in:
李通洲
2023-11-18 16:09:54 +08:00
parent 7f999dfaaf
commit 36bf978dc1
2 changed files with 17 additions and 7 deletions
-1
View File
@@ -890,7 +890,6 @@ elseif(APPLE)
elseif(WIN32)
target_compile_definitions(libfastfetch PRIVATE -D_WIN32_WINNT=0x0601)
target_link_libraries(libfastfetch
PRIVATE "bthprops"
PRIVATE "dwmapi"
PRIVATE "gdi32"
PRIVATE "iphlpapi"
+17 -6
View File
@@ -1,14 +1,20 @@
#include "bluetooth.h"
#include "common/library.h"
#include "util/windows/unicode.h"
#include <windows.h>
#include <bluetoothapis.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */)
{
// Actually bluetoothapis.dll, but it's missing on Windows 7
FF_LIBRARY_LOAD(bluetoothapis, NULL, "dlopen bthprops.cpl failed", "bthprops.cpl", 1)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(bluetoothapis, BluetoothFindFirstDevice)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(bluetoothapis, BluetoothFindNextDevice)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(bluetoothapis, BluetoothFindDeviceClose)
BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = {
.fReturnConnected = TRUE,
.fReturnRemembered = TRUE,
@@ -20,9 +26,14 @@ const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */)
BLUETOOTH_DEVICE_INFO btdi = {
.dwSize = sizeof(btdi)
};
HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&btsp, &btdi);
HBLUETOOTH_DEVICE_FIND hFind = ffBluetoothFindFirstDevice(&btsp, &btdi);
if(!hFind)
return "BluetoothFindFirstDevice() failed or no devices found";
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
return "No Bluetooth devices found";
else
return "BluetoothFindFirstDevice() failed";
}
do {
FFBluetoothResult* device = ffListAdd(devices);
@@ -114,9 +125,9 @@ const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */)
ffStrbufTrimRight(&device->type, ' ');
ffStrbufTrimRight(&device->type, ',');
}
} while (BluetoothFindNextDevice(hFind, &btdi));
} while (ffBluetoothFindNextDevice(hFind, &btdi));
ffBluetoothFindDeviceClose(hFind);
return NULL;
}
#pragma GCC diagnostic pop