summaryrefslogtreecommitdiffstats
path: root/virtual-programs
diff options
context:
space:
mode:
authorOmar Rizwan <omar@omar.website>2024-01-30 18:27:22 +0000
committerOmar Rizwan <omar@omar.website>2024-01-30 18:27:22 +0000
commit2ce4c2beff75773514796542e422ff9c1edffa04 (patch)
tree0e22d53b5c80721457ec2ea634b5afd2373c1716 /virtual-programs
parentMerge pull request #86 from FolkComputer/ac/editor (diff)
downloadfolk-2ce4c2beff75773514796542e422ff9c1edffa04.tar.gz
folk-2ce4c2beff75773514796542e422ff9c1edffa04.zip
Start on Web UI for identifying & printing connected keyboards
Diffstat (limited to 'virtual-programs')
-rw-r--r--virtual-programs/keyboard.folk6
-rw-r--r--virtual-programs/web-keyboards.folk52
2 files changed, 53 insertions, 5 deletions
diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk
index 9746af91..fe1730e5 100644
--- a/virtual-programs/keyboard.folk
+++ b/virtual-programs/keyboard.folk
@@ -1,9 +1,5 @@
set ::debug_keyboard true
-proc udevadmProperties {device} {
- return [exec udevadm info --query=property --name=$device]
-}
-
proc establishKeyPressListener {keyboard} {
set kb [open $keyboard r]
fconfigure $kb -translation binary
@@ -12,7 +8,7 @@ proc establishKeyPressListener {keyboard} {
# Function to check if the device is a keyboard
proc isKeyboard {device} {
- set properties [udevadmProperties $device]
+ set properties [exec udevadm info --query=property --name=$device]
if {$properties eq ""} {
return false
}
diff --git a/virtual-programs/web-keyboards.folk b/virtual-programs/web-keyboards.folk
new file mode 100644
index 00000000..a3a4435d
--- /dev/null
+++ b/virtual-programs/web-keyboards.folk
@@ -0,0 +1,52 @@
+When the keyboards are /keyboards/ {
+ Wish the web server handles route "/keyboards$" with handler [list apply {{keyboards} {
+ upvar ^html ^html
+ html [subst {
+ <html>
+ <body>
+ <span id="status">Status</span>
+ <dl>
+ [join [lmap kb $keyboards { subst {
+ <dt>$kb</dt>
+ <dd>
+ <pre id="$kb-presses"></pre><br>
+ <button>Print</button>
+ </dd>
+ } }] "\n"]
+ </dl>
+
+ <script>
+ function wsConnect() {
+ ws = new WebSocket(window.location.origin.replace("http", "ws") + "/ws");
+ send = function(s) { ws.send(s); }
+
+ ws.onopen = () => {
+ document.getElementById('status').innerHTML = "<span style=background-color:seagreen;color:white;>Connnected</span>";
+ send(`
+ Assert when keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ timestamp /ts/ {{chan kb k t m ts} {
+ ::websocket::send \$chan text "\$kb||\$k"
+ }} with environment \[list \$chan]
+ `);
+ };
+ ws.onclose = window.onbeforeunload = () => {
+ document.getElementById('status').innerHTML = "<span style=background-color:red;color:white;>Disconnnected</span>";
+ send(`Retract when keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ timestamp /ts/ /anything/ with environment \[list \$chan]`)
+ setTimeout(() => { wsConnect(); }, 1000);
+ };
+ ws.onerror = (err) => {
+ document.getElementById('status').innerText = "Error";
+ console.error('Socket encountered error: ', err.message, 'Closing socket');
+ ws.close();
+ }
+ ws.onmessage = (msg) => {
+ const \[kb, key] = msg.data.split("||");
+ document.getElementById(kb + "-presses").innerText += key;
+ }
+ };
+ wsConnect();
+ </script>
+ </body>
+ </html>
+ }]
+ }} $keyboards]
+}