qtbase/util/edid/qedidvendortable.py
Giuseppe D'Angelo 6844c70ae9 QEdidParser: optimize the hardcoded EDID vendor table
Instead of using an array of fixed-length strings for the vendor names,
use qOffsetStringArray instead. This removes the need of using the
longest vendor name length as the size of the fixed-length strings,
which results in a massive waste of space. This saves 150KB of
(readonly) data.

Details: there are 2555 entries, each one hardcoded to be 78 byte long,
for a total of 199290 bytes. However the vendor names themselves amount
only to 50658 bytes.

Making all this data optional on desktop Linux (where the vendor data is
present anyhow on the system, in /usr/share/) is left for a future
change.

Change-Id: I17007865e741e3dab15dd2ab2feffbce4664fb37
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2024-12-21 00:03:36 +01:00

126 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright (C) 2017 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
import urllib.request
import sys
# The original source for this data used to be
# 'https://git.fedorahosted.org/cgit/hwdata.git/plain/pnp.ids'
# which is discontinued. For now there seems to be a fork at:
url = 'https://github.com/vcrhonek/hwdata/raw/master/pnp.ids'
# REUSE-IgnoreStart
copyright = """// Copyright (C) 2017 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
"""
# REUSE-IgnoreEnd
notice = """/*
* This lookup table was generated from {}
*
* Do not change this file directly, instead edit the
* qtbase/util/edid/qedidvendortable.py script and regenerate this file.
*/""".format(url)
header = """
#ifndef QEDIDVENDORTABLE_P_H
#define QEDIDVENDORTABLE_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/private/qglobal_p.h>
#include <QtCore/qtypes.h>
#include <iterator>
QT_BEGIN_NAMESPACE
"""
vendorIdHeader = """struct QEdidVendorId {
const char id[4];
};
static constexpr QEdidVendorId q_edidVendorIds[] = {"""
vendorIdFooter = """};
"""
vendorNameHeader = """static constexpr char q_edidVendorNames[] = {"""
vendorNameFooter = """};
"""
vendorNameOffsetHeader = """static constexpr %s q_edidVendorNamesOffsets[] = {"""
vendorNameOffsetFooter = """};
"""
footer = """static_assert(std::size(q_edidVendorIds) == std::size(q_edidVendorNamesOffsets));
QT_END_NAMESPACE
#endif // QEDIDVENDORTABLE_P_H"""
# Actual script begins here
vendors = {}
vendorNameTotalLength = 0
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
for line in data.split('\n'):
if line.startswith('#'):
continue
elif len(line) == 0:
continue
l = line.split('\t', 1)
if len(l) == 0:
continue
pnp_id = l[0].upper()
if len(pnp_id) != 3:
sys.exit("Id '%s' is non-conforming" % pnp_id)
vendors[pnp_id] = l[1]
vendorNameTotalLength += len(l[1]) + 1
sortedVendorKeys = sorted(vendors.keys())
print(copyright)
print(notice)
print(header)
print(vendorIdHeader)
print(*[(' { "%s" }' % pnp_id) for pnp_id in sortedVendorKeys], sep=",\n")
print(vendorIdFooter)
print(vendorNameHeader)
print(*[(' "%s\\0"' % vendors[pnp_id]) for pnp_id in sortedVendorKeys], sep="\n")
print(vendorNameFooter)
if vendorNameTotalLength < 2**16:
print(vendorNameOffsetHeader % "quint16")
elif vendorNameTotalLength < 2**32:
print(vendorNameOffsetHeader % "quint32")
else:
sys.exit("Vendor name table is too big")
currentOffset = 0
for pnp_id in sortedVendorKeys:
vendor = vendors[pnp_id]
print(' %d,' % currentOffset)
currentOffset += len(vendor) + 1
print(vendorNameOffsetFooter)
print(footer)