allow short edids, import full edid on with kvmd-edidconf

This commit is contained in:
Maxim Devaev
2025-03-19 03:51:31 +02:00
parent 2c4f7f1458
commit b86f4cd437
5 changed files with 47 additions and 36 deletions

View File

@@ -80,27 +80,29 @@ _CEA_SPEAKERS = 4
class Edid:
# https://en.wikipedia.org/wiki/Extended_Display_Identification_Data
def __init__(self, data: bytes, allow_short: bool=False) -> None:
if allow_short:
assert len(data) in [_SHORT, _LONG], f"Invalid EDID length: {len(data)}, should be {_SHORT} or {_LONG} bytes"
else:
assert len(data) == _LONG, f"Invalid EDID length: {len(data)}, should be {_LONG} bytes"
def __init__(self, data: bytes) -> None:
assert len(data) in [_SHORT, _LONG], f"Invalid EDID length: {len(data)}, should be {_SHORT} or {_LONG} bytes"
self.__long = (len(data) == _LONG)
if self.__long:
assert data[126] == 1, "Zero extensions number"
assert (data[_CEA + 0], data[_CEA + 1]) == (0x02, 0x03), "Can't find CEA extension"
self.__data = list(data)
self.__long = (len(data) == _LONG)
@classmethod
def from_file(cls, path: str, allow_short: bool=False) -> "Edid":
def is_header_valid(cls, data: bytes) -> bool:
return data.startswith(b"\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00")
@classmethod
def from_file(cls, path: str) -> "Edid":
with _smart_open(path, "rb") as file:
data = file.read()
if not data.startswith(b"\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00"):
if not cls.is_header_valid(data):
text = re.sub(r"\s", "", data.decode())
data = bytes([
int(text[index:index + 2], 16)
for index in range(0, len(text), 2)
])
return Edid(data, allow_short)
return Edid(data)
def write_hex(self, path: str) -> None:
self.__update_checksums()
@@ -236,7 +238,8 @@ class Edid:
self.__data[_CEA + 3] &= (0xFF - 0b01000000) # ~X
def __parse_cea(self) -> tuple[list[_CeaBlock], bytes]:
assert self.__long, "This EDID does not contain any CEA blocks"
if not self.__long:
raise EdidNoBlockError("This EDID does not contain any CEA blocks")
cea = self.__data[_CEA:]
dtd_begin = cea[2]