fix(hyprland): resolve desc: monitor names before generating config

Extract shared resolveMonitorNames to backend package so both
Hyprland and Sway backends resolve desc: prefixed names to actual
connector names before applying monitor layout.

Previously the Hyprland backend wrote desc:... literally into
monitors.conf, which Hyprland ignores, causing monitors to be
positioned incorrectly or not configured at all.
This commit is contained in:
Maksim Totmin
2026-06-23 09:06:08 +07:00
parent fb40347c68
commit ac2bb77113
3 changed files with 75 additions and 64 deletions
+68 -1
View File
@@ -8,7 +8,12 @@
// cmd/monitor-lets-go/main.go.
package backend
import "context"
import (
"context"
"fmt"
"log/slog"
"strings"
)
// MonitorInfo represents a physical display as reported by the compositor.
// Fields use JSON tags matching hyprctl -j output; other compositors
@@ -97,3 +102,65 @@ type Backend interface {
// Close releases any resources held by the backend.
Close() error
}
// resolveMonitorNames converts desc: prefixed monitor config names to the
// actual connector names (e.g. "desc:Xiaomi Corporation Mi Monitor" → "DP-9")
// by matching against the current set of connected monitors.
//
// Matching logic:
// - "desc:make model" — substring match against description (make + " " + model)
// - "desc:serial" — substring match against serial number
// - plain names are returned as-is
func resolveMonitorNames(ctx context.Context, monitors []MonitorConfig, getMonitors func(context.Context) ([]MonitorInfo, error), logger *slog.Logger) ([]MonitorConfig, error) {
var needsDescResolution bool
for _, m := range monitors {
if strings.HasPrefix(m.Name, "desc:") {
needsDescResolution = true
break
}
}
if !needsDescResolution {
return monitors, nil
}
current, err := getMonitors(ctx)
if err != nil {
return nil, fmt.Errorf("get current monitors: %w", err)
}
resolved := make([]MonitorConfig, len(monitors))
for i, m := range monitors {
if !strings.HasPrefix(m.Name, "desc:") {
resolved[i] = m
continue
}
needle := strings.TrimPrefix(m.Name, "desc:")
var matches []MonitorInfo
for _, mi := range current {
if strings.Contains(mi.Description, needle) || strings.Contains(mi.Serial, needle) {
matches = append(matches, mi)
}
}
switch len(matches) {
case 0:
return nil, fmt.Errorf("no connected monitor matches desc:%q", needle)
case 1:
resolved[i] = m
resolved[i].Name = matches[0].Name
logger.Debug("resolved desc to connector",
"desc", needle, "connector", matches[0].Name)
default:
var names []string
for _, mat := range matches {
names = append(names, mat.Name)
}
return nil, fmt.Errorf(
"desc:%q matches multiple monitors: %s — use a more specific identifier",
needle, strings.Join(names, ", "))
}
}
return resolved, nil
}