mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
Bluetooth: Support multiple devices
This commit is contained in:
+40
-14
@@ -4,6 +4,29 @@
|
||||
#define FF_BLUETOOTH_MODULE_NAME "Bluetooth"
|
||||
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 4
|
||||
|
||||
static void printDevice(FFinstance* instance, const FFBluetoothDevice* device, uint8_t index)
|
||||
{
|
||||
if(instance->config.bluetooth.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_BLUETOOTH_MODULE_NAME, index, &instance->config.bluetooth.key);
|
||||
ffStrbufWriteTo(&device->name, stdout);
|
||||
|
||||
if(device->battery > 0)
|
||||
printf(" (%d%%)", device->battery);
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_BLUETOOTH_MODULE_NAME, index, &instance->config.bluetooth, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->address},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->type},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &device->battery}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintBluetooth(FFinstance* instance)
|
||||
{
|
||||
const FFBluetoothResult* bluetooth = ffDetectBluetooth(instance);
|
||||
@@ -14,23 +37,26 @@ void ffPrintBluetooth(FFinstance* instance)
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.bluetooth.outputFormat.length == 0)
|
||||
FFlist filtered;
|
||||
ffListInit(&filtered, sizeof(FFBluetoothDevice*));
|
||||
|
||||
FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth.key);
|
||||
ffStrbufWriteTo(&bluetooth->name, stdout);
|
||||
if(!device->connected && !instance->config.bluetoothShowDisconnected)
|
||||
continue;
|
||||
|
||||
if(bluetooth->battery > 0)
|
||||
printf(" (%d%%)", bluetooth->battery);
|
||||
|
||||
putchar('\n');
|
||||
*(FFBluetoothDevice**)ffListAdd(&filtered) = device;
|
||||
}
|
||||
else
|
||||
|
||||
if(filtered.length == 0)
|
||||
{
|
||||
ffPrintFormat(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->address},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->type},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &bluetooth->battery}
|
||||
});
|
||||
ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, "No bluetooth devices found");
|
||||
return;
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < filtered.length; i++)
|
||||
{
|
||||
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
|
||||
printDevice(instance, *(FFBluetoothDevice**)ffListGet(&filtered, i), index);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user