11#include "gpu.h"
2+ #include "common/io/io.h"
23
4+ #include <stdlib.h>
35#ifdef __FreeBSD__
46#include <paths.h>
57#endif
68
9+ #if FF_HAVE_EMBEDDED_PCIIDS
10+ #include "fastfetch_pciids.c.inc"
11+ #endif
12+
713static const FFstrbuf * loadPciIds ()
814{
915 static FFstrbuf pciids ;
1016
1117 if (pciids .chars ) return & pciids ;
12- ffStrbufinit (& pciids );
18+ ffStrbufInit (& pciids );
1319
1420 #ifdef FF_CUSTOM_PCI_IDS_PATH
1521
@@ -18,16 +24,18 @@ static const FFstrbuf* loadPciIds()
1824 #else // FF_CUSTOM_PCI_IDS_PATH
1925
2026 #if __linux__
21- ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/hwdata/pci.ids" , pciids );
27+ ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/hwdata/pci.ids" , & pciids );
2228 if (pciids .length == 0 )
23- ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/misc/pci.ids" , pciids ); // debian?
24- if (pciids .length == 0 )
25- ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/local/share/hwdata/pci.ids" , pciids );
29+ {
30+ ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/misc/pci.ids" , & pciids ); // debian?
31+ if (pciids .length == 0 )
32+ ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/local/share/hwdata/pci.ids" , & pciids );
33+ }
2634 #elif __FreeBSD__
2735 // https://github.com/freebsd/freebsd-src/blob/main/usr.sbin/pciconf/pathnames.h
28- ffReadFileBuffer (_PATH_LOCALBASE "/share/pciids/pci.ids" , pciids );
36+ ffReadFileBuffer (_PATH_LOCALBASE "/share/pciids/pci.ids" , & pciids );
2937 if (pciids .length == 0 )
30- ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids" , pciids );
38+ ffReadFileBuffer (FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids" , & pciids );
3139 #elif __sun
3240 ffReadFileBuffer (FASTFETCH_TARGET_DIR_ROOT "/usr/share/hwdata/pci.ids" , & pciids );
3341 #endif
@@ -37,9 +45,8 @@ static const FFstrbuf* loadPciIds()
3745 return & pciids ;
3846}
3947
40- void ffGPUParsePciIds ( uint8_t subclass , uint16_t vendor , uint16_t device , FFGPUResult * gpu )
48+ static void parsePciIdsFile ( const FFstrbuf * content , uint8_t subclass , uint16_t vendor , uint16_t device , FFGPUResult * gpu )
4149{
42- const FFstrbuf * content = loadPciIds ();
4350 if (content -> length )
4451 {
4552 char buffer [32 ];
@@ -112,3 +119,66 @@ void ffGPUParsePciIds(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUR
112119 ffStrbufSetF (& gpu -> name , "%s Device %04X%s" , gpu -> vendor .length ? gpu -> vendor .chars : "Unknown" , device , subclassStr );
113120 }
114121}
122+
123+ #if FF_HAVE_EMBEDDED_PCIIDS
124+ static inline int pciDeviceCmp (const FFPciDevice * a , const FFPciDevice * b )
125+ {
126+ return (int ) a -> id - (int ) b -> id ;
127+ }
128+
129+ static bool loadPciidsInc (uint8_t subclass , uint16_t vendor , uint16_t device , FFGPUResult * gpu )
130+ {
131+ for (const FFPciVendor * pvendor = ffPciVendors ; pvendor -> name ; pvendor ++ )
132+ {
133+ if (pvendor -> id != vendor ) continue ;
134+
135+ if (!gpu -> vendor .length )
136+ ffStrbufSetS (& gpu -> vendor , pvendor -> name );
137+
138+ const FFPciDevice * pdevice = (const FFPciDevice * ) bsearch (& device , pvendor -> devices , pvendor -> nDevices , sizeof (FFPciDevice ), (void * ) pciDeviceCmp );
139+
140+ if (pdevice )
141+ {
142+ uint32_t nameLen = (uint32_t ) strlen (pdevice -> name );
143+ const char * closingBracket = pdevice -> name + nameLen - 1 ;
144+ if (* closingBracket == ']' )
145+ {
146+ const char * openingBracket = memrchr (pdevice -> name , '[' , nameLen - 1 );
147+ if (openingBracket )
148+ {
149+ openingBracket ++ ;
150+ ffStrbufSetNS (& gpu -> name , (uint32_t ) (closingBracket - openingBracket ), openingBracket );
151+ }
152+ }
153+ if (!gpu -> name .length )
154+ ffStrbufSetNS (& gpu -> name , nameLen , pdevice -> name );
155+ return true;
156+ }
157+
158+ if (!gpu -> name .length )
159+ {
160+ const char * subclassStr ;
161+ switch (subclass )
162+ {
163+ case 0 /*PCI_CLASS_DISPLAY_VGA*/ : subclassStr = " (VGA compatible)" ; break ;
164+ case 1 /*PCI_CLASS_DISPLAY_XGA*/ : subclassStr = " (XGA compatible)" ; break ;
165+ case 2 /*PCI_CLASS_DISPLAY_3D*/ : subclassStr = " (3D)" ; break ;
166+ default : subclassStr = "" ; break ;
167+ }
168+
169+ ffStrbufSetF (& gpu -> name , "%s Device %04X%s" , gpu -> vendor .length ? gpu -> vendor .chars : "Unknown" , device , subclassStr );
170+ }
171+ return true;
172+ }
173+ return false;
174+ }
175+ #endif
176+
177+ void ffGPUFillVendorAndName (uint8_t subclass , uint16_t vendor , uint16_t device , FFGPUResult * gpu )
178+ {
179+ #if FF_HAVE_EMBEDDED_PCIIDS
180+ bool ok = loadPciidsInc (subclass , vendor , device , gpu );
181+ if (ok ) return ;
182+ #endif
183+ return parsePciIdsFile (loadPciIds (), subclass , vendor , device , gpu );
184+ }
0 commit comments