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

123 lines
3.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"
2023-03-15 18:17:24 +08:00
#include "detection/cpu/cpu.h"
#include "modules/cpu/cpu.h"
#define FF_CPU_NUM_FORMAT_ARGS 8
void ffPrintCPU(FFinstance* instance, FFCPUOptions* options)
{
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
2023-06-12 09:56:03 +08:00
ffDetectCPU(instance, options, &cpu);
if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1)
2023-03-15 18:17:24 +08:00
{
ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "No CPU detected");
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs.key);
2023-06-12 09:56:03 +08:00
if(cpu.name.length > 0)
ffStrbufWriteTo(&cpu.name, stdout);
else if(cpu.vendor.length > 0)
2023-03-15 18:17:24 +08:00
{
2023-06-12 09:56:03 +08:00
ffStrbufWriteTo(&cpu.vendor, stdout);
2023-03-15 18:17:24 +08:00
fputs(" CPU", stdout);
}
else
fputs("CPU", stdout);
2023-06-12 09:56:03 +08:00
if(cpu.coresOnline > 1)
printf(" (%u)", cpu.coresOnline);
2023-03-15 18:17:24 +08:00
2023-06-12 09:56:03 +08:00
if(cpu.frequencyMax > 0.0)
printf(" @ %.9g GHz", cpu.frequencyMax);
2023-03-15 18:17:24 +08:00
2023-06-12 09:56:03 +08:00
if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET
printf(" - %.1f°C", cpu.temperature);
2023-03-15 18:17:24 +08:00
putchar('\n');
}
else
{
ffPrintFormat(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-12 09:56:03 +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
});
}
}
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 (strcasecmp(subKey, "temp") == 0)
{
options->temp = ffOptionParseBoolean(value);
return true;
}
return false;
}
void ffDestroyCPUOptions(FFCPUOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-06-10 15:01:54 +08:00
void ffParseCPUJsonObject(FFinstance* instance, 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(strcasecmp(key, "type") == 0)
continue;
2023-03-15 18:17:24 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
if (strcasecmp(key, "temp") == 0)
{
2023-06-10 15:01:54 +08:00
options.temp = yyjson_get_bool(val);
2023-03-15 18:17:24 +08:00
continue;
}
ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintCPU(instance, &options);
}