loading pci libs dynamically

This commit is contained in:
Linus Dierheimer
2021-02-28 17:38:26 +01:00
parent 0617b597f9
commit 7248daaa48
2 changed files with 60 additions and 13 deletions
-2
View File
@@ -60,7 +60,6 @@ add_executable(fastfetch
target_link_libraries(fastfetch
${CMAKE_DL_LIBS}
pci
)
add_executable(flashfetch
@@ -74,5 +73,4 @@ target_compile_definitions(flashfetch PUBLIC
target_link_libraries(flashfetch
${CMAKE_DL_LIBS}
pci
)
+60 -11
View File
@@ -1,9 +1,9 @@
#include "fastfetch.h"
#include <pci/pci.h>
#include <dlfcn.h>
static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_dev* dev, uint16_t counter)
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, ...))
{
char key[8];
sprintf(key, "GPU%hu", counter);
@@ -14,10 +14,10 @@ static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_
char gpu[1024];
char vendor[512];
pci_lookup_name(pacc, vendor, sizeof(vendor), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id);
ffpci_lookup_name(pacc, vendor, sizeof(vendor), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id);
char name[512];
pci_lookup_name(pacc, name, sizeof(name), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
ffpci_lookup_name(pacc, name, sizeof(name), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
if(strcasecmp(vendor, "Advanced Micro Devices, Inc. [AMD/ATI]") == 0)
{
@@ -36,26 +36,75 @@ static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_
void ffPrintGPU(FFinstance* instance)
{
void* pci = dlopen("libpci.so", RTLD_LAZY);
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;
}
uint16_t counter = 0;
struct pci_access *pacc;
struct pci_dev *dev;
pacc = pci_alloc();
pci_init(pacc);
pci_scan_bus(pacc);
pacc = ffpci_alloc();
ffpci_init(pacc);
ffpci_scan_bus(pacc);
for (dev=pacc->devices; dev; dev=dev->next)
{
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
ffpci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
char class[1024];
pci_lookup_name(pacc, class, sizeof(class), PCI_LOOKUP_CLASS, dev->device_class);
ffpci_lookup_name(pacc, class, sizeof(class), PCI_LOOKUP_CLASS, dev->device_class);
if(
strcasecmp("VGA compatible controller", class) == 0 ||
strcasecmp("3D controller", class) == 0 ||
strcasecmp("Display controller", class) == 0 )
{
handleGPU(instance, pacc, dev, counter++);
handleGPU(instance, pacc, dev, counter++, ffpci_lookup_name);
}
}
pci_cleanup(pacc);
ffpci_cleanup(pacc);
}