From 3c8d84c46d9f97f62c7245e00a70db10677b852c Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Mon, 26 Aug 2024 12:02:35 +0200 Subject: [PATCH] bridge browser js execution via device /dev/browser/js --- com/isoterminal.js | 47 +++++++++++++++++++++++----- com/isoterminal/mnt/alert | 2 ++ com/isoterminal/mnt/boot | 5 --- com/isoterminal/mnt/confirm | 10 ++++++ com/isoterminal/mnt/dev/browser/html | 0 com/isoterminal/mnt/dev/browser/js | 1 + com/isoterminal/mnt/hello.txt | 1 - com/isoterminal/mnt/js | 5 +++ com/isoterminal/mnt/jsh | 17 ++++++++++ com/isoterminal/mnt/motd | 15 +++++++++ com/isoterminal/mnt/profile | 29 +++++++++++++++++ com/isoterminal/mnt/prompt | 9 ++++++ com/isoterminal/mnt/test.awk | 26 +++++++++++++++ com/require.js | 6 ++-- com/window.js | 5 ++- 15 files changed, 161 insertions(+), 17 deletions(-) create mode 100755 com/isoterminal/mnt/alert delete mode 100644 com/isoterminal/mnt/boot create mode 100755 com/isoterminal/mnt/confirm create mode 100644 com/isoterminal/mnt/dev/browser/html create mode 100644 com/isoterminal/mnt/dev/browser/js delete mode 100644 com/isoterminal/mnt/hello.txt create mode 100755 com/isoterminal/mnt/js create mode 100755 com/isoterminal/mnt/jsh create mode 100644 com/isoterminal/mnt/motd create mode 100644 com/isoterminal/mnt/profile create mode 100755 com/isoterminal/mnt/prompt create mode 100755 com/isoterminal/mnt/test.awk diff --git a/com/isoterminal.js b/com/isoterminal.js index ca77e3c..bd9e385 100644 --- a/com/isoterminal.js +++ b/com/isoterminal.js @@ -44,9 +44,6 @@ AFRAME.registerComponent('isoterminal', { background: #000c; overflow:hidden; } - body .winbox .wb-header{ - background: linear-gradient(45deg, var(--xrsh-primary), var(--xrsh-third) ) - } .isoterminal div{ display:block; } .isoterminal span{ display: inline } @@ -155,9 +152,13 @@ AFRAME.registerComponent('isoterminal', { motd += "\033[0m" const files = [ - "com/isoterminal/mnt/gui", - "com/isoterminal/mnt/boot", - "com/isoterminal/mnt/edit", + "com/isoterminal/mnt/js", + "com/isoterminal/mnt/jsh", + "com/isoterminal/mnt/confirm", + "com/isoterminal/mnt/prompt", + "com/isoterminal/mnt/alert", + "com/isoterminal/mnt/profile", + "com/isoterminal/mnt/motd", ] emulator.bus.register("emulator-started", async () => { @@ -212,7 +213,19 @@ AFRAME.registerComponent('isoterminal', { //console.dir({line,new_line}) if( !ready && line.match(/^(\/ #|~%)/) ){ instance.dom.classList.remove('blink') - emulator.serial0_send("source /mnt/boot\n") + // set environment + let env = ['export BROWSER=1'] + for ( let i in document.location ){ + if( typeof document.location[i] == 'string' ) + env.push( 'export '+String(i).toUpperCase()+'="'+document.location[i]+'"') + } + env.map( (e) => emulator.serial0_send(`echo '${e}' >> /mnt/profile\n`) ) + let boot = `source /mnt/profile` + // exec hash as extra boot cmd + if( document.location.hash.length > 1 ){ + boot += `&& cmd='${decodeURI(document.location.hash.substr(1))}' && $cmd` + } + emulator.serial0_send(boot+"\n") instance.winbox.maximize() emulator.serial_adapter.term.focus() ready = true @@ -220,6 +233,24 @@ AFRAME.registerComponent('isoterminal', { //emulator.serial0_send("mv /mnt/js . && chmod +x js\n") } }); + + // unix to js device + emulator.add_listener("9p-write-end", async (opts) => { + const decoder = new TextDecoder('utf-8'); + + if ( opts[0] == 'js' ){ + const buf = await emulator.read_file("dev/browser/js") + const script = decoder.decode(buf) + try{ + let res = (new Function(`return ${script}`))() + if( res && typeof res != 'string' ) res = JSON.stringify(res,null,2) + emulator.create_file( "dev/browser/js", this.toUint8Array( res || '' ) ) + }catch(e){ + console.dir(e) + emulator.create_file("dev/browser/js", this.toUint8Array( `[e] `+String(e.stack) ) ) + } + } + }) }); @@ -266,7 +297,7 @@ AFRAME.registerComponent('isoterminal', { if( instance.dom.emulator && instance.dom.emulator.serial_adapter ){ setTimeout( () => { this.autoResize(instance.dom.emulator.serial_adapter.term,instance,-5) - },1000) // wait for resize anim + },500) // wait for resize anim } } instance.addEventListener('window.onresize', resize ) diff --git a/com/isoterminal/mnt/alert b/com/isoterminal/mnt/alert new file mode 100755 index 0000000..530b6c8 --- /dev/null +++ b/com/isoterminal/mnt/alert @@ -0,0 +1,2 @@ +#!/bin/sh +jsh alert "$1" diff --git a/com/isoterminal/mnt/boot b/com/isoterminal/mnt/boot deleted file mode 100644 index 79c5f99..0000000 --- a/com/isoterminal/mnt/boot +++ /dev/null @@ -1,5 +0,0 @@ -chmod +x /mnt/gui /mnt/js -clear -cat /mnt/motd -export PATH=$PATH:/mnt -export PS1="\nxrsh $ \033[0m" diff --git a/com/isoterminal/mnt/confirm b/com/isoterminal/mnt/confirm new file mode 100755 index 0000000..c10d2c4 --- /dev/null +++ b/com/isoterminal/mnt/confirm @@ -0,0 +1,10 @@ +#!/bin/sh +test -f /mnt/V86 && { + jsh confirm $1 $2 +} + +test -f /mnt/V86 || { + read -p "$(printf "\033[0m")[?] $1 [y/n] $(printf "\033[0m")" y + test $y = y && echo true && exit + echo false +} diff --git a/com/isoterminal/mnt/dev/browser/html b/com/isoterminal/mnt/dev/browser/html new file mode 100644 index 0000000..e69de29 diff --git a/com/isoterminal/mnt/dev/browser/js b/com/isoterminal/mnt/dev/browser/js new file mode 100644 index 0000000..508fd31 --- /dev/null +++ b/com/isoterminal/mnt/dev/browser/js @@ -0,0 +1 @@ +alert("hello") diff --git a/com/isoterminal/mnt/hello.txt b/com/isoterminal/mnt/hello.txt deleted file mode 100644 index ce01362..0000000 --- a/com/isoterminal/mnt/hello.txt +++ /dev/null @@ -1 +0,0 @@ -hello diff --git a/com/isoterminal/mnt/js b/com/isoterminal/mnt/js new file mode 100755 index 0000000..7cfbf53 --- /dev/null +++ b/com/isoterminal/mnt/js @@ -0,0 +1,5 @@ +#!/bin/sh +flock /dev/browser/js -c "echo '$*' > /dev/browser/js; cat /dev/browser/js" + +# we use flock, an awesome way to make processes read/write the same file +# whilr preventing 1001 concurrency issues diff --git a/com/isoterminal/mnt/jsh b/com/isoterminal/mnt/jsh new file mode 100755 index 0000000..5764221 --- /dev/null +++ b/com/isoterminal/mnt/jsh @@ -0,0 +1,17 @@ +#!/bin/sh +# usage: jsh [arg1] [arg2] ... +# +# 'jsh prompt question answer' executes: js prompt('question','answer') ) + +to_js(){ + printf "%s(" "$1" + shift + for arg in "$@"; do + printf '"%s",' "$arg" + done + printf ")\n" +} + +func=$(to_js "$@") +func=${func/,)/)} +/mnt/js "$func" diff --git a/com/isoterminal/mnt/motd b/com/isoterminal/mnt/motd new file mode 100644 index 0000000..7831350 --- /dev/null +++ b/com/isoterminal/mnt/motd @@ -0,0 +1,15 @@ + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + . . ____ _____________ ________. ._. ._. . . . . . . . . + . . _\ \/ /\______ \/ _____// | \. . . . . . . . + . . _ \ / | _/\_____ \/ ~ \ . . . . . . . + _ _ / \ | | \/ \ Y / _ _ _ _ _ _ _ + . . /___/\ \ |____|_ /_______ /\___|_ /. . . . . . . . + . . . . . .\_/. . . . \/ . . . .\/ . . _ \/ . . . . . . . . + ▬ ▬ ▬ https://xrsh.isvery.ninja ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + Open, local-first, hackable & selfcontained XR apps. + + credits: all FOSS devs | @lvk@mastodon.online + copy.sh (v86) | @utopiah@mastodon.pirateparty.be + diff --git a/com/isoterminal/mnt/profile b/com/isoterminal/mnt/profile new file mode 100644 index 0000000..7d7405b --- /dev/null +++ b/com/isoterminal/mnt/profile @@ -0,0 +1,29 @@ +install_xrsh(){ + chmod +x /mnt/confirm /mnt/prompt /mnt/alert /mnt/js* + + setup_browser_dev(){ + mkdir -p /mnt/dev/browser + touch /mnt/dev/browser/js + touch /mnt/dev/browser/html + ln -s /mnt/dev/browser /dev/browser + test -f /etc/profile && rm /etc/profile + ln -s /mnt/profile /etc/profile + } + + setup_browser_dev +} + +test -d /dev/browser || install_xrsh + +test -f /mnt/V86 && { + mount -a + udhcpc 1>>/var/log/network.log 2>>/var/log/network.log & + echo 0 > /proc/sys/kernel/printk +} + +resize +#clear +cat /mnt/motd +export PATH=$PATH:/mnt +export PS1="\nxrsh # \033[0m" + diff --git a/com/isoterminal/mnt/prompt b/com/isoterminal/mnt/prompt new file mode 100755 index 0000000..dff4c78 --- /dev/null +++ b/com/isoterminal/mnt/prompt @@ -0,0 +1,9 @@ +#!/bin/sh +test -f /mnt/V86 && { + jsh prompt "$1" "$2" +} + +test -f /mnt/V86 || { + read -p "$(printf "\033[0m")[?] $1: $(printf "\033[0m")" answer + echo "$answer" +} diff --git a/com/isoterminal/mnt/test.awk b/com/isoterminal/mnt/test.awk new file mode 100755 index 0000000..8f8a47e --- /dev/null +++ b/com/isoterminal/mnt/test.awk @@ -0,0 +1,26 @@ +#!/bin/awk -f + +BEGIN { + for (i = 1; i < ARGC; i++) { + options[i] = ARGV[i] + } + ARGC = 0 + selected = 1 + n = length(options) + + while (1) { + printf "\r " + for (i = 1; i <= n; i++) { + if (i == selected) + printf "\033[44m%s\033[0m ", options[i] + else + printf "%s ", options[i] + } + if (c == 0) { + getline dir < "/dev/stdin" + print dir + if (dir == "up" && selected > 1) selected-- + if (dir == "down" && selected < n) selected++ + } + } +} diff --git a/com/require.js b/com/require.js index e9e30c5..4e16fc9 100644 --- a/com/require.js +++ b/com/require.js @@ -34,7 +34,8 @@ AFRAME.utils.require = function(arr_or_obj,opts){ if( AFRAME.required[id] ) return // already loaded before AFRAME.required[id] = true - if( !document.body.querySelector(`script#${id}`) ){ + if( !document.body.querySelector(`script#${id}`) && + !document.body.querySelector(`link#${id}`) ){ let {type} = parseURI(package) let p = new Promise( (resolve,reject) => { switch(type){ @@ -49,8 +50,9 @@ AFRAME.utils.require = function(arr_or_obj,opts){ link.id = id link.href = package link.rel = 'stylesheet' + link.onload = () => setTimeout( () => resolve(id), 50 ) + link.onerror = (e) => reject(e) document.body.appendChild(link) - resolve(id) break; } }) diff --git a/com/window.js b/com/window.js index 8ae21b4..e53b485 100644 --- a/com/window.js +++ b/com/window.js @@ -14,7 +14,7 @@ AFRAME.registerComponent('window', { dom: "com/dom.js", html: "https://unpkg.com/aframe-htmlmesh@2.1.0/build/aframe-html.js", // html to AFRAME winboxjs: "https://unpkg.com/winbox@0.2.82/dist/winbox.bundle.min.js", // deadsimple windows: https://nextapps-de.github.io/winbox - winboxcss: "https://unpkg.com/winbox@0.2.82/dist/css/winbox.min.css", // + //winboxcss: "https://unpkg.com/winbox@0.2.82/dist/css/winbox.min.css", // main theme }, init: function(){ @@ -52,6 +52,9 @@ AFRAME.registerComponent('window', { this.el.emit('window.onclose',e) if( e.halt ) return true this.data.dom.style.display = 'none'; + this.data.dom.parentElement.remove() + debugger + this.el.parentElement.remove( this.el ) return false }, });