Files
fastfetch/src/modules/gpu.c
T

105 lines
3.5 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/bar.h"
#include "common/parsing.h"
#include "common/printing.h"
2022-09-04 12:43:30 +02:00
#include "detection/host/host.h"
2022-09-05 17:42:42 +02:00
#include "detection/gpu/gpu.h"
2021-02-18 22:25:36 +01:00
2022-07-17 16:11:08 +02:00
#include <stdlib.h>
2021-04-15 18:59:26 +02:00
#define FF_GPU_MODULE_NAME "GPU"
2023-01-12 11:41:07 +01:00
#define FF_GPU_NUM_FORMAT_ARGS 6
2021-04-03 21:51:50 +02:00
2023-01-12 11:41:07 +01:00
static void printGPUResult(FFinstance* instance, uint8_t index, const FFGPUResult* gpu)
2022-09-01 17:14:50 +02:00
{
2022-12-17 11:39:10 +08:00
if(instance->config.gpu.outputFormat.length == 0)
2022-06-17 23:38:33 +02:00
{
2023-01-12 11:41:07 +01:00
ffPrintLogoAndKey(instance, FF_GPU_MODULE_NAME, index, &instance->config.gpu.key);
2022-12-17 11:39:10 +08:00
FF_STRBUF_AUTO_DESTROY output;
2022-12-17 11:39:10 +08:00
ffStrbufInitA(&output, gpu->vendor.length + 1 + gpu->name.length);
2022-09-01 17:14:50 +02:00
2022-12-17 11:39:10 +08:00
if(gpu->vendor.length > 0 && !ffStrbufStartsWith(&gpu->name, &gpu->vendor))
{
ffStrbufAppend(&output, &gpu->vendor);
ffStrbufAppendC(&output, ' ');
}
2022-09-01 17:14:50 +02:00
2022-12-17 11:39:10 +08:00
ffStrbufAppend(&output, &gpu->name);
2022-09-16 18:11:39 +08:00
2022-12-17 11:39:10 +08:00
if(gpu->coreCount != FF_GPU_CORE_COUNT_UNSET)
ffStrbufAppendF(&output, " (%d)", gpu->coreCount);
2022-09-25 15:23:00 +08:00
2022-12-17 11:39:10 +08:00
if(gpu->temperature == gpu->temperature) //FF_GPU_TEMP_UNSET
ffStrbufAppendF(&output, " - %.1f°C", gpu->temperature);
2022-06-01 00:04:58 +02:00
if(gpu->dedicatedTotal != FF_GPU_VMEM_SIZE_UNSET)
{
ffStrbufAppendS(&output, " (");
2022-12-17 11:39:10 +08:00
if(gpu->dedicatedUsed != FF_GPU_VMEM_SIZE_UNSET)
{
ffParseSize(gpu->dedicatedUsed, instance->config.binaryPrefixType, &output);
ffStrbufAppendS(&output, " / ");
}
ffParseSize(gpu->dedicatedTotal, instance->config.binaryPrefixType, &output);
if(gpu->dedicatedUsed != FF_GPU_VMEM_SIZE_UNSET)
{
ffStrbufAppendS(&output, ", ");
ffAppendPercentNum(instance, &output, (uint8_t) (gpu->dedicatedUsed * 100 / gpu->dedicatedTotal), 50, 80, false);
}
ffStrbufAppendC(&output, ')');
}
ffStrbufPutTo(&output, stdout);
2022-12-17 11:39:10 +08:00
}
else
{
2023-01-13 15:46:29 +08:00
const char* type;
2023-01-12 11:41:07 +01:00
if(gpu->type == FF_GPU_TYPE_INTEGRATED)
2023-01-13 15:46:29 +08:00
type = "Integrated";
2023-01-12 11:41:07 +01:00
else if(gpu->type == FF_GPU_TYPE_DISCRETE)
2023-01-13 15:46:29 +08:00
type = "Discrete";
2023-01-12 11:41:07 +01:00
else
2023-01-13 15:46:29 +08:00
type = "Unknown";
2023-01-12 11:41:07 +01:00
2022-12-17 11:39:10 +08:00
ffPrintFormat(instance, FF_GPU_MODULE_NAME, index, &instance->config.gpu, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver},
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature},
{FF_FORMAT_ARG_TYPE_INT, &gpu->coreCount},
2023-01-13 15:46:29 +08:00
{FF_FORMAT_ARG_TYPE_STRING, type},
2022-12-17 11:39:10 +08:00
});
}
2021-02-18 22:25:36 +01:00
}
2021-11-13 18:25:35 +01:00
void ffPrintGPU(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2022-09-05 17:42:42 +02:00
const FFlist* gpus = ffDetectGPU(instance);
2021-04-03 21:51:50 +02:00
2023-01-12 11:41:07 +01:00
FFlist selectedGpus;
ffListInitA(&selectedGpus, sizeof(const FFGPUResult*), gpus->length);
for(uint32_t i = 0; i < gpus->length; i++)
2022-09-05 17:42:42 +02:00
{
2023-01-12 11:41:07 +01:00
const FFGPUResult* gpu = ffListGet(gpus, i);
if(gpu->type == FF_GPU_TYPE_INTEGRATED && instance->config.gpuHideIntegrated)
continue;
if(gpu->type == FF_GPU_TYPE_DISCRETE && instance->config.gpuHideDiscrete)
continue;
* (const FFGPUResult**) ffListAdd(&selectedGpus) = gpu;
2022-09-05 17:42:42 +02:00
}
2023-01-12 11:41:07 +01:00
for(uint32_t i = 0; i < selectedGpus.length; i++)
printGPUResult(instance, selectedGpus.length == 1 ? 0 : (uint8_t) (i + 1), * (const FFGPUResult**) ffListGet(&selectedGpus, i));
if(selectedGpus.length == 0)
ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &instance->config.gpu, "No GPUs found");
ffListDestroy(&selectedGpus);
2021-11-13 18:25:35 +01:00
}