From 7993cab4dfe459d97333fb164e420f4c4bf1d3f1 Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 11 Jul 2025 14:16:35 +0200 Subject: [PATCH] feat(linux): add gpu name info --- src/linux/hardware.zig | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/linux/hardware.zig b/src/linux/hardware.zig index 0db343c..75a247a 100644 --- a/src/linux/hardware.zig +++ b/src/linux/hardware.zig @@ -1,6 +1,7 @@ const std = @import("std"); const c_unistd = @cImport(@cInclude("unistd.h")); const c_statvfs = @cImport(@cInclude("sys/statvfs.h")); +const c_libpci = @cImport(@cInclude("pci/pci.h")); /// Struct representing CPU informations pub const CpuInfo = struct { @@ -9,6 +10,13 @@ pub const CpuInfo = struct { cpu_max_freq: f32, }; +/// Struct representing GPU informations +pub const GpuInfo = struct { + gpu_name: []u8, + gpu_cores: i32, + gpu_freq: f64, +}; + /// Struct representing RAM usage informations pub const RamInfo = struct { ram_size: f64, @@ -76,6 +84,45 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo { }; } +pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { + var gpu_info = GpuInfo{ + .gpu_name = try allocator.dupe(u8, "Unknown"), + .gpu_cores = 0, + .gpu_freq = 0.0, + }; + + const display_controller = 0x03; + + const pacc = c_libpci.pci_alloc(); + defer c_libpci.pci_cleanup(pacc); + c_libpci.pci_init(pacc); + c_libpci.pci_scan_bus(pacc); + + var devices = pacc.*.devices; + while (devices != null) : (devices = devices.*.next) { + // TODO: handle return value + _ = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS); + + if ((devices.*.device_class >> 8) == display_controller) { + var name_buffer: [256]u8 = undefined; + + const name = c_libpci.pci_lookup_name( + pacc, + &name_buffer, + name_buffer.len, + c_libpci.PCI_LOOKUP_VENDOR | c_libpci.PCI_LOOKUP_DEVICE, + devices.*.vendor_id, + devices.*.device_id, + ); + + allocator.free(gpu_info.gpu_name); + gpu_info.gpu_name = try allocator.dupe(u8, std.mem.span(name)); + } + } + + return gpu_info; +} + pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo { // Reads /proc/meminfo const meminfo_path = "/proc/meminfo";