Host: don't use singleton

This commit is contained in:
李通洲
2023-06-12 22:53:12 +08:00
committed by 李通洲
parent 3bf83e45ed
commit 429d7c1ed9
9 changed files with 45 additions and 52 deletions
-1
View File
@@ -254,7 +254,6 @@ set(LIBFASTFETCH_SRC
src/detection/displayserver/displayserver.c
src/detection/font/font.c
src/detection/gpu/gpu.c
src/detection/host/host.c
src/detection/locale/locale.c
src/detection/media/media.c
src/detection/opencl/opencl.c
-11
View File
@@ -1,11 +0,0 @@
#include "host.h"
#include "detection/internal.h"
void ffDetectHostImpl(FFHostResult* host);
const FFHostResult* ffDetectHost()
{
FF_DETECTION_INTERNAL_GUARD(FFHostResult,
ffDetectHostImpl(&result)
);
}
+1 -2
View File
@@ -12,9 +12,8 @@ typedef struct FFHostResult
FFstrbuf productVersion;
FFstrbuf productSku;
FFstrbuf sysVendor;
FFstrbuf error;
} FFHostResult;
const FFHostResult* ffDetectHost();
const char* ffDetectHost(FFHostResult* result);
#endif
+5 -4
View File
@@ -2,10 +2,8 @@
#include "common/settings.h"
#include <ctype.h>
void ffDetectHostImpl(FFHostResult* host)
const char* ffDetectHost(FFHostResult* host)
{
ffStrbufInit(&host->error);
//Family
ffStrbufInit(&host->productFamily);
@@ -16,7 +14,8 @@ void ffDetectHostImpl(FFHostResult* host)
ffStrbufInit(&host->productName);
ffSettingsGetAndroidProperty("ro.product.brand", &host->productName);
if(host->productName.length > 0){
if(host->productName.length > 0)
{
host->productName.chars[0] = (char) toupper(host->productName.chars[0]);
ffStrbufAppendC(&host->productName, ' ');
}
@@ -34,4 +33,6 @@ void ffDetectHostImpl(FFHostResult* host)
ffStrbufInit(&host->productVersion);
ffStrbufInit(&host->productSku);
return NULL;
}
+5 -6
View File
@@ -155,17 +155,16 @@ static const char* getProductName(const FFstrbuf* hwModel)
return hwModel->chars;
}
void ffDetectHostImpl(FFHostResult* host)
const char* ffDetectHost(FFHostResult* host)
{
ffStrbufInit(&host->error);
ffStrbufInit(&host->productName);
ffStrbufInit(&host->productFamily);
ffStrbufInit(&host->productVersion);
ffStrbufInit(&host->productSku);
ffStrbufInitS(&host->sysVendor, "Apple");
ffStrbufAppendS(&host->error, ffSysctlGetString("hw.model", &host->productFamily));
if(host->error.length == 0)
ffStrbufAppendS(&host->productName, getProductName(&host->productFamily));
const char* error = ffSysctlGetString("hw.model", &host->productFamily);
if (error) return error;
ffStrbufAppendS(&host->productName, getProductName(&host->productFamily));
return NULL;
}
+2 -4
View File
@@ -1,15 +1,13 @@
#include "host.h"
#include "common/sysctl.h"
void ffDetectHostImpl(FFHostResult* host)
const char* ffDetectHost(FFHostResult* host)
{
ffStrbufInit(&host->error);
ffStrbufInit(&host->productName);
ffStrbufInit(&host->productFamily);
ffStrbufInit(&host->productVersion);
ffStrbufInit(&host->productSku);
ffStrbufInit(&host->sysVendor);
ffStrbufAppendS(&host->error, ffSysctlGetString("hw.fdt.model", &host->productName));
return ffSysctlGetString("hw.fdt.model", &host->productName);
}
+3 -3
View File
@@ -59,10 +59,8 @@ static void getHostProductName(FFstrbuf* name)
ffStrbufClear(name);
}
void ffDetectHostImpl(FFHostResult* host)
const char* ffDetectHost(FFHostResult* host)
{
ffStrbufInit(&host->error);
ffStrbufInit(&host->productFamily);
getHostValue("/sys/devices/virtual/dmi/id/product_family", "/sys/class/dmi/id/product_family", &host->productFamily);
@@ -107,4 +105,6 @@ void ffDetectHostImpl(FFHostResult* host)
}
}
}
return NULL;
}
+5 -5
View File
@@ -1,10 +1,8 @@
#include "host.h"
#include "util/windows/registry.h"
void ffDetectHostImpl(FFHostResult* host)
const char* ffDetectHost(FFHostResult* host)
{
ffStrbufInit(&host->error);
ffStrbufInit(&host->productName);
ffStrbufInit(&host->productFamily);
ffStrbufInit(&host->productVersion);
@@ -13,12 +11,14 @@ void ffDetectHostImpl(FFHostResult* host)
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, &host->error))
return;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, NULL))
return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\BIOS\", &hKey, NULL) failed";
ffRegReadStrbuf(hKey, L"SystemProductName", &host->productName, NULL);
ffRegReadStrbuf(hKey, L"SystemFamily", &host->productFamily, NULL);
ffRegReadStrbuf(hKey, L"SystemVersion", &host->productVersion, NULL);
ffRegReadStrbuf(hKey, L"SystemSKU", &host->productSku, NULL);
ffRegReadStrbuf(hKey, L"SystemManufacturer", &host->sysVendor, NULL);
return NULL;
}
+24 -16
View File
@@ -7,18 +7,19 @@
void ffPrintHost(FFinstance* instance, FFHostOptions* options)
{
const FFHostResult* host = ffDetectHost();
FFHostResult host;
const char* error = ffDetectHost(&host);
if(host->error.length > 0)
if(error)
{
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "%*s", host->error.length, host->error.chars);
return;
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
goto exit;
}
if(host->productFamily.length == 0 && host->productName.length == 0)
if(host.productFamily.length == 0 && host.productName.length == 0)
{
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "neither product_family nor product_name is set by O.E.M.");
return;
goto exit;
}
if(options->moduleArgs.outputFormat.length == 0)
@@ -27,14 +28,14 @@ void ffPrintHost(FFinstance* instance, FFHostOptions* options)
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreate();
if(host->productName.length > 0)
ffStrbufAppend(&output, &host->productName);
if(host.productName.length > 0)
ffStrbufAppend(&output, &host.productName);
else
ffStrbufAppend(&output, &host->productFamily);
ffStrbufAppend(&output, &host.productFamily);
if(host->productVersion.length > 0 && !ffStrbufIgnCaseEqualS(&host->productVersion, "none"))
if(host.productVersion.length > 0 && !ffStrbufIgnCaseEqualS(&host.productVersion, "none"))
{
ffStrbufAppendF(&output, " (%s)", host->productVersion.chars);
ffStrbufAppendF(&output, " (%s)", host.productVersion.chars);
}
ffStrbufPutTo(&output, stdout);
@@ -42,13 +43,20 @@ void ffPrintHost(FFinstance* instance, FFHostOptions* options)
else
{
ffPrintFormat(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_HOST_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productFamily},
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productName},
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productVersion},
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productSku},
{FF_FORMAT_ARG_TYPE_STRBUF, &host->sysVendor}
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productFamily},
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productName},
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productVersion},
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productSku},
{FF_FORMAT_ARG_TYPE_STRBUF, &host.sysVendor}
});
}
exit:
ffStrbufDestroy(&host.productFamily);
ffStrbufDestroy(&host.productName);
ffStrbufDestroy(&host.productVersion);
ffStrbufDestroy(&host.productSku);
ffStrbufDestroy(&host.sysVendor);
}
void ffInitHostOptions(FFHostOptions* options)