Files
fastfetch/src/modules/bluetooth/bluetooth.c
T

126 lines
3.8 KiB
C
Raw Normal View History

2023-03-13 17:36:48 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-13 17:36:48 +08:00
#include "detection/bluetooth/bluetooth.h"
#include "modules/bluetooth/bluetooth.h"
#include "util/stringUtils.h"
2023-03-13 17:36:48 +08:00
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 4
static void printDevice(FFBluetoothOptions* options, const FFBluetoothDevice* device, uint8_t index)
2023-03-13 17:36:48 +08:00
{
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-13 17:36:48 +08:00
ffStrbufWriteTo(&device->name, stdout);
if(device->battery > 0)
printf(" (%d%%)", device->battery);
2023-06-24 15:58:37 +08:00
if(!device->connected)
puts(" [disconnected]");
else
putchar('\n');
2023-03-13 17:36:48 +08:00
}
else
{
ffPrintFormat(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) {
2023-03-13 17:36:48 +08:00
{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(FFBluetoothOptions* options)
2023-03-13 17:36:48 +08:00
{
2023-06-12 21:50:25 +08:00
FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothDevice));
const char* error = ffDetectBluetooth(&devices);
2023-03-13 17:36:48 +08:00
2023-06-12 21:50:25 +08:00
if(error)
2023-03-13 17:36:48 +08:00
{
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2023-03-13 17:36:48 +08:00
}
2023-06-24 15:58:37 +08:00
else
2023-03-13 17:36:48 +08:00
{
2023-06-24 15:58:37 +08:00
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*));
2023-03-13 17:36:48 +08:00
2023-06-24 15:58:37 +08:00
FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices)
{
if(!device->connected && !options->showDisconnected)
continue;
2023-03-13 17:36:48 +08:00
2023-06-24 15:58:37 +08:00
*(FFBluetoothDevice**)ffListAdd(&filtered) = device;
}
2023-03-13 17:36:48 +08:00
2023-06-24 15:58:37 +08:00
if(filtered.length == 0)
{
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "No bluetooth devices found");
}
for(uint32_t i = 0; i < filtered.length; i++)
{
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
printDevice(options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index);
}
2023-03-13 17:36:48 +08:00
}
2023-06-12 21:50:25 +08:00
FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices)
{
ffStrbufDestroy(&device->name);
ffStrbufDestroy(&device->type);
ffStrbufDestroy(&device->address);
}
2023-03-13 17:36:48 +08:00
}
void ffInitBluetoothOptions(FFBluetoothOptions* options)
{
2023-08-17 10:56:54 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_BLUETOOTH_MODULE_NAME, ffParseBluetoothCommandOptions, ffParseBluetoothJsonObject, ffPrintBluetooth);
2023-03-13 17:36:48 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
options->showDisconnected = false;
}
bool ffParseBluetoothCommandOptions(FFBluetoothOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_BLUETOOTH_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if (ffStrEqualsIgnCase(subKey, "show-disconnected"))
2023-06-24 15:58:37 +08:00
{
2023-03-13 17:36:48 +08:00
options->showDisconnected = ffOptionParseBoolean(value);
2023-06-24 15:58:37 +08:00
return true;
}
2023-03-13 17:36:48 +08:00
return false;
}
void ffDestroyBluetoothOptions(FFBluetoothOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-08-17 10:56:54 +08:00
void ffParseBluetoothJsonObject(FFBluetoothOptions* options, yyjson_val* module)
2023-03-13 17:36:48 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-03-13 17:36:48 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-13 17:36:48 +08:00
2023-08-17 16:09:41 +08:00
if (ffStrEqualsIgnCase(key, "showDisconnected"))
{
options->showDisconnected = yyjson_get_bool(val);
continue;
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-13 17:36:48 +08:00
}
}