From ab0a88364c1cf29c492d648b0e0db24038ca9979 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 00:16:05 -0300 Subject: [PATCH 1/9] Embedded clojurust nREPL behind a `repl` feature FOSFORA_NREPL= (0 = any free port) starts a clojurust interpreter on a `cljrs-repl` thread with an nREPL server on 127.0.0.1, writing .nrepl-port so CIDER/Calva/Conjure auto-connect. `(require '[fosfora :as f])` is preloaded. Commands reuse the web remote's vocabulary: the `fosfora` namespace pr-strs a message map, `fosfora.native/send-edn` reads it with cljrs-reader, converts it to JSON and runs web::client::parse_client_message, so the REPL and the WebSocket remote cannot drift apart. Messages go through a WebSystem queue that outlives server restarts and is drained with the network queue in update(). Reads come from the snapshots the web system already builds (full state and audio at 10 Hz), kept fresh while the REPL is attached even with no client. `f/put-trama!` writes a trama effect file for the hot-reload watcher. clojurust values are !Send, so the runtime lives on its own thread and is driven by cljrs_nrepl::Poller; its per-thread GC never pauses rendering. Interpreter only: no JIT and no git-deps tree. SPIKE: cljrs crates are relative path deps resolved from this worktree, because the clojurust HEAD in use is unpushed. Switch to a pinned git rev before merge. Verified live: load-effect!, set-param! (read back from f/state), error on an unknown message, and CIDER eval + completion over the embedded server. --- .gitignore | 3 + Cargo.lock | 515 +++++++++++++++++++++- crates/fosfora-app/Cargo.toml | 22 + crates/fosfora-app/src/lib.rs | 2 + crates/fosfora-app/src/repl/fosfora.cljrs | 75 ++++ crates/fosfora-app/src/repl/mod.rs | 394 +++++++++++++++++ crates/fosfora-app/src/web/client.rs | 5 +- crates/fosfora-app/src/web/local.rs | 57 +++ crates/fosfora-app/src/web/mod.rs | 44 +- 9 files changed, 1096 insertions(+), 21 deletions(-) create mode 100644 crates/fosfora-app/src/repl/fosfora.cljrs create mode 100644 crates/fosfora-app/src/repl/mod.rs create mode 100644 crates/fosfora-app/src/web/local.rs diff --git a/.gitignore b/.gitignore index 15f2260e..26f9fbff 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ __pycache__/ /bench/out/ /bench/*.mp3 /bench/*.signal.jsonl + +# Embedded nREPL (repl feature) writes this in the working directory +.nrepl-port diff --git a/Cargo.lock b/Cargo.lock index 210d9a11..66750c0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,6 +195,24 @@ dependencies = [ "x11rb", ] +[[package]] +name = "arc-swap" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db4c94f57a570fce5d381be3289b3f79aef6332d72948fe82ab5772e423ccf92" +dependencies = [ + "rustversion", +] + +[[package]] +name = "archery" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca55ee147b1926dbea904f50fe4902494e97bc742205abbbf10c709e43815f" +dependencies = [ + "triomphe", +] + [[package]] name = "arrayref" version = "0.3.9" @@ -222,6 +240,15 @@ dependencies = [ "libloading 0.8.9", ] +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -246,6 +273,19 @@ version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" +[[package]] +name = "bigdecimal" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fa3f3d8cbf4dffcfe4991de61d012bef509a409ecbe9dd41049bfe32b4d4653" +dependencies = [ + "autocfg", + "libm", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "bindgen" version = "0.65.1" @@ -489,6 +529,30 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if 1.0.4", + "cpufeatures 0.3.1", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link 0.2.1", +] + [[package]] name = "clang-sys" version = "1.8.1" @@ -509,6 +573,120 @@ dependencies = [ "error-code", ] +[[package]] +name = "cljrs-gc" +version = "0.1.0" +dependencies = [ + "bigdecimal", + "num-bigint", + "num-rational", + "system-memory", + "tracing", +] + +[[package]] +name = "cljrs-ir" +version = "0.1.0" +dependencies = [ + "cljrs-reader", + "cljrs-types", + "postcard", + "serde", +] + +[[package]] +name = "cljrs-nrepl" +version = "0.1.0" +dependencies = [ + "cljrs-gc", + "cljrs-reader", + "cljrs-runtime", + "cljrs-value", + "miette", + "thiserror 2.0.21", + "tokio", +] + +[[package]] +name = "cljrs-project" +version = "0.1.0" +dependencies = [ + "cljrs-gc", + "cljrs-reader", + "cljrs-types", + "thiserror 2.0.21", +] + +[[package]] +name = "cljrs-reader" +version = "0.1.0" +dependencies = [ + "cljrs-types", + "miette", +] + +[[package]] +name = "cljrs-runtime" +version = "0.1.0" +dependencies = [ + "bigdecimal", + "cljrs-gc", + "cljrs-ir", + "cljrs-project", + "cljrs-reader", + "cljrs-types", + "cljrs-value", + "num-bigint", + "num-rational", + "num-traits", + "rand 0.10.3", + "rpds", + "thiserror 2.0.21", + "tracing", + "tracing-subscriber", + "uuid", +] + +[[package]] +name = "cljrs-stdlib" +version = "0.1.0" +dependencies = [ + "chrono", + "cljrs-gc", + "cljrs-ir", + "cljrs-reader", + "cljrs-runtime", + "cljrs-value", + "uuid", +] + +[[package]] +name = "cljrs-types" +version = "0.1.0" +dependencies = [ + "miette", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "cljrs-value" +version = "0.1.0" +dependencies = [ + "arc-swap", + "bigdecimal", + "cljrs-gc", + "cljrs-reader", + "cljrs-types", + "num-bigint", + "num-rational", + "num-traits", + "regex", + "rpds", + "thiserror 2.0.21", + "uuid", +] + [[package]] name = "cmake" version = "0.1.58" @@ -518,6 +696,15 @@ dependencies = [ "cc", ] +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "cocoa" version = "0.20.2" @@ -554,7 +741,7 @@ checksum = "fe6d2e5af09e8c8ad56c969f2157a3d4238cebc7c55f0a517728c38f7b200f81" dependencies = [ "serde", "termcolor", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -785,6 +972,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.5.0" @@ -794,6 +990,12 @@ dependencies = [ "cfg-if 1.0.4", ] +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + [[package]] name = "crossbeam-channel" version = "0.5.17" @@ -1081,6 +1283,18 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "env_filter" version = "2.0.0" @@ -1310,6 +1524,12 @@ version = "2.8.1" dependencies = [ "anyhow", "bytemuck", + "cljrs-gc", + "cljrs-nrepl", + "cljrs-reader", + "cljrs-runtime", + "cljrs-stdlib", + "cljrs-value", "cpal", "crossbeam-channel", "ctrlc", @@ -1450,6 +1670,7 @@ dependencies = [ "js-sys", "libc", "r-efi 6.0.0", + "rand_core 0.10.1", "wasm-bindgen", ] @@ -1609,6 +1830,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + [[package]] name = "hashbrown" version = "0.15.5" @@ -1629,6 +1859,20 @@ dependencies = [ "foldhash 0.2.0", ] +[[package]] +name = "heapless" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +dependencies = [ + "atomic-polyfill", + "hash32", + "rustc_version", + "serde", + "spin", + "stable_deref_trait", +] + [[package]] name = "hermit-abi" version = "0.5.2" @@ -1666,6 +1910,30 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys 0.8.7", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core 0.62.2", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "2.1.1" @@ -2137,6 +2405,15 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" +dependencies = [ + "libc", +] + [[package]] name = "mach2" version = "0.5.0" @@ -2231,6 +2508,28 @@ dependencies = [ "windows 0.62.2", ] +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "cfg-if 1.0.4", + "miette-derive", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -2528,6 +2827,25 @@ dependencies = [ "bitflags 2.11.0", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-complex" version = "0.4.6" @@ -2563,6 +2881,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3150,6 +3479,19 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "heapless", + "serde", +] + [[package]] name = "potential_utf" version = "0.1.4" @@ -3288,7 +3630,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" dependencies = [ "rand_chacha", - "rand_core", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", ] [[package]] @@ -3298,7 +3651,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.9.5", ] [[package]] @@ -3310,6 +3663,12 @@ dependencies = [ "getrandom 0.3.4", ] +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + [[package]] name = "range-alloc" version = "0.1.5" @@ -3468,6 +3827,16 @@ dependencies = [ "time", ] +[[package]] +name = "rpds" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e025feb26210bc196b908e72deb063b1b4000754304341cbc168a1e72c857ebc" +dependencies = [ + "archery", + "smallvec", +] + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3670,7 +4039,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" dependencies = [ "cfg-if 1.0.4", - "cpufeatures", + "cpufeatures 0.2.17", "digest", ] @@ -3681,10 +4050,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if 1.0.4", - "cpufeatures", + "cpufeatures 0.2.17", "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -3806,6 +4184,16 @@ dependencies = [ "serde", ] +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "spin" version = "0.9.9" @@ -4054,6 +4442,19 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "system-memory" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e643d568427db177ec7de23c26448140d6de9b2a6c6f02db3e8ed277048bcc7" +dependencies = [ + "errno", + "libc", + "mach", + "rustc_version", + "windows-sys 0.59.0", +] + [[package]] name = "tar" version = "0.4.46" @@ -4127,6 +4528,15 @@ dependencies = [ "syn 3.0.7", ] +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if 1.0.4", +] + [[package]] name = "tiff" version = "0.11.3" @@ -4207,6 +4617,32 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tokio" +version = "1.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27ad5e34374e03cfffefc301becb44e9dc3c17584f414349ebe29ed26661822d" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "toml_datetime" version = "1.0.0+spec-1.1.0" @@ -4245,9 +4681,21 @@ checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "tracing-core" version = "0.1.36" @@ -4255,6 +4703,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -4267,6 +4741,12 @@ dependencies = [ "strength_reduce", ] +[[package]] +name = "triomphe" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d15f4f5898bcfc5d8a8e45ed30b2f108d710c1a2e5489dbf80e078f707181bc" + [[package]] name = "ttf-parser" version = "0.25.1" @@ -4287,7 +4767,7 @@ dependencies = [ "http", "httparse", "log", - "rand", + "rand 0.9.2", "sha1", "thiserror 2.0.21", "utf-8", @@ -4326,6 +4806,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.2" @@ -4409,6 +4895,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a06f08a856b009bc7bbaf7d3e79808595d032dd19074c0c2ef26e49e03e6bdc" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "v4l" version = "0.14.0" @@ -4429,6 +4926,12 @@ dependencies = [ "bindgen 0.65.1", ] +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "version_check" version = "0.9.5" diff --git a/crates/fosfora-app/Cargo.toml b/crates/fosfora-app/Cargo.toml index 810bff84..6dd23b3b 100644 --- a/crates/fosfora-app/Cargo.toml +++ b/crates/fosfora-app/Cargo.toml @@ -128,6 +128,17 @@ symphonia = { version = "0.6", default-features = false, features = [ "id3v2", "id3v1", ], optional = true } +# Embedded clojurust nREPL (`repl` feature): live-code a running instance from +# CIDER/Calva/Conjure. Interpreter only: no JIT (Cranelift) and no git-deps +# tree. SPIKE: relative path deps on a sibling clojurust checkout, resolved +# from this worktree. Replace with a pinned git rev before merging, or lockfile +# resolution breaks for every other checkout and in CI. +cljrs-runtime = { path = "../../../../../../clojurust/crates/cljrs-runtime", default-features = false, features = ["regex-full"], optional = true } +cljrs-stdlib = { path = "../../../../../../clojurust/crates/cljrs-stdlib", default-features = false, features = ["regex-full"], optional = true } +cljrs-nrepl = { path = "../../../../../../clojurust/crates/cljrs-nrepl", default-features = false, features = ["regex-full"], optional = true } +cljrs-value = { path = "../../../../../../clojurust/crates/cljrs-value", default-features = false, features = ["regex-full"], optional = true } +cljrs-gc = { path = "../../../../../../clojurust/crates/cljrs-gc", optional = true } +cljrs-reader = { path = "../../../../../../clojurust/crates/cljrs-reader", optional = true } [target.'cfg(target_os = "linux")'.dependencies] # v4l2 loopback output (GH #6). MIT; already in the graph transitively via nokhwa. @@ -243,4 +254,15 @@ decode = ["symphonia"] # `--analyze `: offline song analysis, no GPU and no window (#2027). analyze = ["desktop", "decode"] profiling = ["desktop", "wgpu-profiler"] +# Embedded clojurust nREPL: set FOSFORA_NREPL= (0 = any free port) and +# connect an editor; `(require '[fosfora :as f])` is preloaded in `user`. +repl = [ + "desktop", + "dep:cljrs-runtime", + "dep:cljrs-stdlib", + "dep:cljrs-nrepl", + "dep:cljrs-value", + "dep:cljrs-gc", + "dep:cljrs-reader", +] wgpu-profiler = ["dep:wgpu-profiler"] diff --git a/crates/fosfora-app/src/lib.rs b/crates/fosfora-app/src/lib.rs index 0effbc08..c89c1471 100644 --- a/crates/fosfora-app/src/lib.rs +++ b/crates/fosfora-app/src/lib.rs @@ -57,6 +57,8 @@ pub mod paths; pub mod preset; #[cfg(feature = "desktop")] pub mod recording; +#[cfg(feature = "repl")] +pub mod repl; pub mod scene; pub mod settings; pub mod shader; diff --git a/crates/fosfora-app/src/repl/fosfora.cljrs b/crates/fosfora-app/src/repl/fosfora.cljrs new file mode 100644 index 00000000..d4e36f72 --- /dev/null +++ b/crates/fosfora-app/src/repl/fosfora.cljrs @@ -0,0 +1,75 @@ +(ns fosfora + "Live control of the running fosfora instance. + + Commands are queued for the render thread and apply on the next frame. + Values for params, opacity and data are normalized to 0..1, exactly as the + web remote sends them. + + (f/state) ; full snapshot: effects, layers, params, presets + (f/audio) ; latest audio features (bands, rms, kick, beat, bpm...) + (f/set-param! :speed 0.8) + (f/load-effect! 3) + (f/trigger! :next-preset)" + (:require [fosfora.native :as native] + [clojure.edn :as edn] + [clojure.string :as str])) + +(defn send! + "Queue one raw web-remote message, e.g. {:type \"set_param\" :name \"speed\" :value 0.5}." + [msg] + (native/send-edn (pr-str msg))) + +(defn state + "The latest full state (refreshed at 10 Hz), or nil before the first frame." + [] + (edn/read-string (native/state-edn))) + +(defn audio + "The latest audio features (refreshed at 10 Hz), or nil before the first frame." + [] + (edn/read-string (native/audio-edn))) + +(defn- param-name [p] (if (keyword? p) (name p) (str p))) + +(defn set-param! + "Set a param on the active layer. `v` is normalized 0..1." + [param v] + (send! {:type "set_param" :name (param-name param) :value (double v)})) + +(defn set-layer-param! + "Set a param on layer `layer`. `v` is normalized 0..1." + [layer param v] + (send! {:type "set_layer_param" :layer layer :name (param-name param) :value (double v)})) + +(defn load-effect! [index] (send! {:type "load_effect" :index index})) +(defn select-layer! [index] (send! {:type "select_layer" :index index})) +(defn load-preset! [index] (send! {:type "load_preset" :index index})) + +(defn opacity! [layer v] (send! {:type "set_layer_opacity" :layer layer :value (double v)})) +(defn blend! [layer mode] (send! {:type "set_layer_blend" :layer layer :value mode})) +(defn enable! [layer on?] (send! {:type "set_layer_enabled" :layer layer :value (boolean on?)})) +(defn postprocess! [on?] (send! {:type "set_postprocess_enabled" :value (boolean on?)})) + +(defn trigger! + "Fire a trigger: :next-effect :prev-effect :toggle-postprocess :toggle-overlay + :next-preset :prev-preset :next-layer :prev-layer :scene-go-next :scene-go-prev + :toggle-timeline :tempo-half :tempo-double :tempo-tap" + [action] + (send! {:type "trigger" :action (str/replace (name action) "-" "_")})) + +(defn data! + "Feed the binding bus: fields appear in the Binding Matrix as ws... + Values expire after 5 s unless something is bound to them, so send them steadily." + [source fields] + (send! {:type "data" + :source (param-name source) + :fields (into {} (map (fn [[k v]] [(param-name k) (double v)]) fields))})) + +(defn trama-dir [] (native/trama-dir)) + +(defn put-trama! + "Write a trama effect, assets/trama/effects/.wgsl. The source must open + with its /*! trama {...} */ manifest whose id equals `id`. It is validated and + hot-swapped on the render thread; a broken file keeps the last good version." + [id wgsl] + (native/write-trama! (param-name id) wgsl)) diff --git a/crates/fosfora-app/src/repl/mod.rs b/crates/fosfora-app/src/repl/mod.rs new file mode 100644 index 00000000..2c0287e7 --- /dev/null +++ b/crates/fosfora-app/src/repl/mod.rs @@ -0,0 +1,394 @@ +//! Embedded clojurust nREPL (`repl` feature). +//! +//! Set `FOSFORA_NREPL=` (`0` lets the OS pick) and fosfora starts a +//! clojurust interpreter on its own thread with an nREPL server on +//! `127.0.0.1:`, writing `.nrepl-port` in the working directory so +//! CIDER, Calva and Conjure can auto-connect. +//! +//! The interpreter never touches render state. Every command it issues is a +//! [`WsInMessage`], the same vocabulary the web remote speaks, pushed into a +//! queue that [`crate::web::WebSystem::update`] drains on the render thread. +//! Reads go the other way through snapshots the web system already builds: +//! the full state JSON (10 Hz) and the audio JSON (10 Hz). +//! +//! clojurust values are not `Send`, so the runtime is built, used and dropped +//! on the `cljrs-repl` thread alone. Its GC heap is per-thread, so a +//! collection pauses that thread only, never rendering. + +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; +use std::thread::JoinHandle; +use std::time::Duration; + +use cljrs_gc::GcPtr; +use cljrs_reader::{Form, FormKind, Parser}; +use cljrs_runtime::tiered::{Env, GlobalEnv}; +use cljrs_value::{Arity, NativeFn, Value, ValueError, ValueResult}; + +use crate::inbound::DropOldestSender; +use crate::web::types::WsInMessage; + +/// Namespace of the raw natives. User code goes through `fosfora`. +const NATIVE_NS: &str = "fosfora.native"; + +/// The `fosfora` namespace: the friendly API over the natives. +const FOSFORA_SRC: &str = include_str!("fosfora.cljrs"); + +/// What the interpreter thread shares with the render thread. +pub(crate) struct Bridge { + /// Commands into the render thread (drained by `WebSystem::update`). + pub(crate) commands: DropOldestSender, + /// Latest full-state JSON, as the web remote receives it. + pub(crate) state: Arc>, + /// Latest audio-feature JSON, as the web remote receives it. + pub(crate) audio: Arc>, +} + +/// A running embedded nREPL. Dropping it stops the server and joins the +/// interpreter thread. +pub struct Repl { + port: u16, + shutdown: cljrs_nrepl::ShutdownHandle, + thread: Option>, +} + +impl Repl { + pub fn port(&self) -> u16 { + self.port + } +} + +impl Drop for Repl { + fn drop(&mut self) { + self.shutdown.shutdown(); + if let Some(t) = self.thread.take() { + let _ = t.join(); + } + } +} + +/// The port requested by `FOSFORA_NREPL`, if the embedded nREPL is wanted. +pub fn requested_port() -> Option { + let raw = std::env::var("FOSFORA_NREPL").ok()?; + let raw = raw.trim(); + if raw.is_empty() { + return Some(0); + } + match raw.parse() { + Ok(port) => Some(port), + Err(_) => { + log::warn!("FOSFORA_NREPL={raw:?} is not a port number; nREPL not started"); + None + } + } +} + +/// Start the interpreter thread and its nREPL server on `127.0.0.1:port`. +/// Blocks until the server is listening (or failed to boot). +pub(crate) fn start(port: u16, bridge: Bridge) -> anyhow::Result { + let (ready_tx, ready_rx) = std::sync::mpsc::channel(); + let thread = std::thread::Builder::new() + .name("cljrs-repl".into()) + // The tree-walking interpreter recurses deeply; the cljrs CLI uses 64 MB too. + .stack_size(64 << 20) + .spawn(move || match boot(port, bridge) { + Ok(mut poller) => { + let _ = ready_tx.send(Ok((poller.port(), poller.shutdown_handle()))); + let mut eval = cljrs_runtime::tiered::eval; + while poller.poll_timeout(&mut eval, Duration::from_millis(50)) + == cljrs_nrepl::PollStatus::Open + {} + } + Err(e) => { + let _ = ready_tx.send(Err(e)); + } + })?; + let (port, shutdown) = ready_rx + .recv() + .map_err(|_| anyhow::anyhow!("cljrs-repl thread exited during boot"))??; + log::info!("nREPL server started on port {port} on host 127.0.0.1 - nrepl://127.0.0.1:{port}"); + Ok(Repl { + port, + shutdown, + thread: Some(thread), + }) +} + +/// Build the runtime, register the natives, preload `fosfora` into `user`, +/// and bind the nREPL listener. Runs on the interpreter thread. +fn boot(port: u16, bridge: Bridge) -> anyhow::Result { + let runtime = cljrs_runtime::Runtime::builder() + .builtin_source("fosfora", FOSFORA_SRC) + .build() + .map_err(|e| anyhow::anyhow!("cljrs runtime: {e:?}"))?; + cljrs_stdlib::install(&runtime); + let globals = runtime.into_globals(); + register_natives(&globals, bridge); + + let mut env = Env::new(globals.clone(), "user"); + eval_str(&mut env, "(require '[fosfora :as f])")?; + + let config = cljrs_nrepl::Config { + addr: ([127, 0, 0, 1], port).into(), + port_file: Some(PathBuf::from(".nrepl-port")), + }; + let server = + cljrs_nrepl::start(config, globals).map_err(|e| anyhow::anyhow!("nREPL: {e:?}"))?; + Ok(server.into_poller()) +} + +fn eval_str(env: &mut Env, src: &str) -> anyhow::Result<()> { + let forms = Parser::new(src.to_string(), "".to_string()) + .parse_all() + .map_err(|e| anyhow::anyhow!("parse {src}: {e:?}"))?; + for form in &forms { + cljrs_runtime::tiered::eval(form, env).map_err(|e| anyhow::anyhow!("eval {src}: {e:?}"))?; + } + Ok(()) +} + +fn def_native( + globals: &GlobalEnv, + name: &'static str, + arity: Arity, + f: impl Fn(&[Value]) -> ValueResult + 'static, +) { + let native = NativeFn::with_closure(format!("{NATIVE_NS}/{name}"), arity, f); + globals.intern( + NATIVE_NS, + Arc::from(name), + Value::NativeFunction(GcPtr::new(native)), + ); +} + +fn str_arg(args: &[Value], i: usize) -> ValueResult { + match args.get(i) { + Some(Value::Str(s)) => Ok(s.get().clone()), + Some(other) => Err(ValueError::WrongType { + expected: "string", + got: other.type_name().to_string(), + }), + None => Err(ValueError::Other(format!("missing argument {i}"))), + } +} + +fn str_value(s: String) -> Value { + Value::Str(GcPtr::new(s)) +} + +fn register_natives(globals: &GlobalEnv, bridge: Bridge) { + let Bridge { + commands, + state, + audio, + } = bridge; + + // (send-edn "{:type \"set_param\" ...}") queues one web-remote message. + def_native(globals, "send-edn", Arity::Fixed(1), move |args| { + let edn = str_arg(args, 0)?; + let json = edn_to_json(&edn).map_err(ValueError::Other)?; + let text = json.to_string(); + match crate::web::client::parse_client_message(&text) { + Some(msg) => { + commands.send(msg); + Ok(Value::Bool(true)) + } + None => Err(ValueError::Other(format!( + "fosfora: not a message the remote understands: {text}" + ))), + } + }); + + // (state-edn) / (audio-edn): the latest snapshots, as EDN text. + def_native(globals, "state-edn", Arity::Fixed(0), move |_| { + Ok(str_value(snapshot_edn(&state))) + }); + def_native(globals, "audio-edn", Arity::Fixed(0), move |_| { + Ok(str_value(snapshot_edn(&audio))) + }); + + // (trama-dir): where trama effect files live (hot-reloaded on write). + def_native(globals, "trama-dir", Arity::Fixed(0), |_| { + Ok(str_value( + crate::trama::effect::trama_effects_dir() + .display() + .to_string(), + )) + }); + + // (write-trama! "id" "wgsl source") writes /.wgsl. The + // watcher compiles it on the render thread; a broken file keeps the last + // good version. + def_native(globals, "write-trama!", Arity::Fixed(2), |args| { + let id = str_arg(args, 0)?; + let src = str_arg(args, 1)?; + if id.is_empty() + || !id + .chars() + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') + { + return Err(ValueError::Other(format!( + "trama id must be [a-z0-9_]+, got {id:?}" + ))); + } + let path = crate::trama::effect::trama_effects_dir().join(format!("{id}.wgsl")); + std::fs::write(&path, src) + .map_err(|e| ValueError::Other(format!("write {}: {e}", path.display())))?; + Ok(str_value(path.display().to_string())) + }); + + globals.mark_loaded(NATIVE_NS); +} + +fn snapshot_edn(snapshot: &Mutex) -> String { + let json = snapshot.lock().map(|s| s.clone()).unwrap_or_default(); + if json.is_empty() { + return "nil".to_string(); + } + match serde_json::from_str::(&json) { + Ok(v) => { + let mut out = String::with_capacity(json.len()); + json_to_edn(&v, &mut out); + out + } + Err(_) => "nil".to_string(), + } +} + +// ── EDN <-> JSON ──────────────────────────────────────────────────────────── + +/// Read one EDN form (as `pr-str` prints a map) into JSON. Keywords become +/// strings without the colon, so `{:type "trigger"}` is `{"type":"trigger"}`. +fn edn_to_json(edn: &str) -> Result { + let forms = Parser::new(edn.to_string(), "".to_string()) + .parse_all() + .map_err(|e| format!("bad EDN: {e:?}"))?; + match forms.as_slice() { + [form] => form_to_json(form), + _ => Err(format!("expected one form, got {}", forms.len())), + } +} + +fn form_to_json(form: &Form) -> Result { + use serde_json::Value as J; + Ok(match &form.kind { + FormKind::Nil => J::Null, + FormKind::Bool(b) => J::Bool(*b), + FormKind::Int(i) => J::from(*i), + FormKind::Float(f) => serde_json::Number::from_f64(*f) + .map(J::Number) + .ok_or_else(|| format!("non-finite number {f}"))?, + FormKind::Str(s) => J::String(s.clone()), + FormKind::Keyword(k) | FormKind::Symbol(k) => { + J::String(k.trim_start_matches(':').to_string()) + } + FormKind::Vector(items) | FormKind::List(items) | FormKind::Set(items) => J::Array( + items + .iter() + .map(form_to_json) + .collect::, _>>()?, + ), + FormKind::Map(kv) => { + let mut obj = serde_json::Map::new(); + for pair in kv.chunks(2) { + let key = match form_to_json(&pair[0])? { + J::String(s) => s, + other => other.to_string(), + }; + obj.insert(key, form_to_json(&pair[1])?); + } + J::Object(obj) + } + other => return Err(format!("unsupported EDN form {other:?}")), + }) +} + +/// Write JSON as EDN. Object keys that are valid keyword names become +/// keywords (`"bpm"` is `:bpm`); anything else stays a string key. +fn json_to_edn(v: &serde_json::Value, out: &mut String) { + use serde_json::Value as J; + match v { + J::Null => out.push_str("nil"), + J::Bool(b) => out.push_str(if *b { "true" } else { "false" }), + J::Number(n) => out.push_str(&n.to_string()), + J::String(s) => push_edn_str(s, out), + J::Array(items) => { + out.push('['); + for (i, item) in items.iter().enumerate() { + if i > 0 { + out.push(' '); + } + json_to_edn(item, out); + } + out.push(']'); + } + J::Object(map) => { + out.push('{'); + for (i, (k, item)) in map.iter().enumerate() { + if i > 0 { + out.push_str(", "); + } + if is_keyword_name(k) { + out.push(':'); + out.push_str(k); + } else { + push_edn_str(k, out); + } + out.push(' '); + json_to_edn(item, out); + } + out.push('}'); + } + } +} + +fn is_keyword_name(s: &str) -> bool { + let mut chars = s.chars(); + chars + .next() + .is_some_and(|c| c.is_ascii_alphabetic() || "*+!-_?".contains(c)) + && chars.all(|c| c.is_ascii_alphanumeric() || "*+!-_?.".contains(c)) +} + +fn push_edn_str(s: &str, out: &mut String) { + out.push('"'); + for c in s.chars() { + match c { + '"' => out.push_str("\\\""), + '\\' => out.push_str("\\\\"), + '\n' => out.push_str("\\n"), + '\r' => out.push_str("\\r"), + '\t' => out.push_str("\\t"), + c => out.push(c), + } + } + out.push('"'); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn edn_map_becomes_the_json_the_remote_parses() { + let json = edn_to_json(r#"{:type "set_param", :name "speed", :value 0.5}"#).unwrap(); + assert_eq!(json["type"], "set_param"); + assert_eq!(json["name"], "speed"); + assert_eq!(json["value"], 0.5); + let msg = crate::web::client::parse_client_message(&json.to_string()); + assert!(matches!(msg, Some(WsInMessage::SetParam { ref name, .. }) if name == "speed")); + } + + #[test] + fn json_snapshot_reads_back_as_edn_keywords() { + let v: serde_json::Value = serde_json::from_str( + r#"{"type":"audio","bpm":120.5,"bands":[0.1,0.2],"odd key":"a\"b"}"#, + ) + .unwrap(); + let mut out = String::new(); + json_to_edn(&v, &mut out); + assert!(out.contains(":bpm 120.5"), "{out}"); + assert!(out.contains(":bands [0.1 0.2]"), "{out}"); + assert!(out.contains(r#""odd key" "a\"b""#), "{out}"); + } +} diff --git a/crates/fosfora-app/src/web/client.rs b/crates/fosfora-app/src/web/client.rs index c73b4408..79a44cb4 100644 --- a/crates/fosfora-app/src/web/client.rs +++ b/crates/fosfora-app/src/web/client.rs @@ -103,8 +103,9 @@ fn finite_f32(v: &serde_json::Value) -> Option { v.as_f64().map(|f| f as f32).filter(|f| f.is_finite()) } -/// Parse a JSON message from the client into a WsInMessage. -fn parse_client_message(text: &str) -> Option { +/// Parse a JSON message from the client into a WsInMessage. Also the embedded +/// REPL's command path (`repl` feature), so both speak one vocabulary. +pub(crate) fn parse_client_message(text: &str) -> Option { let v: serde_json::Value = serde_json::from_str(text).ok()?; let msg_type = v.get("type")?.as_str()?; diff --git a/crates/fosfora-app/src/web/local.rs b/crates/fosfora-app/src/web/local.rs new file mode 100644 index 00000000..60625dce --- /dev/null +++ b/crates/fosfora-app/src/web/local.rs @@ -0,0 +1,57 @@ +//! In-process producers and readers of the web remote's vocabulary. +//! +//! The embedded REPL (`repl` feature) issues the same [`WsInMessage`]s a +//! WebSocket client does, through a queue that outlives server restarts, and +//! reads the same state and audio snapshots the remote is sent. + +use super::WebSystem; +use super::types::WsInMessage; + +impl WebSystem { + /// Start the embedded nREPL on `port` (0 = any free port). + #[cfg(feature = "repl")] + pub fn start_repl(&mut self, port: u16) { + self.repl = None; + let bridge = crate::repl::Bridge { + commands: self.local_tx.clone(), + state: self.latest_state.clone(), + audio: self.latest_audio.clone(), + }; + match crate::repl::start(port, bridge) { + Ok(repl) => self.repl = Some(repl), + Err(e) => log::error!("Failed to start embedded nREPL: {e:#}"), + } + } + + /// Port of the embedded nREPL, when one is running. + pub fn repl_port(&self) -> Option { + #[cfg(feature = "repl")] + return self.repl.as_ref().map(crate::repl::Repl::port); + #[cfg(not(feature = "repl"))] + None + } + + /// Whether an in-process reader wants the snapshots kept fresh even with + /// no WebSocket client connected. + pub(super) fn local_reader(&self) -> bool { + self.repl_port().is_some() + } + + /// Everything queued this frame: network clients first, then in-process. + pub(super) fn drain_inbound(&self) -> Vec { + let mut messages: Vec = self + .inbound_rx + .as_ref() + .map(|rx| rx.try_iter().collect()) + .unwrap_or_default(); + messages.extend(self.local_rx.try_iter()); + messages + } + + /// Keep the latest audio snapshot for in-process readers. + pub(super) fn store_audio(&self, json: &str) { + if let Ok(mut audio) = self.latest_audio.lock() { + json.clone_into(&mut audio); + } + } +} diff --git a/crates/fosfora-app/src/web/mod.rs b/crates/fosfora-app/src/web/mod.rs index bd931c90..db2de659 100644 --- a/crates/fosfora-app/src/web/mod.rs +++ b/crates/fosfora-app/src/web/mod.rs @@ -1,4 +1,5 @@ pub mod client; +mod local; pub mod server; #[allow(dead_code)] pub mod state; @@ -36,14 +37,28 @@ pub struct WebSystem { pub bind_values: std::collections::HashMap, /// Preview thumbnail JPEG data from bridge sources. pub preview_images: std::collections::HashMap>, + /// In-process producers (the embedded REPL). Unlike `inbound_tx` this + /// queue lives as long as the system, so a server restart keeps it. + local_tx: DropOldestSender, + local_rx: Receiver, + /// Latest audio snapshot JSON, kept only while a local reader is attached. + latest_audio: Arc>, + #[cfg(feature = "repl")] + repl: Option, } impl WebSystem { /// The system with `config` and nothing opened: no server started. fn unconnected(config: WebConfig) -> Self { let (inbound_tx, inbound_rx) = crate::inbound::bounded(64); + let (local_tx, local_rx) = crate::inbound::bounded(256); Self { + local_tx, + local_rx, + latest_audio: Arc::new(Mutex::new(String::new())), + #[cfg(feature = "repl")] + repl: None, inbound_rx: Some(inbound_rx), inbound_tx, clients: Arc::new(Mutex::new(Vec::new())), @@ -78,6 +93,11 @@ impl WebSystem { sys.start_server(); } + #[cfg(feature = "repl")] + if let Some(port) = crate::repl::requested_port() { + sys.start_repl(port); + } + sys } @@ -182,11 +202,7 @@ impl WebSystem { self.refresh_client_count(); - let Some(ref rx) = self.inbound_rx else { - return result; - }; - - let messages: Vec = rx.try_iter().collect(); + let messages = self.drain_inbound(); if messages.is_empty() { return result; } @@ -248,11 +264,7 @@ impl WebSystem { self.refresh_client_count(); - let Some(ref rx) = self.inbound_rx else { - return result; - }; - - let messages: Vec = rx.try_iter().collect(); + let messages = self.drain_inbound(); if messages.is_empty() { return result; } @@ -313,7 +325,8 @@ impl WebSystem { /// Broadcast audio features at 10Hz. pub fn broadcast_audio(&mut self, features: &AudioFeatures) { - if self.client_count == 0 { + let local = self.local_reader(); + if self.client_count == 0 && !local { return; } // 10Hz = 100ms interval @@ -323,14 +336,19 @@ impl WebSystem { self.last_audio_broadcast = Instant::now(); let json = state::build_audio_snapshot(features); - self.broadcast_json(&json); + if local { + self.store_audio(&json); + } + if self.client_count > 0 { + self.broadcast_json(&json); + } } /// Whether the caller should build full state this frame: the server is up /// and either `changed` forces it or the 10 Hz refresh is due. Building it /// serialises every layer's parameters, so it is not done every frame. pub fn state_due(&self, changed: bool) -> bool { - (self.client_count > 0 || self.is_running()) + (self.client_count > 0 || self.is_running() || self.local_reader()) && (changed || self .last_state_update From a19cc264904b7e9324d9f54b606ab23885516847 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 01:06:39 -0300 Subject: [PATCH 2/9] Opt-in hardware video decode for media layers (FOSFORA_HWACCEL) FOSFORA_HWACCEL= (cuda, vaapi, qsv, vulkan, auto) adds -hwaccel before the clip's input and -sws_flags fast_bilinear to the output. Unset, empty or `none` leaves the ffmpeg command unchanged. The method must be a plain identifier, so the variable cannot smuggle extra arguments. Frames are still downloaded for the RGBA pipe, so decode alone moving to the GPU saves little: the YUV to RGBA conversion dominates. The clip is never resized, so fast_bilinear only affects chroma upsampling. Measured on a 16 s 1080p30 H.264 clip (RTX 4070 Laptop, ffmpeg 8.0.1), CPU time: stock 17.0 s, -hwaccel cuda 13.2 s, fast_bilinear 8.3 s, both 6.5 s. scale_cuda cannot convert nv12 to rgb0 in this ffmpeg, so the conversion stays on the CPU; uploading NV12 and converting in a shader is the larger follow-up. --- crates/fosfora-app/src/media/hwaccel.rs | 52 ++++++++++++++++++++ crates/fosfora-app/src/media/mod.rs | 2 + crates/fosfora-app/src/media/video_stream.rs | 12 ++--- 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 crates/fosfora-app/src/media/hwaccel.rs diff --git a/crates/fosfora-app/src/media/hwaccel.rs b/crates/fosfora-app/src/media/hwaccel.rs new file mode 100644 index 00000000..442bccf0 --- /dev/null +++ b/crates/fosfora-app/src/media/hwaccel.rs @@ -0,0 +1,52 @@ +//! Opt-in hardware video decoding for the ffmpeg pipes. +//! +//! `FOSFORA_HWACCEL=` (`cuda`, `vaapi`, `qsv`, `vulkan`, `auto`, ...) +//! adds `-hwaccel ` before ffmpeg's input, moving H.264/HEVC decode +//! off the CPU. Frames are still downloaded to system memory for the RGBA +//! conversion and the pipe, so only decode moves. Unset, empty or `none` +//! leaves the command exactly as before, and ffmpeg itself falls back to +//! software for a codec the device cannot decode. + +/// The `-hwaccel` method requested by `FOSFORA_HWACCEL`, if any. +pub fn method() -> Option { + parse(std::env::var("FOSFORA_HWACCEL").ok().as_deref()) +} + +fn parse(raw: Option<&str>) -> Option { + let m = raw?.trim(); + let valid = !m.is_empty() + && !m.eq_ignore_ascii_case("none") + && m.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'); + valid.then(|| m.to_ascii_lowercase()) +} + +/// ffmpeg input options for the requested method: `["-hwaccel", m]` or none. +pub fn input_args() -> Vec { + method().map_or_else(Vec::new, |m| vec!["-hwaccel".to_owned(), m]) +} + +/// ffmpeg output options for the fast path. With hardware decode the +/// remaining CPU cost is the YUV to RGBA conversion, and the clip is never +/// resized, so `fast_bilinear` only touches chroma upsampling. Measured on a +/// 16 s 1080p30 H.264 clip: 17.0 s CPU stock, 13.2 s with `-hwaccel cuda`, +/// 6.5 s with both. +pub fn output_args() -> Vec { + method().map_or_else(Vec::new, |_| { + vec!["-sws_flags".to_owned(), "fast_bilinear".to_owned()] + }) +} + +#[cfg(test)] +mod tests { + use super::parse; + + #[test] + fn only_a_plain_method_name_is_passed_to_ffmpeg() { + assert_eq!(parse(Some("cuda")).as_deref(), Some("cuda")); + assert_eq!(parse(Some(" VAAPI ")).as_deref(), Some("vaapi")); + assert_eq!(parse(Some("none")), None); + assert_eq!(parse(Some("")), None); + assert_eq!(parse(None), None); + assert_eq!(parse(Some("cuda -y")), None, "no smuggled arguments"); + } +} diff --git a/crates/fosfora-app/src/media/mod.rs b/crates/fosfora-app/src/media/mod.rs index 7d2d3b79..70aa15e8 100644 --- a/crates/fosfora-app/src/media/mod.rs +++ b/crates/fosfora-app/src/media/mod.rs @@ -1,4 +1,6 @@ pub mod decoder; +#[cfg(feature = "video")] +pub mod hwaccel; #[cfg(feature = "webcam")] pub mod stream; pub mod types; diff --git a/crates/fosfora-app/src/media/video_stream.rs b/crates/fosfora-app/src/media/video_stream.rs index 655e53ca..b0d56576 100644 --- a/crates/fosfora-app/src/media/video_stream.rs +++ b/crates/fosfora-app/src/media/video_stream.rs @@ -293,15 +293,11 @@ fn settle_seek(cmd_rx: &Receiver, looping: &mut bool, mut secs: f64) -> Flo /// `start + n / fps` is frame `n`'s time exactly. fn spawn_ffmpeg(path: &Path, meta: &VideoMeta, start: f64) -> Result { Command::new("ffmpeg") - .args([ - "-v", - "error", - "-nostdin", - "-ss", - &format!("{start:.3}"), - "-i", - ]) + .args(["-v", "error", "-nostdin", "-ss", &format!("{start:.3}")]) + .args(super::hwaccel::input_args()) + .arg("-i") .arg(path) + .args(super::hwaccel::output_args()) .args([ "-an", "-vf", From 1de29ed424197bd866dd105a1b6d926cdc1c5e90 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 01:15:22 -0300 Subject: [PATCH 3/9] REPL opens media layers: f/media!, f/ls-media, f/tessera (f/media! path) decodes an image or video into a new media layer through App::start_media_layer, the same background decode the "add media layer" dialog uses; the layer becomes active when ready. (f/ls-media dir) lists the playable files in a folder, and (f/tessera [pick]) lists or opens the loops in ~/PP/mentat-collective/tessera/out/hd by file-name substring. The command is WsInMessage::AddMedia, which parse_client_message never produces: only the in-process REPL can name a local path, never a WebSocket or LAN client. app.rs collects the paths inside the active-layer borrow and starts them right after it. The pre-decode path (clips up to 60 s) now takes FOSFORA_HWACCEL too. It does not shorten the load (16 s 1080p: 3.3 s before, 3.6 s after; the 3.8 GB RGBA copy dominates), but keeps decode off the CPU. --- crates/fosfora-app/src/app.rs | 6 ++ crates/fosfora-app/src/media/video.rs | 2 + crates/fosfora-app/src/repl/fosfora.cljrs | 26 ++++++ crates/fosfora-app/src/repl/media.rs | 102 ++++++++++++++++++++++ crates/fosfora-app/src/repl/mod.rs | 3 + crates/fosfora-app/src/web/mod.rs | 2 + crates/fosfora-app/src/web/types.rs | 7 ++ 7 files changed, 148 insertions(+) create mode 100644 crates/fosfora-app/src/repl/media.rs diff --git a/crates/fosfora-app/src/app.rs b/crates/fosfora-app/src/app.rs index 51a38286..ce2d3dc3 100644 --- a/crates/fosfora-app/src/app.rs +++ b/crates/fosfora-app/src/app.rs @@ -1219,6 +1219,8 @@ impl App { // Set when a preset load should reach web clients this frame rather // than at the next 10 Hz state refresh. let mut web_state_changed = false; + // Media the embedded REPL asked for, started once the layer borrow ends. + let mut web_media_add = Vec::new(); // Drain WebSocket messages (runs after OSC — last-write-wins) if let Some(layer) = self.layer_stack.active_mut() { @@ -1230,6 +1232,7 @@ impl App { self.web.update(values, changed, defs) }; self.pending_web_triggers = web_result.triggers; + web_media_add = web_result.media_add; // Apply layer-targeted WS messages for (layer_idx, name, value) in web_result.layer_params { @@ -1315,6 +1318,9 @@ impl App { // After preset load, push full state so all clients update web_state_changed |= had_preset_loads; } + for path in web_media_add { + self.start_media_layer(path); + } // Evaluate binding bus (runs after MIDI/OSC/WS drain — bus overrides direct mappings) self.binding_bus.ingest_ws_values(&self.web.bind_values); diff --git a/crates/fosfora-app/src/media/video.rs b/crates/fosfora-app/src/media/video.rs index 7f99bc0b..a41e37fe 100644 --- a/crates/fosfora-app/src/media/video.rs +++ b/crates/fosfora-app/src/media/video.rs @@ -124,8 +124,10 @@ pub fn decode_all_frames( ); let mut child = Command::new("ffmpeg") + .args(super::hwaccel::input_args()) .args(["-i"]) .arg(path) + .args(super::hwaccel::output_args()) .args([ "-f", "rawvideo", diff --git a/crates/fosfora-app/src/repl/fosfora.cljrs b/crates/fosfora-app/src/repl/fosfora.cljrs index d4e36f72..b380739a 100644 --- a/crates/fosfora-app/src/repl/fosfora.cljrs +++ b/crates/fosfora-app/src/repl/fosfora.cljrs @@ -65,6 +65,32 @@ :source (param-name source) :fields (into {} (map (fn [[k v]] [(param-name k) (double v)]) fields))})) +(defn ls-media + "Playable images and videos in `dir` (a leading ~ is your home), sorted." + [dir] + (edn/read-string (native/list-media dir))) + +(defn media! + "Decode `path` (image or video) into a new media layer on top of the stack. + Decoding runs in the background; the layer appears when it is ready and + becomes the active layer. Returns the resolved path." + [path] + (native/add-media! path)) + +(def tessera-dir "~/PP/mentat-collective/tessera/out/hd") + +(defn tessera + "The tessera HD loops, or open one by name (a substring of its file name): + (f/tessera) lists them, (f/tessera \"graphene\") opens graphene-fermi.mp4." + ([] (mapv #(last (str/split % #"/")) (ls-media tessera-dir))) + ([pick] + (let [hits (filter #(str/includes? (last (str/split % #"/")) pick) (ls-media tessera-dir))] + (case (count hits) + 0 (throw (ex-info (str "no tessera loop matches " pick) {:pick pick})) + 1 (media! (first hits)) + (throw (ex-info (str pick " matches several loops") + {:matches (mapv #(last (str/split % #"/")) hits)})))))) + (defn trama-dir [] (native/trama-dir)) (defn put-trama! diff --git a/crates/fosfora-app/src/repl/media.rs b/crates/fosfora-app/src/repl/media.rs new file mode 100644 index 00000000..bbd5a319 --- /dev/null +++ b/crates/fosfora-app/src/repl/media.rs @@ -0,0 +1,102 @@ +//! Media natives: browse a folder of clips and open one as a media layer. +//! +//! `(list-media "dir")` returns the playable files in `dir` as EDN text, and +//! `(add-media! "path")` queues [`WsInMessage::AddMedia`], which the render +//! thread hands to `App::start_media_layer`: the same background decode the +//! "add media layer" file dialog uses. + +use std::path::{Path, PathBuf}; + +use cljrs_runtime::tiered::GlobalEnv; +use cljrs_value::{Arity, ValueError}; + +use super::{def_native, push_edn_str, str_arg, str_value}; +use crate::inbound::DropOldestSender; +use crate::web::types::WsInMessage; + +const IMAGE_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "bmp", "webp"]; + +/// `~/x` and `~` against `$HOME`; anything else as given. +fn expand(path: &str) -> PathBuf { + match (path.strip_prefix('~'), std::env::var_os("HOME")) { + (Some(rest), Some(home)) if rest.is_empty() || rest.starts_with('/') => { + PathBuf::from(home).join(rest.trim_start_matches('/')) + } + _ => PathBuf::from(path), + } +} + +fn is_media(path: &Path) -> bool { + path.extension() + .and_then(|e| e.to_str()) + .map(str::to_ascii_lowercase) + .is_some_and(|e| { + crate::media::decoder::VIDEO_EXTENSIONS.contains(&e.as_str()) + || IMAGE_EXTENSIONS.contains(&e.as_str()) + }) +} + +fn other(msg: String) -> ValueError { + ValueError::Other(msg) +} + +pub(super) fn register(globals: &GlobalEnv, commands: DropOldestSender) { + def_native(globals, "list-media", Arity::Fixed(1), |args| { + let dir = expand(&str_arg(args, 0)?); + let entries = + std::fs::read_dir(&dir).map_err(|e| other(format!("{}: {e}", dir.display())))?; + let mut files: Vec = entries + .filter_map(Result::ok) + .map(|e| e.path()) + .filter(|p| p.is_file() && is_media(p)) + .collect(); + files.sort(); + let mut edn = String::from("["); + for (i, f) in files.iter().enumerate() { + if i > 0 { + edn.push(' '); + } + push_edn_str(&f.display().to_string(), &mut edn); + } + edn.push(']'); + Ok(str_value(edn)) + }); + + def_native(globals, "add-media!", Arity::Fixed(1), move |args| { + let path = expand(&str_arg(args, 0)?); + let path = path + .canonicalize() + .map_err(|e| other(format!("{}: {e}", path.display())))?; + if !path.is_file() || !is_media(&path) { + return Err(other(format!( + "not an image or video fosfora can open: {}", + path.display() + ))); + } + let shown = path.display().to_string(); + commands.send(WsInMessage::AddMedia { path }); + Ok(str_value(shown)) + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn media_is_recognised_by_extension_case_insensitively() { + assert!(is_media(Path::new("/x/loop.MP4"))); + assert!(is_media(Path::new("/x/still.webp"))); + assert!(!is_media(Path::new("/x/notes.txt"))); + assert!(!is_media(Path::new("/x/noext"))); + } + + #[test] + fn tilde_expands_against_home_only_at_the_start() { + let home = std::env::var("HOME").unwrap(); + assert_eq!(expand("~/clips"), PathBuf::from(&home).join("clips")); + assert_eq!(expand("~"), PathBuf::from(&home)); + assert_eq!(expand("/a/~b"), PathBuf::from("/a/~b")); + assert_eq!(expand("~other/x"), PathBuf::from("~other/x")); + } +} diff --git a/crates/fosfora-app/src/repl/mod.rs b/crates/fosfora-app/src/repl/mod.rs index 2c0287e7..f27f2534 100644 --- a/crates/fosfora-app/src/repl/mod.rs +++ b/crates/fosfora-app/src/repl/mod.rs @@ -28,6 +28,8 @@ use cljrs_value::{Arity, NativeFn, Value, ValueError, ValueResult}; use crate::inbound::DropOldestSender; use crate::web::types::WsInMessage; +mod media; + /// Namespace of the raw natives. User code goes through `fosfora`. const NATIVE_NS: &str = "fosfora.native"; @@ -182,6 +184,7 @@ fn register_natives(globals: &GlobalEnv, bridge: Bridge) { state, audio, } = bridge; + media::register(globals, commands.clone()); // (send-edn "{:type \"set_param\" ...}") queues one web-remote message. def_native(globals, "send-edn", Arity::Fixed(1), move |args| { diff --git a/crates/fosfora-app/src/web/mod.rs b/crates/fosfora-app/src/web/mod.rs index db2de659..12928b30 100644 --- a/crates/fosfora-app/src/web/mod.rs +++ b/crates/fosfora-app/src/web/mod.rs @@ -252,6 +252,7 @@ impl WebSystem { WsInMessage::BindPreview { source, jpeg_data } => { self.preview_images.insert(source, jpeg_data); } + WsInMessage::AddMedia { path } => result.media_add.push(path), } } @@ -303,6 +304,7 @@ impl WebSystem { WsInMessage::BindPreview { source, jpeg_data } => { self.preview_images.insert(source, jpeg_data); } + WsInMessage::AddMedia { path } => result.media_add.push(path), _ => {} // Skip active-layer param application } } diff --git a/crates/fosfora-app/src/web/types.rs b/crates/fosfora-app/src/web/types.rs index 4d56b179..0545c87f 100644 --- a/crates/fosfora-app/src/web/types.rs +++ b/crates/fosfora-app/src/web/types.rs @@ -44,6 +44,10 @@ pub enum WsInMessage { }, /// Preview thumbnail image (JPEG) from a bridge source. BindPreview { source: String, jpeg_data: Vec }, + /// Decode a local image or video into a new media layer. In-process only + /// (the embedded REPL): `parse_client_message` never produces it, so a + /// network client cannot name a path on this machine. + AddMedia { path: PathBuf }, } /// Metadata for a WebSocket source field. @@ -75,6 +79,8 @@ pub struct WebFrameResult { pub select_layer: Option, /// Last preset load this frame (coalesced like `effect_load`). pub preset_load: Option, + /// Files to decode into new media layers, in order. + pub media_add: Vec, } impl WebFrameResult { @@ -89,6 +95,7 @@ impl WebFrameResult { effect_load: None, select_layer: None, preset_load: None, + media_add: Vec::new(), } } } From 2ef5602464d6a75abb87f076fe6ceace8bc5e5b9 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 02:28:55 -0300 Subject: [PATCH 4/9] repl: resolve cljrs path deps from the main checkout (../clojurust beside fosfora) --- crates/fosfora-app/Cargo.toml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/fosfora-app/Cargo.toml b/crates/fosfora-app/Cargo.toml index 6dd23b3b..967e8807 100644 --- a/crates/fosfora-app/Cargo.toml +++ b/crates/fosfora-app/Cargo.toml @@ -130,15 +130,16 @@ symphonia = { version = "0.6", default-features = false, features = [ ], optional = true } # Embedded clojurust nREPL (`repl` feature): live-code a running instance from # CIDER/Calva/Conjure. Interpreter only: no JIT (Cranelift) and no git-deps -# tree. SPIKE: relative path deps on a sibling clojurust checkout, resolved -# from this worktree. Replace with a pinned git rev before merging, or lockfile -# resolution breaks for every other checkout and in CI. -cljrs-runtime = { path = "../../../../../../clojurust/crates/cljrs-runtime", default-features = false, features = ["regex-full"], optional = true } -cljrs-stdlib = { path = "../../../../../../clojurust/crates/cljrs-stdlib", default-features = false, features = ["regex-full"], optional = true } -cljrs-nrepl = { path = "../../../../../../clojurust/crates/cljrs-nrepl", default-features = false, features = ["regex-full"], optional = true } -cljrs-value = { path = "../../../../../../clojurust/crates/cljrs-value", default-features = false, features = ["regex-full"], optional = true } -cljrs-gc = { path = "../../../../../../clojurust/crates/cljrs-gc", optional = true } -cljrs-reader = { path = "../../../../../../clojurust/crates/cljrs-reader", optional = true } +# tree. LOCAL ONLY: path deps on a clojurust checkout next to this repo +# (../clojurust beside ../fosfora). Cargo resolves optional path deps even when +# the feature is off, so a clone without that sibling cannot build at all. +# Switch to a pinned git rev before this goes upstream. +cljrs-runtime = { path = "../../../clojurust/crates/cljrs-runtime", default-features = false, features = ["regex-full"], optional = true } +cljrs-stdlib = { path = "../../../clojurust/crates/cljrs-stdlib", default-features = false, features = ["regex-full"], optional = true } +cljrs-nrepl = { path = "../../../clojurust/crates/cljrs-nrepl", default-features = false, features = ["regex-full"], optional = true } +cljrs-value = { path = "../../../clojurust/crates/cljrs-value", default-features = false, features = ["regex-full"], optional = true } +cljrs-gc = { path = "../../../clojurust/crates/cljrs-gc", optional = true } +cljrs-reader = { path = "../../../clojurust/crates/cljrs-reader", optional = true } [target.'cfg(target_os = "linux")'.dependencies] # v4l2 loopback output (GH #6). MIT; already in the graph transitively via nokhwa. From d455e05468aa4e45d8549f4b217e96284c07c9d5 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 02:33:05 -0300 Subject: [PATCH 5/9] web: local_tx is REPL-only; allow dead_code without the repl feature (CI builds -D warnings) --- crates/fosfora-app/src/web/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/fosfora-app/src/web/mod.rs b/crates/fosfora-app/src/web/mod.rs index 12928b30..19ecb175 100644 --- a/crates/fosfora-app/src/web/mod.rs +++ b/crates/fosfora-app/src/web/mod.rs @@ -39,6 +39,8 @@ pub struct WebSystem { pub preview_images: std::collections::HashMap>, /// In-process producers (the embedded REPL). Unlike `inbound_tx` this /// queue lives as long as the system, so a server restart keeps it. + /// Only the REPL clones it; without that feature the queue stays empty. + #[cfg_attr(not(feature = "repl"), allow(dead_code))] local_tx: DropOldestSender, local_rx: Receiver, /// Latest audio snapshot JSON, kept only while a local reader is attached. From ffeb3974006403a38b0dd1eab42e9e3a727fa711 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 03:13:47 -0300 Subject: [PATCH 6/9] Extra trama effect directories (FOSFORA_TRAMA_DIRS) FOSFORA_TRAMA_DIRS lists directories (split like PATH, leading ~ expanded) whose *.wgsl files are trama effects exactly like assets/trama/effects/: loaded at startup, a new file registers live, a change hot-swaps, a broken save keeps the last good version. An external generator can write straight into one; the first user is tessera, whose desargues emitter writes field loops to tessera/out/layers/. Load order is the built-in directory, then each extra directory; an id defined twice takes the last definition, which is also what a hot reload does (the changed file wins). The watcher recognises the built-in directory by name (trama/effects) and an extra one by being listed, watched non-recursively and seeded like the built-in one so startup reads are not reported as changes. A shader-library change reloads every directory. Tested end to end through the real watcher: a file written into an extra directory arrives on the trama route, not the layer-shader one. --- crates/fosfora-app/src/shader/hot_reload.rs | 33 ++- crates/fosfora-app/src/trama/dirs.rs | 221 ++++++++++++++++++++ crates/fosfora-app/src/trama/mod.rs | 13 +- 3 files changed, 252 insertions(+), 15 deletions(-) create mode 100644 crates/fosfora-app/src/trama/dirs.rs diff --git a/crates/fosfora-app/src/shader/hot_reload.rs b/crates/fosfora-app/src/shader/hot_reload.rs index 507b6e92..81077d00 100644 --- a/crates/fosfora-app/src/shader/hot_reload.rs +++ b/crates/fosfora-app/src/shader/hot_reload.rs @@ -102,15 +102,17 @@ impl ShaderWatcher { &assets_dir().join("shaders"), &assets_dir().join("effects"), &crate::trama::effect::trama_effects_dir(), + &crate::trama::dirs::extra_dirs(), ) } /// The watcher over explicit directories, so a test can point it at a /// scratch tree instead of the real assets. - fn watching( + pub(crate) fn watching( shader_dir: &std::path::Path, effects_dir: &std::path::Path, trama_dir: &std::path::Path, + extra_trama: &[PathBuf], ) -> Self { // Absolute, all three. In the dev workflow `assets_dir()` is the // RELATIVE path `assets`, while notify reports absolute paths — so @@ -122,6 +124,8 @@ impl ShaderWatcher { &absolute(effects_dir), &absolute(trama_dir), ); + let extra_trama: Vec = extra_trama.iter().map(|p| absolute(p)).collect(); + let extra_route = extra_trama.clone(); let (tx, rx): (Sender, Receiver) = crossbeam_channel::unbounded(); let (pfx_tx, pfx_rx): (Sender, Receiver) = crossbeam_channel::unbounded(); let (trama_tx, trama_rx): (Sender, Receiver) = @@ -134,7 +138,12 @@ impl ShaderWatcher { for event in events { if event.kind == DebouncedEventKind::Any { let path = event.path.clone(); - let _ = match route(&path) { + let routed = if crate::trama::dirs::is_effect_in(&path, &extra_route) { + Route::Trama + } else { + route(&path) + }; + let _ = match routed { Route::Shader => tx.send(path), Route::Pfx => pfx_tx.send(path), Route::Trama => trama_tx.send(path), @@ -149,7 +158,7 @@ impl ShaderWatcher { let mut failed: Vec = Vec::new(); let debouncer = match debouncer { Ok(mut debouncer) => { - for (dir, mode, what) in [ + let mut dirs = vec![ (shader_dir, notify::RecursiveMode::Recursive, "shader"), (effects_dir, notify::RecursiveMode::Recursive, ".pfx"), ( @@ -157,7 +166,10 @@ impl ShaderWatcher { notify::RecursiveMode::NonRecursive, "trama effect", ), - ] { + ]; + let extra = notify::RecursiveMode::NonRecursive; + dirs.extend(extra_trama.iter().map(|d| (d, extra, "trama effect"))); + for (dir, mode, what) in dirs { if !dir.exists() { continue; } @@ -190,6 +202,9 @@ impl ShaderWatcher { seed(shader_dir, true, &mut seen); seed(effects_dir, true, &mut seen); seed(trama_dir, false, &mut seen); + for dir in &extra_trama { + seed(dir, false, &mut seen); + } Self { _debouncer: debouncer, @@ -281,8 +296,12 @@ mod tests { .collect(); let relative = |abs: &Path| up.join(abs.strip_prefix("/").unwrap()); assert!(relative(&trama).is_relative() && relative(&trama).is_dir()); - let mut w = - ShaderWatcher::watching(&relative(&shaders), &relative(&effects), &relative(&trama)); + let mut w = ShaderWatcher::watching( + &relative(&shaders), + &relative(&effects), + &relative(&trama), + &[], + ); // Poll the way the app does. `quiet` waits out the whole window (the // debounce is 100 ms); `next` returns as soon as something arrives. @@ -365,7 +384,7 @@ mod tests { return; } - let mut w = ShaderWatcher::watching(&shaders, &effects, &trama); + let mut w = ShaderWatcher::watching(&shaders, &effects, &trama, &[]); std::fs::set_permissions(&shaders, std::fs::Permissions::from_mode(0o755)).unwrap(); let notice = w.take_degraded_notice(); diff --git a/crates/fosfora-app/src/trama/dirs.rs b/crates/fosfora-app/src/trama/dirs.rs new file mode 100644 index 00000000..4f11fd23 --- /dev/null +++ b/crates/fosfora-app/src/trama/dirs.rs @@ -0,0 +1,221 @@ +//! Extra trama effect directories. +//! +//! `FOSFORA_TRAMA_DIRS` lists directories (`:`-separated on Unix, `;` on +//! Windows; a leading `~` is the home directory) whose `*.wgsl` files are +//! trama effects exactly like `assets/trama/effects/`: loaded at startup, +//! hot-reloaded on change, registered live when new, and kept at the last +//! good version when a save breaks them. An external generator (a shader DSL, +//! another project's render pipeline) can write straight into one. +//! +//! The built-in directory loads first, then each extra directory in order. An +//! effect id defined in more than one place takes the LAST definition, which +//! is also what a hot reload does (the changed file wins), so the two paths +//! agree. + +use std::ffi::OsString; +use std::path::{Path, PathBuf}; + +use super::effect::{TramaRegistry, trama_effects_dir}; +use crate::effect::loader::EffectLoader; + +/// The variable naming the extra directories. +pub const ENV: &str = "FOSFORA_TRAMA_DIRS"; + +/// Extra directories from [`ENV`], absolute, in the order given. +pub fn extra_dirs() -> Vec { + parse(std::env::var_os(ENV), std::env::var_os("HOME")) +} + +/// The built-in directory, then every extra one: load and reload order. +pub fn all_dirs() -> Vec { + let mut dirs = vec![trama_effects_dir()]; + dirs.extend(extra_dirs()); + dirs +} + +/// Is `path` a trama effect file directly inside one of `dirs`? The watcher +/// routes by this: the built-in directory is recognised by its name +/// (`trama/effects`), an extra one only by being listed. +pub fn is_effect_in(path: &Path, dirs: &[PathBuf]) -> bool { + path.extension().is_some_and(|e| e == "wgsl") + && path.parent().is_some_and(|p| dirs.iter().any(|d| d == p)) +} + +fn parse(raw: Option, home: Option) -> Vec { + let Some(raw) = raw else { + return Vec::new(); + }; + std::env::split_paths(&raw) + .filter(|p| !p.as_os_str().is_empty()) + .map(|p| expand_home(&p, home.as_deref())) + .map(|p| std::path::absolute(&p).unwrap_or(p)) + .collect() +} + +/// `~` and `~/rest` against `home`; anything else unchanged. +fn expand_home(path: &Path, home: Option<&std::ffi::OsStr>) -> PathBuf { + let (Some(home), Ok(rest)) = (home, path.strip_prefix("~")) else { + return path.to_path_buf(); + }; + PathBuf::from(home).join(rest) +} + +impl TramaRegistry { + /// [`TramaRegistry::load`] over several directories, in order; a later + /// directory's effect replaces an earlier one with the same id. + pub fn load_all( + device: &wgpu::Device, + cache: Option<&wgpu::PipelineCache>, + loader: &EffectLoader, + dirs: &[PathBuf], + ) -> Self { + let Some((first, rest)) = dirs.split_first() else { + return Self::load(device, cache, loader, &trama_effects_dir()); + }; + let mut registry = Self::load(device, cache, loader, first); + for dir in rest { + if !dir.is_dir() { + log::warn!( + "trama: {ENV} names {}, which is not a directory", + dir.display() + ); + continue; + } + let more = Self::load(device, cache, loader, dir); + log::info!( + "trama: {} effect(s) from {}", + more.effects.len(), + dir.display() + ); + for def in more.effects { + if registry.effects.iter().any(|e| e.id == def.id) { + log::info!( + "trama: {} from {} replaces the earlier one", + def.id.0, + dir.display() + ); + registry.effects.retain(|e| e.id != def.id); + } + registry.effects.push(def); + } + registry.errors.extend(more.errors); + } + registry.effects.sort_by(|a, b| a.id.0.cmp(&b.id.0)); + registry + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const HOME: &str = "/srv/me"; + + /// `parse` takes `std::env::var_os` results, which are `Option`s. + #[allow(clippy::unnecessary_wraps)] + fn os(s: &str) -> Option { + Some(OsString::from(s)) + } + + #[test] + fn unset_or_empty_means_no_extra_dirs() { + assert!(parse(None, os(HOME)).is_empty()); + assert!(parse(os(""), os(HOME)).is_empty()); + } + + #[cfg(unix)] + #[test] + fn dirs_split_on_colon_and_expand_home() { + let dirs = parse(os("~/a/layers:/abs/b::~"), os(HOME)); + assert_eq!( + dirs, + vec![ + PathBuf::from("/srv/me/a/layers"), + PathBuf::from("/abs/b"), + PathBuf::from("/srv/me"), + ] + ); + } + + #[test] + fn only_a_leading_tilde_component_is_home() { + let home = Some(std::ffi::OsStr::new(HOME)); + assert_eq!( + expand_home(Path::new("~other/x"), home), + PathBuf::from("~other/x") + ); + assert_eq!(expand_home(Path::new("/a/~"), home), PathBuf::from("/a/~")); + } + + #[test] + fn only_wgsl_directly_in_a_listed_dir_is_an_effect() { + let dirs = vec![PathBuf::from("/srv/tessera/out/layers")]; + assert!(is_effect_in( + Path::new("/srv/tessera/out/layers/rings.wgsl"), + &dirs + )); + assert!(!is_effect_in( + Path::new("/srv/tessera/out/layers/notes.txt"), + &dirs + )); + assert!(!is_effect_in( + Path::new("/srv/tessera/out/layers/sub/rings.wgsl"), + &dirs + )); + assert!(!is_effect_in(Path::new("/srv/other/rings.wgsl"), &dirs)); + } + + /// End to end through the real watcher: a file a generator writes into an + /// extra directory arrives on the TRAMA route, not the layer-shader one + /// (which would match it against no pass and drop it silently). + #[cfg(all(target_os = "linux", feature = "desktop"))] + #[test] + fn a_new_file_in_an_extra_dir_reaches_the_trama_route() { + use crate::shader::hot_reload::ShaderWatcher; + use std::time::Duration; + + let root = std::env::temp_dir().join(format!("fosfora-extra-trama-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let shaders = root.join("shaders"); + let effects = root.join("effects"); + let trama = root.join("trama/effects"); + let extra = root.join("tessera/out/layers"); + for d in [&shaders, &effects, &trama, &extra] { + std::fs::create_dir_all(d).unwrap(); + } + let mut w = + ShaderWatcher::watching(&shaders, &effects, &trama, std::slice::from_ref(&extra)); + std::thread::sleep(Duration::from_millis(200)); + + let file = extra.join("rings.wgsl"); + std::fs::write(&file, "/*! trama */").unwrap(); + let mut trama_changes = Vec::new(); + for _ in 0..40 { + std::thread::sleep(Duration::from_millis(50)); + trama_changes.extend(w.drain_trama_changes()); + if !trama_changes.is_empty() { + break; + } + } + let shader_changes = w.drain_changes(); + let _ = std::fs::remove_dir_all(&root); + + assert!( + trama_changes + .iter() + .any(|p| p.ends_with("tessera/out/layers/rings.wgsl")), + "new extra-dir file reported as a trama change: {trama_changes:?}" + ); + assert!( + shader_changes.is_empty(), + "not as a layer shader: {shader_changes:?}" + ); + } + + #[test] + fn relative_dirs_become_absolute() { + let dirs = parse(os("rel/layers"), None); + assert!(dirs[0].is_absolute()); + assert!(dirs[0].ends_with("rel/layers")); + } +} diff --git a/crates/fosfora-app/src/trama/mod.rs b/crates/fosfora-app/src/trama/mod.rs index b1ce0d1e..4d7eb867 100644 --- a/crates/fosfora-app/src/trama/mod.rs +++ b/crates/fosfora-app/src/trama/mod.rs @@ -5,6 +5,7 @@ //! manifest registry, the scene-level executor behind the //! `execute_and_composite` seam, and the canvas. pub mod audio; +pub mod dirs; pub mod effect; pub mod exec; pub mod graph; @@ -89,8 +90,7 @@ impl TramaSystem { width: u32, height: u32, ) -> Self { - let registry = - effect::TramaRegistry::load(device, cache, loader, &effect::trama_effects_dir()); + let registry = effect::TramaRegistry::load_all(device, cache, loader, &dirs::all_dirs()); let master = graph::NodeGraph::new_with_output(); let canvas = ui::canvas::CanvasState::default(); Self { @@ -242,12 +242,9 @@ impl TramaSystem { .map(|p| self.registry.reload_file(device, cache, loader, p)) .collect(); if all { - outcomes.extend(self.registry.reload_all( - device, - cache, - loader, - &effect::trama_effects_dir(), - )); + for dir in dirs::all_dirs() { + outcomes.extend(self.registry.reload_all(device, cache, loader, &dir)); + } } for outcome in outcomes { let effect::Reloaded::Swapped { From 47e9ac3f1aa6c20817a78a71c3c15a41a7cbb163 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 03:40:11 -0300 Subject: [PATCH 7/9] Extra .pfx effect directories (FOSFORA_EFFECT_DIRS) FOSFORA_EFFECT_DIRS lists directories (same rules as FOSFORA_TRAMA_DIRS) whose *.pfx files are layer effects like assets/effects/: they appear in the catalog (under "Yours" unless authored "Fosfora"), load onto layers, save in presets by name, and hot-reload when the .pfx or one of its shaders changes. Each directory is watched recursively, so a .pfx and its .wgsl can sit side by side. The first user is tessera, whose loops get a catalog entry each. A relative `shader` in an extra directory's .pfx resolves against that .pfx's own directory (effect::dirs::localize), at scan and again on every hot reload, so the reload path cannot point it back at assets/shaders. Built-in effects and absolute paths are untouched. Extra effects are appended after the built-ins, so built-in indices do not move. The built-in scan now reads each file through the same read_pfx helper, which keeps the interaction_3d fixup in one place. --- crates/fosfora-app/src/app.rs | 1 + crates/fosfora-app/src/effect/dirs.rs | 178 ++++++++++++++++++++ crates/fosfora-app/src/effect/loader.rs | 50 ++---- crates/fosfora-app/src/effect/mod.rs | 1 + crates/fosfora-app/src/shader/hot_reload.rs | 12 +- crates/fosfora-app/src/trama/dirs.rs | 13 +- 6 files changed, 217 insertions(+), 38 deletions(-) create mode 100644 crates/fosfora-app/src/effect/dirs.rs diff --git a/crates/fosfora-app/src/app.rs b/crates/fosfora-app/src/app.rs index ce2d3dc3..e5438ba6 100644 --- a/crates/fosfora-app/src/app.rs +++ b/crates/fosfora-app/src/app.rs @@ -1879,6 +1879,7 @@ impl App { }; let new_effect = match serde_json::from_str::(&json) { Ok(mut e) => { + crate::effect::dirs::localize(&mut e, pfx_path); e.source_path = Some(pfx_path.clone()); e } diff --git a/crates/fosfora-app/src/effect/dirs.rs b/crates/fosfora-app/src/effect/dirs.rs new file mode 100644 index 00000000..37d56e24 --- /dev/null +++ b/crates/fosfora-app/src/effect/dirs.rs @@ -0,0 +1,178 @@ +//! Extra `.pfx` effect directories. +//! +//! `FOSFORA_EFFECT_DIRS` lists directories (same rules as +//! `FOSFORA_TRAMA_DIRS`: split like `PATH`, a leading `~` is home) whose +//! `*.pfx` files are layer effects exactly like `assets/effects/`: they appear +//! in the catalog (under "Yours" unless their author is "Fosfora"), load onto +//! layers, save in presets by name, and hot-reload when the `.pfx` or one of +//! its shaders changes. Each directory is watched recursively, so a `.pfx` and +//! its `.wgsl` can sit side by side or in subfolders. +//! +//! A relative `shader` path in an extra directory's `.pfx` resolves against +//! that `.pfx`'s own directory, not `assets/shaders/`, so a generator can ship +//! a self-contained folder. [`localize`] does the rewrite, at scan and again +//! on every hot reload of the file. +//! +//! A brand-new `.pfx` is picked up at the next start, as in `assets/effects/`. + +use std::path::{Path, PathBuf}; + +use super::format::PfxEffect; + +/// The variable naming the extra directories. +pub const ENV: &str = "FOSFORA_EFFECT_DIRS"; + +/// Extra directories from [`ENV`], absolute, in the order given. +pub fn extra_dirs() -> Vec { + crate::trama::dirs::parse(std::env::var_os(ENV), std::env::var_os("HOME")) +} + +/// Is `path` inside one of `dirs` (at any depth)? +fn is_under(path: &Path, dirs: &[PathBuf]) -> bool { + dirs.iter().any(|d| path.starts_with(d)) +} + +/// Make the relative shader paths of an effect read from `pfx_path`'s +/// directory, when that file lives in an extra directory. Built-in effects +/// and absolute paths are left alone, and a second call changes nothing. +pub fn localize(effect: &mut PfxEffect, pfx_path: &Path) { + localize_in(effect, pfx_path, &extra_dirs()); +} + +fn localize_in(effect: &mut PfxEffect, pfx_path: &Path, dirs: &[PathBuf]) { + let abs = std::path::absolute(pfx_path).unwrap_or_else(|_| pfx_path.to_path_buf()); + if !is_under(&abs, dirs) { + return; + } + let Some(base) = abs.parent() else { + return; + }; + let fix = |shader: &mut String| { + if !shader.is_empty() && Path::new(shader.as_str()).is_relative() { + *shader = base.join(shader.as_str()).display().to_string(); + } + }; + fix(&mut effect.shader); + for pass in &mut effect.passes { + fix(&mut pass.shader); + } +} + +/// Read one `.pfx`: parse it, apply the load-time fixups the built-in scan +/// applies, and remember where it came from. `None` (logged) if unreadable. +pub(crate) fn read_pfx(path: &Path) -> Option { + let json = std::fs::read_to_string(path) + .map_err(|e| log::warn!("Failed to read {}: {e}", path.display())) + .ok()?; + let mut effect: PfxEffect = serde_json::from_str(&json) + .map_err(|e| log::warn!("Failed to parse {}: {e}", path.display())) + .ok()?; + // `interaction_3d` implies `interaction`: set it so every reader of the + // flag builds and reports the hash. + if let Some(pd) = effect.particles.as_mut() { + pd.interaction |= pd.interaction_3d; + } + let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf()); + localize(&mut effect, &path); + log::info!("Found effect: {} ({})", effect.name, path.display()); + effect.source_path = Some(path); + Some(effect) +} + +/// Every `*.pfx` under `dir`, recursively, sorted by path. +pub(crate) fn pfx_files(dir: &Path) -> Vec { + let mut out = Vec::new(); + let Ok(entries) = std::fs::read_dir(dir) else { + return out; + }; + for path in entries.filter_map(Result::ok).map(|e| e.path()) { + if path.is_dir() { + out.extend(pfx_files(&path)); + } else if path.extension().is_some_and(|e| e == "pfx") { + out.push(path); + } + } + out.sort(); + out +} + +/// The effects of every extra directory, in directory then path order. +pub(crate) fn scan_extra() -> Vec { + let mut effects = Vec::new(); + for dir in extra_dirs() { + if !dir.is_dir() { + log::warn!("{ENV} names {}, which is not a directory", dir.display()); + continue; + } + let found: Vec = pfx_files(&dir).iter().filter_map(|p| read_pfx(p)).collect(); + log::info!("{} effect(s) from {}", found.len(), dir.display()); + effects.extend(found); + } + effects +} + +#[cfg(test)] +mod tests { + use super::*; + + fn effect(shader: &str, pass_shaders: &[&str]) -> PfxEffect { + let passes: Vec = pass_shaders + .iter() + .map(|s| serde_json::json!({ "name": "p", "shader": s })) + .collect(); + serde_json::from_value(serde_json::json!({ + "name": "T", "shader": shader, "passes": passes + })) + .unwrap() + } + + #[test] + fn extra_dir_effects_read_shaders_beside_their_pfx() { + let dirs = vec![PathBuf::from("/srv/tessera/out/effects")]; + let mut e = effect("graphene.wgsl", &["sub/a.wgsl", "/abs/b.wgsl"]); + localize_in( + &mut e, + Path::new("/srv/tessera/out/effects/graphene.pfx"), + &dirs, + ); + assert_eq!(e.shader, "/srv/tessera/out/effects/graphene.wgsl"); + assert_eq!(e.passes[0].shader, "/srv/tessera/out/effects/sub/a.wgsl"); + assert_eq!(e.passes[1].shader, "/abs/b.wgsl", "absolute paths are kept"); + + let once = e.shader.clone(); + localize_in( + &mut e, + Path::new("/srv/tessera/out/effects/graphene.pfx"), + &dirs, + ); + assert_eq!(e.shader, once, "a second call changes nothing"); + } + + #[test] + fn built_in_effects_keep_assets_relative_shaders() { + let dirs = vec![PathBuf::from("/srv/tessera/out/effects")]; + let mut e = effect("aurora.wgsl", &[]); + localize_in( + &mut e, + Path::new("/srv/fosfora/assets/effects/aurora.pfx"), + &dirs, + ); + assert_eq!(e.shader, "aurora.wgsl"); + } + + #[test] + fn pfx_files_are_found_recursively_and_sorted() { + let root = std::env::temp_dir().join(format!("fosfora-effect-dirs-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(root.join("sub")).unwrap(); + for f in ["b.pfx", "a.pfx", "sub/c.pfx", "shader.wgsl"] { + std::fs::write(root.join(f), "{}").unwrap(); + } + let found: Vec = pfx_files(&root) + .iter() + .map(|p| p.strip_prefix(&root).unwrap().display().to_string()) + .collect(); + let _ = std::fs::remove_dir_all(&root); + assert_eq!(found, vec!["a.pfx", "b.pfx", "sub/c.pfx"]); + } +} diff --git a/crates/fosfora-app/src/effect/loader.rs b/crates/fosfora-app/src/effect/loader.rs index 77cbe760..40bda58d 100644 --- a/crates/fosfora-app/src/effect/loader.rs +++ b/crates/fosfora-app/src/effect/loader.rs @@ -470,42 +470,24 @@ impl EffectLoader { pub fn scan_effects_directory(&mut self) { self.effects.clear(); let dir = assets_dir().join("effects"); - if !dir.exists() { + if dir.exists() { + let mut entries: Vec<_> = std::fs::read_dir(dir) + .into_iter() + .flatten() + .filter_map(|e| e.ok()) + .filter(|e| e.path().extension().is_some_and(|ext| ext == "pfx")) + .collect(); + entries.sort_by_key(|e| e.file_name()); + self.effects.extend( + entries + .iter() + .filter_map(|e| super::dirs::read_pfx(&e.path())), + ); + } else { log::warn!("Effects directory not found: {}", dir.display()); - return; - } - - let mut entries: Vec<_> = std::fs::read_dir(dir) - .into_iter() - .flatten() - .filter_map(|e| e.ok()) - .filter(|e| e.path().extension().is_some_and(|ext| ext == "pfx")) - .collect(); - entries.sort_by_key(|e| e.file_name()); - - for entry in entries { - match std::fs::read_to_string(entry.path()) { - Ok(json) => match serde_json::from_str::(&json) { - Ok(mut effect) => { - // `interaction_3d` implies `interaction`: set it so every - // reader of the flag builds and reports the hash. - if let Some(pd) = effect.particles.as_mut() { - pd.interaction |= pd.interaction_3d; - } - let path = entry.path().canonicalize().unwrap_or_else(|_| entry.path()); - log::info!("Found effect: {} ({})", effect.name, path.display()); - effect.source_path = Some(path); - self.effects.push(effect); - } - Err(e) => { - log::warn!("Failed to parse {}: {e}", entry.path().display()); - } - }, - Err(e) => { - log::warn!("Failed to read {}: {e}", entry.path().display()); - } - } } + // FOSFORA_EFFECT_DIRS, after the built-ins so their indices are stable. + self.effects.extend(super::dirs::scan_extra()); log::info!("Found {} effects", self.effects.len()); } diff --git a/crates/fosfora-app/src/effect/mod.rs b/crates/fosfora-app/src/effect/mod.rs index 0a6ce47c..ccbb1d8e 100644 --- a/crates/fosfora-app/src/effect/mod.rs +++ b/crates/fosfora-app/src/effect/mod.rs @@ -1,3 +1,4 @@ +pub mod dirs; pub mod format; pub mod loader; pub mod rates; diff --git a/crates/fosfora-app/src/shader/hot_reload.rs b/crates/fosfora-app/src/shader/hot_reload.rs index 81077d00..e487d349 100644 --- a/crates/fosfora-app/src/shader/hot_reload.rs +++ b/crates/fosfora-app/src/shader/hot_reload.rs @@ -103,6 +103,7 @@ impl ShaderWatcher { &assets_dir().join("effects"), &crate::trama::effect::trama_effects_dir(), &crate::trama::dirs::extra_dirs(), + &crate::effect::dirs::extra_dirs(), ) } @@ -113,6 +114,7 @@ impl ShaderWatcher { effects_dir: &std::path::Path, trama_dir: &std::path::Path, extra_trama: &[PathBuf], + extra_effects: &[PathBuf], ) -> Self { // Absolute, all three. In the dev workflow `assets_dir()` is the // RELATIVE path `assets`, while notify reports absolute paths — so @@ -126,6 +128,8 @@ impl ShaderWatcher { ); let extra_trama: Vec = extra_trama.iter().map(|p| absolute(p)).collect(); let extra_route = extra_trama.clone(); + // `.pfx` and their shaders: routed by extension, like assets/effects. + let extra_effects: Vec = extra_effects.iter().map(|p| absolute(p)).collect(); let (tx, rx): (Sender, Receiver) = crossbeam_channel::unbounded(); let (pfx_tx, pfx_rx): (Sender, Receiver) = crossbeam_channel::unbounded(); let (trama_tx, trama_rx): (Sender, Receiver) = @@ -169,6 +173,8 @@ impl ShaderWatcher { ]; let extra = notify::RecursiveMode::NonRecursive; dirs.extend(extra_trama.iter().map(|d| (d, extra, "trama effect"))); + let effect = notify::RecursiveMode::Recursive; + dirs.extend(extra_effects.iter().map(|d| (d, effect, "effect"))); for (dir, mode, what) in dirs { if !dir.exists() { continue; @@ -205,6 +211,9 @@ impl ShaderWatcher { for dir in &extra_trama { seed(dir, false, &mut seen); } + for dir in &extra_effects { + seed(dir, true, &mut seen); + } Self { _debouncer: debouncer, @@ -301,6 +310,7 @@ mod tests { &relative(&effects), &relative(&trama), &[], + &[], ); // Poll the way the app does. `quiet` waits out the whole window (the @@ -384,7 +394,7 @@ mod tests { return; } - let mut w = ShaderWatcher::watching(&shaders, &effects, &trama, &[]); + let mut w = ShaderWatcher::watching(&shaders, &effects, &trama, &[], &[]); std::fs::set_permissions(&shaders, std::fs::Permissions::from_mode(0o755)).unwrap(); let notice = w.take_degraded_notice(); diff --git a/crates/fosfora-app/src/trama/dirs.rs b/crates/fosfora-app/src/trama/dirs.rs index 4f11fd23..75836dc2 100644 --- a/crates/fosfora-app/src/trama/dirs.rs +++ b/crates/fosfora-app/src/trama/dirs.rs @@ -41,7 +41,9 @@ pub fn is_effect_in(path: &Path, dirs: &[PathBuf]) -> bool { && path.parent().is_some_and(|p| dirs.iter().any(|d| d == p)) } -fn parse(raw: Option, home: Option) -> Vec { +/// A `PATH`-style directory list: split, `~` expanded against `home`, made +/// absolute. Shared with `effect::dirs` (`FOSFORA_EFFECT_DIRS`). +pub(crate) fn parse(raw: Option, home: Option) -> Vec { let Some(raw) = raw else { return Vec::new(); }; @@ -183,8 +185,13 @@ mod tests { for d in [&shaders, &effects, &trama, &extra] { std::fs::create_dir_all(d).unwrap(); } - let mut w = - ShaderWatcher::watching(&shaders, &effects, &trama, std::slice::from_ref(&extra)); + let mut w = ShaderWatcher::watching( + &shaders, + &effects, + &trama, + std::slice::from_ref(&extra), + &[], + ); std::thread::sleep(Duration::from_millis(200)); let file = extra.join("rings.wgsl"); From f90079707b32c1e705875143e57149981da71593 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 16:51:24 -0300 Subject: [PATCH 8/9] repl: pin cljrs to a BuddhiLW/clojurust rev instead of ../clojurust path deps Cargo resolves optional path deps even with the feature off, so any clone without a sibling clojurust checkout (CI included) could not build at all. The cljrs crates now come from the fork's staging line (upstream csm/clojurust plus fixes still under review there). Local development can point back at a checkout with a [patch."https://github.com/BuddhiLW/clojurust"] table in a .cargo/config.toml above the repo. Builds that the old path setup hid: - cljrs_nrepl::Config gained a `limits` field upstream; take its default. - repl/media.rs used VIDEO_EXTENSIONS unconditionally, which only exists with the `video` feature, so `--features repl` alone did not compile. - archery moved to 1.2.2 in Cargo.lock to satisfy cljrs-value's exact pin. --- Cargo.lock | 155 +++++++++++++++------------ crates/fosfora-app/Cargo.toml | 21 ++-- crates/fosfora-app/src/repl/media.rs | 8 +- crates/fosfora-app/src/repl/mod.rs | 1 + 4 files changed, 104 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 66750c0c..46888675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -206,9 +206,9 @@ dependencies = [ [[package]] name = "archery" -version = "1.2.3" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ca55ee147b1926dbea904f50fe4902494e97bc742205abbbf10c709e43815f" +checksum = "70e0a5f99dfebb87bb342d0f53bb92c81842e100bbb915223e38349580e5441d" dependencies = [ "triomphe", ] @@ -240,15 +240,6 @@ dependencies = [ "libloading 0.8.9", ] -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - [[package]] name = "atomic-waker" version = "1.1.2" @@ -573,20 +564,39 @@ dependencies = [ "error-code", ] +[[package]] +name = "cljrs-async" +version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" +dependencies = [ + "cljrs-gc", + "cljrs-reader", + "cljrs-runtime", + "cljrs-value", + "corosensei", + "futures-util", + "gloo-timers", + "rand 0.10.3", + "tokio", +] + [[package]] name = "cljrs-gc" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "bigdecimal", + "inventory", + "libc", "num-bigint", "num-rational", - "system-memory", "tracing", ] [[package]] name = "cljrs-ir" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "cljrs-reader", "cljrs-types", @@ -597,7 +607,9 @@ dependencies = [ [[package]] name = "cljrs-nrepl" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ + "cljrs-async", "cljrs-gc", "cljrs-reader", "cljrs-runtime", @@ -610,16 +622,16 @@ dependencies = [ [[package]] name = "cljrs-project" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ - "cljrs-gc", "cljrs-reader", - "cljrs-types", "thiserror 2.0.21", ] [[package]] name = "cljrs-reader" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "cljrs-types", "miette", @@ -628,6 +640,7 @@ dependencies = [ [[package]] name = "cljrs-runtime" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "bigdecimal", "cljrs-gc", @@ -640,7 +653,6 @@ dependencies = [ "num-rational", "num-traits", "rand 0.10.3", - "rpds", "thiserror 2.0.21", "tracing", "tracing-subscriber", @@ -650,10 +662,10 @@ dependencies = [ [[package]] name = "cljrs-stdlib" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "chrono", "cljrs-gc", - "cljrs-ir", "cljrs-reader", "cljrs-runtime", "cljrs-value", @@ -663,6 +675,7 @@ dependencies = [ [[package]] name = "cljrs-types" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "miette", "serde", @@ -672,12 +685,13 @@ dependencies = [ [[package]] name = "cljrs-value" version = "0.1.0" +source = "git+https://github.com/BuddhiLW/clojurust?rev=5cf0d706#5cf0d706bfdea58af3a2b5e5dfd71b76dcdf779d" dependencies = [ "arc-swap", + "archery", "bigdecimal", "cljrs-gc", "cljrs-reader", - "cljrs-types", "num-bigint", "num-rational", "num-traits", @@ -933,6 +947,19 @@ dependencies = [ "core-foundation-sys 0.8.7", ] +[[package]] +name = "corosensei" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6886a0c0f263965933c438626e7179139a62b978a33aa18281cbf0cd5a975f34" +dependencies = [ + "autocfg", + "cfg-if 1.0.4", + "libc", + "scopeguard", + "windows-sys 0.59.0", +] + [[package]] name = "cpal" version = "0.17.3" @@ -990,12 +1017,6 @@ dependencies = [ "cfg-if 1.0.4", ] -[[package]] -name = "critical-section" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" - [[package]] name = "crossbeam-channel" version = "0.5.17" @@ -1585,12 +1606,32 @@ dependencies = [ "zip", ] +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + [[package]] name = "futures-core" version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "futures-sink" version = "0.3.32" @@ -1610,6 +1651,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" dependencies = [ "futures-core", + "futures-macro", "futures-task", "pin-project-lite", "slab", @@ -1707,6 +1749,18 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" +[[package]] +name = "gloo-timers" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "glow" version = "0.16.0" @@ -1830,15 +1884,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "hash32" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" -dependencies = [ - "byteorder", -] - [[package]] name = "hashbrown" version = "0.15.5" @@ -1859,20 +1904,6 @@ dependencies = [ "foldhash 0.2.0", ] -[[package]] -name = "heapless" -version = "0.7.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" -dependencies = [ - "atomic-polyfill", - "hash32", - "rustc_version", - "serde", - "spin", - "stable_deref_trait", -] - [[package]] name = "hermit-abi" version = "0.5.2" @@ -2101,6 +2132,15 @@ dependencies = [ "libc", ] +[[package]] +name = "inventory" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6928282826c822ad91bf1c9a1cb90a30ba1c26770749929b4656cd6be829cd7c" +dependencies = [ + "rustversion", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -2405,15 +2445,6 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" -[[package]] -name = "mach" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" -dependencies = [ - "libc", -] - [[package]] name = "mach2" version = "0.5.0" @@ -3488,7 +3519,6 @@ dependencies = [ "cobs", "embedded-io 0.4.0", "embedded-io 0.6.1", - "heapless", "serde", ] @@ -4442,19 +4472,6 @@ dependencies = [ "syn 2.0.117", ] -[[package]] -name = "system-memory" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e643d568427db177ec7de23c26448140d6de9b2a6c6f02db3e8ed277048bcc7" -dependencies = [ - "errno", - "libc", - "mach", - "rustc_version", - "windows-sys 0.59.0", -] - [[package]] name = "tar" version = "0.4.46" diff --git a/crates/fosfora-app/Cargo.toml b/crates/fosfora-app/Cargo.toml index 967e8807..b5dc4e27 100644 --- a/crates/fosfora-app/Cargo.toml +++ b/crates/fosfora-app/Cargo.toml @@ -130,16 +130,17 @@ symphonia = { version = "0.6", default-features = false, features = [ ], optional = true } # Embedded clojurust nREPL (`repl` feature): live-code a running instance from # CIDER/Calva/Conjure. Interpreter only: no JIT (Cranelift) and no git-deps -# tree. LOCAL ONLY: path deps on a clojurust checkout next to this repo -# (../clojurust beside ../fosfora). Cargo resolves optional path deps even when -# the feature is off, so a clone without that sibling cannot build at all. -# Switch to a pinned git rev before this goes upstream. -cljrs-runtime = { path = "../../../clojurust/crates/cljrs-runtime", default-features = false, features = ["regex-full"], optional = true } -cljrs-stdlib = { path = "../../../clojurust/crates/cljrs-stdlib", default-features = false, features = ["regex-full"], optional = true } -cljrs-nrepl = { path = "../../../clojurust/crates/cljrs-nrepl", default-features = false, features = ["regex-full"], optional = true } -cljrs-value = { path = "../../../clojurust/crates/cljrs-value", default-features = false, features = ["regex-full"], optional = true } -cljrs-gc = { path = "../../../clojurust/crates/cljrs-gc", optional = true } -cljrs-reader = { path = "../../../clojurust/crates/cljrs-reader", optional = true } +# tree. Pinned to a rev of the BuddhiLW/clojurust fork's `staging` branch +# (upstream csm/clojurust plus fixes still under review upstream). To build +# against a local clojurust checkout instead, put a +# [patch."https://github.com/BuddhiLW/clojurust"] table with path entries in a +# .cargo/config.toml above this repo. +cljrs-runtime = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", default-features = false, features = ["regex-full"], optional = true } +cljrs-stdlib = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", default-features = false, features = ["regex-full"], optional = true } +cljrs-nrepl = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", default-features = false, features = ["regex-full"], optional = true } +cljrs-value = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", default-features = false, features = ["regex-full"], optional = true } +cljrs-gc = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", optional = true } +cljrs-reader = { git = "https://github.com/BuddhiLW/clojurust", rev = "5cf0d706", optional = true } [target.'cfg(target_os = "linux")'.dependencies] # v4l2 loopback output (GH #6). MIT; already in the graph transitively via nokhwa. diff --git a/crates/fosfora-app/src/repl/media.rs b/crates/fosfora-app/src/repl/media.rs index bbd5a319..c6982424 100644 --- a/crates/fosfora-app/src/repl/media.rs +++ b/crates/fosfora-app/src/repl/media.rs @@ -31,8 +31,12 @@ fn is_media(path: &Path) -> bool { .and_then(|e| e.to_str()) .map(str::to_ascii_lowercase) .is_some_and(|e| { - crate::media::decoder::VIDEO_EXTENSIONS.contains(&e.as_str()) - || IMAGE_EXTENSIONS.contains(&e.as_str()) + // Video files only load when the `video` feature is on. + #[cfg(feature = "video")] + if crate::media::decoder::VIDEO_EXTENSIONS.contains(&e.as_str()) { + return true; + } + IMAGE_EXTENSIONS.contains(&e.as_str()) }) } diff --git a/crates/fosfora-app/src/repl/mod.rs b/crates/fosfora-app/src/repl/mod.rs index f27f2534..8f8af9f6 100644 --- a/crates/fosfora-app/src/repl/mod.rs +++ b/crates/fosfora-app/src/repl/mod.rs @@ -133,6 +133,7 @@ fn boot(port: u16, bridge: Bridge) -> anyhow::Result { let config = cljrs_nrepl::Config { addr: ([127, 0, 0, 1], port).into(), port_file: Some(PathBuf::from(".nrepl-port")), + ..Default::default() }; let server = cljrs_nrepl::start(config, globals).map_err(|e| anyhow::anyhow!("nREPL: {e:?}"))?; From ecc218a4fbfa0d045dcc7c3f9c6e56380194a369 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Branquinho Date: Sun, 8 Nov 2026 17:33:27 -0300 Subject: [PATCH 9/9] CHANGELOG: live REPL, extra effect folders, opt-in hardware decode --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08fbcabb..99ecf800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ - **Color Grade node.** Lift, gamma and gain, temperature and tint, hue and saturation in one node, with `shadows` and `highlights` colors that push the dark and bright ends toward a color. Temperature and tint keep brightness, so sweeping them from a binding only moves the color. - **Tempo-synced LFOs as binding sources.** Bind any control to a sine, triangle, saw, square or sample-and-hold LFO from 4 bars down to 1/16, with an optional phase offset, from the new LFO group in the binding matrix or as `lfo.sine.bar` / `lfo.saw.beat.90` in a bindings file; this used to need the external Smart LFO bridge. - **Seven more blend modes: Lighten, Darken, Soft Light, Hue, Saturation, Color and Luminosity,** in the layer menus and the web remote, numbered 13–19 over OSC. A preset that says `SoftLight` used to load as Overlay and now gets the real, gentler Soft Light; set it to Overlay to keep the old look. A blend driven from the binding bus still sweeps only modes 0–9. +- **Live-code a running instance from your editor.** Build with `--features repl`, start with `FOSFORA_NREPL=` (0 picks a free one) and connect CIDER, Calva or Conjure; `(require '[fosfora :as f])` is preloaded, and `f/media!` opens an image or video as a new layer. Off by default and not in the release downloads. +- **Your own effect folders.** `FOSFORA_EFFECT_DIRS` and `FOSFORA_TRAMA_DIRS` list extra directories (split like `PATH`) whose `.pfx` layer effects and trama `.wgsl` effects load next to the built-in ones and hot-reload when an external tool writes to them. +- **Opt-in hardware video decode for media layers.** `FOSFORA_HWACCEL=cuda` (or `vaapi`, `qsv`, `vulkan`, `auto`) hands decoding to the GPU and uses a cheaper color conversion, cutting CPU time on a 1080p clip from 17 s to 6.5 s; unset or `none`, nothing changes. ### Changed - **Audio analysis picks up each block of samples as it arrives instead of checking every 10 ms,** so features and beats reach the visuals up to 10 ms sooner, with that much less frame-to-frame jitter.