Skip to contentSkip to conversation

edid-decode: add function to enable usage as a library

PATCH New Ilia Mirkin 1 participant 2 messages Feb 13, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 edid-decode: add function to enable usage as a library

This is helpful for an emscripten setup, as there's no great way to reinitialize the object from scratch.

---

v2: rebase, move opening brace to new line

 edid-decode.cpp | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
edid-decode.cpp +19 -0
index 47135a9..0473bbf 100644
@@ -1214,3 +1214,22 @@ int main(int argc, char **argv)
 
 	return ret ? ret : state.parse_edid();
 }
+
+#ifdef __EMSCRIPTEN__
+/*
+ * The surrounding JavaScript implementation will call this function
+ * each time it wants to decode an EDID. So this should reset all the
+ * state and start over.
+ */
+extern "C" int parse_edid(const char *input)
+{
+	for (unsigned i = 0; i < EDID_MAX_BLOCKS + 1; i++) {
+		s_msgs[i][0].clear();
+		s_msgs[i][1].clear();
+	}
+	options[OptCheck] = 1;
+	state = edid_state();
+	int ret = edid_from_file(input);
+	return ret ? ret : state.parse_edid();
+}
+#endif
Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 2/2 edid-decode: add build rule for emscripten output

All I can really say is that this works in my current setup. This current setup was not the first setup I tried, with lots of random flailing around emscripten and binaryen versions to be compatible with my LLVM version. Perhaps later emscripten needs slightly different flags to export functions, who knows.

---
 Makefile | 5 +++++
 1 file changed, 5 insertions(+)
Makefile +5 -0
index 6db6386..49ad39b 100644
@@ -1,6 +1,8 @@
 bindir ?= /usr/bin
 mandir ?= /usr/share/man
 
+EMXX ?= em++
+
 SOURCES = edid-decode.cpp parse-base-block.cpp parse-cta-block.cpp \
 	  parse-displayid-block.cpp parse-ls-ext-block.cpp \
 	  parse-di-ext-block.cpp parse-vtb-ext-block.cpp
@@ -13,6 +15,9 @@ sha = -DSHA=$(shell if [ -d .git ]; then git rev-parse HEAD ; else printf '"not
 edid-decode: $(SOURCES) edid-decode.h Makefile
 	$(CXX) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(WARN_FLAGS) -g $(sha) -o $@ $(SOURCES) -lm
 
+edid-decode.js: $(SOURCES) edid-decode.h Makefile
+	$(EMXX) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(WARN_FLAGS) $(sha) -s EXPORTED_FUNCTIONS='["_parse_edid"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -o $@ $(SOURCES) -lm
+
 clean:
 	rm -f edid-decode
 
Signature · 1 line
2.24.1