Files
fastfetch/src/modules/bluetooth.c
T

63 lines
2.0 KiB
C
Raw Normal View History

2023-01-24 10:39:33 +01:00
#include "common/printing.h"
#include "detection/bluetooth/bluetooth.h"
#define FF_BLUETOOTH_MODULE_NAME "Bluetooth"
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 4
2023-01-24 12:02:20 +01:00
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}
});
}
}
2023-01-24 10:39:33 +01:00
void ffPrintBluetooth(FFinstance* instance)
{
const FFBluetoothResult* bluetooth = ffDetectBluetooth(instance);
if(bluetooth->error.length > 0)
{
ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, "%s", bluetooth->error.chars);
return;
}
2023-01-24 12:02:20 +01:00
FFlist filtered;
ffListInit(&filtered, sizeof(FFBluetoothDevice*));
FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices)
2023-01-24 10:39:33 +01:00
{
2023-01-24 12:02:20 +01:00
if(!device->connected && !instance->config.bluetoothShowDisconnected)
continue;
2023-01-24 10:39:33 +01:00
2023-01-24 12:02:20 +01:00
*(FFBluetoothDevice**)ffListAdd(&filtered) = device;
}
2023-01-24 10:39:33 +01:00
2023-01-24 12:02:20 +01:00
if(filtered.length == 0)
{
ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, "No bluetooth devices found");
return;
2023-01-24 10:39:33 +01:00
}
2023-01-24 12:02:20 +01:00
for(uint32_t i = 0; i < filtered.length; i++)
2023-01-24 10:39:33 +01:00
{
2023-01-24 12:02:20 +01:00
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
printDevice(instance, *(FFBluetoothDevice**)ffListGet(&filtered, i), index);
2023-01-24 10:39:33 +01:00
}
}