Skip to content

Commit

Permalink
sysinfo: better gpu (card) handling
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Oct 20, 2023
1 parent 864c97b commit 6bd6ab2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
57 changes: 57 additions & 0 deletions sysinfo/gpu.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 0 additions & 38 deletions sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package sysinfo

import (
"os"
"io/fs"
"strconv"
"bufio"
"regexp"
"syscall"
"strings"
"path/filepath"
)

type Kernel struct {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6bd6ab2

Please sign in to comment.