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

171 lines
5.5 KiB
C
Raw Normal View History

2023-03-15 18:17:24 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
#include "common/parsing.h"
2023-03-15 18:17:24 +08:00
#include "detection/cpu/cpu.h"
#include "modules/cpu/cpu.h"
#include "util/stringUtils.h"
2023-03-15 18:17:24 +08:00
#define FF_CPU_NUM_FORMAT_ARGS 8
void ffPrintCPU(FFCPUOptions* options)
2023-03-15 18:17:24 +08:00
{
2023-06-12 09:56:03 +08:00
FFCPUResult cpu;
cpu.temperature = FF_CPU_TEMP_UNSET;
cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0;
cpu.frequencyMax = cpu.frequencyMin = 0;
ffStrbufInit(&cpu.name);
ffStrbufInit(&cpu.vendor);
2023-03-15 18:17:24 +08:00
const char* error = ffDetectCPU(options, &cpu);
2023-06-12 09:56:03 +08:00
2023-06-17 15:23:05 +08:00
if(error)
{
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2023-06-17 15:23:05 +08:00
}
else if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1)
2023-03-15 18:17:24 +08:00
{
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "No CPU detected");
2023-03-15 18:17:24 +08:00
}
2023-06-12 23:00:37 +08:00
else
2023-03-15 18:17:24 +08:00
{
2023-06-12 23:00:37 +08:00
if(options->moduleArgs.outputFormat.length == 0)
2023-03-15 18:17:24 +08:00
{
ffPrintLogoAndKey(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-15 18:17:24 +08:00
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
2023-06-12 23:00:37 +08:00
if(cpu.name.length > 0)
ffStrbufAppend(&str, &cpu.name);
2023-06-12 23:00:37 +08:00
else if(cpu.vendor.length > 0)
{
ffStrbufAppend(&str, &cpu.vendor);
ffStrbufAppendS(&str, " CPU");
2023-06-12 23:00:37 +08:00
}
else
ffStrbufAppendS(&str, "Unknown");
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.coresOnline > 1)
ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.frequencyMax > 0.0)
ffStrbufAppendF(&str, " @ %.9g GHz", cpu.frequencyMax);
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET
{
ffStrbufAppendS(&str, " - ");
ffParseTemperature(cpu.temperature, &str);
}
2023-06-12 23:00:37 +08:00
ffStrbufPutTo(&str, stdout);
2023-06-12 23:00:37 +08:00
}
else
{
ffPrintFormat(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-12 23:00:37 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu.name},
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu.vendor},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresPhysical},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresLogical},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresOnline},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMin},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMax},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.temperature}
});
}
2023-03-15 18:17:24 +08:00
}
2023-06-12 23:00:37 +08:00
ffStrbufDestroy(&cpu.name);
ffStrbufDestroy(&cpu.vendor);
2023-03-15 18:17:24 +08:00
}
void ffInitCPUOptions(FFCPUOptions* options)
{
2023-08-30 14:46:04 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPU_MODULE_NAME, ffParseCPUCommandOptions, ffParseCPUJsonObject, ffPrintCPU, ffGenerateCPUJson);
2023-03-15 18:17:24 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
options->temp = false;
}
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CPU_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if (ffStrEqualsIgnCase(subKey, "temp"))
2023-03-15 18:17:24 +08:00
{
options->temp = ffOptionParseBoolean(value);
return true;
}
return false;
}
void ffDestroyCPUOptions(FFCPUOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-08-17 10:56:54 +08:00
void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module)
2023-03-15 18:17:24 +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-15 18:17:24 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-03-15 18:17:24 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-15 18:17:24 +08:00
2023-08-17 16:09:41 +08:00
if (ffStrEqualsIgnCase(key, "temp"))
{
options->temp = yyjson_get_bool(val);
continue;
2023-03-15 18:17:24 +08:00
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-15 18:17:24 +08:00
}
}
2023-08-30 14:46:04 +08:00
void ffGenerateCPUJson(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FFCPUResult cpu;
cpu.temperature = FF_CPU_TEMP_UNSET;
cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0;
cpu.frequencyMax = cpu.frequencyMin = 0;
ffStrbufInit(&cpu.name);
ffStrbufInit(&cpu.vendor);
const char* error = ffDetectCPU(options, &cpu);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
}
else if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1)
{
yyjson_mut_obj_add_str(doc, module, "error", "No CPU detected");
}
else
{
2023-08-31 13:54:46 +08:00
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
2023-08-30 14:46:04 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name);
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor);
2023-09-19 10:50:52 +08:00
yyjson_mut_val* cores = yyjson_mut_obj_add_obj(doc, obj, "cores");
yyjson_mut_obj_add_uint(doc, cores, "physical", cpu.coresPhysical);
yyjson_mut_obj_add_uint(doc, cores, "logical", cpu.coresLogical);
yyjson_mut_obj_add_uint(doc, cores, "online", cpu.coresOnline);
yyjson_mut_val* frequency = yyjson_mut_obj_add_obj(doc, obj, "frequency");
yyjson_mut_obj_add_real(doc, frequency, "min", cpu.frequencyMin);
yyjson_mut_obj_add_real(doc, frequency, "max", cpu.frequencyMax);
2023-09-18 20:12:33 +08:00
yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature);
2023-08-30 14:46:04 +08:00
}
2023-09-08 20:03:26 +08:00
ffStrbufDestroy(&cpu.name);
ffStrbufDestroy(&cpu.vendor);
2023-08-30 14:46:04 +08:00
}