diff --git a/sysinfo/gpu.go b/sysinfo/gpu.go new file mode 100644 index 00000000..d3b0d745 --- /dev/null +++ b/sysinfo/gpu.go @@ -0,0 +1,57 @@ +package sysinfo + +import ( + "os" + "strings" + "path" + "path/filepath" +) + +// ANY error here will be ignored. All of the filepath querying +// and such is done within /sys/, which is stored in memory. + +type Card struct { + Path string + Driver string + Index int + Embedded bool +} + +const drmPath = "/sys/class/drm" +var embeddedDisplays = []string{"eDP", "LVDS", "DP-2"} + +func Cards() (cards []Card) { + drmCards, _ := filepath.Glob(path.Join(drmPath, "card[0-9]")) + + for i, c := range drmCards { + d, _ := filepath.EvalSymlinks(path.Join(c, "device/driver")) + d = path.Base(d) + + cards = append(cards, Card{ + Path: c, + Driver: d, + Index: i, + Embedded: embedded(c), + }) + } + + return +} + +func embedded(cardPath string) (embed bool) { + filepath.Walk(drmPath, func(p string, f os.FileInfo, err error) error { + if !strings.HasPrefix(p, cardPath) { + return nil + } + + for _, hwd := range embeddedDisplays { + if strings.Contains(p, hwd) { + embed = true + } + } + + return nil + }) + + return +} diff --git a/sysinfo/sysinfo.go b/sysinfo/sysinfo.go index ea10f9b3..7197ce42 100644 --- a/sysinfo/sysinfo.go +++ b/sysinfo/sysinfo.go @@ -2,13 +2,10 @@ package sysinfo import ( "os" - "io/fs" - "strconv" "bufio" "regexp" "syscall" "strings" - "path/filepath" ) type Kernel struct { @@ -21,15 +18,6 @@ type CPU struct { Flags []string } -type GPU struct { - Path string - Integrated bool - Index int - Driver string -} - -type GPUs []GPU - func GetKernel() Kernel { var un syscall.Utsname _ = syscall.Uname(&un) @@ -93,32 +81,6 @@ func (cpu *CPU) String() string { return cpu.Model } -func GetGPUs() (gpus GPUs) { - card := regexp.MustCompile(`card([0-9]+)(?:-eDP-\d+)?$`) - - filepath.Walk("/sys/class/drm", func(p string, i fs.FileInfo, err error) error { - var gpu GPU - - match := card.FindStringSubmatch(p) - if match == nil { - return nil - } - - if len(match) == 2 { - gpu.Integrated = true - } - - gpu.Index, _ = strconv.Atoi(match[1]) - gpu.Driver, _ = filepath.EvalSymlinks(filepath.Join(p, "device/driver")) - gpu.Path = p - - gpus = append(gpus, gpu) - return nil - }) - - return -} - func InFlatpak() bool { _, err := os.Stat("/.flatpak-info") return err == nil