feature: Add basic EDID reading, writing, and patching capabilities
This commit is contained in:
parent
d0a4d26082
commit
183d2606dc
12 changed files with 431 additions and 5 deletions
72
app/edidtools/edid_parser.go
Normal file
72
app/edidtools/edid_parser.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package edidtools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
edidparser "github.com/anoopengineer/edidparser/edid"
|
||||
)
|
||||
|
||||
func ParseEDID(rawEDIDFile []byte, allowUnsupportedDevices bool) (*DisplayMetadata, error) {
|
||||
parsedEDID, err := edidparser.NewEdid(rawEDIDFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse EDID file: %w", err)
|
||||
}
|
||||
|
||||
for manufacturer, manufacturerSupportedDevices := range QuirksRegistry {
|
||||
if parsedEDID.ManufacturerId == manufacturer {
|
||||
if deviceQuirks, ok := manufacturerSupportedDevices[parsedEDID.MonitorName]; ok || allowUnsupportedDevices {
|
||||
maxWidth := 0
|
||||
maxHeight := 0
|
||||
maxRefreshRate := 0
|
||||
|
||||
for _, resolution := range parsedEDID.DetailedTimingDescriptors {
|
||||
if int(resolution.HorizontalActive) > maxWidth && int(resolution.VerticalActive) > maxHeight {
|
||||
maxWidth = int(resolution.HorizontalActive)
|
||||
maxHeight = int(resolution.VerticalActive)
|
||||
}
|
||||
|
||||
// Convert pixel clock to refresh rate
|
||||
// Refresh Rate = Pixel Clock / ((Horizontal Active + Horizontal Blanking) * (Vertical Active + Vertical Blanking))
|
||||
hTotal := int(resolution.HorizontalActive + resolution.HorizontalBlanking)
|
||||
vTotal := int(resolution.VerticalActive + resolution.VerticalBlanking)
|
||||
refreshRate := int(int(resolution.PixelClock*1000) / (hTotal * vTotal))
|
||||
|
||||
if refreshRate > maxRefreshRate {
|
||||
maxRefreshRate = refreshRate
|
||||
}
|
||||
}
|
||||
|
||||
if maxWidth == 0 || maxHeight == 0 {
|
||||
if deviceQuirks.MaxWidth == 0 || deviceQuirks.MaxHeight == 0 {
|
||||
return nil, fmt.Errorf("failed to determine maximum resolution for monitor '%s'", parsedEDID.MonitorName)
|
||||
}
|
||||
|
||||
maxWidth = deviceQuirks.MaxWidth
|
||||
maxHeight = deviceQuirks.MaxHeight
|
||||
}
|
||||
|
||||
if maxRefreshRate == 0 {
|
||||
if deviceQuirks.MaxRefreshRate == 0 {
|
||||
return nil, fmt.Errorf("failed to determine maximum refresh rate for monitor '%s'", parsedEDID.MonitorName)
|
||||
}
|
||||
|
||||
maxRefreshRate = deviceQuirks.MaxRefreshRate
|
||||
}
|
||||
|
||||
displayMetadata := &DisplayMetadata{
|
||||
EDID: rawEDIDFile,
|
||||
DeviceVendor: parsedEDID.ManufacturerId,
|
||||
DeviceQuirks: deviceQuirks,
|
||||
MaxWidth: maxWidth,
|
||||
MaxHeight: maxHeight,
|
||||
MaxRefreshRate: maxRefreshRate,
|
||||
}
|
||||
|
||||
return displayMetadata, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to match manufacturer for monitor vendor: '%s'", parsedEDID.ManufacturerId)
|
||||
}
|
127
app/edidtools/patching_tools_linux.go
Normal file
127
app/edidtools/patching_tools_linux.go
Normal file
|
@ -0,0 +1,127 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package edidtools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
// Attempts to fetch the EDID firmware for any supported XR glasses device
|
||||
func FetchXRGlassEDID(allowUnsupportedDevices bool) (*DisplayMetadata, error) {
|
||||
// Implementation goes here
|
||||
pciDeviceCommand, err := exec.Command("lspci").Output()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute lspci command: %w", err)
|
||||
}
|
||||
|
||||
pciDevices := strings.Split(string(pciDeviceCommand), "\n")
|
||||
pciDevices = pciDevices[:len(pciDevices)-1]
|
||||
|
||||
vgaDevices := []string{}
|
||||
|
||||
for _, pciDevice := range pciDevices {
|
||||
if strings.Contains(pciDevice, "VGA compatible controller:") {
|
||||
vgaDevices = append(vgaDevices, pciDevice[:strings.Index(pciDevice, " ")])
|
||||
}
|
||||
}
|
||||
|
||||
for _, vgaDevice := range vgaDevices {
|
||||
cardDevices, err := os.ReadDir("/sys/devices/pci0000:00/0000:" + vgaDevice + "/drm/")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read directory for device '%s': %w", vgaDevice, err)
|
||||
}
|
||||
|
||||
for _, cardDevice := range cardDevices {
|
||||
if !strings.Contains(cardDevice.Name(), "card") {
|
||||
continue
|
||||
}
|
||||
|
||||
monitors, err := os.ReadDir("/sys/devices/pci0000:00/0000:" + vgaDevice + "/drm/" + cardDevice.Name())
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read directory for card device '%s': %w", cardDevice.Name(), err)
|
||||
}
|
||||
|
||||
for _, monitor := range monitors {
|
||||
if !strings.Contains(monitor.Name(), cardDevice.Name()) {
|
||||
continue
|
||||
}
|
||||
|
||||
rawEDIDFile, err := os.ReadFile("/sys/devices/pci0000:00/0000:" + vgaDevice + "/drm/" + cardDevice.Name() + "/" + monitor.Name() + "/edid")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read EDID file for monitor '%s': %w", monitor.Name(), err)
|
||||
}
|
||||
|
||||
if len(rawEDIDFile) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parsedEDID, err := ParseEDID(rawEDIDFile, allowUnsupportedDevices)
|
||||
|
||||
if err != nil {
|
||||
if !strings.HasPrefix(err.Error(), "failed to match manufacturer for monitor vendor") {
|
||||
log.Warnf("Failed to parse EDID for monitor '%s': %s", monitor.Name(), err.Error())
|
||||
}
|
||||
} else {
|
||||
parsedEDID.LinuxDRMCard = cardDevice.Name()
|
||||
parsedEDID.LinuxDRMConnector = strings.Replace(cardDevice.Name(), cardDevice.Name()+"-", "", 1)
|
||||
|
||||
return parsedEDID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("could not find supported device! Check if the XR device is plugged in. If it is plugged in and working correctly, check the README or open an issue.")
|
||||
}
|
||||
|
||||
// Loads custom firmware for a supported XR glass device
|
||||
func LoadCustomEDIDFirmware(displayMetadata *DisplayMetadata, edidFirmware []byte) error {
|
||||
if displayMetadata.LinuxDRMCard == "" || displayMetadata.LinuxDRMConnector == "" {
|
||||
return fmt.Errorf("missing Linux DRM card or connector information")
|
||||
}
|
||||
|
||||
drmFile, err := os.Open("/sys/kernel/debug/dri/" + strings.Replace(displayMetadata.LinuxDRMCard, "card", "", 1) + "/" + displayMetadata.LinuxDRMConnector + "/edid_override")
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open EDID override file for monitor '%s': %w", displayMetadata.LinuxDRMConnector, err)
|
||||
}
|
||||
|
||||
defer drmFile.Close()
|
||||
|
||||
if _, err := drmFile.Write(edidFirmware); err != nil {
|
||||
return fmt.Errorf("failed to write EDID firmware for monitor '%s': %w", displayMetadata.LinuxDRMConnector, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unloads custom firmware for a supported XR glass device
|
||||
func UnloadCustomEDIDFirmware(displayMetadata *DisplayMetadata) error {
|
||||
if displayMetadata.LinuxDRMCard == "" || displayMetadata.LinuxDRMConnector == "" {
|
||||
return fmt.Errorf("missing Linux DRM card or connector information")
|
||||
}
|
||||
|
||||
drmFile, err := os.Open("/sys/kernel/debug/dri/" + strings.Replace(displayMetadata.LinuxDRMCard, "card", "", 1) + "/" + displayMetadata.LinuxDRMConnector + "/edid_override")
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open EDID override file for monitor '%s': %w", displayMetadata.LinuxDRMConnector, err)
|
||||
}
|
||||
|
||||
defer drmFile.Close()
|
||||
|
||||
if _, err := drmFile.Write([]byte("reset")); err != nil {
|
||||
return fmt.Errorf("failed to unload EDID firmware for monitor '%s': %w", displayMetadata.LinuxDRMConnector, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
21
app/edidtools/patching_tools_macos.go
Normal file
21
app/edidtools/patching_tools_macos.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package edidtools
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Attempts to fetch the EDID firmware for any supported XR glasses device
|
||||
func FetchXRGlassEDID(allowUnsupportedDevices bool) (*DisplayMetadata, error) {
|
||||
return nil, fmt.Errorf("automatic fetching of EDID data is not supported on macOS")
|
||||
}
|
||||
|
||||
// Loads custom firmware for a supported XR glass device
|
||||
func LoadCustomEDIDFirmware(displayMetadata *DisplayMetadata, edidFirmware []byte) error {
|
||||
return fmt.Errorf("loading custom EDID firmware is not supported on macOS")
|
||||
}
|
||||
|
||||
// Unloads custom firmware for a supported XR glass device
|
||||
func UnloadCustomEDIDFirmware(displayMetadata *DisplayMetadata) error {
|
||||
return fmt.Errorf("unloading custom EDID firmware is not supported on macOS")
|
||||
}
|
21
app/edidtools/patching_tools_win.go
Normal file
21
app/edidtools/patching_tools_win.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package edidtools
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Attempts to fetch the EDID firmware for any supported XR glasses device
|
||||
func FetchXRGlassEDID(allowUnsupportedDevices bool) (*DisplayMetadata, error) {
|
||||
return nil, fmt.Errorf("automatic fetching of EDID data is not supported on Windows")
|
||||
}
|
||||
|
||||
// Loads custom firmware for a supported XR glass device
|
||||
func LoadCustomEDIDFirmware(displayMetadata *DisplayMetadata, edidFirmware []byte) error {
|
||||
return fmt.Errorf("loading custom EDID firmware is not supported on Windows")
|
||||
}
|
||||
|
||||
// Unloads custom firmware for a supported XR glass device
|
||||
func UnloadCustomEDIDFirmware(displayMetadata *DisplayMetadata) error {
|
||||
return fmt.Errorf("unloading custom EDID firmware is not supported on Windows")
|
||||
}
|
14
app/edidtools/quirks.go
Normal file
14
app/edidtools/quirks.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package edidtools
|
||||
|
||||
// Vendor and devices names sourced from "https://uefi.org/uefi-pnp-export"
|
||||
var QuirksRegistry = map[string]map[string]DisplayQuirks{
|
||||
"MRG": {
|
||||
"Air": {
|
||||
MaxWidth: 1920,
|
||||
MaxHeight: 1080,
|
||||
MaxRefreshRate: 120,
|
||||
SensorInitDelay: 10,
|
||||
ZVectorDisabled: true,
|
||||
},
|
||||
},
|
||||
}
|
20
app/edidtools/struct.go
Normal file
20
app/edidtools/struct.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package edidtools
|
||||
|
||||
type DisplayQuirks struct {
|
||||
MaxWidth int
|
||||
MaxHeight int
|
||||
MaxRefreshRate int
|
||||
SensorInitDelay int
|
||||
ZVectorDisabled bool
|
||||
}
|
||||
|
||||
type DisplayMetadata struct {
|
||||
EDID []byte
|
||||
DeviceVendor string
|
||||
DeviceQuirks DisplayQuirks
|
||||
MaxWidth int
|
||||
MaxHeight int
|
||||
MaxRefreshRate int
|
||||
LinuxDRMCard string
|
||||
LinuxDRMConnector string
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue