Skip to content

Commit

Permalink
feat(adjust-grub-theme): getScreenSizeFromXrandr
Browse files Browse the repository at this point in the history
  • Loading branch information
UTsweetyfish committed Sep 5, 2023
1 parent 15123e6 commit f06aafb
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions adjust-grub-theme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,44 @@ func getScreenSizeFromGrubParams(grubParamsFilePath string) (w, h int, err error
return
}

func getScreenSizeFromXrandr() (w, h int, err error) {
out, err = exec.Command("xrandr").Output()
if err != nil {
return 0, 0, err
}
// try to get primary screen resolution, for example, extract 1440x900
// from "LVDS1 connected primary 1440x900+0+0 (normal left inverted
// right x axis y axis)"
re := regexp.MustCompile(` connected primary (\d+)x(\d+)`)
matches := re.FindStringSubmatch(string(out))
if len(matches) == 3 {
w, err := strconv.Atoi(matches[1])
if err != nil {
return 0, 0, err
}
h := strconv.Atoi(matches[2])
if err != nil {
return 0, 0, err
}
return w, h, nil
}
// if failed, try to get the master screen resolution
re = regexp.MustCompile(`Screen 0: .*current (\d+) x (\d+)`)
matches := re.FindStringSubmatch(string(out))
if len(matches) == 3 {
w, err := strconv.Atoi(matches[1])
if err != nil {
return 0, 0, err
}
h := strconv.Atoi(matches[2])
if err != nil {
return 0, 0, err
}
return w, h, nil
}
return 0, 0, fmt.Errorf("Failed to extract primary screen resolution")
}

func cropSaveStyleBox(img image.Image, filenamePrefix string, r int) {
imgW := img.Bounds().Dx()
imgH := img.Bounds().Dy()
Expand Down Expand Up @@ -492,8 +530,12 @@ func main() {
optScreenWidth, optScreenHeight, err = getScreenSizeFromGrubParams(grubParamsFile)
if err != nil {
logger.Debug(err)
optScreenWidth = 1024
optScreenHeight = 768
optScreenWidth, optScreenHeight, err = getScreenSizeFromXrandr()
if err != nil {
logger.Debug(err)
optScreenWidth = 1024
optScreenHeight = 768
}
}
logger.Debug("screen width:", optScreenWidth)
logger.Debug("screen height:", optScreenHeight)
Expand Down

0 comments on commit f06aafb

Please sign in to comment.