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

131 lines
3.8 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"
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.key, &options->moduleArgs.keyColor);
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.name.length > 0)
ffStrbufWriteTo(&cpu.name, stdout);
else if(cpu.vendor.length > 0)
{
ffStrbufWriteTo(&cpu.vendor, stdout);
fputs(" CPU", stdout);
}
else
fputs("CPU", stdout);
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.coresOnline > 1)
printf(" (%u)", cpu.coresOnline);
2023-03-15 18:17:24 +08:00
2023-06-12 23:00:37 +08:00
if(cpu.frequencyMax > 0.0)
printf(" @ %.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
printf(" - %.1f°C", cpu.temperature);
putchar('\n');
}
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)
{
options->moduleName = FF_CPU_MODULE_NAME;
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);
}
void ffParseCPUJsonObject(yyjson_val* module)
2023-03-15 18:17:24 +08:00
{
FFCPUOptions __attribute__((__cleanup__(ffDestroyCPUOptions))) options;
ffInitCPUOptions(&options);
if (module)
{
2023-06-10 15:01:54 +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-06-10 15:01:54 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
2023-06-10 15:01:54 +08:00
continue;
2023-03-15 18:17:24 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
if (ffStrEqualsIgnCase(key, "temp"))
2023-03-15 18:17:24 +08:00
{
2023-06-10 15:01:54 +08:00
options.temp = yyjson_get_bool(val);
2023-03-15 18:17:24 +08:00
continue;
}
ffPrintError(FF_CPU_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
2023-03-15 18:17:24 +08:00
}
}
ffPrintCPU(&options);
2023-03-15 18:17:24 +08:00
}