zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs

impl_unix.go (2217B)


      1 // Copyright 2018 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 // +build dragonfly freebsd linux netbsd openbsd solaris
     16 // +build !android
     17 
     18 package devicescale
     19 
     20 import (
     21 	"os"
     22 	"os/exec"
     23 	"regexp"
     24 	"strconv"
     25 	"strings"
     26 )
     27 
     28 type desktop int
     29 
     30 const (
     31 	desktopUnknown desktop = iota
     32 	desktopGnome
     33 	desktopCinnamon
     34 	desktopUnity
     35 	desktopKDE
     36 	desktopXfce
     37 )
     38 
     39 func currentDesktop() desktop {
     40 	tokens := strings.Split(os.Getenv("XDG_CURRENT_DESKTOP"), ":")
     41 	switch tokens[len(tokens)-1] {
     42 	case "GNOME":
     43 		return desktopGnome
     44 	case "X-Cinnamon":
     45 		return desktopCinnamon
     46 	case "Unity":
     47 		return desktopUnity
     48 	case "KDE":
     49 		return desktopKDE
     50 	case "XFCE":
     51 		return desktopXfce
     52 	default:
     53 		return desktopUnknown
     54 	}
     55 }
     56 
     57 var gsettingsRe = regexp.MustCompile(`\Auint32 (\d+)\s*\z`)
     58 
     59 func gnomeScale() float64 {
     60 	// TODO: Should 'monitors.xml' be loaded?
     61 
     62 	out, err := exec.Command("gsettings", "get", "org.gnome.desktop.interface", "scaling-factor").Output()
     63 	if err != nil {
     64 		if err == exec.ErrNotFound {
     65 			return 0
     66 		}
     67 		if _, ok := err.(*exec.ExitError); ok {
     68 			return 0
     69 		}
     70 		panic(err)
     71 	}
     72 	m := gsettingsRe.FindStringSubmatch(string(out))
     73 	s, err := strconv.Atoi(m[1])
     74 	if err != nil {
     75 		return 0
     76 	}
     77 	return float64(s)
     78 }
     79 
     80 func impl(x, y int) float64 {
     81 	s := -1.0
     82 	switch currentDesktop() {
     83 	case desktopGnome:
     84 		// TODO: Support wayland and per-monitor scaling https://wiki.gnome.org/HowDoI/HiDpi
     85 		s = gnomeScale()
     86 	case desktopCinnamon:
     87 		s = cinnamonScale()
     88 	case desktopUnity:
     89 		// TODO: Implement, supports per-monitor scaling
     90 	case desktopKDE:
     91 		// TODO: Implement, appears to support per-monitor scaling
     92 	case desktopXfce:
     93 		// TODO: Implement
     94 	}
     95 	if s <= 0 {
     96 		s = 1
     97 	}
     98 
     99 	return s
    100 }