From edaaa178b849c1245a86bb547e1134568efaa1ce Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 3 Jan 2018 23:20:28 +0900 Subject: [PATCH] devicescale: Implement scaling for Gnome and Cinnamon (#344) --- internal/devicescale/devicescale_xwindow.go | 53 ++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/internal/devicescale/devicescale_xwindow.go b/internal/devicescale/devicescale_xwindow.go index 5dc3ce3a7..c740c54f6 100644 --- a/internal/devicescale/devicescale_xwindow.go +++ b/internal/devicescale/devicescale_xwindow.go @@ -18,7 +18,58 @@ package devicescale +import ( + "os/exec" + "regexp" + "strconv" +) + +var gsettingsRe = regexp.MustCompile(`\Auint32 (\d+)\s*\z`) + +func gnomeScale() float64 { + out, err := exec.Command("gsettings", "get", "org.gnome.desktop.interface", "scaling-factor").Output() + if err != nil { + if err == exec.ErrNotFound { + return 0 + } + if _, ok := err.(*exec.ExitError); ok { + return 0 + } + panic(err) + } + m := gsettingsRe.FindStringSubmatch(string(out)) + s, err := strconv.Atoi(m[1]) + if err != nil { + return 0 + } + return float64(s) +} + +func cinnamonScale() float64 { + out, err := exec.Command("gsettings", "get", "org.cinnamon.desktop.interface", "scaling-factor").Output() + if err != nil { + if err == exec.ErrNotFound { + return 0 + } + if _, ok := err.(*exec.ExitError); ok { + return 0 + } + panic(err) + } + m := gsettingsRe.FindStringSubmatch(string(out)) + s, err := strconv.Atoi(m[1]) + if err != nil { + return 0 + } + return float64(s) +} + func impl() float64 { - // TODO: Implement this + if s := gnomeScale(); s != 0 { + return s + } + if s := cinnamonScale(); s != 0 { + return s + } return 1 }