mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-08 22:20:17 +01:00
Global: fixes a lot bugs found by AI
This commit is contained in:
@@ -7,31 +7,50 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static int sortByNameAsc(FFDisplayResult* a, FFDisplayResult* b) {
|
||||
return ffStrbufComp(&a->name, &b->name);
|
||||
static int sortByNameAsc(const void* a, const void* b) {
|
||||
return ffStrbufComp(&((const FFDisplayResult*) a)->name, &((const FFDisplayResult*) b)->name);
|
||||
}
|
||||
|
||||
static int sortByNameDesc(FFDisplayResult* a, FFDisplayResult* b) {
|
||||
return -ffStrbufComp(&a->name, &b->name);
|
||||
static int sortByNameDesc(const void* a, const void* b) {
|
||||
return -ffStrbufComp(&((const FFDisplayResult*) a)->name, &((const FFDisplayResult*) b)->name);
|
||||
}
|
||||
|
||||
bool ffPrintDisplay(FFDisplayOptions* options) {
|
||||
const FFDisplayServerResult* dsResult = ffConnectDisplayServer();
|
||||
|
||||
if (dsResult->displays.length == 0) {
|
||||
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Display), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect display");
|
||||
return false;
|
||||
// `ffConnectDisplayServer()` hands out a process-wide result that other modules read too: `Monitor`
|
||||
// prints the same list, and `Brightness` pairs its own readings with these entries by position. It
|
||||
// must therefore not be reordered in place, which is what this module used to do through a cast
|
||||
// that dropped `const`. Sort a copy instead, so both the text and the JSON path get the requested
|
||||
// order without any other consumer seeing it.
|
||||
//
|
||||
// The copy is shallow: the entries keep owning their `FFstrbuf`s through the original result, and
|
||||
// it is never destroyed element-wise, so only the array itself is freed.
|
||||
static FFlist getOrderedDisplays(const FFDisplayOptions* options) {
|
||||
const FFlist* displays = &ffConnectDisplayServer()->displays;
|
||||
FFlist result = ffListCreateA(sizeof(FFDisplayResult), displays->length);
|
||||
if (displays->length > 0) {
|
||||
memcpy(result.data, displays->data, (size_t) displays->length * sizeof(FFDisplayResult));
|
||||
result.length = displays->length;
|
||||
}
|
||||
|
||||
if (options->order != FF_DISPLAY_ORDER_NONE) {
|
||||
ffListSort((FFlist*) &dsResult->displays, sizeof(FFDisplayResult), (void*) (options->order == FF_DISPLAY_ORDER_ASC ? sortByNameAsc : sortByNameDesc));
|
||||
ffListSort(&result, sizeof(FFDisplayResult), options->order == FF_DISPLAY_ORDER_ASC ? sortByNameAsc : sortByNameDesc);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ffPrintDisplay(FFDisplayOptions* options) {
|
||||
FF_LIST_AUTO_DESTROY displays = getOrderedDisplays(options);
|
||||
|
||||
if (displays.length == 0) {
|
||||
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Display), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect display");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options->compactType != FF_DISPLAY_COMPACT_TYPE_NONE) {
|
||||
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Display), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
||||
FF_LIST_FOR_EACH (FFDisplayResult, result, dsResult->displays) {
|
||||
FF_LIST_FOR_EACH (FFDisplayResult, result, displays) {
|
||||
if (options->compactType & FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT) {
|
||||
ffStrbufAppendF(&buffer, "%ix%i", result->width, result->height);
|
||||
} else {
|
||||
@@ -42,7 +61,7 @@ bool ffPrintDisplay(FFDisplayOptions* options) {
|
||||
|
||||
if (options->compactType & FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT) {
|
||||
if (result->refreshRate > 0) {
|
||||
const char* space = instance.config.display.freqSpaceBeforeUnit == FF_SPACE_BEFORE_UNIT_ALWAYS ? " " : "";
|
||||
const char* space = instance.config.display.freqSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER ? " " : "";
|
||||
if (options->preciseRefreshRate) {
|
||||
ffStrbufAppendF(&buffer, " @ %g%sHz", result->refreshRate, space);
|
||||
} else {
|
||||
@@ -62,9 +81,9 @@ bool ffPrintDisplay(FFDisplayOptions* options) {
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
||||
|
||||
for (uint32_t i = 0; i < dsResult->displays.length; i++) {
|
||||
FFDisplayResult* result = FF_LIST_GET(FFDisplayResult, dsResult->displays, i);
|
||||
uint32_t moduleIndex = dsResult->displays.length == 1 ? 0 : i + 1;
|
||||
for (uint32_t i = 0; i < displays.length; i++) {
|
||||
FFDisplayResult* result = FF_LIST_GET(FFDisplayResult, displays, i);
|
||||
uint32_t moduleIndex = displays.length == 1 ? 0 : i + 1;
|
||||
const char* displayType = result->type == FF_DISPLAY_TYPE_UNKNOWN ? nullptr : result->type == FF_DISPLAY_TYPE_BUILTIN ? "Built-in"
|
||||
: "External";
|
||||
|
||||
@@ -295,16 +314,16 @@ void ffGenerateDisplayJsonConfig(FFDisplayOptions* options, yyjson_mut_doc* doc,
|
||||
}
|
||||
}
|
||||
|
||||
bool ffGenerateDisplayJsonResult([[maybe_unused]] FFDisplayOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
||||
const FFDisplayServerResult* dsResult = ffConnectDisplayServer();
|
||||
bool ffGenerateDisplayJsonResult(FFDisplayOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
||||
FF_LIST_AUTO_DESTROY displays = getOrderedDisplays(options);
|
||||
|
||||
if (dsResult->displays.length == 0) {
|
||||
if (displays.length == 0) {
|
||||
yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect display");
|
||||
return false;
|
||||
}
|
||||
|
||||
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
||||
FF_LIST_FOR_EACH (FFDisplayResult, item, dsResult->displays) {
|
||||
FF_LIST_FOR_EACH (FFDisplayResult, item, displays) {
|
||||
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
||||
yyjson_mut_obj_add_uint(doc, obj, "id", item->id);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
|
||||
|
||||
Reference in New Issue
Block a user