From eec95e3d5e1a4f2e13b1f6b34cc287475ca57daf Mon Sep 17 00:00:00 2001
From: illiliti <illiliti@protonmail.com>
Date: Mon, 27 Jun 2022 00:11:47 +0300
Subject: backend/drm: use pnp.ids to fetch EDID data

---
 .builds/alpine.yml         |  1 +
 .builds/archlinux.yml      |  1 +
 .builds/freebsd.yml        |  1 +
 README.md                  |  2 ++
 backend/drm/gen_pnpids.sh  | 27 ++++++++++++++
 backend/drm/meson.build    | 18 ++++++++++
 backend/drm/util.c         | 88 ++++++----------------------------------------
 include/backend/drm/util.h |  2 ++
 8 files changed, 63 insertions(+), 77 deletions(-)
 create mode 100755 backend/drm/gen_pnpids.sh

diff --git a/.builds/alpine.yml b/.builds/alpine.yml
index ab613b28..6028af00 100644
--- a/.builds/alpine.yml
+++ b/.builds/alpine.yml
@@ -17,6 +17,7 @@ packages:
   - xcb-util-wm-dev
   - xwayland
   - libseat-dev
+  - hwdata
 sources:
   - https://gitlab.freedesktop.org/wlroots/wlroots.git
 tasks:
diff --git a/.builds/archlinux.yml b/.builds/archlinux.yml
index ec98563c..2d62d0d1 100644
--- a/.builds/archlinux.yml
+++ b/.builds/archlinux.yml
@@ -18,6 +18,7 @@ packages:
   - vulkan-icd-loader
   - vulkan-headers
   - glslang
+  - hwdata
 sources:
   - https://gitlab.freedesktop.org/wlroots/wlroots.git
 tasks:
diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml
index dc79d561..0f4007bb 100644
--- a/.builds/freebsd.yml
+++ b/.builds/freebsd.yml
@@ -25,6 +25,7 @@ packages:
   - x11-servers/xwayland
   - sysutils/seatd
   - gmake
+  - hwdata
 sources:
   - https://gitlab.freedesktop.org/wlroots/wlroots.git
 tasks:
diff --git a/README.md b/README.md
index 7fbd691a..60ec180c 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,7 @@ Install dependencies:
 * udev
 * pixman
 * [libseat]
+* [hwdata] (optional, for the DRM backend)
 
 If you choose to enable X11 support:
 
@@ -78,4 +79,5 @@ See [CONTRIBUTING.md].
 [#sway-devel on Libera Chat]: https://web.libera.chat/gamja/?channels=#sway-devel
 [wrapper libraries]: https://gitlab.freedesktop.org/wlroots/wlroots/-/wikis/Projects-which-use-wlroots#wrapper-libraries
 [libseat]: https://git.sr.ht/~kennylevinsen/seatd
+[hwdata]: https://github.com/vcrhonek/hwdata
 [CONTRIBUTING.md]: https://gitlab.freedesktop.org/wlroots/wlroots/-/blob/master/CONTRIBUTING.md
diff --git a/backend/drm/gen_pnpids.sh b/backend/drm/gen_pnpids.sh
new file mode 100755
index 00000000..80e5542d
--- /dev/null
+++ b/backend/drm/gen_pnpids.sh
@@ -0,0 +1,27 @@
+#!/bin/sh -eu
+#
+# usage: gen_pnpids.sh < pnp.ids > pnpids.c
+
+gen_pnps()
+{
+	while read -r id vendor; do
+		[ "${#id}" = 3 ] || exit 1
+
+		printf "\tcase PNP_ID('%c', '%c', '%c'): return \"%s\";\n" \
+			"$id" "${id#?}" "${id#??}" "$vendor"
+	done
+}
+
+cat << EOF
+#include <stdint.h>
+#include <stddef.h>
+#include "backend/drm/util.h"
+#define PNP_ID(a, b, c) ((a & 0x1f) << 10) | ((b & 0x1f) << 5) | (c & 0x1f)
+const char *get_pnp_manufacturer(uint16_t code) {
+	switch (code) {
+$(gen_pnps)
+	}
+	return NULL;
+}
+#undef PNP_ID
+EOF
diff --git a/backend/drm/meson.build b/backend/drm/meson.build
index cc791f36..7bde50c2 100644
--- a/backend/drm/meson.build
+++ b/backend/drm/meson.build
@@ -1,3 +1,21 @@
+hwdata = dependency('hwdata', required: false, native: true)
+if hwdata.found()
+	hwdata_dir = hwdata.get_variable(pkgconfig: 'pkgdatadir')
+	pnp_ids = files(hwdata_dir / 'pnp.ids')
+else
+	pnp_ids = files('/usr/share/hwdata/pnp.ids')
+endif
+
+pnpids_c = custom_target(
+	'pnpids.c',
+	output: 'pnpids.c',
+	input: pnp_ids,
+	feed: true,
+	capture: true,
+	command: files('gen_pnpids.sh'),
+)
+wlr_files += pnpids_c
+
 wlr_files += files(
 	'atomic.c',
 	'backend.c',
diff --git a/backend/drm/util.c b/backend/drm/util.c
index 0f424416..03c4b0b4 100644
--- a/backend/drm/util.c
+++ b/backend/drm/util.c
@@ -48,82 +48,6 @@ enum wlr_output_mode_aspect_ratio get_picture_aspect_ratio(const drmModeModeInfo
 	}
 }
 
-// Constructed from http://edid.tv/manufacturer
-static const char *get_manufacturer(uint16_t id) {
-#define ID(a, b, c) ((a & 0x1f) << 10) | ((b & 0x1f) << 5) | (c & 0x1f)
-	switch (id) {
-	case ID('A', 'A', 'A'): return "Avolites Ltd";
-	case ID('A', 'C', 'I'): return "Ancor Communications Inc";
-	case ID('A', 'C', 'R'): return "Acer Technologies";
-	case ID('A', 'D', 'A'): return "Addi-Data GmbH";
-	case ID('A', 'P', 'P'): return "Apple Computer Inc";
-	case ID('A', 'S', 'K'): return "Ask A/S";
-	case ID('A', 'V', 'T'): return "Avtek (Electronics) Pty Ltd";
-	case ID('B', 'N', 'O'): return "Bang & Olufsen";
-	case ID('B', 'N', 'Q'): return "BenQ Corporation";
-	case ID('C', 'M', 'N'): return "Chimei Innolux Corporation";
-	case ID('C', 'M', 'O'): return "Chi Mei Optoelectronics corp.";
-	case ID('C', 'R', 'O'): return "Extraordinary Technologies PTY Limited";
-	case ID('D', 'E', 'L'): return "Dell Inc.";
-	case ID('D', 'G', 'C'): return "Data General Corporation";
-	case ID('D', 'O', 'N'): return "DENON, Ltd.";
-	case ID('E', 'N', 'C'): return "Eizo Nanao Corporation";
-	case ID('E', 'P', 'H'): return "Epiphan Systems Inc.";
-	case ID('E', 'X', 'P'): return "Data Export Corporation";
-	case ID('F', 'N', 'I'): return "Funai Electric Co., Ltd.";
-	case ID('F', 'U', 'S'): return "Fujitsu Siemens Computers GmbH";
-	case ID('G', 'S', 'M'): return "Goldstar Company Ltd";
-	case ID('H', 'I', 'Q'): return "Kaohsiung Opto Electronics Americas, Inc.";
-	case ID('H', 'S', 'D'): return "HannStar Display Corp";
-	case ID('H', 'T', 'C'): return "Hitachi Ltd";
-	case ID('H', 'W', 'P'): return "Hewlett Packard";
-	case ID('I', 'N', 'T'): return "Interphase Corporation";
-	case ID('I', 'N', 'X'): return "Communications Supply Corporation (A division of WESCO)";
-	case ID('I', 'T', 'E'): return "Integrated Tech Express Inc";
-	case ID('I', 'V', 'M'): return "Iiyama North America";
-	case ID('L', 'E', 'N'): return "Lenovo Group Limited";
-	case ID('M', 'A', 'X'): return "Rogen Tech Distribution Inc";
-	case ID('M', 'E', 'G'): return "Abeam Tech Ltd";
-	case ID('M', 'E', 'I'): return "Panasonic Industry Company";
-	case ID('M', 'T', 'C'): return "Mars-Tech Corporation";
-	case ID('M', 'T', 'X'): return "Matrox";
-	case ID('N', 'E', 'C'): return "NEC Corporation";
-	case ID('N', 'E', 'X'): return "Nexgen Mediatech Inc.";
-	case ID('O', 'N', 'K'): return "ONKYO Corporation";
-	case ID('O', 'R', 'N'): return "ORION ELECTRIC CO., LTD.";
-	case ID('O', 'T', 'M'): return "Optoma Corporation";
-	case ID('O', 'V', 'R'): return "Oculus VR, Inc.";
-	case ID('P', 'H', 'L'): return "Philips Consumer Electronics Company";
-	case ID('P', 'I', 'O'): return "Pioneer Electronic Corporation";
-	case ID('P', 'N', 'R'): return "Planar Systems, Inc.";
-	case ID('Q', 'D', 'S'): return "Quanta Display Inc.";
-	case ID('R', 'A', 'T'): return "Rent-A-Tech";
-	case ID('R', 'E', 'N'): return "Renesas Technology Corp.";
-	case ID('S', 'A', 'M'): return "Samsung Electric Company";
-	case ID('S', 'A', 'N'): return "Sanyo Electric Co., Ltd.";
-	case ID('S', 'E', 'C'): return "Seiko Epson Corporation";
-	case ID('S', 'H', 'P'): return "Sharp Corporation";
-	case ID('S', 'I', 'I'): return "Silicon Image, Inc.";
-	case ID('S', 'N', 'Y'): return "Sony";
-	case ID('S', 'T', 'D'): return "STD Computer Inc";
-	case ID('S', 'V', 'S'): return "SVSI";
-	case ID('S', 'Y', 'N'): return "Synaptics Inc";
-	case ID('T', 'C', 'L'): return "Technical Concepts Ltd";
-	case ID('T', 'O', 'P'): return "Orion Communications Co., Ltd.";
-	case ID('T', 'S', 'B'): return "Toshiba America Info Systems Inc";
-	case ID('T', 'S', 'T'): return "Transtream Inc";
-	case ID('U', 'N', 'K'): return "Unknown";
-	case ID('V', 'E', 'S'): return "Vestel Elektronik Sanayi ve Ticaret A. S.";
-	case ID('V', 'I', 'T'): return "Visitech AS";
-	case ID('V', 'I', 'Z'): return "VIZIO, Inc";
-	case ID('V', 'L', 'V'): return "Valve";
-	case ID('V', 'S', 'C'): return "ViewSonic Corporation";
-	case ID('Y', 'M', 'H'): return "Yamaha Corporation";
-	default:                return "Unknown";
-	}
-#undef ID
-}
-
 /* See https://en.wikipedia.org/wiki/Extended_Display_Identification_Data for layout of EDID data.
  * We don't parse the EDID properly. We just expect to receive valid data.
  */
@@ -142,7 +66,17 @@ void parse_edid(struct wlr_drm_connector *conn, size_t len, const uint8_t *data)
 	}
 
 	uint16_t id = (data[8] << 8) | data[9];
-	output->make = strdup(get_manufacturer(id));
+	const char *manu = get_pnp_manufacturer(id);
+	char pnp_id[4];
+	if (!manu) {
+		// The ASCII 3-letter manufacturer PnP ID is encoded in 5-bit codes
+		pnp_id[0] = ((id >> 10) & 0x1F) + '@';
+		pnp_id[1] = ((id >> 5) & 0x1F) + '@';
+		pnp_id[2] = ((id >> 0) & 0x1F) + '@';
+		pnp_id[3] = '\0';
+		manu = pnp_id;
+	}
+	output->make = strdup(manu);
 
 	uint16_t model = data[10] | (data[11] << 8);
 	char model_str[32];
diff --git a/include/backend/drm/util.h b/include/backend/drm/util.h
index 0d4c3d74..9bfe5f8a 100644
--- a/include/backend/drm/util.h
+++ b/include/backend/drm/util.h
@@ -10,6 +10,8 @@ struct wlr_drm_connector;
 // Calculates a more accurate refresh rate (mHz) than what mode itself provides
 int32_t calculate_refresh_rate(const drmModeModeInfo *mode);
 enum wlr_output_mode_aspect_ratio get_picture_aspect_ratio(const drmModeModeInfo *mode);
+// Returns manufacturer based on pnp id
+const char *get_pnp_manufacturer(uint16_t code);
 // Populates the make/model/phys_{width,height} of output from the edid data
 void parse_edid(struct wlr_drm_connector *conn, size_t len, const uint8_t *data);
 
-- 
cgit v1.2.3