]> git.eli173.com Git - pwa-c/commitdiff
minimal functional example
authorElijah Cohen <eli@eli173.com>
Wed, 1 Jan 2025 18:39:02 +0000 (12:39 -0600)
committerElijah Cohen <eli@eli173.com>
Wed, 1 Jan 2025 21:39:18 +0000 (15:39 -0600)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
icon.png [new file with mode: 0644]
main.c [new file with mode: 0644]
manifest.json [new file with mode: 0644]
shell.html [new file with mode: 0644]
sw.js [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..bdb90ee
--- /dev/null
@@ -0,0 +1 @@
+/outdir/
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..1b460f3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+
+
+all: main.c shell.html
+       mkdir -p outdir
+       emcc main.c --shell-file shell.html -o outdir/index.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']"
+       cp manifest.json outdir
+       cp sw.js outdir
+       cp icon.png outdir
diff --git a/icon.png b/icon.png
new file mode 100644 (file)
index 0000000..6dfd1bd
Binary files /dev/null and b/icon.png differ
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..f6a6a50
--- /dev/null
+++ b/main.c
@@ -0,0 +1,33 @@
+#ifdef __EMSCRIPTEN__
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <emscripten/emscripten.h>
+
+int main() {
+       printf("hello, world!\ntell me something\n");
+       return 0;
+}
+
+void reverse(char* s) {
+       char* start = s;
+       char* end = s - 1+ strlen(s);
+       char tmp;
+       while(start < end) {
+               tmp = *start;
+               *start = *end;
+               *end = tmp;
+               start++;
+               end--;
+       }
+}
+
+EMSCRIPTEN_KEEPALIVE char* do_this(char* in) {
+       char* out = strdup(in);
+       reverse(out);
+       return out;
+}
+
+
+#endif
diff --git a/manifest.json b/manifest.json
new file mode 100644 (file)
index 0000000..f84a778
--- /dev/null
@@ -0,0 +1,16 @@
+{
+  "background_color": "#8E5DCE",
+  "description": "minimal example",
+  "display": "fullscreen",
+  "icons": [
+    {
+      "src": "icon.png",
+      "sizes": "512x512",
+      "type": "image/png"
+    }
+  ],
+  "name": "min-ex",
+  "short_name": "min-ex",
+  "start_url": "."
+}
+
diff --git a/shell.html b/shell.html
new file mode 100644 (file)
index 0000000..39613a7
--- /dev/null
@@ -0,0 +1,59 @@
+<!doctype html>
+<html lang="en-us">
+  <head>
+    <meta charset="utf-8">
+               <meta name="viewport" content="width=device-width, initial-scale=1">
+               <link rel="manifest" href="manifest.json" />
+               <link rel="shortcut icon" type="image/jpg" href="icon.png"/>
+    <title>minmal example</title>
+  </head>
+  <body>
+                       <div id="outzone"></div>
+                       <form id="inform">
+                               <input id="in" type="text" autocorrect="off" autocapitalize="off"></input>
+                       </form>
+               <script type='text/javascript'>
+                       var Module = {
+                                       print: (function() {
+                                                       var element = document.getElementById('outzone');
+                                                       if (element) element.textContent = '';
+                                                       return (...args) => {
+                                                                       var text = args.join(' ');
+                                                                       console.log(text);
+                                                                       if (element) {
+                                                                                       let printelt = document.createElement('div');
+                                                                                       printelt.textContent += text + "\n";
+                                                                                       element.appendChild(printelt);
+                                                                       }
+                                                       };
+                                       })(),
+                       };
+                       document.getElementById("inform").addEventListener("submit", e => {
+                                       e.preventDefault();
+                                       let outzone = document.getElementById("outzone");
+                                       let inelt = document.createElement('div');
+                                       let input = e.target[0].value
+                                       inelt.textContent += "<- " + input + "\n"
+                                       outzone.appendChild(inelt);
+                                       let outelt = document.createElement('div');
+                                       let output = Module.ccall("do_this", 'string', ['string'], [input])
+                                       outelt.textContent += " -> " + output + "\n"
+                                       outzone.appendChild(outelt);
+                                       let prompt = document.createElement('div');
+                                       prompt.textContent = "tell me something else: ";
+                                       outzone.appendChild(prompt);
+                                       document.getElementById("in").value = "";
+                       });
+
+                       if('serviceWorker' in navigator) {
+                                       console.log('registering');
+                                       navigator.serviceWorker.register('./sw.js')
+                                                       .then(() => { console.log('sw reg'); });
+                       }
+                       else {
+                                       console.log('no sw in nav');
+                       }
+                       </script>
+               {{{ SCRIPT }}}
+       </body>
+</html>
diff --git a/sw.js b/sw.js
new file mode 100644 (file)
index 0000000..b00b84b
--- /dev/null
+++ b/sw.js
@@ -0,0 +1,17 @@
+self.addEventListener('install', (event) => {
+               event.waitUntil(
+                               caches.open('minex').then((cache) => cache.addAll([
+                                               './',
+                                               './index.html',
+                                               './icon.png',
+                                               './index.js',
+                                               './index.wasm',
+                               ]))
+               )});
+
+self.addEventListener('fetch', (event) => {
+               event.respondWith(
+                               caches.match(event.request).then((response) => response || fetch(event.request))
+               );
+});
+