1#!/usr/bin/python3
2
3import sys
4import re
5
6# Convert DMT pdf to txt:
7# pdftotext -layout -f 18 -l 105  DMTr1\ v13.pdf DMT.txt
8
9# Path to the text file
10filepath = sys.argv[1]
11
12m = {}
13line = ""
14
15def parsei(key, regex, base=10):
16	global m
17	global line
18
19	match = re.search(regex, line)
20	if match != None:
21		m[key] = int(match.group(1), base)
22
23def parsef(key, regex, base=10):
24	global m
25	global line
26
27	match = re.search(regex, line)
28	if match != None:
29		m[key] = float(match.group(1))
30
31for line in open(filepath, 'r'):
32	# each page starts with this
33	if "VESA MONITOR TIMING STANDARD" in line:
34		m = { }
35
36	# each page ends with this
37	if "VESA Display Monitor Timing Standard" in line:
38		print("// {:#x} - {}".format(m["dmt_id"], m["name"]))
39
40		flags = []
41		if m["ilace"]:
42			flags += [ "DRM_MODE_FLAG_INTERLACE" ]
43
44		if m["hsp"]:
45			flags += [ "DRM_MODE_FLAG_PHSYNC" ]
46		else:
47			flags += [ "DRM_MODE_FLAG_NHSYNC" ]
48
49		if m["vsp"]:
50			flags += [ "DRM_MODE_FLAG_PVSYNC" ]
51		else:
52			flags += [ "DRM_MODE_FLAG_NVSYNC" ]
53
54		print("DRM_MODE(\"{}\", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}),".format(
55			m["name"],
56			int(m["pclk"] * 1000),
57			m["hact"], m["hfp"], m["hsw"], m["hbp"],
58			m["vact"], m["vfp"], m["vsw"], m["vbp"],
59			" | ".join(flags)
60			))
61
62	match = re.search("Timing Name\s+=\s+([^;]+)", line)
63	if match != None:
64		m["name"] = str.strip(match.group(1))
65
66	parsei("dmt_id", "EDID ID:\s+DMT ID: ([0-9A-Fa-f]+)h", 16)
67	parsef("pclk", "Pixel Clock\s+=\s+(\d+\.\d+)")
68
69	parsei("hact", "Hor Pixels\s+=\s+(\d+)")
70	parsei("hfp", "H Front Porch.*\s(\d+) Pixels")
71	parsei("hsw", "Hor Sync Time.*\s(\d+) Pixels")
72	parsei("hbp", "H Back Porch.*\s(\d+) Pixels")
73
74	parsei("vact", "Ver Pixels\s+=\s+(\d+)")
75	parsei("vfp", "V Front Porch.*\s(\d+)\s+lines")
76	parsei("vsw", "Ver Sync Time.*\s(\d+)\s+lines")
77	parsei("vbp", "V Back Porch.*\s(\d+)\s+lines")
78
79	match = re.search("Scan Type\s+=\s+(\w+);", line)
80	if match != None:
81		if match.group(1) == "NONINTERLACED":
82			m["ilace"] = False
83		elif match.group(1) == "INTERLACED":
84			m["ilace"] = True
85		else:
86			print("Bad scan type")
87			exit(-1)
88
89	match = re.search("Hor Sync Polarity\s+=\s+(\w+)", line)
90	if match != None:
91		if match.group(1) == "POSITIVE":
92			m["hsp"] = True
93		elif match.group(1) == "NEGATIVE":
94			m["hsp"] = False
95		else:
96			print("Bad hsync polarity")
97			exit(-1)
98
99	match = re.search("Ver Sync Polarity\s+=\s+(\w+)", line)
100	if match != None:
101		if match.group(1) == "POSITIVE":
102			m["vsp"] = True
103		elif match.group(1) == "NEGATIVE":
104			m["vsp"] = False
105		else:
106			print("Bad vsync polarity")
107			exit(-1)
108