package main import ( "bufio" "os" "path/filepath" "regexp" "sort" "strings" ) // Profile is one saved autorandr layout: the monitors it is for, by output and EDID fingerprint. type Profile struct { Name string `json:"name"` Path string `json:"path"` Monitors map[string]string `json:"monitors"` Detected bool `json:"detected,omitempty"` Current bool `json:"current,omitempty"` } // profileName is what a profile may be called: it becomes a directory name. var profileName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) // ProfileDirs are where autorandr keeps profiles, the account's first (autorandr(1)). func ProfileDirs(home string) []string { cfg := os.Getenv("XDG_CONFIG_HOME") if cfg == "" { cfg = filepath.Join(home, ".config") } return []string{filepath.Join(cfg, "autorandr"), "/etc/xdg/autorandr"} } // ReadProfiles lists the profiles in dirs; a name in an earlier directory hides a later one, as in // autorandr itself. Each profile's monitors come from its `setup` file: ` ` per line. func ReadProfiles(dirs []string) []Profile { seen := map[string]bool{} var out []Profile for _, dir := range dirs { entries, err := os.ReadDir(dir) if err != nil { continue } for _, e := range entries { if !e.IsDir() || seen[e.Name()] { continue } setup := filepath.Join(dir, e.Name(), "setup") f, err := os.Open(setup) if err != nil { continue } p := Profile{Name: e.Name(), Path: filepath.Join(dir, e.Name()), Monitors: map[string]string{}} sc := bufio.NewScanner(f) for sc.Scan() { fields := strings.Fields(sc.Text()) if len(fields) == 2 { p.Monitors[fields[0]] = shortEDID(fields[1]) } } f.Close() seen[e.Name()] = true out = append(out, p) } } sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) return out } // shortEDID is the fingerprint DecodeEDID gives for the same monitor, so a profile's monitors and the // connected ones are compared by one value. func shortEDID(hexEDID string) string { b := make([]byte, 0, len(hexEDID)/2) for i := 0; i+1 < len(hexEDID); i += 2 { var v byte for _, c := range hexEDID[i : i+2] { v <<= 4 switch { case c >= '0' && c <= '9': v |= byte(c - '0') case c >= 'a' && c <= 'f': v |= byte(c - 'a' + 10) case c >= 'A' && c <= 'F': v |= byte(c - 'A' + 10) default: return hexEDID } } b = append(b, v) } if id := DecodeEDID(b); id != nil { return id.Fingerprint } return hexEDID } // names reads autorandr's one-name-per-line answers (--detected, --current). func names(text string) map[string]bool { out := map[string]bool{} for _, l := range strings.Split(text, "\n") { if l = strings.TrimSpace(l); l != "" { out[strings.Fields(l)[0]] = true } } return out }