2023-12-18 23:08:42 +01:00
|
|
|
package xrandr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-12-18 23:15:12 +01:00
|
|
|
"encoding/json"
|
2023-12-18 23:08:42 +01:00
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/bufr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Displays []DisplayConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCurrentConfig() (*Config, error) {
|
|
|
|
output, err := exec.Command("xrandr").Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bufr.New(bytes.NewReader(output))
|
|
|
|
r.StringUntil(isNewLine)
|
|
|
|
|
|
|
|
configs := make([]DisplayConfig, 0)
|
|
|
|
for config, err := parseDisplayConfig(r); err == nil; config, err = parseDisplayConfig(r) {
|
|
|
|
configs = append(configs, *config)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Config{configs}, nil
|
|
|
|
}
|
2023-12-18 23:15:12 +01:00
|
|
|
|
|
|
|
func (c *Config) String() string {
|
|
|
|
data, _ := json.MarshalIndent(c, "", "\t")
|
|
|
|
return string(data)
|
|
|
|
}
|