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

135 lines
5.5 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/stringUtils.h"
2024-02-02 15:09:54 +08:00
#include "detection/libc/libc.h"
#include "detection/camera/camera.h"
#include "modules/camera/camera.h"
2026-03-29 09:28:49 +08:00
static void printDevice(FFCameraOptions* options, const FFCameraResult* device, uint8_t index) {
if (options->moduleArgs.outputFormat.length == 0) {
2024-02-02 15:09:54 +08:00
ffPrintLogoAndKey(FF_CAMERA_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-02-04 10:10:55 +08:00
ffStrbufWriteTo(&device->name, stdout);
2026-03-29 09:28:49 +08:00
if (device->colorspace.length > 0) {
2024-02-04 10:10:55 +08:00
fputs(" - ", stdout);
ffStrbufWriteTo(&device->colorspace, stdout);
}
2026-03-29 09:28:49 +08:00
if (device->width > 0 && device->height > 0) {
2024-02-04 13:41:08 +08:00
printf(" (%ux%u px)\n", (unsigned) device->width, (unsigned) device->height);
2026-03-29 09:28:49 +08:00
} else {
2024-02-04 10:10:55 +08:00
putchar('\n');
2026-03-29 09:28:49 +08:00
}
} else {
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_CAMERA_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, (((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(device->name, "name"),
FF_ARG(device->vendor, "vendor"),
FF_ARG(device->colorspace, "colorspace"),
FF_ARG(device->id, "id"),
FF_ARG(device->width, "width"),
FF_ARG(device->height, "height"),
})));
2024-02-02 15:09:54 +08:00
}
}
2026-03-29 09:28:49 +08:00
bool ffPrintCamera(FFCameraOptions* options) {
2024-02-02 15:09:54 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult));
const char* error = ffDetectCamera(&result);
2026-03-29 09:28:49 +08:00
if (error) {
ffPrintError(FF_CAMERA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-02-02 15:09:54 +08:00
}
2026-03-29 09:28:49 +08:00
if (result.length == 0) {
ffPrintError(FF_CAMERA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No camera found");
return false;
2024-02-02 15:09:54 +08:00
}
2026-03-29 09:28:49 +08:00
for (uint32_t i = 0; i < result.length; i++) {
2024-02-02 15:09:54 +08:00
uint8_t index = (uint8_t) (result.length == 1 ? 0 : i + 1);
printDevice(options, FF_LIST_GET(FFCameraResult, result, i), index);
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFCameraResult, dev, result) {
2024-02-02 15:09:54 +08:00
ffStrbufDestroy(&dev->name);
ffStrbufDestroy(&dev->id);
2024-02-04 10:10:55 +08:00
ffStrbufDestroy(&dev->colorspace);
2024-02-02 15:09:54 +08:00
}
return true;
2024-02-02 15:09:54 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseCameraJsonObject(FFCameraOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-02-02 15:09:54 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2024-02-02 15:09:54 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-02-02 15:09:54 +08:00
ffPrintError(FF_CAMERA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-02-02 15:09:54 +08:00
}
}
2026-03-29 09:28:49 +08:00
void ffGenerateCameraJsonConfig(FFCameraOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-02-02 15:09:54 +08:00
}
2026-04-09 16:20:59 +08:00
bool ffGenerateCameraJsonResult(FF_A_UNUSED FFCameraOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2024-02-02 15:09:54 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult));
const char* error = ffDetectCamera(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2024-02-02 15:09:54 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-02-02 15:09:54 +08:00
}
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFCameraResult, dev, result) {
2024-02-02 15:09:54 +08:00
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &dev->name);
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &dev->vendor);
2024-02-04 10:10:55 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "colorSpace", &dev->colorspace);
2024-02-02 15:09:54 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "id", &dev->id);
yyjson_mut_obj_add_uint(doc, obj, "width", dev->width);
yyjson_mut_obj_add_uint(doc, obj, "height", dev->height);
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFCameraResult, dev, result) {
2024-02-02 15:09:54 +08:00
ffStrbufDestroy(&dev->name);
ffStrbufDestroy(&dev->id);
2024-02-04 10:10:55 +08:00
ffStrbufDestroy(&dev->colorspace);
2024-02-02 15:09:54 +08:00
}
return true;
2024-02-02 15:09:54 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitCameraOptions(FFCameraOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰄀");
}
2026-03-29 09:28:49 +08:00
void ffDestroyCameraOptions(FFCameraOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffCameraModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_CAMERA_MODULE_NAME,
.description = "Print available cameras",
.initOptions = (void*) ffInitCameraOptions,
.destroyOptions = (void*) ffDestroyCameraOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseCameraJsonObject,
.printModule = (void*) ffPrintCamera,
.generateJsonResult = (void*) ffGenerateCameraJsonResult,
.generateJsonConfig = (void*) ffGenerateCameraJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Device name", "name" },
{ "Vendor", "vendor" },
{ "Color space", "colorspace" },
{ "Identifier", "id" },
{ "Width (in px)", "width" },
{ "Height (in px)", "height" },
}))
};