Files
fastfetch/src/modules/gpu.c
T

123 lines
3.6 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include <pci/pci.h>
2021-02-28 17:38:26 +01:00
#include <dlfcn.h>
2021-02-18 22:25:36 +01:00
2021-02-28 17:38:26 +01:00
static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_dev* dev, uint16_t counter, char*(*ffpci_lookup_name)(struct pci_access*, char*, int, int, ...))
2021-02-18 22:25:36 +01:00
{
char key[8];
sprintf(key, "GPU%hu", counter);
2021-02-22 16:52:51 +01:00
2021-02-27 18:30:05 +01:00
if(ffPrintCachedValue(instance, key))
2021-02-22 16:52:51 +01:00
return;
char vendor[512];
2021-02-28 17:38:26 +01:00
ffpci_lookup_name(pacc, vendor, sizeof(vendor), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id);
2021-02-18 22:25:36 +01:00
2021-03-19 20:57:07 +01:00
char vendorPretty[512];
if(strcasecmp(vendor, "Advanced Micro Devices, Inc. [AMD/ATI]") == 0)
strcpy(vendorPretty, "AMD ATI");
else
strcpy(vendorPretty, vendor);
2021-02-22 16:52:51 +01:00
char name[512];
2021-02-28 17:38:26 +01:00
ffpci_lookup_name(pacc, name, sizeof(name), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
2021-02-18 22:25:36 +01:00
2021-03-21 20:46:20 +01:00
FF_STRBUF_CREATE(gpu);
2021-03-19 20:57:07 +01:00
if(ffStrbufIsEmpty(&instance->config.gpuFormat))
2021-02-22 16:52:51 +01:00
{
2021-03-19 20:57:07 +01:00
ffStrbufSetF(&gpu, "%s %s", vendorPretty, name);
2021-02-22 16:52:51 +01:00
}
2021-02-18 22:25:36 +01:00
else
2021-02-22 16:52:51 +01:00
{
2021-03-19 20:57:07 +01:00
ffParseFormatString(&gpu, &instance->config.gpuFormat, 2,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, vendorPretty},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, name}
);
2021-02-22 16:52:51 +01:00
}
ffPrintAndSaveCachedValue(instance, key, &gpu);
2021-03-19 20:57:07 +01:00
ffStrbufDestroy(&gpu);
2021-02-18 22:25:36 +01:00
}
2021-02-27 18:30:05 +01:00
void ffPrintGPU(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-03-19 20:57:07 +01:00
void* pci;
if(ffStrbufIsEmpty(&instance->config.libPCI))
pci = dlopen("libpci.so", RTLD_LAZY);
else
pci = dlopen(instance->config.libPCI.chars, RTLD_LAZY);
2021-02-28 17:38:26 +01:00
if(pci == NULL)
{
ffPrintError(instance, "GPU", "dlopen(\"libpci.so\", RTLD_LAZY) == NULL");
return;
}
struct pci_access*(*ffpci_alloc)() = dlsym(pci, "pci_alloc");
if(ffpci_alloc == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_alloc\") == NULL");
return;
}
void(*ffpci_init)(struct pci_access*) = dlsym(pci, "pci_init");
if(ffpci_init == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_init\") == NULL");
return;
}
void(*ffpci_scan_bus)(struct pci_access*) = dlsym(pci, "pci_scan_bus");
if(ffpci_scan_bus == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_init\") == NULL");
return;
}
int(*ffpci_fill_info)(struct pci_dev*, int) = dlsym(pci, "pci_fill_info");
if(ffpci_fill_info == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_fill_info\") == NULL");
return;
}
char*(*ffpci_lookup_name)(struct pci_access*, char*, int, int, ...) = dlsym(pci, "pci_lookup_name");
if(ffpci_lookup_name == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_lookup_name\") == NULL");
return;
}
void(*ffpci_cleanup)(struct pci_access*) = dlsym(pci, "pci_cleanup");
if(ffpci_cleanup == NULL)
{
ffPrintError(instance, "GPU", "dlsym(pci, \"pci_cleanup\") == NULL");
return;
}
2021-02-18 22:25:36 +01:00
uint16_t counter = 0;
struct pci_access *pacc;
struct pci_dev *dev;
2021-02-28 17:38:26 +01:00
pacc = ffpci_alloc();
ffpci_init(pacc);
ffpci_scan_bus(pacc);
2021-02-18 22:25:36 +01:00
for (dev=pacc->devices; dev; dev=dev->next)
{
2021-02-28 17:38:26 +01:00
ffpci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
2021-02-18 22:25:36 +01:00
char class[1024];
2021-02-28 17:38:26 +01:00
ffpci_lookup_name(pacc, class, sizeof(class), PCI_LOOKUP_CLASS, dev->device_class);
2021-02-18 22:25:36 +01:00
if(
2021-02-27 21:10:27 +01:00
strcasecmp("VGA compatible controller", class) == 0 ||
strcasecmp("3D controller", class) == 0 ||
strcasecmp("Display controller", class) == 0 )
2021-02-18 22:25:36 +01:00
{
2021-02-28 17:38:26 +01:00
handleGPU(instance, pacc, dev, counter++, ffpci_lookup_name);
2021-02-18 22:25:36 +01:00
}
}
2021-02-28 17:38:26 +01:00
ffpci_cleanup(pacc);
dlclose(pci);
2021-02-18 22:25:36 +01:00
}