Compare commits
289 Commits
main
...
camera-ite
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e7adbca48 | ||
|
|
f80f891636 | ||
|
|
fe9daf168d | ||
|
|
c653c3caa4 | ||
|
|
ce775d8820 | ||
|
|
e9d859961f | ||
|
|
05128f84be | ||
|
|
2a160079bc | ||
|
|
b60c756d8b | ||
|
|
70ab9c5c1d | ||
|
|
ff7b8273c8 | ||
|
|
5c8563f165 | ||
|
|
98e2740116 | ||
|
|
838ed9cbc0 | ||
|
|
12328ef1e2 | ||
|
|
d1a67296fc | ||
|
|
0d11a9ff03 | ||
|
|
c35e44565f | ||
|
|
a15045bba8 | ||
|
|
13b9509d90 | ||
|
|
68d3fc309b | ||
|
|
2f5584d545 | ||
|
|
cb7f3ef4ac | ||
|
|
7095e1df84 | ||
|
|
ee8f789f74 | ||
|
|
1519acc799 | ||
|
|
1d90657def | ||
|
|
cf6285abe0 | ||
|
|
9ae8efb9d6 | ||
|
|
6304541985 | ||
|
|
1babfdf10d | ||
|
|
218089a21d | ||
|
|
120b0a246e | ||
|
|
a5b1370c3e | ||
|
|
c9edcaf281 | ||
|
|
ce8e943c1f | ||
|
|
0dc5b1a92f | ||
|
|
93d35e17e6 | ||
|
|
b2c4e8e72c | ||
|
|
52af8095d6 | ||
|
|
102fc2ef79 | ||
|
|
9042a004d3 | ||
|
|
5360230ca7 | ||
|
|
a4d3ec537f | ||
|
|
ba3faf2c76 | ||
|
|
aa3c892f39 | ||
|
|
de0295fbf8 | ||
|
|
10034cdab2 | ||
|
|
faf6a8f22a | ||
|
|
821e4ae499 | ||
|
|
38709f5eaf | ||
|
|
601a140a70 | ||
|
|
4976c1ce4f | ||
|
|
d3e558c2b4 | ||
|
|
a525b2c39b | ||
|
|
b7cb0b747b | ||
|
|
d56091472e | ||
|
|
8c2c1b0c14 | ||
|
|
2c979c84c4 | ||
|
|
2c3e4bd135 | ||
|
|
4616b9b3bd | ||
|
|
0257655eba | ||
|
|
ccdab7578a | ||
|
|
fc2abfe59c | ||
|
|
7282684104 | ||
|
|
dac3a3092a | ||
|
|
4ffab6580d | ||
|
|
1047f5242b | ||
|
|
eedf5446e8 | ||
|
|
824b1bdd26 | ||
|
|
814d631bb8 | ||
|
|
fe28d6670e | ||
|
|
37413a3e74 | ||
|
|
8ad5fc6584 | ||
|
|
06dfba48d7 | ||
|
|
f7ad007d54 | ||
|
|
7a556439ca | ||
|
|
945514aa31 | ||
|
|
3b557cd851 | ||
|
|
95ecef3426 | ||
|
|
a85504a71a | ||
|
|
eb2ace5d29 | ||
|
|
1923b71985 | ||
|
|
a87823dcc8 | ||
|
|
1c0290aff5 | ||
|
|
e131b2b00f | ||
|
|
728573191f | ||
|
|
cee1137269 | ||
|
|
c6af3298a2 | ||
|
|
11074c9d05 | ||
|
|
aa6ec1afd8 | ||
|
|
a524f2020b | ||
|
|
e2ce337036 | ||
|
|
34c57254a9 | ||
|
|
d7bf01c417 | ||
|
|
59d049e7bd | ||
|
|
c05921fd71 | ||
|
|
5cbd4d807f | ||
|
|
23fe6ead25 | ||
|
|
b3247f5ba8 | ||
|
|
269e5afcc9 | ||
|
|
20070eb778 | ||
|
|
c8206b14dd | ||
|
|
e9089e83a9 | ||
|
|
34faa98dc8 | ||
|
|
187d6ee7d1 | ||
|
|
f7a9976760 | ||
|
|
f42c8963e7 | ||
|
|
f17b4c8ee2 | ||
|
|
47410fe63a | ||
|
|
04d0a77093 | ||
|
|
97c2b82fa7 | ||
|
|
343962fd6f | ||
|
|
2c07278c72 | ||
|
|
bdadc527d2 | ||
|
|
7045904693 | ||
|
|
c45dc36310 | ||
|
|
ed724dcb0f | ||
|
|
c4234becb9 | ||
|
|
cac2be7a4f | ||
|
|
84ba4b47ec | ||
|
|
aea681b5c6 | ||
|
|
7c17dbddb2 | ||
|
|
3f56d1e029 | ||
|
|
2a145de758 | ||
|
|
54ad2667c3 | ||
|
|
fa6ea5adc5 | ||
|
|
ee5a226c5e | ||
|
|
3e0860073d | ||
|
|
ec7f9a1681 | ||
|
|
779431be04 | ||
|
|
683b0c20a1 | ||
|
|
341da038ea | ||
|
|
367066381d | ||
|
|
2f5760c902 | ||
|
|
9072c34d50 | ||
|
|
aefa6dc62f | ||
|
|
35f42559ac | ||
|
|
63ead46a2c | ||
|
|
5682718713 | ||
|
|
58b9ee0f01 | ||
|
|
57039a3ccb | ||
|
|
0fec416731 | ||
|
|
f2c9f91a6b | ||
|
|
aef78f12b5 | ||
|
|
c19a5bcd91 | ||
|
|
de3ada1b5d | ||
|
|
d95b3f55e5 | ||
|
|
643c4959fb | ||
|
|
1b055c9406 | ||
|
|
42782d0f87 | ||
|
|
b5fc321e36 | ||
|
|
931f857458 | ||
|
|
8ecdd3acc6 | ||
|
|
951ca7c4db | ||
|
|
fee12854ca | ||
|
|
14b9129fca | ||
|
|
55ac288391 | ||
|
|
0c15f54bdc | ||
|
|
c42f69cb69 | ||
|
|
7818ae4add | ||
|
|
c09d565dc5 | ||
|
|
9076a0b897 | ||
|
|
37f283b393 | ||
|
|
40e7c6cd20 | ||
|
|
4620e0ca09 | ||
|
|
33d12787c9 | ||
|
|
d7b895f0f4 | ||
|
|
fb027de390 | ||
|
|
19c0ec42ac | ||
|
|
baea2c71a3 | ||
|
|
0694848c9b | ||
|
|
fd95988a42 | ||
|
|
24d8fefcaa | ||
|
|
a434c2aa55 | ||
|
|
c46e01e280 | ||
|
|
529b86fa91 | ||
|
|
b5e87d5911 | ||
|
|
063be773e0 | ||
|
|
3dadbccfff | ||
|
|
cbf5af88b9 | ||
|
|
f46e870864 | ||
|
|
2358ee899a | ||
|
|
4f50772b92 | ||
|
|
c52472fe3c | ||
|
|
52011bd92f | ||
|
|
be97de8a2e | ||
|
|
e9e4d3599d | ||
|
|
6d7d7be3f1 | ||
|
|
d5a951d3c6 | ||
|
|
1b0ef4b7df | ||
|
|
66879148c6 | ||
|
|
7108cfa365 | ||
|
|
05e60c43ce | ||
|
|
75359427c9 | ||
|
|
ba938dfaf9 | ||
|
|
c3721872e0 | ||
|
|
73f3d99877 | ||
|
|
1a5edee751 | ||
|
|
27d75d3098 | ||
|
|
198b342dec | ||
|
|
457ff55904 | ||
|
|
795780130c | ||
|
|
7a69d64e46 | ||
|
|
5350adef98 | ||
|
|
1ae7e4afaa | ||
|
|
e0e5f3b3bc | ||
|
|
d1522a05e8 | ||
|
|
4f1d03196a | ||
|
|
3cf65e5c7b | ||
|
|
3f08e1e212 | ||
|
|
a0d97c8385 | ||
|
|
95629422a7 | ||
|
|
46b3d21fca | ||
|
|
2ce9bf8cd4 | ||
|
|
1f745fddde | ||
|
|
45998df389 | ||
|
|
fe22451be5 | ||
|
|
57ea51fb40 | ||
|
|
2d0865e73c | ||
|
|
8556046b25 | ||
|
|
c0c0dddf23 | ||
|
|
c2665938d9 | ||
|
|
6b7a074fea | ||
|
|
bdd5cbe8b0 | ||
|
|
6068ad5089 | ||
|
|
2de49d981b | ||
|
|
246cf5ceea | ||
|
|
17583471bf | ||
|
|
868621a68c | ||
|
|
769fa24c1d | ||
|
|
89f4aae1ff | ||
|
|
aa79cb2147 | ||
|
|
0cb039c1be | ||
|
|
697320d1c0 | ||
|
|
9f5ad2b9af | ||
|
|
2315aca8b5 | ||
|
|
004454d75e | ||
|
|
4286bc119a | ||
|
|
e7f7f1f062 | ||
|
|
226d674365 | ||
|
|
0dc9dc23e9 | ||
|
|
48719c9862 | ||
|
|
125ab4f74d | ||
|
|
42d2a41d57 | ||
|
|
692ee95500 | ||
|
|
9ec6d31d67 | ||
|
|
e30ad3957a | ||
|
|
35e6e3f1f9 | ||
|
|
597cadd9f2 | ||
|
|
358aaa917a | ||
|
|
6ef154e9b3 | ||
|
|
5874eaef78 | ||
|
|
5b15df42c3 | ||
|
|
c10735acd1 | ||
|
|
2cb000c445 | ||
|
|
77bc08a78b | ||
|
|
fe4930e919 | ||
|
|
d3653e17cb | ||
|
|
418e9fdc54 | ||
|
|
bd62e7d342 | ||
|
|
9da111a3e0 | ||
|
|
b278a43d6d | ||
|
|
892a4165f8 | ||
|
|
088e8e2c9c | ||
|
|
043a1a1b29 | ||
|
|
41e76c9d0a | ||
|
|
6b578fe044 | ||
|
|
e2b00f23ee | ||
|
|
215b91a4bd | ||
|
|
9e0ba504ac | ||
|
|
3cdb6da09c | ||
|
|
3821e14fbd | ||
|
|
e3e7714f1a | ||
|
|
cdbfc27792 | ||
|
|
cbb09a9d97 | ||
|
|
12c2443e5d | ||
|
|
8b38f61e35 | ||
|
|
fad9b7d50c | ||
|
|
60f642364f | ||
|
|
454318b0af | ||
|
|
4e858e4212 | ||
|
|
e4d0159572 | ||
|
|
8ea05e998d | ||
|
|
af3833b263 | ||
|
|
94a78314bb | ||
|
|
a572b215b9 | ||
|
|
b189a6a73c | ||
|
|
44b91bf0e8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,5 +12,4 @@
|
|||||||
|
|
||||||
/libs/
|
/libs/
|
||||||
*.AppImage
|
*.AppImage
|
||||||
*.blend1
|
*.blend1
|
||||||
anchors.txt
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
# The run config is used for both run mode and debug mode
|
|
||||||
|
|
||||||
[[configs]]
|
|
||||||
# the name of this task
|
|
||||||
name = "task"
|
|
||||||
|
|
||||||
# the type of the debugger. If not set, it can't be debugged but can still be run
|
|
||||||
# type = "lldb"
|
|
||||||
|
|
||||||
# the program to run
|
|
||||||
program = "./target/debug/stardust-xr-server"
|
|
||||||
|
|
||||||
# the program arguments, e.g. args = ["arg1", "arg2"], optional
|
|
||||||
# args = []
|
|
||||||
|
|
||||||
# current working directory, optional
|
|
||||||
cwd = "${workspace}"
|
|
||||||
|
|
||||||
# enviroment variables, optional
|
|
||||||
# [configs.env]
|
|
||||||
# VAR1 = "VAL1"
|
|
||||||
# VAR2 = "VAL2"
|
|
||||||
|
|
||||||
# task to run before the run/debug session is started, optional
|
|
||||||
# [configs.prelaunch]
|
|
||||||
# program = "cargo"
|
|
||||||
# args = [
|
|
||||||
# "build",
|
|
||||||
# ]
|
|
||||||
2614
Cargo.lock
generated
2614
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
141
Cargo.toml
141
Cargo.toml
@@ -1,119 +1,102 @@
|
|||||||
[package]
|
[package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
|
||||||
name = "stardust-xr-server"
|
name = "stardust-xr-server"
|
||||||
version = "0.45.0"
|
version = "0.43.0"
|
||||||
authors = ["Nova King <technobaboo@proton.me>"]
|
authors = ["Nova King <technobaboo@proton.me>"]
|
||||||
description = "Stardust XR reference display server"
|
description = "Stardust XR reference display server"
|
||||||
license = "GPLv2"
|
license = "GPLv2"
|
||||||
repository = "https://github.com/StardustXR/stardust-xr-server/"
|
repository = "https://github.com/StardustXR/stardust-xr-server/"
|
||||||
homepage = "https://stardustxr.org"
|
homepage = "https://stardustxr.org"
|
||||||
|
|
||||||
[workspace]
|
|
||||||
members = ["codegen"]
|
|
||||||
|
|
||||||
[workspace.dependencies.stardust-xr]
|
|
||||||
git = "https://github.com/StardustXR/core.git"
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "stardust-xr-server"
|
name = "stardust-xr-server"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["wayland"]
|
default = ["wayland"]
|
||||||
wayland = ["dep:smithay", "dep:wayland-scanner", "dep:wayland-backend"]
|
wayland = ["dep:smithay", "dep:xkbcommon"]
|
||||||
|
xwayland = ["smithay/xwayland"]
|
||||||
profile_tokio = ["dep:console-subscriber", "tokio/tracing"]
|
profile_tokio = ["dep:console-subscriber", "tokio/tracing"]
|
||||||
profile_app = ["dep:tracing-tracy"]
|
profile_app = ["dep:tracing-tracy"]
|
||||||
local_deps = ["stereokit-rust/force-local-deps"]
|
|
||||||
|
|
||||||
[package.metadata.appimage]
|
[package.metadata.appimage]
|
||||||
auto_link = true
|
auto_link = true
|
||||||
auto_link_exclude_list = [
|
auto_link_exclude_list = [
|
||||||
"libc*",
|
"libc*",
|
||||||
"libdl*",
|
"libdl*",
|
||||||
"libpthread*",
|
"libpthread*",
|
||||||
"ld-linux*",
|
"ld-linux*",
|
||||||
"libGL*",
|
"libGL*",
|
||||||
"libEGL*",
|
"libEGL*",
|
||||||
]
|
]
|
||||||
|
|
||||||
[profile.dev.package."*"]
|
[profile.dev.package."*"]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
debug = true
|
|
||||||
strip = "none"
|
|
||||||
debug-assertions = true
|
|
||||||
overflow-checks = true
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
strip = true
|
||||||
debug = "line-tables-only"
|
lto = true
|
||||||
strip = "none"
|
|
||||||
debug-assertions = true
|
|
||||||
overflow-checks = false
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# small utility thingys
|
color-eyre = { version = "0.6.2", default-features = false }
|
||||||
once_cell = "1.19.0"
|
clap = { version = "4.2.4", features = ["derive"] }
|
||||||
nanoid = "0.4.0"
|
glam = { version = "0.23.0", features = ["mint"] }
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.4.0"
|
||||||
rand = "0.8.5"
|
|
||||||
rustc-hash = "2.0.0"
|
|
||||||
portable-atomic = { version = "1.7.0", features = ["float", "std"] }
|
|
||||||
send_wrapper = "0.6.0"
|
|
||||||
slotmap = "1.0.7"
|
|
||||||
global_counter = "=0.2.2"
|
|
||||||
parking_lot = "0.12.3"
|
|
||||||
|
|
||||||
# rust errors/logging
|
|
||||||
color-eyre = { version = "0.6.3", default-features = false }
|
|
||||||
clap = { version = "4.5.13", features = ["derive"] }
|
|
||||||
console-subscriber = { version = "0.4.0", optional = true }
|
|
||||||
tracing = "0.1.40"
|
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
|
||||||
tracing-tracy = { version = "0.11.1", optional = true }
|
|
||||||
|
|
||||||
# (de)serialization
|
|
||||||
serde = { version = "1.0.205", features = ["derive"] }
|
|
||||||
serde_repr = "0.1.19"
|
|
||||||
toml = "0.8.19"
|
|
||||||
|
|
||||||
# mathy stuffs
|
|
||||||
glam = { version = "0.29.0", features = ["mint", "serde"] }
|
|
||||||
mint = "0.5.9"
|
mint = "0.5.9"
|
||||||
tokio = { version = "1.39.2", features = ["rt-multi-thread", "signal", "time"] }
|
nanoid = "0.4.0"
|
||||||
|
once_cell = "1.17.1"
|
||||||
|
parking_lot = "0.12.1"
|
||||||
|
portable-atomic = { version = "1.2.0", features = ["float", "std"] }
|
||||||
|
rustc-hash = "1.1.0"
|
||||||
|
tokio = { version = "1.27.0", features = ["rt-multi-thread", "signal", "time"] }
|
||||||
|
send_wrapper = "0.6.0"
|
||||||
prisma = "0.1.1"
|
prisma = "0.1.1"
|
||||||
|
directories = "5.0.0"
|
||||||
# linux stuffs
|
serde = { version = "1.0.160", features = ["derive"] }
|
||||||
libc = "0.2.155"
|
serde_repr = "0.1.16"
|
||||||
nix = "0.29.0"
|
tracing = "0.1.37"
|
||||||
input-event-codes = "6.2.0"
|
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
|
||||||
zbus = { version = "4.4.0", default-features = false, features = ["tokio"] }
|
global_counter = "0.2.2"
|
||||||
directories = "5.0.1"
|
rand = "0.8.5"
|
||||||
xkbcommon-rs = "0.1.0"
|
atty = "0.2.14"
|
||||||
|
xkbcommon = { version = "0.6.0", default-features = false, optional = true }
|
||||||
# wayland
|
ctrlc = "3.4.1"
|
||||||
wayland-backend = { version = "0.3.7", optional = true, default-features = false }
|
libc = "0.2.148"
|
||||||
wayland-scanner = { version = "0.31.4", optional = true }
|
input-event-codes = "5.16.8"
|
||||||
|
drm-fourcc = { version = "2.2.0", features = ["serde"] }
|
||||||
|
|
||||||
[dependencies.smithay]
|
[dependencies.smithay]
|
||||||
# git = "https://github.com/technobaboo/smithay.git"
|
# git = "https://github.com/technobaboo/smithay.git" # Until we get stereokit to understand OES samplers and external textures
|
||||||
# git = "https://github.com/colinmarc/smithay.git"
|
git = "https://github.com/smithay/smithay.git" # Until we get stereokit to understand OES samplers and external textures
|
||||||
git = "https://github.com/smithay/smithay.git"
|
|
||||||
# path = "../smithay"
|
# path = "../smithay"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["desktop", "backend_drm", "renderer_gl", "wayland_frontend"]
|
features = [
|
||||||
|
"desktop",
|
||||||
|
"backend_drm",
|
||||||
|
"backend_egl",
|
||||||
|
"renderer_gl",
|
||||||
|
"wayland_frontend",
|
||||||
|
]
|
||||||
|
version = "*"
|
||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
|
[dependencies.stereokit]
|
||||||
[dependencies.stereokit-rust]
|
|
||||||
# path = "../StereoKit-rust"
|
|
||||||
git = "https://github.com/mvvvv/StereoKit-rust.git"
|
|
||||||
# git = "https://github.com/technobaboo/StereoKit-rust.git"
|
|
||||||
features = ["no-event-loop"]
|
|
||||||
default-features = false
|
default-features = false
|
||||||
|
features = ["linux-egl"]
|
||||||
|
version = "0.16.9"
|
||||||
|
|
||||||
|
[dependencies.console-subscriber]
|
||||||
|
version = "0.1.8"
|
||||||
|
optional = true
|
||||||
|
|
||||||
|
[dependencies.tracing-tracy]
|
||||||
|
version = "0.10.4"
|
||||||
|
optional = true
|
||||||
|
|
||||||
[dependencies.stardust-xr]
|
[dependencies.stardust-xr]
|
||||||
workspace = true
|
git = "https://github.com/StardustXR/core.git"
|
||||||
|
branch = "camera-item"
|
||||||
|
|
||||||
[dependencies.stardust-xr-server-codegen]
|
# [patch.crates-io.stereokit]
|
||||||
path = "codegen"
|
# path = "../stereokit-rs"
|
||||||
|
# [patch.crates-io.stereokit-sys]
|
||||||
|
# path = "../stereokit-sys"
|
||||||
|
|||||||
203
README.md
203
README.md
@@ -1,164 +1,85 @@
|
|||||||
# Stardust XR Server
|
# Stardust XR Reference Server
|
||||||
|
|
||||||
Stardust XR is a display server for VR and AR headsets on Linux-based systems. [Stardust provides a 3D environment](https://www.youtube.com/watch?v=v2WblwbaLaA), where anything from 2D windows (including your existing apps!), to 3D apps built from objects, can exist together in physical space.
|
This project is a usable Linux display server that reinvents human-computer interaction for all kinds of XR, from putting 2D/XR apps into various 3D shells for varying uses to SDF-based interaction.
|
||||||
|
|
||||||

|
## Prerequisites
|
||||||
|
1. Cargo
|
||||||
|
2. CMake
|
||||||
|
3. EGL+GLES 3.2
|
||||||
|
4. GLX+Xlib
|
||||||
|
5. fontconfig
|
||||||
|
6. dlopen
|
||||||
|
7. OpenXR Loader (required even if run in flatscreen mode)
|
||||||
|
|
||||||
## Core Dependencies
|
## Build
|
||||||
| **Dependency** | **Ubuntu/Debian** | **Arch Linux** | **Fedora** |
|
|
||||||
|-----------------------------|-------------------------------------------------------------------------------------------------|---------------------------------------------------|-------------------------------------------------------------|
|
|
||||||
| **Cargo** | `cargo` | `cargo` | `cargo` |
|
|
||||||
| **CMake** | `cmake` | `cmake` | `cmake` |
|
|
||||||
| **EGL+GLES 3.2** | `libegl1-mesa-dev`, `libgles2-mesa-dev` | `mesa` *(provides EGL/GLES libraries and headers)* | `mesa-libEGL-devel`, `mesa-libGLES-devel` |
|
|
||||||
| **GLX+Xlib** | `libx11-dev`, `libxfixes-dev`, `libxcb1-dev`, `libgl1-mesa-dev`, `libxkbcommon-dev` | `libx11`, `libxfixes`, `libxcb` *(and GLX via mesa)*| `libX11-devel`, `libXfixes-devel`, `libxcb-devel`, `mesa-libGL-devel` *(or equivalent)* |
|
|
||||||
| **fontconfig** | `libfontconfig1-dev` | `fontconfig` | `fontconfig-devel` |
|
|
||||||
| **dlopen** (glibc function) | Provided by `libc6-dev` (part of the core C library) | Provided by `glibc` *(included in base-devel)* | Provided by `glibc-devel` |
|
|
||||||
| **OpenXR Loader** | `libopenxr-loader1`, `libopenxr-dev`, `libopenxr1-monado` | `openxr` | `openxr-devel` |
|
|
||||||
|
|
||||||
Command line installation of core & dynamic dependencies are provided below:
|
|
||||||
<details>
|
|
||||||
<summary>Ubuntu/Debian</summary>
|
|
||||||
<pre><code class="language-bash">
|
|
||||||
sudo apt-get update && sudo apt-get install -y \
|
|
||||||
build-essential \
|
|
||||||
cargo \
|
|
||||||
cmake \
|
|
||||||
libegl1-mesa-dev libgles2-mesa-dev \
|
|
||||||
libx11-dev libxfixes-dev libxcb1-dev libxau-dev libgl1-mesa-dev libxkbcommon-dev \
|
|
||||||
libfontconfig1-dev libfreetype6-dev libharfbuzz-dev libgraphite2-dev \
|
|
||||||
libc6-dev \
|
|
||||||
libopenxr-loader1 libopenxr-dev libopenxr1-monado libwayland-dev \
|
|
||||||
libjsoncpp-dev libdrm-dev libexpat1-dev libxcb-randr0-dev \
|
|
||||||
libxml2-dev libffi-dev libbz2-dev libpng-dev libbrotli-dev liblzma-dev libglib2.0-dev libpcre2-dev
|
|
||||||
</code></pre>
|
|
||||||
</details>
|
|
||||||
|
|
||||||
<details>
|
|
||||||
<summary>Arch Linux</summary>
|
|
||||||
<pre><code class="language-bash">
|
|
||||||
sudo pacman -Syu --needed \
|
|
||||||
base-devel \
|
|
||||||
rust \
|
|
||||||
cmake \
|
|
||||||
mesa \
|
|
||||||
libx11 \
|
|
||||||
libxfixes \
|
|
||||||
libxcb \
|
|
||||||
libxkbcommon \
|
|
||||||
fontconfig \
|
|
||||||
freetype2 \
|
|
||||||
openxr \
|
|
||||||
jsoncpp \
|
|
||||||
libffi \
|
|
||||||
wayland \
|
|
||||||
expat \
|
|
||||||
libxml2 \
|
|
||||||
libxau \
|
|
||||||
bzip2 \
|
|
||||||
xz \
|
|
||||||
libpng \
|
|
||||||
brotli \
|
|
||||||
pcre2 \
|
|
||||||
glib2 \
|
|
||||||
libdrm
|
|
||||||
</code></pre>
|
|
||||||
</details>
|
|
||||||
<details>
|
|
||||||
<summary>Fedora</summary>
|
|
||||||
<pre><code class="language-bash">
|
|
||||||
sudo dnf group install development-tools && \
|
|
||||||
sudo dnf install -y \
|
|
||||||
cargo \
|
|
||||||
cmake \
|
|
||||||
mesa-libEGL-devel \
|
|
||||||
mesa-libGLES-devel \
|
|
||||||
libX11-devel \
|
|
||||||
libXfixes-devel \
|
|
||||||
libxcb-devel \
|
|
||||||
libxkbcommon-devel \
|
|
||||||
fontconfig-devel \
|
|
||||||
freetype-devel \
|
|
||||||
harfbuzz-devel \
|
|
||||||
graphite2-devel \
|
|
||||||
openxr-devel \
|
|
||||||
wayland-devel \
|
|
||||||
jsoncpp-devel \
|
|
||||||
libdrm-devel \
|
|
||||||
expat-devel \
|
|
||||||
xcb-util-devel \
|
|
||||||
libxml2-devel \
|
|
||||||
libXau-devel \
|
|
||||||
bzip2-devel \
|
|
||||||
xz-devel \
|
|
||||||
libpng-devel \
|
|
||||||
brotli-devel \
|
|
||||||
pcre2-devel \
|
|
||||||
glib2-devel
|
|
||||||
</code></pre>
|
|
||||||
</details>
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
More detailed instructions and walkthroughs are provided at https://www.stardustxr.org
|
|
||||||
|
|
||||||
The [Terra Repository](https://terra.fyralabs.com/) is required, and comes pre-installed with [Ultramarine Linux](https://ultramarine-linux.org/). Other Fedora Editions and derivatives can directly install terra-release:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sudo dnf install --nogpgcheck --repofrompath 'terra,https://repos.fyralabs.com/terra$releasever' terra-release
|
|
||||||
```
|
|
||||||
|
|
||||||
For a full installation of the Stardust XR server *and* a selected group of clients, run:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sudo dnf group install stardust-xr
|
|
||||||
```
|
|
||||||
|
|
||||||
## Manual Build
|
|
||||||
We've provided a manual installation script [here](https://github.com/cyberneticmelon/usefulscripts/blob/main/stardustxr_setup.sh) that clones and builds the Stardust XR server along with a number of other clients from their respective repositories, and provides a startup script for automatically launching some clients.
|
|
||||||
|
|
||||||
After cloning the repository
|
|
||||||
```bash
|
```bash
|
||||||
cargo build
|
cargo build
|
||||||
```
|
```
|
||||||
|
The latest stable server is automatically built to an appimage at https://github.com/StardustXR/server/releases for easy testing.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
> [!NOTE]
|
|
||||||
> For help with setting up an XR headset on linux, visit https://stardustxr.org/docs/get-started/setup-openxr
|
|
||||||
|
|
||||||
|
First, try running `cargo run` in a terminal window. If a headset is plugged in and OpenXR is working no window will show up. However, the headset should show the same things as the window that opens:
|
||||||
|
|
||||||
The **Stardust XR Server** is a server that runs clients, so without any running, you will see a black screen. If you only have the server installed, we recommend also cloning and building the following clients to start: [Flatland](https://github.com/StardustXR/flatland), which allows normal 2D apps to run in Stardust, [Protostar](https://github.com/StardustXR/protostar), which contains Hexagon Launcher, an app launcher menu, and [Black Hole](https://github.com/StardustXR/black-hole) to quickly tuck away your objects and apps (kind of like desktop peek on Windows).
|

|
||||||
|
|
||||||
First, try running `cargo run -- -f` in a terminal window to check out flatscreen mode, (or `stardust-xr-server -f` / `stardust-xr-server_dev -f` if you installed via dnf or the manual installation script, respectively, as they provide symlinks.)
|
Stardust won't do anything interesting without clients! Try some from https://github.com/StardustXR.
|
||||||
|
|
||||||
If there aren't already any clients running, you'll need to manually launch them by either navigating to their repositories and running `cargo run`, or running them via their names if you installed via dnf or the manual installation script, such as `flatland`, `hexagon_launcher`, etc.
|
### Default Sky
|
||||||
|
|
||||||
> [!IMPORTANT]
|
You can set a default skytex/skylight by putting your favorite HDRI equirectangular sky in `~/.config/stardust/skytex.hdr`. Certain clients can override this.
|
||||||
> [Flatland](https://github.com/StardustXR/flatland) must be running for 2D apps to launch.
|
|
||||||
|
|
||||||
### Startup Script
|
Flatscreen mode when the default skybox is [Zhengyang Gate](https://polyhaven.com/a/zhengyang_gate):
|
||||||
A startup script can be created at `~/.config/stardust/startup` that will launch specified settings and clients/applications, an example of which is shown [here](https://github.com/cyberneticmelon/usefulscripts/blob/main/startup). If you used the [installation script](https://github.com/cyberneticmelon/usefulscripts/blob/main/stardustxr_setup.sh), one will have already been made for you. This allows wide flexibility of what clients to launch upon startup (and, for example, *where*, using the [Gravity](https://github.com/StardustXR/gravity) client to specify X Y and Z co-ordinates).
|

|
||||||
|
|
||||||
### Flatscreen Navigation
|
### Windowed Mode
|
||||||
A video guide showcasing flatscreen controls is available [here](https://www.youtube.com/watch?v=JCYecSlKlDI)
|
|
||||||
|
|
||||||
To move around, hold down `Shift + W A S D`, with `Q` for moving down and `E` for moving up.
|
If the stardust server can't connect to an OpenXR runtime or you force it into flatscreen mode with `-f`, the server will show in a window.
|
||||||

|

|
||||||
|
|
||||||
To look around, hold down `Shift + Right` Click while moving the mouse.
|
You can navigate around by right click + dragging to look around, Shift+W/A/S/D/Q/E to move. If you have a virtual hand, left click pinches, right click points, both make a fist.
|
||||||

|
|
||||||
|
|
||||||
To drag applications out of the app launcher, hold down `Shift + ~`
|
### Flags
|
||||||

|
#### Flatscreen (-f)
|
||||||
|
|
||||||
### XR Navigation
|
The server will show up in windowed mode no matter what with your mouse pointer being turned into a 3D pointer. Keyboard input will be sent to whatever your mouse is hovering over like visionOS simulator.
|
||||||
A video guide showcasing XR controls is available [here](https://www.youtube.com/watch?v=RbxFq6JjliA)
|
Flatscreen mode upon initial startup:
|
||||||
|

|
||||||
|
|
||||||
**Quest 3 Hand tracking**:
|
#### Overlay (-o \<PRIORITY>)
|
||||||
Pinch to drag and drop, grasp with full hand for grabbing, point and click with pointer finger to click or pinch from a distance
|
|
||||||
|
|
||||||

|
The server will, if in XR mode, be overlaid using the OpenXR overlay extension with the given priority.
|
||||||
|
|
||||||
**Quest 3 Controller**:
|
#### Disable controller (--disable-controller)
|
||||||
Grab with the grip buttons, click by touching the tip of the cones or by using the trigger from a distance
|
|
||||||
|
|
||||||

|
Some runtimes such as Monado may emulate a controller using a hand, and this messes with Stardust's input system. Set this flag to ignore the controllers that the OpenXR runtime provides.
|
||||||
|
|
||||||
|
#### Execute (-e </path/to/executable>)
|
||||||
|
|
||||||
|
When wayland and OpenXR and such are initialized, run the given executable (such as a bash script) with all the environment variables needed to connect all clients of any type to the server. If not set, the server will run the executable at `~/.config/stardust/startup` if it exists. This is how stardust desktop environments can be made.
|
||||||
|
|
||||||
|
#### Help (-h, --help)
|
||||||
|
|
||||||
|
help
|
||||||
|
|
||||||
|
## Test
|
||||||
|
|
||||||
|
##### Gnome Graphical Integration Test
|
||||||
|
|
||||||
|
- `nix build .#gnome-graphical-test`
|
||||||
|
|
||||||
|
This test uses Nix to reproducibly execute a QEMU virtual machine which
|
||||||
|
spawns a full Gnome desktop. It runs `monado-service`, `stardust-xr-server`
|
||||||
|
`flatland` underneath of Gnome and then attaches `weston-cliptest` to the
|
||||||
|
`flatland` process running underneath of `stardust-xr-server`, the result is
|
||||||
|
a screenshot in PNG format that should look like expected. If any process in
|
||||||
|
this test produces an exit code above 0, the test will fail, graphical bugs
|
||||||
|
should be visible in the screenshot. An example of the result is below.
|
||||||
|
|
||||||
|
###### Result
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
##### Everything
|
||||||
|
|
||||||
|
`nix flake check` will build every test underneath of the `checks` attribute in the `flake.nix`
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
[package]
|
|
||||||
edition = "2021"
|
|
||||||
name = "stardust-xr-server-codegen"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
convert_case = "0.6.0"
|
|
||||||
quote = "1.0.33"
|
|
||||||
mint = "0.5.9"
|
|
||||||
proc-macro2 = "1.0.71"
|
|
||||||
split-iter = "0.1.0"
|
|
||||||
|
|
||||||
stardust-xr = { workspace = true }
|
|
||||||
@@ -1,617 +0,0 @@
|
|||||||
use convert_case::{Case, Casing};
|
|
||||||
use proc_macro2::{Ident, Span, TokenStream};
|
|
||||||
use quote::{quote, ToTokens};
|
|
||||||
use split_iter::Splittable;
|
|
||||||
use stardust_xr::schemas::protocol::*;
|
|
||||||
|
|
||||||
fn fold_tokens(a: TokenStream, b: TokenStream) -> TokenStream {
|
|
||||||
quote!(#a #b)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_root_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(ROOT_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_node_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(NODE_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_spatial_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(SPATIAL_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_field_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(FIELD_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_data_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(DATA_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_audio_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(AUDIO_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_drawable_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(DRAWABLE_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_input_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(INPUT_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_item_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(ITEM_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_item_camera_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(ITEM_CAMERA_PROTOCOL)
|
|
||||||
}
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn codegen_item_panel_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
codegen_protocol(ITEM_PANEL_PROTOCOL)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn codegen_protocol(protocol: &'static str) -> proc_macro::TokenStream {
|
|
||||||
let protocol = Protocol::parse(protocol).unwrap();
|
|
||||||
let interface = protocol
|
|
||||||
.interface
|
|
||||||
.map(|p| {
|
|
||||||
let node_id = p.node_id;
|
|
||||||
let node_id = quote! {
|
|
||||||
const INTERFACE_NODE_ID: u64 = #node_id;
|
|
||||||
};
|
|
||||||
let aspect = generate_aspect(&Aspect {
|
|
||||||
name: "interface".to_string(),
|
|
||||||
description: protocol.description.clone(),
|
|
||||||
inherits: vec![],
|
|
||||||
members: p.members,
|
|
||||||
});
|
|
||||||
quote!(#node_id #aspect)
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
|
||||||
let custom_enums = protocol
|
|
||||||
.custom_enums
|
|
||||||
.iter()
|
|
||||||
.map(generate_custom_enum)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let custom_unions = protocol
|
|
||||||
.custom_unions
|
|
||||||
.iter()
|
|
||||||
.map(generate_custom_union)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let custom_structs = protocol
|
|
||||||
.custom_structs
|
|
||||||
.iter()
|
|
||||||
.map(generate_custom_struct)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let aspects = protocol
|
|
||||||
.aspects
|
|
||||||
.iter()
|
|
||||||
.map(generate_aspect)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
quote!(#custom_enums #custom_unions #custom_structs #aspects #interface).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_custom_enum(custom_enum: &CustomEnum) -> TokenStream {
|
|
||||||
let name = Ident::new(&custom_enum.name.to_case(Case::Pascal), Span::call_site());
|
|
||||||
let description = &custom_enum.description;
|
|
||||||
|
|
||||||
let argument_decls = custom_enum
|
|
||||||
.variants
|
|
||||||
.iter()
|
|
||||||
.map(|a| Ident::new(&a.to_case(Case::Pascal), Span::call_site()).to_token_stream())
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
#[derive(Debug, Clone, Copy, serde_repr::Deserialize_repr, serde_repr::Serialize_repr)]
|
|
||||||
#[repr(u32)]
|
|
||||||
pub enum #name {#argument_decls}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_custom_union(custom_union: &CustomUnion) -> TokenStream {
|
|
||||||
let name = Ident::new(&custom_union.name.to_case(Case::Pascal), Span::call_site());
|
|
||||||
let description = &custom_union.description;
|
|
||||||
|
|
||||||
let option_decls = custom_union
|
|
||||||
.options
|
|
||||||
.iter()
|
|
||||||
.map(generate_union_option)
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum #name {#option_decls}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_union_option(union_option: &UnionOption) -> TokenStream {
|
|
||||||
let name = union_option
|
|
||||||
.name
|
|
||||||
.as_ref()
|
|
||||||
.map(|n| n.to_case(Case::Pascal))
|
|
||||||
.unwrap_or_else(|| argument_type_option_name(&union_option._type));
|
|
||||||
let description = union_option
|
|
||||||
.description
|
|
||||||
.as_ref()
|
|
||||||
.map(|d| quote!(#[doc = #d]))
|
|
||||||
.unwrap_or_default();
|
|
||||||
let identifier = Ident::new(&name, Span::call_site());
|
|
||||||
let _type = generate_argument_type(&union_option._type, false, true);
|
|
||||||
quote! (#description #identifier(#_type))
|
|
||||||
}
|
|
||||||
fn generate_custom_struct(custom_struct: &CustomStruct) -> TokenStream {
|
|
||||||
let name = Ident::new(&custom_struct.name.to_case(Case::Pascal), Span::call_site());
|
|
||||||
let description = &custom_struct.description;
|
|
||||||
|
|
||||||
let argument_decls = custom_struct
|
|
||||||
.fields
|
|
||||||
.iter()
|
|
||||||
.map(|a| generate_argument_decl(a, true))
|
|
||||||
.map(|d| quote!(pub #d))
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub struct #name {#argument_decls}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_aspect(aspect: &Aspect) -> TokenStream {
|
|
||||||
let description = &aspect.description;
|
|
||||||
let (client_members, server_members) = aspect.members.iter().split(|m| m.side == Side::Server);
|
|
||||||
|
|
||||||
let client_mod_name = Ident::new(
|
|
||||||
&format!("{}_client", &aspect.name.to_case(Case::Snake)),
|
|
||||||
Span::call_site(),
|
|
||||||
);
|
|
||||||
let client_side_members = client_members
|
|
||||||
.map(generate_member)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.map(|t| {
|
|
||||||
// TODO: properly import all dependencies
|
|
||||||
quote! {
|
|
||||||
pub mod #client_mod_name {
|
|
||||||
use super::*;
|
|
||||||
#t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let aspect_trait_name = Ident::new(
|
|
||||||
&format!("{}Aspect", &aspect.name.to_case(Case::Pascal)),
|
|
||||||
Span::call_site(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let opcodes = aspect
|
|
||||||
.members
|
|
||||||
.iter()
|
|
||||||
.map(|m| {
|
|
||||||
let aspect_name = aspect.name.to_case(Case::ScreamingSnake);
|
|
||||||
let member_name = m.name.to_case(Case::ScreamingSnake);
|
|
||||||
let name_type = if m.side == Side::Client {
|
|
||||||
"CLIENT"
|
|
||||||
} else {
|
|
||||||
"SERVER"
|
|
||||||
};
|
|
||||||
let name = Ident::new(
|
|
||||||
&format!("{aspect_name}_{member_name}_{name_type}_OPCODE"),
|
|
||||||
Span::call_site(),
|
|
||||||
);
|
|
||||||
let opcode = m.opcode;
|
|
||||||
|
|
||||||
quote!(pub(crate) const #name: u64 = #opcode;)
|
|
||||||
})
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let alias_info = generate_alias_info(aspect);
|
|
||||||
|
|
||||||
let server_side_members = server_members
|
|
||||||
.map(generate_member)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let add_node_members = aspect
|
|
||||||
.members
|
|
||||||
.iter()
|
|
||||||
.filter(|m| m.side == Side::Server)
|
|
||||||
.map(generate_handler)
|
|
||||||
.reduce(fold_tokens)
|
|
||||||
.map(|members| {
|
|
||||||
quote! {
|
|
||||||
fn add_node_members(node: &crate::nodes::Node) {
|
|
||||||
#members
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
|
||||||
let server_side_members = quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
pub trait #aspect_trait_name {
|
|
||||||
#add_node_members
|
|
||||||
#server_side_members
|
|
||||||
}
|
|
||||||
};
|
|
||||||
quote!(#opcodes #alias_info #client_side_members #server_side_members)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_alias_opcodes(aspect: &Aspect, side: Side, _type: MemberType) -> TokenStream {
|
|
||||||
aspect
|
|
||||||
.members
|
|
||||||
.iter()
|
|
||||||
.filter(|m| m.side == side)
|
|
||||||
.filter(|m| m._type == _type)
|
|
||||||
.map(|m| m.opcode)
|
|
||||||
.map(|o| quote!(#o))
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
fn generate_alias_info(aspect: &Aspect) -> TokenStream {
|
|
||||||
let aspect_alias_info_name = Ident::new(
|
|
||||||
&format!(
|
|
||||||
"{}_ASPECT_ALIAS_INFO",
|
|
||||||
aspect.name.to_case(Case::ScreamingSnake)
|
|
||||||
),
|
|
||||||
Span::call_site(),
|
|
||||||
);
|
|
||||||
let local_signals = generate_alias_opcodes(aspect, Side::Server, MemberType::Signal);
|
|
||||||
let local_methods = generate_alias_opcodes(aspect, Side::Server, MemberType::Method);
|
|
||||||
let remote_signals = generate_alias_opcodes(aspect, Side::Client, MemberType::Signal);
|
|
||||||
|
|
||||||
let inherits = aspect
|
|
||||||
.inherits
|
|
||||||
.iter()
|
|
||||||
.map(|a| {
|
|
||||||
Ident::new(
|
|
||||||
&format!("{}_ASPECT_ALIAS_INFO", a.to_case(Case::ScreamingSnake)),
|
|
||||||
Span::call_site(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.map(|a| quote!(#a.clone()))
|
|
||||||
.fold(quote!(), |a, b| quote!(#a + #b));
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
pub static ref #aspect_alias_info_name: crate::nodes::alias::AliasInfo = crate::nodes::alias::AliasInfo {
|
|
||||||
server_signals: vec![#local_signals],
|
|
||||||
server_methods: vec![#local_methods],
|
|
||||||
client_signals: vec![#remote_signals],
|
|
||||||
}
|
|
||||||
#inherits;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_member(member: &Member) -> TokenStream {
|
|
||||||
let id = member.opcode;
|
|
||||||
let name = Ident::new(&member.name.to_case(Case::Snake), Span::call_site());
|
|
||||||
let description = &member.description;
|
|
||||||
|
|
||||||
let side = member.side;
|
|
||||||
let _type = member._type;
|
|
||||||
|
|
||||||
let first_args = match member.side {
|
|
||||||
Side::Server => {
|
|
||||||
quote!(_node: std::sync::Arc<crate::nodes::Node>, _calling_client: std::sync::Arc<crate::core::client::Client>)
|
|
||||||
}
|
|
||||||
Side::Client => quote!(_node: &crate::nodes::Node),
|
|
||||||
};
|
|
||||||
let argument_decls = member
|
|
||||||
.arguments
|
|
||||||
.iter()
|
|
||||||
.map(|a| generate_argument_decl(a, member.side == Side::Server))
|
|
||||||
.fold(first_args, |a, b| quote!(#a, #b));
|
|
||||||
|
|
||||||
let argument_uses = member
|
|
||||||
.arguments
|
|
||||||
.iter()
|
|
||||||
.map(|a| generate_argument_serialize(&a.name, &a._type, a.optional))
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default();
|
|
||||||
let return_type = member
|
|
||||||
.return_type
|
|
||||||
.as_ref()
|
|
||||||
.map(|r| generate_argument_type(r, false, true))
|
|
||||||
.unwrap_or_else(|| quote!(()));
|
|
||||||
|
|
||||||
match (side, _type) {
|
|
||||||
(Side::Client, MemberType::Method) => {
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
pub async fn #name(#argument_decls) -> color_eyre::eyre::Result<(#return_type, Vec<std::os::fd::OwnedFd>)> {
|
|
||||||
_node.execute_remote_method_typed(#id, &(#argument_uses), vec![]).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(Side::Client, MemberType::Signal) => {
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
pub fn #name(#argument_decls) -> color_eyre::eyre::Result<()> {
|
|
||||||
let serialized = stardust_xr::schemas::flex::serialize((#argument_uses))?;
|
|
||||||
_node.send_remote_signal(#id, serialized)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(Side::Server, MemberType::Method) => {
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
fn #name(#argument_decls) -> impl std::future::Future<Output = color_eyre::eyre::Result<#return_type>> + Send + 'static;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(Side::Server, MemberType::Signal) => {
|
|
||||||
quote! {
|
|
||||||
#[doc = #description]
|
|
||||||
fn #name(#argument_decls) -> color_eyre::eyre::Result<()>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_handler(member: &Member) -> TokenStream {
|
|
||||||
let opcode = member.opcode;
|
|
||||||
let member_name_ident = Ident::new(&member.name, Span::call_site());
|
|
||||||
|
|
||||||
let argument_names = member
|
|
||||||
.arguments
|
|
||||||
.iter()
|
|
||||||
.map(generate_argument_name)
|
|
||||||
.reduce(|a, b| quote!(#a, #b));
|
|
||||||
let argument_types = member
|
|
||||||
.arguments
|
|
||||||
.iter()
|
|
||||||
.map(|a| {
|
|
||||||
let _type = convert_deserializeable_argument_type(&a._type);
|
|
||||||
generate_argument_type(&_type, a.optional, true)
|
|
||||||
})
|
|
||||||
.reduce(|a, b| quote!(#a, #b));
|
|
||||||
// dbg!(&argument_types);
|
|
||||||
let deserialize = argument_names
|
|
||||||
.clone()
|
|
||||||
.zip(argument_types)
|
|
||||||
.map(|(argument_names, argument_types)| {
|
|
||||||
quote!(let (#argument_names): (#argument_types) = stardust_xr::schemas::flex::deserialize(_message.as_ref())?;)
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
|
||||||
let serialize = generate_argument_serialize(
|
|
||||||
"result",
|
|
||||||
&member.return_type.clone().unwrap_or(ArgumentType::Empty),
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
let argument_uses = member
|
|
||||||
.arguments
|
|
||||||
.iter()
|
|
||||||
.map(|a| generate_argument_deserialize(&a.name, &a._type, a.optional))
|
|
||||||
.reduce(|a, b| quote!(#a, #b))
|
|
||||||
.unwrap_or_default();
|
|
||||||
match member._type {
|
|
||||||
MemberType::Signal => quote! {
|
|
||||||
node.add_local_signal(#opcode, |_node, _calling_client, _message| {
|
|
||||||
#deserialize
|
|
||||||
Self::#member_name_ident(_node, _calling_client.clone(), #argument_uses)
|
|
||||||
});
|
|
||||||
},
|
|
||||||
MemberType::Method => quote! {
|
|
||||||
node.add_local_method(#opcode, |_node, _calling_client, _message, _method_response| {
|
|
||||||
_method_response.wrap_async(async move {
|
|
||||||
#deserialize
|
|
||||||
let result = Self::#member_name_ident(_node, _calling_client.clone(), #argument_uses).await?;
|
|
||||||
Ok((#serialize, Vec::new()))
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_argument_name(argument: &Argument) -> TokenStream {
|
|
||||||
Ident::new(&argument.name.to_case(Case::Snake), Span::call_site()).to_token_stream()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert_deserializeable_argument_type(argument_type: &ArgumentType) -> ArgumentType {
|
|
||||||
match argument_type {
|
|
||||||
ArgumentType::Node { .. } => ArgumentType::NodeID,
|
|
||||||
ArgumentType::Vec(v) => {
|
|
||||||
ArgumentType::Vec(Box::new(convert_deserializeable_argument_type(v.as_ref())))
|
|
||||||
}
|
|
||||||
ArgumentType::Map(v) => {
|
|
||||||
ArgumentType::Map(Box::new(convert_deserializeable_argument_type(v.as_ref())))
|
|
||||||
}
|
|
||||||
f => f.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_argument_deserialize(
|
|
||||||
argument_name: &str,
|
|
||||||
argument_type: &ArgumentType,
|
|
||||||
optional: bool,
|
|
||||||
) -> TokenStream {
|
|
||||||
let name = Ident::new(&argument_name.to_case(Case::Snake), Span::call_site());
|
|
||||||
if let ArgumentType::Node { .. } = argument_type {
|
|
||||||
return match optional {
|
|
||||||
true => quote!(#name.map(|n| _calling_client.get_node(#argument_name, n)?)),
|
|
||||||
false => quote!(_calling_client.get_node(#argument_name, #name)?),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if optional {
|
|
||||||
let mapping = generate_argument_deserialize("o", argument_type, false);
|
|
||||||
return quote!(#name.map(|o| Ok::<_, color_eyre::eyre::Report>(#mapping)).transpose()?);
|
|
||||||
}
|
|
||||||
|
|
||||||
match argument_type {
|
|
||||||
ArgumentType::Color => quote!(color::rgba_linear!(#name[0], #name[1], #name[2], #name[3])),
|
|
||||||
ArgumentType::Vec(v) => {
|
|
||||||
let mapping = generate_argument_deserialize("a", v, false);
|
|
||||||
quote!(#name.into_iter().map(|a| Ok(#mapping)).collect::<color_eyre::eyre::Result<Vec<_>>>()?)
|
|
||||||
}
|
|
||||||
ArgumentType::Map(v) => {
|
|
||||||
let mapping = generate_argument_deserialize("a", v, false);
|
|
||||||
quote!(#name.into_iter().map(|(k, a)| Ok((k, #mapping))).collect::<color_eyre::eyre::Result<rustc_hash::FxHashMap<String, _>>>()?)
|
|
||||||
}
|
|
||||||
_ => quote!(#name),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_argument_serialize(
|
|
||||||
argument_name: &str,
|
|
||||||
argument_type: &ArgumentType,
|
|
||||||
optional: bool,
|
|
||||||
) -> TokenStream {
|
|
||||||
let name = Ident::new(&argument_name.to_case(Case::Snake), Span::call_site());
|
|
||||||
match argument_type {
|
|
||||||
ArgumentType::Node {
|
|
||||||
_type,
|
|
||||||
return_id_parameter_name: _,
|
|
||||||
} => match optional {
|
|
||||||
true => quote!(#name.map(|n| n.get_id())),
|
|
||||||
false => quote!(#name.get_id()),
|
|
||||||
},
|
|
||||||
ArgumentType::Color => quote!([#name.c.r, #name.c.g, #name.c.b, #name.a]),
|
|
||||||
ArgumentType::Vec(v) => {
|
|
||||||
let mapping = generate_argument_serialize("a", v, false);
|
|
||||||
quote!(#name.into_iter().map(|a| Ok(#mapping)).collect::<color_eyre::eyre::Result<Vec<_>>>()?)
|
|
||||||
}
|
|
||||||
ArgumentType::Map(v) => {
|
|
||||||
let mapping = generate_argument_serialize("a", v, false);
|
|
||||||
quote!(#name.into_iter().map(|(k, a)| Ok((k, #mapping))).collect::<color_eyre::eyre::Result<rustc_hash::FxHashMap<String, _>>>()?)
|
|
||||||
}
|
|
||||||
_ => quote!(#name),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_argument_decl(argument: &Argument, owned_values: bool) -> TokenStream {
|
|
||||||
let name = Ident::new(&argument.name.to_case(Case::Snake), Span::call_site());
|
|
||||||
let mut _type = generate_argument_type(&argument._type, argument.optional, owned_values);
|
|
||||||
quote!(#name: #_type)
|
|
||||||
}
|
|
||||||
fn argument_type_option_name(argument_type: &ArgumentType) -> String {
|
|
||||||
match argument_type {
|
|
||||||
ArgumentType::Empty => "Empty".to_string(),
|
|
||||||
ArgumentType::Bool => "Bool".to_string(),
|
|
||||||
ArgumentType::Int => "Int".to_string(),
|
|
||||||
ArgumentType::UInt => "UInt".to_string(),
|
|
||||||
ArgumentType::Float => "Float".to_string(),
|
|
||||||
ArgumentType::Vec2(_) => "Vec2".to_string(),
|
|
||||||
ArgumentType::Vec3(_) => "Vec3".to_string(),
|
|
||||||
ArgumentType::Quat => "Quat".to_string(),
|
|
||||||
ArgumentType::Mat4 => "Mat4".to_string(),
|
|
||||||
ArgumentType::Color => "Color".to_string(),
|
|
||||||
ArgumentType::String => "String".to_string(),
|
|
||||||
ArgumentType::Bytes => "Bytes".to_string(),
|
|
||||||
ArgumentType::Vec(v) => format!("{}Vector", argument_type_option_name(v)),
|
|
||||||
ArgumentType::Map(m) => format!("{}Map", argument_type_option_name(m)),
|
|
||||||
ArgumentType::NodeID => "Node ID".to_string(),
|
|
||||||
ArgumentType::Datamap => "Datamap".to_string(),
|
|
||||||
ArgumentType::ResourceID => "ResourceID".to_string(),
|
|
||||||
ArgumentType::Enum(e) => e.clone(),
|
|
||||||
ArgumentType::Union(u) => u.clone(),
|
|
||||||
ArgumentType::Struct(s) => s.clone(),
|
|
||||||
ArgumentType::Node { _type, .. } => _type.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn generate_argument_type(
|
|
||||||
argument_type: &ArgumentType,
|
|
||||||
optional: bool,
|
|
||||||
owned: bool,
|
|
||||||
) -> TokenStream {
|
|
||||||
let _type = match argument_type {
|
|
||||||
ArgumentType::Empty => quote!(()),
|
|
||||||
ArgumentType::Bool => quote!(bool),
|
|
||||||
ArgumentType::Int => quote!(i32),
|
|
||||||
ArgumentType::UInt => quote!(u32),
|
|
||||||
ArgumentType::Float => quote!(f32),
|
|
||||||
ArgumentType::Vec2(t) => {
|
|
||||||
let t = generate_argument_type(t, false, true);
|
|
||||||
quote!(stardust_xr::values::Vector2<#t>)
|
|
||||||
}
|
|
||||||
ArgumentType::Vec3(t) => {
|
|
||||||
let t = generate_argument_type(t, false, true);
|
|
||||||
quote!(stardust_xr::values::Vector3<#t>)
|
|
||||||
}
|
|
||||||
ArgumentType::Quat => quote!(stardust_xr::values::Quaternion),
|
|
||||||
ArgumentType::Mat4 => quote!(stardust_xr::values::Mat4),
|
|
||||||
ArgumentType::Color => quote!(stardust_xr::values::Color),
|
|
||||||
ArgumentType::Bytes => {
|
|
||||||
if !owned {
|
|
||||||
quote!(&[u8])
|
|
||||||
} else {
|
|
||||||
quote!(Vec<u8>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::String => {
|
|
||||||
if !owned {
|
|
||||||
quote!(&str)
|
|
||||||
} else {
|
|
||||||
quote!(String)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::Vec(t) => {
|
|
||||||
let t = generate_argument_type(t, false, true);
|
|
||||||
if !owned {
|
|
||||||
quote!(&[#t])
|
|
||||||
} else {
|
|
||||||
quote!(Vec<#t>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::Map(t) => {
|
|
||||||
let t = generate_argument_type(t, false, true);
|
|
||||||
|
|
||||||
if !owned {
|
|
||||||
quote!(&stardust_xr::values::Map<String, #t>)
|
|
||||||
} else {
|
|
||||||
quote!(stardust_xr::values::Map<String, #t>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::NodeID => quote!(u64),
|
|
||||||
ArgumentType::Datamap => {
|
|
||||||
if !owned {
|
|
||||||
quote!(&stardust_xr::values::Datamap)
|
|
||||||
} else {
|
|
||||||
quote!(stardust_xr::values::Datamap)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::ResourceID => {
|
|
||||||
if !owned {
|
|
||||||
quote!(&stardust_xr::values::ResourceID)
|
|
||||||
} else {
|
|
||||||
quote!(stardust_xr::values::ResourceID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::Enum(e) => {
|
|
||||||
let enum_name = Ident::new(&e.to_case(Case::Pascal), Span::call_site());
|
|
||||||
quote!(#enum_name)
|
|
||||||
}
|
|
||||||
ArgumentType::Union(u) => {
|
|
||||||
let union_name = Ident::new(&u.to_case(Case::Pascal), Span::call_site());
|
|
||||||
quote!(#union_name)
|
|
||||||
}
|
|
||||||
ArgumentType::Struct(s) => {
|
|
||||||
let struct_name = Ident::new(&s.to_case(Case::Pascal), Span::call_site());
|
|
||||||
if !owned {
|
|
||||||
quote!(&#struct_name)
|
|
||||||
} else {
|
|
||||||
quote!(#struct_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArgumentType::Node {
|
|
||||||
_type,
|
|
||||||
return_id_parameter_name: _,
|
|
||||||
} => {
|
|
||||||
if !owned {
|
|
||||||
quote!(&std::sync::Arc<crate::nodes::Node>)
|
|
||||||
} else {
|
|
||||||
quote!(std::sync::Arc<crate::nodes::Node>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if optional {
|
|
||||||
quote!(Option<#_type>)
|
|
||||||
} else {
|
|
||||||
_type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
202
flake.lock
generated
202
flake.lock
generated
@@ -1,23 +1,24 @@
|
|||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"crane": {
|
"fenix": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": [
|
"nixpkgs": [
|
||||||
"flatland",
|
"flatland",
|
||||||
"nixpkgs"
|
"nixpkgs"
|
||||||
]
|
],
|
||||||
|
"rust-analyzer-src": "rust-analyzer-src"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1712681629,
|
"lastModified": 1678775037,
|
||||||
"narHash": "sha256-bMDXn4AkTXLCpoZbII6pDGoSeSe9gI87jxPsHRXgu/E=",
|
"narHash": "sha256-chx0tWnXKpcayPkPY3Qh+2hNwptvX8XE3o+fYZ+GNzg=",
|
||||||
"owner": "ipetkov",
|
"owner": "nix-community",
|
||||||
"repo": "crane",
|
"repo": "fenix",
|
||||||
"rev": "220387ac8e99cbee0ca4c95b621c4bc782b6a235",
|
"rev": "ee59e1c769657b1e27e608f8b981fa8f6b715583",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "ipetkov",
|
"owner": "nix-community",
|
||||||
"repo": "crane",
|
"repo": "fenix",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -26,11 +27,11 @@
|
|||||||
"nixpkgs-lib": "nixpkgs-lib"
|
"nixpkgs-lib": "nixpkgs-lib"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1722555600,
|
"lastModified": 1690933134,
|
||||||
"narHash": "sha256-XOQkdLafnb/p9ij77byFQjDf5m5QYl9b2REiVClC+x4=",
|
"narHash": "sha256-ab989mN63fQZBFrkk4Q8bYxQCktuHmBIBqUG1jl6/FQ=",
|
||||||
"owner": "hercules-ci",
|
"owner": "hercules-ci",
|
||||||
"repo": "flake-parts",
|
"repo": "flake-parts",
|
||||||
"rev": "8471fe90ad337a8074e957b69ca4d0089218391d",
|
"rev": "59cf3f1447cfc75087e7273b04b31e689a8599fb",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -41,17 +42,14 @@
|
|||||||
},
|
},
|
||||||
"flake-parts_2": {
|
"flake-parts_2": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs-lib": [
|
"nixpkgs-lib": "nixpkgs-lib_2"
|
||||||
"hercules-ci-effects",
|
|
||||||
"nixpkgs"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1712014858,
|
"lastModified": 1688466019,
|
||||||
"narHash": "sha256-sB4SWl2lX95bExY2gMFG5HIzvva5AVMJd4Igm+GpZNw=",
|
"narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=",
|
||||||
"owner": "hercules-ci",
|
"owner": "hercules-ci",
|
||||||
"repo": "flake-parts",
|
"repo": "flake-parts",
|
||||||
"rev": "9126214d0a59633752a136528f5f3b9aa8565b7d",
|
"rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -59,17 +57,39 @@
|
|||||||
"type": "indirect"
|
"type": "indirect"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"flake-parts_3": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs-lib": [
|
||||||
|
"hercules-ci-effects",
|
||||||
|
"hercules-ci-agent",
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1688466019,
|
||||||
|
"narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=",
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"flatland": {
|
"flatland": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"crane": "crane",
|
"fenix": "fenix",
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1713050562,
|
"lastModified": 1694190588,
|
||||||
"narHash": "sha256-m7c6XpmpTM1URuqMG2KqtaWbL2Vt8vJFJtmvq123BmY=",
|
"narHash": "sha256-tcvp09A54AbXkkXS/P6Ed5TUWzMEuQ9h/fnfLz7gnJc=",
|
||||||
"owner": "StardustXR",
|
"owner": "StardustXR",
|
||||||
"repo": "flatland",
|
"repo": "flatland",
|
||||||
"rev": "b3b0f29c4ea1b82c96cf9de507837bf15a5e4c0e",
|
"rev": "3867067452761959a95497d6589f9336b347270c",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -78,17 +98,53 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"hercules-ci-effects": {
|
"haskell-flake": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1684780604,
|
||||||
|
"narHash": "sha256-2uMZsewmRn7rRtAnnQNw1lj0uZBMh4m6Cs/7dV5YF08=",
|
||||||
|
"owner": "srid",
|
||||||
|
"repo": "haskell-flake",
|
||||||
|
"rev": "74210fa80a49f1b6f67223debdbf1494596ff9f2",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "srid",
|
||||||
|
"ref": "0.3.0",
|
||||||
|
"repo": "haskell-flake",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"hercules-ci-agent": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-parts": "flake-parts_2",
|
"flake-parts": "flake-parts_3",
|
||||||
|
"haskell-flake": "haskell-flake",
|
||||||
"nixpkgs": "nixpkgs_2"
|
"nixpkgs": "nixpkgs_2"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1719226092,
|
"lastModified": 1688568579,
|
||||||
"narHash": "sha256-YNkUMcCUCpnULp40g+svYsaH1RbSEj6s4WdZY/SHe38=",
|
"narHash": "sha256-ON0M56wtY/TIIGPkXDlJboAmuYwc73Hi8X9iJGtxOhM=",
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "hercules-ci-agent",
|
||||||
|
"rev": "367dd8cd649b57009a6502e878005a1e54ad78c5",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "hercules-ci-agent",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"hercules-ci-effects": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-parts": "flake-parts_2",
|
||||||
|
"hercules-ci-agent": "hercules-ci-agent",
|
||||||
|
"nixpkgs": "nixpkgs_3"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1689397210,
|
||||||
|
"narHash": "sha256-fVxZnqxMbsDkB4GzGAs/B41K0wt/e+B/fLxmTFF/S20=",
|
||||||
"owner": "hercules-ci",
|
"owner": "hercules-ci",
|
||||||
"repo": "hercules-ci-effects",
|
"repo": "hercules-ci-effects",
|
||||||
"rev": "11e4b8dc112e2f485d7c97e1cee77f9958f498f5",
|
"rev": "0a63bfa3f00a3775ea3a6722b247880f1ffe91ce",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -99,39 +155,63 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1712791164,
|
"lastModified": 1678703398,
|
||||||
"narHash": "sha256-3sbWO1mbpWsLepZGbWaMovSO7ndZeFqDSdX0hZ9nVyw=",
|
"narHash": "sha256-Y1mW3dBsoWLHpYm+UIHb5VZ7rx024NNHaF16oZBx++o=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "1042fd8b148a9105f3c0aca3a6177fd1d9360ba5",
|
"rev": "67f26c1cfc5d5783628231e776a81c1ade623e0b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"ref": "nixos-unstable",
|
"ref": "nixos-22.11",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nixpkgs-lib": {
|
"nixpkgs-lib": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1722555339,
|
"dir": "lib",
|
||||||
"narHash": "sha256-uFf2QeW7eAHlYXuDktm9c25OxOyCoUOQmh5SZ9amE5Q=",
|
"lastModified": 1690881714,
|
||||||
"type": "tarball",
|
"narHash": "sha256-h/nXluEqdiQHs1oSgkOOWF+j8gcJMWhwnZ9PFabN6q0=",
|
||||||
"url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz"
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "9e1960bc196baf6881340d53dccb203a951745a2",
|
||||||
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"type": "tarball",
|
"dir": "lib",
|
||||||
"url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz"
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs-lib_2": {
|
||||||
|
"locked": {
|
||||||
|
"dir": "lib",
|
||||||
|
"lastModified": 1688049487,
|
||||||
|
"narHash": "sha256-100g4iaKC9MalDjUW9iN6Jl/OocTDtXdeAj7pEGIRh4=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "4bc72cae107788bf3f24f30db2e2f685c9298dc9",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"dir": "lib",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nixpkgs_2": {
|
"nixpkgs_2": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1713714899,
|
"lastModified": 1688322751,
|
||||||
"narHash": "sha256-+z/XjO3QJs5rLE5UOf015gdVauVRQd2vZtsFkaXBq2Y=",
|
"narHash": "sha256-eW62dC5f33oKZL7VWlomttbUnOTHrAbte9yNUNW8rbk=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "6143fc5eeb9c4f00163267708e26191d1e918932",
|
"rev": "0fbe93c5a7cac99f90b60bdf5f149383daaa615f",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -143,11 +223,26 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs_3": {
|
"nixpkgs_3": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1723991338,
|
"lastModified": 1689393711,
|
||||||
"narHash": "sha256-Grh5PF0+gootJfOJFenTTxDTYPidA3V28dqJ/WV7iis=",
|
"narHash": "sha256-l3gyTPy/qWLDk1CY1EgYwlsxcGxN2aVd7MlCzQa69rk=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "27fcd46fa18df36d270174246e7bd8f1787129ff",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs_4": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1692734709,
|
||||||
|
"narHash": "sha256-SCFnyHCyYjwEmgUsHDDuU0TsbVMKeU1vwkR+r7uS2Rg=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "8a3354191c0d7144db9756a74755672387b702ba",
|
"rev": "b85ed9dcbf187b909ef7964774f8847d554fab3b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -162,7 +257,24 @@
|
|||||||
"flake-parts": "flake-parts",
|
"flake-parts": "flake-parts",
|
||||||
"flatland": "flatland",
|
"flatland": "flatland",
|
||||||
"hercules-ci-effects": "hercules-ci-effects",
|
"hercules-ci-effects": "hercules-ci-effects",
|
||||||
"nixpkgs": "nixpkgs_3"
|
"nixpkgs": "nixpkgs_4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-analyzer-src": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1678695923,
|
||||||
|
"narHash": "sha256-rDhiiU8P6sf6mgj5IKgCuTRN9uYeqWr6xl4XLkKnMWg=",
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"rev": "95497533524537b1cc7a2870ce94b0b14503be8b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"ref": "nightly",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"type": "github"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
45
flake.nix
45
flake.nix
@@ -1,9 +1,7 @@
|
|||||||
{
|
{
|
||||||
nixConfig = {
|
nixConfig = {
|
||||||
extra-substituters = [ "https://stardustxr.cachix.org" ];
|
extra-substituters = [ "https://stardustxr.cachix.org" ];
|
||||||
extra-trusted-public-keys = [
|
extra-trusted-public-keys = [ "stardustxr.cachix.org-1:mWSn8Ap2RLsIWT/8gsj+VfbJB6xoOkPaZpbjO+r9HBo=" ];
|
||||||
"stardustxr.cachix.org-1:mWSn8Ap2RLsIWT/8gsj+VfbJB6xoOkPaZpbjO+r9HBo="
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
@@ -14,44 +12,37 @@
|
|||||||
# it to create VM Tests
|
# it to create VM Tests
|
||||||
flatland.url = "github:StardustXR/flatland";
|
flatland.url = "github:StardustXR/flatland";
|
||||||
};
|
};
|
||||||
outputs =
|
outputs = inputs@{ self, flake-parts, nixpkgs, hercules-ci-effects, flatland, ... }:
|
||||||
inputs@{ self, flake-parts, nixpkgs, hercules-ci-effects, flatland, ... }:
|
|
||||||
let
|
let
|
||||||
name = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.name;
|
name = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.name;
|
||||||
src = builtins.path {
|
src = builtins.path {
|
||||||
name = "${name}-source";
|
name = "${name}-source";
|
||||||
path = toString ./.;
|
path = toString ./.;
|
||||||
filter = path: type:
|
filter = path: type:
|
||||||
nixpkgs.lib.all (n: builtins.baseNameOf path != n) [
|
nixpkgs.lib.all
|
||||||
|
(n: builtins.baseNameOf path != n)
|
||||||
|
[
|
||||||
"flake.nix"
|
"flake.nix"
|
||||||
"flake.lock"
|
"flake.lock"
|
||||||
"nix"
|
"nix"
|
||||||
"README.md"
|
"README.md"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
in flake-parts.lib.mkFlake { inherit inputs; } {
|
in
|
||||||
imports = [ flake-parts.flakeModules.easyOverlay ];
|
flake-parts.lib.mkFlake { inherit inputs; } {
|
||||||
|
imports = [
|
||||||
|
flake-parts.flakeModules.easyOverlay
|
||||||
|
];
|
||||||
systems = [ "aarch64-linux" "x86_64-linux" "riscv64-linux" ];
|
systems = [ "aarch64-linux" "x86_64-linux" "riscv64-linux" ];
|
||||||
perSystem = { config, self', inputs', pkgs, system, ... }: {
|
perSystem = { config, self', inputs', pkgs, system, ... }: {
|
||||||
_module.args.pkgs = import inputs.nixpkgs {
|
_module.args.pkgs = import inputs.nixpkgs { inherit system; overlays = [ inputs.self.overlays.default ]; };
|
||||||
inherit system;
|
|
||||||
overlays = [ inputs.self.overlays.default ];
|
|
||||||
};
|
|
||||||
overlayAttrs = config.packages;
|
overlayAttrs = config.packages;
|
||||||
packages = let sk_gpu = pkgs.callPackage ./nix/sk_gpu.nix { };
|
packages = {
|
||||||
in {
|
|
||||||
default = self'.packages.${name};
|
default = self'.packages.${name};
|
||||||
gnome-graphical-test = self'.checks.gnome-graphical-test;
|
gnome-graphical-test = self'.checks.gnome-graphical-test;
|
||||||
"${name}" = pkgs.callPackage ./nix/stardust-xr-server.nix {
|
"${name}" = pkgs.callPackage ./nix/stardust-xr-server.nix { inherit name src; };
|
||||||
inherit name src sk_gpu;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
apps.default = {
|
checks.gnome-graphical-test = pkgs.nixosTest (import ./nix/gnome-graphical-test.nix { inherit pkgs self; });
|
||||||
type = "app";
|
|
||||||
program = self'.packages.${name} + "/bin/stardust-xr-server";
|
|
||||||
};
|
|
||||||
checks.gnome-graphical-test = pkgs.nixosTest
|
|
||||||
(import ./nix/gnome-graphical-test.nix { inherit pkgs self; });
|
|
||||||
devShells.default = pkgs.mkShell {
|
devShells.default = pkgs.mkShell {
|
||||||
inputsFrom = [ self'.packages.default ];
|
inputsFrom = [ self'.packages.default ];
|
||||||
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
|
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
|
||||||
@@ -62,7 +53,7 @@
|
|||||||
effects = let
|
effects = let
|
||||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||||
hci-effects = hercules-ci-effects.lib.withPkgs pkgs;
|
hci-effects = hercules-ci-effects.lib.withPkgs pkgs;
|
||||||
in { ref, rev, ... }: {
|
in { branch, rev, ... }: {
|
||||||
gnome-graphical-test = hci-effects.mkEffect {
|
gnome-graphical-test = hci-effects.mkEffect {
|
||||||
secretsMap."stardustxrDiscord" = "stardustxrDiscord";
|
secretsMap."stardustxrDiscord" = "stardustxrDiscord";
|
||||||
secretsMap."stardustxrIpfs" = "stardustxrIpfs";
|
secretsMap."stardustxrIpfs" = "stardustxrIpfs";
|
||||||
@@ -70,15 +61,13 @@
|
|||||||
readSecretString stardustxrDiscord .webhook > .webhook
|
readSecretString stardustxrDiscord .webhook > .webhook
|
||||||
readSecretString stardustxrIpfs .basicauth > .basicauth
|
readSecretString stardustxrIpfs .basicauth > .basicauth
|
||||||
set -x
|
set -x
|
||||||
export RESPONSE=$(curl -H @.basicauth -F file=@${
|
export RESPONSE=$(curl -H @.basicauth -F file=@${self.packages."x86_64-linux".gnome-graphical-test}/screen.png https://ipfs-api.stardustxr.org/api/v0/add)
|
||||||
self.packages."x86_64-linux".gnome-graphical-test
|
|
||||||
}/screen.png https://ipfs-api.stardustxr.org/api/v0/add)
|
|
||||||
export CID=$(echo "$RESPONSE" | ${pkgs.jq}/bin/jq -r .Hash)
|
export CID=$(echo "$RESPONSE" | ${pkgs.jq}/bin/jq -r .Hash)
|
||||||
set +x
|
set +x
|
||||||
export ADDRESS="https://ipfs.stardustxr.org/ipfs/$CID"
|
export ADDRESS="https://ipfs.stardustxr.org/ipfs/$CID"
|
||||||
${pkgs.discord-sh}/bin/discord.sh \
|
${pkgs.discord-sh}/bin/discord.sh \
|
||||||
--description "\`stardustxr/server\` has been modified, here's how it renders \`weston-cliptest\` on \`flatland\` via \`monado-service\` inside of the \`gnome-graphical-test\`" \
|
--description "\`stardustxr/server\` has been modified, here's how it renders \`weston-cliptest\` on \`flatland\` via \`monado-service\` inside of the \`gnome-graphical-test\`" \
|
||||||
--field "Ref;${ref}" \
|
--field "Branch;${branch}" \
|
||||||
--field "Commit ID;${rev}" \
|
--field "Commit ID;${rev}" \
|
||||||
--field "Flatland Revision;${flatland.rev}" \
|
--field "Flatland Revision;${flatland.rev}" \
|
||||||
--field "Reproducer;\`nix build github:stardustxr/server/${rev}#gnome-graphical-test\`" \
|
--field "Reproducer;\`nix build github:stardustxr/server/${rev}#gnome-graphical-test\`" \
|
||||||
|
|||||||
BIN
img/flatscreen_1.png
Normal file
BIN
img/flatscreen_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.6 KiB |
BIN
img/flatscreen_2.png
Normal file
BIN
img/flatscreen_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
BIN
img/flatscreen_3.png
Normal file
BIN
img/flatscreen_3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 789 KiB |
BIN
img/workflow.png
BIN
img/workflow.png
Binary file not shown.
|
Before Width: | Height: | Size: 872 KiB |
BIN
img/xr_mode_windowed_blank.png
Normal file
BIN
img/xr_mode_windowed_blank.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
@@ -1,27 +0,0 @@
|
|||||||
{ lib, stdenv, fetchFromGitHub, cmake }:
|
|
||||||
|
|
||||||
stdenv.mkDerivation rec {
|
|
||||||
pname = "meshoptimizer";
|
|
||||||
version = "0.20";
|
|
||||||
|
|
||||||
src = fetchFromGitHub {
|
|
||||||
owner = "zeux";
|
|
||||||
repo = "meshoptimizer";
|
|
||||||
rev = "c21d3be6ddf627f8ca852ba4b6db9903b0557858";
|
|
||||||
sha256 = "sha256-QCxpM2g8WtYSZHkBzLTJNQ/oHb5j/n9rjaVmZJcCZIA=";
|
|
||||||
};
|
|
||||||
|
|
||||||
nativeBuildInputs = [ cmake ];
|
|
||||||
|
|
||||||
cmakeFlags = [
|
|
||||||
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
|
|
||||||
];
|
|
||||||
|
|
||||||
meta = with lib; {
|
|
||||||
description = "Mesh optimization library that makes meshes smaller and faster to render";
|
|
||||||
homepage = "https://github.com/zeux/meshoptimizer";
|
|
||||||
license = licenses.mit;
|
|
||||||
platforms = platforms.all;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
{ stdenv, fetchurl, unzip }:
|
|
||||||
|
|
||||||
let
|
|
||||||
sk_gpu_zip = fetchurl {
|
|
||||||
url =
|
|
||||||
"https://github.com/StereoKit/sk_gpu/releases/download/v2024.8.16/sk_gpu.v2024.8.16.zip";
|
|
||||||
sha256 = "sha256-Wk3PZFlWqhrsQ8xG0sQaV2xSasdg2D7TMiPvl/CgtGU=";
|
|
||||||
};
|
|
||||||
in stdenv.mkDerivation rec {
|
|
||||||
name = "sk_gpu";
|
|
||||||
src = sk_gpu_zip;
|
|
||||||
unpackPhase = ''
|
|
||||||
unzip -d $out ${sk_gpu_zip}
|
|
||||||
'';
|
|
||||||
nativeBuildInputs = [ unzip ];
|
|
||||||
}
|
|
||||||
@@ -1,25 +1,17 @@
|
|||||||
{ rustPlatform
|
{ rustPlatform
|
||||||
, src
|
, src
|
||||||
, name
|
, name
|
||||||
|
, openxr-loader
|
||||||
, libGL
|
, libGL
|
||||||
, mesa
|
, mesa
|
||||||
, xorg
|
, xorg
|
||||||
, fontconfig
|
, fontconfig
|
||||||
, libxkbcommon
|
, libxkbcommon
|
||||||
, libclang
|
, libclang
|
||||||
|
|
||||||
, cmake
|
, cmake
|
||||||
, cpm-cmake
|
, cpm-cmake
|
||||||
, pkg-config
|
, pkg-config
|
||||||
, llvmPackages
|
, llvmPackages
|
||||||
, fetchFromGitHub
|
|
||||||
, sk_gpu
|
|
||||||
, libXau
|
|
||||||
|
|
||||||
, libXdmcp
|
|
||||||
, stdenv
|
|
||||||
, lib
|
|
||||||
, openxr-loader
|
|
||||||
}:
|
}:
|
||||||
|
|
||||||
rustPlatform.buildRustPackage rec {
|
rustPlatform.buildRustPackage rec {
|
||||||
@@ -28,59 +20,21 @@ rustPlatform.buildRustPackage rec {
|
|||||||
lockFile = (src + "/Cargo.lock");
|
lockFile = (src + "/Cargo.lock");
|
||||||
allowBuiltinFetchGit = true;
|
allowBuiltinFetchGit = true;
|
||||||
};
|
};
|
||||||
buildFeatures = [ "local_deps" ];
|
|
||||||
FORCE_LOCAL_DEPS = true;
|
|
||||||
CPM_LOCAL_PACKAGES_ONLY = true;
|
|
||||||
CPM_SOURCE_CACHE = "./build";
|
CPM_SOURCE_CACHE = "./build";
|
||||||
CPM_USE_LOCAL_PACKAGES = true;
|
postPatch = ''
|
||||||
CPM_DOWNLOAD_ALL = false;
|
sk=$(echo $cargoDepsCopy/stereokit-sys-*/StereoKit)
|
||||||
|
|
||||||
meshoptimizer = fetchFromGitHub {
|
|
||||||
owner = "zeux";
|
|
||||||
repo = "meshoptimizer";
|
|
||||||
rev = "c21d3be6ddf627f8ca852ba4b6db9903b0557858";
|
|
||||||
sha256 = "sha256-QCxpM2g8WtYSZHkBzLTJNQ/oHb5j/n9rjaVmZJcCZIA=";
|
|
||||||
};
|
|
||||||
basis_universal = fetchFromGitHub {
|
|
||||||
owner = "BinomialLLC";
|
|
||||||
repo = "basis_universal";
|
|
||||||
rev = "900e40fb5d2502927360fe2f31762bdbb624455f";
|
|
||||||
sha256 = "sha256-zBRAXgG5Fi6+5uPQCI/RCGatY6O4ELuYBoKrPNn4K+8=";
|
|
||||||
};
|
|
||||||
|
|
||||||
DEP_MESHOPTIMIZER_SOURCE = "${meshoptimizer}";
|
|
||||||
DEP_BASIS_UNIVERSAL_SOURCE = "${basis_universal}";
|
|
||||||
DEP_SK_GPU_SOURCE = "${sk_gpu}";
|
|
||||||
|
|
||||||
postPatch = let libPath = lib.makeLibraryPath [ stdenv.cc.cc.lib ];
|
|
||||||
in ''
|
|
||||||
sk=$(echo $cargoDepsCopy/stereokit-rust-*/StereoKit)
|
|
||||||
mkdir -p $sk/build/cpm
|
mkdir -p $sk/build/cpm
|
||||||
|
|
||||||
# This is not ideal, the original approach was to fetch the exact cmake
|
# This is not ideal, the original approach was to fetch the exact cmake
|
||||||
# file version that was wanted from GitHub directly, but at least this way it comes from Nixpkgs.. so meh
|
# file version that was wanted from GitHub directly, but at least this way it comes from Nixpkgs.. so meh
|
||||||
cp ${cpm-cmake}/share/cpm/CPM.cmake $sk/build/cpm/CPM_0.38.7.cmake
|
cp ${cpm-cmake}/share/cpm/CPM.cmake $sk/build/cpm/CPM_0.32.2.cmake
|
||||||
mkdir -p $sk/sk_gpu
|
|
||||||
cp -R ${sk_gpu}/* $sk/sk_gpu
|
|
||||||
chmod -R 755 $sk/sk_gpu/tools/linux_x64/*
|
|
||||||
export DEP_SK_GPU_SOURCE=$sk/sk_gpu
|
|
||||||
export LD_LIBRARY_PATH="${stdenv.cc.cc.lib}/lib";
|
|
||||||
patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
|
|
||||||
--set-rpath "${libPath}" \
|
|
||||||
$sk/sk_gpu/tools/linux_x64/skshaderc
|
|
||||||
'';
|
'';
|
||||||
nativeBuildInputs = [ cmake pkg-config llvmPackages.libcxxClang ];
|
nativeBuildInputs = [
|
||||||
|
cmake pkg-config llvmPackages.libcxxClang
|
||||||
|
];
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
libGL
|
openxr-loader libGL mesa xorg.libX11 fontconfig libxkbcommon
|
||||||
mesa
|
|
||||||
xorg.libX11.dev
|
|
||||||
xorg.libXft
|
|
||||||
xorg.libXfixes
|
|
||||||
fontconfig
|
|
||||||
libxkbcommon
|
|
||||||
libXau
|
|
||||||
libXdmcp
|
|
||||||
openxr-loader
|
|
||||||
];
|
];
|
||||||
LIBCLANG_PATH = "${libclang.lib}/lib";
|
LIBCLANG_PATH = "${libclang.lib}/lib";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
src/core/buffers.rs
Normal file
50
src/core/buffers.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use std::ffi::c_void;
|
||||||
|
|
||||||
|
use color_eyre::eyre::{ensure, Result};
|
||||||
|
use smithay::backend::{egl::EGLContext, renderer::gles::GlesRenderer};
|
||||||
|
use stereokit as sk;
|
||||||
|
|
||||||
|
struct EGLRawHandles {
|
||||||
|
display: *const c_void,
|
||||||
|
config: *const c_void,
|
||||||
|
context: *const c_void,
|
||||||
|
}
|
||||||
|
fn get_sk_egl() -> Result<EGLRawHandles> {
|
||||||
|
ensure!(
|
||||||
|
unsafe { sk::sys::backend_graphics_get() }
|
||||||
|
== sk::sys::backend_graphics__backend_graphics_opengles_egl,
|
||||||
|
"StereoKit is not running using EGL!"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(unsafe {
|
||||||
|
EGLRawHandles {
|
||||||
|
display: sk::sys::backend_opengl_egl_get_display() as *const c_void,
|
||||||
|
config: sk::sys::backend_opengl_egl_get_config() as *const c_void,
|
||||||
|
context: sk::sys::backend_opengl_egl_get_context() as *const c_void,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BufferManager {
|
||||||
|
pub renderer: GlesRenderer,
|
||||||
|
}
|
||||||
|
impl BufferManager {
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
let egl_raw_handles = get_sk_egl()?;
|
||||||
|
let renderer = unsafe {
|
||||||
|
GlesRenderer::new(EGLContext::from_raw(
|
||||||
|
egl_raw_handles.display,
|
||||||
|
egl_raw_handles.config,
|
||||||
|
egl_raw_handles.context,
|
||||||
|
)?)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(BufferManager { renderer })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn make_context_current(&self) {
|
||||||
|
unsafe {
|
||||||
|
let _ = self.renderer.egl_context().make_current();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +1,21 @@
|
|||||||
use super::{
|
use super::{
|
||||||
client_state::{ClientStateParsed, CLIENT_STATES},
|
client_state::{ClientState, CLIENT_STATES},
|
||||||
destroy_queue,
|
|
||||||
scenegraph::Scenegraph,
|
scenegraph::Scenegraph,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{registry::OwnedRegistry, task},
|
core::{registry::OwnedRegistry, task},
|
||||||
nodes::{
|
nodes::{audio, data, drawable, fields, hmd, input, items, root::Root, spatial, Node},
|
||||||
audio, data, drawable, fields, input, items,
|
|
||||||
root::{ClientState, Root},
|
|
||||||
spatial, Node,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::{eyre, Result};
|
use color_eyre::eyre::{eyre, Result};
|
||||||
use global_counter::primitive::exact::CounterU32;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use stardust_xr::messenger::{self, MessageSenderHandle};
|
use stardust_xr::{
|
||||||
use std::{fmt::Debug, fs, iter::FromIterator, path::PathBuf, sync::Arc};
|
messenger::{self, MessageSenderHandle},
|
||||||
|
schemas::flex::serialize,
|
||||||
|
};
|
||||||
|
use std::{fs, iter::FromIterator, path::PathBuf, sync::Arc};
|
||||||
use tokio::{net::UnixStream, task::JoinHandle};
|
use tokio::{net::UnixStream, task::JoinHandle};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
@@ -34,11 +31,10 @@ lazy_static! {
|
|||||||
disconnect_status: OnceCell::new(),
|
disconnect_status: OnceCell::new(),
|
||||||
|
|
||||||
message_sender_handle: None,
|
message_sender_handle: None,
|
||||||
id_counter: CounterU32::new(0),
|
|
||||||
scenegraph: Default::default(),
|
scenegraph: Default::default(),
|
||||||
root: OnceCell::new(),
|
root: OnceCell::new(),
|
||||||
base_resource_prefixes: Default::default(),
|
base_resource_prefixes: Default::default(),
|
||||||
state: OnceCell::default(),
|
state: Arc::new(ClientState::default()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +46,7 @@ pub fn get_env(pid: i32) -> Result<FxHashMap<String, String>, std::io::Error> {
|
|||||||
.map(|(k, v)| (k.to_string(), v.to_string())),
|
.map(|(k, v)| (k.to_string(), v.to_string())),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
pub fn state(env: &FxHashMap<String, String>) -> Option<Arc<ClientStateParsed>> {
|
pub fn state(env: &FxHashMap<String, String>) -> Option<Arc<ClientState>> {
|
||||||
let token = env.get("STARDUST_STARTUP_TOKEN")?;
|
let token = env.get("STARDUST_STARTUP_TOKEN")?;
|
||||||
CLIENT_STATES.lock().get(token).cloned()
|
CLIENT_STATES.lock().get(token).cloned()
|
||||||
}
|
}
|
||||||
@@ -63,12 +59,11 @@ pub struct Client {
|
|||||||
flush_join_handle: OnceCell<JoinHandle<Result<()>>>,
|
flush_join_handle: OnceCell<JoinHandle<Result<()>>>,
|
||||||
disconnect_status: OnceCell<Result<()>>,
|
disconnect_status: OnceCell<Result<()>>,
|
||||||
|
|
||||||
id_counter: CounterU32,
|
|
||||||
pub message_sender_handle: Option<MessageSenderHandle>,
|
pub message_sender_handle: Option<MessageSenderHandle>,
|
||||||
pub scenegraph: Arc<Scenegraph>,
|
pub scenegraph: Arc<Scenegraph>,
|
||||||
pub root: OnceCell<Arc<Root>>,
|
pub root: OnceCell<Arc<Root>>,
|
||||||
pub base_resource_prefixes: Mutex<Vec<PathBuf>>,
|
pub base_resource_prefixes: Mutex<Vec<PathBuf>>,
|
||||||
pub state: OnceCell<ClientState>,
|
pub state: Arc<ClientState>,
|
||||||
}
|
}
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn from_connection(connection: UnixStream) -> Result<Arc<Self>> {
|
pub fn from_connection(connection: UnixStream) -> Result<Arc<Self>> {
|
||||||
@@ -88,7 +83,7 @@ impl Client {
|
|||||||
let state = env
|
let state = env
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(state)
|
.and_then(state)
|
||||||
.unwrap_or_else(|| Arc::new(ClientStateParsed::default()));
|
.unwrap_or_else(|| Arc::new(ClientState::default()));
|
||||||
|
|
||||||
let client = CLIENTS.add(Client {
|
let client = CLIENTS.add(Client {
|
||||||
pid,
|
pid,
|
||||||
@@ -99,25 +94,29 @@ impl Client {
|
|||||||
flush_join_handle: OnceCell::new(),
|
flush_join_handle: OnceCell::new(),
|
||||||
disconnect_status: OnceCell::new(),
|
disconnect_status: OnceCell::new(),
|
||||||
|
|
||||||
id_counter: CounterU32::new(256),
|
|
||||||
message_sender_handle: Some(messenger_tx.handle()),
|
message_sender_handle: Some(messenger_tx.handle()),
|
||||||
scenegraph: scenegraph.clone(),
|
scenegraph: scenegraph.clone(),
|
||||||
root: OnceCell::new(),
|
root: OnceCell::new(),
|
||||||
base_resource_prefixes: Default::default(),
|
base_resource_prefixes: Default::default(),
|
||||||
state: OnceCell::default(),
|
state,
|
||||||
});
|
});
|
||||||
let _ = client.scenegraph.client.set(Arc::downgrade(&client));
|
let _ = client.scenegraph.client.set(Arc::downgrade(&client));
|
||||||
let _ = client.root.set(Root::create(&client, state.root)?);
|
let _ = client.root.set(Root::create(&client)?);
|
||||||
|
hmd::make_alias(&client)?;
|
||||||
spatial::create_interface(&client)?;
|
spatial::create_interface(&client)?;
|
||||||
fields::create_interface(&client)?;
|
fields::create_interface(&client)?;
|
||||||
drawable::create_interface(&client)?;
|
drawable::create_interface(&client)?;
|
||||||
audio::create_interface(&client)?;
|
audio::create_interface(&client)?;
|
||||||
data::create_interface(&client)?;
|
data::create_interface(&client)?;
|
||||||
|
items::create_interface(&client)?;
|
||||||
input::create_interface(&client)?;
|
input::create_interface(&client)?;
|
||||||
items::camera::create_interface(&client)?;
|
|
||||||
items::panel::create_interface(&client)?;
|
|
||||||
|
|
||||||
let _ = client.state.set(state.apply_to(&client));
|
client
|
||||||
|
.root
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.node
|
||||||
|
.send_remote_signal("restore_state", serialize(client.state.apply_to(&client))?)?;
|
||||||
|
|
||||||
let pid_printable = pid
|
let pid_printable = pid
|
||||||
.map(|pid| pid.to_string())
|
.map(|pid| pid.to_string())
|
||||||
@@ -141,8 +140,11 @@ impl Client {
|
|||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
async move {
|
async move {
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = messenger_rx.dispatch(&*scenegraph).await {
|
match messenger_rx.dispatch(&*scenegraph).await {
|
||||||
client.disconnect(Err(e.into()));
|
Err(e) => {
|
||||||
|
client.disconnect(Err(e.into()));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,8 +158,11 @@ impl Client {
|
|||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
async move {
|
async move {
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = messenger_tx.flush().await {
|
match messenger_tx.flush().await {
|
||||||
client.disconnect(Err(e.into()));
|
Err(e) => {
|
||||||
|
client.disconnect(Err(e.into()));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,19 +189,15 @@ impl Client {
|
|||||||
let cwd_proc_path = format!("/proc/{pid}/cwd");
|
let cwd_proc_path = format!("/proc/{pid}/cwd");
|
||||||
std::fs::read_link(cwd_proc_path).ok()
|
std::fs::read_link(cwd_proc_path).ok()
|
||||||
}
|
}
|
||||||
pub async fn save_state(&self) -> Option<ClientStateParsed> {
|
pub async fn save_state(&self) -> Option<ClientState> {
|
||||||
let internal = self.root.get()?.save_state().await.ok()?;
|
let internal = self.root.get()?.save_state().await.ok()?;
|
||||||
Some(ClientStateParsed::from_deserialized(self, internal))
|
Some(ClientState::from_deserialized(self, internal))
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_id(&self) -> u64 {
|
|
||||||
self.id_counter.inc() as u64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_node(&self, name: &'static str, id: u64) -> Result<Arc<Node>> {
|
pub fn get_node(&self, name: &'static str, path: &str) -> Result<Arc<Node>> {
|
||||||
self.scenegraph
|
self.scenegraph
|
||||||
.get_node(id)
|
.get_node(path)
|
||||||
.ok_or_else(|| eyre!("{} not found", name))
|
.ok_or_else(|| eyre!("{} not found", name))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,22 +209,7 @@ impl Client {
|
|||||||
if let Some(flush_join_handle) = self.flush_join_handle.get() {
|
if let Some(flush_join_handle) = self.flush_join_handle.get() {
|
||||||
flush_join_handle.abort();
|
flush_join_handle.abort();
|
||||||
}
|
}
|
||||||
if let Some(client) = CLIENTS.remove(self) {
|
CLIENTS.remove(self);
|
||||||
destroy_queue::add(client);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Debug for Client {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("Client")
|
|
||||||
.field("pid", &self.pid)
|
|
||||||
.field("exe", &self.exe)
|
|
||||||
.field("dispatch_join_handle", &self.dispatch_join_handle)
|
|
||||||
.field("flush_join_handle", &self.flush_join_handle)
|
|
||||||
.field("disconnect_status", &self.disconnect_status)
|
|
||||||
.field("base_resource_prefixes", &self.base_resource_prefixes)
|
|
||||||
.field("state", &self.state)
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Drop for Client {
|
impl Drop for Client {
|
||||||
|
|||||||
@@ -1,17 +1,13 @@
|
|||||||
use super::client::{get_env, Client};
|
use super::client::{get_env, Client};
|
||||||
use crate::nodes::{root::ClientState, spatial::Spatial, Node};
|
use crate::nodes::{spatial::Spatial, Node};
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{io::Write, path::PathBuf, sync::Arc};
|
||||||
path::{Path, PathBuf},
|
|
||||||
process::Command,
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientStateParsed>>> = Default::default();
|
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientState>>> = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -31,28 +27,29 @@ impl LaunchInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct ClientStateParsed {
|
pub struct ClientState {
|
||||||
pub launch_info: Option<LaunchInfo>,
|
pub launch_info: Option<LaunchInfo>,
|
||||||
pub data: Vec<u8>,
|
pub data: Option<Vec<u8>>,
|
||||||
pub root: Mat4,
|
pub root: Mat4,
|
||||||
pub spatial_anchors: FxHashMap<String, Mat4>,
|
pub spatial_anchors: FxHashMap<String, Mat4>,
|
||||||
}
|
}
|
||||||
impl ClientStateParsed {
|
impl ClientState {
|
||||||
pub fn from_deserialized(client: &Client, state: ClientState) -> Self {
|
pub fn from_deserialized(client: &Client, state: ClientStateInternal) -> Self {
|
||||||
ClientStateParsed {
|
ClientState {
|
||||||
launch_info: LaunchInfo::from_client(client),
|
launch_info: LaunchInfo::from_client(client),
|
||||||
data: state.data.unwrap_or_default(),
|
data: state.data,
|
||||||
root: Self::spatial_transform(client, state.root).unwrap_or_default(),
|
root: Self::spatial_transform(client, &state.root.unwrap_or_default())
|
||||||
|
.unwrap_or_default(),
|
||||||
spatial_anchors: state
|
spatial_anchors: state
|
||||||
.spatial_anchors
|
.spatial_anchors
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(k, v)| Some((k, Self::spatial_transform(client, v)?)))
|
.filter_map(|(k, v)| Some((k, Self::spatial_transform(client, &v)?)))
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn spatial_transform(client: &Client, id: u64) -> Option<Mat4> {
|
fn spatial_transform(client: &Client, path: &str) -> Option<Mat4> {
|
||||||
let node = client.scenegraph.get_node(id)?;
|
let node = client.scenegraph.get_node(path)?;
|
||||||
let spatial = node.get_aspect::<Spatial>().ok()?;
|
let spatial = node.spatial.get()?;
|
||||||
Some(spatial.global_transform())
|
Some(spatial.global_transform())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,59 +58,51 @@ impl ClientStateParsed {
|
|||||||
CLIENT_STATES.lock().insert(token.clone(), Arc::new(self));
|
CLIENT_STATES.lock().insert(token.clone(), Arc::new(self));
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
pub fn from_file(file: &Path) -> Option<Self> {
|
pub fn to_file(self) {
|
||||||
let file_string = std::fs::read_to_string(file).ok()?;
|
let project_dirs = directories::ProjectDirs::from("", "", "stardust").unwrap();
|
||||||
toml::from_str(&file_string).ok()
|
let state_dir = project_dirs.state_dir().unwrap();
|
||||||
|
std::fs::create_dir_all(state_dir).unwrap();
|
||||||
|
let mut file = std::fs::File::create(state_dir.join(nanoid::nanoid!())).unwrap();
|
||||||
|
file.write_all(&stardust_xr::schemas::flex::flexbuffers::to_vec(self).unwrap())
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
pub fn to_file(&self, directory: &Path) {
|
pub fn apply_to(&self, client: &Arc<Client>) -> ClientStateInternal {
|
||||||
let app_name = self
|
|
||||||
.launch_info
|
|
||||||
.as_ref()
|
|
||||||
.map(|l| l.cmdline.first().unwrap().split('/').last().unwrap())
|
|
||||||
.unwrap_or("unknown");
|
|
||||||
let state_file_path = directory
|
|
||||||
.join(format!("{app_name}-{}", nanoid::nanoid!()))
|
|
||||||
.with_extension("toml");
|
|
||||||
|
|
||||||
std::fs::write(state_file_path, toml::to_string(&self).unwrap()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn apply_to(&self, client: &Arc<Client>) -> ClientState {
|
|
||||||
if let Some(root) = client.root.get() {
|
if let Some(root) = client.root.get() {
|
||||||
root.set_transform(self.root)
|
root.set_transform(self.root)
|
||||||
}
|
}
|
||||||
ClientState {
|
ClientStateInternal {
|
||||||
data: Some(self.data.clone()),
|
data: self.data.clone(),
|
||||||
root: 0,
|
root: Some("/".to_string()),
|
||||||
spatial_anchors: self
|
spatial_anchors: self
|
||||||
.spatial_anchors
|
.spatial_anchors
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, v)| {
|
.map(|(k, v)| {
|
||||||
let node = Node::generate(client, true).add_to_scenegraph().unwrap();
|
(k.clone(), {
|
||||||
Spatial::add_to(&node, None, *v, false);
|
let node = Node::create(client, "/spatial/anchor", k, true)
|
||||||
(k.clone(), node.get_id())
|
.add_to_scenegraph()
|
||||||
|
.unwrap();
|
||||||
|
Spatial::add_to(&node, None, *v, false).unwrap();
|
||||||
|
k.clone()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn launch_command(self) -> Option<Command> {
|
|
||||||
let launch_info = self.launch_info.as_ref()?;
|
|
||||||
let mut cmdline = launch_info.cmdline.iter();
|
|
||||||
let mut command = Command::new(cmdline.next()?);
|
|
||||||
command.args(cmdline);
|
|
||||||
command.current_dir(&launch_info.cwd);
|
|
||||||
command.envs(launch_info.env.iter());
|
|
||||||
command.env("STARDUST_STARTUP_TOKEN", self.token());
|
|
||||||
Some(command)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
impl Default for ClientStateParsed {
|
impl Default for ClientState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
launch_info: None,
|
launch_info: None,
|
||||||
data: Default::default(),
|
data: None,
|
||||||
root: Mat4::IDENTITY,
|
root: Mat4::IDENTITY,
|
||||||
spatial_anchors: Default::default(),
|
spatial_anchors: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Serialize, Deserialize)]
|
||||||
|
pub struct ClientStateInternal {
|
||||||
|
data: Option<Vec<u8>>,
|
||||||
|
root: Option<String>,
|
||||||
|
spatial_anchors: FxHashMap<String, String>,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,24 +1,12 @@
|
|||||||
use once_cell::sync::Lazy;
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use tokio::sync::mpsc::{self, unbounded_channel};
|
|
||||||
|
|
||||||
type Anything = Box<dyn Any + Send + Sync>;
|
static MAIN_DESTROY_QUEUE: Mutex<Vec<Box<dyn Any + Send + Sync>>> = Mutex::new(Vec::new());
|
||||||
|
|
||||||
static MAIN_DESTROY_QUEUE: Lazy<(
|
|
||||||
mpsc::UnboundedSender<Anything>,
|
|
||||||
Mutex<mpsc::UnboundedReceiver<Anything>>,
|
|
||||||
)> = Lazy::new(|| {
|
|
||||||
let (tx, rx) = unbounded_channel();
|
|
||||||
(tx, Mutex::new(rx))
|
|
||||||
});
|
|
||||||
|
|
||||||
pub fn add<T: Any + Sync + Send>(thing: T) {
|
pub fn add<T: Any + Sync + Send>(thing: T) {
|
||||||
MAIN_DESTROY_QUEUE.0.send(Box::new(thing)).unwrap();
|
MAIN_DESTROY_QUEUE.lock().push(Box::new(thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear() {
|
pub fn clear() {
|
||||||
while let Ok(thing) = MAIN_DESTROY_QUEUE.1.lock().try_recv() {
|
MAIN_DESTROY_QUEUE.lock().clear();
|
||||||
drop(thing)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
36
src/core/eventloop.rs
Normal file
36
src/core/eventloop.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use super::client::Client;
|
||||||
|
use super::task;
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::net::UnixListener;
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
pub struct EventLoop {
|
||||||
|
join_handle: JoinHandle<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLoop {
|
||||||
|
pub fn new(socket_path: PathBuf) -> Result<Arc<Self>> {
|
||||||
|
let socket = UnixListener::bind(socket_path)?;
|
||||||
|
|
||||||
|
let join_handle = task::new(|| "event loop", async move {
|
||||||
|
loop {
|
||||||
|
let Ok((socket, _)) = socket.accept().await else { continue };
|
||||||
|
if let Err(e) = Client::from_connection(socket) {
|
||||||
|
error!(?e, "Unable to create client from connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
let event_loop = Arc::new(EventLoop { join_handle });
|
||||||
|
|
||||||
|
Ok(event_loop)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for EventLoop {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.join_handle.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#[macro_export]
|
|
||||||
macro_rules! create_interface {
|
|
||||||
($iface:ident) => {
|
|
||||||
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
|
||||||
let node = Node::from_id(client, INTERFACE_NODE_ID, false);
|
|
||||||
<$iface as self::InterfaceAspect>::add_node_members(&node);
|
|
||||||
node.add_to_scenegraph()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -2,8 +2,11 @@ pub mod client;
|
|||||||
pub mod client_state;
|
pub mod client_state;
|
||||||
pub mod delta;
|
pub mod delta;
|
||||||
pub mod destroy_queue;
|
pub mod destroy_queue;
|
||||||
pub mod idl_utils;
|
pub mod eventloop;
|
||||||
|
pub mod node_collections;
|
||||||
pub mod registry;
|
pub mod registry;
|
||||||
pub mod resource;
|
pub mod resource;
|
||||||
pub mod scenegraph;
|
pub mod scenegraph;
|
||||||
pub mod task;
|
pub mod task;
|
||||||
|
pub mod typed_datamap;
|
||||||
|
pub mod buffers;
|
||||||
|
|||||||
83
src/core/node_collections.rs
Normal file
83
src/core/node_collections.rs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
use crate::nodes::Node;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
|
use std::{
|
||||||
|
borrow::Borrow,
|
||||||
|
hash::Hash,
|
||||||
|
sync::{Arc, Weak},
|
||||||
|
};
|
||||||
|
|
||||||
|
// #[derive(Default)]
|
||||||
|
// pub struct LifeLinkedNodeList {
|
||||||
|
// nodes: Mutex<Vec<Weak<Node>>>,
|
||||||
|
// }
|
||||||
|
// impl LifeLinkedNodeList {
|
||||||
|
// pub fn add(&self, node: Weak<Node>) {
|
||||||
|
// self.nodes.lock().push(node);
|
||||||
|
// }
|
||||||
|
// pub fn clear(&self) {
|
||||||
|
// self.nodes
|
||||||
|
// .lock()
|
||||||
|
// .iter()
|
||||||
|
// .filter_map(|node| node.upgrade())
|
||||||
|
// .for_each(|node| {
|
||||||
|
// node.destroy();
|
||||||
|
// });
|
||||||
|
// self.nodes.lock().clear();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// impl Drop for LifeLinkedNodeList {
|
||||||
|
// fn drop(&mut self) {
|
||||||
|
// self.clear();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct LifeLinkedNodeMap<K: Hash + Eq> {
|
||||||
|
nodes: Mutex<FxHashMap<K, Weak<Node>>>,
|
||||||
|
}
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl<K: Hash + Eq> LifeLinkedNodeMap<K> {
|
||||||
|
pub fn add(&self, key: K, node: &Arc<Node>) {
|
||||||
|
self.nodes.lock().insert(key, Arc::downgrade(node));
|
||||||
|
}
|
||||||
|
pub fn get<Q>(&self, key: &Q) -> Option<Arc<Node>>
|
||||||
|
where
|
||||||
|
Q: ?Sized,
|
||||||
|
K: Borrow<Q>,
|
||||||
|
Q: Hash + Eq,
|
||||||
|
{
|
||||||
|
self.nodes.lock().get(key).and_then(|n| n.upgrade())
|
||||||
|
}
|
||||||
|
pub fn nodes(&self) -> Vec<Arc<Node>> {
|
||||||
|
self.nodes
|
||||||
|
.lock()
|
||||||
|
.values()
|
||||||
|
.filter_map(|v| v.upgrade())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
pub fn remove<Q>(&self, key: &Q) -> Option<Arc<Node>>
|
||||||
|
where
|
||||||
|
Q: ?Sized,
|
||||||
|
K: Borrow<Q>,
|
||||||
|
Q: Hash + Eq,
|
||||||
|
{
|
||||||
|
self.nodes.lock().remove(key).and_then(|n| n.upgrade())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&self) {
|
||||||
|
let mut nodes = self.nodes.lock();
|
||||||
|
nodes
|
||||||
|
.values()
|
||||||
|
.filter_map(|node| node.upgrade())
|
||||||
|
.for_each(|node| {
|
||||||
|
node.destroy();
|
||||||
|
});
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K: Hash + Eq> Drop for LifeLinkedNodeMap<K> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ use rustc_hash::FxHashMap;
|
|||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Registry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Weak<T>>>>);
|
pub struct Registry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Weak<T>>>>);
|
||||||
|
|
||||||
impl<T: Send + Sync + ?Sized> Registry<T> {
|
impl<T: Send + Sync + ?Sized> Registry<T> {
|
||||||
@@ -13,7 +12,9 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
|
|||||||
Registry(const_mutex(None))
|
Registry(const_mutex(None))
|
||||||
}
|
}
|
||||||
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
|
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
|
||||||
MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default))
|
MutexGuard::map(self.0.lock(), |r| {
|
||||||
|
r.get_or_insert_with(|| FxHashMap::default())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
pub fn add(&self, t: T) -> Arc<T>
|
pub fn add(&self, t: T) -> Arc<T>
|
||||||
where
|
where
|
||||||
@@ -31,38 +32,12 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
|
|||||||
self.lock()
|
self.lock()
|
||||||
.contains_key(&(ptr::addr_of!(*t) as *const () as usize))
|
.contains_key(&(ptr::addr_of!(*t) as *const () as usize))
|
||||||
}
|
}
|
||||||
pub fn get_changes(old: &Registry<T>, new: &Registry<T>) -> (Vec<Arc<T>>, Vec<Arc<T>>) {
|
|
||||||
let old = old.lock();
|
|
||||||
let new = new.lock();
|
|
||||||
|
|
||||||
let mut added = Vec::new();
|
|
||||||
let mut removed = Vec::new();
|
|
||||||
|
|
||||||
for (id, entry) in new.iter() {
|
|
||||||
if let Some(entry) = entry.upgrade() {
|
|
||||||
if !old.contains_key(id) {
|
|
||||||
added.push(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (id, entry) in old.iter() {
|
|
||||||
if let Some(entry) = entry.upgrade() {
|
|
||||||
if !new.contains_key(id) {
|
|
||||||
removed.push(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(added, removed)
|
|
||||||
}
|
|
||||||
pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
|
pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
|
||||||
self.lock()
|
self.lock()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|pair| pair.1.upgrade())
|
.filter_map(|pair| pair.1.upgrade())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
pub fn set(&self, other: &Registry<T>) {
|
|
||||||
self.lock().clone_from(&other.lock());
|
|
||||||
}
|
|
||||||
pub fn take_valid_contents(&self) -> Vec<Arc<T>> {
|
pub fn take_valid_contents(&self) -> Vec<Arc<T>> {
|
||||||
self.0
|
self.0
|
||||||
.lock()
|
.lock()
|
||||||
@@ -72,14 +47,6 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
|
|||||||
.filter_map(|pair| pair.1.upgrade())
|
.filter_map(|pair| pair.1.upgrade())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
pub fn retain<F: Fn(&Arc<T>) -> bool>(&self, f: F) {
|
|
||||||
self.lock().retain(|_, v| {
|
|
||||||
let Some(v) = v.upgrade() else {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
(f)(&v)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
pub fn remove(&self, t: &T) {
|
pub fn remove(&self, t: &T) {
|
||||||
self.lock()
|
self.lock()
|
||||||
.remove(&(ptr::addr_of!(*t) as *const () as usize));
|
.remove(&(ptr::addr_of!(*t) as *const () as usize));
|
||||||
@@ -89,9 +56,7 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
|
|||||||
}
|
}
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
let registry = self.0.lock();
|
let registry = self.0.lock();
|
||||||
let Some(registry) = &*registry else {
|
let Some(registry) = &*registry else {return true};
|
||||||
return true;
|
|
||||||
};
|
|
||||||
if registry.is_empty() {
|
if registry.is_empty() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -103,11 +68,6 @@ impl<T: Send + Sync + ?Sized> Clone for Registry<T> {
|
|||||||
Self(Mutex::new(self.0.lock().clone()))
|
Self(Mutex::new(self.0.lock().clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T: Send + Sync + ?Sized> Default for Registry<T> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct OwnedRegistry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Arc<T>>>>);
|
pub struct OwnedRegistry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Arc<T>>>>);
|
||||||
|
|
||||||
@@ -116,7 +76,9 @@ impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {
|
|||||||
OwnedRegistry(const_mutex(None))
|
OwnedRegistry(const_mutex(None))
|
||||||
}
|
}
|
||||||
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Arc<T>>> {
|
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Arc<T>>> {
|
||||||
MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default))
|
MutexGuard::map(self.0.lock(), |r| {
|
||||||
|
r.get_or_insert_with(|| FxHashMap::default())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
pub fn add(&self, t: T) -> Arc<T>
|
pub fn add(&self, t: T) -> Arc<T>
|
||||||
where
|
where
|
||||||
@@ -136,12 +98,9 @@ impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {
|
|||||||
self.lock()
|
self.lock()
|
||||||
.contains_key(&(ptr::addr_of!(*t) as *const () as usize))
|
.contains_key(&(ptr::addr_of!(*t) as *const () as usize))
|
||||||
}
|
}
|
||||||
pub fn remove(&self, t: &T) -> Option<Arc<T>>
|
pub fn remove(&self, t: &T) {
|
||||||
where
|
|
||||||
T: Sized,
|
|
||||||
{
|
|
||||||
self.lock()
|
self.lock()
|
||||||
.remove(&(ptr::addr_of!(*t) as *const () as usize))
|
.remove(&(ptr::addr_of!(*t) as *const () as usize));
|
||||||
}
|
}
|
||||||
pub fn clear(&self) {
|
pub fn clear(&self) {
|
||||||
self.lock().clear();
|
self.lock().clear();
|
||||||
|
|||||||
@@ -1,49 +1,83 @@
|
|||||||
use stardust_xr::values::ResourceID;
|
use color_eyre::eyre::eyre;
|
||||||
use std::{
|
use serde::{de::Visitor, Deserialize};
|
||||||
ffi::OsStr,
|
use std::{ffi::OsStr, path::PathBuf};
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::client::Client;
|
#[derive(Debug)]
|
||||||
|
pub enum ResourceID {
|
||||||
lazy_static::lazy_static! {
|
File(PathBuf),
|
||||||
static ref THEMES: Vec<PathBuf> = std::env::var("STARDUST_THEMES").map(|s| s.split(':').map(PathBuf::from).collect()).unwrap_or_default();
|
Namespaced { namespace: String, path: PathBuf },
|
||||||
}
|
}
|
||||||
|
impl ResourceID {
|
||||||
fn has_extension(path: &Path, extensions: &[&OsStr]) -> bool {
|
pub fn get_file(&self, prefixes: &[PathBuf], extensions: &[&OsStr]) -> Option<PathBuf> {
|
||||||
if let Some(path_extension) = path.extension() {
|
match self {
|
||||||
extensions.contains(&path_extension)
|
ResourceID::File(file) => (file.is_absolute()
|
||||||
} else {
|
&& file.exists() && Self::has_extension(file, extensions))
|
||||||
false
|
.then_some(file.clone()),
|
||||||
}
|
ResourceID::Namespaced { namespace, path } => {
|
||||||
}
|
let file_name = path.file_name()?;
|
||||||
|
prefixes
|
||||||
pub fn get_resource_file(
|
.iter()
|
||||||
resource: &ResourceID,
|
.filter_map(|prefix| {
|
||||||
client: &Client,
|
let prefixed_path = prefix.clone().join(namespace).join(path);
|
||||||
extensions: &[&OsStr],
|
let parent = prefixed_path.parent()?;
|
||||||
) -> Option<PathBuf> {
|
std::fs::read_dir(parent).ok()
|
||||||
match resource {
|
})
|
||||||
ResourceID::Direct(file) => {
|
.flatten()
|
||||||
(file.is_absolute() && file.exists() && has_extension(file, extensions))
|
.filter_map(|item| item.ok())
|
||||||
.then_some(file.clone())
|
.map(|dir_entry| dir_entry.path())
|
||||||
|
.filter(|path| path.file_stem() == Some(file_name))
|
||||||
|
.find(|path| Self::has_extension(path, extensions))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ResourceID::Namespaced { namespace, path } => {
|
}
|
||||||
let file_name = path.file_name()?;
|
|
||||||
let base_prefixes = client.base_resource_prefixes.lock().clone();
|
fn has_extension(path: &PathBuf, extensions: &[&OsStr]) -> bool {
|
||||||
THEMES
|
if let Some(path_extension) = path.extension() {
|
||||||
.iter()
|
extensions.contains(&path_extension)
|
||||||
.chain(base_prefixes.iter())
|
} else {
|
||||||
.filter_map(|prefix| {
|
false
|
||||||
let prefixed_path = prefix.clone().join(namespace).join(path);
|
|
||||||
let parent = prefixed_path.parent()?;
|
|
||||||
std::fs::read_dir(parent).ok()
|
|
||||||
})
|
|
||||||
.flatten()
|
|
||||||
.filter_map(|item| item.ok())
|
|
||||||
.map(|dir_entry| dir_entry.path())
|
|
||||||
.filter(|path| path.file_stem() == Some(file_name))
|
|
||||||
.find(|path| has_extension(path, extensions))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<'de> Deserialize<'de> for ResourceID {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_any(ResourceVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ResourceVisitor;
|
||||||
|
impl<'de> Visitor<'de> for ResourceVisitor {
|
||||||
|
type Value = ResourceID;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str("A string containing an absolute path to file or \"[namespace]:[path]\" for a namespaced resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
Ok(if v.starts_with('/') {
|
||||||
|
let path = PathBuf::from(v);
|
||||||
|
path.metadata().map_err(serde::de::Error::custom)?;
|
||||||
|
ResourceID::File(path)
|
||||||
|
} else if let Some((namespace, path)) = v.split_once(':') {
|
||||||
|
ResourceID::Namespaced {
|
||||||
|
namespace: namespace.to_string(),
|
||||||
|
path: PathBuf::from(path),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(serde::de::Error::custom(eyre!("Invalid format for string")));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
self.visit_str(&v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
use crate::nodes::alias::get_original;
|
|
||||||
use crate::nodes::Node;
|
use crate::nodes::Node;
|
||||||
use crate::{core::client::Client, nodes::Message};
|
use crate::{core::client::Client, nodes::Message};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use portable_atomic::Ordering;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::Serialize;
|
|
||||||
use stardust_xr::scenegraph;
|
use stardust_xr::scenegraph;
|
||||||
use stardust_xr::scenegraph::ScenegraphError;
|
use stardust_xr::scenegraph::ScenegraphError;
|
||||||
use stardust_xr::schemas::flex::serialize;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::os::fd::OwnedFd;
|
use std::os::fd::OwnedFd;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
@@ -18,7 +15,7 @@ use tracing::{debug, debug_span};
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Scenegraph {
|
pub struct Scenegraph {
|
||||||
pub(super) client: OnceCell<Weak<Client>>,
|
pub(super) client: OnceCell<Weak<Client>>,
|
||||||
nodes: Mutex<FxHashMap<u64, Arc<Node>>>,
|
nodes: Mutex<FxHashMap<String, Arc<Node>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scenegraph {
|
impl Scenegraph {
|
||||||
@@ -33,69 +30,52 @@ impl Scenegraph {
|
|||||||
}
|
}
|
||||||
pub fn add_node_raw(&self, node: Arc<Node>) {
|
pub fn add_node_raw(&self, node: Arc<Node>) {
|
||||||
debug!(node = ?&*node, "Add node");
|
debug!(node = ?&*node, "Add node");
|
||||||
self.nodes.lock().insert(node.get_id(), node);
|
let path = node.get_path().to_string();
|
||||||
|
self.nodes.lock().insert(path, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_node(&self, node: u64) -> Option<Arc<Node>> {
|
pub fn get_node(&self, path: &str) -> Option<Arc<Node>> {
|
||||||
let node = self.nodes.lock().get(&node)?.clone();
|
let mut node = self.nodes.lock().get(path)?.clone();
|
||||||
get_original(node, true)
|
while let Some(alias) = node.alias.get() {
|
||||||
|
if alias.enabled.load(Ordering::Relaxed) {
|
||||||
|
node = alias.original.upgrade()?;
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_node(&self, node: u64) -> Option<Arc<Node>> {
|
pub fn remove_node(&self, path: &str) -> Option<Arc<Node>> {
|
||||||
debug!(node, "Remove node");
|
debug!(path, "Remove node");
|
||||||
self.nodes.lock().remove(&node)
|
self.nodes.lock().remove(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type MethodResponse = Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>;
|
pub struct MethodResponseSender(oneshot::Sender<Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>>);
|
||||||
pub struct MethodResponseSender(oneshot::Sender<MethodResponse>);
|
|
||||||
impl MethodResponseSender {
|
impl MethodResponseSender {
|
||||||
pub fn send(self, t: Result<Message, ScenegraphError>) {
|
pub fn send(self, t: Result<Message, ScenegraphError>) {
|
||||||
let _ = self.0.send(t.map(|m| (m.data, m.fds)));
|
let _ = self.0.send(t.map(|m| (m.data, m.fds)));
|
||||||
}
|
}
|
||||||
// pub fn send_method_return<T: Serialize>(
|
|
||||||
// self,
|
|
||||||
// result: color_eyre::eyre::Result<(T, Vec<OwnedFd>)>,
|
|
||||||
// ) {
|
|
||||||
// let _ = self.0.send(map_method_return(result));
|
|
||||||
// }
|
|
||||||
pub fn wrap_sync<F: FnOnce() -> color_eyre::eyre::Result<Message>>(self, f: F) {
|
pub fn wrap_sync<F: FnOnce() -> color_eyre::eyre::Result<Message>>(self, f: F) {
|
||||||
self.send(f().map_err(|e| ScenegraphError::MethodError {
|
self.send(f().map_err(|e| ScenegraphError::MethodError {
|
||||||
error: e.to_string(),
|
error: e.to_string(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
pub fn wrap_async<T: Serialize>(
|
|
||||||
self,
|
|
||||||
f: impl Future<Output = color_eyre::eyre::Result<(T, Vec<OwnedFd>)>> + Send + 'static,
|
|
||||||
) {
|
|
||||||
tokio::task::spawn(async move { self.0.send(map_method_return(f.await)) });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn map_method_return<T: Serialize>(
|
|
||||||
result: color_eyre::eyre::Result<(T, Vec<OwnedFd>)>,
|
|
||||||
) -> Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError> {
|
|
||||||
let (value, fds) = result.map_err(|e| ScenegraphError::MethodError {
|
|
||||||
error: e.to_string(),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let serialized_value = serialize(value).map_err(|e| ScenegraphError::MethodError {
|
|
||||||
error: format!("Internal: Serialization failed: {e}"),
|
|
||||||
})?;
|
|
||||||
Ok((serialized_value, fds))
|
|
||||||
}
|
}
|
||||||
impl scenegraph::Scenegraph for Scenegraph {
|
impl scenegraph::Scenegraph for Scenegraph {
|
||||||
fn send_signal(
|
fn send_signal(
|
||||||
&self,
|
&self,
|
||||||
node: u64,
|
path: &str,
|
||||||
method: u64,
|
method: &str,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
fds: Vec<OwnedFd>,
|
fds: Vec<OwnedFd>,
|
||||||
) -> Result<(), ScenegraphError> {
|
) -> Result<(), ScenegraphError> {
|
||||||
let Some(client) = self.get_client() else {
|
let Some(client) = self.get_client() else {
|
||||||
return Err(ScenegraphError::SignalNotFound);
|
return Err(ScenegraphError::SignalNotFound);
|
||||||
};
|
};
|
||||||
debug_span!("Handle signal", node, method).in_scope(|| {
|
debug_span!("Handle signal", path, method).in_scope(|| {
|
||||||
self.get_node(node)
|
self.get_node(path)
|
||||||
.ok_or(ScenegraphError::NodeNotFound)?
|
.ok_or(ScenegraphError::NodeNotFound)?
|
||||||
.send_local_signal(
|
.send_local_signal(
|
||||||
client,
|
client,
|
||||||
@@ -109,8 +89,8 @@ impl scenegraph::Scenegraph for Scenegraph {
|
|||||||
}
|
}
|
||||||
fn execute_method(
|
fn execute_method(
|
||||||
&self,
|
&self,
|
||||||
node: u64,
|
path: &str,
|
||||||
method: u64,
|
method: &str,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
fds: Vec<OwnedFd>,
|
fds: Vec<OwnedFd>,
|
||||||
response: oneshot::Sender<Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>>,
|
response: oneshot::Sender<Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>>,
|
||||||
@@ -119,8 +99,8 @@ impl scenegraph::Scenegraph for Scenegraph {
|
|||||||
let _ = response.send(Err(ScenegraphError::MethodNotFound));
|
let _ = response.send(Err(ScenegraphError::MethodNotFound));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
debug!(node, method, "Handle method");
|
debug!(path, method, "Handle method");
|
||||||
let Some(node) = self.get_node(node) else {
|
let Some(node) = self.get_node(path) else {
|
||||||
let _ = response.send(Err(ScenegraphError::NodeNotFound));
|
let _ = response.send(Err(ScenegraphError::NodeNotFound));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|||||||
56
src/core/typed_datamap.rs
Normal file
56
src/core/typed_datamap.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
use stardust_xr::schemas::{
|
||||||
|
flat::Datamap,
|
||||||
|
flex::flexbuffers::{FlexbufferSerializer, Reader, ReaderError},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::nodes::Message;
|
||||||
|
|
||||||
|
pub struct TypedDatamap<T: DeserializeOwned + Serialize>(T);
|
||||||
|
impl<T: DeserializeOwned + Serialize> TypedDatamap<T> {
|
||||||
|
pub fn new(data: T) -> Self {
|
||||||
|
TypedDatamap(data)
|
||||||
|
}
|
||||||
|
pub fn from_flex(message: Message) -> Result<Self> {
|
||||||
|
let root = Reader::get_root(message.as_ref())?;
|
||||||
|
T::deserialize(root).map(Self::new).map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
pub fn to_datamap(&mut self) -> Result<Datamap> {
|
||||||
|
let mut serializer = FlexbufferSerializer::default();
|
||||||
|
self.0.serialize(&mut serializer)?;
|
||||||
|
Datamap::new(serializer.take_buffer()).map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
pub fn serialize(&mut self) -> Option<Vec<u8>> {
|
||||||
|
let mut serializer = FlexbufferSerializer::default();
|
||||||
|
self.0.serialize(&mut serializer).ok()?;
|
||||||
|
// check if this is actually a map
|
||||||
|
Reader::get_root(serializer.view()).ok()?.get_map().ok()?;
|
||||||
|
Some(serializer.take_buffer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: DeserializeOwned + Serialize> Default for TypedDatamap<T>
|
||||||
|
where
|
||||||
|
T: Default,
|
||||||
|
{
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(T::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: DeserializeOwned + Serialize> Deref for TypedDatamap<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: DeserializeOwned + Serialize> DerefMut for TypedDatamap<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
450
src/main.rs
450
src/main.rs
@@ -1,86 +1,84 @@
|
|||||||
#![allow(clippy::empty_docs)]
|
|
||||||
mod core;
|
mod core;
|
||||||
mod nodes;
|
mod nodes;
|
||||||
mod objects;
|
mod objects;
|
||||||
mod session;
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
mod wayland;
|
mod wayland;
|
||||||
|
|
||||||
use crate::core::destroy_queue;
|
use crate::core::client::CLIENTS;
|
||||||
|
use crate::core::client_state::ClientState;
|
||||||
|
use crate::core::{destroy_queue, buffers};
|
||||||
use crate::nodes::items::camera;
|
use crate::nodes::items::camera;
|
||||||
use crate::nodes::{audio, drawable, input};
|
use crate::nodes::{audio, drawable, hmd, input};
|
||||||
|
use crate::objects::input::eye_pointer::EyePointer;
|
||||||
|
use crate::objects::input::mouse_pointer::MousePointer;
|
||||||
|
use crate::objects::input::sk_controller::SkController;
|
||||||
|
use crate::objects::input::sk_hand::SkHand;
|
||||||
|
use crate::objects::play_space::PlaySpace;
|
||||||
|
|
||||||
|
use self::core::eventloop::EventLoop;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use core::client::Client;
|
|
||||||
use core::task;
|
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use objects::ServerObjects;
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use session::{launch_start, save_session};
|
use smithay::reexports::nix;
|
||||||
use stardust_xr::server;
|
use stardust_xr::server;
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use stereokit_rust::material::Material;
|
use stereokit::{
|
||||||
use stereokit_rust::shader::Shader;
|
named_colors::BLACK, DepthMode, DisplayMode, Handed, LogLevel, StereoKitMultiThread,
|
||||||
use stereokit_rust::sk::{sk_quit, AppMode, DepthMode, OriginMode, QuitReason, SkSettings};
|
TextureFormat, TextureType,
|
||||||
use stereokit_rust::system::{LogLevel, Renderer};
|
};
|
||||||
use stereokit_rust::tex::{SHCubemap, Tex, TexFormat, TexType};
|
use stereokit::{DisplayBlend, Sk};
|
||||||
use stereokit_rust::ui::Ui;
|
|
||||||
use stereokit_rust::util::{Color128, Time};
|
|
||||||
use tokio::net::UnixListener;
|
|
||||||
use tokio::sync::Notify;
|
use tokio::sync::Notify;
|
||||||
|
use tokio::task::LocalSet;
|
||||||
|
use tokio::{runtime::Handle, sync::oneshot};
|
||||||
use tracing::metadata::LevelFilter;
|
use tracing::metadata::LevelFilter;
|
||||||
use tracing::{debug_span, error, info};
|
use tracing::{debug_span, error, info};
|
||||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||||
use zbus::fdo::ObjectManager;
|
|
||||||
use zbus::Connection;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Parser)]
|
||||||
#[clap(author, version, about, long_about = None)]
|
#[clap(author, version, about, long_about = None)]
|
||||||
struct CliArgs {
|
struct CliArgs {
|
||||||
/// Force flatscreen mode and use the mouse pointer as a 3D pointer
|
/// Force flatscreen mode and use the mouse pointer as a 3D pointer
|
||||||
#[clap(short, long, action)]
|
#[clap(short, long, action)]
|
||||||
flatscreen: bool,
|
flatscreen: bool,
|
||||||
|
|
||||||
/// If monado insists on emulating them, set this flag...we want the raw input
|
|
||||||
#[clap(long)]
|
|
||||||
disable_controllers: bool,
|
|
||||||
/// If monado insists on emulating , set this flag...we want the raw input
|
|
||||||
#[clap(long)]
|
|
||||||
disable_hands: bool,
|
|
||||||
|
|
||||||
/// Run Stardust XR as an overlay with given priority
|
/// Run Stardust XR as an overlay with given priority
|
||||||
#[clap(id = "PRIORITY", short = 'o', long = "overlay", action)]
|
#[clap(id = "PRIORITY", short = 'o', long = "overlay", action)]
|
||||||
overlay_priority: Option<u32>,
|
overlay_priority: Option<u32>,
|
||||||
|
|
||||||
/// Debug the clients started by the server
|
/// Don't create a tip input for controller because SOME RUNTIMES will lie
|
||||||
#[clap(short = 'd', long = "debug", action)]
|
#[clap(long, action)]
|
||||||
debug_launched_clients: bool,
|
disable_controller: bool,
|
||||||
|
|
||||||
/// Run a script when ready for clients to connect. If this is not set the script at $HOME/.config/stardust/startup will be ran if it exists.
|
/// Run a script when ready for clients to connect. If this is not set the script at $HOME/.config/stardust/startup will be ran if it exists.
|
||||||
#[clap(id = "PATH", short = 'e', long = "execute-startup-script", action)]
|
#[clap(id = "PATH", short = 'e', long = "execute-startup-script", action)]
|
||||||
startup_script: Option<PathBuf>,
|
startup_script: Option<PathBuf>,
|
||||||
|
|
||||||
/// Restore the session with the given ID (or `latest`), ignoring the startup script. Sessions are stored in directories at `~/.local/state/stardust/`.
|
|
||||||
#[clap(id = "SESSION_ID", long = "restore", action)]
|
|
||||||
restore: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static STARDUST_INSTANCE: OnceCell<String> = OnceCell::new();
|
static STARDUST_INSTANCE: OnceCell<String> = OnceCell::new();
|
||||||
|
static SK_MULTITHREAD: OnceCell<Sk> = OnceCell::new();
|
||||||
|
static STOP_NOTIFIER: Notify = Notify::const_new();
|
||||||
|
|
||||||
// #[tokio::main]
|
struct EventLoopInfo {
|
||||||
#[tokio::main(flavor = "current_thread")]
|
tokio_handle: Handle,
|
||||||
async fn main() {
|
socket_path: PathBuf,
|
||||||
color_eyre::install().unwrap();
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
ctrlc::set_handler(|| {
|
||||||
|
if atty::isnt(atty::Stream::Stdout) {
|
||||||
|
STOP_NOTIFIER.notify_waiters()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let registry = tracing_subscriber::registry();
|
let registry = tracing_subscriber::registry();
|
||||||
|
|
||||||
#[cfg(feature = "profile_app")]
|
#[cfg(feature = "profile_app")]
|
||||||
let registry = registry.with(
|
let registry = registry.with(tracing_tracy::TracyLayer::new().with_filter(LevelFilter::DEBUG));
|
||||||
tracing_tracy::TracyLayer::new(tracing_tracy::DefaultConfig::default())
|
|
||||||
.with_filter(LevelFilter::DEBUG),
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(feature = "profile_tokio")]
|
#[cfg(feature = "profile_tokio")]
|
||||||
let (console_layer, _) = console_subscriber::ConsoleLayer::builder().build();
|
let (console_layer, _) = console_subscriber::ConsoleLayer::builder().build();
|
||||||
@@ -94,93 +92,22 @@ async fn main() {
|
|||||||
.with_filter(EnvFilter::from_default_env());
|
.with_filter(EnvFilter::from_default_env());
|
||||||
registry.with(log_layer).init();
|
registry.with(log_layer).init();
|
||||||
|
|
||||||
let cli_args = CliArgs::parse();
|
|
||||||
|
|
||||||
let socket_path =
|
|
||||||
server::get_free_socket_path().expect("Unable to find a free stardust socket path");
|
|
||||||
STARDUST_INSTANCE.set(socket_path.file_name().unwrap().to_string_lossy().into_owned()).expect("Someone hasn't done their job, yell at Nova because how is this set multiple times what the hell");
|
|
||||||
info!(
|
|
||||||
socket_path = ?socket_path.display(),
|
|
||||||
"Stardust socket created"
|
|
||||||
);
|
|
||||||
let socket =
|
|
||||||
UnixListener::bind(socket_path).expect("Couldn't spawn stardust server at {socket_path}");
|
|
||||||
task::new(|| "client join loop", async move {
|
|
||||||
loop {
|
|
||||||
let Ok((stream, _)) = socket.accept().await else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
if let Err(e) = Client::from_connection(stream) {
|
|
||||||
error!(?e, "Unable to create client from connection");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
info!("Init client join loop");
|
|
||||||
|
|
||||||
let project_dirs = ProjectDirs::from("", "", "stardust");
|
let project_dirs = ProjectDirs::from("", "", "stardust");
|
||||||
if project_dirs.is_none() {
|
if project_dirs.is_none() {
|
||||||
error!("Unable to get Stardust project directories, default skybox and startup script will not work.");
|
error!("Unable to get Stardust project directories, default skybox and startup script will not work.");
|
||||||
}
|
}
|
||||||
|
let cli_args = Arc::new(CliArgs::parse());
|
||||||
|
|
||||||
let dbus_connection = Connection::session()
|
let sk = stereokit::Settings {
|
||||||
.await
|
app_name: "Stardust XR".to_string(),
|
||||||
.expect("Could not open dbus session");
|
display_preference: if cli_args.flatscreen {
|
||||||
dbus_connection
|
DisplayMode::Flatscreen
|
||||||
.request_name("org.stardustxr.HMD")
|
|
||||||
.await
|
|
||||||
.expect("Another instance of the server is running. This is not supported currently (but is planned).");
|
|
||||||
|
|
||||||
dbus_connection
|
|
||||||
.object_server()
|
|
||||||
.at("/", ObjectManager)
|
|
||||||
.await
|
|
||||||
.expect("Couldn't add the object manager");
|
|
||||||
|
|
||||||
let sk_ready_notifier = Arc::new(Notify::new());
|
|
||||||
let stereokit_loop = tokio::task::spawn_blocking({
|
|
||||||
let sk_ready_notifier = sk_ready_notifier.clone();
|
|
||||||
let project_dirs = project_dirs.clone();
|
|
||||||
let cli_args = cli_args.clone();
|
|
||||||
let dbus_connection = dbus_connection.clone();
|
|
||||||
move || stereokit_loop(sk_ready_notifier, project_dirs, cli_args, dbus_connection)
|
|
||||||
});
|
|
||||||
sk_ready_notifier.notified().await;
|
|
||||||
let mut startup_children = project_dirs
|
|
||||||
.as_ref()
|
|
||||||
.map(|project_dirs| launch_start(&cli_args, project_dirs))
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
tokio::select! {
|
|
||||||
_ = stereokit_loop => (),
|
|
||||||
_ = tokio::signal::ctrl_c() => unsafe {sk_quit(QuitReason::SystemClose)},
|
|
||||||
}
|
|
||||||
info!("Stopping...");
|
|
||||||
if let Some(project_dirs) = project_dirs {
|
|
||||||
save_session(&project_dirs).await;
|
|
||||||
}
|
|
||||||
for mut startup_child in startup_children.drain(..) {
|
|
||||||
let _ = startup_child.kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Cleanly shut down Stardust");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stereokit_loop(
|
|
||||||
sk_ready_notifier: Arc<Notify>,
|
|
||||||
project_dirs: Option<ProjectDirs>,
|
|
||||||
args: CliArgs,
|
|
||||||
dbus_connection: Connection,
|
|
||||||
) {
|
|
||||||
let sk = SkSettings::default()
|
|
||||||
.app_name("Stardust XR")
|
|
||||||
.mode(if args.flatscreen {
|
|
||||||
AppMode::Simulator
|
|
||||||
} else {
|
} else {
|
||||||
AppMode::XR
|
DisplayMode::MixedReality
|
||||||
})
|
},
|
||||||
.depth_mode(DepthMode::D32)
|
blend_preference: DisplayBlend::AnyTransparent,
|
||||||
.log_filter(match EnvFilter::from_default_env().max_level_hint() {
|
depth_mode: DepthMode::D32,
|
||||||
|
log_filter: match EnvFilter::from_default_env().max_level_hint() {
|
||||||
Some(LevelFilter::ERROR) => LogLevel::Error,
|
Some(LevelFilter::ERROR) => LogLevel::Error,
|
||||||
Some(LevelFilter::WARN) => LogLevel::Warning,
|
Some(LevelFilter::WARN) => LogLevel::Warning,
|
||||||
Some(LevelFilter::INFO) => LogLevel::Inform,
|
Some(LevelFilter::INFO) => LogLevel::Inform,
|
||||||
@@ -188,90 +115,223 @@ fn stereokit_loop(
|
|||||||
Some(LevelFilter::TRACE) => LogLevel::Diagnostic,
|
Some(LevelFilter::TRACE) => LogLevel::Diagnostic,
|
||||||
Some(LevelFilter::OFF) => LogLevel::None,
|
Some(LevelFilter::OFF) => LogLevel::None,
|
||||||
None => LogLevel::Warning,
|
None => LogLevel::Warning,
|
||||||
})
|
},
|
||||||
.overlay_app(args.overlay_priority.is_some())
|
overlay_app: cli_args.overlay_priority.is_some(),
|
||||||
.overlay_priority(args.overlay_priority.unwrap_or(u32::MAX))
|
overlay_priority: cli_args.overlay_priority.unwrap_or(u32::MAX),
|
||||||
.disable_desktop_input_window(true)
|
disable_desktop_input_window: true,
|
||||||
.origin(OriginMode::Local)
|
..Default::default()
|
||||||
.init()
|
}
|
||||||
.expect("StereoKit failed to initialize");
|
.init()
|
||||||
|
.expect("StereoKit failed to initialize");
|
||||||
|
let _ = SK_MULTITHREAD.set(sk.multithreaded());
|
||||||
info!("Init StereoKit");
|
info!("Init StereoKit");
|
||||||
|
|
||||||
Renderer::multisample(0);
|
sk.material_set_shader(
|
||||||
Material::default().shader(Shader::pbr_clip());
|
sk.material_find("default/material_pbr").unwrap(),
|
||||||
Ui::enable_far_interact(false);
|
sk.shader_find("default/shader_pbr_clip").unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
// Skytex/light stuff
|
// Skytex/light stuff
|
||||||
{
|
{
|
||||||
if let Some(sky) = project_dirs
|
if let Some((light, tex)) = project_dirs
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|dirs| dirs.config_dir().join("skytex.hdr"))
|
.and_then(|dirs| {
|
||||||
.filter(|f| f.exists())
|
let skytex_path = dirs.config_dir().join("skytex.hdr");
|
||||||
.and_then(|p| SHCubemap::from_cubemap_equirectangular(p, true, 100).ok())
|
skytex_path
|
||||||
|
.exists()
|
||||||
|
.then(|| sk.tex_create_cubemap_file(&skytex_path, true, 100).ok())
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
{
|
{
|
||||||
sky.render_as_sky();
|
sk.render_set_skytex(&tex);
|
||||||
|
sk.render_set_skylight(light);
|
||||||
} else {
|
} else {
|
||||||
Renderer::skytex(Tex::gen_color(
|
sk.render_set_skytex(sk.tex_gen_color(
|
||||||
Color128::BLACK,
|
BLACK,
|
||||||
1,
|
1,
|
||||||
1,
|
1,
|
||||||
TexType::Cubemap,
|
TextureType::CUBEMAP,
|
||||||
TexFormat::RGBA32,
|
TextureFormat::RGBA32,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut mouse_pointer = cli_args
|
||||||
|
.flatscreen
|
||||||
|
.then(MousePointer::new)
|
||||||
|
.transpose()
|
||||||
|
.unwrap();
|
||||||
|
let mut hands = (!cli_args.flatscreen)
|
||||||
|
.then(|| {
|
||||||
|
let left = SkHand::new(Handed::Left).ok();
|
||||||
|
let right = SkHand::new(Handed::Right).ok();
|
||||||
|
left.zip(right)
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
let mut controllers = (!cli_args.flatscreen && !cli_args.disable_controller)
|
||||||
|
.then(|| {
|
||||||
|
let left = SkController::new(&sk, Handed::Left).ok();
|
||||||
|
let right = SkController::new(&sk, Handed::Right).ok();
|
||||||
|
left.zip(right)
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
let eye_pointer = (sk.active_display_mode() == DisplayMode::MixedReality
|
||||||
|
&& sk.device_has_eye_gaze())
|
||||||
|
.then(EyePointer::new)
|
||||||
|
.transpose()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if hands.is_none() {
|
||||||
|
sk.input_hand_visible(Handed::Left, false);
|
||||||
|
sk.input_hand_visible(Handed::Right, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
let play_space = sk
|
||||||
|
.world_has_bounds()
|
||||||
|
.then(|| PlaySpace::new().ok())
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
let (info_sender, info_receiver) = oneshot::channel::<EventLoopInfo>();
|
||||||
|
let event_thread = std::thread::Builder::new()
|
||||||
|
.name("event_loop".to_owned())
|
||||||
|
.spawn(move || event_loop(info_sender))
|
||||||
|
.unwrap();
|
||||||
|
let event_loop_info = info_receiver.blocking_recv().unwrap();
|
||||||
|
let _tokio_handle = event_loop_info.tokio_handle.enter();
|
||||||
|
|
||||||
|
let mut buffer_manager = buffers::BufferManager::new().expect("Could not initialize buffer manager");
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
let mut wayland = wayland::Wayland::new().expect("Could not initialize wayland");
|
let mut wayland = wayland::Wayland::new(&buffer_manager).expect("Could not initialize wayland");
|
||||||
#[cfg(feature = "wayland")]
|
|
||||||
wayland.make_context_current();
|
|
||||||
sk_ready_notifier.notify_waiters();
|
|
||||||
info!("Stardust ready!");
|
info!("Stardust ready!");
|
||||||
|
|
||||||
let mut objects = ServerObjects::new(
|
let mut startup_child = (|| {
|
||||||
dbus_connection.clone(),
|
let project_dirs = project_dirs.as_ref()?;
|
||||||
&sk,
|
let startup_script_path = cli_args
|
||||||
args.disable_controllers,
|
.startup_script
|
||||||
args.disable_hands,
|
.clone()
|
||||||
);
|
.and_then(|p| p.canonicalize().ok())
|
||||||
|
.unwrap_or_else(|| project_dirs.config_dir().join("startup"));
|
||||||
|
let mut startup_command = Command::new("bash");
|
||||||
|
startup_command.arg(startup_script_path);
|
||||||
|
startup_command.arg("&");
|
||||||
|
|
||||||
|
startup_command.stdin(Stdio::null());
|
||||||
|
startup_command.stdout(Stdio::null());
|
||||||
|
startup_command.stderr(Stdio::null());
|
||||||
|
startup_command.env(
|
||||||
|
"FLAT_WAYLAND_DISPLAY",
|
||||||
|
std::env::var_os("WAYLAND_DISPLAY").unwrap_or_default(),
|
||||||
|
);
|
||||||
|
startup_command.env(
|
||||||
|
"STARDUST_INSTANCE",
|
||||||
|
event_loop_info
|
||||||
|
.socket_path
|
||||||
|
.file_name()
|
||||||
|
.expect("Stardust socket path not found"),
|
||||||
|
);
|
||||||
|
#[cfg(feature = "wayland")]
|
||||||
|
{
|
||||||
|
if let Some(wayland_socket) = wayland.socket_name.as_ref() {
|
||||||
|
startup_command.env("WAYLAND_DISPLAY", &wayland_socket);
|
||||||
|
}
|
||||||
|
#[cfg(feature = "xwayland")]
|
||||||
|
startup_command.env("DISPLAY", format!(":{}", wayland.xwayland_state.display));
|
||||||
|
startup_command.env("GDK_BACKEND", "wayland");
|
||||||
|
startup_command.env("QT_QPA_PLATFORM", "wayland");
|
||||||
|
startup_command.env("MOZ_ENABLE_WAYLAND", "1");
|
||||||
|
startup_command.env("CLUTTER_BACKEND", "wayland");
|
||||||
|
startup_command.env("SDL_VIDEODRIVER", "wayland");
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
startup_command.pre_exec(|| {
|
||||||
|
nix::unistd::setsid()
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| std::io::ErrorKind::Other.into())
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let child = startup_command.spawn().ok()?;
|
||||||
|
Some(child)
|
||||||
|
})();
|
||||||
|
|
||||||
let mut last_frame_delta = Duration::ZERO;
|
let mut last_frame_delta = Duration::ZERO;
|
||||||
let mut sleep_duration = Duration::ZERO;
|
let mut sleep_duration = Duration::ZERO;
|
||||||
while let Some(token) = sk.step() {
|
debug_span!("StereoKit").in_scope(|| {
|
||||||
let _span = debug_span!("StereoKit step");
|
sk.run(
|
||||||
let _span = _span.enter();
|
|sk| {
|
||||||
|
let _span = debug_span!("StereoKit step");
|
||||||
|
let _span = _span.enter();
|
||||||
|
|
||||||
camera::update(token);
|
camera::send_rendered();
|
||||||
#[cfg(feature = "wayland")]
|
hmd::frame(sk);
|
||||||
wayland.frame_event();
|
#[cfg(feature = "wayland")]
|
||||||
destroy_queue::clear();
|
wayland.frame_event(sk);
|
||||||
|
destroy_queue::clear();
|
||||||
|
|
||||||
objects.update(&sk, token);
|
if let Some(mouse_pointer) = &mut mouse_pointer {
|
||||||
input::process_input();
|
mouse_pointer.update(sk);
|
||||||
nodes::root::Root::send_frame_events(Time::get_step_unscaled());
|
}
|
||||||
adaptive_sleep(
|
if let Some((left_hand, right_hand)) = &mut hands {
|
||||||
&mut last_frame_delta,
|
left_hand.update(!cli_args.disable_controller, sk);
|
||||||
&mut sleep_duration,
|
right_hand.update(!cli_args.disable_controller, sk);
|
||||||
Duration::from_micros(250),
|
}
|
||||||
);
|
if let Some((left_controller, right_controller)) = &mut controllers {
|
||||||
|
left_controller.update(sk);
|
||||||
|
right_controller.update(sk);
|
||||||
|
}
|
||||||
|
if let Some(eye_pointer) = &eye_pointer {
|
||||||
|
eye_pointer.update(sk);
|
||||||
|
}
|
||||||
|
if let Some(play_space) = &play_space {
|
||||||
|
play_space.update(sk);
|
||||||
|
}
|
||||||
|
input::process_input();
|
||||||
|
nodes::root::Root::send_frame_events(sk.time_elapsed_unscaled());
|
||||||
|
adaptive_sleep(
|
||||||
|
sk,
|
||||||
|
&mut last_frame_delta,
|
||||||
|
&mut sleep_duration,
|
||||||
|
Duration::from_micros(250),
|
||||||
|
);
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
wayland.update();
|
wayland.update(sk, &mut buffer_manager);
|
||||||
drawable::draw(token);
|
drawable::draw(sk);
|
||||||
audio::update();
|
audio::update(sk);
|
||||||
}
|
|
||||||
|
buffer_manager.make_context_current();
|
||||||
|
|
||||||
|
camera::update(sk, &mut buffer_manager);
|
||||||
|
},
|
||||||
|
|_sk| {
|
||||||
|
info!("Cleanly shut down StereoKit");
|
||||||
|
|
||||||
|
if let Some(mut startup_child) = startup_child.take() {
|
||||||
|
let _ = startup_child.kill();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
info!("Cleanly shut down StereoKit");
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
drop(wayland);
|
drop(wayland);
|
||||||
|
|
||||||
|
STOP_NOTIFIER.notify_waiters();
|
||||||
|
event_thread
|
||||||
|
.join()
|
||||||
|
.expect("Failed to cleanly shut down event loop")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
info!("Cleanly shut down Stardust");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn adaptive_sleep(
|
fn adaptive_sleep(
|
||||||
|
sk: &impl StereoKitMultiThread,
|
||||||
last_frame_delta: &mut Duration,
|
last_frame_delta: &mut Duration,
|
||||||
sleep_duration: &mut Duration,
|
sleep_duration: &mut Duration,
|
||||||
sleep_duration_increase: Duration,
|
sleep_duration_increase: Duration,
|
||||||
) {
|
) {
|
||||||
let frame_delta = Duration::from_secs_f64(Time::get_step_unscaled());
|
let frame_delta = Duration::from_secs_f64(sk.time_elapsed_unscaled());
|
||||||
if *last_frame_delta < frame_delta {
|
if *last_frame_delta < frame_delta {
|
||||||
if let Some(frame_delta_delta) = frame_delta.checked_sub(*last_frame_delta) {
|
if let Some(frame_delta_delta) = frame_delta.checked_sub(*last_frame_delta) {
|
||||||
if let Some(new_sleep_duration) = sleep_duration.checked_sub(frame_delta_delta) {
|
if let Some(new_sleep_duration) = sleep_duration.checked_sub(frame_delta_delta) {
|
||||||
@@ -287,3 +347,47 @@ fn adaptive_sleep(
|
|||||||
std::thread::sleep(*sleep_duration); // to give clients a chance to even update anything before drawing
|
std::thread::sleep(*sleep_duration); // to give clients a chance to even update anything before drawing
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
// #[tokio::main(flavor = "current_thread")]
|
||||||
|
async fn event_loop(info_sender: oneshot::Sender<EventLoopInfo>) -> color_eyre::eyre::Result<()> {
|
||||||
|
let socket_path =
|
||||||
|
server::get_free_socket_path().expect("Unable to find a free stardust socket path");
|
||||||
|
STARDUST_INSTANCE.set(socket_path.file_name().unwrap().to_string_lossy().into_owned()).expect("Someone hasn't done their job, yell at Nova because how is this set multiple times what the hell");
|
||||||
|
let _event_loop = EventLoop::new(socket_path.clone()).expect("Couldn't create server socket");
|
||||||
|
info!("Init event loop");
|
||||||
|
info!(
|
||||||
|
socket_path = ?socket_path.display(),
|
||||||
|
"Stardust socket created"
|
||||||
|
);
|
||||||
|
let _ = info_sender.send(EventLoopInfo {
|
||||||
|
tokio_handle: Handle::current(),
|
||||||
|
socket_path,
|
||||||
|
});
|
||||||
|
|
||||||
|
STOP_NOTIFIER.notified().await;
|
||||||
|
println!("Stopping...");
|
||||||
|
save_clients().await;
|
||||||
|
|
||||||
|
info!("Cleanly shut down event loop");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
stereokit::sys::sk_quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn save_clients() {
|
||||||
|
let local_set = LocalSet::new();
|
||||||
|
for client in CLIENTS.get_vec() {
|
||||||
|
local_set.spawn_local(async move {
|
||||||
|
tokio::select! {
|
||||||
|
biased;
|
||||||
|
s = client.save_state() => {s.map(ClientState::to_file);},
|
||||||
|
_ = tokio::time::sleep(Duration::from_millis(100)) => (),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
local_set.await;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,141 +1,49 @@
|
|||||||
use super::{Aspect, Node};
|
use super::Node;
|
||||||
use crate::core::{client::Client, registry::Registry};
|
use crate::core::client::Client;
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::{ensure, Result};
|
||||||
use std::{
|
use portable_atomic::AtomicBool;
|
||||||
ops::Add,
|
use std::sync::{Arc, Weak};
|
||||||
sync::{Arc, Weak},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct AliasInfo {
|
pub struct AliasInfo {
|
||||||
pub(super) server_signals: Vec<u64>,
|
pub(super) server_signals: Vec<&'static str>,
|
||||||
pub(super) server_methods: Vec<u64>,
|
pub(super) server_methods: Vec<&'static str>,
|
||||||
pub(super) client_signals: Vec<u64>,
|
pub(super) client_signals: Vec<&'static str>,
|
||||||
}
|
|
||||||
impl Add for AliasInfo {
|
|
||||||
type Output = AliasInfo;
|
|
||||||
fn add(mut self, mut rhs: Self) -> Self::Output {
|
|
||||||
self.server_signals.append(&mut rhs.server_signals);
|
|
||||||
self.server_methods.append(&mut rhs.server_methods);
|
|
||||||
self.client_signals.append(&mut rhs.client_signals);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[allow(dead_code)]
|
||||||
pub struct Alias {
|
pub struct Alias {
|
||||||
|
pub enabled: Arc<AtomicBool>,
|
||||||
pub(super) node: Weak<Node>,
|
pub(super) node: Weak<Node>,
|
||||||
pub(super) original: Weak<Node>,
|
pub original: Weak<Node>,
|
||||||
pub(super) info: AliasInfo,
|
|
||||||
|
pub info: AliasInfo,
|
||||||
}
|
}
|
||||||
impl Alias {
|
impl Alias {
|
||||||
pub fn create(
|
pub fn create(
|
||||||
original: &Arc<Node>,
|
|
||||||
client: &Arc<Client>,
|
client: &Arc<Client>,
|
||||||
info: AliasInfo,
|
parent: &str,
|
||||||
list: Option<&AliasList>,
|
name: &str,
|
||||||
) -> Result<Arc<Node>> {
|
|
||||||
let node = Node::generate(client, true).add_to_scenegraph()?;
|
|
||||||
Self::add_to(&node, original, info)?;
|
|
||||||
if let Some(list) = list {
|
|
||||||
list.add(&node);
|
|
||||||
}
|
|
||||||
Ok(node)
|
|
||||||
}
|
|
||||||
pub fn create_with_id(
|
|
||||||
original: &Arc<Node>,
|
original: &Arc<Node>,
|
||||||
client: &Arc<Client>,
|
|
||||||
new_id: u64,
|
|
||||||
info: AliasInfo,
|
info: AliasInfo,
|
||||||
list: Option<&AliasList>,
|
|
||||||
) -> Result<Arc<Node>> {
|
) -> Result<Arc<Node>> {
|
||||||
let node = Node::from_id(client, new_id, true).add_to_scenegraph()?;
|
ensure!(
|
||||||
Self::add_to(&node, original, info)?;
|
client
|
||||||
if let Some(list) = list {
|
.scenegraph
|
||||||
list.add(&node);
|
.get_node(&(parent.to_string() + "/" + name))
|
||||||
}
|
.is_none(),
|
||||||
Ok(node)
|
"Node already exists"
|
||||||
}
|
);
|
||||||
|
|
||||||
fn add_to(new_node: &Arc<Node>, original: &Arc<Node>, info: AliasInfo) -> Result<()> {
|
let node = Node::create(client, parent, name, true).add_to_scenegraph()?;
|
||||||
let alias = Alias {
|
let alias = Alias {
|
||||||
node: Arc::downgrade(new_node),
|
enabled: Arc::new(AtomicBool::new(true)),
|
||||||
|
node: Arc::downgrade(&node),
|
||||||
original: Arc::downgrade(original),
|
original: Arc::downgrade(original),
|
||||||
info,
|
info,
|
||||||
};
|
};
|
||||||
let alias = original.aliases.add(alias);
|
let alias = original.aliases.add(alias);
|
||||||
new_node.add_aspect_raw(alias);
|
let _ = node.alias.set(alias);
|
||||||
Ok(())
|
Ok(node)
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Aspect for Alias {
|
|
||||||
const NAME: &'static str = "Alias";
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_original(node: Arc<Node>, stop_on_disabled: bool) -> Option<Arc<Node>> {
|
|
||||||
let Ok(alias) = node.get_aspect::<Alias>() else {
|
|
||||||
return Some(node);
|
|
||||||
};
|
|
||||||
if stop_on_disabled && !node.enabled() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
get_original(alias.original.upgrade()?, stop_on_disabled)
|
|
||||||
}
|
|
||||||
pub fn links_to(alias: Arc<Node>, original: Weak<Node>) -> bool {
|
|
||||||
let Ok(alias) = alias.get_aspect::<Alias>() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
if alias.original.ptr_eq(&original) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
let Some(original_strong) = alias.original.upgrade() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
links_to(original_strong, original)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
pub struct AliasList(Registry<Node>);
|
|
||||||
impl AliasList {
|
|
||||||
fn add(&self, node: &Arc<Node>) {
|
|
||||||
self.0.add_raw(node);
|
|
||||||
}
|
|
||||||
pub fn get_from_original_node(&self, original: Weak<Node>) -> Option<Arc<Node>> {
|
|
||||||
self.0
|
|
||||||
.get_valid_contents()
|
|
||||||
.into_iter()
|
|
||||||
.find(move |node| links_to(node.clone(), original.clone()))
|
|
||||||
}
|
|
||||||
pub fn get_from_aspect<A: Aspect>(&self, aspect: &A) -> Option<Arc<Node>> {
|
|
||||||
self.0.get_valid_contents().into_iter().find(|node| {
|
|
||||||
let Some(node) = get_original(node.clone(), false) else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
let Ok(aspect2) = node.get_aspect::<A>() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
Arc::as_ptr(&aspect2) == (aspect as *const A)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
pub fn get_aliases(&self) -> Vec<Arc<Node>> {
|
|
||||||
self.0.get_valid_contents()
|
|
||||||
}
|
|
||||||
pub fn remove_aspect<A: Aspect>(&self, aspect: &A) {
|
|
||||||
self.0.retain(|node| {
|
|
||||||
let Some(original) = get_original(node.clone(), false) else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
let Ok(aspect2) = original.get_aspect::<A>() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
Arc::as_ptr(&aspect2) != (aspect as *const A)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for AliasList {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
for node in self.0.take_valid_contents() {
|
|
||||||
node.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +1,53 @@
|
|||||||
use super::{Aspect, Node};
|
use super::{Message, Node};
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use crate::core::destroy_queue;
|
use crate::core::destroy_queue;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::core::resource::get_resource_file;
|
use crate::core::resource::ResourceID;
|
||||||
use crate::create_interface;
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
use color_eyre::eyre::{ensure, eyre, Result};
|
||||||
use crate::nodes::spatial::{Spatial, Transform};
|
|
||||||
use color_eyre::eyre::{eyre, Result};
|
|
||||||
use glam::{vec3, Vec4Swizzles};
|
use glam::{vec3, Vec4Swizzles};
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use stardust_xr::values::ResourceID;
|
use send_wrapper::SendWrapper;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::sync::Arc;
|
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
|
||||||
use std::{ffi::OsStr, path::PathBuf};
|
use stereokit::{Sound as SkSound, SoundInstance, StereoKitDraw};
|
||||||
use stereokit_rust::sound::{Sound as SkSound, SoundInst};
|
|
||||||
|
|
||||||
static SOUND_REGISTRY: Registry<Sound> = Registry::new();
|
static SOUND_REGISTRY: Registry<Sound> = Registry::new();
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_audio_protocol!();
|
|
||||||
pub struct Sound {
|
pub struct Sound {
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
|
|
||||||
volume: f32,
|
volume: f32,
|
||||||
pending_audio_path: PathBuf,
|
pending_audio_path: PathBuf,
|
||||||
sk_sound: OnceCell<SkSound>,
|
sk_sound: OnceCell<SendWrapper<SkSound>>,
|
||||||
instance: Mutex<Option<SoundInst>>,
|
instance: Mutex<Option<SoundInstance>>,
|
||||||
stop: Mutex<Option<()>>,
|
stop: Mutex<Option<()>>,
|
||||||
play: Mutex<Option<()>>,
|
play: Mutex<Option<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sound {
|
impl Sound {
|
||||||
pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Sound>> {
|
pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Sound>> {
|
||||||
let pending_audio_path = get_resource_file(
|
ensure!(
|
||||||
&resource_id,
|
node.spatial.get().is_some(),
|
||||||
&*node.get_client().ok_or_else(|| eyre!("Client not found"))?,
|
"Internal: Node does not have a spatial attached!"
|
||||||
&[OsStr::new("wav"), OsStr::new("mp3")],
|
);
|
||||||
)
|
let pending_audio_path = resource_id
|
||||||
.ok_or_else(|| eyre!("Resource not found"))?;
|
.get_file(
|
||||||
|
&node
|
||||||
|
.get_client()
|
||||||
|
.ok_or_else(|| eyre!("Client not found"))?
|
||||||
|
.base_resource_prefixes
|
||||||
|
.lock()
|
||||||
|
.clone(),
|
||||||
|
&[OsStr::new("wav"), OsStr::new("mp3")],
|
||||||
|
)
|
||||||
|
.ok_or_else(|| eyre!("Resource not found"))?;
|
||||||
let sound = Sound {
|
let sound = Sound {
|
||||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
space: node.spatial.get().unwrap().clone(),
|
||||||
volume: 1.0,
|
volume: 1.0,
|
||||||
pending_audio_path,
|
pending_audio_path,
|
||||||
sk_sound: OnceCell::new(),
|
sk_sound: OnceCell::new(),
|
||||||
@@ -48,77 +56,81 @@ impl Sound {
|
|||||||
play: Mutex::new(None),
|
play: Mutex::new(None),
|
||||||
};
|
};
|
||||||
let sound_arc = SOUND_REGISTRY.add(sound);
|
let sound_arc = SOUND_REGISTRY.add(sound);
|
||||||
node.add_aspect_raw(sound_arc.clone());
|
node.add_local_signal("play", Sound::play_flex);
|
||||||
<Sound as SoundAspect>::add_node_members(node);
|
node.add_local_signal("stop", Sound::stop_flex);
|
||||||
|
let _ = node.sound.set(sound_arc.clone());
|
||||||
Ok(sound_arc)
|
Ok(sound_arc)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&self) {
|
fn update(&self, sk: &impl StereoKitDraw) {
|
||||||
let sound = self
|
let sound = self.sk_sound.get_or_init(|| {
|
||||||
.sk_sound
|
SendWrapper::new(sk.sound_create(self.pending_audio_path.clone()).unwrap())
|
||||||
.get_or_init(|| SkSound::from_file(self.pending_audio_path.clone()).unwrap());
|
});
|
||||||
if self.stop.lock().take().is_some() {
|
if self.stop.lock().take().is_some() {
|
||||||
if let Some(instance) = self.instance.lock().take() {
|
if let Some(instance) = self.instance.lock().take() {
|
||||||
instance.stop();
|
sk.sound_inst_stop(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.instance.lock().is_none() && self.play.lock().take().is_some() {
|
if self.play.lock().is_some() && self.instance.lock().is_none() {
|
||||||
let instance = sound.play(vec3(0.0, 0.0, 0.0), Some(self.volume));
|
self.instance.lock().replace(sk.sound_play(
|
||||||
self.instance.lock().replace(instance);
|
sound.as_ref(),
|
||||||
|
vec3(0.0, 0.0, 0.0),
|
||||||
|
self.volume,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
if let Some(instance) = self.instance.lock().deref_mut() {
|
if let Some(instance) = self.instance.lock().deref_mut() {
|
||||||
instance.position(self.space.global_transform().w_axis.xyz());
|
sk.sound_inst_set_pos(*instance, self.space.global_transform().w_axis.xyz());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl Aspect for Sound {
|
fn play_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
|
||||||
const NAME: &'static str = "Sound";
|
let sound = node.sound.get().unwrap();
|
||||||
}
|
|
||||||
impl SoundAspect for Sound {
|
|
||||||
fn play(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
let sound = node.get_aspect::<Sound>().unwrap();
|
|
||||||
sound.play.lock().replace(());
|
sound.play.lock().replace(());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn stop(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
let sound = node.get_aspect::<Sound>().unwrap();
|
pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
|
||||||
|
let sound = node.sound.get().unwrap();
|
||||||
sound.stop.lock().replace(());
|
sound.stop.lock().replace(());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update(sk: &impl StereoKitDraw) {
|
||||||
|
for sound in SOUND_REGISTRY.get_valid_contents() {
|
||||||
|
sound.update(sk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "audio", false);
|
||||||
|
node.add_local_signal("create_sound", create_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateSoundInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
resource: ResourceID,
|
||||||
|
}
|
||||||
|
let info: CreateSoundInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/audio/sound", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
Sound::add_to(&node, info.resource)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
impl Drop for Sound {
|
impl Drop for Sound {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(sk_sound) = self.sk_sound.take() {
|
if let Some(instance) = self.instance.lock().take() {
|
||||||
destroy_queue::add(sk_sound);
|
destroy_queue::add(instance);
|
||||||
}
|
}
|
||||||
SOUND_REGISTRY.remove(self);
|
SOUND_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update() {
|
|
||||||
for sound in SOUND_REGISTRY.get_valid_contents() {
|
|
||||||
sound.update()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
create_interface!(AudioInterface);
|
|
||||||
struct AudioInterface;
|
|
||||||
impl InterfaceAspect for AudioInterface {
|
|
||||||
#[doc = "Create a sound node. WAV and MP3 are supported."]
|
|
||||||
fn create_sound(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
resource: ResourceID,
|
|
||||||
) -> Result<()> {
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, true);
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Sound::add_to(&node, resource)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,44 +1,41 @@
|
|||||||
use super::alias::AliasList;
|
use super::alias::AliasInfo;
|
||||||
use super::fields::Field;
|
use super::fields::Field;
|
||||||
use super::spatial::{parse_transform, Spatial};
|
use super::spatial::{parse_transform, Spatial};
|
||||||
use super::{Alias, Aspect, Node};
|
use super::{Alias, Message, Node};
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use crate::core::node_collections::LifeLinkedNodeMap;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::create_interface;
|
use crate::core::scenegraph::MethodResponseSender;
|
||||||
use crate::nodes::fields::FIELD_ALIAS_INFO;
|
use crate::nodes::fields::{find_field, FIELD_ALIAS_INFO};
|
||||||
use crate::nodes::spatial::Transform;
|
use crate::nodes::spatial::find_spatial_parent;
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
|
||||||
use color_eyre::eyre::{bail, ensure, eyre, Result};
|
use color_eyre::eyre::{bail, ensure, eyre, Result};
|
||||||
|
use glam::vec3a;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use mint::{Quaternion, Vector3};
|
||||||
|
use nanoid::nanoid;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use slotmap::{DefaultKey, Key, KeyData, SlotMap};
|
use rustc_hash::FxHashMap;
|
||||||
use stardust_xr::schemas::flex::flexbuffers;
|
use serde::{Deserialize, Serialize};
|
||||||
use stardust_xr::values::Datamap;
|
use stardust_xr::schemas::flex::{deserialize, flexbuffers, serialize};
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref KEYMAPS: Mutex<SlotMap<DefaultKey, String>> = Mutex::new(SlotMap::default());
|
pub static ref KEYMAPS: Mutex<FxHashMap<String, String>> = Mutex::new(FxHashMap::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: probably just use d-bus for this stuff (custom protocol for exporting spatials as refs) because the mask stuff is just too confusing
|
|
||||||
|
|
||||||
static PULSE_SENDER_REGISTRY: Registry<PulseSender> = Registry::new();
|
static PULSE_SENDER_REGISTRY: Registry<PulseSender> = Registry::new();
|
||||||
pub static PULSE_RECEIVER_REGISTRY: Registry<PulseReceiver> = Registry::new();
|
pub static PULSE_RECEIVER_REGISTRY: Registry<PulseReceiver> = Registry::new();
|
||||||
|
|
||||||
pub fn get_mask(datamap: &Datamap) -> Result<flexbuffers::MapReader<&[u8]>> {
|
pub fn mask_matches(mask_map_lesser: &Mask, mask_map_greater: &Mask) -> bool {
|
||||||
flexbuffers::Reader::get_root(datamap.raw().as_slice())
|
|
||||||
.map_err(|_| eyre!("Mask is not a valid flexbuffer"))?
|
|
||||||
.get_map()
|
|
||||||
.map_err(|_| eyre!("Mask is not a valid map"))
|
|
||||||
}
|
|
||||||
pub fn mask_matches(mask_map_lesser: &Datamap, mask_map_greater: &Datamap) -> bool {
|
|
||||||
(|| -> Result<_> {
|
(|| -> Result<_> {
|
||||||
for key in get_mask(mask_map_lesser)?.iter_keys() {
|
for key in mask_map_lesser.get_mask()?.iter_keys() {
|
||||||
let lesser_key = get_mask(mask_map_lesser)?.index(key)?;
|
let lesser_key = mask_map_lesser.get_mask()?.index(key)?;
|
||||||
let greater_key = get_mask(mask_map_greater)?.index(key)?;
|
let greater_key = mask_map_greater.get_mask()?.index(key)?;
|
||||||
// otherwise zero-length vectors don't count the same as a single type vector
|
// otherwise zero-length vectors don't count the same as a single type vector
|
||||||
if lesser_key.flexbuffer_type().is_heterogenous_vector()
|
if lesser_key.flexbuffer_type().is_heterogenous_vector()
|
||||||
&& lesser_key.as_vector().is_empty()
|
&& lesser_key.as_vector().len() == 0
|
||||||
&& greater_key.flexbuffer_type().is_vector()
|
&& greater_key.flexbuffer_type().is_vector()
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@@ -54,26 +51,57 @@ pub fn mask_matches(mask_map_lesser: &Datamap, mask_map_greater: &Datamap) -> bo
|
|||||||
.is_ok()
|
.is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_data_protocol!();
|
pub struct Mask(pub Vec<u8>);
|
||||||
|
impl Mask {
|
||||||
|
pub fn from_struct<T: Default + Serialize>() -> Self {
|
||||||
|
let mut serializer = flexbuffers::FlexbufferSerializer::new();
|
||||||
|
T::default().serialize(&mut serializer).unwrap();
|
||||||
|
Mask(serializer.take_buffer())
|
||||||
|
}
|
||||||
|
pub fn get_mask(&self) -> Result<flexbuffers::MapReader<&[u8]>> {
|
||||||
|
flexbuffers::Reader::get_root(self.0.as_slice())
|
||||||
|
.map_err(|_| eyre!("Mask is not a valid flexbuffer"))?
|
||||||
|
.get_map()
|
||||||
|
.map_err(|_| eyre!("Mask is not a valid map"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Display for Mask {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
flexbuffers::Reader::get_root(self.0.as_slice())
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct SendDataInfo<'a> {
|
||||||
|
uid: &'a str,
|
||||||
|
data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PulseSender {
|
pub struct PulseSender {
|
||||||
|
uid: String,
|
||||||
node: Weak<Node>,
|
node: Weak<Node>,
|
||||||
pub mask: Datamap,
|
pub mask: Mask,
|
||||||
aliases: AliasList,
|
aliases: LifeLinkedNodeMap<String>,
|
||||||
field_aliases: AliasList,
|
|
||||||
}
|
}
|
||||||
impl PulseSender {
|
impl PulseSender {
|
||||||
pub fn add_to(node: &Arc<Node>, mask: Datamap) -> Result<Arc<PulseSender>> {
|
pub fn add_to(node: &Arc<Node>, mask: Mask) -> Result<Arc<PulseSender>> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
|
||||||
let sender = PulseSender {
|
let sender = PulseSender {
|
||||||
|
uid: nanoid!(),
|
||||||
node: Arc::downgrade(node),
|
node: Arc::downgrade(node),
|
||||||
mask,
|
mask,
|
||||||
aliases: AliasList::default(),
|
aliases: LifeLinkedNodeMap::default(),
|
||||||
field_aliases: AliasList::default(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// <PulseSender as PulseSenderAspect>::add_node_members(node);
|
|
||||||
let sender = PULSE_SENDER_REGISTRY.add(sender);
|
let sender = PULSE_SENDER_REGISTRY.add(sender);
|
||||||
node.add_aspect_raw(sender.clone());
|
let _ = node.pulse_sender.set(sender.clone());
|
||||||
|
node.add_local_signal("send_data", PulseSender::send_data_flex);
|
||||||
for receiver in PULSE_RECEIVER_REGISTRY.get_valid_contents() {
|
for receiver in PULSE_RECEIVER_REGISTRY.get_valid_contents() {
|
||||||
sender.handle_new_receiver(&receiver);
|
sender.handle_new_receiver(&receiver);
|
||||||
}
|
}
|
||||||
@@ -83,60 +111,97 @@ impl PulseSender {
|
|||||||
if !mask_matches(&self.mask, &receiver.mask) {
|
if !mask_matches(&self.mask, &receiver.mask) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let Some(tx_node) = self.node.upgrade() else {
|
let Some(tx_node) = self.node.upgrade() else {return};
|
||||||
return;
|
let Some(tx_client) = tx_node.get_client() else {return};
|
||||||
};
|
let Some(rx_node) = receiver.node.upgrade() else {return};
|
||||||
let Some(tx_client) = tx_node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(rx_node) = receiver.node.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
// Receiver itself
|
// Receiver itself
|
||||||
let Ok(rx_alias) = Alias::create(
|
let rx_alias = Alias::create(
|
||||||
|
&tx_client,
|
||||||
|
tx_node.get_path(),
|
||||||
|
receiver.uid.as_str(),
|
||||||
&rx_node,
|
&rx_node,
|
||||||
&tx_client,
|
AliasInfo {
|
||||||
PULSE_RECEIVER_ASPECT_ALIAS_INFO.clone(),
|
server_methods: vec!["sendData", "getTransform"],
|
||||||
Some(&self.aliases),
|
..Default::default()
|
||||||
) else {
|
},
|
||||||
return;
|
);
|
||||||
};
|
if let Ok(rx_alias) = rx_alias {
|
||||||
|
self.aliases.add(receiver.uid.clone(), &rx_alias);
|
||||||
|
|
||||||
// Receiver's field
|
if let Some(rx_field_node) = receiver.field.spatial_ref().node.upgrade() {
|
||||||
let Ok(rx_field_alias) = Alias::create(
|
// Receiver's field
|
||||||
&rx_node
|
let rx_field_alias = Alias::create(
|
||||||
.get_aspect::<PulseReceiver>()
|
&tx_client,
|
||||||
.unwrap()
|
rx_alias.get_path(),
|
||||||
|
"field",
|
||||||
|
&rx_field_node,
|
||||||
|
FIELD_ALIAS_INFO.clone(),
|
||||||
|
);
|
||||||
|
if let Ok(rx_field_alias) = rx_field_alias {
|
||||||
|
self.aliases
|
||||||
|
.add(receiver.uid.clone() + "-field", &rx_field_alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct NewReceiverInfo<'a> {
|
||||||
|
uid: &'a str,
|
||||||
|
distance: f32,
|
||||||
|
position: Vector3<f32>,
|
||||||
|
rotation: Quaternion<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_, rotation, position) = Spatial::space_to_space_matrix(
|
||||||
|
rx_node.spatial.get().map(|s| s.as_ref()),
|
||||||
|
tx_node.spatial.get().map(|s| s.as_ref()),
|
||||||
|
)
|
||||||
|
.to_scale_rotation_translation();
|
||||||
|
|
||||||
|
let info = NewReceiverInfo {
|
||||||
|
uid: &receiver.uid,
|
||||||
|
distance: receiver
|
||||||
.field
|
.field
|
||||||
.spatial
|
.distance(tx_node.spatial.get().unwrap(), vec3a(0.0, 0.0, 0.0)),
|
||||||
.node()
|
position: position.into(),
|
||||||
.unwrap(),
|
rotation: rotation.into(),
|
||||||
&tx_client,
|
|
||||||
FIELD_ALIAS_INFO.clone(),
|
|
||||||
Some(&self.aliases),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = pulse_sender_client::new_receiver(&tx_node, &rx_alias, &rx_field_alias);
|
let Ok(data) = serialize(info) else {return};
|
||||||
|
let _ = tx_node.send_remote_signal("new_receiver", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_drop_receiver(&self, receiver: &PulseReceiver) {
|
fn handle_drop_receiver(&self, receiver: &PulseReceiver) {
|
||||||
let Some(node) = receiver.node.upgrade() else {
|
let uid = receiver.uid.as_str();
|
||||||
return;
|
self.aliases.remove(uid);
|
||||||
};
|
self.aliases.remove(&(uid.to_string() + "-field"));
|
||||||
self.aliases.remove_aspect(receiver);
|
let Some(tx_node) = self.node.upgrade() else {return};
|
||||||
self.field_aliases.remove_aspect(receiver.field.as_ref());
|
let Ok(data) = serialize(&uid) else {return};
|
||||||
let Some(tx_node) = self.node.upgrade() else {
|
let _ = tx_node.send_remote_signal("drop_receiver", data);
|
||||||
return;
|
}
|
||||||
};
|
|
||||||
let _ = pulse_sender_client::drop_receiver(&tx_node, node.get_id());
|
fn send_data_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let info: SendDataInfo = deserialize(message.as_ref())?;
|
||||||
|
let receiver_node = calling_client.get_node("Pulse receiver", info.uid)?;
|
||||||
|
let receiver =
|
||||||
|
receiver_node.get_aspect("Pulse Receiver", "pulse receiver", |n| &n.pulse_receiver)?;
|
||||||
|
let receiver_mask = &receiver_node
|
||||||
|
.get_aspect("Pulse receiver", "pulse receiver", |node| {
|
||||||
|
&node.pulse_receiver
|
||||||
|
})?
|
||||||
|
.mask;
|
||||||
|
|
||||||
|
let data_mask = Mask(info.data);
|
||||||
|
data_mask.get_mask()?;
|
||||||
|
ensure!(
|
||||||
|
mask_matches(receiver_mask, &data_mask),
|
||||||
|
"Message ({}) does not contain the same keys as the receiver's mask ({})",
|
||||||
|
data_mask,
|
||||||
|
receiver_mask
|
||||||
|
);
|
||||||
|
receiver.send_data(&node.pulse_sender.get().unwrap().uid, data_mask.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Aspect for PulseSender {
|
|
||||||
const NAME: &'static str = "PulseSender";
|
|
||||||
}
|
|
||||||
impl PulseSenderAspect for PulseSender {}
|
|
||||||
impl Drop for PulseSender {
|
impl Drop for PulseSender {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
PULSE_SENDER_REGISTRY.remove(self);
|
PULSE_SENDER_REGISTRY.remove(self);
|
||||||
@@ -144,52 +209,41 @@ impl Drop for PulseSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct PulseReceiver {
|
pub struct PulseReceiver {
|
||||||
|
uid: String,
|
||||||
pub node: Weak<Node>,
|
pub node: Weak<Node>,
|
||||||
pub field: Arc<Field>,
|
pub field: Arc<Field>,
|
||||||
pub mask: Datamap,
|
pub mask: Mask,
|
||||||
}
|
}
|
||||||
impl PulseReceiver {
|
impl PulseReceiver {
|
||||||
pub fn add_to(
|
pub fn add_to(node: &Arc<Node>, field: Arc<Field>, mask: Mask) -> Result<Arc<PulseReceiver>> {
|
||||||
node: &Arc<Node>,
|
ensure!(
|
||||||
field: Arc<Field>,
|
node.spatial.get().is_some(),
|
||||||
mask: Datamap,
|
"Internal: Node does not have a spatial attached!"
|
||||||
) -> Result<Arc<PulseReceiver>> {
|
);
|
||||||
|
|
||||||
let receiver = PulseReceiver {
|
let receiver = PulseReceiver {
|
||||||
|
uid: nanoid!(),
|
||||||
node: Arc::downgrade(node),
|
node: Arc::downgrade(node),
|
||||||
field,
|
field,
|
||||||
mask,
|
mask,
|
||||||
};
|
};
|
||||||
let receiver = PULSE_RECEIVER_REGISTRY.add(receiver);
|
let receiver = PULSE_RECEIVER_REGISTRY.add(receiver);
|
||||||
|
|
||||||
<PulseReceiver as PulseReceiverAspect>::add_node_members(node);
|
|
||||||
node.add_aspect_raw(receiver.clone());
|
|
||||||
for sender in PULSE_SENDER_REGISTRY.get_valid_contents() {
|
for sender in PULSE_SENDER_REGISTRY.get_valid_contents() {
|
||||||
sender.handle_new_receiver(&receiver);
|
sender.handle_new_receiver(&receiver);
|
||||||
}
|
}
|
||||||
|
let _ = node.pulse_receiver.set(receiver.clone());
|
||||||
Ok(receiver)
|
Ok(receiver)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl Aspect for PulseReceiver {
|
|
||||||
const NAME: &'static str = "PulseReceiver";
|
|
||||||
}
|
|
||||||
impl PulseReceiverAspect for PulseReceiver {
|
|
||||||
fn send_data(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
sender: Arc<Node>,
|
|
||||||
data: Datamap,
|
|
||||||
) -> Result<()> {
|
|
||||||
let this_receiver = node.get_aspect::<PulseReceiver>().unwrap();
|
|
||||||
|
|
||||||
ensure!(
|
pub fn send_data(&self, uid: &str, data: Vec<u8>) -> Result<()> {
|
||||||
mask_matches(&this_receiver.mask, &data),
|
if let Some(node) = self.node.upgrade() {
|
||||||
"Message ({data:?}) does not contain the same keys as the receiver's mask ({:?})",
|
node.send_remote_signal("data", serialize(SendDataInfo { uid, data })?)?;
|
||||||
this_receiver.mask
|
}
|
||||||
);
|
|
||||||
pulse_receiver_client::data(&node, &sender, &data)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for PulseReceiver {
|
impl Drop for PulseReceiver {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
PULSE_RECEIVER_REGISTRY.remove(self);
|
PULSE_RECEIVER_REGISTRY.remove(self);
|
||||||
@@ -199,54 +253,76 @@ impl Drop for PulseReceiver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
create_interface!(DataInterface);
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
struct DataInterface;
|
let node = Node::create(client, "", "data", false);
|
||||||
impl InterfaceAspect for DataInterface {
|
node.add_local_signal("create_pulse_sender", create_pulse_sender_flex);
|
||||||
fn create_pulse_sender(
|
node.add_local_signal("create_pulse_receiver", create_pulse_receiver_flex);
|
||||||
_node: Arc<Node>,
|
node.add_local_method("register_keymap", register_keymap_flex);
|
||||||
calling_client: Arc<Client>,
|
node.add_local_method("get_keymap", get_keymap_flex);
|
||||||
id: u64,
|
node.add_to_scenegraph().map(|_| ())
|
||||||
parent: Arc<Node>,
|
}
|
||||||
|
|
||||||
|
pub fn create_pulse_sender_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreatePulseSenderInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
transform: Transform,
|
transform: Transform,
|
||||||
mask: Datamap,
|
mask: Vec<u8>,
|
||||||
) -> Result<()> {
|
|
||||||
get_mask(&mask)?;
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, false);
|
|
||||||
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
PulseSender::add_to(&node, mask)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
let info: CreatePulseSenderInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/data/sender", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
|
||||||
fn create_pulse_receiver(
|
let mask = Mask(info.mask);
|
||||||
_node: Arc<Node>,
|
mask.get_mask()?;
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
let node = node.add_to_scenegraph()?;
|
||||||
parent: Arc<Node>,
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
PulseSender::add_to(&node, mask)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_pulse_receiver_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreatePulseReceiverInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
transform: Transform,
|
transform: Transform,
|
||||||
field: Arc<Node>,
|
field_path: &'a str,
|
||||||
mask: Datamap,
|
mask: Vec<u8>,
|
||||||
) -> Result<()> {
|
|
||||||
get_mask(&mask)?;
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = parse_transform(transform, true, true, false);
|
|
||||||
let field = field.get_aspect::<Field>()?;
|
|
||||||
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
PulseReceiver::add_to(&node, field, mask)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
let info: CreatePulseReceiverInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/data/receiver", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let field = find_field(&calling_client, info.field_path)?;
|
||||||
|
let mask = Mask(info.mask);
|
||||||
|
mask.get_mask()?;
|
||||||
|
|
||||||
async fn register_keymap(
|
let node = node.add_to_scenegraph()?;
|
||||||
_node: Arc<Node>,
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
_calling_client: Arc<Client>,
|
PulseReceiver::add_to(&node, field, mask)?;
|
||||||
keymap: String,
|
Ok(())
|
||||||
) -> Result<u64> {
|
}
|
||||||
|
|
||||||
|
pub fn register_keymap_flex(
|
||||||
|
_node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let keymap: String = deserialize(message.as_ref())?;
|
||||||
let mut keymaps = KEYMAPS.lock();
|
let mut keymaps = KEYMAPS.lock();
|
||||||
if let Some(found_keymap_id) = keymaps
|
if let Some(found_keymap_id) = keymaps
|
||||||
.iter()
|
.iter()
|
||||||
@@ -254,23 +330,26 @@ impl InterfaceAspect for DataInterface {
|
|||||||
.map(|(k, _v)| k)
|
.map(|(k, _v)| k)
|
||||||
.last()
|
.last()
|
||||||
{
|
{
|
||||||
return Ok(found_keymap_id.data().as_ffi());
|
return Ok(serialize(found_keymap_id)?.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let key = keymaps.insert(keymap);
|
let generated_id = nanoid!();
|
||||||
Ok(key.data().as_ffi())
|
keymaps.insert(generated_id.clone(), keymap);
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_keymap(
|
Ok(serialize(generated_id)?.into())
|
||||||
_node: Arc<Node>,
|
});
|
||||||
_calling_client: Arc<Client>,
|
}
|
||||||
keymap_id: u64,
|
pub fn get_keymap_flex(
|
||||||
) -> Result<String> {
|
_node: &Node,
|
||||||
let keymaps = KEYMAPS.lock();
|
_calling_client: Arc<Client>,
|
||||||
let Some(keymap) = keymaps.get(KeyData::from_ffi(keymap_id).into()) else {
|
message: Message,
|
||||||
bail!("Could not find keymap. Try registering it")
|
response: MethodResponseSender,
|
||||||
};
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
Ok(keymap.clone())
|
let keymap_id: &str = deserialize(message.as_ref())?;
|
||||||
}
|
let keymaps = KEYMAPS.lock();
|
||||||
|
let Some(keymap) = keymaps.get(keymap_id) else {bail!("Could not find keymap. Try registering it")};
|
||||||
|
|
||||||
|
Ok(serialize(keymap)?.into())
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -1,95 +1,126 @@
|
|||||||
use super::{Line, LinesAspect};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{client::Client, registry::Registry},
|
core::{client::Client, registry::Registry},
|
||||||
nodes::{spatial::Spatial, Aspect, Node},
|
nodes::{
|
||||||
|
spatial::{find_spatial_parent, parse_transform, Spatial},
|
||||||
|
Message, Node,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::{bail, ensure, Result};
|
||||||
use glam::Vec3;
|
use glam::Vec3A;
|
||||||
|
use mint::Vector3;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use prisma::Lerp;
|
use portable_atomic::{AtomicBool, Ordering};
|
||||||
|
use prisma::{Flatten, Lerp, Rgba};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::{schemas::flex::deserialize, values::Transform};
|
||||||
use std::{collections::VecDeque, sync::Arc};
|
use std::{collections::VecDeque, sync::Arc};
|
||||||
use stereokit_rust::{
|
use stereokit::{bounds_grow_to_fit_pt, Bounds, Color128, LinePoint as SkLinePoint, StereoKitDraw};
|
||||||
maths::Bounds, sk::MainThreadToken, system::LinePoint as SkLinePoint, util::Color128,
|
|
||||||
};
|
use super::Drawable;
|
||||||
|
|
||||||
static LINES_REGISTRY: Registry<Lines> = Registry::new();
|
static LINES_REGISTRY: Registry<Lines> = Registry::new();
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct LinePointRaw {
|
||||||
|
point: Vector3<f32>,
|
||||||
|
thickness: f32,
|
||||||
|
color: [f32; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct LineData {
|
||||||
|
points: Vec<LinePointRaw>,
|
||||||
|
cyclic: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Lines {
|
pub struct Lines {
|
||||||
|
enabled: Arc<AtomicBool>,
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
data: Mutex<Vec<Line>>,
|
data: Mutex<LineData>,
|
||||||
}
|
}
|
||||||
impl Lines {
|
impl Lines {
|
||||||
pub fn add_to(node: &Arc<Node>, lines: Vec<Line>) -> Result<Arc<Lines>> {
|
fn add_to(node: &Arc<Node>, points: Vec<LinePointRaw>, cyclic: bool) -> Result<Arc<Lines>> {
|
||||||
let _ = node
|
ensure!(
|
||||||
.get_aspect::<Spatial>()
|
node.drawable.get().is_none(),
|
||||||
.unwrap()
|
"Internal: Node already has a drawable attached!"
|
||||||
.bounding_box_calc
|
);
|
||||||
.set(|node| {
|
|
||||||
let mut bounds = Bounds::default();
|
let _ = node.spatial.get().unwrap().bounding_box_calc.set(|node| {
|
||||||
if let Ok(lines) = node.get_aspect::<Lines>() {
|
let mut bounds = Bounds::default();
|
||||||
for line in &*lines.data.lock() {
|
let Some(Drawable::Lines(lines)) = node.drawable.get() else {return bounds};
|
||||||
for point in &line.points {
|
for point in &lines.data.lock().points {
|
||||||
bounds.grown_point(Vec3::from(point.point));
|
bounds = bounds_grow_to_fit_pt(bounds, point.point);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
bounds
|
||||||
bounds
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let lines = LINES_REGISTRY.add(Lines {
|
let lines = LINES_REGISTRY.add(Lines {
|
||||||
space: node.get_aspect::<Spatial>()?.clone(),
|
enabled: node.enabled.clone(),
|
||||||
data: Mutex::new(lines),
|
space: node.get_aspect("Lines", "spatial", |n| &n.spatial)?.clone(),
|
||||||
|
data: Mutex::new(LineData { points, cyclic }),
|
||||||
});
|
});
|
||||||
<Lines as LinesAspect>::add_node_members(node);
|
node.add_local_signal("set_points", Lines::set_points_flex);
|
||||||
node.add_aspect_raw(lines.clone());
|
node.add_local_signal("set_cyclic", Lines::set_cyclic_flex);
|
||||||
|
let _ = node.drawable.set(Drawable::Lines(lines.clone()));
|
||||||
|
|
||||||
Ok(lines)
|
Ok(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self, token: &MainThreadToken) {
|
fn draw(&self, draw_ctx: &impl StereoKitDraw) {
|
||||||
let transform_mat = self.space.global_transform();
|
let transform_mat = self.space.global_transform();
|
||||||
let data = self.data.lock().clone();
|
let data = self.data.lock().clone();
|
||||||
for line in &data {
|
let mut points: VecDeque<SkLinePoint> = data
|
||||||
let mut points: VecDeque<SkLinePoint> = line
|
.points
|
||||||
.points
|
.iter()
|
||||||
.iter()
|
.map(|p| SkLinePoint {
|
||||||
.map(|p| SkLinePoint {
|
pt: transform_mat.transform_point3a(Vec3A::from(p.point)).into(),
|
||||||
pt: transform_mat.transform_point3(Vec3::from(p.point)).into(),
|
thickness: p.thickness,
|
||||||
thickness: p.thickness,
|
color: p.color.map(|c| (c * 255.0) as u8).into(),
|
||||||
color: Color128::new(p.color.c.r, p.color.c.g, p.color.c.b, p.color.a).into(),
|
})
|
||||||
})
|
.collect();
|
||||||
.collect();
|
if data.cyclic && !points.is_empty() {
|
||||||
if line.cyclic && !points.is_empty() {
|
let first = data.points.first().unwrap();
|
||||||
let first = line.points.first().unwrap();
|
let last = data.points.last().unwrap();
|
||||||
let last = line.points.last().unwrap();
|
let color = Rgba::from_slice(&first.color).lerp(&Rgba::from_slice(&last.color), 0.5);
|
||||||
|
let connect_point = SkLinePoint {
|
||||||
let color = Color128 {
|
pt: transform_mat
|
||||||
r: first.color.c.r.lerp(&last.color.c.r, 0.5),
|
.transform_point3a(Vec3A::from(first.point).lerp(Vec3A::from(last.point), 0.5))
|
||||||
g: first.color.c.g.lerp(&last.color.c.g, 0.5),
|
.into(),
|
||||||
b: first.color.c.b.lerp(&last.color.c.b, 0.5),
|
thickness: (first.thickness + last.thickness) * 0.5,
|
||||||
a: first.color.a.lerp(&last.color.a, 0.5),
|
color: Color128::from([color.red(), color.green(), color.blue(), color.alpha()])
|
||||||
};
|
.into(),
|
||||||
let connect_point = SkLinePoint {
|
};
|
||||||
pt: transform_mat
|
points.push_front(connect_point.clone());
|
||||||
.transform_point3(Vec3::from(first.point).lerp(Vec3::from(last.point), 0.5))
|
points.push_back(connect_point);
|
||||||
.into(),
|
|
||||||
thickness: (first.thickness + last.thickness) * 0.5,
|
|
||||||
color: color.into(),
|
|
||||||
};
|
|
||||||
points.push_front(connect_point);
|
|
||||||
points.push_back(connect_point);
|
|
||||||
}
|
|
||||||
stereokit_rust::system::Lines::add_list(token, points.make_contiguous());
|
|
||||||
}
|
}
|
||||||
|
draw_ctx.line_add_listv(points.make_contiguous());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl Aspect for Lines {
|
pub fn set_points_flex(
|
||||||
const NAME: &'static str = "Lines";
|
node: &Node,
|
||||||
}
|
_calling_client: Arc<Client>,
|
||||||
impl LinesAspect for Lines {
|
message: Message,
|
||||||
fn set_lines(node: Arc<Node>, _calling_client: Arc<Client>, lines: Vec<Line>) -> Result<()> {
|
) -> Result<()> {
|
||||||
let lines_aspect = node.get_aspect::<Lines>()?;
|
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
|
||||||
*lines_aspect.data.lock() = lines;
|
|
||||||
|
let mut points: Vec<LinePointRaw> = deserialize(message.as_ref())?;
|
||||||
|
for p in &mut points {
|
||||||
|
p.color[0] = p.color[0].powf(2.2);
|
||||||
|
p.color[1] = p.color[1].powf(2.2);
|
||||||
|
p.color[2] = p.color[2].powf(2.2);
|
||||||
|
}
|
||||||
|
lines.data.lock().points = points;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn set_cyclic_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
|
||||||
|
|
||||||
|
lines.data.lock().cyclic = deserialize(message.as_ref())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,12 +130,36 @@ impl Drop for Lines {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_all(token: &MainThreadToken) {
|
pub fn draw_all(draw_ctx: &impl StereoKitDraw) {
|
||||||
for lines in LINES_REGISTRY.get_valid_contents() {
|
for lines in LINES_REGISTRY.get_valid_contents() {
|
||||||
if let Some(node) = lines.space.node() {
|
if lines.enabled.load(Ordering::Relaxed) {
|
||||||
if node.enabled() {
|
lines.draw(draw_ctx);
|
||||||
lines.draw(token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateTextInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
points: Vec<LinePointRaw>,
|
||||||
|
cyclic: bool,
|
||||||
|
}
|
||||||
|
let mut info: CreateTextInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/drawable/lines", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
|
||||||
|
for p in &mut info.points {
|
||||||
|
p.color[0] = p.color[0].powf(2.2);
|
||||||
|
p.color[1] = p.color[1].powf(2.2);
|
||||||
|
p.color[2] = p.color[2].powf(2.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
Lines::add_to(&node, info.points, info.cyclic)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,40 +1,54 @@
|
|||||||
pub mod lines;
|
pub mod lines;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
#[cfg(feature = "wayland")]
|
|
||||||
pub mod shader_manipulation;
|
|
||||||
pub mod shaders;
|
pub mod shaders;
|
||||||
pub mod text;
|
pub mod text;
|
||||||
|
|
||||||
use self::{lines::Lines, model::Model, text::Text};
|
use self::{
|
||||||
use super::{
|
lines::Lines,
|
||||||
spatial::{Spatial, Transform},
|
model::{Model, ModelPart},
|
||||||
Node,
|
text::Text,
|
||||||
};
|
};
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::{
|
|
||||||
core::{client::Client, resource::get_resource_file},
|
|
||||||
create_interface,
|
|
||||||
};
|
|
||||||
use color_eyre::eyre::{self, Result};
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use stardust_xr::values::ResourceID;
|
|
||||||
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
|
|
||||||
use stereokit_rust::{sk::MainThreadToken, system::Renderer, tex::SHCubemap};
|
|
||||||
|
|
||||||
// #[instrument(level = "debug", skip(sk))]
|
use super::{Message, Node};
|
||||||
pub fn draw(token: &MainThreadToken) {
|
use crate::core::client::Client;
|
||||||
lines::draw_all(token);
|
use color_eyre::eyre::Result;
|
||||||
model::draw_all(token);
|
use parking_lot::Mutex;
|
||||||
text::draw_all(token);
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
use stereokit::StereoKitDraw;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "drawable", false);
|
||||||
|
node.add_local_signal("create_lines", lines::create_flex);
|
||||||
|
node.add_local_signal("create_model", model::create_flex);
|
||||||
|
node.add_local_signal("create_text", text::create_flex);
|
||||||
|
node.add_local_signal("set_sky_file", set_sky_file_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Drawable {
|
||||||
|
Lines(Arc<Lines>),
|
||||||
|
Model(Arc<Model>),
|
||||||
|
ModelPart(Arc<ModelPart>),
|
||||||
|
Text(Arc<Text>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(sk))]
|
||||||
|
pub fn draw(sk: &impl StereoKitDraw) {
|
||||||
|
lines::draw_all(sk);
|
||||||
|
model::draw_all(sk);
|
||||||
|
text::draw_all(sk);
|
||||||
|
|
||||||
if let Some(skytex) = QUEUED_SKYTEX.lock().take() {
|
if let Some(skytex) = QUEUED_SKYTEX.lock().take() {
|
||||||
if let Ok(skytex) = SHCubemap::from_cubemap_equirectangular(skytex, true, 100) {
|
if let Ok((_skylight, skytex)) = sk.tex_create_cubemap_file(&skytex, true, i32::MAX) {
|
||||||
Renderer::skytex(skytex.tex);
|
sk.render_set_skytex(&skytex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(skylight) = QUEUED_SKYLIGHT.lock().take() {
|
if let Some(skylight) = QUEUED_SKYLIGHT.lock().take() {
|
||||||
if let Ok(skylight) = SHCubemap::from_cubemap_equirectangular(skylight, true, 100) {
|
if let Ok((skylight, _)) = sk.tex_create_cubemap_file(&skylight, true, i32::MAX) {
|
||||||
Renderer::skylight(skylight.sh);
|
sk.render_set_skylight(skylight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,80 +56,21 @@ pub fn draw(token: &MainThreadToken) {
|
|||||||
static QUEUED_SKYLIGHT: Mutex<Option<PathBuf>> = Mutex::new(None);
|
static QUEUED_SKYLIGHT: Mutex<Option<PathBuf>> = Mutex::new(None);
|
||||||
static QUEUED_SKYTEX: Mutex<Option<PathBuf>> = Mutex::new(None);
|
static QUEUED_SKYTEX: Mutex<Option<PathBuf>> = Mutex::new(None);
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_drawable_protocol!();
|
fn set_sky_file_flex(_node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
create_interface!(DrawableInterface);
|
#[derive(Deserialize)]
|
||||||
|
struct SkyFileInfo {
|
||||||
pub struct DrawableInterface;
|
path: PathBuf,
|
||||||
impl InterfaceAspect for DrawableInterface {
|
skytex: Option<bool>,
|
||||||
fn set_sky_tex(_node: Arc<Node>, calling_client: Arc<Client>, tex: ResourceID) -> Result<()> {
|
skylight: Option<bool>,
|
||||||
let resource_path = get_resource_file(&tex, &calling_client, &[OsStr::new("hdr")])
|
}
|
||||||
.ok_or(eyre::eyre!("Could not find resource"))?;
|
let info: SkyFileInfo = deserialize(message.as_ref())?;
|
||||||
QUEUED_SKYTEX.lock().replace(resource_path);
|
info.path.metadata()?;
|
||||||
Ok(())
|
if info.skytex.unwrap_or_default() {
|
||||||
|
QUEUED_SKYTEX.lock().replace(info.path.clone());
|
||||||
|
}
|
||||||
|
if info.skylight.unwrap_or_default() {
|
||||||
|
QUEUED_SKYLIGHT.lock().replace(info.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_sky_light(
|
Ok(())
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
light: ResourceID,
|
|
||||||
) -> Result<()> {
|
|
||||||
let resource_path = get_resource_file(&light, &calling_client, &[OsStr::new("hdr")])
|
|
||||||
.ok_or(eyre::eyre!("Could not find resource"))?;
|
|
||||||
QUEUED_SKYLIGHT.lock().replace(resource_path);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_lines(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
lines: Vec<Line>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, true);
|
|
||||||
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Lines::add_to(&node, lines)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_model(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
model: ResourceID,
|
|
||||||
) -> Result<()> {
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, true);
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Model::add_to(&node, model)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_text(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
text: String,
|
|
||||||
style: TextStyle,
|
|
||||||
) -> Result<()> {
|
|
||||||
let node = Node::from_id(&calling_client, id, true);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, true);
|
|
||||||
|
|
||||||
let node = node.add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Text::add_to(&node, text, style)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,124 +1,114 @@
|
|||||||
use super::{MaterialParameter, ModelAspect, ModelPartAspect, MODEL_PART_ASPECT_ALIAS_INFO};
|
use super::Node;
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use crate::core::node_collections::LifeLinkedNodeMap;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::core::resource::get_resource_file;
|
use crate::core::resource::ResourceID;
|
||||||
use crate::nodes::alias::{Alias, AliasList};
|
use crate::nodes::drawable::Drawable;
|
||||||
use crate::nodes::spatial::Spatial;
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
use crate::nodes::{Aspect, Node};
|
use crate::nodes::Message;
|
||||||
use color_eyre::eyre::{bail, eyre, Result};
|
use crate::SK_MULTITHREAD;
|
||||||
use glam::{Mat4, Vec2, Vec3};
|
use color_eyre::eyre::{bail, ensure, eyre, Result};
|
||||||
use once_cell::sync::{Lazy, OnceCell};
|
use glam::Mat4;
|
||||||
|
use mint::{ColumnMatrix4, Vector2, Vector3, Vector4};
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use portable_atomic::{AtomicBool, Ordering};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use stardust_xr::values::ResourceID;
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use stereokit_rust::material::Transparency;
|
use stereokit::named_colors::WHITE;
|
||||||
use stereokit_rust::maths::Bounds;
|
use stereokit::{
|
||||||
use stereokit_rust::sk::MainThreadToken;
|
Bounds, Color128, Material, Model as SKModel, RenderLayer, Shader, StereoKitDraw,
|
||||||
use stereokit_rust::{material::Material, model::Model as SKModel, tex::Tex, util::Color128};
|
StereoKitMultiThread,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct MaterialWrapper(pub Material);
|
|
||||||
impl Hash for MaterialWrapper {
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
self.0.get_shader().0.as_ptr().hash(state);
|
|
||||||
for param in self.0.get_all_param_info() {
|
|
||||||
param.to_string().hash(state)
|
|
||||||
}
|
|
||||||
self.0.get_chain().map(MaterialWrapper).hash(state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl PartialEq for MaterialWrapper {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
if self.0.get_shader().0.as_ptr() != other.0.get_shader().0.as_ptr() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if self.0.get_all_param_info().count() != other.0.get_all_param_info().count() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for self_param in self.0.get_all_param_info() {
|
|
||||||
let Some(other_param) = other
|
|
||||||
.0
|
|
||||||
.get_all_param_info()
|
|
||||||
.get_data(self_param.get_name(), self_param.get_type())
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
if self_param.to_string() != other_param.to_string() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.0.get_chain().map(MaterialWrapper) == other.0.get_chain().map(MaterialWrapper)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Eq for MaterialWrapper {}
|
|
||||||
unsafe impl Send for MaterialWrapper {}
|
|
||||||
unsafe impl Sync for MaterialWrapper {}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct MaterialRegistry(Mutex<FxHashMap<u64, String>>);
|
|
||||||
impl MaterialRegistry {
|
|
||||||
fn add_or_get(&self, material: Arc<MaterialWrapper>) -> Arc<MaterialWrapper> {
|
|
||||||
let mut lock = self.0.lock();
|
|
||||||
let hash = {
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
|
||||||
material.hash(&mut hasher);
|
|
||||||
hasher.finish()
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(id) = lock.get(&hash) {
|
|
||||||
if let Ok(existing) = Material::find(id) {
|
|
||||||
return Arc::new(MaterialWrapper(existing));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.insert(hash, material.0.get_id().to_string());
|
|
||||||
material
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static MATERIAL_REGISTRY: Lazy<MaterialRegistry> = Lazy::new(MaterialRegistry::default);
|
|
||||||
static MODEL_REGISTRY: Registry<Model> = Registry::new();
|
static MODEL_REGISTRY: Registry<Model> = Registry::new();
|
||||||
static HOLDOUT_MATERIAL: OnceCell<Arc<MaterialWrapper>> = OnceCell::new();
|
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(tag = "t", content = "c")]
|
||||||
|
pub enum MaterialParameter {
|
||||||
|
Float(f32),
|
||||||
|
Vector2(Vector2<f32>),
|
||||||
|
Vector3(Vector3<f32>),
|
||||||
|
Vector4(Vector4<f32>),
|
||||||
|
Color([f32; 4]),
|
||||||
|
Int(i32),
|
||||||
|
Int2(Vector2<i32>),
|
||||||
|
Int3(Vector3<i32>),
|
||||||
|
Int4(Vector4<i32>),
|
||||||
|
Bool(bool),
|
||||||
|
UInt(u32),
|
||||||
|
UInt2(Vector2<u32>),
|
||||||
|
UInt3(Vector3<u32>),
|
||||||
|
UInt4(Vector4<u32>),
|
||||||
|
Matrix(ColumnMatrix4<f32>),
|
||||||
|
Texture(ResourceID),
|
||||||
|
}
|
||||||
impl MaterialParameter {
|
impl MaterialParameter {
|
||||||
fn apply_to_material(&self, client: &Client, material: &Material, parameter_name: &str) {
|
fn apply_to_material(
|
||||||
let mut params = material.get_all_param_info();
|
&self,
|
||||||
|
client: &Client,
|
||||||
|
sk: &impl StereoKitMultiThread,
|
||||||
|
material: &Material,
|
||||||
|
parameter_name: &str,
|
||||||
|
) {
|
||||||
match self {
|
match self {
|
||||||
MaterialParameter::Bool(val) => {
|
|
||||||
params.set_bool(parameter_name, *val);
|
|
||||||
}
|
|
||||||
MaterialParameter::Int(val) => {
|
|
||||||
params.set_int(parameter_name, &[*val]);
|
|
||||||
}
|
|
||||||
MaterialParameter::UInt(val) => {
|
|
||||||
params.set_uint(parameter_name, &[*val]);
|
|
||||||
}
|
|
||||||
MaterialParameter::Float(val) => {
|
MaterialParameter::Float(val) => {
|
||||||
params.set_float(parameter_name, *val);
|
sk.material_set_float(material, parameter_name, *val);
|
||||||
}
|
}
|
||||||
MaterialParameter::Vec2(val) => {
|
MaterialParameter::Vector2(val) => {
|
||||||
params.set_vec2(parameter_name, Vec2::from(*val));
|
sk.material_set_vector2(material, parameter_name, *val);
|
||||||
}
|
}
|
||||||
MaterialParameter::Vec3(val) => {
|
MaterialParameter::Vector3(val) => {
|
||||||
params.set_vec3(parameter_name, Vec3::from(*val));
|
sk.material_set_vector3(material, parameter_name, *val);
|
||||||
|
}
|
||||||
|
MaterialParameter::Vector4(val) => {
|
||||||
|
sk.material_set_vector4(material, parameter_name, *val);
|
||||||
}
|
}
|
||||||
MaterialParameter::Color(val) => {
|
MaterialParameter::Color(val) => {
|
||||||
params.set_color(
|
sk.material_set_color(material, parameter_name, Color128::from(val.clone()));
|
||||||
parameter_name,
|
}
|
||||||
Color128::new(val.c.r, val.c.g, val.c.b, val.a),
|
MaterialParameter::Int(val) => {
|
||||||
);
|
sk.material_set_int(material, parameter_name, *val);
|
||||||
|
}
|
||||||
|
MaterialParameter::Int2(val) => {
|
||||||
|
sk.material_set_int2(material, parameter_name, val.x, val.y);
|
||||||
|
}
|
||||||
|
MaterialParameter::Int3(val) => {
|
||||||
|
sk.material_set_int3(material, parameter_name, val.x, val.y, val.z);
|
||||||
|
}
|
||||||
|
MaterialParameter::Int4(val) => {
|
||||||
|
sk.material_set_int4(material, parameter_name, val.w, val.x, val.y, val.z);
|
||||||
|
}
|
||||||
|
MaterialParameter::Bool(val) => {
|
||||||
|
sk.material_set_bool(material, parameter_name, *val);
|
||||||
|
}
|
||||||
|
MaterialParameter::UInt(val) => {
|
||||||
|
sk.material_set_uint(material, parameter_name, *val);
|
||||||
|
}
|
||||||
|
MaterialParameter::UInt2(val) => {
|
||||||
|
sk.material_set_uint2(material, parameter_name, val.x, val.y);
|
||||||
|
}
|
||||||
|
MaterialParameter::UInt3(val) => {
|
||||||
|
sk.material_set_uint3(material, parameter_name, val.x, val.y, val.z);
|
||||||
|
}
|
||||||
|
MaterialParameter::UInt4(val) => {
|
||||||
|
sk.material_set_uint4(material, parameter_name, val.w, val.x, val.y, val.z);
|
||||||
|
}
|
||||||
|
MaterialParameter::Matrix(val) => {
|
||||||
|
sk.material_set_matrix(material, parameter_name, Mat4::from(*val));
|
||||||
}
|
}
|
||||||
MaterialParameter::Texture(resource) => {
|
MaterialParameter::Texture(resource) => {
|
||||||
let Some(texture_path) =
|
let Some(texture_path) = resource.get_file(
|
||||||
get_resource_file(resource, client, &[OsStr::new("png"), OsStr::new("jpg")])
|
&client.base_resource_prefixes.lock().clone(),
|
||||||
else {
|
&[OsStr::new("png"), OsStr::new("jpg")],
|
||||||
return;
|
) else {return};
|
||||||
};
|
if let Ok(tex) = sk.tex_create_file(texture_path, true, 0) {
|
||||||
if let Ok(tex) = Tex::from_file(texture_path, true, None) {
|
sk.material_set_texture(material, parameter_name, &tex);
|
||||||
params.set_texture(parameter_name, &tex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,275 +117,245 @@ impl MaterialParameter {
|
|||||||
|
|
||||||
pub struct ModelPart {
|
pub struct ModelPart {
|
||||||
id: i32,
|
id: i32,
|
||||||
path: String,
|
path: PathBuf,
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
model: Weak<Model>,
|
model: Weak<Model>,
|
||||||
pending_material_parameters: Mutex<FxHashMap<String, MaterialParameter>>,
|
pending_material_parameters: Mutex<FxHashMap<String, MaterialParameter>>,
|
||||||
pending_material_replacement: Mutex<Option<Arc<MaterialWrapper>>>,
|
pending_material_replacement: Mutex<Option<Arc<Material>>>,
|
||||||
aliases: AliasList,
|
|
||||||
}
|
}
|
||||||
impl ModelPart {
|
impl ModelPart {
|
||||||
fn create_for_model(model: &Arc<Model>, sk_model: &SKModel) {
|
fn create_for_model(sk: &impl StereoKitMultiThread, model: &Arc<Model>, sk_model: &SKModel) {
|
||||||
HOLDOUT_MATERIAL.get_or_init(|| {
|
let first_root_part = sk.model_node_get_root(sk_model);
|
||||||
let mut mat = Material::copy(&Material::unlit());
|
let mut current_option_part = Some(first_root_part);
|
||||||
mat.transparency(Transparency::None);
|
|
||||||
mat.color_tint(Color128::BLACK_TRANSPARENT);
|
|
||||||
Arc::new(MaterialWrapper(mat))
|
|
||||||
});
|
|
||||||
|
|
||||||
let nodes = sk_model.get_nodes();
|
while let Some(current_part) = &mut current_option_part {
|
||||||
for part in nodes.all() {
|
ModelPart::create(sk, model, sk_model, *current_part);
|
||||||
ModelPart::create(model, &part);
|
|
||||||
|
if let Some(child) = sk.model_node_child(sk_model, *current_part) {
|
||||||
|
*current_part = child;
|
||||||
|
} else if let Some(sibling) = sk.model_node_sibling(sk_model, *current_part) {
|
||||||
|
*current_part = sibling;
|
||||||
|
} else {
|
||||||
|
while let Some(current_part) = &mut current_option_part {
|
||||||
|
if let Some(sibling) = sk.model_node_sibling(sk_model, *current_part) {
|
||||||
|
*current_part = sibling;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_option_part = sk.model_node_parent(sk_model, *current_part);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(model: &Arc<Model>, part: &stereokit_rust::model::ModelNode) -> Option<Arc<Self>> {
|
fn create(
|
||||||
let mut parts = model.parts.lock();
|
sk: &impl StereoKitMultiThread,
|
||||||
let parent_part = part
|
model: &Arc<Model>,
|
||||||
.get_parent()
|
sk_model: &SKModel,
|
||||||
.and_then(|part| parts.iter().find(|p| p.id == *part.get_id()));
|
id: i32,
|
||||||
|
) -> Option<Arc<Self>> {
|
||||||
|
let parent_node = sk
|
||||||
|
.model_node_parent(sk_model, id)
|
||||||
|
.and_then(|id| model.parts.get(&id));
|
||||||
|
let parent_part = parent_node
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|node| match node.drawable.get() {
|
||||||
|
Some(Drawable::ModelPart(model_part)) => Some(model_part),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
let stardust_model_part = model.space.node()?;
|
let stardust_model_part = model.space.node()?;
|
||||||
let client = stardust_model_part.get_client()?;
|
let client = stardust_model_part.get_client()?;
|
||||||
let mut part_path = parent_part
|
let mut part_path = parent_part.map(|n| n.path.clone()).unwrap_or_default();
|
||||||
.map(|n| n.path.clone() + "/")
|
part_path.push(sk.model_node_get_name(sk_model, id)?);
|
||||||
.unwrap_or_default();
|
let node = client.scenegraph.add_node(Node::create(
|
||||||
part_path += part.get_name().unwrap();
|
&client,
|
||||||
|
stardust_model_part.get_path(),
|
||||||
let node = client.scenegraph.add_node(Node::generate(&client, false));
|
part_path.to_str()?,
|
||||||
let spatial_parent = parent_part
|
false,
|
||||||
.map(|n| n.space.clone())
|
));
|
||||||
|
let spatial_parent = parent_node
|
||||||
|
.and_then(|n| n.spatial.get().cloned())
|
||||||
.unwrap_or_else(|| model.space.clone());
|
.unwrap_or_else(|| model.space.clone());
|
||||||
|
|
||||||
let local_transform = unsafe { part.get_local_transform().m };
|
|
||||||
let space = Spatial::add_to(
|
let space = Spatial::add_to(
|
||||||
&node,
|
&node,
|
||||||
Some(spatial_parent),
|
Some(spatial_parent),
|
||||||
Mat4::from_cols_array(&local_transform),
|
sk.model_node_get_transform_local(sk_model, id),
|
||||||
false,
|
false,
|
||||||
);
|
)
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
let _ = space.bounding_box_calc.set(|node| {
|
let _ = node.spatial.get().unwrap().bounding_box_calc.set(|node| {
|
||||||
let Ok(model_part) = node.get_aspect::<ModelPart>() else {
|
let Some(Drawable::ModelPart(model_part)) = node.drawable.get() else {return Bounds::default()};
|
||||||
return Bounds::default();
|
let Some(sk) = SK_MULTITHREAD.get() else {return Bounds::default()};
|
||||||
};
|
let Some(model) = model_part.model.upgrade() else {return Bounds::default()};
|
||||||
let Some(model) = model_part.model.upgrade() else {
|
let Some(sk_model) = model.sk_model.get() else {return Bounds::default()};
|
||||||
return Bounds::default();
|
let Some(sk_mesh) = sk.model_node_get_mesh(sk_model, model_part.id) else {return Bounds::default()};
|
||||||
};
|
sk.mesh_get_bounds(sk_mesh)
|
||||||
let Some(sk_model) = model.sk_model.get() else {
|
|
||||||
return Bounds::default();
|
|
||||||
};
|
|
||||||
let model_nodes = sk_model.get_nodes();
|
|
||||||
let Some(model_node) = model_nodes.get_index(model_part.id) else {
|
|
||||||
return Bounds::default();
|
|
||||||
};
|
|
||||||
let Some(sk_mesh) = model_node.get_mesh() else {
|
|
||||||
return Bounds::default();
|
|
||||||
};
|
|
||||||
sk_mesh.get_bounds()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let model_part = Arc::new(ModelPart {
|
let model_part = Arc::new(ModelPart {
|
||||||
id: *part.get_id(),
|
id,
|
||||||
path: part_path,
|
path: part_path,
|
||||||
space,
|
space,
|
||||||
model: Arc::downgrade(model),
|
model: Arc::downgrade(model),
|
||||||
pending_material_parameters: Mutex::new(FxHashMap::default()),
|
pending_material_parameters: Mutex::new(FxHashMap::default()),
|
||||||
pending_material_replacement: Mutex::new(None),
|
pending_material_replacement: Mutex::new(None),
|
||||||
aliases: AliasList::default(),
|
|
||||||
});
|
});
|
||||||
<ModelPart as ModelPartAspect>::add_node_members(&node);
|
node.add_local_signal(
|
||||||
node.add_aspect_raw(model_part.clone());
|
"set_material_parameter",
|
||||||
parts.push(model_part.clone());
|
ModelPart::set_material_parameter_flex,
|
||||||
|
);
|
||||||
|
let _ = node.drawable.set(Drawable::ModelPart(model_part.clone()));
|
||||||
|
model.parts.add(id, &node);
|
||||||
Some(model_part)
|
Some(model_part)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace_material(&self, replacement: Arc<MaterialWrapper>) {
|
fn set_material_parameter_flex(
|
||||||
let shared_material = MATERIAL_REGISTRY.add_or_get(replacement);
|
node: &Node,
|
||||||
self.pending_material_replacement
|
|
||||||
.lock()
|
|
||||||
.replace(shared_material);
|
|
||||||
}
|
|
||||||
/// only to be run on the main thread
|
|
||||||
pub fn replace_material_now(&self, replacement: &Material) {
|
|
||||||
let Some(model) = self.model.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(sk_model) = model.sk_model.get() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let nodes = sk_model.get_nodes();
|
|
||||||
let Some(mut part) = nodes.get_index(self.id) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let shared_material =
|
|
||||||
MATERIAL_REGISTRY.add_or_get(Arc::new(MaterialWrapper(replacement.copy())));
|
|
||||||
part.material(&shared_material.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&self) {
|
|
||||||
let Some(model) = self.model.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(sk_model) = model.sk_model.get() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(node) = model.space.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let nodes = sk_model.get_nodes();
|
|
||||||
let Some(mut part) = nodes.get_index(self.id) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
part.model_transform(Spatial::space_to_space_matrix(
|
|
||||||
Some(&self.space),
|
|
||||||
Some(&model.space),
|
|
||||||
));
|
|
||||||
|
|
||||||
let Some(client) = node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(material_replacement) = self.pending_material_replacement.lock().take() {
|
|
||||||
part.material(&material_replacement.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
'mat_params: {
|
|
||||||
let mut material_parameters = self.pending_material_parameters.lock();
|
|
||||||
if !material_parameters.is_empty() {
|
|
||||||
let Some(material) = part.get_material() else {
|
|
||||||
break 'mat_params;
|
|
||||||
};
|
|
||||||
let new_material = material.copy();
|
|
||||||
for (parameter_name, parameter_value) in material_parameters.drain() {
|
|
||||||
parameter_value.apply_to_material(&client, &new_material, ¶meter_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
let shared_material =
|
|
||||||
MATERIAL_REGISTRY.add_or_get(Arc::new(MaterialWrapper(new_material)));
|
|
||||||
part.material(&shared_material.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Aspect for ModelPart {
|
|
||||||
const NAME: &'static str = "ModelPart";
|
|
||||||
}
|
|
||||||
impl ModelPartAspect for ModelPart {
|
|
||||||
#[doc = "Set this model part's material to one that cuts a hole in the world. Often used for overlays/passthrough where you want to show the background through an object."]
|
|
||||||
fn apply_holdout_material(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
let model_part = node.get_aspect::<ModelPart>()?;
|
|
||||||
model_part.replace_material(HOLDOUT_MATERIAL.get().unwrap().clone());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Set the material parameter with `parameter_name` to `value`"]
|
|
||||||
fn set_material_parameter(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
parameter_name: String,
|
message: Message,
|
||||||
value: MaterialParameter,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let model_part = node.get_aspect::<ModelPart>()?;
|
let Some(Drawable::ModelPart(model_part)) = node.drawable.get() else {bail!("Not a drawable??")};
|
||||||
|
|
||||||
|
let (name, value): (String, MaterialParameter) = deserialize(message.as_ref())?;
|
||||||
|
|
||||||
model_part
|
model_part
|
||||||
.pending_material_parameters
|
.pending_material_parameters
|
||||||
.lock()
|
.lock()
|
||||||
.insert(parameter_name, value);
|
.insert(name, value);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn replace_material(&self, replacement: Arc<Material>) {
|
||||||
|
self.pending_material_replacement
|
||||||
|
.lock()
|
||||||
|
.replace(replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&self, sk: &impl StereoKitDraw) {
|
||||||
|
let Some(model) = self.model.upgrade() else {return};
|
||||||
|
let Some(sk_model) = model.sk_model.get() else {return};
|
||||||
|
let Some(node) = model.space.node() else {return};
|
||||||
|
let Some(client) = node.get_client() else {return};
|
||||||
|
if let Some(material_replacement) = self.pending_material_replacement.lock().take() {
|
||||||
|
sk.model_node_set_material(sk_model, self.id, material_replacement.as_ref().as_ref());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut material_parameters = self.pending_material_parameters.lock();
|
||||||
|
for (parameter_name, parameter_value) in material_parameters.drain() {
|
||||||
|
let Some(material) = sk.model_node_get_material(sk_model, self.id) else {continue};
|
||||||
|
let new_material = sk.material_copy(material);
|
||||||
|
parameter_value.apply_to_material(&client, sk, &new_material, parameter_name.as_str());
|
||||||
|
sk.model_node_set_material(sk_model, self.id, &new_material);
|
||||||
|
}
|
||||||
|
|
||||||
|
sk.model_node_set_transform_model(
|
||||||
|
sk_model,
|
||||||
|
self.id,
|
||||||
|
Spatial::space_to_space_matrix(Some(&self.space), Some(&model.space)),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
|
self_ref: Weak<Model>,
|
||||||
|
enabled: Arc<AtomicBool>,
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
_resource_id: ResourceID,
|
_resource_id: ResourceID,
|
||||||
sk_model: OnceCell<SKModel>,
|
sk_model: OnceCell<SKModel>,
|
||||||
parts: Mutex<Vec<Arc<ModelPart>>>,
|
parts: LifeLinkedNodeMap<i32>,
|
||||||
}
|
}
|
||||||
|
unsafe impl Send for Model {}
|
||||||
|
unsafe impl Sync for Model {}
|
||||||
|
|
||||||
impl Model {
|
impl Model {
|
||||||
pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Model>> {
|
pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Model>> {
|
||||||
let pending_model_path = get_resource_file(
|
ensure!(
|
||||||
&resource_id,
|
node.spatial.get().is_some(),
|
||||||
&*node.get_client().ok_or_else(|| eyre!("Client not found"))?,
|
"Internal: Node does not have a spatial attached!"
|
||||||
&[OsStr::new("glb"), OsStr::new("gltf")],
|
);
|
||||||
)
|
ensure!(
|
||||||
.ok_or_else(|| eyre!("Resource not found"))?;
|
node.drawable.get().is_none(),
|
||||||
|
"Internal: Node already has a drawable attached!"
|
||||||
|
);
|
||||||
|
|
||||||
let model = Arc::new(Model {
|
let pending_model_path = resource_id
|
||||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
.get_file(
|
||||||
|
&node
|
||||||
|
.get_client()
|
||||||
|
.ok_or_else(|| eyre!("Client not found"))?
|
||||||
|
.base_resource_prefixes
|
||||||
|
.lock()
|
||||||
|
.clone(),
|
||||||
|
&[OsStr::new("glb"), OsStr::new("gltf")],
|
||||||
|
)
|
||||||
|
.ok_or_else(|| eyre!("Resource not found"))?;
|
||||||
|
|
||||||
|
let model = Arc::new_cyclic(|self_ref| Model {
|
||||||
|
self_ref: self_ref.clone(),
|
||||||
|
enabled: node.enabled.clone(),
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
_resource_id: resource_id,
|
_resource_id: resource_id,
|
||||||
sk_model: OnceCell::new(),
|
sk_model: OnceCell::new(),
|
||||||
parts: Mutex::new(Vec::default()),
|
parts: LifeLinkedNodeMap::default(),
|
||||||
});
|
});
|
||||||
<Model as ModelAspect>::add_node_members(node);
|
|
||||||
MODEL_REGISTRY.add_raw(&model);
|
MODEL_REGISTRY.add_raw(&model);
|
||||||
|
|
||||||
// technically doing this in anything but the main thread isn't a good idea but dangit we need those model nodes ASAP
|
let sk = SK_MULTITHREAD.get().unwrap();
|
||||||
let sk_model = SKModel::copy(SKModel::from_file(
|
let sk_model = sk.model_copy(
|
||||||
pending_model_path.to_str().unwrap(),
|
sk.model_create_file(pending_model_path.to_str().unwrap(), None::<Shader>)?,
|
||||||
None,
|
);
|
||||||
)?);
|
ModelPart::create_for_model(sk, &model.self_ref.upgrade().unwrap(), &sk_model);
|
||||||
ModelPart::create_for_model(&model, &sk_model);
|
|
||||||
let _ = model.sk_model.set(sk_model);
|
let _ = model.sk_model.set(sk_model);
|
||||||
node.add_aspect_raw(model.clone());
|
let _ = node.drawable.set(Drawable::Model(model.clone()));
|
||||||
Ok(model)
|
Ok(model)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self, token: &MainThreadToken) {
|
fn draw(&self, sk: &impl StereoKitDraw) {
|
||||||
let Some(sk_model) = self.sk_model.get() else {
|
let Some(sk_model) = self.sk_model.get() else {return};
|
||||||
return;
|
for model_node_node in self.parts.nodes() {
|
||||||
};
|
let Some(Drawable::ModelPart(model_node)) = model_node_node.drawable.get() else {continue};
|
||||||
let parts = self.parts.lock();
|
model_node.update(sk);
|
||||||
for model_node in &*parts {
|
|
||||||
model_node.update();
|
|
||||||
}
|
}
|
||||||
drop(parts);
|
|
||||||
|
|
||||||
if let Some(node) = self.space.node() {
|
sk.model_draw(
|
||||||
if node.enabled() {
|
sk_model,
|
||||||
sk_model.draw(token, self.space.global_transform(), None, None);
|
self.space.global_transform(),
|
||||||
}
|
WHITE,
|
||||||
}
|
RenderLayer::LAYER0,
|
||||||
}
|
);
|
||||||
}
|
|
||||||
// TODO: proper hread safety in stereokit_rust (probably just bind stereokit directly)
|
|
||||||
unsafe impl Send for Model {}
|
|
||||||
unsafe impl Sync for Model {}
|
|
||||||
impl Aspect for Model {
|
|
||||||
const NAME: &'static str = "Model";
|
|
||||||
}
|
|
||||||
impl ModelAspect for Model {
|
|
||||||
#[doc = "Bind a model part to the node with the ID input."]
|
|
||||||
fn bind_model_part(
|
|
||||||
node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
part_path: String,
|
|
||||||
) -> color_eyre::eyre::Result<()> {
|
|
||||||
let model = node.get_aspect::<Model>()?;
|
|
||||||
let parts = model.parts.lock();
|
|
||||||
let Some(part) = parts.iter().find(|p| p.path == part_path) else {
|
|
||||||
let paths = parts.iter().map(|p| &p.path).collect::<Vec<_>>();
|
|
||||||
bail!("Couldn't find model part at path {part_path}, all available paths: {paths:?}",)
|
|
||||||
};
|
|
||||||
Alias::create_with_id(
|
|
||||||
&part.space.node().unwrap(),
|
|
||||||
&calling_client,
|
|
||||||
id,
|
|
||||||
MODEL_PART_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
Some(&part.aliases),
|
|
||||||
)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Model {
|
impl Drop for Model {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
MODEL_REGISTRY.remove(self);
|
MODEL_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_all(token: &MainThreadToken) {
|
pub fn draw_all(sk: &impl StereoKitDraw) {
|
||||||
for model in MODEL_REGISTRY.get_valid_contents() {
|
for model in MODEL_REGISTRY.get_valid_contents() {
|
||||||
model.draw(token);
|
if model.enabled.load(Ordering::Relaxed) {
|
||||||
|
model.draw(sk);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateModelInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
resource: ResourceID,
|
||||||
|
}
|
||||||
|
let info: CreateModelInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/drawable/model", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
Model::add_to(&node, info.resource)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
// Simula shader with fancy lanzcos sampling
|
|
||||||
pub const UNLIT_SHADER_BYTES: &[u8] = include_bytes!("assets/shaders/shader_unlit_gamma.hlsl.sks");
|
|
||||||
|
|
||||||
// Simula shader with fancy lanzcos sampling
|
|
||||||
pub const PANEL_SHADER_BYTES: &[u8] = include_bytes!("assets/shaders/shader_unlit_simula.hlsl.sks");
|
|
||||||
@@ -3,9 +3,16 @@ use smithay::backend::renderer::gles::{
|
|||||||
ffi::{self, Gles2, FRAGMENT_SHADER, VERTEX_SHADER},
|
ffi::{self, Gles2, FRAGMENT_SHADER, VERTEX_SHADER},
|
||||||
GlesError,
|
GlesError,
|
||||||
};
|
};
|
||||||
use stereokit_rust::shader::{Shader, _ShaderT};
|
use std::mem::transmute;
|
||||||
|
use stereokit::Shader;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
// Simula shader with fancy lanzcos sampling
|
||||||
|
pub const UNLIT_SHADER_BYTES: &[u8] = include_bytes!("shader_unlit_gamma.sks");
|
||||||
|
|
||||||
|
// Simula shader with fancy lanzcos sampling
|
||||||
|
pub const PANEL_SHADER_BYTES: &[u8] = include_bytes!("shader_unlit_simula.sks");
|
||||||
|
|
||||||
struct FfiAssetHeader {
|
struct FfiAssetHeader {
|
||||||
asset_type: i32,
|
asset_type: i32,
|
||||||
asset_state: i32,
|
asset_state: i32,
|
||||||
@@ -126,7 +133,7 @@ pub unsafe fn shader_inject(
|
|||||||
let gl_frag = compile_shader(c, FRAGMENT_SHADER, frag_str)?;
|
let gl_frag = compile_shader(c, FRAGMENT_SHADER, frag_str)?;
|
||||||
let gl_prog = link_program(c, gl_vert, gl_frag)?;
|
let gl_prog = link_program(c, gl_vert, gl_frag)?;
|
||||||
|
|
||||||
let shader = sk_shader.0.as_mut() as *mut _ShaderT as *mut FfiShader;
|
let shader: *mut FfiShader = transmute(sk_shader.0.as_mut());
|
||||||
if let Some(shader) = shader.as_mut() {
|
if let Some(shader) = shader.as_mut() {
|
||||||
shader.shader.vertex = gl_vert;
|
shader.shader.vertex = gl_vert;
|
||||||
shader.shader.pixel = gl_frag;
|
shader.shader.pixel = gl_frag;
|
||||||
BIN
src/nodes/drawable/shaders/shader_unlit_gamma.sks
Normal file
BIN
src/nodes/drawable/shaders/shader_unlit_gamma.sks
Normal file
Binary file not shown.
BIN
src/nodes/drawable/shaders/shader_unlit_simula.sks
Normal file
BIN
src/nodes/drawable/shaders/shader_unlit_simula.sks
Normal file
Binary file not shown.
@@ -1,78 +1,107 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::{client::Client, destroy_queue, registry::Registry, resource::get_resource_file},
|
core::{client::Client, destroy_queue, registry::Registry, resource::ResourceID},
|
||||||
nodes::{spatial::Spatial, Aspect, Node},
|
nodes::{
|
||||||
|
drawable::Drawable,
|
||||||
|
spatial::{find_spatial_parent, parse_transform, Spatial},
|
||||||
|
Message, Node,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::{eyre, Result};
|
use color_eyre::eyre::{bail, ensure, eyre, Result};
|
||||||
use glam::{vec3, Mat4, Vec2};
|
use glam::{vec3, Mat4, Vec2};
|
||||||
|
use mint::Vector2;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use portable_atomic::{AtomicBool, Ordering};
|
||||||
|
use prisma::{Flatten, Rgba};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::{schemas::flex::deserialize, values::Transform};
|
||||||
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
|
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
|
||||||
use stereokit_rust::{
|
use stereokit::{named_colors::WHITE, Color128, StereoKitDraw, TextAlign, TextFit, TextStyle};
|
||||||
font::Font,
|
|
||||||
sk::MainThreadToken,
|
|
||||||
system::{TextAlign, TextFit, TextStyle as SkTextStyle},
|
|
||||||
util::{Color128, Color32},
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{TextAspect, TextStyle};
|
|
||||||
|
|
||||||
static TEXT_REGISTRY: Registry<Text> = Registry::new();
|
static TEXT_REGISTRY: Registry<Text> = Registry::new();
|
||||||
|
|
||||||
fn convert_align(x_align: super::XAlign, y_align: super::YAlign) -> TextAlign {
|
struct TextData {
|
||||||
match (x_align, y_align) {
|
text: String,
|
||||||
(super::XAlign::Left, super::YAlign::Top) => TextAlign::TopLeft,
|
character_height: f32,
|
||||||
(super::XAlign::Left, super::YAlign::Center) => TextAlign::CenterLeft,
|
text_align: TextAlign,
|
||||||
(super::XAlign::Left, super::YAlign::Bottom) => TextAlign::BottomLeft,
|
bounds: Option<Vec2>,
|
||||||
(super::XAlign::Center, super::YAlign::Top) => TextAlign::Center,
|
fit: TextFit,
|
||||||
(super::XAlign::Center, super::YAlign::Center) => TextAlign::Center,
|
bounds_align: TextAlign,
|
||||||
(super::XAlign::Center, super::YAlign::Bottom) => TextAlign::BottomCenter,
|
color: Rgba<f32>,
|
||||||
(super::XAlign::Right, super::YAlign::Top) => TextAlign::TopRight,
|
|
||||||
(super::XAlign::Right, super::YAlign::Center) => TextAlign::CenterRight,
|
|
||||||
(super::XAlign::Right, super::YAlign::Bottom) => TextAlign::BottomRight,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Text {
|
pub struct Text {
|
||||||
|
enabled: Arc<AtomicBool>,
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
font_path: Option<PathBuf>,
|
font_path: Option<PathBuf>,
|
||||||
style: OnceCell<SkTextStyle>,
|
style: OnceCell<TextStyle>,
|
||||||
|
|
||||||
text: Mutex<String>,
|
data: Mutex<TextData>,
|
||||||
data: Mutex<TextStyle>,
|
|
||||||
}
|
}
|
||||||
impl Text {
|
impl Text {
|
||||||
pub fn add_to(node: &Arc<Node>, text: String, style: TextStyle) -> Result<Arc<Text>> {
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn add_to(
|
||||||
|
node: &Arc<Node>,
|
||||||
|
font_resource_id: Option<ResourceID>,
|
||||||
|
text: String,
|
||||||
|
character_height: f32,
|
||||||
|
text_align: TextAlign,
|
||||||
|
bounds: Option<Vector2<f32>>,
|
||||||
|
fit: TextFit,
|
||||||
|
bounds_align: TextAlign,
|
||||||
|
color: Rgba<f32>,
|
||||||
|
) -> Result<Arc<Text>> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
ensure!(
|
||||||
|
node.drawable.get().is_none(),
|
||||||
|
"Internal: Node already has a drawable attached!"
|
||||||
|
);
|
||||||
|
|
||||||
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
|
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
|
||||||
let text = TEXT_REGISTRY.add(Text {
|
let text = TEXT_REGISTRY.add(Text {
|
||||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
enabled: node.enabled.clone(),
|
||||||
font_path: style.font.as_ref().and_then(|res| {
|
space: node.spatial.get().unwrap().clone(),
|
||||||
get_resource_file(res, &client, &[OsStr::new("ttf"), OsStr::new("otf")])
|
font_path: font_resource_id.and_then(|res| {
|
||||||
|
res.get_file(
|
||||||
|
&client.base_resource_prefixes.lock().clone(),
|
||||||
|
&[OsStr::new("ttf"), OsStr::new("otf")],
|
||||||
|
)
|
||||||
}),
|
}),
|
||||||
style: OnceCell::new(),
|
style: OnceCell::new(),
|
||||||
|
|
||||||
text: Mutex::new(text),
|
data: Mutex::new(TextData {
|
||||||
data: Mutex::new(style),
|
text,
|
||||||
|
character_height,
|
||||||
|
text_align,
|
||||||
|
bounds: bounds.map(|b| b.into()),
|
||||||
|
fit,
|
||||||
|
bounds_align,
|
||||||
|
color,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
<Text as TextAspect>::add_node_members(node);
|
node.add_local_signal("set_character_height", Text::set_character_height_flex);
|
||||||
node.add_aspect_raw(text.clone());
|
node.add_local_signal("set_text", Text::set_text_flex);
|
||||||
|
let _ = node.drawable.set(Drawable::Text(text.clone()));
|
||||||
|
|
||||||
Ok(text)
|
Ok(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self, token: &MainThreadToken) {
|
fn draw(&self, sk: &impl StereoKitDraw) {
|
||||||
let style =
|
let style = self
|
||||||
self.style
|
.style
|
||||||
.get_or_try_init(|| -> Result<SkTextStyle, color_eyre::eyre::Error> {
|
.get_or_try_init(|| -> Result<TextStyle, color_eyre::eyre::Error> {
|
||||||
let font = self
|
let font = self
|
||||||
.font_path
|
.font_path
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.and_then(|path| Font::from_file(path).ok())
|
.and_then(|path| sk.font_create(path).ok())
|
||||||
.unwrap_or_default();
|
.unwrap_or_else(|| sk.font_find("default/font").unwrap());
|
||||||
Ok(SkTextStyle::from_font(font, 1.0, Color32::WHITE))
|
Ok(unsafe { sk.text_make_style(font, 1.0, WHITE) })
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Ok(style) = style {
|
if let Ok(style) = style {
|
||||||
let text = self.text.lock();
|
|
||||||
let data = self.data.lock();
|
let data = self.data.lock();
|
||||||
let transform = self.space.global_transform()
|
let transform = self.space.global_transform()
|
||||||
* Mat4::from_scale(vec3(
|
* Mat4::from_scale(vec3(
|
||||||
@@ -80,75 +109,61 @@ impl Text {
|
|||||||
data.character_height,
|
data.character_height,
|
||||||
data.character_height,
|
data.character_height,
|
||||||
));
|
));
|
||||||
if let Some(bounds) = &data.bounds {
|
if let Some(bounds) = data.bounds {
|
||||||
stereokit_rust::system::Text::add_in(
|
sk.text_add_in(
|
||||||
token,
|
&data.text,
|
||||||
&*text,
|
|
||||||
transform,
|
transform,
|
||||||
Vec2::from(bounds.bounds) / data.character_height,
|
bounds / data.character_height,
|
||||||
match bounds.fit {
|
data.fit,
|
||||||
super::TextFit::Wrap => TextFit::Wrap,
|
*style,
|
||||||
super::TextFit::Clip => TextFit::Clip,
|
data.bounds_align,
|
||||||
super::TextFit::Squeeze => TextFit::Squeeze,
|
data.text_align,
|
||||||
super::TextFit::Exact => TextFit::Exact,
|
vec3(0.0, 0.0, 0.0),
|
||||||
super::TextFit::Overflow => TextFit::Overflow,
|
Color128::from([
|
||||||
},
|
data.color.red(),
|
||||||
Some(*style),
|
data.color.green(),
|
||||||
Some(Color128::new(
|
data.color.blue(),
|
||||||
data.color.c.r,
|
data.color.alpha(),
|
||||||
data.color.c.g,
|
]),
|
||||||
data.color.c.b,
|
|
||||||
data.color.a,
|
|
||||||
)),
|
|
||||||
data.bounds
|
|
||||||
.as_ref()
|
|
||||||
.map(|b| convert_align(b.anchor_align_x, b.anchor_align_y)),
|
|
||||||
Some(convert_align(data.text_align_x, data.text_align_y)),
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
stereokit_rust::system::Text::add_at(
|
sk.text_add_at(
|
||||||
token,
|
&data.text,
|
||||||
&*text,
|
|
||||||
transform,
|
transform,
|
||||||
Some(*style),
|
*style,
|
||||||
Some(Color128::new(
|
data.bounds_align,
|
||||||
data.color.c.r,
|
data.text_align,
|
||||||
data.color.c.g,
|
vec3(0.0, 0.0, 0.0),
|
||||||
data.color.c.b,
|
Color128::from([
|
||||||
data.color.a,
|
data.color.red(),
|
||||||
)),
|
data.color.green(),
|
||||||
data.bounds
|
data.color.blue(),
|
||||||
.as_ref()
|
data.color.alpha(),
|
||||||
.map(|b| convert_align(b.anchor_align_x, b.anchor_align_y)),
|
]),
|
||||||
Some(convert_align(data.text_align_x, data.text_align_y)),
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl Aspect for Text {
|
pub fn set_character_height_flex(
|
||||||
const NAME: &'static str = "Text";
|
node: &Node,
|
||||||
}
|
|
||||||
impl TextAspect for Text {
|
|
||||||
fn set_character_height(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
height: f32,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let this_text = node.get_aspect::<Text>()?;
|
let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")};
|
||||||
this_text.data.lock().character_height = height;
|
|
||||||
|
text.data.lock().character_height = deserialize(message.as_ref())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_text(node: Arc<Node>, _calling_client: Arc<Client>, text: String) -> Result<()> {
|
pub fn set_text_flex(
|
||||||
let this_text = node.get_aspect::<Text>()?;
|
node: &Node,
|
||||||
*this_text.text.lock() = text;
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")};
|
||||||
|
|
||||||
|
text.data.lock().text = deserialize(message.as_ref())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,12 +176,47 @@ impl Drop for Text {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_all(token: &MainThreadToken) {
|
pub fn draw_all(sk: &impl StereoKitDraw) {
|
||||||
for text in TEXT_REGISTRY.get_valid_contents() {
|
for text in TEXT_REGISTRY.get_valid_contents() {
|
||||||
if let Some(node) = text.space.node() {
|
if text.enabled.load(Ordering::Relaxed) {
|
||||||
if node.enabled() {
|
text.draw(sk);
|
||||||
text.draw(token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateTextInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
text: String,
|
||||||
|
font_resource: Option<ResourceID>,
|
||||||
|
character_height: f32,
|
||||||
|
text_align: TextAlign,
|
||||||
|
bounds: Option<Vector2<f32>>,
|
||||||
|
fit: TextFit,
|
||||||
|
bounds_align: TextAlign,
|
||||||
|
color: [f32; 4],
|
||||||
|
}
|
||||||
|
let info: CreateTextInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/drawable/text", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
let color = Rgba::from_slice(&info.color);
|
||||||
|
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
Text::add_to(
|
||||||
|
&node,
|
||||||
|
info.font_resource,
|
||||||
|
info.text,
|
||||||
|
info.character_height,
|
||||||
|
info.text_align,
|
||||||
|
info.bounds,
|
||||||
|
info.fit,
|
||||||
|
info.bounds_align,
|
||||||
|
color,
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,287 +0,0 @@
|
|||||||
use super::alias::{Alias, AliasInfo};
|
|
||||||
use super::spatial::{
|
|
||||||
Spatial, SPATIAL_REF_GET_LOCAL_BOUNDING_BOX_SERVER_OPCODE,
|
|
||||||
SPATIAL_REF_GET_RELATIVE_BOUNDING_BOX_SERVER_OPCODE, SPATIAL_REF_GET_TRANSFORM_SERVER_OPCODE,
|
|
||||||
};
|
|
||||||
use super::{Aspect, Node};
|
|
||||||
use crate::core::client::Client;
|
|
||||||
use crate::create_interface;
|
|
||||||
use crate::nodes::spatial::Transform;
|
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::nodes::spatial::SPATIAL_REF_ASPECT_ALIAS_INFO;
|
|
||||||
use color_eyre::eyre::{OptionExt, Result};
|
|
||||||
use glam::{vec2, vec3, vec3a, Vec3, Vec3A, Vec3Swizzles};
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
use stardust_xr::values::Vector3;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
// TODO: get SDFs working properly with non-uniform scale and so on, output distance relative to the spatial it's compared against
|
|
||||||
|
|
||||||
pub static FIELD_ALIAS_INFO: Lazy<AliasInfo> = Lazy::new(|| AliasInfo {
|
|
||||||
server_methods: vec![
|
|
||||||
SPATIAL_REF_GET_TRANSFORM_SERVER_OPCODE,
|
|
||||||
SPATIAL_REF_GET_LOCAL_BOUNDING_BOX_SERVER_OPCODE,
|
|
||||||
SPATIAL_REF_GET_RELATIVE_BOUNDING_BOX_SERVER_OPCODE,
|
|
||||||
FIELD_REF_DISTANCE_SERVER_OPCODE,
|
|
||||||
FIELD_REF_NORMAL_SERVER_OPCODE,
|
|
||||||
FIELD_REF_CLOSEST_POINT_SERVER_OPCODE,
|
|
||||||
FIELD_REF_RAY_MARCH_SERVER_OPCODE,
|
|
||||||
],
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_field_protocol!();
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
pub static ref EXPORTED_FIELDS: Mutex<FxHashMap<u64, Arc<Node>>> = Mutex::new(FxHashMap::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait FieldTrait: Send + Sync + 'static {
|
|
||||||
fn spatial_ref(&self) -> &Spatial;
|
|
||||||
|
|
||||||
fn local_distance(&self, p: Vec3A) -> f32;
|
|
||||||
fn local_normal(&self, p: Vec3A, r: f32) -> Vec3A {
|
|
||||||
let d = self.local_distance(p);
|
|
||||||
let e = vec2(r, 0_f32);
|
|
||||||
|
|
||||||
let n = vec3a(d, d, d)
|
|
||||||
- vec3a(
|
|
||||||
self.local_distance(vec3a(e.x, e.y, e.y)),
|
|
||||||
self.local_distance(vec3a(e.y, e.x, e.y)),
|
|
||||||
self.local_distance(vec3a(e.y, e.y, e.x)),
|
|
||||||
);
|
|
||||||
|
|
||||||
n.normalize()
|
|
||||||
}
|
|
||||||
fn local_closest_point(&self, p: Vec3A, r: f32) -> Vec3A {
|
|
||||||
p - (self.local_normal(p, r) * self.local_distance(p))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn distance(&self, reference_space: &Spatial, p: Vec3A) -> f32 {
|
|
||||||
let reference_to_local_space =
|
|
||||||
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
|
||||||
let local_p = reference_to_local_space.transform_point3a(p);
|
|
||||||
self.local_distance(local_p)
|
|
||||||
}
|
|
||||||
fn normal(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
|
||||||
let reference_to_local_space =
|
|
||||||
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
|
||||||
let local_p = reference_to_local_space.transform_point3a(p);
|
|
||||||
reference_to_local_space
|
|
||||||
.inverse()
|
|
||||||
.transform_vector3a(self.local_normal(local_p, r))
|
|
||||||
}
|
|
||||||
fn closest_point(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
|
||||||
let reference_to_local_space =
|
|
||||||
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
|
||||||
let local_p = reference_to_local_space.transform_point3a(p);
|
|
||||||
reference_to_local_space
|
|
||||||
.inverse()
|
|
||||||
.transform_point3a(self.local_closest_point(local_p, r))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ray_march(&self, ray: Ray) -> RayMarchResult {
|
|
||||||
let mut result = RayMarchResult {
|
|
||||||
ray_origin: ray.origin.into(),
|
|
||||||
ray_direction: ray.direction.into(),
|
|
||||||
min_distance: f32::MAX,
|
|
||||||
deepest_point_distance: 0_f32,
|
|
||||||
ray_length: 0_f32,
|
|
||||||
ray_steps: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
let ray_to_field_matrix =
|
|
||||||
Spatial::space_to_space_matrix(Some(&ray.space), Some(self.spatial_ref()));
|
|
||||||
let mut ray_point = ray_to_field_matrix.transform_point3a(ray.origin.into());
|
|
||||||
let ray_direction = ray_to_field_matrix
|
|
||||||
.transform_vector3a(ray.direction.into())
|
|
||||||
.normalize();
|
|
||||||
|
|
||||||
while result.ray_steps < MAX_RAY_STEPS && result.ray_length < MAX_RAY_LENGTH {
|
|
||||||
let distance = self.local_distance(ray_point);
|
|
||||||
let march_distance = distance.clamp(MIN_RAY_MARCH, MAX_RAY_MARCH);
|
|
||||||
|
|
||||||
result.ray_length += march_distance;
|
|
||||||
ray_point += ray_direction * march_distance;
|
|
||||||
|
|
||||||
if result.min_distance > distance {
|
|
||||||
result.deepest_point_distance = result.ray_length;
|
|
||||||
result.min_distance = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.ray_steps += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Ray {
|
|
||||||
pub origin: Vec3,
|
|
||||||
pub direction: Vec3,
|
|
||||||
pub space: Arc<Spatial>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// const MIN_RAY_STEPS: u32 = 0;
|
|
||||||
const MAX_RAY_STEPS: u32 = 1000;
|
|
||||||
|
|
||||||
const MIN_RAY_MARCH: f32 = 0.001_f32;
|
|
||||||
const MAX_RAY_MARCH: f32 = f32::MAX;
|
|
||||||
|
|
||||||
// const MIN_RAY_LENGTH: f32 = 0_f32;
|
|
||||||
const MAX_RAY_LENGTH: f32 = 1000_f32;
|
|
||||||
|
|
||||||
pub struct Field {
|
|
||||||
pub spatial: Arc<Spatial>,
|
|
||||||
pub shape: Mutex<Shape>,
|
|
||||||
}
|
|
||||||
impl Field {
|
|
||||||
pub fn add_to(node: &Arc<Node>, shape: Shape) -> Result<Arc<Field>> {
|
|
||||||
let spatial = node.get_aspect::<Spatial>()?;
|
|
||||||
let field = Field {
|
|
||||||
spatial,
|
|
||||||
shape: Mutex::new(shape),
|
|
||||||
};
|
|
||||||
let field = node.add_aspect(field);
|
|
||||||
<Field as FieldRefAspect>::add_node_members(node);
|
|
||||||
<Field as FieldAspect>::add_node_members(node);
|
|
||||||
Ok(field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Aspect for Field {
|
|
||||||
const NAME: &'static str = "Field";
|
|
||||||
}
|
|
||||||
impl FieldRefAspect for Field {
|
|
||||||
async fn distance(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
space: Arc<Node>,
|
|
||||||
point: Vector3<f32>,
|
|
||||||
) -> Result<f32> {
|
|
||||||
let reference_space = space.get_aspect::<Spatial>()?;
|
|
||||||
let field = node.get_aspect::<Field>()?;
|
|
||||||
Ok(field.distance(&reference_space, point.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn normal(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
space: Arc<Node>,
|
|
||||||
point: Vector3<f32>,
|
|
||||||
) -> Result<Vector3<f32>> {
|
|
||||||
let reference_space = space.get_aspect::<Spatial>()?;
|
|
||||||
let field = node.get_aspect::<Field>()?;
|
|
||||||
Ok(field.normal(&reference_space, point.into(), 0.0001).into())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn closest_point(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
space: Arc<Node>,
|
|
||||||
point: Vector3<f32>,
|
|
||||||
) -> Result<Vector3<f32>> {
|
|
||||||
let reference_space = space.get_aspect::<Spatial>()?;
|
|
||||||
let field = node.get_aspect::<Field>()?;
|
|
||||||
Ok(field
|
|
||||||
.closest_point(&reference_space, point.into(), 0.0001)
|
|
||||||
.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn ray_march(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
space: Arc<Node>,
|
|
||||||
ray_origin: Vector3<f32>,
|
|
||||||
ray_direction: Vector3<f32>,
|
|
||||||
) -> Result<RayMarchResult> {
|
|
||||||
let space = space.get_aspect::<Spatial>()?;
|
|
||||||
let field = node.get_aspect::<Field>()?;
|
|
||||||
Ok(field.ray_march(Ray {
|
|
||||||
origin: ray_origin.into(),
|
|
||||||
direction: ray_direction.into(),
|
|
||||||
space,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl FieldAspect for Field {
|
|
||||||
fn set_shape(node: Arc<Node>, _calling_client: Arc<Client>, shape: Shape) -> Result<()> {
|
|
||||||
let field = node.get_aspect::<Field>()?;
|
|
||||||
*field.shape.lock() = shape;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn export_field(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> {
|
|
||||||
let id = rand::random();
|
|
||||||
EXPORTED_FIELDS.lock().insert(id, node);
|
|
||||||
Ok(id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl FieldTrait for Field {
|
|
||||||
fn spatial_ref(&self) -> &Spatial {
|
|
||||||
&self.spatial
|
|
||||||
}
|
|
||||||
fn local_distance(&self, p: Vec3A) -> f32 {
|
|
||||||
match self.shape.lock().clone() {
|
|
||||||
Shape::Box(size) => {
|
|
||||||
let q = vec3(
|
|
||||||
p.x.abs() - (size.x * 0.5_f32),
|
|
||||||
p.y.abs() - (size.y * 0.5_f32),
|
|
||||||
p.z.abs() - (size.z * 0.5_f32),
|
|
||||||
);
|
|
||||||
let v = vec3a(q.x.max(0_f32), q.y.max(0_f32), q.z.max(0_f32));
|
|
||||||
v.length() + q.x.max(q.y.max(q.z)).min(0_f32)
|
|
||||||
}
|
|
||||||
Shape::Cylinder(CylinderShape { length, radius }) => {
|
|
||||||
let d = vec2(p.xy().length().abs() - radius, p.z.abs() - (length * 0.5));
|
|
||||||
d.x.max(d.y).min(0.0) + d.max(vec2(0.0, 0.0)).length()
|
|
||||||
}
|
|
||||||
Shape::Sphere(radius) => p.length() - radius,
|
|
||||||
Shape::Torus(TorusShape { radius_a, radius_b }) => {
|
|
||||||
let q = vec2(p.xz().length() - radius_a, p.y);
|
|
||||||
q.length() - radius_b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
create_interface!(FieldInterface);
|
|
||||||
pub struct FieldInterface;
|
|
||||||
impl InterfaceAspect for FieldInterface {
|
|
||||||
async fn import_field_ref(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
uid: u64,
|
|
||||||
) -> Result<Arc<Node>> {
|
|
||||||
EXPORTED_FIELDS
|
|
||||||
.lock()
|
|
||||||
.get(&uid)
|
|
||||||
.map(|s| {
|
|
||||||
Alias::create(
|
|
||||||
s,
|
|
||||||
&calling_client,
|
|
||||||
FIELD_REF_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.ok_or_eyre("Couldn't find spatial with that ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_field(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
shape: Shape,
|
|
||||||
) -> Result<()> {
|
|
||||||
let transform = transform.to_mat4(true, true, false);
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Field::add_to(&node, shape)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
92
src/nodes/fields/box.rs
Normal file
92
src/nodes/fields/box.rs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
use super::{Field, FieldTrait, Node};
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
|
use crate::nodes::Message;
|
||||||
|
use color_eyre::eyre::{ensure, Result};
|
||||||
|
use glam::{vec3, vec3a, Vec3, Vec3A};
|
||||||
|
use mint::Vector3;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct BoxField {
|
||||||
|
space: Arc<Spatial>,
|
||||||
|
size: Mutex<Vec3>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoxField {
|
||||||
|
pub fn add_to(node: &Arc<Node>, size: Vector3<f32>) -> Result<Arc<Field>> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
ensure!(
|
||||||
|
node.field.get().is_none(),
|
||||||
|
"Internal: Node already has a field attached!"
|
||||||
|
);
|
||||||
|
let box_field = BoxField {
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
|
size: Mutex::new(size.into()),
|
||||||
|
};
|
||||||
|
box_field.add_field_methods(node);
|
||||||
|
node.add_local_signal("set_size", BoxField::set_size_flex);
|
||||||
|
let field = Arc::new(Field::Box(box_field));
|
||||||
|
let _ = node.field.set(field.clone());
|
||||||
|
Ok(field)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size(&self, size: Vector3<f32>) {
|
||||||
|
*self.size.lock() = size.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Field::Box(box_field) = node.field.get().unwrap().as_ref() else { return Ok(()) };
|
||||||
|
box_field.set_size(deserialize(message.as_ref())?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FieldTrait for BoxField {
|
||||||
|
fn local_distance(&self, p: Vec3A) -> f32 {
|
||||||
|
let size = self.size.lock();
|
||||||
|
let q = vec3(
|
||||||
|
p.x.abs() - (size.x * 0.5_f32),
|
||||||
|
p.y.abs() - (size.y * 0.5_f32),
|
||||||
|
p.z.abs() - (size.z * 0.5_f32),
|
||||||
|
);
|
||||||
|
let v = vec3a(q.x.max(0_f32), q.y.max(0_f32), q.z.max(0_f32));
|
||||||
|
v.length() + q.x.max(q.y.max(q.z)).min(0_f32)
|
||||||
|
}
|
||||||
|
fn spatial_ref(&self) -> &Spatial {
|
||||||
|
self.space.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_box_field_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateFieldInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
size: Vector3<f32>,
|
||||||
|
}
|
||||||
|
let info: CreateFieldInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/field", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
BoxField::add_to(&node, info.size)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
93
src/nodes/fields/cylinder.rs
Normal file
93
src/nodes/fields/cylinder.rs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
use super::{Field, FieldTrait, Node};
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
|
use crate::nodes::Message;
|
||||||
|
use color_eyre::eyre::{ensure, Result};
|
||||||
|
use glam::{swizzles::*, vec2, Vec3A};
|
||||||
|
use portable_atomic::AtomicF32;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct CylinderField {
|
||||||
|
space: Arc<Spatial>,
|
||||||
|
length: AtomicF32,
|
||||||
|
radius: AtomicF32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CylinderField {
|
||||||
|
pub fn add_to(node: &Arc<Node>, length: f32, radius: f32) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
ensure!(
|
||||||
|
node.field.get().is_none(),
|
||||||
|
"Internal: Node already has a field attached!"
|
||||||
|
);
|
||||||
|
let cylinder_field = CylinderField {
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
|
length: AtomicF32::new(length.abs()),
|
||||||
|
radius: AtomicF32::new(radius.abs()),
|
||||||
|
};
|
||||||
|
cylinder_field.add_field_methods(node);
|
||||||
|
node.add_local_signal("set_size", CylinderField::set_size_flex);
|
||||||
|
let _ = node.field.set(Arc::new(Field::Cylinder(cylinder_field)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size(&self, length: f32, radius: f32) {
|
||||||
|
self.length.store(length.abs(), Ordering::Relaxed);
|
||||||
|
self.radius.store(radius.abs(), Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Field::Cylinder(cylinder_field) = node.field.get().unwrap().as_ref() else { return Ok(()) };
|
||||||
|
let (length, radius) = deserialize(message.as_ref())?;
|
||||||
|
cylinder_field.set_size(length, radius);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FieldTrait for CylinderField {
|
||||||
|
fn local_distance(&self, p: Vec3A) -> f32 {
|
||||||
|
let radius = self.radius.load(Ordering::Relaxed);
|
||||||
|
let length = self.length.load(Ordering::Relaxed);
|
||||||
|
let d = vec2(p.xy().length().abs() - radius, p.z.abs() - (length * 0.5));
|
||||||
|
|
||||||
|
d.x.max(d.y).min(0.0) + d.max(vec2(0.0, 0.0)).length()
|
||||||
|
}
|
||||||
|
fn spatial_ref(&self) -> &Spatial {
|
||||||
|
self.space.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_cylinder_field_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateFieldInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
length: f32,
|
||||||
|
radius: f32,
|
||||||
|
}
|
||||||
|
let info: CreateFieldInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/field", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
CylinderField::add_to(&node, info.length, info.radius)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
264
src/nodes/fields/mod.rs
Normal file
264
src/nodes/fields/mod.rs
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
pub mod r#box;
|
||||||
|
mod cylinder;
|
||||||
|
mod sphere;
|
||||||
|
mod torus;
|
||||||
|
|
||||||
|
use self::cylinder::{create_cylinder_field_flex, CylinderField};
|
||||||
|
use self::r#box::{create_box_field_flex, BoxField};
|
||||||
|
use self::sphere::{create_sphere_field_flex, SphereField};
|
||||||
|
use self::torus::{create_torus_field_flex, TorusField};
|
||||||
|
|
||||||
|
use super::alias::AliasInfo;
|
||||||
|
use super::spatial::Spatial;
|
||||||
|
use super::{Message, Node};
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::core::scenegraph::MethodResponseSender;
|
||||||
|
use crate::nodes::spatial::find_reference_space;
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use glam::{vec2, vec3a, Vec3, Vec3A};
|
||||||
|
use mint::Vector3;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
|
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub static FIELD_ALIAS_INFO: Lazy<AliasInfo> = Lazy::new(|| AliasInfo {
|
||||||
|
server_methods: vec!["distance", "normal", "closest_point", "ray_march"],
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
|
||||||
|
pub trait FieldTrait {
|
||||||
|
fn add_field_methods(&self, node: &Arc<Node>) {
|
||||||
|
node.add_local_method("distance", field_distance_flex);
|
||||||
|
node.add_local_method("normal", field_normal_flex);
|
||||||
|
node.add_local_method("closest_point", field_closest_point_flex);
|
||||||
|
node.add_local_method("ray_march", field_ray_march_flex);
|
||||||
|
}
|
||||||
|
fn spatial_ref(&self) -> &Spatial;
|
||||||
|
|
||||||
|
fn local_distance(&self, p: Vec3A) -> f32;
|
||||||
|
fn local_normal(&self, p: Vec3A, r: f32) -> Vec3A {
|
||||||
|
let d = self.local_distance(p);
|
||||||
|
let e = vec2(r, 0_f32);
|
||||||
|
|
||||||
|
let n = vec3a(d, d, d)
|
||||||
|
- vec3a(
|
||||||
|
self.local_distance(vec3a(e.x, e.y, e.y)),
|
||||||
|
self.local_distance(vec3a(e.y, e.x, e.y)),
|
||||||
|
self.local_distance(vec3a(e.y, e.y, e.x)),
|
||||||
|
);
|
||||||
|
|
||||||
|
n.normalize()
|
||||||
|
}
|
||||||
|
fn local_closest_point(&self, p: Vec3A, r: f32) -> Vec3A {
|
||||||
|
p - (self.local_normal(p, r) * self.local_distance(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn distance(&self, reference_space: &Spatial, p: Vec3A) -> f32 {
|
||||||
|
let reference_to_local_space =
|
||||||
|
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
||||||
|
let local_p = reference_to_local_space.transform_point3a(p);
|
||||||
|
self.local_distance(local_p)
|
||||||
|
}
|
||||||
|
fn normal(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
||||||
|
let reference_to_local_space =
|
||||||
|
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
||||||
|
let local_p = reference_to_local_space.transform_point3a(p);
|
||||||
|
reference_to_local_space
|
||||||
|
.inverse()
|
||||||
|
.transform_vector3a(self.local_normal(local_p, r))
|
||||||
|
}
|
||||||
|
fn closest_point(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
||||||
|
let reference_to_local_space =
|
||||||
|
Spatial::space_to_space_matrix(Some(reference_space), Some(self.spatial_ref()));
|
||||||
|
let local_p = reference_to_local_space.transform_point3a(p);
|
||||||
|
reference_to_local_space
|
||||||
|
.inverse()
|
||||||
|
.transform_point3a(self.local_closest_point(local_p, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ray_march(&self, ray: Ray) -> RayMarchResult {
|
||||||
|
let mut result = RayMarchResult {
|
||||||
|
min_distance: f32::MAX,
|
||||||
|
deepest_point_distance: 0_f32,
|
||||||
|
ray_length: 0_f32,
|
||||||
|
ray_steps: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let ray_to_field_matrix =
|
||||||
|
Spatial::space_to_space_matrix(Some(&ray.space), Some(self.spatial_ref()));
|
||||||
|
let mut ray_point = ray_to_field_matrix.transform_point3a(ray.origin.into());
|
||||||
|
let ray_direction = ray_to_field_matrix.transform_vector3a(ray.direction.into());
|
||||||
|
|
||||||
|
while result.ray_steps < MAX_RAY_STEPS && result.ray_length < MAX_RAY_LENGTH {
|
||||||
|
let distance = self.local_distance(ray_point);
|
||||||
|
let march_distance = distance.clamp(MIN_RAY_MARCH, MAX_RAY_MARCH);
|
||||||
|
|
||||||
|
result.ray_length += march_distance;
|
||||||
|
ray_point += ray_direction * march_distance;
|
||||||
|
|
||||||
|
if result.min_distance > distance {
|
||||||
|
result.deepest_point_distance = result.ray_length;
|
||||||
|
result.min_distance = distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.ray_steps += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Ray {
|
||||||
|
pub origin: Vec3,
|
||||||
|
pub direction: Vec3,
|
||||||
|
pub space: Arc<Spatial>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct RayMarchResult {
|
||||||
|
pub min_distance: f32,
|
||||||
|
pub deepest_point_distance: f32,
|
||||||
|
pub ray_length: f32,
|
||||||
|
pub ray_steps: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// const MIN_RAY_STEPS: u32 = 0;
|
||||||
|
const MAX_RAY_STEPS: u32 = 1000;
|
||||||
|
|
||||||
|
const MIN_RAY_MARCH: f32 = 0.001_f32;
|
||||||
|
const MAX_RAY_MARCH: f32 = f32::MAX;
|
||||||
|
|
||||||
|
// const MIN_RAY_LENGTH: f32 = 0_f32;
|
||||||
|
const MAX_RAY_LENGTH: f32 = 1000_f32;
|
||||||
|
|
||||||
|
fn field_distance_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FieldInfoArgs<'a> {
|
||||||
|
reference_space_path: &'a str,
|
||||||
|
point: Vector3<f32>,
|
||||||
|
}
|
||||||
|
let args: FieldInfoArgs = deserialize(message.as_ref())?;
|
||||||
|
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
|
||||||
|
|
||||||
|
let distance = node
|
||||||
|
.field
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.distance(reference_space.as_ref(), args.point.into());
|
||||||
|
Ok(serialize(distance)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn field_normal_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FieldInfoArgs<'a> {
|
||||||
|
reference_space_path: &'a str,
|
||||||
|
point: Vector3<f32>,
|
||||||
|
}
|
||||||
|
let args: FieldInfoArgs = deserialize(message.as_ref())?;
|
||||||
|
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
|
||||||
|
|
||||||
|
let normal = node.field.get().as_ref().unwrap().normal(
|
||||||
|
reference_space.as_ref(),
|
||||||
|
args.point.into(),
|
||||||
|
0.001,
|
||||||
|
);
|
||||||
|
Ok(serialize(mint::Vector3::from(normal))?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn field_closest_point_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FieldInfoArgs<'a> {
|
||||||
|
reference_space_path: &'a str,
|
||||||
|
point: Vector3<f32>,
|
||||||
|
}
|
||||||
|
let args: FieldInfoArgs = deserialize(message.as_ref())?;
|
||||||
|
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
|
||||||
|
|
||||||
|
let closest_point = node.field.get().as_ref().unwrap().closest_point(
|
||||||
|
reference_space.as_ref(),
|
||||||
|
args.point.into(),
|
||||||
|
0.001,
|
||||||
|
);
|
||||||
|
Ok(serialize(mint::Vector3::from(closest_point))?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn field_ray_march_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FieldInfoArgs<'a> {
|
||||||
|
reference_space_path: &'a str,
|
||||||
|
ray_origin: Vector3<f32>,
|
||||||
|
ray_direction: Vector3<f32>,
|
||||||
|
}
|
||||||
|
let args: FieldInfoArgs = deserialize(message.as_ref())?;
|
||||||
|
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
|
||||||
|
|
||||||
|
let ray_march_result = node.field.get().unwrap().ray_march(Ray {
|
||||||
|
origin: args.ray_origin.into(),
|
||||||
|
direction: args.ray_direction.into(),
|
||||||
|
space: reference_space,
|
||||||
|
});
|
||||||
|
Ok(serialize(ray_march_result)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Field {
|
||||||
|
Box(BoxField),
|
||||||
|
Cylinder(CylinderField),
|
||||||
|
Sphere(SphereField),
|
||||||
|
Torus(TorusField),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for Field {
|
||||||
|
type Target = dyn FieldTrait;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
match self {
|
||||||
|
Field::Box(field) => field,
|
||||||
|
Field::Cylinder(field) => field,
|
||||||
|
Field::Sphere(field) => field,
|
||||||
|
Field::Torus(field) => field,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "field", false);
|
||||||
|
node.add_local_signal("create_box_field", create_box_field_flex);
|
||||||
|
node.add_local_signal("create_cylinder_field", create_cylinder_field_flex);
|
||||||
|
node.add_local_signal("create_sphere_field", create_sphere_field_flex);
|
||||||
|
node.add_local_signal("create_torus_field", create_torus_field_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_field(client: &Client, path: &str) -> Result<Arc<Field>> {
|
||||||
|
client
|
||||||
|
.get_node("Field", path)?
|
||||||
|
.get_aspect("Field", "info", |n| &n.field)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
93
src/nodes/fields/sphere.rs
Normal file
93
src/nodes/fields/sphere.rs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
use super::{Field, FieldTrait, Node};
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::nodes::spatial::{find_spatial_parent, Spatial};
|
||||||
|
use crate::nodes::Message;
|
||||||
|
use color_eyre::eyre::{ensure, Result};
|
||||||
|
use glam::{Mat4, Vec3A};
|
||||||
|
use mint::Vector3;
|
||||||
|
use portable_atomic::AtomicF32;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct SphereField {
|
||||||
|
space: Arc<Spatial>,
|
||||||
|
radius: AtomicF32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SphereField {
|
||||||
|
pub fn add_to(node: &Arc<Node>, radius: f32) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
ensure!(
|
||||||
|
node.field.get().is_none(),
|
||||||
|
"Internal: Node already has a field attached!"
|
||||||
|
);
|
||||||
|
let sphere_field = SphereField {
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
|
radius: AtomicF32::new(radius),
|
||||||
|
};
|
||||||
|
sphere_field.add_field_methods(node);
|
||||||
|
node.add_local_signal("set_radius", SphereField::set_radius_flex);
|
||||||
|
let _ = node.field.set(Arc::new(Field::Sphere(sphere_field)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_radius(&self, radius: f32) {
|
||||||
|
self.radius.store(radius, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_radius_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Field::Sphere(sphere_field) = node.field.get().unwrap().as_ref() else { return Ok(()) };
|
||||||
|
sphere_field.set_radius(deserialize(message.as_ref())?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FieldTrait for SphereField {
|
||||||
|
fn local_distance(&self, p: Vec3A) -> f32 {
|
||||||
|
p.length() - self.radius.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
fn local_normal(&self, p: Vec3A, _r: f32) -> Vec3A {
|
||||||
|
-p.normalize()
|
||||||
|
}
|
||||||
|
fn local_closest_point(&self, p: Vec3A, _r: f32) -> Vec3A {
|
||||||
|
p.normalize() * self.radius.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
fn spatial_ref(&self) -> &Spatial {
|
||||||
|
self.space.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_sphere_field_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateFieldInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
origin: Option<Vector3<f32>>,
|
||||||
|
radius: f32,
|
||||||
|
}
|
||||||
|
let info: CreateFieldInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/field", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = Mat4::from_translation(
|
||||||
|
info.origin
|
||||||
|
.unwrap_or_else(|| Vector3::from([0.0; 3]))
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
SphereField::add_to(&node, info.radius)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
93
src/nodes/fields/torus.rs
Normal file
93
src/nodes/fields/torus.rs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
use super::{Field, FieldTrait, Node};
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
|
use crate::nodes::Message;
|
||||||
|
use color_eyre::eyre::{ensure, Result};
|
||||||
|
use glam::{swizzles::*, vec2, Vec3A};
|
||||||
|
use portable_atomic::AtomicF32;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct TorusField {
|
||||||
|
space: Arc<Spatial>,
|
||||||
|
radius_a: AtomicF32,
|
||||||
|
radius_b: AtomicF32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TorusField {
|
||||||
|
pub fn add_to(node: &Arc<Node>, radius_a: f32, radius_b: f32) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
ensure!(
|
||||||
|
node.field.get().is_none(),
|
||||||
|
"Internal: Node already has a field attached!"
|
||||||
|
);
|
||||||
|
let torus_field = TorusField {
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
|
radius_a: AtomicF32::new(radius_a.abs()),
|
||||||
|
radius_b: AtomicF32::new(radius_b.abs()),
|
||||||
|
};
|
||||||
|
torus_field.add_field_methods(node);
|
||||||
|
node.add_local_signal("set_size", TorusField::set_size_flex);
|
||||||
|
let _ = node.field.set(Arc::new(Field::Torus(torus_field)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size(&self, radius_a: f32, radius_b: f32) {
|
||||||
|
self.radius_a.store(radius_a.abs(), Ordering::Relaxed);
|
||||||
|
self.radius_b.store(radius_b.abs(), Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_size_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let Field::Torus(torus_field) = node.field.get().unwrap().as_ref() else { return Ok(()) };
|
||||||
|
let (radius_a, radius_b) = deserialize(message.as_ref())?;
|
||||||
|
torus_field.set_size(radius_a, radius_b);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FieldTrait for TorusField {
|
||||||
|
fn local_distance(&self, p: Vec3A) -> f32 {
|
||||||
|
let radius_a = self.radius_a.load(Ordering::Relaxed);
|
||||||
|
let radius_b = self.radius_b.load(Ordering::Relaxed);
|
||||||
|
let q = vec2(p.xz().length() - radius_a, p.y);
|
||||||
|
q.length() - radius_b
|
||||||
|
}
|
||||||
|
fn spatial_ref(&self) -> &Spatial {
|
||||||
|
self.space.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_torus_field_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateFieldInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
radius_a: f32,
|
||||||
|
radius_b: f32,
|
||||||
|
}
|
||||||
|
let info: CreateFieldInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/field", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
TorusField::add_to(&node, info.radius_a, info.radius_b)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
46
src/nodes/hmd.rs
Normal file
46
src/nodes/hmd.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use super::{alias::Alias, spatial::Spatial, Node};
|
||||||
|
use crate::{
|
||||||
|
core::client::{Client, INTERNAL_CLIENT},
|
||||||
|
nodes::alias::AliasInfo,
|
||||||
|
};
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use glam::{vec3, Mat4};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use stereokit::StereoKitMultiThread;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
static ref HMD: Arc<Node> = create();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create() -> Arc<Node> {
|
||||||
|
let node = Arc::new(Node::create(&INTERNAL_CLIENT, "", "hmd", false));
|
||||||
|
Spatial::add_to(&node, None, Mat4::IDENTITY, false).expect("Unable to make spatial for HMD");
|
||||||
|
|
||||||
|
node
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn frame(sk: &impl StereoKitMultiThread) {
|
||||||
|
let spatial = HMD
|
||||||
|
.spatial
|
||||||
|
.get()
|
||||||
|
.expect("Unable to get spatial to update HMD");
|
||||||
|
let hmd_pose = sk.input_head();
|
||||||
|
*spatial.transform.lock() = Mat4::from_scale_rotation_translation(
|
||||||
|
vec3(1.0, 1.0, 1.0),
|
||||||
|
hmd_pose.orientation.into(),
|
||||||
|
hmd_pose.position.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn make_alias(client: &Arc<Client>) -> Result<Arc<Node>> {
|
||||||
|
Alias::create(
|
||||||
|
client,
|
||||||
|
"",
|
||||||
|
"hmd",
|
||||||
|
&HMD,
|
||||||
|
AliasInfo {
|
||||||
|
server_signals: vec!["get_bounds", "get_transform"],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,89 +1,51 @@
|
|||||||
use super::{Finger, Hand, InputDataTrait, InputHandler, InputMethod, Joint, Thumb};
|
use crate::nodes::fields::Field;
|
||||||
use crate::nodes::fields::{Field, FieldTrait};
|
|
||||||
use crate::nodes::spatial::Spatial;
|
use crate::nodes::spatial::Spatial;
|
||||||
use glam::{vec3a, Mat4, Quat};
|
use glam::{vec3a, Mat4};
|
||||||
|
use stardust_xr::schemas::flat::{Hand as FlatHand, InputDataType, Joint};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
impl Default for Joint {
|
use super::{DistanceLink, InputSpecialization};
|
||||||
fn default() -> Self {
|
|
||||||
Joint {
|
|
||||||
position: [0.0; 3].into(),
|
|
||||||
rotation: Quat::IDENTITY.into(),
|
|
||||||
radius: 0.0,
|
|
||||||
distance: 0.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[allow(clippy::derivable_impls)]
|
|
||||||
impl Default for Finger {
|
|
||||||
fn default() -> Self {
|
|
||||||
Finger {
|
|
||||||
tip: Default::default(),
|
|
||||||
distal: Default::default(),
|
|
||||||
intermediate: Default::default(),
|
|
||||||
proximal: Default::default(),
|
|
||||||
metacarpal: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[allow(clippy::derivable_impls)]
|
|
||||||
impl Default for Thumb {
|
|
||||||
fn default() -> Self {
|
|
||||||
Thumb {
|
|
||||||
tip: Default::default(),
|
|
||||||
distal: Default::default(),
|
|
||||||
proximal: Default::default(),
|
|
||||||
metacarpal: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[allow(clippy::derivable_impls)]
|
|
||||||
impl Default for Hand {
|
|
||||||
fn default() -> Self {
|
|
||||||
Hand {
|
|
||||||
right: Default::default(),
|
|
||||||
thumb: Default::default(),
|
|
||||||
index: Default::default(),
|
|
||||||
middle: Default::default(),
|
|
||||||
ring: Default::default(),
|
|
||||||
little: Default::default(),
|
|
||||||
palm: Default::default(),
|
|
||||||
wrist: Default::default(),
|
|
||||||
elbow: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InputDataTrait for Hand {
|
#[derive(Debug, Default)]
|
||||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
pub struct Hand {
|
||||||
|
pub base: FlatHand,
|
||||||
|
}
|
||||||
|
impl InputSpecialization for Hand {
|
||||||
|
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
|
self.true_distance(space, field).abs()
|
||||||
|
}
|
||||||
|
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
let mut min_distance = f32::MAX;
|
let mut min_distance = f32::MAX;
|
||||||
|
|
||||||
for tip in [
|
for tip in [
|
||||||
&self.thumb.tip.position,
|
&self.base.thumb.tip.position,
|
||||||
&self.index.tip.position,
|
&self.base.index.tip.position,
|
||||||
&self.middle.tip.position,
|
&self.base.middle.tip.position,
|
||||||
&self.ring.tip.position,
|
&self.base.ring.tip.position,
|
||||||
&self.little.tip.position,
|
&self.base.little.tip.position,
|
||||||
] {
|
] {
|
||||||
min_distance = min_distance.min(field.distance(space, vec3a(tip.x, tip.y, tip.z)));
|
min_distance = min_distance.min(field.distance(space, vec3a(tip.x, tip.y, tip.z)));
|
||||||
}
|
}
|
||||||
|
|
||||||
min_distance
|
min_distance
|
||||||
}
|
}
|
||||||
fn transform(&mut self, method: &InputMethod, handler: &InputHandler) {
|
fn serialize(
|
||||||
let local_to_handler_matrix =
|
&self,
|
||||||
Spatial::space_to_space_matrix(Some(&method.spatial), Some(&handler.spatial));
|
distance_link: &DistanceLink,
|
||||||
|
local_to_handler_matrix: Mat4,
|
||||||
|
) -> InputDataType {
|
||||||
|
let mut hand = self.base;
|
||||||
let mut joints: Vec<&mut Joint> = Vec::new();
|
let mut joints: Vec<&mut Joint> = Vec::new();
|
||||||
|
|
||||||
joints.extend([&mut self.palm, &mut self.wrist]);
|
joints.extend([&mut hand.palm, &mut hand.wrist]);
|
||||||
if let Some(elbow) = &mut self.elbow {
|
if let Some(elbow) = &mut hand.elbow {
|
||||||
joints.push(elbow);
|
joints.push(elbow);
|
||||||
}
|
}
|
||||||
for finger in [
|
for finger in [
|
||||||
&mut self.index,
|
&mut hand.index,
|
||||||
&mut self.middle,
|
&mut hand.middle,
|
||||||
&mut self.ring,
|
&mut hand.ring,
|
||||||
&mut self.little,
|
&mut hand.little,
|
||||||
] {
|
] {
|
||||||
joints.extend([
|
joints.extend([
|
||||||
&mut finger.tip,
|
&mut finger.tip,
|
||||||
@@ -94,10 +56,10 @@ impl InputDataTrait for Hand {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
joints.extend([
|
joints.extend([
|
||||||
&mut self.thumb.tip,
|
&mut hand.thumb.tip,
|
||||||
&mut self.thumb.distal,
|
&mut hand.thumb.distal,
|
||||||
&mut self.thumb.proximal,
|
&mut hand.thumb.proximal,
|
||||||
&mut self.thumb.metacarpal,
|
&mut hand.thumb.metacarpal,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
for joint in joints {
|
for joint in joints {
|
||||||
@@ -106,7 +68,12 @@ impl InputDataTrait for Hand {
|
|||||||
let (_, rotation, position) = joint_matrix.to_scale_rotation_translation();
|
let (_, rotation, position) = joint_matrix.to_scale_rotation_translation();
|
||||||
joint.position = position.into();
|
joint.position = position.into();
|
||||||
joint.rotation = rotation.into();
|
joint.rotation = rotation.into();
|
||||||
joint.distance = handler.field.distance(&handler.spatial, position.into());
|
joint.distance = distance_link
|
||||||
|
.handler
|
||||||
|
.field
|
||||||
|
.distance(&distance_link.handler.spatial, position.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputDataType::Hand(Box::new(hand))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
use super::{InputHandlerAspect, INPUT_HANDLER_REGISTRY, INPUT_METHOD_REGISTRY};
|
|
||||||
use crate::nodes::{alias::AliasList, fields::Field, spatial::Spatial, Aspect, Node};
|
|
||||||
use color_eyre::eyre::Result;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
pub struct InputHandler {
|
|
||||||
pub spatial: Arc<Spatial>,
|
|
||||||
pub field: Arc<Field>,
|
|
||||||
pub(super) method_aliases: AliasList,
|
|
||||||
}
|
|
||||||
impl InputHandler {
|
|
||||||
pub fn add_to(node: &Arc<Node>, field: &Arc<Field>) -> Result<()> {
|
|
||||||
let handler = InputHandler {
|
|
||||||
spatial: node.get_aspect::<Spatial>().unwrap().clone(),
|
|
||||||
field: field.clone(),
|
|
||||||
method_aliases: AliasList::default(),
|
|
||||||
};
|
|
||||||
for method in INPUT_METHOD_REGISTRY.get_valid_contents() {
|
|
||||||
method.handle_new_handler(&handler);
|
|
||||||
}
|
|
||||||
let handler = INPUT_HANDLER_REGISTRY.add(handler);
|
|
||||||
node.add_aspect_raw(handler);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Aspect for InputHandler {
|
|
||||||
const NAME: &'static str = "InputHandler";
|
|
||||||
}
|
|
||||||
impl InputHandlerAspect for InputHandler {}
|
|
||||||
impl PartialEq for InputHandler {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.spatial == other.spatial
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for InputHandler {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
INPUT_HANDLER_REGISTRY.remove(self);
|
|
||||||
for method in INPUT_METHOD_REGISTRY.get_valid_contents() {
|
|
||||||
method.handle_drop_handler(self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,233 +0,0 @@
|
|||||||
use super::{
|
|
||||||
input_method_client, InputData, InputDataTrait, InputDataType, InputHandler, InputMethodAspect,
|
|
||||||
InputMethodRefAspect, INPUT_HANDLER_REGISTRY, INPUT_METHOD_REF_ASPECT_ALIAS_INFO,
|
|
||||||
INPUT_METHOD_REGISTRY,
|
|
||||||
};
|
|
||||||
use crate::{
|
|
||||||
core::{client::Client, registry::Registry},
|
|
||||||
nodes::{
|
|
||||||
alias::{Alias, AliasList},
|
|
||||||
fields::{Field, FIELD_ALIAS_INFO},
|
|
||||||
spatial::Spatial,
|
|
||||||
Aspect, Node,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use color_eyre::eyre::Result;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use stardust_xr::values::Datamap;
|
|
||||||
use std::sync::{Arc, Weak};
|
|
||||||
|
|
||||||
pub struct InputMethod {
|
|
||||||
pub spatial: Arc<Spatial>,
|
|
||||||
pub data: Mutex<InputDataType>,
|
|
||||||
pub datamap: Mutex<Datamap>,
|
|
||||||
|
|
||||||
handler_aliases: AliasList,
|
|
||||||
handler_field_aliases: AliasList,
|
|
||||||
pub(super) handler_order: Mutex<Vec<Weak<InputHandler>>>,
|
|
||||||
pub internal_capture_requests: Registry<InputHandler>,
|
|
||||||
pub captures: Registry<InputHandler>,
|
|
||||||
}
|
|
||||||
impl InputMethod {
|
|
||||||
pub fn add_to(
|
|
||||||
node: &Arc<Node>,
|
|
||||||
data: InputDataType,
|
|
||||||
datamap: Datamap,
|
|
||||||
) -> Result<Arc<InputMethod>> {
|
|
||||||
let method = InputMethod {
|
|
||||||
spatial: node.get_aspect::<Spatial>().unwrap().clone(),
|
|
||||||
data: Mutex::new(data),
|
|
||||||
datamap: Mutex::new(datamap),
|
|
||||||
|
|
||||||
handler_aliases: AliasList::default(),
|
|
||||||
handler_field_aliases: AliasList::default(),
|
|
||||||
handler_order: Mutex::new(Vec::new()),
|
|
||||||
internal_capture_requests: Registry::new(),
|
|
||||||
captures: Registry::new(),
|
|
||||||
};
|
|
||||||
<InputMethod as InputMethodRefAspect>::add_node_members(node);
|
|
||||||
<InputMethod as InputMethodAspect>::add_node_members(node);
|
|
||||||
for handler in INPUT_HANDLER_REGISTRY.get_valid_contents() {
|
|
||||||
method.handle_new_handler(&handler);
|
|
||||||
}
|
|
||||||
let method = INPUT_METHOD_REGISTRY.add(method);
|
|
||||||
node.add_aspect_raw(method.clone());
|
|
||||||
Ok(method)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn distance(&self, to: &Field) -> f32 {
|
|
||||||
self.data.lock().distance(&self.spatial, to)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_handler_order<'a>(&self, handlers: impl Iterator<Item = &'a Arc<InputHandler>>) {
|
|
||||||
*self.handler_order.lock() = handlers.map(Arc::downgrade).collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn make_alias(&self, handler: &InputHandler) {
|
|
||||||
let Some(method_node) = self.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(handler_node) = handler.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(client) = handler_node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Ok(method_alias) = Alias::create(
|
|
||||||
&method_node,
|
|
||||||
&client,
|
|
||||||
INPUT_METHOD_REF_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
Some(&handler.method_aliases),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
method_alias.set_enabled(false);
|
|
||||||
}
|
|
||||||
pub(super) fn handle_new_handler(&self, handler: &InputHandler) {
|
|
||||||
self.make_alias(handler);
|
|
||||||
|
|
||||||
let Some(method_node) = self.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(method_client) = method_node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(handler_node) = handler.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
// Receiver itself
|
|
||||||
let Ok(handler_alias) = Alias::create(
|
|
||||||
&handler_node,
|
|
||||||
&method_client,
|
|
||||||
INPUT_METHOD_REF_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
Some(&self.handler_aliases),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(handler_field_node) = handler.field.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
// Handler's field
|
|
||||||
let Ok(rx_field_alias) = Alias::create(
|
|
||||||
&handler_field_node,
|
|
||||||
&method_client,
|
|
||||||
FIELD_ALIAS_INFO.clone(),
|
|
||||||
Some(&self.handler_field_aliases),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = input_method_client::create_handler(&method_node, &handler_alias, &rx_field_alias);
|
|
||||||
}
|
|
||||||
pub(super) fn handle_drop_handler(&self, handler: &InputHandler) {
|
|
||||||
let Some(tx_node) = self.spatial.node() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(handler_alias) = self.handler_aliases.get_from_aspect(handler) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = input_method_client::destroy_handler(&tx_node, handler_alias.id);
|
|
||||||
self.handler_aliases.remove_aspect(handler);
|
|
||||||
self.handler_field_aliases
|
|
||||||
.remove_aspect(handler.field.as_ref());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn serialize(&self, alias_id: u64, handler: &Arc<InputHandler>) -> InputData {
|
|
||||||
let mut input = self.data.lock().clone();
|
|
||||||
input.transform(self, handler);
|
|
||||||
|
|
||||||
InputData {
|
|
||||||
id: alias_id,
|
|
||||||
input,
|
|
||||||
distance: self.distance(&handler.field),
|
|
||||||
datamap: self.datamap.lock().clone(),
|
|
||||||
order: self
|
|
||||||
.handler_order
|
|
||||||
.lock()
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.find(|(_, h)| h.ptr_eq(&Arc::downgrade(handler)))
|
|
||||||
.unwrap()
|
|
||||||
.0 as u32,
|
|
||||||
captured: self.captures.get_valid_contents().contains(handler),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Aspect for InputMethod {
|
|
||||||
const NAME: &'static str = "InputMethod";
|
|
||||||
}
|
|
||||||
impl InputMethodRefAspect for InputMethod {
|
|
||||||
#[doc = "Have the input handler that this method reference came from capture the method for the next frame."]
|
|
||||||
fn request_capture(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
handler: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let input_method = node.get_aspect::<InputMethod>()?;
|
|
||||||
let input_handler = handler.get_aspect::<InputHandler>()?;
|
|
||||||
|
|
||||||
input_method
|
|
||||||
.internal_capture_requests
|
|
||||||
.add_raw(&input_handler);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl InputMethodAspect for InputMethod {
|
|
||||||
#[doc = "Set the spatial input component of this input method. You must keep the same input data type throughout the entire thing."]
|
|
||||||
fn set_input(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
input: InputDataType,
|
|
||||||
) -> Result<()> {
|
|
||||||
let input_method = node.get_aspect::<InputMethod>()?;
|
|
||||||
*input_method.data.lock() = input;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Set the datmap of this input method"]
|
|
||||||
fn set_datamap(node: Arc<Node>, _calling_client: Arc<Client>, datamap: Datamap) -> Result<()> {
|
|
||||||
let input_method = node.get_aspect::<InputMethod>()?;
|
|
||||||
*input_method.datamap.lock() = datamap;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Manually set the order of handlers to propagate input to, or else let the server decide."]
|
|
||||||
fn set_handler_order(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
handlers: Vec<Arc<Node>>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let input_method = node.get_aspect::<InputMethod>()?;
|
|
||||||
let handlers = handlers
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|p| p.get_aspect::<InputHandler>().ok())
|
|
||||||
.map(|i| Arc::downgrade(&i))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
*input_method.handler_order.lock() = handlers;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Set which handlers are captured."]
|
|
||||||
fn set_captures(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
handlers: Vec<Arc<Node>>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let input_method = node.get_aspect::<InputMethod>()?;
|
|
||||||
input_method.captures.clear();
|
|
||||||
for handler in handlers {
|
|
||||||
let Ok(handler) = handler.get_aspect::<InputHandler>() else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
input_method.captures.add_raw(&handler);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for InputMethod {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
INPUT_METHOD_REGISTRY.remove(self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,152 +1,421 @@
|
|||||||
#![allow(clippy::needless_question_mark)]
|
pub mod hand;
|
||||||
|
pub mod pointer;
|
||||||
|
pub mod tip;
|
||||||
|
|
||||||
mod hand;
|
use self::hand::Hand;
|
||||||
mod handler;
|
use self::pointer::Pointer;
|
||||||
mod method;
|
use self::tip::Tip;
|
||||||
mod pointer;
|
|
||||||
mod tip;
|
|
||||||
|
|
||||||
pub use handler::*;
|
use super::{
|
||||||
pub use method::*;
|
alias::{Alias, AliasInfo},
|
||||||
|
fields::{find_field, Field, FIELD_ALIAS_INFO},
|
||||||
use super::fields::Field;
|
spatial::{find_spatial_parent, parse_transform, Spatial},
|
||||||
use super::spatial::Spatial;
|
Message, Node,
|
||||||
use crate::create_interface;
|
};
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
use crate::core::registry::Registry;
|
||||||
use crate::nodes::spatial::SPATIAL_REF_ASPECT_ALIAS_INFO;
|
use crate::core::{client::Client, node_collections::LifeLinkedNodeMap};
|
||||||
use crate::{core::client::Client, nodes::Node};
|
use color_eyre::eyre::{ensure, Result};
|
||||||
use crate::{core::registry::Registry, nodes::spatial::Transform};
|
use glam::Mat4;
|
||||||
use color_eyre::eyre::Result;
|
use once_cell::sync::OnceCell;
|
||||||
use stardust_xr::values::Datamap;
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use portable_atomic::AtomicBool;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::{flat::InputData, flex::deserialize};
|
||||||
|
use stardust_xr::schemas::{
|
||||||
|
flat::{Datamap, InputDataType},
|
||||||
|
flex::serialize,
|
||||||
|
};
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::sync::{Arc, Weak};
|
||||||
|
use tracing::{debug_span, instrument};
|
||||||
|
|
||||||
static INPUT_METHOD_REGISTRY: Registry<InputMethod> = Registry::new();
|
static INPUT_METHOD_REGISTRY: Registry<InputMethod> = Registry::new();
|
||||||
pub static INPUT_HANDLER_REGISTRY: Registry<InputHandler> = Registry::new();
|
static INPUT_HANDLER_REGISTRY: Registry<InputHandler> = Registry::new();
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_input_protocol!();
|
pub trait InputSpecialization: Send + Sync {
|
||||||
|
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
||||||
pub trait InputDataTrait {
|
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
||||||
fn transform(&mut self, method: &InputMethod, handler: &InputHandler);
|
fn serialize(
|
||||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
&self,
|
||||||
|
distance_link: &DistanceLink,
|
||||||
|
local_to_handler_matrix: Mat4,
|
||||||
|
) -> InputDataType;
|
||||||
}
|
}
|
||||||
impl InputDataTrait for InputDataType {
|
pub enum InputType {
|
||||||
fn transform(&mut self, method: &InputMethod, handler: &InputHandler) {
|
Pointer(Pointer),
|
||||||
|
Hand(Box<Hand>),
|
||||||
|
Tip(Tip),
|
||||||
|
}
|
||||||
|
impl Deref for InputType {
|
||||||
|
type Target = dyn InputSpecialization;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
match self {
|
match self {
|
||||||
InputDataType::Pointer(i) => i.transform(method, handler),
|
InputType::Pointer(p) => p,
|
||||||
InputDataType::Hand(i) => i.transform(method, handler),
|
InputType::Hand(h) => h.as_ref(),
|
||||||
InputDataType::Tip(i) => i.transform(method, handler),
|
InputType::Tip(t) => t,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
|
||||||
match self {
|
|
||||||
InputDataType::Pointer(i) => i.distance(space, field),
|
|
||||||
InputDataType::Hand(i) => i.distance(space, field),
|
|
||||||
InputDataType::Tip(i) => i.distance(space, field),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
create_interface!(InputInterface);
|
pub struct InputMethod {
|
||||||
pub struct InputInterface;
|
node: Weak<Node>,
|
||||||
impl InterfaceAspect for InputInterface {
|
uid: String,
|
||||||
#[doc = "Create an input method node"]
|
pub enabled: Mutex<bool>,
|
||||||
fn create_input_method(
|
pub spatial: Arc<Spatial>,
|
||||||
_node: Arc<Node>,
|
pub specialization: Mutex<InputType>,
|
||||||
calling_client: Arc<Client>,
|
captures: Registry<InputHandler>,
|
||||||
id: u64,
|
pub datamap: Mutex<Option<Datamap>>,
|
||||||
parent: Arc<Node>,
|
handler_aliases: LifeLinkedNodeMap<String>,
|
||||||
transform: Transform,
|
handler_order: OnceCell<Mutex<Vec<Weak<InputHandler>>>>,
|
||||||
initial_data: InputDataType,
|
}
|
||||||
datamap: Datamap,
|
impl InputMethod {
|
||||||
) -> Result<()> {
|
#[allow(dead_code)]
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
pub fn add_to(
|
||||||
let transform = transform.to_mat4(true, true, true);
|
node: &Arc<Node>,
|
||||||
|
specialization: InputType,
|
||||||
|
datamap: Option<Datamap>,
|
||||||
|
) -> Result<Arc<InputMethod>> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
node.add_local_signal("capture", InputMethod::capture_flex);
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
node.add_local_signal("set_datamap", InputMethod::set_datamap_flex);
|
||||||
InputMethod::add_to(&node, initial_data, datamap)?;
|
node.add_local_signal("set_handlers", InputMethod::set_handlers_flex);
|
||||||
|
|
||||||
|
let method = InputMethod {
|
||||||
|
node: Arc::downgrade(node),
|
||||||
|
uid: node.uid.clone(),
|
||||||
|
enabled: Mutex::new(true),
|
||||||
|
spatial: node.spatial.get().unwrap().clone(),
|
||||||
|
specialization: Mutex::new(specialization),
|
||||||
|
captures: Registry::new(),
|
||||||
|
datamap: Mutex::new(datamap),
|
||||||
|
handler_aliases: LifeLinkedNodeMap::default(),
|
||||||
|
handler_order: OnceCell::new(),
|
||||||
|
};
|
||||||
|
for handler in INPUT_HANDLER_REGISTRY.get_valid_contents() {
|
||||||
|
method.handle_new_handler(&handler);
|
||||||
|
method.make_alias(&handler);
|
||||||
|
}
|
||||||
|
let method = INPUT_METHOD_REGISTRY.add(method);
|
||||||
|
let _ = node.input_method.set(method.clone());
|
||||||
|
Ok(method)
|
||||||
|
}
|
||||||
|
fn get(node: &Node) -> Result<Arc<Self>> {
|
||||||
|
node.get_aspect("Input Method", "input method", |n| &n.input_method)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn capture_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let method = InputMethod::get(node)?;
|
||||||
|
let handler = InputHandler::find(&calling_client, deserialize(message.as_ref())?)?;
|
||||||
|
|
||||||
|
method.captures.add_raw(&handler);
|
||||||
|
node.send_remote_signal("capture", message)
|
||||||
|
}
|
||||||
|
fn set_datamap_flex(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let method = InputMethod::get(node)?;
|
||||||
|
method.datamap.lock().replace(Datamap::new(message.data)?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn set_handlers_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let method = InputMethod::get(node)?;
|
||||||
|
let handler_paths: Vec<&str> = deserialize(message.as_ref())?;
|
||||||
|
let handlers: Vec<Weak<InputHandler>> = handler_paths
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|p| InputHandler::find(&calling_client, p).ok())
|
||||||
|
.map(|h| Arc::downgrade(&h))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
*method
|
||||||
|
.handler_order
|
||||||
|
.get_or_init(|| Mutex::new(Vec::new()))
|
||||||
|
.lock() = handlers;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Create an input handler node"]
|
fn make_alias(&self, handler: &InputHandler) {
|
||||||
fn create_input_handler(
|
let Some(method_node) = self.node.upgrade() else {return};
|
||||||
_node: Arc<Node>,
|
let Some(handler_node) = handler.node.upgrade() else {return};
|
||||||
calling_client: Arc<Client>,
|
let Some(client) = handler_node.get_client() else {return};
|
||||||
id: u64,
|
let Ok(method_alias) = Alias::create(
|
||||||
parent: Arc<Node>,
|
&client,
|
||||||
transform: Transform,
|
handler_node.get_path(),
|
||||||
field: Arc<Node>,
|
&self.uid,
|
||||||
) -> Result<()> {
|
&method_node,
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
AliasInfo {
|
||||||
let transform = transform.to_mat4(true, true, true);
|
server_signals: vec!["capture"],
|
||||||
let field = field.get_aspect::<Field>()?;
|
..Default::default()
|
||||||
|
},
|
||||||
|
) else {return};
|
||||||
|
method_alias.enabled.store(false, Ordering::Relaxed);
|
||||||
|
handler
|
||||||
|
.method_aliases
|
||||||
|
.add(self as *const InputMethod as usize, &method_alias);
|
||||||
|
}
|
||||||
|
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
fn compare_distance(&self, to: &Field) -> f32 {
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
self.specialization
|
||||||
InputHandler::add_to(&node, &field)?;
|
.lock()
|
||||||
Ok(())
|
.compare_distance(&self.spatial, to)
|
||||||
|
}
|
||||||
|
fn true_distance(&self, to: &Field) -> f32 {
|
||||||
|
self.specialization.lock().true_distance(&self.spatial, to)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_new_handler(&self, handler: &InputHandler) {
|
||||||
|
let Some(method_node) = self.node.upgrade() else {return};
|
||||||
|
let Some(method_client) = method_node.get_client() else {return};
|
||||||
|
let Some(handler_node) = handler.node.upgrade() else {return};
|
||||||
|
// Receiver itself
|
||||||
|
let Ok(handler_alias) = Alias::create(
|
||||||
|
&method_client,
|
||||||
|
method_node.get_path(),
|
||||||
|
handler.uid.as_str(),
|
||||||
|
&handler_node,
|
||||||
|
AliasInfo {
|
||||||
|
server_methods: vec!["getTransform"],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
) else {return};
|
||||||
|
self.handler_aliases
|
||||||
|
.add(handler.uid.clone(), &handler_alias);
|
||||||
|
|
||||||
|
if let Some(handler_field_node) = handler.field.spatial_ref().node.upgrade() {
|
||||||
|
// Handler's field
|
||||||
|
let Ok(rx_field_alias) = Alias::create(
|
||||||
|
&method_client,
|
||||||
|
handler_alias.get_path(),
|
||||||
|
"field",
|
||||||
|
&handler_field_node,
|
||||||
|
FIELD_ALIAS_INFO.clone(),
|
||||||
|
) else {return};
|
||||||
|
self.handler_aliases
|
||||||
|
.add(handler.uid.clone() + "-field", &rx_field_alias);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(data) = serialize(&handler.uid) else {return};
|
||||||
|
let _ = method_node.send_remote_signal("handler_created", data);
|
||||||
|
}
|
||||||
|
fn handle_drop_handler(&self, handler: &InputHandler) {
|
||||||
|
let uid = handler.uid.as_str();
|
||||||
|
self.handler_aliases.remove(uid);
|
||||||
|
self.handler_aliases.remove(&(uid.to_string() + "-field"));
|
||||||
|
let Some(tx_node) = self.node.upgrade() else {return};
|
||||||
|
let Ok(data) = serialize(&uid) else {return};
|
||||||
|
let _ = tx_node.send_remote_signal("handler_destroyed", data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for InputMethod {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
INPUT_METHOD_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct DistanceLink {
|
||||||
|
distance: f32,
|
||||||
|
method: Arc<InputMethod>,
|
||||||
|
handler: Arc<InputHandler>,
|
||||||
|
}
|
||||||
|
impl DistanceLink {
|
||||||
|
fn from(method: Arc<InputMethod>, handler: Arc<InputHandler>) -> Self {
|
||||||
|
DistanceLink {
|
||||||
|
distance: method.compare_distance(&handler.field),
|
||||||
|
method,
|
||||||
|
handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_input(&self, order: u32, datamap: Datamap) {
|
||||||
|
self.handler.send_input(order, self, datamap);
|
||||||
|
}
|
||||||
|
#[instrument(level = "debug", skip(self))]
|
||||||
|
fn serialize(&self, order: u32, datamap: Datamap) -> Vec<u8> {
|
||||||
|
let input = self.method.specialization.lock().serialize(
|
||||||
|
self,
|
||||||
|
Spatial::space_to_space_matrix(Some(&self.method.spatial), Some(&self.handler.spatial)),
|
||||||
|
);
|
||||||
|
|
||||||
|
let root = InputData {
|
||||||
|
uid: self.method.uid.clone(),
|
||||||
|
input,
|
||||||
|
distance: self.method.true_distance(&self.handler.field),
|
||||||
|
datamap,
|
||||||
|
order,
|
||||||
|
};
|
||||||
|
root.serialize()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct InputHandler {
|
||||||
|
enabled: Arc<AtomicBool>,
|
||||||
|
uid: String,
|
||||||
|
node: Weak<Node>,
|
||||||
|
spatial: Arc<Spatial>,
|
||||||
|
field: Arc<Field>,
|
||||||
|
method_aliases: LifeLinkedNodeMap<usize>,
|
||||||
|
}
|
||||||
|
impl InputHandler {
|
||||||
|
pub fn add_to(node: &Arc<Node>, field: &Arc<Field>) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
node.spatial.get().is_some(),
|
||||||
|
"Internal: Node does not have a spatial attached!"
|
||||||
|
);
|
||||||
|
|
||||||
|
let handler = InputHandler {
|
||||||
|
enabled: node.enabled.clone(),
|
||||||
|
uid: node.uid.clone(),
|
||||||
|
node: Arc::downgrade(node),
|
||||||
|
spatial: node.spatial.get().unwrap().clone(),
|
||||||
|
field: field.clone(),
|
||||||
|
method_aliases: LifeLinkedNodeMap::default(),
|
||||||
|
};
|
||||||
|
for method in INPUT_METHOD_REGISTRY.get_valid_contents() {
|
||||||
|
method.make_alias(&handler);
|
||||||
|
method.handle_new_handler(&handler);
|
||||||
|
}
|
||||||
|
let handler = INPUT_HANDLER_REGISTRY.add(handler);
|
||||||
|
let _ = node.input_handler.set(handler);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn find(client: &Client, path: &str) -> Result<Arc<Self>> {
|
||||||
|
InputHandler::get(&*client.get_node("Input Handler", path)?)
|
||||||
|
}
|
||||||
|
fn get(node: &Node) -> Result<Arc<Self>> {
|
||||||
|
node.get_aspect("Input Handler", "input handler", |n| &n.input_handler)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(self, distance_link))]
|
||||||
|
fn send_input(&self, order: u32, distance_link: &DistanceLink, datamap: Datamap) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
let _ = node.send_remote_signal("input", distance_link.serialize(order, datamap));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq for InputHandler {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.spatial == other.spatial
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for InputHandler {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
INPUT_HANDLER_REGISTRY.remove(self);
|
||||||
|
for method in INPUT_METHOD_REGISTRY.get_valid_contents() {
|
||||||
|
method.handle_drop_handler(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "input", false);
|
||||||
|
node.add_local_signal("create_input_handler", create_input_handler_flex);
|
||||||
|
node.add_local_signal("create_input_method_pointer", pointer::create_pointer_flex);
|
||||||
|
node.add_local_signal("create_input_method_tip", tip::create_tip_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_input_handler_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateInputHandlerInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
field_path: &'a str,
|
||||||
|
}
|
||||||
|
let info: CreateInputHandlerInfo = deserialize(message.as_ref())?;
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
let field = find_field(&calling_client, info.field_path)?;
|
||||||
|
|
||||||
|
let node =
|
||||||
|
Node::create(&calling_client, "/input/handler", info.name, true).add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
InputHandler::add_to(&node, &field)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
#[tracing::instrument(level = "debug")]
|
#[tracing::instrument(level = "debug")]
|
||||||
pub fn process_input() {
|
pub fn process_input() {
|
||||||
// Iterate over all valid input methods
|
// Iterate over all valid input methods
|
||||||
let methods = INPUT_METHOD_REGISTRY
|
let methods = debug_span!("Get valid methods").in_scope(|| {
|
||||||
.get_valid_contents()
|
INPUT_METHOD_REGISTRY
|
||||||
.into_iter()
|
.get_valid_contents()
|
||||||
.filter(|method| {
|
.into_iter()
|
||||||
let Some(node) = method.spatial.node() else {
|
.filter(|method| *method.enabled.lock())
|
||||||
return false;
|
.filter(|method| method.datamap.lock().is_some())
|
||||||
};
|
});
|
||||||
node.enabled()
|
let handlers = INPUT_HANDLER_REGISTRY.get_valid_contents();
|
||||||
});
|
const LIMIT: usize = 50;
|
||||||
for handler in INPUT_HANDLER_REGISTRY.get_valid_contents() {
|
|
||||||
for method_alias in handler.method_aliases.get_aliases() {
|
|
||||||
method_alias.set_enabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
let Some(handler_node) = handler.spatial.node() else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
if !handler_node.enabled() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Some(handler_field_node) = handler.field.spatial.node() {
|
|
||||||
if !handler_field_node.enabled() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (methods, datas) = methods
|
|
||||||
.clone()
|
|
||||||
// filter out methods without the handler in their handler order
|
|
||||||
.filter(|a| {
|
|
||||||
a.handler_order
|
|
||||||
.lock()
|
|
||||||
.iter()
|
|
||||||
.any(|h| h.ptr_eq(&Arc::downgrade(&handler)))
|
|
||||||
})
|
|
||||||
// filter out methods without the proper alias
|
|
||||||
.filter_map(|m| {
|
|
||||||
Some((
|
|
||||||
handler
|
|
||||||
.method_aliases
|
|
||||||
.get_from_original_node(m.spatial.node.clone())?,
|
|
||||||
m,
|
|
||||||
))
|
|
||||||
})
|
|
||||||
// make sure the input method alias is enabled
|
|
||||||
.inspect(|(a, _)| {
|
|
||||||
a.set_enabled(true);
|
|
||||||
})
|
|
||||||
// serialize the data
|
|
||||||
.map(|(a, m)| (a.clone(), m.serialize(a.get_id(), &handler)))
|
|
||||||
.unzip::<_, _, Vec<_>, Vec<_>>();
|
|
||||||
|
|
||||||
let _ = input_handler_client::input(&handler_node, &methods, &datas);
|
|
||||||
}
|
|
||||||
for method in methods {
|
for method in methods {
|
||||||
method.internal_capture_requests.clear();
|
for alias in method.node.upgrade().unwrap().aliases.get_valid_contents() {
|
||||||
|
alias.enabled.store(false, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_span!("Process input method").in_scope(|| {
|
||||||
|
// Get all valid input handlers and convert them to DistanceLink objects
|
||||||
|
let distance_links: Vec<DistanceLink> = debug_span!("Generate distance links")
|
||||||
|
.in_scope(|| {
|
||||||
|
if let Some(handler_order) = method.handler_order.get() {
|
||||||
|
let handler_order = handler_order.lock();
|
||||||
|
handler_order
|
||||||
|
.iter()
|
||||||
|
.filter_map(|h| h.upgrade())
|
||||||
|
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
|
||||||
|
.map(|handler| DistanceLink::from(method.clone(), handler))
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
let mut distance_links: Vec<_> = handlers
|
||||||
|
.iter()
|
||||||
|
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
|
||||||
|
.map(|handler| {
|
||||||
|
debug_span!("Create distance link").in_scope(|| {
|
||||||
|
DistanceLink::from(method.clone(), handler.clone())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Sort the distance links by their distance in ascending order
|
||||||
|
debug_span!("Sort distance links").in_scope(|| {
|
||||||
|
distance_links.sort_unstable_by(|a, b| {
|
||||||
|
a.distance.abs().partial_cmp(&b.distance.abs()).unwrap()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
distance_links.truncate(LIMIT);
|
||||||
|
distance_links
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let captures = method.captures.take_valid_contents();
|
||||||
|
// Iterate over the distance links and send input to them
|
||||||
|
for (i, distance_link) in distance_links.into_iter().enumerate() {
|
||||||
|
if i > LIMIT {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(method_alias) = distance_link
|
||||||
|
.handler
|
||||||
|
.method_aliases
|
||||||
|
.get(&(Arc::as_ptr(&distance_link.method) as usize))
|
||||||
|
.and_then(|a| a.alias.get().cloned())
|
||||||
|
{
|
||||||
|
method_alias.enabled.store(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
distance_link.send_input(i as u32, method.datamap.lock().clone().unwrap());
|
||||||
|
|
||||||
|
// If the current distance link is in the list of captured input handlers,
|
||||||
|
// break out of the loop to avoid sending input to the remaining distance links
|
||||||
|
if captures.contains(&distance_link.handler) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,52 +1,92 @@
|
|||||||
use super::{InputDataTrait, InputHandler, InputMethod, Pointer};
|
use super::{DistanceLink, InputSpecialization};
|
||||||
use crate::nodes::{
|
use crate::core::client::Client;
|
||||||
fields::{Field, FieldTrait, Ray, RayMarchResult},
|
use crate::nodes::fields::{Field, Ray, RayMarchResult};
|
||||||
spatial::Spatial,
|
use crate::nodes::input::{InputMethod, InputType};
|
||||||
};
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
use glam::{vec3, Mat4, Quat};
|
use crate::nodes::{Message, Node};
|
||||||
use std::sync::{Arc, Weak};
|
use glam::{vec3, Mat4};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flat::{Datamap, InputDataType, Pointer as FlatPointer};
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
impl Default for Pointer {
|
#[derive(Default)]
|
||||||
fn default() -> Self {
|
pub struct Pointer;
|
||||||
Pointer {
|
// impl Default for Pointer {
|
||||||
origin: [0.0; 3].into(),
|
// fn default() -> Self {
|
||||||
orientation: Quat::IDENTITY.into(),
|
// Pointer {
|
||||||
deepest_point: [0.0; 3].into(),
|
// grab: Default::default(),
|
||||||
}
|
// select: Default::default(),
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
impl Pointer {
|
impl Pointer {
|
||||||
fn ray_march(&self, method_space: &Arc<Spatial>, field: &Field) -> RayMarchResult {
|
fn ray_march(&self, space: &Arc<Spatial>, field: &Field) -> RayMarchResult {
|
||||||
field.ray_march(Ray {
|
field.ray_march(Ray {
|
||||||
origin: vec3(0.0, 0.0, 0.0),
|
origin: vec3(0.0, 0.0, 0.0),
|
||||||
direction: vec3(0.0, 0.0, -1.0),
|
direction: vec3(0.0, 0.0, -1.0),
|
||||||
space: Spatial::new(
|
space: space.clone(),
|
||||||
Weak::new(),
|
|
||||||
Some(method_space.clone()),
|
|
||||||
Mat4::from_rotation_translation(self.orientation.into(), self.origin.into()),
|
|
||||||
),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl InputDataTrait for Pointer {
|
|
||||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
impl InputSpecialization for Pointer {
|
||||||
|
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
|
let ray_info = self.ray_march(space, field);
|
||||||
|
if ray_info.min_distance > 0.0 {
|
||||||
|
ray_info.deepest_point_distance + 1000.0
|
||||||
|
} else {
|
||||||
|
ray_info
|
||||||
|
.deepest_point_distance
|
||||||
|
.hypot(0.001 / ray_info.min_distance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
let ray_info = self.ray_march(space, field);
|
let ray_info = self.ray_march(space, field);
|
||||||
ray_info.min_distance
|
ray_info.min_distance
|
||||||
}
|
}
|
||||||
fn transform(&mut self, method: &InputMethod, handler: &InputHandler) {
|
fn serialize(
|
||||||
let local_to_handler_matrix =
|
&self,
|
||||||
Mat4::from_rotation_translation(self.orientation.into(), self.origin.into())
|
distance_link: &DistanceLink,
|
||||||
* Spatial::space_to_space_matrix(Some(&method.spatial), Some(&handler.spatial));
|
local_to_handler_matrix: Mat4,
|
||||||
|
) -> InputDataType {
|
||||||
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
||||||
|
let direction = local_to_handler_matrix.transform_vector3(vec3(0.0, 0.0, -1.0));
|
||||||
let ray_march = self.ray_march(&method.spatial, &handler.field);
|
let ray_march = self.ray_march(&distance_link.method.spatial, &distance_link.handler.field);
|
||||||
let direction = local_to_handler_matrix
|
|
||||||
.transform_vector3(vec3(0.0, 0.0, -1.0))
|
|
||||||
.normalize();
|
|
||||||
let deepest_point = (direction * ray_march.deepest_point_distance) + origin;
|
let deepest_point = (direction * ray_march.deepest_point_distance) + origin;
|
||||||
|
|
||||||
self.origin = origin.into();
|
InputDataType::Pointer(FlatPointer {
|
||||||
self.orientation = orientation.into();
|
origin: origin.into(),
|
||||||
self.deepest_point = deepest_point.into();
|
orientation: orientation.into(),
|
||||||
|
deepest_point: deepest_point.into(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_pointer_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> color_eyre::eyre::Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreatePointerInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
datamap: Option<Vec<u8>>,
|
||||||
|
}
|
||||||
|
let info: CreatePointerInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/input/method/pointer", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
InputMethod::add_to(
|
||||||
|
&node,
|
||||||
|
InputType::Pointer(Pointer),
|
||||||
|
info.datamap.and_then(|datamap| Datamap::new(datamap).ok()),
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,29 +1,73 @@
|
|||||||
use super::{InputDataTrait, InputHandler, InputMethod, Tip};
|
use super::{DistanceLink, InputSpecialization};
|
||||||
use crate::nodes::{
|
use crate::core::client::Client;
|
||||||
fields::{Field, FieldTrait},
|
use crate::nodes::fields::Field;
|
||||||
spatial::Spatial,
|
use crate::nodes::input::{InputMethod, InputType};
|
||||||
};
|
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
use glam::{Mat4, Quat};
|
use crate::nodes::{Message, Node};
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use glam::{vec3a, Mat4};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flat::{Datamap, InputDataType, Tip as FlatTip};
|
||||||
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
impl Default for Tip {
|
#[derive(Default)]
|
||||||
fn default() -> Self {
|
pub struct Tip {
|
||||||
Tip {
|
pub radius: f32,
|
||||||
origin: [0.0; 3].into(),
|
}
|
||||||
orientation: Quat::IDENTITY.into(),
|
impl Tip {
|
||||||
|
fn set_radius(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
if let InputType::Tip(tip) = &mut *node.input_method.get().unwrap().specialization.lock() {
|
||||||
|
tip.radius = deserialize(message.as_ref())?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl InputDataTrait for Tip {
|
impl InputSpecialization for Tip {
|
||||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
field.distance(space, self.origin.into())
|
field.distance(space, vec3a(0.0, 0.0, 0.0)).abs()
|
||||||
}
|
}
|
||||||
fn transform(&mut self, method: &InputMethod, handler: &InputHandler) {
|
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||||
let local_to_handler_matrix =
|
field.distance(space, vec3a(0.0, 0.0, 0.0))
|
||||||
Spatial::space_to_space_matrix(Some(&method.spatial), Some(&handler.spatial))
|
}
|
||||||
* Mat4::from_rotation_translation(self.orientation.into(), self.origin.into());
|
fn serialize(
|
||||||
|
&self,
|
||||||
|
_distance_link: &DistanceLink,
|
||||||
|
local_to_handler_matrix: Mat4,
|
||||||
|
) -> InputDataType {
|
||||||
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
||||||
self.origin = origin.into();
|
InputDataType::Tip(FlatTip {
|
||||||
self.orientation = orientation.into();
|
origin: origin.into(),
|
||||||
|
orientation: orientation.into(),
|
||||||
|
radius: self.radius,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_tip_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateTipInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
radius: f32,
|
||||||
|
datamap: Option<Vec<u8>>,
|
||||||
|
}
|
||||||
|
let info: CreateTipInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/input/method/tip", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
InputMethod::add_to(
|
||||||
|
&node,
|
||||||
|
InputType::Tip(Tip {
|
||||||
|
radius: info.radius,
|
||||||
|
}),
|
||||||
|
info.datamap.and_then(|datamap| Datamap::new(datamap).ok()),
|
||||||
|
)?;
|
||||||
|
node.add_local_signal("set_radius", Tip::set_radius);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,231 +1,357 @@
|
|||||||
use super::{
|
use super::{Item, ItemType};
|
||||||
create_item_acceptor_flex, register_item_ui_flex, Item, ItemAcceptor, ItemInterface, ItemType,
|
|
||||||
};
|
|
||||||
use crate::nodes::items::ITEM_ACCEPTOR_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::nodes::items::ITEM_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{client::Client, registry::Registry, scenegraph::MethodResponseSender},
|
core::{
|
||||||
create_interface,
|
buffers::BufferManager,
|
||||||
|
client::{Client, INTERNAL_CLIENT},
|
||||||
|
registry::Registry,
|
||||||
|
scenegraph::MethodResponseSender,
|
||||||
|
},
|
||||||
nodes::{
|
nodes::{
|
||||||
drawable::{
|
drawable::{model::ModelPart, shaders::UNLIT_SHADER_BYTES, Drawable},
|
||||||
model::{MaterialWrapper, ModelPart},
|
|
||||||
shaders::UNLIT_SHADER_BYTES,
|
|
||||||
},
|
|
||||||
items::TypeInfo,
|
items::TypeInfo,
|
||||||
spatial::{Spatial, Transform},
|
spatial::{find_spatial_parent, parse_transform, Spatial},
|
||||||
Message, Node,
|
Message, Node,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::{bail, eyre, Result};
|
use color_eyre::eyre::{bail, Result};
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use mint::{ColumnMatrix4, Vector2};
|
use mint::{RowMatrix4, Vector2};
|
||||||
|
use nanoid::nanoid;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use serde::Deserialize;
|
||||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
use smithay::backend::{
|
||||||
use std::sync::Arc;
|
allocator::{
|
||||||
use stereokit_rust::{
|
dmabuf::{Dmabuf, DmabufFlags},
|
||||||
material::{Material, Transparency},
|
Buffer,
|
||||||
shader::Shader,
|
},
|
||||||
sk::MainThreadToken,
|
renderer::ImportDma,
|
||||||
system::Renderer,
|
|
||||||
tex::{Tex, TexFormat, TexType},
|
|
||||||
util::Color128,
|
|
||||||
};
|
};
|
||||||
use tracing::error;
|
use stardust_xr::{
|
||||||
|
scenegraph::ScenegraphError,
|
||||||
|
schemas::flex::{deserialize, serialize},
|
||||||
|
values::{BufferInfo, Transform},
|
||||||
|
};
|
||||||
|
use std::{ffi::c_void, sync::Arc};
|
||||||
|
use stereokit::{
|
||||||
|
Color128, Material, Rect, RenderLayer, StereoKitDraw, Tex, TextureFormat, TextureType,
|
||||||
|
Transparency,
|
||||||
|
};
|
||||||
|
use tokio::sync::mpsc::{self, error::SendError};
|
||||||
|
|
||||||
pub struct TexWrapper(pub Tex);
|
|
||||||
unsafe impl Send for TexWrapper {}
|
|
||||||
unsafe impl Sync for TexWrapper {}
|
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_item_camera_protocol!();
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub(super) static ref ITEM_TYPE_INFO_CAMERA: TypeInfo = TypeInfo {
|
pub(super) static ref ITEM_TYPE_INFO_CAMERA: TypeInfo = TypeInfo {
|
||||||
type_name: "camera",
|
type_name: "camera",
|
||||||
alias_info: CAMERA_ITEM_ASPECT_ALIAS_INFO.clone(),
|
aliased_local_signals: vec!["apply_preview_material", "set_proj_matrix"],
|
||||||
ui_node_id: INTERFACE_NODE_ID,
|
aliased_local_methods: vec!["render"],
|
||||||
|
aliased_remote_signals: vec![],
|
||||||
ui: Default::default(),
|
ui: Default::default(),
|
||||||
items: Registry::new(),
|
items: Registry::new(),
|
||||||
acceptors: Registry::new(),
|
acceptors: Registry::new(),
|
||||||
new_acceptor_fn: |node, acceptor, acceptor_field| {
|
|
||||||
let _ = camera_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FrameInfo {
|
struct FrameInfo {
|
||||||
proj_matrix: Mat4,
|
proj_matrix: Mat4,
|
||||||
px_size: Vector2<u32>,
|
preview_size: Vector2<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CameraItem {
|
pub struct CameraItem {
|
||||||
space: Arc<Spatial>,
|
space: Arc<Spatial>,
|
||||||
frame_info: Mutex<FrameInfo>,
|
frame_info: Mutex<FrameInfo>,
|
||||||
sk_tex: OnceCell<TexWrapper>,
|
sk_tex: OnceCell<Tex>,
|
||||||
sk_mat: OnceCell<Arc<MaterialWrapper>>,
|
sk_mat: OnceCell<Arc<Material>>,
|
||||||
applied_to: Registry<ModelPart>,
|
render_requests_tx: mpsc::UnboundedSender<(Dmabuf, MethodResponseSender)>,
|
||||||
apply_to: Registry<ModelPart>,
|
render_requests_rx: Mutex<mpsc::UnboundedReceiver<(Dmabuf, MethodResponseSender)>>,
|
||||||
|
rendered_notifiers: Mutex<Vec<MethodResponseSender>>,
|
||||||
|
applied_preview_to: Registry<ModelPart>,
|
||||||
|
apply_preview_to: Registry<ModelPart>,
|
||||||
}
|
}
|
||||||
#[allow(unused)]
|
|
||||||
impl CameraItem {
|
impl CameraItem {
|
||||||
pub fn add_to(node: &Arc<Node>, proj_matrix: Mat4, px_size: Vector2<u32>) {
|
pub fn add_to(node: &Arc<Node>, proj_matrix: Mat4, preview_size: Vector2<u32>) {
|
||||||
|
let (render_requests_tx, render_requests_rx) = mpsc::unbounded_channel();
|
||||||
|
let camera_specialization = CameraItem {
|
||||||
|
space: node.spatial.get().unwrap().clone(),
|
||||||
|
frame_info: Mutex::new(FrameInfo {
|
||||||
|
proj_matrix,
|
||||||
|
preview_size,
|
||||||
|
}),
|
||||||
|
sk_tex: OnceCell::new(),
|
||||||
|
sk_mat: OnceCell::new(),
|
||||||
|
render_requests_tx,
|
||||||
|
render_requests_rx: Mutex::new(render_requests_rx),
|
||||||
|
rendered_notifiers: Mutex::new(Vec::new()),
|
||||||
|
applied_preview_to: Registry::new(),
|
||||||
|
apply_preview_to: Registry::new(),
|
||||||
|
};
|
||||||
Item::add_to(
|
Item::add_to(
|
||||||
node,
|
node,
|
||||||
|
nanoid!(),
|
||||||
&ITEM_TYPE_INFO_CAMERA,
|
&ITEM_TYPE_INFO_CAMERA,
|
||||||
ItemType::Camera(CameraItem {
|
ItemType::Camera(camera_specialization),
|
||||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
|
||||||
frame_info: Mutex::new(FrameInfo {
|
|
||||||
proj_matrix,
|
|
||||||
px_size,
|
|
||||||
}),
|
|
||||||
sk_tex: OnceCell::new(),
|
|
||||||
sk_mat: OnceCell::new(),
|
|
||||||
applied_to: Registry::new(),
|
|
||||||
apply_to: Registry::new(),
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
// <CameraItem as CameraItemAspect>::node_methods(node);
|
node.add_local_method("render", CameraItem::render_flex);
|
||||||
|
node.add_local_signal(
|
||||||
|
"apply_preview_material",
|
||||||
|
CameraItem::apply_preview_material_flex,
|
||||||
|
);
|
||||||
|
node.add_local_signal("set_proj_matrix", CameraItem::set_proj_matrix_flex);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame_flex(
|
fn render_flex(
|
||||||
node: Arc<Node>,
|
node: &Node,
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
_message: Message,
|
message: Message,
|
||||||
response: MethodResponseSender,
|
response: MethodResponseSender,
|
||||||
) {
|
) {
|
||||||
response.wrap_sync(move || {
|
let Some(item) = node.item.get() else {
|
||||||
let ItemType::Camera(_camera) = &node.get_aspect::<Item>().unwrap().specialization
|
let _ = response.send(Err(ScenegraphError::MethodError {
|
||||||
else {
|
error: "Item not found".to_string(),
|
||||||
return Err(eyre!("Wrong item type?"));
|
}));
|
||||||
};
|
return;
|
||||||
Ok(serialize(())?.into())
|
};
|
||||||
});
|
let ItemType::Camera(camera) = &item.specialization else {
|
||||||
|
let _ = response.send(Err(ScenegraphError::MethodError {
|
||||||
|
error: "Wrong item type?".to_string(),
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let buffer_info: BufferInfo = match deserialize(&message.data) {
|
||||||
|
Ok(i) => i,
|
||||||
|
Err(e) => {
|
||||||
|
let _ = response.send(Err(ScenegraphError::MethodError {
|
||||||
|
error: e.to_string(),
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut builder = Dmabuf::builder(
|
||||||
|
(buffer_info.width as i32, buffer_info.height as i32),
|
||||||
|
buffer_info.fourcc.try_into().unwrap(),
|
||||||
|
DmabufFlags::from_bits_truncate(buffer_info.flags),
|
||||||
|
);
|
||||||
|
for (fd, plane) in message.fds.into_iter().zip(buffer_info.planes) {
|
||||||
|
builder.add_plane(
|
||||||
|
fd,
|
||||||
|
plane.idx,
|
||||||
|
plane.offset,
|
||||||
|
plane.stride,
|
||||||
|
plane.modifier.try_into().unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let buffer_to_render = builder.build().unwrap();
|
||||||
|
|
||||||
|
if let Err(SendError((_dmabuf, sender))) =
|
||||||
|
camera.render_requests_tx.send((buffer_to_render, response))
|
||||||
|
{
|
||||||
|
sender.send(Err(ScenegraphError::MethodError {
|
||||||
|
error: "Internal: sender broke????".to_string(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_preview_material_flex(
|
fn apply_preview_material_flex(
|
||||||
node: Arc<Node>,
|
node: &Node,
|
||||||
calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
message: Message,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let ItemType::Camera(camera) = &node.get_aspect::<Item>().unwrap().specialization else {
|
let Some(item) = node.item.get() else {
|
||||||
|
bail!("Item not found?")
|
||||||
|
};
|
||||||
|
let ItemType::Camera(camera) = &item.specialization else {
|
||||||
bail!("Wrong item type?")
|
bail!("Wrong item type?")
|
||||||
};
|
};
|
||||||
let model_part_node =
|
|
||||||
calling_client.get_node("Model part", deserialize(&message.data).unwrap())?;
|
let model_part_path = deserialize(&message.data)?;
|
||||||
let model_part = model_part_node.get_aspect::<ModelPart>()?;
|
let model_part_node = calling_client.get_node("Model part", model_part_path)?;
|
||||||
camera.applied_to.add_raw(&model_part);
|
let Drawable::ModelPart(model_part) =
|
||||||
camera.apply_to.add_raw(&model_part);
|
model_part_node.get_aspect("Model part", "model part", |n| &n.drawable)?
|
||||||
|
else {
|
||||||
|
bail!("Drawable is not a model node")
|
||||||
|
};
|
||||||
|
|
||||||
|
camera.applied_preview_to.add_raw(model_part);
|
||||||
|
camera.apply_preview_to.add_raw(model_part);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_ui_item_created(&self, node: &Node, item: &Arc<Node>) {
|
fn set_proj_matrix_flex(
|
||||||
let _ = camera_item_ui_client::create_item(node, item);
|
node: &Node,
|
||||||
}
|
_calling_client: Arc<Client>,
|
||||||
pub fn send_acceptor_item_created(&self, node: &Node, item: &Arc<Node>) {
|
message: Message,
|
||||||
let _ = camera_item_acceptor_client::capture_item(node, item);
|
) -> Result<()> {
|
||||||
|
let Some(item) = node.item.get() else {
|
||||||
|
bail!("Item not found?")
|
||||||
|
};
|
||||||
|
let ItemType::Camera(camera) = &item.specialization else {
|
||||||
|
bail!("Wrong item type?")
|
||||||
|
};
|
||||||
|
|
||||||
|
let proj_matrix: RowMatrix4<f32> = deserialize(&message.data)?;
|
||||||
|
|
||||||
|
let mut frame_info = camera.frame_info.lock();
|
||||||
|
frame_info.proj_matrix = proj_matrix.into();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&self, token: &MainThreadToken) {
|
pub fn serialize_start_data(&self, id: &str) -> Result<Message> {
|
||||||
|
Ok(serialize(id)?.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&self, sk: &impl StereoKitDraw, buffer_manager: &mut BufferManager) {
|
||||||
let frame_info = self.frame_info.lock();
|
let frame_info = self.frame_info.lock();
|
||||||
let sk_tex = self.sk_tex.get_or_init(|| {
|
self.render_preview(sk, &*frame_info);
|
||||||
TexWrapper(Tex::gen_color(
|
self.render_dmabuf(sk, buffer_manager, &*frame_info);
|
||||||
Color128::default(),
|
}
|
||||||
frame_info.px_size.x as i32,
|
|
||||||
frame_info.px_size.y as i32,
|
fn render_preview(&self, sk: &impl StereoKitDraw, frame_info: &FrameInfo) {
|
||||||
TexType::Rendertarget,
|
if !self.apply_preview_to.is_empty() {
|
||||||
TexFormat::RGBA32Linear,
|
let sk_tex = self.sk_tex.get_or_init(|| {
|
||||||
))
|
sk.tex_gen_color(
|
||||||
});
|
Color128::default(),
|
||||||
let sk_mat = self
|
frame_info.preview_size.x as i32,
|
||||||
.sk_mat
|
frame_info.preview_size.y as i32,
|
||||||
.get_or_try_init(|| -> Result<Arc<MaterialWrapper>> {
|
TextureType::RENDER_TARGET,
|
||||||
let shader = Shader::from_memory(UNLIT_SHADER_BYTES)?;
|
TextureFormat::RGBA32Linear,
|
||||||
let mut mat = Material::new(&shader, None);
|
)
|
||||||
mat.get_all_param_info().set_texture("diffuse", &sk_tex.0);
|
|
||||||
mat.transparency(Transparency::Blend);
|
|
||||||
Ok(Arc::new(MaterialWrapper(mat)))
|
|
||||||
});
|
});
|
||||||
let Ok(sk_mat) = sk_mat else {
|
let sk_mat = self.sk_mat.get_or_init(|| {
|
||||||
error!("unable to make camera item stereokit texture");
|
let shader = sk.shader_create_mem(&UNLIT_SHADER_BYTES).unwrap();
|
||||||
return;
|
let mat = sk.material_create(&shader);
|
||||||
};
|
sk.material_set_texture(&mat, "diffuse", sk_tex.as_ref());
|
||||||
for model_part in self.apply_to.take_valid_contents() {
|
sk.material_set_transparency(&mat, Transparency::Blend);
|
||||||
model_part.replace_material(sk_mat.clone())
|
Arc::new(mat)
|
||||||
|
});
|
||||||
|
for model_part in self.apply_preview_to.take_valid_contents() {
|
||||||
|
model_part.replace_material(sk_mat.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.applied_to.is_empty() {
|
if !self.applied_preview_to.is_empty() {
|
||||||
Renderer::render_to(
|
sk.render_to(
|
||||||
token,
|
self.sk_tex.get().unwrap(),
|
||||||
&sk_tex.0,
|
|
||||||
self.space.global_transform(),
|
self.space.global_transform(),
|
||||||
frame_info.proj_matrix,
|
frame_info.proj_matrix,
|
||||||
None,
|
RenderLayer::all(),
|
||||||
None,
|
stereokit::RenderClear::All,
|
||||||
None,
|
Rect {
|
||||||
)
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
w: 0.0,
|
||||||
|
h: 0.0,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_dmabuf(
|
||||||
|
&self,
|
||||||
|
sk: &impl StereoKitDraw,
|
||||||
|
buffer_manager: &mut BufferManager,
|
||||||
|
frame_info: &FrameInfo,
|
||||||
|
) {
|
||||||
|
let mut render_notifiers = self.rendered_notifiers.lock();
|
||||||
|
let mut render_requests_rx = self.render_requests_rx.lock();
|
||||||
|
while let Ok((buffer_to_render, render_response_sender)) = render_requests_rx.try_recv() {
|
||||||
|
let imported_dmabuf = buffer_manager
|
||||||
|
.renderer
|
||||||
|
.import_dmabuf(&buffer_to_render, None);
|
||||||
|
let smithay_tex = match imported_dmabuf {
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(e) => {
|
||||||
|
let _ = render_response_sender.send(Err(ScenegraphError::MethodError {
|
||||||
|
error: e.to_string(),
|
||||||
|
}));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let sk_tex = sk.tex_create(TextureType::IMAGE_NO_MIPS, TextureFormat::RGBA32);
|
||||||
|
unsafe {
|
||||||
|
sk.tex_set_surface(
|
||||||
|
&sk_tex,
|
||||||
|
smithay_tex.tex_id() as usize as *mut c_void,
|
||||||
|
TextureType::RENDER_TARGET,
|
||||||
|
smithay::backend::renderer::gles::ffi::SRGB8_ALPHA8.into(),
|
||||||
|
buffer_to_render.size().w,
|
||||||
|
buffer_to_render.size().h,
|
||||||
|
1,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sk.render_to(
|
||||||
|
sk_tex,
|
||||||
|
self.space.global_transform(),
|
||||||
|
frame_info.proj_matrix,
|
||||||
|
RenderLayer::all(),
|
||||||
|
stereokit::RenderClear::All,
|
||||||
|
Rect {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
w: 0.0,
|
||||||
|
h: 0.0,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
render_notifiers.push(render_response_sender);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_rendered(&self) {
|
||||||
|
for notifier in self.rendered_notifiers.lock().drain(..) {
|
||||||
|
let _ = notifier.send(Ok(Vec::new().into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl CameraItemAspect for CameraItem {}
|
|
||||||
|
|
||||||
impl CameraItemAcceptorAspect for ItemAcceptor {
|
pub fn update(sk: &impl StereoKitDraw, buffer_manager: &mut BufferManager) {
|
||||||
fn capture_item(node: Arc<Node>, _calling_client: Arc<Client>, item: Arc<Node>) -> Result<()> {
|
|
||||||
super::acceptor_capture_item_flex(node, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(token: &MainThreadToken) {
|
|
||||||
for camera in ITEM_TYPE_INFO_CAMERA.items.get_valid_contents() {
|
for camera in ITEM_TYPE_INFO_CAMERA.items.get_valid_contents() {
|
||||||
let ItemType::Camera(camera) = &camera.specialization else {
|
let ItemType::Camera(camera) = &camera.specialization else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
camera.update(token);
|
camera.update(sk, buffer_manager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
create_interface!(ItemInterface);
|
pub fn send_rendered() {
|
||||||
impl InterfaceAspect for ItemInterface {
|
for camera in ITEM_TYPE_INFO_CAMERA.items.get_valid_contents() {
|
||||||
#[doc = "Create a camera item at a specific location"]
|
let ItemType::Camera(camera) = &camera.specialization else {
|
||||||
fn create_camera_item(
|
continue;
|
||||||
_node: Arc<Node>,
|
};
|
||||||
calling_client: Arc<Client>,
|
camera.send_rendered();
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
proj_matrix: ColumnMatrix4<f32>,
|
|
||||||
px_size: Vector2<u32>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let space = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, false);
|
|
||||||
|
|
||||||
let node = Node::from_id(&calling_client, id, false).add_to_scenegraph()?;
|
|
||||||
Spatial::add_to(&node, None, transform * space.global_transform(), false);
|
|
||||||
CameraItem::add_to(&node, proj_matrix.into(), px_size);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Register this client to manage camera items and create default 3D UI for them."]
|
|
||||||
fn register_camera_item_ui(_node: Arc<Node>, calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
register_item_ui_flex(calling_client, &ITEM_TYPE_INFO_CAMERA)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Create an item acceptor to allow temporary ownership of a given type of item. Creates a node at `/item/camera/acceptor/<name>`."]
|
|
||||||
fn create_camera_item_acceptor(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
field: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
create_item_acceptor_flex(
|
|
||||||
calling_client,
|
|
||||||
id,
|
|
||||||
parent,
|
|
||||||
transform,
|
|
||||||
&ITEM_TYPE_INFO_CAMERA,
|
|
||||||
field,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn create_camera_item_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateCameraItemInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
proj_matrix: RowMatrix4<f32>,
|
||||||
|
preview_size: Vector2<u32>,
|
||||||
|
}
|
||||||
|
let info: CreateCameraItemInfo = deserialize(message.as_ref())?;
|
||||||
|
let parent_name = format!("/item/{}/item", ITEM_TYPE_INFO_CAMERA.type_name);
|
||||||
|
let space = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
|
||||||
|
let node =
|
||||||
|
Node::create(&INTERNAL_CLIENT, &parent_name, info.name, false).add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, None, transform * space.global_transform(), false)?;
|
||||||
|
CameraItem::add_to(&node, info.proj_matrix.into(), info.preview_size);
|
||||||
|
// TODO: this means ANY client can render anywhere with no limits, this needs to be changed!!
|
||||||
|
node.item
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.make_alias_named(&calling_client, &parent_name, info.name)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
95
src/nodes/items/environment.rs
Normal file
95
src/nodes/items/environment.rs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
use super::{Item, ItemType};
|
||||||
|
use crate::{
|
||||||
|
core::{
|
||||||
|
client::{Client, INTERNAL_CLIENT},
|
||||||
|
registry::Registry,
|
||||||
|
scenegraph::MethodResponseSender,
|
||||||
|
},
|
||||||
|
nodes::{
|
||||||
|
items::TypeInfo,
|
||||||
|
spatial::{find_spatial_parent, parse_transform, Spatial},
|
||||||
|
Message, Node,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use color_eyre::eyre::{eyre, Result};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use nanoid::nanoid;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::{
|
||||||
|
schemas::flex::{deserialize, serialize},
|
||||||
|
values::Transform,
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub(super) static ref ITEM_TYPE_INFO_ENVIRONMENT: TypeInfo = TypeInfo {
|
||||||
|
type_name: "environment",
|
||||||
|
aliased_local_signals: vec!["apply_sky_tex", "apply_sky_light"],
|
||||||
|
aliased_local_methods: vec![],
|
||||||
|
aliased_remote_signals: vec![],
|
||||||
|
ui: Default::default(),
|
||||||
|
items: Registry::new(),
|
||||||
|
acceptors: Registry::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EnvironmentItem {
|
||||||
|
path: String,
|
||||||
|
}
|
||||||
|
impl EnvironmentItem {
|
||||||
|
pub fn add_to(node: &Arc<Node>, path: String) {
|
||||||
|
Item::add_to(
|
||||||
|
node,
|
||||||
|
nanoid!(),
|
||||||
|
&ITEM_TYPE_INFO_ENVIRONMENT,
|
||||||
|
ItemType::Environment(EnvironmentItem { path }),
|
||||||
|
);
|
||||||
|
node.add_local_method("get_path", EnvironmentItem::get_path_flex);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_path_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
_message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let ItemType::Environment(environment_item) = &node.item.get().unwrap().specialization else {
|
||||||
|
return Err(eyre!("Wrong item type?"))
|
||||||
|
};
|
||||||
|
Ok(serialize(environment_item.path.as_str())?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialize_start_data(&self, id: &str) -> Result<Message> {
|
||||||
|
Ok(serialize((id, self.path.as_str()))?.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn create_environment_item_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateEnvironmentItemInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
item_data: String,
|
||||||
|
}
|
||||||
|
let info: CreateEnvironmentItemInfo = deserialize(message.as_ref())?;
|
||||||
|
let parent_name = format!("/item/{}/item", ITEM_TYPE_INFO_ENVIRONMENT.type_name);
|
||||||
|
let space = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
|
||||||
|
let node =
|
||||||
|
Node::create(&INTERNAL_CLIENT, &parent_name, info.name, false).add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, None, transform * space.global_transform(), false)?;
|
||||||
|
EnvironmentItem::add_to(&node, info.item_data);
|
||||||
|
node.item
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.make_alias_named(&calling_client, &parent_name, info.name)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,25 +1,44 @@
|
|||||||
pub mod camera;
|
pub mod camera;
|
||||||
|
mod environment;
|
||||||
pub mod panel;
|
pub mod panel;
|
||||||
|
|
||||||
use self::camera::CameraItem;
|
use self::camera::CameraItem;
|
||||||
use self::panel::PanelItemTrait;
|
use self::environment::{EnvironmentItem, ITEM_TYPE_INFO_ENVIRONMENT};
|
||||||
use super::alias::AliasList;
|
use self::panel::{PanelItemTrait, ITEM_TYPE_INFO_PANEL};
|
||||||
use super::fields::{Field, FIELD_ALIAS_INFO};
|
use super::fields::Field;
|
||||||
use super::spatial::Spatial;
|
use super::spatial::{find_spatial_parent, parse_transform, Spatial};
|
||||||
use super::{Alias, Aspect, Node};
|
use super::{Alias, Message, Node};
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use crate::core::node_collections::LifeLinkedNodeMap;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::nodes::alias::AliasInfo;
|
use crate::nodes::alias::AliasInfo;
|
||||||
use crate::nodes::spatial::Transform;
|
use crate::nodes::fields::find_field;
|
||||||
use crate::nodes::spatial::SPATIAL_ASPECT_ALIAS_INFO;
|
use color_eyre::eyre::{ensure, eyre, Result};
|
||||||
use color_eyre::eyre::{ensure, Result};
|
use lazy_static::lazy_static;
|
||||||
|
use nanoid::nanoid;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use portable_atomic::Ordering;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_item_protocol!();
|
lazy_static! {
|
||||||
|
static ref ITEM_ALIAS_LOCAL_SIGNALS: Vec<&'static str> = vec![
|
||||||
|
"get_bounds",
|
||||||
|
"get_transform",
|
||||||
|
"set_transform",
|
||||||
|
"set_spatial_parent",
|
||||||
|
"set_spatial_parent_in_place",
|
||||||
|
"set_zoneable",
|
||||||
|
"release",
|
||||||
|
];
|
||||||
|
static ref ITEM_ALIAS_LOCAL_METHODS: Vec<&'static str> = vec![];
|
||||||
|
static ref ITEM_ALIAS_REMOTE_SIGNALS: Vec<&'static str> = vec![];
|
||||||
|
}
|
||||||
|
|
||||||
fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
|
pub fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
|
||||||
if item.captured_acceptor.lock().strong_count() > 0 {
|
if item.captured_acceptor.lock().strong_count() > 0 {
|
||||||
release(item);
|
release(item);
|
||||||
}
|
}
|
||||||
@@ -35,19 +54,19 @@ fn release(item: &Item) {
|
|||||||
*captured_acceptor = Weak::default();
|
*captured_acceptor = Weak::default();
|
||||||
acceptor.handle_release(item);
|
acceptor.handle_release(item);
|
||||||
if let Some(ui) = item.type_info.ui.lock().upgrade() {
|
if let Some(ui) = item.type_info.ui.lock().upgrade() {
|
||||||
ui.handle_release_item(item, acceptor);
|
ui.handle_release_item(item, &acceptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TypeInfo {
|
pub struct TypeInfo {
|
||||||
pub type_name: &'static str,
|
pub type_name: &'static str,
|
||||||
pub alias_info: AliasInfo,
|
pub aliased_local_signals: Vec<&'static str>,
|
||||||
pub ui_node_id: u64,
|
pub aliased_local_methods: Vec<&'static str>,
|
||||||
|
pub aliased_remote_signals: Vec<&'static str>,
|
||||||
pub ui: Mutex<Weak<ItemUI>>,
|
pub ui: Mutex<Weak<ItemUI>>,
|
||||||
pub items: Registry<Item>,
|
pub items: Registry<Item>,
|
||||||
pub acceptors: Registry<ItemAcceptor>,
|
pub acceptors: Registry<ItemAcceptor>,
|
||||||
pub new_acceptor_fn: fn(node: &Node, acceptor: &Arc<Node>, acceptor_field: &Arc<Node>),
|
|
||||||
}
|
}
|
||||||
impl Hash for TypeInfo {
|
impl Hash for TypeInfo {
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||||
@@ -62,7 +81,8 @@ impl PartialEq for TypeInfo {
|
|||||||
impl Eq for TypeInfo {}
|
impl Eq for TypeInfo {}
|
||||||
|
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
spatial: Arc<Spatial>,
|
node: Weak<Node>,
|
||||||
|
uid: String,
|
||||||
type_info: &'static TypeInfo,
|
type_info: &'static TypeInfo,
|
||||||
captured_acceptor: Mutex<Weak<ItemAcceptor>>,
|
captured_acceptor: Mutex<Weak<ItemAcceptor>>,
|
||||||
pub specialization: ItemType,
|
pub specialization: ItemType,
|
||||||
@@ -70,22 +90,24 @@ pub struct Item {
|
|||||||
impl Item {
|
impl Item {
|
||||||
pub fn add_to(
|
pub fn add_to(
|
||||||
node: &Arc<Node>,
|
node: &Arc<Node>,
|
||||||
|
uid: String,
|
||||||
type_info: &'static TypeInfo,
|
type_info: &'static TypeInfo,
|
||||||
specialization: ItemType,
|
specialization: ItemType,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
let item = Item {
|
let item = Item {
|
||||||
spatial: node.aspects.get::<Spatial>().unwrap(),
|
node: Arc::downgrade(node),
|
||||||
|
uid,
|
||||||
type_info,
|
type_info,
|
||||||
captured_acceptor: Default::default(),
|
captured_acceptor: Default::default(),
|
||||||
specialization,
|
specialization,
|
||||||
};
|
};
|
||||||
let item = type_info.items.add(item);
|
let item = type_info.items.add(item);
|
||||||
|
|
||||||
<Item as ItemAspect>::add_node_members(node);
|
node.add_local_signal("release", Item::release_flex);
|
||||||
if let Some(ui) = type_info.ui.lock().upgrade() {
|
if let Some(ui) = type_info.ui.lock().upgrade() {
|
||||||
ui.handle_create_item(&item);
|
ui.handle_create_item(&item);
|
||||||
}
|
}
|
||||||
node.add_aspect_raw(item.clone());
|
let _ = node.item.set(item.clone());
|
||||||
|
|
||||||
// if let Some(auto_acceptor) = node.get_client().and_then(|client| {
|
// if let Some(auto_acceptor) = node.get_client().and_then(|client| {
|
||||||
// client
|
// client
|
||||||
@@ -99,22 +121,44 @@ impl Item {
|
|||||||
|
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
fn make_alias(&self, client: &Arc<Client>, alias_list: &AliasList) -> Result<Arc<Node>> {
|
fn make_alias_named(
|
||||||
|
&self,
|
||||||
|
client: &Arc<Client>,
|
||||||
|
parent: &str,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Arc<Node>> {
|
||||||
Alias::create(
|
Alias::create(
|
||||||
&self.spatial.node().unwrap(),
|
|
||||||
client,
|
client,
|
||||||
self.type_info.alias_info.clone() + ITEM_ASPECT_ALIAS_INFO.clone(),
|
parent,
|
||||||
Some(alias_list),
|
name,
|
||||||
|
&self.node.upgrade().unwrap(),
|
||||||
|
AliasInfo {
|
||||||
|
server_signals: [
|
||||||
|
&self.type_info.aliased_local_signals,
|
||||||
|
ITEM_ALIAS_LOCAL_SIGNALS.as_slice(),
|
||||||
|
]
|
||||||
|
.concat(),
|
||||||
|
server_methods: [
|
||||||
|
&self.type_info.aliased_local_methods,
|
||||||
|
ITEM_ALIAS_LOCAL_METHODS.as_slice(),
|
||||||
|
]
|
||||||
|
.concat(),
|
||||||
|
client_signals: [
|
||||||
|
&self.type_info.aliased_remote_signals,
|
||||||
|
ITEM_ALIAS_REMOTE_SIGNALS.as_slice(),
|
||||||
|
]
|
||||||
|
.concat(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
fn make_alias(&self, client: &Arc<Client>, parent: &str) -> Result<Arc<Node>> {
|
||||||
impl Aspect for Item {
|
self.make_alias_named(client, parent, &self.uid)
|
||||||
const NAME: &'static str = "Item";
|
}
|
||||||
}
|
|
||||||
impl ItemAspect for Item {
|
fn release_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
|
||||||
fn release(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
let item = node.get_aspect("Item", "item", |n| &n.item)?;
|
||||||
let item = node.get_aspect::<Item>()?;
|
release(item);
|
||||||
release(&item);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,19 +174,15 @@ impl Drop for Item {
|
|||||||
|
|
||||||
pub enum ItemType {
|
pub enum ItemType {
|
||||||
Camera(CameraItem),
|
Camera(CameraItem),
|
||||||
|
Environment(EnvironmentItem),
|
||||||
Panel(Arc<dyn PanelItemTrait>),
|
Panel(Arc<dyn PanelItemTrait>),
|
||||||
}
|
}
|
||||||
impl ItemType {
|
impl ItemType {
|
||||||
fn send_ui_item_created(&self, node: &Node, item: &Arc<Node>) {
|
fn serialize_start_data(&self, id: &str) -> Result<Message> {
|
||||||
match self {
|
match self {
|
||||||
ItemType::Camera(c) => c.send_ui_item_created(node, item),
|
ItemType::Camera(c) => c.serialize_start_data(id),
|
||||||
ItemType::Panel(p) => p.send_ui_item_created(node, item),
|
ItemType::Environment(e) => e.serialize_start_data(id),
|
||||||
}
|
ItemType::Panel(p) => p.serialize_start_data(id),
|
||||||
}
|
|
||||||
fn send_acceptor_item_created(&self, node: &Node, item: &Arc<Node>) {
|
|
||||||
match self {
|
|
||||||
ItemType::Camera(c) => c.send_acceptor_item_created(node, item),
|
|
||||||
ItemType::Panel(p) => p.send_acceptor_item_created(node, item),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,9 +200,9 @@ impl ItemType {
|
|||||||
pub struct ItemUI {
|
pub struct ItemUI {
|
||||||
node: Weak<Node>,
|
node: Weak<Node>,
|
||||||
type_info: &'static TypeInfo,
|
type_info: &'static TypeInfo,
|
||||||
item_aliases: AliasList,
|
item_aliases: LifeLinkedNodeMap<String>,
|
||||||
acceptor_aliases: AliasList,
|
acceptor_aliases: LifeLinkedNodeMap<String>,
|
||||||
acceptor_field_aliases: AliasList,
|
acceptor_field_aliases: LifeLinkedNodeMap<String>,
|
||||||
}
|
}
|
||||||
impl ItemUI {
|
impl ItemUI {
|
||||||
fn add_to(node: &Arc<Node>, type_info: &'static TypeInfo) -> Result<()> {
|
fn add_to(node: &Arc<Node>, type_info: &'static TypeInfo) -> Result<()> {
|
||||||
@@ -174,12 +214,12 @@ impl ItemUI {
|
|||||||
let ui = Arc::new(ItemUI {
|
let ui = Arc::new(ItemUI {
|
||||||
node: Arc::downgrade(node),
|
node: Arc::downgrade(node),
|
||||||
type_info,
|
type_info,
|
||||||
item_aliases: AliasList::default(),
|
item_aliases: Default::default(),
|
||||||
acceptor_aliases: AliasList::default(),
|
acceptor_aliases: Default::default(),
|
||||||
acceptor_field_aliases: AliasList::default(),
|
acceptor_field_aliases: Default::default(),
|
||||||
});
|
});
|
||||||
*type_info.ui.lock() = Arc::downgrade(&ui);
|
*type_info.ui.lock() = Arc::downgrade(&ui);
|
||||||
node.add_aspect_raw(ui.clone());
|
let _ = node.item_ui.set(ui.clone());
|
||||||
|
|
||||||
for item in type_info.items.get_valid_contents() {
|
for item in type_info.items.get_valid_contents() {
|
||||||
ui.handle_create_item(&item);
|
ui.handle_create_item(&item);
|
||||||
@@ -189,104 +229,62 @@ impl ItemUI {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
fn send_state(&self, state: &str, name: &str) {
|
||||||
|
let Ok(serialized_data) = serialize(name) else {return};
|
||||||
|
let _ = self
|
||||||
|
.node
|
||||||
|
.upgrade()
|
||||||
|
.unwrap()
|
||||||
|
.send_remote_signal(state, serialized_data);
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_create_item(&self, item: &Item) {
|
fn handle_create_item(&self, item: &Item) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let Some(client) = node.get_client() else {return};
|
||||||
};
|
|
||||||
let Some(client) = node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Ok(item_alias) = item.make_alias(&client, &self.item_aliases) else {
|
if let Ok(alias_node) = item.make_alias(&client, &(node.get_path().to_string() + "/item")) {
|
||||||
return;
|
self.item_aliases.add(item.uid.clone(), &alias_node);
|
||||||
};
|
}
|
||||||
|
|
||||||
item.specialization.send_ui_item_created(&node, &item_alias);
|
let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return};
|
||||||
}
|
let _ = node.send_remote_signal("create_item", serialized_data);
|
||||||
fn handle_capture_item(&self, item: &Item, acceptor: &ItemAcceptor) {
|
|
||||||
let Some(item_alias) = self.item_aliases.get_from_aspect(item) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(acceptor_alias) = self.acceptor_aliases.get_from_aspect(acceptor) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = item_ui_client::capture_item(
|
|
||||||
&self.node.upgrade().unwrap(),
|
|
||||||
item_alias.id,
|
|
||||||
acceptor_alias.id,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
fn handle_release_item(&self, item: &Item, acceptor: &ItemAcceptor) {
|
|
||||||
let Some(item_alias) = self.item_aliases.get_from_aspect(item) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(acceptor_alias) = self.acceptor_aliases.get_from_aspect(acceptor) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = item_ui_client::release_item(
|
|
||||||
&self.node.upgrade().unwrap(),
|
|
||||||
item_alias.id,
|
|
||||||
acceptor_alias.id,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
fn handle_destroy_item(&self, item: &Item) {
|
fn handle_destroy_item(&self, item: &Item) {
|
||||||
let Some(item_alias) = self
|
self.item_aliases.remove(&item.uid);
|
||||||
.item_aliases
|
self.send_state("destroy_item", item.uid.as_str());
|
||||||
.get_from_original_node(item.spatial.node.clone())
|
}
|
||||||
else {
|
fn handle_capture_item(&self, item: &Item, acceptor: &ItemAcceptor) {
|
||||||
return;
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
};
|
|
||||||
let _ = item_ui_client::destroy_item(&self.node.upgrade().unwrap(), item_alias.id);
|
let Ok(message) = serialize((item.uid.as_str(), acceptor.uid.as_str())) else {return};
|
||||||
self.item_aliases.remove_aspect(item);
|
let _ = node.send_remote_signal("capture_item", message);
|
||||||
|
}
|
||||||
|
fn handle_release_item(&self, item: &Item, acceptor: &ItemAcceptor) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
|
||||||
|
let Ok(message) = serialize((item.uid.as_str(), acceptor.uid.as_str())) else {return};
|
||||||
|
let _ = node.send_remote_signal("release_item", message);
|
||||||
}
|
}
|
||||||
fn handle_create_acceptor(&self, acceptor: &ItemAcceptor) {
|
fn handle_create_acceptor(&self, acceptor: &ItemAcceptor) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let Some(client) = node.get_client() else {return};
|
||||||
};
|
|
||||||
let Some(client) = node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(acceptor_node) = acceptor.spatial.node() else {
|
let Ok((alias, field_alias)) = acceptor.make_aliases(
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Ok(acceptor_alias) = Alias::create(
|
|
||||||
&acceptor_node,
|
|
||||||
&client,
|
&client,
|
||||||
ITEM_ACCEPTOR_ASPECT_ALIAS_INFO.clone(),
|
&format!("/item/{}/acceptor", self.type_info.type_name),
|
||||||
Some(&self.acceptor_aliases),
|
) else {return};
|
||||||
) else {
|
self.acceptor_aliases.add(acceptor.uid.clone(), &alias);
|
||||||
return;
|
self.acceptor_field_aliases
|
||||||
};
|
.add(acceptor.uid.clone(), &field_alias);
|
||||||
|
let Ok(message) = serialize(&acceptor.uid) else {return};
|
||||||
let Some(acceptor_field_node) = acceptor.field.spatial.node() else {
|
let _ = node.send_remote_signal("create_acceptor", message);
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Ok(acceptor_field_alias) = Alias::create(
|
|
||||||
&acceptor_field_node,
|
|
||||||
&client,
|
|
||||||
FIELD_ALIAS_INFO.clone(),
|
|
||||||
Some(&self.acceptor_aliases),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
(acceptor.type_info.new_acceptor_fn)(&node, &acceptor_alias, &acceptor_field_alias);
|
|
||||||
}
|
}
|
||||||
fn handle_destroy_acceptor(&self, acceptor: &ItemAcceptor) {
|
fn handle_destroy_acceptor(&self, acceptor: &ItemAcceptor) {
|
||||||
let acceptor_alias = self.acceptor_aliases.get_from_aspect(acceptor).unwrap();
|
self.send_state("destroy_acceptor", acceptor.uid.as_str());
|
||||||
let _ = item_ui_client::destroy_acceptor(&self.node.upgrade().unwrap(), acceptor_alias.id);
|
self.acceptor_aliases.remove(&acceptor.uid);
|
||||||
|
self.acceptor_field_aliases.remove(&acceptor.uid);
|
||||||
self.acceptor_aliases
|
|
||||||
.remove_aspect(acceptor.spatial.as_ref());
|
|
||||||
self.acceptor_field_aliases
|
|
||||||
.remove_aspect(acceptor.field.as_ref());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Aspect for ItemUI {
|
|
||||||
const NAME: &'static str = "Item";
|
|
||||||
}
|
|
||||||
impl Drop for ItemUI {
|
impl Drop for ItemUI {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
*self.type_info.ui.lock() = Weak::new();
|
*self.type_info.ui.lock() = Weak::new();
|
||||||
@@ -294,58 +292,88 @@ impl Drop for ItemUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ItemAcceptor {
|
pub struct ItemAcceptor {
|
||||||
spatial: Arc<Spatial>,
|
uid: String,
|
||||||
|
node: Weak<Node>,
|
||||||
pub type_info: &'static TypeInfo,
|
pub type_info: &'static TypeInfo,
|
||||||
field: Arc<Field>,
|
field: Arc<Field>,
|
||||||
accepted_aliases: AliasList,
|
accepted_aliases: LifeLinkedNodeMap<String>,
|
||||||
accepted_registry: Registry<Item>,
|
accepted_registry: Registry<Item>,
|
||||||
}
|
}
|
||||||
impl ItemAcceptor {
|
impl ItemAcceptor {
|
||||||
fn add_to(node: &Arc<Node>, type_info: &'static TypeInfo, field: Arc<Field>) {
|
fn add_to(node: &Arc<Node>, type_info: &'static TypeInfo, field: Arc<Field>) {
|
||||||
let acceptor = type_info.acceptors.add(ItemAcceptor {
|
let acceptor = type_info.acceptors.add(ItemAcceptor {
|
||||||
spatial: node.get_aspect::<Spatial>().unwrap(),
|
uid: nanoid!(),
|
||||||
|
node: Arc::downgrade(node),
|
||||||
type_info,
|
type_info,
|
||||||
field,
|
field,
|
||||||
accepted_aliases: AliasList::default(),
|
accepted_aliases: Default::default(),
|
||||||
accepted_registry: Registry::new(),
|
accepted_registry: Registry::new(),
|
||||||
});
|
});
|
||||||
|
node.add_local_signal("capture", ItemAcceptor::capture_flex);
|
||||||
if let Some(ui) = type_info.ui.lock().upgrade() {
|
if let Some(ui) = type_info.ui.lock().upgrade() {
|
||||||
ui.handle_create_acceptor(&acceptor);
|
ui.handle_create_acceptor(&acceptor);
|
||||||
}
|
}
|
||||||
node.add_aspect_raw(acceptor.clone());
|
let _ = node.item_acceptor.set(acceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn capture_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
if !node.enabled.load(Ordering::Relaxed) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let acceptor = node.item_acceptor.get().unwrap();
|
||||||
|
let item_path: &str = deserialize(message.as_ref())?;
|
||||||
|
let item_node = calling_client.get_node("Item", item_path)?;
|
||||||
|
let item = item_node.get_aspect("Item", "item", |n| &n.item)?;
|
||||||
|
capture(item, acceptor);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_aliases(&self, client: &Arc<Client>, parent: &str) -> Result<(Arc<Node>, Arc<Node>)> {
|
||||||
|
let acceptor_node = &self.node.upgrade().unwrap();
|
||||||
|
let acceptor_alias = Alias::create(
|
||||||
|
client,
|
||||||
|
parent,
|
||||||
|
&self.uid,
|
||||||
|
acceptor_node,
|
||||||
|
AliasInfo {
|
||||||
|
server_signals: vec!["capture"],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let acceptor_field_alias = Alias::create(
|
||||||
|
client,
|
||||||
|
acceptor_alias.get_path(),
|
||||||
|
"field",
|
||||||
|
&self.field.spatial_ref().node.upgrade().unwrap(),
|
||||||
|
AliasInfo::default(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((acceptor_alias, acceptor_field_alias))
|
||||||
|
}
|
||||||
fn handle_capture(&self, item: &Arc<Item>) {
|
fn handle_capture(&self, item: &Arc<Item>) {
|
||||||
let Some(node) = self.spatial.node() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let Some(client) = node.get_client() else {return};
|
||||||
};
|
|
||||||
let Some(client) = node.get_client() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
self.accepted_registry.add_raw(item);
|
self.accepted_registry.add_raw(item);
|
||||||
let Ok(alias_node) = item.make_alias(&client, &self.accepted_aliases) else {
|
if let Ok(alias_node) = item.make_alias(&client, &node.path) {
|
||||||
return;
|
self.accepted_aliases.add(item.uid.clone(), &alias_node);
|
||||||
};
|
}
|
||||||
|
|
||||||
item.specialization
|
let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return};
|
||||||
.send_acceptor_item_created(&node, &alias_node);
|
let _ = node.send_remote_signal("capture", serialized_data);
|
||||||
}
|
}
|
||||||
fn handle_release(&self, item: &Item) {
|
fn handle_release(&self, item: &Item) {
|
||||||
self.accepted_registry.remove(item);
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
self.accepted_aliases.remove_aspect(item);
|
|
||||||
|
|
||||||
let Some(node) = self.spatial.node() else {
|
self.accepted_registry.remove(item);
|
||||||
return;
|
self.accepted_aliases.remove(&item.uid);
|
||||||
};
|
let Ok(message) = serialize(&item.uid) else {return};
|
||||||
let alias = self.accepted_aliases.get_from_aspect(item).unwrap();
|
let _ = node.send_remote_signal("release", message);
|
||||||
let _ = item_acceptor_client::release_item(&node, alias.id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Aspect for ItemAcceptor {
|
|
||||||
const NAME: &'static str = "ItemAcceptor";
|
|
||||||
}
|
|
||||||
impl ItemAcceptorAspect for ItemAcceptor {}
|
|
||||||
impl Drop for ItemAcceptor {
|
impl Drop for ItemAcceptor {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.type_info.acceptors.remove(self);
|
self.type_info.acceptors.remove(self);
|
||||||
@@ -358,39 +386,71 @@ impl Drop for ItemAcceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "item", false);
|
||||||
|
node.add_local_signal("create_camera_item", camera::create_camera_item_flex);
|
||||||
|
node.add_local_signal(
|
||||||
|
"create_environment_item",
|
||||||
|
environment::create_environment_item_flex,
|
||||||
|
);
|
||||||
|
node.add_local_signal("register_item_ui", register_item_ui_flex);
|
||||||
|
node.add_local_signal("create_item_acceptor", create_item_acceptor_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_info(name: &str) -> Result<&'static TypeInfo> {
|
||||||
|
match name {
|
||||||
|
"environment" => Ok(&ITEM_TYPE_INFO_ENVIRONMENT),
|
||||||
|
#[cfg(feature = "wayland")]
|
||||||
|
"panel" => Ok(&ITEM_TYPE_INFO_PANEL),
|
||||||
|
_ => Err(eyre!("Invalid item type")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn register_item_ui_flex(
|
pub fn register_item_ui_flex(
|
||||||
|
_node: &Node,
|
||||||
calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
type_info: &'static TypeInfo,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let ui = Node::from_id(&calling_client, type_info.ui_node_id, true).add_to_scenegraph()?;
|
#[derive(Deserialize)]
|
||||||
|
struct RegisterItemUIInfo<'a> {
|
||||||
|
item_type: &'a str,
|
||||||
|
}
|
||||||
|
let info: RegisterItemUIInfo = deserialize(message.as_ref())?;
|
||||||
|
let type_info = type_info(info.item_type)?;
|
||||||
|
let ui =
|
||||||
|
Node::create(&calling_client, "/item", type_info.type_name, true).add_to_scenegraph()?;
|
||||||
ItemUI::add_to(&ui, type_info)?;
|
ItemUI::add_to(&ui, type_info)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn create_item_acceptor_flex(
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
type_info: &'static TypeInfo,
|
|
||||||
field: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let space = parent.get_aspect::<Spatial>()?;
|
|
||||||
let field = field.get_aspect::<Field>()?;
|
|
||||||
let transform = transform.to_mat4(true, true, false);
|
|
||||||
|
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
fn create_item_acceptor_flex(
|
||||||
Spatial::add_to(&node, Some(space.clone()), transform, false);
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateItemAcceptorInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
field_path: &'a str,
|
||||||
|
item_type: &'a str,
|
||||||
|
}
|
||||||
|
let info: CreateItemAcceptorInfo = deserialize(message.as_ref())?;
|
||||||
|
let space = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let field = find_field(&calling_client, info.field_path)?;
|
||||||
|
let type_info = type_info(info.item_type)?;
|
||||||
|
|
||||||
|
let node = Node::create(
|
||||||
|
&calling_client,
|
||||||
|
&format!("/item/{}/acceptor", type_info.type_name),
|
||||||
|
info.name,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(space), transform, false)?;
|
||||||
ItemAcceptor::add_to(&node, type_info, field);
|
ItemAcceptor::add_to(&node, type_info, field);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn acceptor_capture_item_flex(node: Arc<Node>, item: Arc<Node>) -> Result<()> {
|
|
||||||
let acceptor = node.get_aspect::<ItemAcceptor>()?;
|
|
||||||
let item = item.get_aspect::<Item>()?;
|
|
||||||
capture(&item, &acceptor);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ItemInterface;
|
|
||||||
// create_interface!(ItemInterface);
|
|
||||||
|
|||||||
@@ -1,110 +1,241 @@
|
|||||||
use super::{create_item_acceptor_flex, register_item_ui_flex, ItemAcceptor, ItemInterface};
|
|
||||||
use crate::nodes::items::ITEM_ACCEPTOR_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::nodes::items::ITEM_ASPECT_ALIAS_INFO;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{
|
core::{
|
||||||
client::{get_env, state, Client, INTERNAL_CLIENT},
|
client::{get_env, state, Client, INTERNAL_CLIENT},
|
||||||
registry::Registry,
|
registry::Registry,
|
||||||
},
|
},
|
||||||
create_interface,
|
|
||||||
nodes::{
|
nodes::{
|
||||||
drawable::model::ModelPart,
|
drawable::{model::ModelPart, Drawable},
|
||||||
items::{Item, ItemType, TypeInfo},
|
items::{Item, ItemType, TypeInfo},
|
||||||
spatial::{Spatial, Transform},
|
spatial::Spatial,
|
||||||
Node,
|
Message, Node,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::{bail, eyre, Result};
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use mint::Vector2;
|
use mint::Vector2;
|
||||||
|
use nanoid::nanoid;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
|
use serde::{
|
||||||
|
de::{Deserializer, Error, SeqAccess, Visitor},
|
||||||
|
ser::Serializer,
|
||||||
|
Deserialize, Serialize,
|
||||||
|
};
|
||||||
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use tracing::{debug, info};
|
use tracing::debug;
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_item_panel_protocol!();
|
|
||||||
impl Default for Geometry {
|
|
||||||
fn default() -> Self {
|
|
||||||
Geometry {
|
|
||||||
origin: [0, 0].into(),
|
|
||||||
size: [0, 0].into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo {
|
pub static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo {
|
||||||
type_name: "panel",
|
type_name: "panel",
|
||||||
alias_info: PANEL_ITEM_ASPECT_ALIAS_INFO.clone(),
|
aliased_local_signals: vec![
|
||||||
ui_node_id: INTERFACE_NODE_ID,
|
"apply_surface_material",
|
||||||
|
"close_toplevel",
|
||||||
|
"auto_size_toplevel",
|
||||||
|
"set_toplevel_size",
|
||||||
|
"set_toplevel_focused_visuals",
|
||||||
|
"pointer_motion",
|
||||||
|
"pointer_button",
|
||||||
|
"pointer_scroll",
|
||||||
|
"keyboard_keymap",
|
||||||
|
"keyboard_key",
|
||||||
|
"touch_down",
|
||||||
|
"touch_move",
|
||||||
|
"touch_up",
|
||||||
|
"reset_touches",
|
||||||
|
],
|
||||||
|
aliased_local_methods: vec![],
|
||||||
|
aliased_remote_signals: vec![
|
||||||
|
"toplevel_parent_changed",
|
||||||
|
"toplevel_title_changed",
|
||||||
|
"toplevel_app_id_changed",
|
||||||
|
"toplevel_fullscreen_active",
|
||||||
|
"toplevel_move_request",
|
||||||
|
"toplevel_resize_request",
|
||||||
|
"toplevel_size_changed",
|
||||||
|
"set_cursor",
|
||||||
|
"new_child",
|
||||||
|
"reposition_child",
|
||||||
|
"drop_child",
|
||||||
|
],
|
||||||
ui: Default::default(),
|
ui: Default::default(),
|
||||||
items: Registry::new(),
|
items: Registry::new(),
|
||||||
acceptors: Registry::new(),
|
acceptors: Registry::new(),
|
||||||
new_acceptor_fn: |node, acceptor, acceptor_field| {
|
|
||||||
let _ = panel_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An ID for a surface inside this panel item
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum SurfaceID {
|
||||||
|
Cursor,
|
||||||
|
Toplevel,
|
||||||
|
Child(String),
|
||||||
|
}
|
||||||
|
impl Default for SurfaceID {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Toplevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for SurfaceID {
|
||||||
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||||
|
deserializer.deserialize_seq(SurfaceIDVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SurfaceIDVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for SurfaceIDVisitor {
|
||||||
|
type Value = SurfaceID;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
f.write_str("idk")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||||
|
let Some(discrim) = seq.next_element()? else {
|
||||||
|
return Err(A::Error::missing_field("discrim"));
|
||||||
|
};
|
||||||
|
|
||||||
|
// idk if you wanna check for extraneous elements
|
||||||
|
// I didn't bother
|
||||||
|
|
||||||
|
match discrim {
|
||||||
|
"Cursor" => Ok(SurfaceID::Cursor),
|
||||||
|
"Toplevel" => Ok(SurfaceID::Toplevel),
|
||||||
|
"Child" => {
|
||||||
|
let Some(text) = seq.next_element()? else {
|
||||||
|
return Err(A::Error::missing_field("child_text"));
|
||||||
|
};
|
||||||
|
Ok(SurfaceID::Child(text))
|
||||||
|
}
|
||||||
|
_ => Err(A::Error::unknown_variant(
|
||||||
|
discrim,
|
||||||
|
&["Cursor", "Toplevel", "Child"],
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for SurfaceID {
|
||||||
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
|
match self {
|
||||||
|
Self::Cursor => ["Cursor"].serialize(serializer),
|
||||||
|
Self::Toplevel => ["Toplevel"].serialize(serializer),
|
||||||
|
Self::Child(text) => ["Child", text].serialize(serializer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The origin and size of the surface's "solid" part.
|
||||||
|
#[derive(Debug, Serialize, Clone, Copy)]
|
||||||
|
pub struct Geometry {
|
||||||
|
pub origin: Vector2<i32>,
|
||||||
|
pub size: Vector2<u32>,
|
||||||
|
}
|
||||||
|
/// The state of the panel item's toplevel.
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct ToplevelInfo {
|
||||||
|
/// The UID of the panel item of the parent of this toplevel, if it exists
|
||||||
|
pub parent: Option<String>,
|
||||||
|
/// Equivalent to the window title
|
||||||
|
pub title: Option<String>,
|
||||||
|
/// Application identifier, see <https://standards.freedesktop.org/desktop-entry-spec/>
|
||||||
|
pub app_id: Option<String>,
|
||||||
|
/// Current size in pixels
|
||||||
|
pub size: Vector2<u32>,
|
||||||
|
/// Recommended minimum size in pixels
|
||||||
|
pub min_size: Option<Vector2<u32>>,
|
||||||
|
/// Recommended maximum size in pixels
|
||||||
|
pub max_size: Option<Vector2<u32>>,
|
||||||
|
/// Surface geometry
|
||||||
|
pub logical_rectangle: Geometry,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Data on positioning a child
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct ChildInfo {
|
||||||
|
pub parent: SurfaceID,
|
||||||
|
pub geometry: Geometry,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The init data for the panel item.
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct PanelItemInitData {
|
||||||
|
/// The cursor, if applicable.
|
||||||
|
pub cursor: Option<Geometry>,
|
||||||
|
/// Size of the toplevel surface in pixels.
|
||||||
|
pub toplevel: ToplevelInfo,
|
||||||
|
/// Vector of childs that already exist
|
||||||
|
pub children: FxHashMap<String, ChildInfo>,
|
||||||
|
/// The surface, if any, that has exclusive input to the pointer.
|
||||||
|
pub pointer_grab: Option<SurfaceID>,
|
||||||
|
/// The surface, if any, that has exclusive input to the keyboard.
|
||||||
|
pub keyboard_grab: Option<SurfaceID>,
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Backend: Send + Sync + 'static {
|
pub trait Backend: Send + Sync + 'static {
|
||||||
fn start_data(&self) -> Result<PanelItemInitData>;
|
fn start_data(&self) -> Result<PanelItemInitData>;
|
||||||
|
|
||||||
fn apply_cursor_material(&self, model_part: &Arc<ModelPart>);
|
fn apply_surface_material(&self, surface: SurfaceID, model_part: &Arc<ModelPart>);
|
||||||
fn apply_surface_material(&self, surface: SurfaceId, model_part: &Arc<ModelPart>);
|
|
||||||
|
|
||||||
fn close_toplevel(&self);
|
fn close_toplevel(&self);
|
||||||
fn auto_size_toplevel(&self);
|
fn auto_size_toplevel(&self);
|
||||||
fn set_toplevel_size(&self, size: Vector2<u32>);
|
fn set_toplevel_size(&self, size: Vector2<u32>);
|
||||||
fn set_toplevel_focused_visuals(&self, focused: bool);
|
fn set_toplevel_focused_visuals(&self, focused: bool);
|
||||||
|
|
||||||
fn pointer_motion(&self, surface: &SurfaceId, position: Vector2<f32>);
|
fn pointer_motion(&self, surface: &SurfaceID, position: Vector2<f32>);
|
||||||
fn pointer_button(&self, surface: &SurfaceId, button: u32, pressed: bool);
|
fn pointer_button(&self, surface: &SurfaceID, button: u32, pressed: bool);
|
||||||
fn pointer_scroll(
|
fn pointer_scroll(
|
||||||
&self,
|
&self,
|
||||||
surface: &SurfaceId,
|
surface: &SurfaceID,
|
||||||
scroll_distance: Option<Vector2<f32>>,
|
scroll_distance: Option<Vector2<f32>>,
|
||||||
scroll_steps: Option<Vector2<f32>>,
|
scroll_steps: Option<Vector2<f32>>,
|
||||||
);
|
);
|
||||||
|
|
||||||
fn keyboard_keys(&self, surface: &SurfaceId, keymap_id: u64, keys: Vec<i32>);
|
fn keyboard_keys(&self, surface: &SurfaceID, keymap_id: &str, keys: Vec<i32>);
|
||||||
|
|
||||||
fn touch_down(&self, surface: &SurfaceId, id: u32, position: Vector2<f32>);
|
fn touch_down(&self, surface: &SurfaceID, id: u32, position: Vector2<f32>);
|
||||||
fn touch_move(&self, id: u32, position: Vector2<f32>);
|
fn touch_move(&self, id: u32, position: Vector2<f32>);
|
||||||
fn touch_up(&self, id: u32);
|
fn touch_up(&self, id: u32);
|
||||||
fn reset_input(&self);
|
fn reset_touches(&self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn panel_item_from_node(node: &Node) -> Option<Arc<dyn PanelItemTrait>> {
|
pub fn panel_item_from_node(node: &Node) -> Option<Arc<dyn PanelItemTrait>> {
|
||||||
let ItemType::Panel(panel_item) = &node.get_aspect::<Item>().ok()?.specialization else {
|
let ItemType::Panel(panel_item) = &node.item.get()?.specialization else {return None};
|
||||||
return None;
|
|
||||||
};
|
|
||||||
Some(panel_item.clone())
|
Some(panel_item.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PanelItemTrait: Send + Sync + 'static {
|
pub trait PanelItemTrait: Backend + Send + Sync + 'static {
|
||||||
fn backend(&self) -> &dyn Backend;
|
fn uid(&self) -> &str;
|
||||||
fn send_ui_item_created(&self, node: &Node, item: &Arc<Node>);
|
fn serialize_start_data(&self, id: &str) -> Result<Message>;
|
||||||
fn send_acceptor_item_created(&self, node: &Node, item: &Arc<Node>);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PanelItem<B: Backend> {
|
pub struct PanelItem<B: Backend + ?Sized> {
|
||||||
pub node: Weak<Node>,
|
pub uid: String,
|
||||||
|
node: Weak<Node>,
|
||||||
pub backend: Box<B>,
|
pub backend: Box<B>,
|
||||||
}
|
}
|
||||||
impl<B: Backend> PanelItem<B> {
|
impl<B: Backend + ?Sized> PanelItem<B> {
|
||||||
pub fn create(backend: Box<B>, pid: Option<i32>) -> (Arc<Node>, Arc<PanelItem<B>>) {
|
pub fn create(backend: Box<B>, pid: Option<i32>) -> Arc<PanelItem<B>> {
|
||||||
debug!(?pid, "Create panel item");
|
debug!(?pid, "Create panel item");
|
||||||
|
|
||||||
let startup_settings = pid
|
let startup_settings = pid
|
||||||
.and_then(|pid| get_env(pid).ok())
|
.and_then(|pid| get_env(pid).ok())
|
||||||
.and_then(|env| state(&env));
|
.and_then(|env| state(&env));
|
||||||
|
|
||||||
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, true));
|
let uid = nanoid!();
|
||||||
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
let node = Node::create(&INTERNAL_CLIENT, "/item/panel/item", &uid, true)
|
||||||
|
.add_to_scenegraph()
|
||||||
|
.unwrap();
|
||||||
|
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
|
||||||
if let Some(startup_settings) = &startup_settings {
|
if let Some(startup_settings) = &startup_settings {
|
||||||
spatial.set_local_transform(startup_settings.root);
|
spatial.set_local_transform(startup_settings.root);
|
||||||
}
|
}
|
||||||
|
|
||||||
let panel_item = Arc::new(PanelItem {
|
let panel_item = Arc::new(PanelItem {
|
||||||
|
uid: uid.clone(),
|
||||||
node: Arc::downgrade(&node),
|
node: Arc::downgrade(&node),
|
||||||
backend,
|
backend,
|
||||||
});
|
});
|
||||||
@@ -112,352 +243,300 @@ impl<B: Backend> PanelItem<B> {
|
|||||||
let generic_panel_item: Arc<dyn PanelItemTrait> = panel_item.clone();
|
let generic_panel_item: Arc<dyn PanelItemTrait> = panel_item.clone();
|
||||||
Item::add_to(
|
Item::add_to(
|
||||||
&node,
|
&node,
|
||||||
|
uid,
|
||||||
&ITEM_TYPE_INFO_PANEL,
|
&ITEM_TYPE_INFO_PANEL,
|
||||||
ItemType::Panel(generic_panel_item),
|
ItemType::Panel(generic_panel_item),
|
||||||
);
|
);
|
||||||
<Self as PanelItemAspect>::add_node_members(&node);
|
|
||||||
|
|
||||||
(node, panel_item)
|
node.add_local_signal("apply_surface_material", Self::apply_surface_material_flex);
|
||||||
|
node.add_local_signal("close_toplevel", Self::close_toplevel_flex);
|
||||||
|
node.add_local_signal("auto_size_toplevel", Self::auto_size_toplevel_flex);
|
||||||
|
node.add_local_signal(
|
||||||
|
"set_toplevel_size_changed",
|
||||||
|
Self::set_toplevel_size_changed_flex,
|
||||||
|
);
|
||||||
|
|
||||||
|
node.add_local_signal("pointer_motion", Self::pointer_motion_flex);
|
||||||
|
node.add_local_signal("pointer_button", Self::pointer_button_flex);
|
||||||
|
node.add_local_signal("pointer_scroll", Self::pointer_scroll_flex);
|
||||||
|
|
||||||
|
node.add_local_signal("keyboard_key", Self::keyboard_keys_flex);
|
||||||
|
|
||||||
|
node.add_local_signal("touch_down", Self::touch_down_flex);
|
||||||
|
node.add_local_signal("touch_move", Self::touch_move_flex);
|
||||||
|
node.add_local_signal("touch_up", Self::touch_up_flex);
|
||||||
|
node.add_local_signal("reset_touches", Self::reset_touches_flex);
|
||||||
|
|
||||||
|
panel_item
|
||||||
|
}
|
||||||
|
pub fn drop_toplevel(&self) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
node.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote signals
|
// Remote signals
|
||||||
#[allow(unused)]
|
impl<B: Backend + ?Sized> PanelItem<B> {
|
||||||
impl<B: Backend> PanelItem<B> {
|
pub fn toplevel_parent_changed(&self, parent: &str) {
|
||||||
pub fn toplevel_parent_changed(&self, parent: u64) {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
let Some(node) = self.node.upgrade() else {
|
let _ = node.send_remote_signal("toplevel_parent_changed", serialize(parent).unwrap());
|
||||||
return;
|
|
||||||
};
|
|
||||||
panel_item_client::toplevel_parent_changed(&node, parent);
|
|
||||||
}
|
}
|
||||||
pub fn toplevel_title_changed(&self, title: &str) {
|
pub fn toplevel_title_changed(&self, title: &str) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("toplevel_title_changed", serialize(title).unwrap());
|
||||||
};
|
|
||||||
panel_item_client::toplevel_title_changed(&node, title);
|
|
||||||
}
|
}
|
||||||
pub fn toplevel_app_id_changed(&self, app_id: &str) {
|
pub fn toplevel_app_id_changed(&self, app_id: &str) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("toplevel_app_id_changed", serialize(app_id).unwrap());
|
||||||
};
|
|
||||||
panel_item_client::toplevel_app_id_changed(&node, app_id);
|
|
||||||
}
|
}
|
||||||
pub fn toplevel_fullscreen_active(&self, active: bool) {
|
pub fn toplevel_fullscreen_active(&self, active: bool) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("toplevel_fullscreen_active", serialize(active).unwrap());
|
||||||
};
|
|
||||||
panel_item_client::toplevel_fullscreen_active(&node, active);
|
|
||||||
}
|
}
|
||||||
pub fn toplevel_move_request(&self) {
|
pub fn toplevel_move_request(&self) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("toplevel_move_request", Vec::<u8>::new());
|
||||||
};
|
|
||||||
panel_item_client::toplevel_move_request(&node);
|
|
||||||
}
|
}
|
||||||
pub fn toplevel_resize_request(&self, up: bool, down: bool, left: bool, right: bool) {
|
pub fn toplevel_resize_request(&self, up: bool, down: bool, left: bool, right: bool) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal(
|
||||||
};
|
"toplevel_resize_request",
|
||||||
panel_item_client::toplevel_resize_request(&node, up, down, left, right);
|
serialize((up, down, left, right)).unwrap(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
pub fn toplevel_size_changed(&self, size: Vector2<u32>) {
|
pub fn toplevel_size_changed(&self, size: Vector2<u32>) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("toplevel_size_changed", serialize(size).unwrap());
|
||||||
};
|
|
||||||
panel_item_client::toplevel_size_changed(&node, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cursor(&self, geometry: Option<Geometry>) {
|
pub fn set_cursor(&self, geometry: Option<Geometry>) {
|
||||||
let Some(node) = self.node.upgrade() else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
return;
|
let _ = node.send_remote_signal("set_cursor", serialize(geometry).unwrap());
|
||||||
};
|
}
|
||||||
if let Some(geometry) = geometry {
|
|
||||||
panel_item_client::set_cursor(&node, &geometry);
|
pub fn new_child(&self, uid: &str, info: ChildInfo) {
|
||||||
} else {
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
panel_item_client::hide_cursor(&node);
|
let _ = node.send_remote_signal("new_child", serialize((uid, info)).unwrap());
|
||||||
|
}
|
||||||
|
pub fn reposition_child(&self, uid: &str, geometry: Geometry) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
let _ = node.send_remote_signal("reposition_child", serialize((uid, geometry)).unwrap());
|
||||||
|
}
|
||||||
|
pub fn drop_child(&self, uid: &str) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
let _ = node.send_remote_signal("drop_child", serialize(uid).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Local signals
|
||||||
|
macro_rules! flex_no_args {
|
||||||
|
($fn_name: ident, $trait_fn: ident) => {
|
||||||
|
fn $fn_name(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
|
||||||
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
|
panel_item.$trait_fn();
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
pub fn create_child(&self, id: u64, info: &ChildInfo) {
|
|
||||||
let Some(node) = self.node.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
panel_item_client::create_child(&node, id, info);
|
|
||||||
}
|
|
||||||
pub fn reposition_child(&self, id: u64, geometry: &Geometry) {
|
|
||||||
let Some(node) = self.node.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
panel_item_client::reposition_child(&node, id, geometry);
|
|
||||||
}
|
|
||||||
pub fn destroy_child(&self, id: u64) {
|
|
||||||
let Some(node) = self.node.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
panel_item_client::destroy_child(&node, id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
macro_rules! flex_deserialize {
|
||||||
// make these stupid vectors u32 in the protocol somehow!!!!!!!1
|
($fn_name: ident, $trait_fn: ident) => {
|
||||||
|
fn $fn_name(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
#[allow(unused)]
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
impl<B: Backend> PanelItemAspect for PanelItem<B> {
|
panel_item.$trait_fn(deserialize(message.as_ref())?);
|
||||||
#[doc = "Apply the cursor as a material to a model."]
|
Ok(())
|
||||||
fn apply_cursor_material(
|
}
|
||||||
node: Arc<Node>,
|
};
|
||||||
_calling_client: Arc<Client>,
|
}
|
||||||
model_part: Arc<Node>,
|
impl<B: Backend + ?Sized> PanelItem<B> {
|
||||||
) -> Result<()> {
|
fn apply_surface_material_flex(
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
node: &Node,
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
let model_part = model_part.get_aspect::<ModelPart>()?;
|
|
||||||
|
|
||||||
panel_item.backend().apply_cursor_material(&model_part);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Apply a surface's visuals as a material to a model."]
|
|
||||||
fn apply_surface_material(
|
|
||||||
node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
surface: SurfaceId,
|
message: Message,
|
||||||
model_part: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
return Ok(());
|
|
||||||
};
|
#[derive(Debug, Deserialize)]
|
||||||
let model_part = model_part.get_aspect::<ModelPart>()?;
|
struct SurfaceMaterialInfo<'a> {
|
||||||
|
surface: SurfaceID,
|
||||||
|
model_node_path: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
let info: SurfaceMaterialInfo = deserialize(message.as_ref())?;
|
||||||
|
|
||||||
|
let model_node = calling_client
|
||||||
|
.scenegraph
|
||||||
|
.get_node(info.model_node_path)
|
||||||
|
.ok_or_else(|| eyre!("Model node not found"))?;
|
||||||
|
let Some(Drawable::ModelPart(model_part)) = model_node.drawable.get() else {bail!("Node is not a model")};
|
||||||
|
debug!(?info, "Apply surface material");
|
||||||
|
|
||||||
|
panel_item.apply_surface_material(info.surface, model_part);
|
||||||
|
|
||||||
panel_item
|
|
||||||
.backend()
|
|
||||||
.apply_surface_material(surface, &model_part);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Try to close the toplevel.\n \n The panel item UI handler or panel item acceptor will drop the panel item if this succeeds."]
|
flex_no_args!(close_toplevel_flex, close_toplevel);
|
||||||
fn close_toplevel(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
flex_no_args!(auto_size_toplevel_flex, auto_size_toplevel);
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
flex_deserialize!(set_toplevel_size_changed_flex, set_toplevel_size);
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().close_toplevel();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Request a resize of the surface to whatever size the 2D app wants."]
|
fn pointer_motion_flex(
|
||||||
fn auto_size_toplevel(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
node: &Node,
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().auto_size_toplevel();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Request a resize of the surface (in pixels)."]
|
|
||||||
fn set_toplevel_size(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
size: mint::Vector2<u32>,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
return Ok(());
|
|
||||||
};
|
let (surface_id, position): (SurfaceID, Vector2<f32>) = deserialize(message.as_ref())?;
|
||||||
panel_item.backend().set_toplevel_size(size);
|
debug!(?surface_id, ?position, "Pointer deactivate");
|
||||||
|
|
||||||
|
panel_item.pointer_motion(&surface_id, position);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
fn pointer_button_flex(
|
||||||
#[doc = "Tell the toplevel to appear focused visually if true, or unfocused if false."]
|
node: &Node,
|
||||||
fn set_toplevel_focused_visuals(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
focused: bool,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
return Ok(());
|
|
||||||
};
|
let (surface_id, button, state): (SurfaceID, u32, u32) = deserialize(message.as_ref())?;
|
||||||
panel_item.backend().set_toplevel_focused_visuals(focused);
|
debug!(?surface_id, button, state, "Pointer button");
|
||||||
|
|
||||||
|
panel_item.pointer_button(&surface_id, button, state != 0);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
fn pointer_scroll_flex(
|
||||||
#[doc = "Send an event to set the pointer's position (in pixels, relative to top-left of surface). This will activate the pointer."]
|
node: &Node,
|
||||||
fn pointer_motion(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
surface: SurfaceId,
|
message: Message,
|
||||||
position: mint::Vector2<f32>,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
return Ok(());
|
|
||||||
};
|
#[derive(Debug, Deserialize)]
|
||||||
panel_item.backend().pointer_motion(&surface, position);
|
struct PointerScrollInfo {
|
||||||
|
surface_id: SurfaceID,
|
||||||
|
axis_continuous: Option<Vector2<f32>>,
|
||||||
|
axis_discrete: Option<Vector2<f32>>,
|
||||||
|
}
|
||||||
|
let info: PointerScrollInfo = deserialize(message.as_ref())?;
|
||||||
|
debug!(?info, "Pointer scroll");
|
||||||
|
|
||||||
|
panel_item.pointer_scroll(&info.surface_id, info.axis_continuous, info.axis_discrete);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Send an event to set a pointer button's state if the pointer's active. The `button` is from the `input_event_codes` crate (e.g. BTN_LEFT for left click)."]
|
fn keyboard_keys_flex(
|
||||||
fn pointer_button(
|
node: &Node,
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
surface: SurfaceId,
|
message: Message,
|
||||||
button: u32,
|
|
||||||
pressed: bool,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
return Ok(());
|
let (surface_id, keymap_id, keys): (SurfaceID, &str, Vec<i32>) =
|
||||||
};
|
deserialize(message.as_ref())?;
|
||||||
panel_item
|
debug!(?keys, "Set keyboard key state");
|
||||||
.backend()
|
|
||||||
.pointer_button(&surface, button, pressed);
|
panel_item.keyboard_keys(&surface_id, keymap_id, keys);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
pub fn grab_keyboard(&self, sid: Option<SurfaceID>) {
|
||||||
|
let Some(node) = self.node.upgrade() else {return};
|
||||||
|
|
||||||
#[doc = "Send an event to scroll the pointer if it's active.\nScroll distance is a value in pixels corresponding to the `distance` the surface should be scrolled.\nScroll steps is a value in columns/rows corresponding to the wheel clicks of a mouse or such. This also supports fractions of a wheel click."]
|
let Ok(message) = serialize(sid) else {return};
|
||||||
|
let _ = node.send_remote_signal("grab_keyboard", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn touch_down_flex(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
|
|
||||||
|
let (surface_id, id, position): (SurfaceID, u32, Vector2<f32>) =
|
||||||
|
deserialize(message.as_ref())?;
|
||||||
|
debug!(?surface_id, id, ?position, "Touch down");
|
||||||
|
|
||||||
|
panel_item.touch_down(&surface_id, id, position);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn touch_move_flex(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
|
||||||
|
|
||||||
|
let (id, position): (u32, Vector2<f32>) = deserialize(message.as_ref())?;
|
||||||
|
debug!(?position, "Touch move");
|
||||||
|
|
||||||
|
panel_item.touch_move(id, position);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
flex_deserialize!(touch_up_flex, touch_up);
|
||||||
|
flex_no_args!(reset_touches_flex, reset_touches);
|
||||||
|
}
|
||||||
|
impl<B: Backend + ?Sized> PanelItemTrait for PanelItem<B> {
|
||||||
|
fn uid(&self) -> &str {
|
||||||
|
&self.uid
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_start_data(&self, id: &str) -> Result<Message> {
|
||||||
|
Ok(serialize((id, self.start_data()?))?.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<B: Backend + ?Sized> Backend for PanelItem<B> {
|
||||||
|
fn start_data(&self) -> Result<PanelItemInitData> {
|
||||||
|
self.backend.start_data()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_surface_material(&self, surface: SurfaceID, model_part: &Arc<ModelPart>) {
|
||||||
|
self.backend.apply_surface_material(surface, model_part)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close_toplevel(&self) {
|
||||||
|
self.backend.close_toplevel()
|
||||||
|
}
|
||||||
|
fn auto_size_toplevel(&self) {
|
||||||
|
self.backend.auto_size_toplevel()
|
||||||
|
}
|
||||||
|
fn set_toplevel_size(&self, size: Vector2<u32>) {
|
||||||
|
self.backend.set_toplevel_size(size)
|
||||||
|
}
|
||||||
|
fn set_toplevel_focused_visuals(&self, focused: bool) {
|
||||||
|
self.backend.set_toplevel_focused_visuals(focused)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pointer_motion(&self, surface: &SurfaceID, position: Vector2<f32>) {
|
||||||
|
self.backend.pointer_motion(surface, position)
|
||||||
|
}
|
||||||
|
fn pointer_button(&self, surface: &SurfaceID, button: u32, pressed: bool) {
|
||||||
|
self.backend.pointer_button(surface, button, pressed)
|
||||||
|
}
|
||||||
fn pointer_scroll(
|
fn pointer_scroll(
|
||||||
node: Arc<Node>,
|
&self,
|
||||||
_calling_client: Arc<Client>,
|
surface: &SurfaceID,
|
||||||
surface: SurfaceId,
|
scroll_distance: Option<Vector2<f32>>,
|
||||||
scroll_distance: mint::Vector2<f32>,
|
scroll_steps: Option<Vector2<f32>>,
|
||||||
scroll_steps: mint::Vector2<f32>,
|
) {
|
||||||
) -> Result<()> {
|
self.backend
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
.pointer_scroll(surface, scroll_distance, scroll_steps)
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item
|
|
||||||
.backend()
|
|
||||||
.pointer_scroll(&surface, Some(scroll_distance), Some(scroll_steps));
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Send an event to stop scrolling the pointer."]
|
fn keyboard_keys(&self, surface: &SurfaceID, keymap_id: &str, keys: Vec<i32>) {
|
||||||
fn pointer_stop_scroll(
|
self.backend.keyboard_keys(surface, keymap_id, keys)
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
surface: SurfaceId,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().pointer_scroll(&surface, None, None);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Send a series of key presses and releases (positive keycode for pressed, negative for released)."]
|
fn touch_down(&self, surface: &SurfaceID, id: u32, position: Vector2<f32>) {
|
||||||
fn keyboard_keys(
|
self.backend.touch_down(surface, id, position)
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
surface: SurfaceId,
|
|
||||||
keymap_id: u64,
|
|
||||||
keys: Vec<i32>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item
|
|
||||||
.backend()
|
|
||||||
.keyboard_keys(&surface, keymap_id, keys);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
fn touch_move(&self, id: u32, position: Vector2<f32>) {
|
||||||
#[doc = "Put a touch down on this surface with the unique ID `uid` at `position` (in pixels) from top left corner of the surface."]
|
self.backend.touch_move(id, position)
|
||||||
fn touch_down(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
surface: SurfaceId,
|
|
||||||
uid: u32,
|
|
||||||
position: mint::Vector2<f32>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().touch_down(&surface, uid, position);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
fn touch_up(&self, id: u32) {
|
||||||
#[doc = "Move an existing touch point."]
|
self.backend.touch_up(id)
|
||||||
fn touch_move(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
uid: u32,
|
|
||||||
position: mint::Vector2<f32>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().touch_move(uid, position);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
fn reset_touches(&self) {
|
||||||
#[doc = "Release a touch from its surface."]
|
self.backend.reset_touches()
|
||||||
fn touch_up(node: Arc<Node>, _calling_client: Arc<Client>, uid: u32) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().touch_up(uid);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Reset all input, such as pressed keys and pointer clicks and touches. Useful for when it's newly captured into an item acceptor to make sure no input gets stuck."]
|
|
||||||
fn reset_input(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
let Some(panel_item) = panel_item_from_node(&node) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
panel_item.backend().reset_input();
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<B: Backend + ?Sized> Drop for PanelItem<B> {
|
||||||
impl PanelItemAcceptorAspect for ItemAcceptor {
|
|
||||||
fn capture_item(node: Arc<Node>, _calling_client: Arc<Client>, item: Arc<Node>) -> Result<()> {
|
|
||||||
super::acceptor_capture_item_flex(node, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: Backend> PanelItemTrait for PanelItem<B> {
|
|
||||||
fn backend(&self) -> &dyn Backend {
|
|
||||||
self.backend.as_ref()
|
|
||||||
}
|
|
||||||
fn send_ui_item_created(&self, node: &Node, item: &Arc<Node>) {
|
|
||||||
let Ok(init_data) = self.backend.start_data() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = panel_item_ui_client::create_item(node, item, init_data);
|
|
||||||
}
|
|
||||||
fn send_acceptor_item_created(&self, node: &Node, item: &Arc<Node>) {
|
|
||||||
let Ok(init_data) = self.backend.start_data() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = panel_item_acceptor_client::capture_item(node, item, init_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<B: Backend> Drop for PanelItem<B> {
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Dropped panel item, basically just a debug breakpoint place
|
// Dropped panel item, basically just a debug breakpoint place
|
||||||
info!("Dropped panel item");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
create_interface!(ItemInterface);
|
|
||||||
impl InterfaceAspect for ItemInterface {
|
|
||||||
#[doc = "Register this client to manage the items of a certain type and create default 3D UI for them."]
|
|
||||||
fn register_panel_item_ui(_node: Arc<Node>, calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
register_item_ui_flex(calling_client, &ITEM_TYPE_INFO_PANEL)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Create an item acceptor to allow temporary ownership of a given type of item. Creates a node at `/item/<item_type>/acceptor/<name>`."]
|
|
||||||
fn create_panel_item_acceptor(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
id: u64,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
transform: Transform,
|
|
||||||
field: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
create_item_acceptor_flex(
|
|
||||||
calling_client,
|
|
||||||
id,
|
|
||||||
parent,
|
|
||||||
transform,
|
|
||||||
&ITEM_TYPE_INFO_PANEL,
|
|
||||||
field,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
304
src/nodes/mod.rs
304
src/nodes/mod.rs
@@ -3,30 +3,41 @@ pub mod audio;
|
|||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod drawable;
|
pub mod drawable;
|
||||||
pub mod fields;
|
pub mod fields;
|
||||||
|
pub mod hmd;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod items;
|
pub mod items;
|
||||||
pub mod root;
|
pub mod root;
|
||||||
pub mod spatial;
|
pub mod spatial;
|
||||||
|
|
||||||
use self::alias::Alias;
|
|
||||||
use crate::core::client::Client;
|
|
||||||
use crate::core::registry::Registry;
|
|
||||||
use crate::core::scenegraph::MethodResponseSender;
|
|
||||||
use color_eyre::eyre::{eyre, Result};
|
use color_eyre::eyre::{eyre, Result};
|
||||||
|
use nanoid::nanoid;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use portable_atomic::{AtomicBool, Ordering};
|
use portable_atomic::{AtomicBool, Ordering};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
|
||||||
use spatial::Spatial;
|
|
||||||
use stardust_xr::messenger::MessageSenderHandle;
|
use stardust_xr::messenger::MessageSenderHandle;
|
||||||
use stardust_xr::scenegraph::ScenegraphError;
|
use stardust_xr::scenegraph::ScenegraphError;
|
||||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
use stardust_xr::schemas::flex::deserialize;
|
||||||
use std::any::{Any, TypeId};
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
use std::future::Future;
|
||||||
use std::os::fd::OwnedFd;
|
use std::os::fd::OwnedFd;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
use crate::core::client::Client;
|
||||||
|
use crate::core::registry::Registry;
|
||||||
|
use crate::core::scenegraph::MethodResponseSender;
|
||||||
|
|
||||||
|
use self::alias::Alias;
|
||||||
|
use self::audio::Sound;
|
||||||
|
use self::data::{PulseReceiver, PulseSender};
|
||||||
|
use self::drawable::Drawable;
|
||||||
|
use self::fields::Field;
|
||||||
|
use self::input::{InputHandler, InputMethod};
|
||||||
|
use self::items::{Item, ItemAcceptor, ItemUI};
|
||||||
|
use self::spatial::zone::Zone;
|
||||||
|
use self::spatial::Spatial;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
@@ -46,54 +57,91 @@ impl AsRef<[u8]> for Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Signal = fn(Arc<Node>, Arc<Client>, Message) -> Result<()>;
|
pub type Signal = fn(&Node, Arc<Client>, Message) -> Result<()>;
|
||||||
pub type Method = fn(Arc<Node>, Arc<Client>, Message, MethodResponseSender);
|
pub type Method = fn(&Node, Arc<Client>, Message, MethodResponseSender);
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_node_protocol!();
|
|
||||||
|
|
||||||
pub struct OwnedNode(pub Arc<Node>);
|
|
||||||
impl Drop for OwnedNode {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.0.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Node {
|
pub struct Node {
|
||||||
enabled: AtomicBool,
|
pub enabled: Arc<AtomicBool>,
|
||||||
id: u64,
|
pub(super) uid: String,
|
||||||
|
path: String,
|
||||||
client: Weak<Client>,
|
client: Weak<Client>,
|
||||||
message_sender_handle: Option<MessageSenderHandle>,
|
message_sender_handle: Option<MessageSenderHandle>,
|
||||||
|
// trailing_slash_pos: usize,
|
||||||
local_signals: Mutex<FxHashMap<u64, Signal>>,
|
local_signals: Mutex<FxHashMap<String, Signal>>,
|
||||||
local_methods: Mutex<FxHashMap<u64, Method>>,
|
local_methods: Mutex<FxHashMap<String, Method>>,
|
||||||
aliases: Registry<Alias>,
|
|
||||||
aspects: Aspects,
|
|
||||||
destroyable: bool,
|
destroyable: bool,
|
||||||
|
|
||||||
|
pub alias: OnceCell<Arc<Alias>>,
|
||||||
|
aliases: Registry<Alias>,
|
||||||
|
|
||||||
|
pub spatial: OnceCell<Arc<Spatial>>,
|
||||||
|
pub field: OnceCell<Arc<Field>>,
|
||||||
|
pub zone: OnceCell<Arc<Zone>>,
|
||||||
|
|
||||||
|
// Data
|
||||||
|
pub pulse_sender: OnceCell<Arc<PulseSender>>,
|
||||||
|
pub pulse_receiver: OnceCell<Arc<PulseReceiver>>,
|
||||||
|
|
||||||
|
// Drawable
|
||||||
|
pub drawable: OnceCell<Drawable>,
|
||||||
|
|
||||||
|
// Input
|
||||||
|
pub input_method: OnceCell<Arc<InputMethod>>,
|
||||||
|
pub input_handler: OnceCell<Arc<InputHandler>>,
|
||||||
|
|
||||||
|
// Item
|
||||||
|
pub item: OnceCell<Arc<Item>>,
|
||||||
|
pub item_acceptor: OnceCell<Arc<ItemAcceptor>>,
|
||||||
|
pub item_ui: OnceCell<Arc<ItemUI>>,
|
||||||
|
|
||||||
|
// Sound
|
||||||
|
pub sound: OnceCell<Arc<Sound>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node {
|
impl Node {
|
||||||
pub fn get_client(&self) -> Option<Arc<Client>> {
|
pub fn get_client(&self) -> Option<Arc<Client>> {
|
||||||
self.client.upgrade()
|
self.client.upgrade()
|
||||||
}
|
}
|
||||||
pub fn get_id(&self) -> u64 {
|
// pub fn get_name(&self) -> &str {
|
||||||
self.id
|
// &self.path[self.trailing_slash_pos + 1..]
|
||||||
|
// }
|
||||||
|
pub fn get_path(&self) -> &str {
|
||||||
|
self.path.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate(client: &Arc<Client>, destroyable: bool) -> Self {
|
pub fn create(client: &Arc<Client>, parent: &str, name: &str, destroyable: bool) -> Self {
|
||||||
Self::from_id(client, client.generate_id(), destroyable)
|
let mut path = parent.to_string();
|
||||||
}
|
path.push('/');
|
||||||
pub fn from_id(client: &Arc<Client>, id: u64, destroyable: bool) -> Self {
|
path.push_str(name);
|
||||||
let node = Node {
|
let node = Node {
|
||||||
enabled: AtomicBool::new(true),
|
enabled: Arc::new(AtomicBool::new(true)),
|
||||||
|
uid: nanoid!(),
|
||||||
client: Arc::downgrade(client),
|
client: Arc::downgrade(client),
|
||||||
message_sender_handle: client.message_sender_handle.clone(),
|
message_sender_handle: client.message_sender_handle.clone(),
|
||||||
id,
|
path,
|
||||||
|
// trailing_slash_pos: parent.len(),
|
||||||
local_signals: Default::default(),
|
local_signals: Default::default(),
|
||||||
local_methods: Default::default(),
|
local_methods: Default::default(),
|
||||||
aliases: Default::default(),
|
|
||||||
aspects: Default::default(),
|
|
||||||
destroyable,
|
destroyable,
|
||||||
|
|
||||||
|
alias: OnceCell::new(),
|
||||||
|
aliases: Registry::new(),
|
||||||
|
|
||||||
|
spatial: OnceCell::new(),
|
||||||
|
field: OnceCell::new(),
|
||||||
|
zone: OnceCell::new(),
|
||||||
|
pulse_sender: OnceCell::new(),
|
||||||
|
pulse_receiver: OnceCell::new(),
|
||||||
|
drawable: OnceCell::new(),
|
||||||
|
input_method: OnceCell::new(),
|
||||||
|
input_handler: OnceCell::new(),
|
||||||
|
item: OnceCell::new(),
|
||||||
|
item_acceptor: OnceCell::new(),
|
||||||
|
item_ui: OnceCell::new(),
|
||||||
|
sound: OnceCell::new(),
|
||||||
};
|
};
|
||||||
<Node as OwnedAspect>::add_node_members(&node);
|
node.add_local_signal("set_enabled", Node::set_enabled_flex);
|
||||||
|
node.add_local_signal("destroy", Node::destroy_flex);
|
||||||
node
|
node
|
||||||
}
|
}
|
||||||
pub fn add_to_scenegraph(self) -> Result<Arc<Node>> {
|
pub fn add_to_scenegraph(self) -> Result<Arc<Node>> {
|
||||||
@@ -103,38 +151,24 @@ impl Node {
|
|||||||
.scenegraph
|
.scenegraph
|
||||||
.add_node(self))
|
.add_node(self))
|
||||||
}
|
}
|
||||||
pub fn add_to_scenegraph_owned(self) -> Result<OwnedNode> {
|
|
||||||
Ok(OwnedNode(
|
|
||||||
self.get_client()
|
|
||||||
.ok_or_else(|| eyre!("Internal: Unable to get client"))?
|
|
||||||
.scenegraph
|
|
||||||
.add_node(self),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
pub fn enabled(&self) -> bool {
|
|
||||||
self.enabled.load(Ordering::Relaxed)
|
|
||||||
&& if let Ok(spatial) = self.get_aspect::<Spatial>() {
|
|
||||||
spatial
|
|
||||||
.global_transform()
|
|
||||||
.to_scale_rotation_translation()
|
|
||||||
.0
|
|
||||||
.length_squared() > 0.0
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_enabled(&self, enabled: bool) {
|
|
||||||
self.enabled.store(enabled, Ordering::Relaxed)
|
|
||||||
}
|
|
||||||
pub fn destroy(&self) {
|
pub fn destroy(&self) {
|
||||||
if let Some(client) = self.get_client() {
|
if let Some(client) = self.get_client() {
|
||||||
client.scenegraph.remove_node(self.get_id());
|
client.scenegraph.remove_node(self.get_path());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_enabled_flex(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
node.enabled
|
||||||
|
.store(deserialize(message.as_ref())?, Ordering::Relaxed);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
// very much up for debate if we should allow this, as you can match objects using this
|
// very much up for debate if we should allow this, as you can match objects using this
|
||||||
// pub fn get_client_pid_flex(
|
// pub fn get_client_pid_flex(
|
||||||
// node: Arc<Node>,
|
// node: &Node,
|
||||||
// _calling_client: Arc<Client>,
|
// _calling_client: Arc<Client>,
|
||||||
// _message: Message,
|
// _message: Message,
|
||||||
// ) -> Result<Message> {
|
// ) -> Result<Message> {
|
||||||
@@ -145,32 +179,46 @@ impl Node {
|
|||||||
// let pid = client.pid.ok_or_else(|| eyre!("Client PID is unknown"))?;
|
// let pid = client.pid.ok_or_else(|| eyre!("Client PID is unknown"))?;
|
||||||
// Ok(serialize(pid)?.into())
|
// Ok(serialize(pid)?.into())
|
||||||
// }
|
// }
|
||||||
|
pub fn destroy_flex(
|
||||||
pub fn add_local_signal(&self, id: u64, signal: Signal) {
|
node: &Node,
|
||||||
self.local_signals.lock().insert(id, signal);
|
_calling_client: Arc<Client>,
|
||||||
}
|
_message: Message,
|
||||||
pub fn add_local_method(&self, id: u64, method: Method) {
|
) -> Result<()> {
|
||||||
self.local_methods.lock().insert(id, method);
|
if node.destroyable {
|
||||||
|
node.destroy();
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_aspect<A: Aspect>(&self, aspect: A) -> Arc<A> {
|
pub fn add_local_signal(&self, name: &str, signal: Signal) {
|
||||||
self.aspects.add(aspect)
|
self.local_signals.lock().insert(name.to_string(), signal);
|
||||||
}
|
}
|
||||||
pub fn add_aspect_raw<A: Aspect>(&self, aspect: Arc<A>) {
|
pub fn add_local_method(&self, name: &str, method: Method) {
|
||||||
self.aspects.add_raw(aspect)
|
self.local_methods.lock().insert(name.to_string(), method);
|
||||||
}
|
}
|
||||||
pub fn get_aspect<A: Aspect>(&self) -> Result<Arc<A>> {
|
|
||||||
self.aspects.get()
|
pub fn get_aspect<F, T>(
|
||||||
|
&self,
|
||||||
|
node_name: &'static str,
|
||||||
|
aspect_type: &'static str,
|
||||||
|
aspect_fn: F,
|
||||||
|
) -> Result<&T>
|
||||||
|
where
|
||||||
|
F: FnOnce(&Node) -> &OnceCell<T>,
|
||||||
|
{
|
||||||
|
aspect_fn(self)
|
||||||
|
.get()
|
||||||
|
.ok_or_else(|| eyre!("{} is not a {} node", node_name, aspect_type))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_local_signal(
|
pub fn send_local_signal(
|
||||||
self: Arc<Self>,
|
&self,
|
||||||
calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
method: u64,
|
method: &str,
|
||||||
message: Message,
|
message: Message,
|
||||||
) -> Result<(), ScenegraphError> {
|
) -> Result<(), ScenegraphError> {
|
||||||
if let Ok(alias) = self.get_aspect::<Alias>() {
|
if let Some(alias) = self.alias.get() {
|
||||||
if !alias.info.server_signals.iter().any(|e| *e == method) {
|
if !alias.info.server_signals.iter().any(|e| e == &method) {
|
||||||
return Err(ScenegraphError::SignalNotFound);
|
return Err(ScenegraphError::SignalNotFound);
|
||||||
}
|
}
|
||||||
alias
|
alias
|
||||||
@@ -182,7 +230,7 @@ impl Node {
|
|||||||
let signal = self
|
let signal = self
|
||||||
.local_signals
|
.local_signals
|
||||||
.lock()
|
.lock()
|
||||||
.get(&method)
|
.get(method)
|
||||||
.cloned()
|
.cloned()
|
||||||
.ok_or(ScenegraphError::SignalNotFound)?;
|
.ok_or(ScenegraphError::SignalNotFound)?;
|
||||||
signal(self, calling_client, message).map_err(|error| ScenegraphError::SignalError {
|
signal(self, calling_client, message).map_err(|error| ScenegraphError::SignalError {
|
||||||
@@ -191,14 +239,14 @@ impl Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn execute_local_method(
|
pub fn execute_local_method(
|
||||||
self: Arc<Self>,
|
&self,
|
||||||
calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
method: u64,
|
method: &str,
|
||||||
message: Message,
|
message: Message,
|
||||||
response: MethodResponseSender,
|
response: MethodResponseSender,
|
||||||
) {
|
) {
|
||||||
if let Ok(alias) = self.get_aspect::<Alias>() {
|
if let Some(alias) = self.alias.get() {
|
||||||
if !alias.info.server_methods.iter().any(|e| *e == method) {
|
if !alias.info.server_methods.iter().any(|e| e == &method) {
|
||||||
response.send(Err(ScenegraphError::MethodNotFound));
|
response.send(Err(ScenegraphError::MethodNotFound));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -216,14 +264,14 @@ impl Node {
|
|||||||
response,
|
response,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let Some(method) = self.local_methods.lock().get(&method).cloned() else {
|
let Some(method) = self.local_methods.lock().get(method).cloned() else {
|
||||||
response.send(Err(ScenegraphError::MethodNotFound));
|
response.send(Err(ScenegraphError::MethodNotFound));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
method(self, calling_client, message, response);
|
method(self, calling_client, message, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn send_remote_signal(&self, method: u64, message: impl Into<Message>) -> Result<()> {
|
pub fn send_remote_signal(&self, method: &str, message: impl Into<Message>) -> Result<()> {
|
||||||
let message = message.into();
|
let message = message.into();
|
||||||
self.aliases
|
self.aliases
|
||||||
.get_valid_contents()
|
.get_valid_contents()
|
||||||
@@ -240,92 +288,48 @@ impl Node {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
let path = self.path.clone();
|
||||||
|
let method = method.to_string();
|
||||||
if let Some(handle) = self.message_sender_handle.as_ref() {
|
if let Some(handle) = self.message_sender_handle.as_ref() {
|
||||||
handle.signal(self.id, method, &message.data, message.fds)?;
|
handle.signal(path.as_str(), method.as_str(), &message.data, message.fds)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub async fn execute_remote_method_typed<S: Serialize, D: DeserializeOwned>(
|
pub fn execute_remote_method(
|
||||||
&self,
|
&self,
|
||||||
method: u64,
|
method: &str,
|
||||||
input: S,
|
message: impl Into<Message>,
|
||||||
fds: Vec<OwnedFd>,
|
) -> Result<impl Future<Output = Result<Message>>> {
|
||||||
) -> Result<(D, Vec<OwnedFd>)> {
|
let message = message.into();
|
||||||
let message_sender_handle = self
|
let message_sender_handle = self
|
||||||
.message_sender_handle
|
.message_sender_handle
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(eyre!("Messenger does not exist for this node"))?;
|
.ok_or(eyre!("Messenger does not exist for this node"))?;
|
||||||
|
|
||||||
let serialized = serialize(input)?;
|
let future =
|
||||||
let result = message_sender_handle
|
message_sender_handle.method(self.path.as_str(), method, &message.data, message.fds)?;
|
||||||
.method(self.id, method, &serialized, fds)?
|
|
||||||
.await
|
|
||||||
.map_err(|e| eyre!(e))?;
|
|
||||||
|
|
||||||
let (message, fds) = result.into_components();
|
Ok(async {
|
||||||
let deserialized: D = deserialize(&message)?;
|
match future.await {
|
||||||
Ok((deserialized, fds))
|
Ok(m) => {
|
||||||
|
let (data, fds) = m.into_components();
|
||||||
|
Ok(Message { data, fds })
|
||||||
|
}
|
||||||
|
Err(e) => Err(eyre!(e)),
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Debug for Node {
|
impl Debug for Node {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("Node")
|
f.debug_struct("Node")
|
||||||
.field("id", &self.id)
|
.field("uid", &self.uid)
|
||||||
.field("local_signals", &self.local_signals.lock().keys())
|
.field("path", &self.path)
|
||||||
.field("local_methods", &self.local_methods.lock().keys())
|
|
||||||
.field("destroyable", &self.destroyable)
|
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl OwnedAspect for Node {
|
|
||||||
fn set_enabled(node: Arc<Node>, _calling_client: Arc<Client>, enabled: bool) -> Result<()> {
|
|
||||||
node.set_enabled(enabled);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn destroy(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
if node.destroyable {
|
|
||||||
node.destroy();
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for Node {
|
impl Drop for Node {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Debug breakpoint
|
// Debug breakpoint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Aspect: Any + Send + Sync + 'static {
|
|
||||||
const NAME: &'static str;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct Aspects(Mutex<FxHashMap<TypeId, Arc<dyn Any + Send + Sync + 'static>>>);
|
|
||||||
|
|
||||||
impl Aspects {
|
|
||||||
fn add<A: Aspect>(&self, t: A) -> Arc<A> {
|
|
||||||
let aspect = Arc::new(t);
|
|
||||||
self.add_raw(aspect.clone());
|
|
||||||
aspect
|
|
||||||
}
|
|
||||||
fn add_raw<A: Aspect>(&self, aspect: Arc<A>) {
|
|
||||||
self.0.lock().insert(Self::type_key::<A>(), aspect);
|
|
||||||
}
|
|
||||||
fn get<A: Aspect>(&self) -> Result<Arc<A>> {
|
|
||||||
self.0
|
|
||||||
.lock()
|
|
||||||
.get(&Self::type_key::<A>())
|
|
||||||
.and_then(|a| Arc::downcast(a.clone()).ok())
|
|
||||||
.ok_or(eyre!("Couldn't get aspect {}", A::NAME.to_lowercase()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_key<A: 'static>() -> TypeId {
|
|
||||||
TypeId::of::<A>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for Aspects {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.0.lock().clear()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,104 +1,138 @@
|
|||||||
use super::spatial::Spatial;
|
use super::spatial::Spatial;
|
||||||
use super::Node;
|
use super::{Message, Node};
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use crate::core::client_state::ClientStateParsed;
|
use crate::core::client_state::{ClientState, ClientStateInternal};
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::nodes::spatial::SPATIAL_REF_ASPECT_ALIAS_INFO;
|
use crate::core::scenegraph::MethodResponseSender;
|
||||||
use crate::session::connection_env;
|
use crate::wayland::WAYLAND_DISPLAY;
|
||||||
use color_eyre::eyre::{bail, Result};
|
use crate::STARDUST_INSTANCE;
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use std::path::PathBuf;
|
use rustc_hash::FxHashMap;
|
||||||
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Instant;
|
|
||||||
use tracing::info;
|
|
||||||
|
|
||||||
static ROOT_REGISTRY: Registry<Root> = Registry::new();
|
static ROOT_REGISTRY: Registry<Root> = Registry::new();
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_root_protocol!();
|
|
||||||
|
|
||||||
pub struct Root {
|
pub struct Root {
|
||||||
node: Arc<Node>,
|
pub node: Arc<Node>,
|
||||||
connect_instant: Instant,
|
send_frame_event: AtomicBool,
|
||||||
}
|
}
|
||||||
impl Root {
|
impl Root {
|
||||||
pub fn create(client: &Arc<Client>, transform: Mat4) -> Result<Arc<Self>> {
|
pub fn create(client: &Arc<Client>) -> Result<Arc<Self>> {
|
||||||
let node = Node::from_id(client, 0, false);
|
let node = Node::create(client, "", "", false);
|
||||||
<Self as RootAspect>::add_node_members(&node);
|
node.add_local_signal("subscribe_frame", Root::subscribe_frame_flex);
|
||||||
|
node.add_local_signal("set_base_prefixes", Root::set_base_prefixes_flex);
|
||||||
|
node.add_local_method("state_token", Root::state_token_flex);
|
||||||
|
node.add_local_method(
|
||||||
|
"get_connection_environment",
|
||||||
|
get_connection_environment_flex,
|
||||||
|
);
|
||||||
let node = node.add_to_scenegraph()?;
|
let node = node.add_to_scenegraph()?;
|
||||||
let _ = Spatial::add_to(&node, None, transform, false);
|
let _ = Spatial::add_to(&node, None, client.state.root, false);
|
||||||
|
|
||||||
Ok(ROOT_REGISTRY.add(Root {
|
Ok(ROOT_REGISTRY.add(Root {
|
||||||
node,
|
node,
|
||||||
connect_instant: Instant::now(),
|
send_frame_event: AtomicBool::from(false),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscribe_frame_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
_message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
calling_client
|
||||||
|
.root
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.send_frame_event
|
||||||
|
.store(true, Ordering::Relaxed);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug")]
|
||||||
pub fn send_frame_events(delta: f64) {
|
pub fn send_frame_events(delta: f64) {
|
||||||
for root in ROOT_REGISTRY.get_valid_contents() {
|
if let Ok(data) = serialize((delta, 0.0)) {
|
||||||
let _ = root_client::frame(
|
for root in ROOT_REGISTRY.get_valid_contents() {
|
||||||
&root.node,
|
if root.send_frame_event.load(Ordering::Relaxed) {
|
||||||
&FrameInfo {
|
let _ = root.node.send_remote_signal("frame", data.clone());
|
||||||
delta: delta as f32,
|
}
|
||||||
elapsed: root.connect_instant.elapsed().as_secs_f32(),
|
}
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_base_prefixes_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
*calling_client.base_resource_prefixes.lock() = deserialize(message.as_ref())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn state_token_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(|| {
|
||||||
|
let state: ClientStateInternal = deserialize(message.as_ref())?;
|
||||||
|
let token = ClientState::from_deserialized(&calling_client, state).token();
|
||||||
|
Ok(serialize(token)?.into())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_transform(&self, transform: Mat4) {
|
pub fn set_transform(&self, transform: Mat4) {
|
||||||
let spatial = self.node.get_aspect::<Spatial>().unwrap();
|
let spatial = self.node.spatial.get().unwrap();
|
||||||
spatial.set_spatial_parent(None).unwrap();
|
spatial.set_spatial_parent(None).unwrap();
|
||||||
spatial.set_local_transform(transform);
|
spatial.set_local_transform(transform);
|
||||||
}
|
}
|
||||||
pub async fn save_state(&self) -> Result<ClientState> {
|
pub fn save_state(&self) -> impl Future<Output = Result<ClientStateInternal>> {
|
||||||
Ok(root_client::save_state(&self.node).await?.0)
|
let future = self
|
||||||
|
.node
|
||||||
|
.execute_remote_method("save_state", Message::default());
|
||||||
|
async move { Ok(deserialize(&future?.await?.data)?) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl RootAspect for Root {
|
|
||||||
async fn get_state(_node: Arc<Node>, calling_client: Arc<Client>) -> Result<ClientState> {
|
|
||||||
let Some(state) = calling_client.state.get() else {
|
|
||||||
bail!("Couldn't get state");
|
|
||||||
};
|
|
||||||
Ok(state.clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Get a hashmap of all the environment variables to connect a given app to the stardust server"]
|
|
||||||
async fn get_connection_environment(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
) -> Result<stardust_xr::values::Map<String, String>> {
|
|
||||||
Ok(connection_env())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Generate a client state token and return it back.\n\n When launching a new client, set the environment variable `STARDUST_STARTUP_TOKEN` to the returned string.\n Make sure the environment variable shows in `/proc/{pid}/environ` as that's the only reliable way to pass the value to the server (suggestions welcome).\n"]
|
|
||||||
async fn generate_state_token(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
state: ClientState,
|
|
||||||
) -> Result<String> {
|
|
||||||
Ok(ClientStateParsed::from_deserialized(&calling_client, state).token())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Set initial list of folders to look for namespaced resources in"]
|
|
||||||
fn set_base_prefixes(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
prefixes: Vec<String>,
|
|
||||||
) -> Result<()> {
|
|
||||||
info!(?calling_client, ?prefixes, "Set base prefixes");
|
|
||||||
*calling_client.base_resource_prefixes.lock() =
|
|
||||||
prefixes.into_iter().map(PathBuf::from).collect();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Cleanly disconnect from the server"]
|
|
||||||
fn disconnect(_node: Arc<Node>, calling_client: Arc<Client>) -> color_eyre::eyre::Result<()> {
|
|
||||||
calling_client.disconnect(Ok(()));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for Root {
|
impl Drop for Root {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
ROOT_REGISTRY.remove(self);
|
ROOT_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! var_env_insert {
|
||||||
|
($env:ident, $name:ident) => {
|
||||||
|
$env.insert(stringify!($name).to_string(), $name.get().unwrap().clone());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn get_connection_environment_flex(
|
||||||
|
_node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
_message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let mut env: FxHashMap<String, String> = FxHashMap::default();
|
||||||
|
var_env_insert!(env, STARDUST_INSTANCE);
|
||||||
|
#[cfg(feature = "wayland")]
|
||||||
|
{
|
||||||
|
var_env_insert!(env, WAYLAND_DISPLAY);
|
||||||
|
#[cfg(feature = "xwayland")]
|
||||||
|
var_env_insert!(env, DISPLAY);
|
||||||
|
env.insert("GDK_BACKEND".to_string(), "wayland".to_string());
|
||||||
|
env.insert("QT_QPA_PLATFORM".to_string(), "wayland".to_string());
|
||||||
|
env.insert("MOZ_ENABLE_WAYLAND".to_string(), "1".to_string());
|
||||||
|
env.insert("CLUTTER_BACKEND".to_string(), "wayland".to_string());
|
||||||
|
env.insert("SDL_VIDEODRIVER".to_string(), "wayland".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(serialize(env)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,70 +1,49 @@
|
|||||||
pub mod zone;
|
pub mod zone;
|
||||||
|
|
||||||
use self::zone::Zone;
|
use self::zone::{create_zone_flex, Zone};
|
||||||
use super::alias::Alias;
|
use super::{Message, Node};
|
||||||
use super::fields::{Field, FieldTrait};
|
|
||||||
use super::Aspect;
|
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::create_interface;
|
use crate::core::scenegraph::MethodResponseSender;
|
||||||
use crate::nodes::{Node, OWNED_ASPECT_ALIAS_INFO};
|
use color_eyre::eyre::{ensure, eyre, Result};
|
||||||
use color_eyre::eyre::{eyre, OptionExt, Result};
|
use glam::{vec3a, Mat4, Quat};
|
||||||
use glam::{vec3a, Mat4, Quat, Vec3};
|
|
||||||
use mint::Vector3;
|
use mint::Vector3;
|
||||||
use once_cell::sync::OnceCell;
|
use nanoid::nanoid;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
|
use stardust_xr::values::Transform;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, OnceLock, Weak};
|
||||||
use stereokit_rust::maths::Bounds;
|
use stereokit::{bounds_grow_to_fit_box, Bounds};
|
||||||
|
|
||||||
stardust_xr_server_codegen::codegen_spatial_protocol!();
|
|
||||||
impl Transform {
|
|
||||||
pub fn to_mat4(&self, position: bool, rotation: bool, scale: bool) -> Mat4 {
|
|
||||||
let position = position
|
|
||||||
.then_some(self.translation)
|
|
||||||
.flatten()
|
|
||||||
.unwrap_or_else(|| Vector3::from([0.0; 3]));
|
|
||||||
let rotation = rotation
|
|
||||||
.then_some(self.rotation)
|
|
||||||
.flatten()
|
|
||||||
.unwrap_or_else(|| Quat::IDENTITY.into());
|
|
||||||
let scale = scale
|
|
||||||
.then_some(self.scale)
|
|
||||||
.flatten()
|
|
||||||
.unwrap_or_else(|| Vector3::from([1.0; 3]));
|
|
||||||
|
|
||||||
Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
pub static ref EXPORTED_SPATIALS: Mutex<FxHashMap<u64, Arc<Node>>> = Mutex::new(FxHashMap::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
static ZONEABLE_REGISTRY: Registry<Spatial> = Registry::new();
|
static ZONEABLE_REGISTRY: Registry<Spatial> = Registry::new();
|
||||||
|
|
||||||
pub struct Spatial {
|
pub struct Spatial {
|
||||||
pub node: Weak<Node>,
|
uid: String,
|
||||||
|
pub(super) node: Weak<Node>,
|
||||||
|
self_ref: Weak<Spatial>,
|
||||||
parent: Mutex<Option<Arc<Spatial>>>,
|
parent: Mutex<Option<Arc<Spatial>>>,
|
||||||
old_parent: Mutex<Option<Arc<Spatial>>>,
|
old_parent: Mutex<Option<Arc<Spatial>>>,
|
||||||
transform: Mutex<Mat4>,
|
pub(super) transform: Mutex<Mat4>,
|
||||||
zone: Mutex<Weak<Zone>>,
|
zone: Mutex<Weak<Zone>>,
|
||||||
children: Registry<Spatial>,
|
children: Registry<Spatial>,
|
||||||
pub bounding_box_calc: OnceCell<fn(&Node) -> Bounds>,
|
pub(super) bounding_box_calc: OnceLock<fn(&Node) -> Bounds>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Spatial {
|
impl Spatial {
|
||||||
pub fn new(node: Weak<Node>, parent: Option<Arc<Spatial>>, transform: Mat4) -> Arc<Self> {
|
pub fn new(node: Weak<Node>, parent: Option<Arc<Spatial>>, transform: Mat4) -> Arc<Self> {
|
||||||
Arc::new(Spatial {
|
Arc::new_cyclic(|self_ref| Spatial {
|
||||||
|
uid: nanoid!(),
|
||||||
node,
|
node,
|
||||||
|
self_ref: self_ref.clone(),
|
||||||
parent: Mutex::new(parent),
|
parent: Mutex::new(parent),
|
||||||
old_parent: Mutex::new(None),
|
old_parent: Mutex::new(None),
|
||||||
transform: Mutex::new(transform),
|
transform: Mutex::new(transform),
|
||||||
zone: Mutex::new(Weak::new()),
|
zone: Mutex::new(Weak::new()),
|
||||||
children: Registry::new(),
|
children: Registry::new(),
|
||||||
bounding_box_calc: OnceCell::default(),
|
bounding_box_calc: OnceLock::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn add_to(
|
pub fn add_to(
|
||||||
@@ -72,19 +51,29 @@ impl Spatial {
|
|||||||
parent: Option<Arc<Spatial>>,
|
parent: Option<Arc<Spatial>>,
|
||||||
transform: Mat4,
|
transform: Mat4,
|
||||||
zoneable: bool,
|
zoneable: bool,
|
||||||
) -> Arc<Spatial> {
|
) -> Result<Arc<Spatial>> {
|
||||||
let spatial = Spatial::new(Arc::downgrade(node), parent.clone(), transform);
|
ensure!(
|
||||||
<Spatial as SpatialAspect>::add_node_members(node);
|
node.spatial.get().is_none(),
|
||||||
|
"Internal: Node already has a Spatial aspect!"
|
||||||
|
);
|
||||||
|
let spatial = Spatial::new(Arc::downgrade(node), parent, transform);
|
||||||
|
node.add_local_method("get_bounding_box", Spatial::get_bounding_box_flex);
|
||||||
|
node.add_local_method("get_transform", Spatial::get_transform_flex);
|
||||||
|
node.add_local_signal("set_transform", Spatial::set_transform_flex);
|
||||||
|
node.add_local_signal("set_spatial_parent", Spatial::set_spatial_parent_flex);
|
||||||
|
node.add_local_signal(
|
||||||
|
"set_spatial_parent_in_place",
|
||||||
|
Spatial::set_spatial_parent_in_place_flex,
|
||||||
|
);
|
||||||
|
node.add_local_signal("set_zoneable", Spatial::set_zoneable_flex);
|
||||||
|
node.add_local_method("field_distance", Spatial::field_distance_flex);
|
||||||
|
node.add_local_method("field_normal", Spatial::field_normal_flex);
|
||||||
|
node.add_local_method("field_closest_point", Spatial::field_closest_point_flex);
|
||||||
if zoneable {
|
if zoneable {
|
||||||
ZONEABLE_REGISTRY.add_raw(&spatial);
|
ZONEABLE_REGISTRY.add_raw(&spatial);
|
||||||
}
|
}
|
||||||
if let Some(parent) = parent {
|
let _ = node.spatial.set(spatial.clone());
|
||||||
parent.children.add_raw(&spatial);
|
Ok(spatial)
|
||||||
}
|
|
||||||
<Spatial as SpatialRefAspect>::add_node_members(node);
|
|
||||||
<Spatial as SpatialAspect>::add_node_members(node);
|
|
||||||
node.add_aspect_raw(spatial.clone());
|
|
||||||
spatial
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn node(&self) -> Option<Arc<Node>> {
|
pub fn node(&self) -> Option<Arc<Node>> {
|
||||||
@@ -99,16 +88,18 @@ impl Spatial {
|
|||||||
|
|
||||||
// the output bounds are probably way bigger than they need to be
|
// the output bounds are probably way bigger than they need to be
|
||||||
pub fn get_bounding_box(&self) -> Bounds {
|
pub fn get_bounding_box(&self) -> Bounds {
|
||||||
let Some(node) = self.node() else {
|
let Some(node) = self.node() else {return Bounds::default()};
|
||||||
return Bounds::default();
|
|
||||||
};
|
|
||||||
let mut bounds = self
|
let mut bounds = self
|
||||||
.bounding_box_calc
|
.bounding_box_calc
|
||||||
.get()
|
.get()
|
||||||
.map(|b| (b)(&node))
|
.map(|b| (b)(&node))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
for child in self.children.get_valid_contents() {
|
for child in self.children.get_valid_contents() {
|
||||||
bounds.grown_box(child.get_bounding_box(), child.local_transform());
|
bounds = bounds_grow_to_fit_box(
|
||||||
|
bounds,
|
||||||
|
child.get_bounding_box(),
|
||||||
|
Some(child.local_transform()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
bounds
|
bounds
|
||||||
}
|
}
|
||||||
@@ -117,12 +108,10 @@ impl Spatial {
|
|||||||
*self.transform.lock()
|
*self.transform.lock()
|
||||||
}
|
}
|
||||||
pub fn global_transform(&self) -> Mat4 {
|
pub fn global_transform(&self) -> Mat4 {
|
||||||
let parent_transform = self
|
match self.get_parent() {
|
||||||
.get_parent()
|
Some(value) => value.global_transform() * *self.transform.lock(),
|
||||||
.as_deref()
|
None => *self.transform.lock(),
|
||||||
.map(Self::global_transform)
|
}
|
||||||
.unwrap_or_default();
|
|
||||||
parent_transform * self.local_transform()
|
|
||||||
}
|
}
|
||||||
pub fn set_local_transform(&self, transform: Mat4) {
|
pub fn set_local_transform(&self, transform: Mat4) {
|
||||||
*self.transform.lock() = transform;
|
*self.transform.lock() = transform;
|
||||||
@@ -148,7 +137,7 @@ impl Spatial {
|
|||||||
let (mut reference_space_scl, mut reference_space_rot, mut reference_space_pos) =
|
let (mut reference_space_scl, mut reference_space_rot, mut reference_space_pos) =
|
||||||
local_transform_in_reference_space.to_scale_rotation_translation();
|
local_transform_in_reference_space.to_scale_rotation_translation();
|
||||||
|
|
||||||
if let Some(pos) = transform.translation {
|
if let Some(pos) = transform.position {
|
||||||
reference_space_pos = pos.into()
|
reference_space_pos = pos.into()
|
||||||
}
|
}
|
||||||
if let Some(rot) = transform.rotation {
|
if let Some(rot) = transform.rotation {
|
||||||
@@ -188,21 +177,23 @@ impl Spatial {
|
|||||||
fn get_parent(&self) -> Option<Arc<Spatial>> {
|
fn get_parent(&self) -> Option<Arc<Spatial>> {
|
||||||
self.parent.lock().clone()
|
self.parent.lock().clone()
|
||||||
}
|
}
|
||||||
fn set_parent(self: &Arc<Self>, new_parent: Option<&Arc<Spatial>>) {
|
fn set_parent(&self, new_parent: Option<Arc<Spatial>>) {
|
||||||
if let Some(parent) = self.get_parent() {
|
if let Some(parent) = self.get_parent() {
|
||||||
parent.children.remove(self);
|
parent.children.remove(self);
|
||||||
}
|
}
|
||||||
if let Some(new_parent) = &new_parent {
|
if let Some(new_parent) = &new_parent {
|
||||||
new_parent.children.add_raw(self);
|
new_parent
|
||||||
|
.children
|
||||||
|
.add_raw(&self.self_ref.upgrade().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
*self.parent.lock() = new_parent.cloned();
|
*self.parent.lock() = new_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_spatial_parent(self: &Arc<Self>, parent: Option<&Arc<Spatial>>) -> Result<()> {
|
pub fn set_spatial_parent(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
|
||||||
let is_ancestor = parent
|
let is_ancestor = parent
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|parent| self.is_ancestor_of((*parent).clone()))
|
.map(|parent| self.is_ancestor_of(parent.clone()))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if is_ancestor {
|
if is_ancestor {
|
||||||
return Err(eyre!("Setting spatial parent would cause a loop"));
|
return Err(eyre!("Setting spatial parent would cause a loop"));
|
||||||
@@ -211,13 +202,11 @@ impl Spatial {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn set_spatial_parent_in_place(
|
|
||||||
self: &Arc<Self>,
|
pub fn set_spatial_parent_in_place(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
|
||||||
parent: Option<&Arc<Spatial>>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let is_ancestor = parent
|
let is_ancestor = parent
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|parent| self.is_ancestor_of((*parent).clone()))
|
.map(|parent| self.is_ancestor_of(parent.clone()))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if is_ancestor {
|
if is_ancestor {
|
||||||
return Err(eyre!("Setting spatial parent would cause a loop"));
|
return Err(eyre!("Setting spatial parent would cause a loop"));
|
||||||
@@ -225,157 +214,238 @@ impl Spatial {
|
|||||||
|
|
||||||
self.set_local_transform(Spatial::space_to_space_matrix(
|
self.set_local_transform(Spatial::space_to_space_matrix(
|
||||||
Some(self),
|
Some(self),
|
||||||
parent.map(AsRef::as_ref),
|
parent.as_deref(),
|
||||||
));
|
));
|
||||||
self.set_parent(parent);
|
self.set_parent(parent);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(self) fn zone_distance(&self) -> f32 {
|
pub fn get_bounding_box_flex(
|
||||||
self.zone
|
node: &Node,
|
||||||
.lock()
|
calling_client: Arc<Client>,
|
||||||
.upgrade()
|
message: Message,
|
||||||
.map(|zone| zone.field.clone())
|
response: MethodResponseSender,
|
||||||
.map(|field| field.distance(self, vec3a(0.0, 0.0, 0.0)))
|
) {
|
||||||
.unwrap_or(f32::MAX)
|
response.wrap_sync(move || {
|
||||||
}
|
let this_spatial = node
|
||||||
}
|
.spatial
|
||||||
impl Aspect for Spatial {
|
.get()
|
||||||
const NAME: &'static str = "Spatial";
|
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
|
||||||
}
|
let relative_spatial_path: Option<&str> = deserialize(message.as_ref())?;
|
||||||
impl SpatialRefAspect for Spatial {
|
let bounds = if let Some(relative_spatial_path) = relative_spatial_path {
|
||||||
async fn get_local_bounding_box(
|
let relative_spatial =
|
||||||
node: Arc<Node>,
|
find_reference_space(&calling_client, relative_spatial_path)?;
|
||||||
_calling_client: Arc<Client>,
|
let center =
|
||||||
) -> Result<BoundingBox> {
|
Spatial::space_to_space_matrix(Some(&this_spatial), Some(&relative_spatial))
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
.transform_point3([0.0; 3].into());
|
||||||
let bounds = this_spatial.get_bounding_box();
|
let bounds: Bounds = Bounds {
|
||||||
|
center,
|
||||||
|
dimensions: [0.0; 3].into(),
|
||||||
|
};
|
||||||
|
bounds_grow_to_fit_box(
|
||||||
|
bounds,
|
||||||
|
this_spatial.get_bounding_box(),
|
||||||
|
Some(Spatial::space_to_space_matrix(
|
||||||
|
Some(&this_spatial),
|
||||||
|
Some(&relative_spatial),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
this_spatial.get_bounding_box()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(BoundingBox {
|
Ok(serialize((
|
||||||
center: Vec3::from(bounds.center).into(),
|
mint::Vector3::from(bounds.center),
|
||||||
size: Vec3::from(bounds.dimensions).into(),
|
mint::Vector3::from(bounds.dimensions),
|
||||||
})
|
))?
|
||||||
|
.into())
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_relative_bounding_box(
|
pub fn get_transform_flex(
|
||||||
node: Arc<Node>,
|
node: &Node,
|
||||||
_calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
relative_to: Arc<Node>,
|
message: Message,
|
||||||
) -> Result<BoundingBox> {
|
response: MethodResponseSender,
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
) {
|
||||||
let relative_spatial = relative_to.get_aspect::<Spatial>()?;
|
response.wrap_sync(move || {
|
||||||
let center = Spatial::space_to_space_matrix(Some(&this_spatial), Some(&relative_spatial))
|
let this_spatial = node
|
||||||
.transform_point3([0.0; 3].into());
|
.spatial
|
||||||
let mut bounds = Bounds {
|
.get()
|
||||||
center: center.into(),
|
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
|
||||||
dimensions: [0.0; 3].into(),
|
let relative_spatial =
|
||||||
};
|
find_reference_space(&calling_client, deserialize(message.as_ref())?)?;
|
||||||
bounds.grown_box(
|
|
||||||
this_spatial.get_bounding_box(),
|
let (scale, rotation, position) = Spatial::space_to_space_matrix(
|
||||||
Spatial::space_to_space_matrix(Some(&this_spatial), Some(&relative_spatial)),
|
Some(this_spatial.as_ref()),
|
||||||
|
Some(relative_spatial.as_ref()),
|
||||||
|
)
|
||||||
|
.to_scale_rotation_translation();
|
||||||
|
|
||||||
|
Ok(serialize((
|
||||||
|
mint::Vector3::from(position),
|
||||||
|
mint::Quaternion::from(rotation),
|
||||||
|
mint::Vector3::from(scale),
|
||||||
|
))?
|
||||||
|
.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pub fn set_transform_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TransformArgs<'a> {
|
||||||
|
reference_space_path: Option<&'a str>,
|
||||||
|
transform: Transform,
|
||||||
|
}
|
||||||
|
let transform_args: TransformArgs = deserialize(message.as_ref())?;
|
||||||
|
let reference_space_transform = transform_args
|
||||||
|
.reference_space_path
|
||||||
|
.map(|path| find_reference_space(&calling_client, path))
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
node.spatial.get().unwrap().set_local_transform_components(
|
||||||
|
reference_space_transform.as_deref(),
|
||||||
|
transform_args.transform,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(BoundingBox {
|
|
||||||
center: Vec3::from(bounds.center).into(),
|
|
||||||
size: Vec3::from(bounds.dimensions).into(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_transform(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
relative_to: Arc<Node>,
|
|
||||||
) -> Result<Transform> {
|
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
|
||||||
let relative_spatial = relative_to.get_aspect::<Spatial>()?;
|
|
||||||
|
|
||||||
let (scale, rotation, position) = Spatial::space_to_space_matrix(
|
|
||||||
Some(this_spatial.as_ref()),
|
|
||||||
Some(relative_spatial.as_ref()),
|
|
||||||
)
|
|
||||||
.to_scale_rotation_translation();
|
|
||||||
|
|
||||||
Ok(Transform {
|
|
||||||
translation: Some(position.into()),
|
|
||||||
rotation: Some(rotation.into()),
|
|
||||||
scale: Some(scale.into()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl SpatialAspect for Spatial {
|
|
||||||
fn set_local_transform(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
transform: Transform,
|
|
||||||
) -> Result<()> {
|
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
|
||||||
this_spatial.set_local_transform_components(None, transform);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn set_relative_transform(
|
pub fn set_spatial_parent_flex(
|
||||||
node: Arc<Node>,
|
node: &Node,
|
||||||
_calling_client: Arc<Client>,
|
calling_client: Arc<Client>,
|
||||||
relative_to: Arc<Node>,
|
message: Message,
|
||||||
transform: Transform,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?;
|
||||||
let relative_spatial = relative_to.get_aspect::<Spatial>()?;
|
node.spatial.get().unwrap().set_spatial_parent(Some(parent))
|
||||||
|
}
|
||||||
this_spatial.set_local_transform_components(Some(&relative_spatial), transform);
|
pub fn set_spatial_parent_in_place_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?;
|
||||||
|
node.spatial
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
|
.set_spatial_parent_in_place(Some(parent))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
pub fn set_zoneable_flex(
|
||||||
fn set_spatial_parent(
|
node: &Node,
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
parent: Arc<Node>,
|
message: Message,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
let zoneable: bool = deserialize(message.as_ref())?;
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
let spatial = node.spatial.get().unwrap();
|
||||||
|
|
||||||
this_spatial.set_spatial_parent(Some(&parent))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_spatial_parent_in_place(
|
|
||||||
node: Arc<Node>,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
parent: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let this_spatial = node.get_aspect::<Spatial>()?;
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
|
|
||||||
this_spatial.set_spatial_parent_in_place(Some(&parent))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_zoneable(node: Arc<Node>, _calling_client: Arc<Client>, zoneable: bool) -> Result<()> {
|
|
||||||
let spatial = node.get_aspect::<Spatial>()?;
|
|
||||||
if zoneable {
|
if zoneable {
|
||||||
ZONEABLE_REGISTRY.add_raw(&spatial);
|
ZONEABLE_REGISTRY.add_raw(spatial);
|
||||||
} else {
|
} else {
|
||||||
ZONEABLE_REGISTRY.remove(&spatial);
|
ZONEABLE_REGISTRY.remove(spatial);
|
||||||
zone::release(&spatial);
|
zone::release(spatial);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// legit gotta find a way to remove old ones, this just keeps the node alive
|
pub fn field_distance_flex(
|
||||||
async fn export_spatial(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<u64> {
|
node: &Node,
|
||||||
let id = rand::random();
|
calling_client: Arc<Client>,
|
||||||
EXPORTED_SPATIALS.lock().insert(id, node);
|
message: Message,
|
||||||
Ok(id)
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(message.as_ref())?;
|
||||||
|
let spatial = node.spatial.get().unwrap();
|
||||||
|
|
||||||
|
let output = fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|f| {
|
||||||
|
calling_client
|
||||||
|
.get_node("Field", f?)
|
||||||
|
.ok()?
|
||||||
|
.get_aspect("Field", "field", |n| &n.field)
|
||||||
|
.ok()
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
.map(|f| f.map(|f| f.distance(spatial, point.into())))
|
||||||
|
.collect::<Vec<Option<f32>>>();
|
||||||
|
|
||||||
|
Ok(serialize(output)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pub fn field_normal_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(message.as_ref())?;
|
||||||
|
let spatial = node.spatial.get().unwrap();
|
||||||
|
|
||||||
|
let output = fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|f| {
|
||||||
|
calling_client
|
||||||
|
.get_node("Field", f?)
|
||||||
|
.ok()?
|
||||||
|
.get_aspect("Field", "field", |n| &n.field)
|
||||||
|
.ok()
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
.map(|f| f.map(|f| Vector3::from(f.normal(spatial, point.into(), 0.001))))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(serialize(output)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pub fn field_closest_point_flex(
|
||||||
|
node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
response: MethodResponseSender,
|
||||||
|
) {
|
||||||
|
response.wrap_sync(move || {
|
||||||
|
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(message.as_ref())?;
|
||||||
|
let spatial = node.spatial.get().unwrap();
|
||||||
|
|
||||||
|
let output = fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|f| {
|
||||||
|
calling_client
|
||||||
|
.get_node("Field", f?)
|
||||||
|
.ok()?
|
||||||
|
.get_aspect("Field", "field", |n| &n.field)
|
||||||
|
.ok()
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
.map(|f| f.map(|f| Vector3::from(f.closest_point(spatial, point.into(), 0.001))))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(serialize(output)?.into())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(self) fn zone_distance(&self) -> f32 {
|
||||||
|
self.zone
|
||||||
|
.lock()
|
||||||
|
.upgrade()
|
||||||
|
.and_then(|zone| zone.field.upgrade())
|
||||||
|
.map(|field| field.distance(self, vec3a(0.0, 0.0, 0.0)))
|
||||||
|
.unwrap_or(f32::MAX)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialEq for Spatial {
|
impl PartialEq for Spatial {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.node.as_ptr() == other.node.as_ptr()
|
self.uid == other.uid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Debug for Spatial {
|
impl Debug for Spatial {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("Spatial")
|
f.debug_struct("Spatial")
|
||||||
|
.field("uid", &self.uid)
|
||||||
.field("parent", &self.parent)
|
.field("parent", &self.parent)
|
||||||
.field("old_parent", &self.old_parent)
|
.field("old_parent", &self.old_parent)
|
||||||
.field("transform", &self.transform)
|
.field("transform", &self.transform)
|
||||||
@@ -384,14 +454,14 @@ impl Debug for Spatial {
|
|||||||
}
|
}
|
||||||
impl Drop for Spatial {
|
impl Drop for Spatial {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
zone::release(self);
|
|
||||||
ZONEABLE_REGISTRY.remove(self);
|
ZONEABLE_REGISTRY.remove(self);
|
||||||
|
zone::release(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_transform(transform: Transform, position: bool, rotation: bool, scale: bool) -> Mat4 {
|
pub fn parse_transform(transform: Transform, position: bool, rotation: bool, scale: bool) -> Mat4 {
|
||||||
let position = position
|
let position = position
|
||||||
.then_some(transform.translation)
|
.then_some(transform.position)
|
||||||
.flatten()
|
.flatten()
|
||||||
.unwrap_or_else(|| Vector3::from([0.0; 3]));
|
.unwrap_or_else(|| Vector3::from([0.0; 3]));
|
||||||
let rotation = rotation
|
let rotation = rotation
|
||||||
@@ -406,59 +476,53 @@ pub fn parse_transform(transform: Transform, position: bool, rotation: bool, sca
|
|||||||
Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into())
|
Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SpatialInterface;
|
pub fn find_spatial(
|
||||||
impl InterfaceAspect for SpatialInterface {
|
calling_client: &Arc<Client>,
|
||||||
fn create_spatial(
|
node_name: &'static str,
|
||||||
_node: Arc<Node>,
|
node_path: &str,
|
||||||
calling_client: Arc<Client>,
|
) -> color_eyre::eyre::Result<Arc<Spatial>> {
|
||||||
id: u64,
|
calling_client
|
||||||
parent: Arc<Node>,
|
.get_node(node_name, node_path)?
|
||||||
transform: Transform,
|
.get_aspect(node_name, "spatial", |n| &n.spatial)
|
||||||
zoneable: bool,
|
.cloned()
|
||||||
) -> Result<()> {
|
}
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
pub fn find_spatial_parent(
|
||||||
let transform = parse_transform(transform, true, true, true);
|
calling_client: &Arc<Client>,
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
node_path: &str,
|
||||||
Spatial::add_to(&node, Some(parent.clone()), transform, zoneable);
|
) -> color_eyre::eyre::Result<Arc<Spatial>> {
|
||||||
Ok(())
|
find_spatial(calling_client, "Spatial parent", node_path)
|
||||||
}
|
}
|
||||||
fn create_zone(
|
pub fn find_reference_space(
|
||||||
_node: Arc<Node>,
|
calling_client: &Arc<Client>,
|
||||||
calling_client: Arc<Client>,
|
node_path: &str,
|
||||||
id: u64,
|
) -> color_eyre::eyre::Result<Arc<Spatial>> {
|
||||||
parent: Arc<Node>,
|
find_spatial(calling_client, "Reference space", node_path)
|
||||||
transform: Transform,
|
|
||||||
field: Arc<Node>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let parent = parent.get_aspect::<Spatial>()?;
|
|
||||||
let transform = parse_transform(transform, true, true, false);
|
|
||||||
let field = field.get_aspect::<Field>()?;
|
|
||||||
|
|
||||||
let node = Node::from_id(&calling_client, id, true).add_to_scenegraph()?;
|
|
||||||
let space = Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
|
||||||
Zone::add_to(&node, space, field);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn import_spatial_ref(
|
|
||||||
_node: Arc<Node>,
|
|
||||||
calling_client: Arc<Client>,
|
|
||||||
uid: u64,
|
|
||||||
) -> Result<Arc<Node>> {
|
|
||||||
EXPORTED_SPATIALS
|
|
||||||
.lock()
|
|
||||||
.get(&uid)
|
|
||||||
.map(|s| {
|
|
||||||
Alias::create(
|
|
||||||
s,
|
|
||||||
&calling_client,
|
|
||||||
SPATIAL_REF_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.ok_or_eyre("Couldn't find spatial with that ID")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create_interface!(SpatialInterface);
|
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
|
||||||
|
let node = Node::create(client, "", "spatial", false);
|
||||||
|
node.add_local_signal("create_spatial", create_spatial_flex);
|
||||||
|
node.add_local_signal("create_zone", create_zone_flex);
|
||||||
|
node.add_to_scenegraph().map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_spatial_flex(
|
||||||
|
_node: &Node,
|
||||||
|
calling_client: Arc<Client>,
|
||||||
|
message: Message,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateSpatialInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
zoneable: bool,
|
||||||
|
}
|
||||||
|
let info: CreateSpatialInfo = deserialize(message.as_ref())?;
|
||||||
|
let node = Node::create(&calling_client, "/spatial/spatial", info.name, true);
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, true);
|
||||||
|
let node = node.add_to_scenegraph()?;
|
||||||
|
Spatial::add_to(&node, Some(parent), transform, info.zoneable)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,162 +1,171 @@
|
|||||||
use super::{
|
use super::{find_spatial, Spatial, ZONEABLE_REGISTRY};
|
||||||
Spatial, ZoneAspect, SPATIAL_ASPECT_ALIAS_INFO, SPATIAL_REF_ASPECT_ALIAS_INFO,
|
|
||||||
ZONEABLE_REGISTRY,
|
|
||||||
};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{client::Client, registry::Registry},
|
core::{client::Client, registry::Registry},
|
||||||
nodes::{
|
nodes::{
|
||||||
alias::{get_original, Alias, AliasList},
|
alias::{Alias, AliasInfo},
|
||||||
fields::{Field, FieldTrait},
|
fields::{find_field, Field},
|
||||||
Aspect, Node,
|
spatial::{find_spatial_parent, parse_transform},
|
||||||
|
Message, Node,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::vec3a;
|
use glam::vec3a;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use stardust_xr::{
|
||||||
|
schemas::flex::{deserialize, serialize},
|
||||||
|
values::Transform,
|
||||||
|
};
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
pub fn capture(spatial: &Arc<Spatial>, zone: &Arc<Zone>) {
|
pub fn capture(spatial: &Arc<Spatial>, zone: &Arc<Zone>) {
|
||||||
let old_distance = spatial.zone_distance();
|
let old_distance = spatial.zone_distance();
|
||||||
let new_distance = zone.field.distance(spatial, vec3a(0.0, 0.0, 0.0));
|
let new_distance = zone
|
||||||
|
.field
|
||||||
|
.upgrade()
|
||||||
|
.map(|field| field.distance(spatial, vec3a(0.0, 0.0, 0.0)))
|
||||||
|
.unwrap_or(f32::MAX);
|
||||||
if new_distance.abs() < old_distance.abs() {
|
if new_distance.abs() < old_distance.abs() {
|
||||||
release(spatial);
|
release(spatial);
|
||||||
*spatial.old_parent.lock() = spatial.get_parent();
|
*spatial.old_parent.lock() = spatial.get_parent();
|
||||||
*spatial.zone.lock() = Arc::downgrade(zone);
|
*spatial.zone.lock() = Arc::downgrade(zone);
|
||||||
let Some(zone_node) = zone.spatial.node.upgrade() else {
|
zone.captured.add_raw(spatial);
|
||||||
return;
|
let Some(node) = zone.spatial.node.upgrade() else {return};
|
||||||
};
|
let Ok(message) = serialize(&spatial.uid) else {return};
|
||||||
let Some(spatial_node) = spatial.node.upgrade() else {
|
let _ = node.send_remote_signal("capture", message);
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Ok(spatial_alias) = Alias::create(
|
|
||||||
&spatial_node,
|
|
||||||
&zone_node.get_client().unwrap(),
|
|
||||||
SPATIAL_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
Some(&zone.captured),
|
|
||||||
) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let _ = super::zone_client::capture(&zone_node, &spatial_alias);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn release(spatial: &Spatial) {
|
pub fn release(spatial: &Spatial) {
|
||||||
let Some(spatial_node) = spatial.node.upgrade() else {
|
let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take());
|
||||||
return;
|
|
||||||
};
|
|
||||||
let spatial = spatial_node.get_aspect::<Spatial>().unwrap();
|
|
||||||
|
|
||||||
let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take().as_ref());
|
|
||||||
let mut spatial_zone = spatial.zone.lock();
|
let mut spatial_zone = spatial.zone.lock();
|
||||||
|
|
||||||
if let Some(spatial_zone) = spatial_zone.upgrade() {
|
if let Some(spatial_zone) = spatial_zone.upgrade() {
|
||||||
spatial_zone.captured.remove_aspect(spatial.as_ref());
|
let Some(node) = spatial_zone.spatial.node.upgrade() else {return};
|
||||||
let Some(node) = spatial_zone.spatial.node.upgrade() else {
|
spatial_zone.captured.remove(spatial);
|
||||||
return;
|
let Ok(message) = serialize(&spatial.uid) else {return};
|
||||||
};
|
let _ = node.send_remote_signal("release", message);
|
||||||
let _ = super::zone_client::release(&node, spatial_node.id);
|
|
||||||
}
|
}
|
||||||
*spatial_zone = Weak::new();
|
*spatial_zone = Weak::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Zone {
|
pub struct Zone {
|
||||||
spatial: Arc<Spatial>,
|
spatial: Arc<Spatial>,
|
||||||
pub field: Arc<Field>,
|
pub field: Weak<Field>,
|
||||||
intersecting_spatials: Registry<Spatial>,
|
zoneables: Mutex<FxHashMap<String, Arc<Node>>>,
|
||||||
intersecting: AliasList,
|
captured: Registry<Spatial>,
|
||||||
captured: AliasList,
|
|
||||||
}
|
}
|
||||||
impl Zone {
|
impl Zone {
|
||||||
pub fn add_to(node: &Arc<Node>, spatial: Arc<Spatial>, field: Arc<Field>) -> Arc<Zone> {
|
pub fn add_to(node: &Arc<Node>, spatial: Arc<Spatial>, field: &Arc<Field>) -> Arc<Zone> {
|
||||||
let zone = Arc::new(Zone {
|
let zone = Arc::new(Zone {
|
||||||
spatial,
|
spatial,
|
||||||
field,
|
field: Arc::downgrade(field),
|
||||||
intersecting_spatials: Registry::default(),
|
zoneables: Mutex::new(FxHashMap::default()),
|
||||||
intersecting: AliasList::default(),
|
captured: Registry::new(),
|
||||||
captured: AliasList::default(),
|
|
||||||
});
|
});
|
||||||
<Zone as ZoneAspect>::add_node_members(node);
|
node.add_local_signal("capture", Zone::capture_flex);
|
||||||
node.add_aspect_raw(zone.clone());
|
node.add_local_signal("release", Zone::release_flex);
|
||||||
|
node.add_local_signal("update", Zone::update);
|
||||||
|
let _ = node.zone.set(zone.clone());
|
||||||
zone
|
zone
|
||||||
}
|
}
|
||||||
pub fn update(&self) -> Result<()> {
|
fn capture_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
let node = self.spatial.node().unwrap();
|
let zone = node.zone.get().unwrap();
|
||||||
|
let capture_path: &str = deserialize(message.as_ref())?;
|
||||||
let current_zoneables = Registry::new();
|
let spatial = find_spatial(&calling_client, "Spatial", capture_path)?;
|
||||||
for zoneable in ZONEABLE_REGISTRY.get_valid_contents() {
|
capture(&spatial, zone);
|
||||||
let distance = self.field.distance(&zoneable, [0.0; 3].into());
|
|
||||||
if distance > 0.0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Some(zone) = zoneable.zone.lock().upgrade() {
|
|
||||||
let zoneable_distance = zone.field.distance(&zoneable, [0.0; 3].into());
|
|
||||||
if zoneable_distance < distance {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
current_zoneables.add_raw(&zoneable);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (added, removed) =
|
|
||||||
Registry::get_changes(&self.intersecting_spatials, ¤t_zoneables);
|
|
||||||
for added in added {
|
|
||||||
let Some(added_node) = added.node() else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let Ok(alias) = Alias::create(
|
|
||||||
&added_node,
|
|
||||||
&self.spatial.node().unwrap().get_client().unwrap(),
|
|
||||||
SPATIAL_REF_ASPECT_ALIAS_INFO.clone(),
|
|
||||||
Some(&self.intersecting),
|
|
||||||
) else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let _ = super::zone_client::enter(&node, &alias);
|
|
||||||
}
|
|
||||||
for removed in removed {
|
|
||||||
let Some(removed_node) = removed.node() else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
release(&removed);
|
|
||||||
let _ = super::zone_client::leave(&node, removed_node.id);
|
|
||||||
self.intersecting.remove_aspect(removed.as_ref());
|
|
||||||
}
|
|
||||||
self.intersecting_spatials.set(¤t_zoneables);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
fn release_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
impl Aspect for Zone {
|
let capture_path: &str = deserialize(message.as_ref())?;
|
||||||
const NAME: &'static str = "Zone";
|
let spatial = find_spatial(&calling_client, "Spatial", capture_path)?;
|
||||||
}
|
|
||||||
impl ZoneAspect for Zone {
|
|
||||||
fn update(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<()> {
|
|
||||||
let zone = node.get_aspect::<Zone>()?;
|
|
||||||
let _ = zone.update();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn capture(node: Arc<Node>, _calling_client: Arc<Client>, spatial: Arc<Node>) -> Result<()> {
|
|
||||||
let zone = node.get_aspect::<Zone>()?;
|
|
||||||
let spatial = spatial.get_aspect()?;
|
|
||||||
capture(&spatial, &zone);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn release(_node: Arc<Node>, _calling_client: Arc<Client>, spatial: Arc<Node>) -> Result<()> {
|
|
||||||
let spatial = spatial.get_aspect()?;
|
|
||||||
release(&spatial);
|
release(&spatial);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
fn update(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
|
||||||
|
let zone = node.zone.get().unwrap();
|
||||||
|
let Some(field) = zone.field.upgrade() else { return Err(color_eyre::eyre::eyre!("Zone's field has been destroyed")) };
|
||||||
|
let Some((zone_client, zone_node)) = zone
|
||||||
|
.spatial
|
||||||
|
.node
|
||||||
|
.upgrade()
|
||||||
|
.and_then(|n| n.get_client().zip(Some(n))) else { return Err(color_eyre::eyre::eyre!("No client on node?")) };
|
||||||
|
let mut old_zoneables = zone.zoneables.lock();
|
||||||
|
for (_uid, zoneable) in old_zoneables.iter() {
|
||||||
|
zoneable.destroy();
|
||||||
|
}
|
||||||
|
let captured = zone.captured.get_valid_contents();
|
||||||
|
let zoneables = ZONEABLE_REGISTRY
|
||||||
|
.get_valid_contents()
|
||||||
|
.into_iter()
|
||||||
|
.filter(|zoneable| zoneable.node.upgrade().is_some())
|
||||||
|
.filter(|zoneable| {
|
||||||
|
if captured
|
||||||
|
.iter()
|
||||||
|
.any(|captured| Arc::ptr_eq(captured, zoneable))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
let spatial_zone_distance = zoneable.zone_distance();
|
||||||
|
let self_zone_distance = field.distance(zoneable, vec3a(0.0, 0.0, 0.0));
|
||||||
|
self_zone_distance < 0.0 && spatial_zone_distance > self_zone_distance
|
||||||
|
})
|
||||||
|
.filter_map(|zoneable| {
|
||||||
|
let alias = Alias::create(
|
||||||
|
&zone_client,
|
||||||
|
zone_node.get_path(),
|
||||||
|
&zoneable.uid,
|
||||||
|
&zoneable.node.upgrade().unwrap(),
|
||||||
|
AliasInfo {
|
||||||
|
server_signals: vec![
|
||||||
|
"set_transform",
|
||||||
|
"set_spatial_parent",
|
||||||
|
"set_spatial_parent_in_place",
|
||||||
|
],
|
||||||
|
server_methods: vec!["get_bounds", "get_transform"],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.ok()?;
|
||||||
|
Some((zoneable.uid.clone(), alias))
|
||||||
|
})
|
||||||
|
.collect::<FxHashMap<String, Arc<Node>>>();
|
||||||
|
|
||||||
|
for entered_uid in zoneables.keys().filter(|k| !old_zoneables.contains_key(*k)) {
|
||||||
|
node.send_remote_signal("enter", serialize(entered_uid)?)?;
|
||||||
|
}
|
||||||
|
for left_uid in old_zoneables.keys().filter(|k| !zoneables.contains_key(*k)) {
|
||||||
|
node.send_remote_signal("leave", serialize(left_uid)?)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
*old_zoneables = zoneables;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl Drop for Zone {
|
impl Drop for Zone {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
for captured in self
|
for captured in self.captured.get_valid_contents() {
|
||||||
.captured
|
|
||||||
.get_aliases()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|n| get_original(n, false))
|
|
||||||
.filter_map(|n| n.get_aspect::<Spatial>().ok())
|
|
||||||
{
|
|
||||||
release(&captured);
|
release(&captured);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_zone_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateZoneInfo<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
parent_path: &'a str,
|
||||||
|
transform: Transform,
|
||||||
|
field_path: &'a str,
|
||||||
|
}
|
||||||
|
let info: CreateZoneInfo = deserialize(message.as_ref())?;
|
||||||
|
let parent = find_spatial_parent(&calling_client, info.parent_path)?;
|
||||||
|
let transform = parse_transform(info.transform, true, true, false);
|
||||||
|
let field = find_field(&calling_client, info.field_path)?;
|
||||||
|
|
||||||
|
let node =
|
||||||
|
Node::create(&calling_client, "/spatial/zone", info.name, true).add_to_scenegraph()?;
|
||||||
|
let space = Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||||
|
Zone::add_to(&node, space, &field);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,23 +1,18 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::client::INTERNAL_CLIENT,
|
core::client::INTERNAL_CLIENT,
|
||||||
nodes::{
|
nodes::{
|
||||||
fields::{FieldTrait, Ray},
|
input::{pointer::Pointer, InputMethod, InputType},
|
||||||
input::{InputDataType, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
|
|
||||||
spatial::Spatial,
|
spatial::Spatial,
|
||||||
Node, OwnedNode,
|
Node,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::{vec3, Mat4};
|
use glam::Mat4;
|
||||||
use serde::{Deserialize, Serialize};
|
use nanoid::nanoid;
|
||||||
use stardust_xr::values::Datamap;
|
use serde::Serialize;
|
||||||
|
use stardust_xr::schemas::{flat::Datamap, flex::flexbuffers};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use stereokit_rust::system::Input;
|
use stereokit::StereoKitMultiThread;
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
|
||||||
pub struct EyeDatamap {
|
|
||||||
eye: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct KeyboardEvent {
|
pub struct KeyboardEvent {
|
||||||
@@ -28,79 +23,32 @@ pub struct KeyboardEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct EyePointer {
|
pub struct EyePointer {
|
||||||
node: OwnedNode,
|
|
||||||
spatial: Arc<Spatial>,
|
spatial: Arc<Spatial>,
|
||||||
pointer: Arc<InputMethod>,
|
pointer: Arc<InputMethod>,
|
||||||
}
|
}
|
||||||
impl EyePointer {
|
impl EyePointer {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
|
let node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
|
||||||
let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
|
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
|
||||||
let pointer = InputMethod::add_to(
|
let pointer =
|
||||||
&node.0,
|
InputMethod::add_to(&node, InputType::Pointer(Pointer::default()), None).unwrap();
|
||||||
InputDataType::Pointer(Pointer::default()),
|
|
||||||
Datamap::from_typed(EyeDatamap::default())?,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(EyePointer {
|
Ok(EyePointer { spatial, pointer })
|
||||||
node,
|
|
||||||
spatial,
|
|
||||||
pointer,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn update(&self) {
|
pub fn update(&self, sk: &impl StereoKitMultiThread) {
|
||||||
let ray = Input::get_eyes();
|
let ray = sk.input_eyes();
|
||||||
self.spatial
|
self.spatial
|
||||||
.set_local_transform(Mat4::from_rotation_translation(
|
.set_local_transform(Mat4::from_rotation_translation(
|
||||||
ray.orientation.into(),
|
ray.orientation,
|
||||||
ray.position.into(),
|
ray.position,
|
||||||
));
|
));
|
||||||
{
|
{
|
||||||
// Set pointer input datamap
|
// Set pointer input datamap
|
||||||
*self.pointer.datamap.lock() = Datamap::from_typed(EyeDatamap { eye: 2 }).unwrap();
|
let mut fbb = flexbuffers::Builder::default();
|
||||||
|
let mut map = fbb.start_map();
|
||||||
|
map.push("eye", 2);
|
||||||
|
map.end_map();
|
||||||
|
*self.pointer.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
// send input to all the input handlers that are the closest to the ray as possible
|
|
||||||
let rx = INPUT_HANDLER_REGISTRY
|
|
||||||
.get_valid_contents()
|
|
||||||
.into_iter()
|
|
||||||
// filter out all the disabled handlers
|
|
||||||
.filter(|handler| {
|
|
||||||
let Some(node) = handler.spatial.node() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
node.enabled()
|
|
||||||
})
|
|
||||||
// ray march to all the enabled handlers' fields
|
|
||||||
.map(|handler| {
|
|
||||||
let result = handler.field.ray_march(Ray {
|
|
||||||
origin: vec3(0.0, 0.0, 0.0),
|
|
||||||
direction: vec3(0.0, 0.0, -1.0),
|
|
||||||
space: self.spatial.clone(),
|
|
||||||
});
|
|
||||||
(vec![handler], result)
|
|
||||||
})
|
|
||||||
// make sure the field isn't at the pointer origin and that it's being hit
|
|
||||||
.filter(|(_, result)| result.deepest_point_distance > 0.01 && result.min_distance < 0.0)
|
|
||||||
// .inspect(|(_, result)| {
|
|
||||||
// dbg!(result);
|
|
||||||
// })
|
|
||||||
// now collect all handlers that are same distance if they're the closest
|
|
||||||
.reduce(|(mut handlers_a, result_a), (handlers_b, result_b)| {
|
|
||||||
if (result_a.deepest_point_distance - result_b.deepest_point_distance).abs() < 0.001
|
|
||||||
{
|
|
||||||
// distance is basically the same
|
|
||||||
handlers_a.extend(handlers_b);
|
|
||||||
(handlers_a, result_a)
|
|
||||||
} else if result_a.deepest_point_distance < result_b.deepest_point_distance {
|
|
||||||
(handlers_a, result_a)
|
|
||||||
} else {
|
|
||||||
(handlers_b, result_b)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|(rx, _)| rx)
|
|
||||||
.unwrap_or_default();
|
|
||||||
self.pointer.set_handler_order(rx.iter());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,97 +2,3 @@ pub mod eye_pointer;
|
|||||||
pub mod mouse_pointer;
|
pub mod mouse_pointer;
|
||||||
pub mod sk_controller;
|
pub mod sk_controller;
|
||||||
pub mod sk_hand;
|
pub mod sk_hand;
|
||||||
|
|
||||||
use crate::nodes::{
|
|
||||||
fields::{Field, FieldTrait, Ray},
|
|
||||||
input::{InputDataTrait, InputDataType, InputHandler, InputMethod, INPUT_HANDLER_REGISTRY},
|
|
||||||
spatial::Spatial,
|
|
||||||
};
|
|
||||||
use glam::vec3;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct CaptureManager {
|
|
||||||
pub capture: Option<Arc<InputHandler>>,
|
|
||||||
}
|
|
||||||
impl CaptureManager {
|
|
||||||
pub fn update_capture(&mut self, pointer: &InputMethod) {
|
|
||||||
if let Some(capture) = &self.capture {
|
|
||||||
if !pointer
|
|
||||||
.internal_capture_requests
|
|
||||||
.get_valid_contents()
|
|
||||||
.contains(capture)
|
|
||||||
{
|
|
||||||
self.capture.take();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_new_capture(
|
|
||||||
&mut self,
|
|
||||||
pointer: &InputMethod,
|
|
||||||
distance_calculator: DistanceCalculator,
|
|
||||||
) {
|
|
||||||
if self.capture.is_none() {
|
|
||||||
self.capture = find_closest_capture(pointer, distance_calculator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn apply_capture(&self, method: &InputMethod) {
|
|
||||||
method.captures.clear();
|
|
||||||
if let Some(capture) = &self.capture {
|
|
||||||
method.set_handler_order([capture].into_iter());
|
|
||||||
method.captures.add_raw(capture);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type DistanceCalculator = fn(&Arc<Spatial>, &InputDataType, &Field) -> Option<f32>;
|
|
||||||
|
|
||||||
pub fn find_closest_capture(
|
|
||||||
method: &InputMethod,
|
|
||||||
distance_calculator: DistanceCalculator,
|
|
||||||
) -> Option<Arc<InputHandler>> {
|
|
||||||
method
|
|
||||||
.internal_capture_requests
|
|
||||||
.get_valid_contents()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|h| {
|
|
||||||
distance_calculator(&method.spatial, &method.data.lock(), &h.field)
|
|
||||||
.map(|dist| (h.clone(), dist))
|
|
||||||
})
|
|
||||||
.min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap())
|
|
||||||
.map(|(handler, _)| handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_sorted_handlers(
|
|
||||||
method: &InputMethod,
|
|
||||||
distance_calculator: DistanceCalculator,
|
|
||||||
) -> Vec<Arc<InputHandler>> {
|
|
||||||
INPUT_HANDLER_REGISTRY
|
|
||||||
.get_valid_contents()
|
|
||||||
.into_iter()
|
|
||||||
.filter(|handler| handler.spatial.node().map_or(false, |node| node.enabled()))
|
|
||||||
.filter(|handler| {
|
|
||||||
handler
|
|
||||||
.field
|
|
||||||
.spatial
|
|
||||||
.node()
|
|
||||||
.map_or(false, |node| node.enabled())
|
|
||||||
})
|
|
||||||
.filter_map(|handler| {
|
|
||||||
distance_calculator(&method.spatial, &method.data.lock(), &handler.field)
|
|
||||||
.map(|distance| (vec![handler], distance))
|
|
||||||
})
|
|
||||||
.filter(|(_, distance)| *distance > 0.0)
|
|
||||||
.reduce(|(mut handlers_a, distance_a), (handlers_b, distance_b)| {
|
|
||||||
if (distance_a - distance_b).abs() < 0.001 {
|
|
||||||
handlers_a.extend(handlers_b);
|
|
||||||
(handlers_a, distance_a)
|
|
||||||
} else if distance_a < distance_b {
|
|
||||||
(handlers_a, distance_a)
|
|
||||||
} else {
|
|
||||||
(handlers_b, distance_b)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|(handlers, _)| handlers)
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,164 +1,115 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::client::INTERNAL_CLIENT,
|
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
|
||||||
nodes::{
|
nodes::{
|
||||||
data::{
|
data::{mask_matches, Mask, PulseSender, KEYMAPS, PULSE_RECEIVER_REGISTRY},
|
||||||
mask_matches, pulse_receiver_client, PulseSender, KEYMAPS, PULSE_RECEIVER_REGISTRY,
|
fields::Ray,
|
||||||
},
|
input::{pointer::Pointer, InputMethod, InputType},
|
||||||
fields::{FieldTrait, Ray},
|
|
||||||
input::{InputDataType, InputHandler, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
|
|
||||||
spatial::Spatial,
|
spatial::Spatial,
|
||||||
Node, OwnedNode,
|
Node,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::{vec3, Mat4, Vec3};
|
use glam::{vec2, vec3, Mat4, Vec2, Vec3};
|
||||||
use mint::Vector2;
|
use nanoid::nanoid;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use slotmap::{DefaultKey, Key as SlotKey};
|
use std::{convert::TryFrom, sync::Arc};
|
||||||
use stardust_xr::values::Datamap;
|
use stereokit::{ray_from_mouse, ButtonState, Key, StereoKitMultiThread};
|
||||||
use std::sync::Arc;
|
use xkbcommon::xkb::{Context, Keymap, FORMAT_TEXT_V1};
|
||||||
use stereokit_rust::system::{Input, Key};
|
|
||||||
use xkbcommon_rs::{xkb_keymap::CompileFlags, Context, Keymap, KeymapFormat};
|
|
||||||
|
|
||||||
use super::{get_sorted_handlers, CaptureManager, DistanceCalculator};
|
#[derive(Default, Deserialize, Serialize)]
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
struct MouseEvent {
|
struct MouseEvent {
|
||||||
select: f32,
|
select: f32,
|
||||||
middle: f32,
|
|
||||||
context: f32,
|
|
||||||
grab: f32,
|
grab: f32,
|
||||||
scroll_continuous: Vector2<f32>,
|
scroll_continuous: Vec2,
|
||||||
scroll_discrete: Vector2<f32>,
|
scroll_discrete: Vec2,
|
||||||
raw_input_events: Vec<u32>,
|
|
||||||
}
|
}
|
||||||
impl Default for MouseEvent {
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct KeyboardEvent {
|
||||||
|
pub keyboard: (),
|
||||||
|
pub xkbv1: (),
|
||||||
|
pub keymap_id: String,
|
||||||
|
pub keys: Vec<i32>,
|
||||||
|
}
|
||||||
|
impl Default for KeyboardEvent {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
MouseEvent {
|
Self {
|
||||||
select: 0.0,
|
keyboard: (),
|
||||||
middle: 0.0,
|
xkbv1: (),
|
||||||
context: 0.0,
|
keymap_id: "flatscreen".to_string(),
|
||||||
grab: 0.0,
|
keys: Default::default(),
|
||||||
scroll_continuous: [0.0; 2].into(),
|
|
||||||
scroll_discrete: [0.0; 2].into(),
|
|
||||||
raw_input_events: vec![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
|
||||||
pub struct KeyboardEvent {
|
|
||||||
pub keyboard: (),
|
|
||||||
pub xkbv1: (),
|
|
||||||
pub keymap_id: u64,
|
|
||||||
pub keys: Vec<i32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
pub struct MousePointer {
|
pub struct MousePointer {
|
||||||
node: OwnedNode,
|
node: Arc<Node>,
|
||||||
keymap: DefaultKey,
|
|
||||||
spatial: Arc<Spatial>,
|
spatial: Arc<Spatial>,
|
||||||
pointer: Arc<InputMethod>,
|
pointer: Arc<InputMethod>,
|
||||||
capture_manager: CaptureManager,
|
mouse_datamap: TypedDatamap<MouseEvent>,
|
||||||
mouse_datamap: MouseEvent,
|
keyboard_datamap: TypedDatamap<KeyboardEvent>,
|
||||||
keyboard_datamap: KeyboardEvent,
|
|
||||||
keyboard_sender: Arc<PulseSender>,
|
keyboard_sender: Arc<PulseSender>,
|
||||||
}
|
}
|
||||||
impl MousePointer {
|
impl MousePointer {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
|
let node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
|
||||||
let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
|
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
|
||||||
let pointer = InputMethod::add_to(
|
let pointer =
|
||||||
&node.0,
|
InputMethod::add_to(&node, InputType::Pointer(Pointer::default()), None).unwrap();
|
||||||
InputDataType::Pointer(Pointer::default()),
|
|
||||||
Datamap::from_typed(MouseEvent::default())?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let context = Context::new(0).unwrap();
|
KEYMAPS.lock().insert(
|
||||||
let keymap = KEYMAPS.lock().insert(
|
"flatscreen".to_string(),
|
||||||
Keymap::new_from_names(context, None, CompileFlags::NO_FLAGS)
|
Keymap::new_from_names(&Context::new(0), "evdev", "", "", "", None, 0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get_as_string(KeymapFormat::TextV1)
|
.get_as_string(FORMAT_TEXT_V1),
|
||||||
.unwrap(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let keyboard_sender = PulseSender::add_to(
|
let keyboard_sender =
|
||||||
&node.0,
|
PulseSender::add_to(&node, Mask::from_struct::<KeyboardEvent>()).unwrap();
|
||||||
Datamap::from_typed(KeyboardEvent::default()).unwrap(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(MousePointer {
|
Ok(MousePointer {
|
||||||
node,
|
node,
|
||||||
spatial,
|
spatial,
|
||||||
pointer,
|
pointer,
|
||||||
capture_manager: CaptureManager::default(),
|
|
||||||
mouse_datamap: Default::default(),
|
mouse_datamap: Default::default(),
|
||||||
keyboard_datamap: KeyboardEvent {
|
keyboard_datamap: Default::default(),
|
||||||
keyboard: (),
|
|
||||||
xkbv1: (),
|
|
||||||
keymap_id: keymap.data().as_ffi(),
|
|
||||||
keys: vec![],
|
|
||||||
},
|
|
||||||
keymap,
|
|
||||||
keyboard_sender,
|
keyboard_sender,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self, sk: &impl StereoKitMultiThread) {
|
||||||
let mouse = Input::get_mouse();
|
let mouse = sk.input_mouse();
|
||||||
|
|
||||||
let ray = mouse.get_ray();
|
let ray = ray_from_mouse(mouse.pos).unwrap();
|
||||||
self.spatial.set_local_transform(
|
self.spatial.set_local_transform(
|
||||||
Mat4::look_to_rh(
|
Mat4::look_to_rh(
|
||||||
Vec3::from(ray.position),
|
Vec3::from(ray.pos),
|
||||||
Vec3::from(ray.direction),
|
Vec3::from(ray.dir),
|
||||||
vec3(0.0, 1.0, 0.0),
|
vec3(0.0, 1.0, 0.0),
|
||||||
)
|
)
|
||||||
.inverse(),
|
.inverse(),
|
||||||
);
|
);
|
||||||
{
|
{
|
||||||
// Set pointer input datamap
|
// Set pointer input datamap
|
||||||
self.mouse_datamap = MouseEvent {
|
self.mouse_datamap.select =
|
||||||
select: Input::key(Key::MouseLeft).is_active() as u32 as f32,
|
if sk.input_key(Key::MouseLeft).contains(ButtonState::ACTIVE) {
|
||||||
middle: Input::key(Key::MouseCenter).is_active() as u32 as f32,
|
1.0f32
|
||||||
context: Input::key(Key::MouseRight).is_active() as u32 as f32,
|
} else {
|
||||||
grab: (Input::key(Key::Backtick).is_active() && Input::key(Key::Shift).is_active()) as u32 as f32, // Was Mouse 5
|
0.0f32
|
||||||
scroll_continuous: [0.0, mouse.scroll_change / 120.0].into(),
|
};
|
||||||
scroll_discrete: [0.0, mouse.scroll_change / 120.0].into(),
|
self.mouse_datamap.grab = if sk.input_key(Key::MouseRight).contains(ButtonState::ACTIVE)
|
||||||
raw_input_events: vec![],
|
{
|
||||||
|
1.0f32
|
||||||
|
} else {
|
||||||
|
0.0f32
|
||||||
};
|
};
|
||||||
*self.pointer.datamap.lock() = Datamap::from_typed(&self.mouse_datamap).unwrap();
|
self.mouse_datamap.scroll_continuous = vec2(0.0, mouse.scroll_change / 120.0);
|
||||||
|
self.mouse_datamap.scroll_discrete = vec2(0.0, mouse.scroll_change / 120.0);
|
||||||
|
*self.pointer.datamap.lock() = self.mouse_datamap.to_datamap().ok();
|
||||||
}
|
}
|
||||||
self.target_pointer_input();
|
self.send_keyboard_input(sk);
|
||||||
self.send_keyboard_input();
|
|
||||||
}
|
|
||||||
fn target_pointer_input(&mut self) {
|
|
||||||
let distance_calculator: DistanceCalculator = |space, data, field| {
|
|
||||||
let result = field.ray_march(Ray {
|
|
||||||
origin: vec3(0.0, 0.0, 0.0),
|
|
||||||
direction: vec3(0.0, 0.0, -1.0),
|
|
||||||
space: space.clone(),
|
|
||||||
});
|
|
||||||
let valid =
|
|
||||||
result.deepest_point_distance > 0.0 && result.min_distance.is_sign_negative();
|
|
||||||
valid.then_some(result.deepest_point_distance)
|
|
||||||
};
|
|
||||||
|
|
||||||
self.capture_manager.update_capture(&self.pointer);
|
|
||||||
self.capture_manager
|
|
||||||
.set_new_capture(&self.pointer, distance_calculator);
|
|
||||||
self.capture_manager.apply_capture(&self.pointer);
|
|
||||||
|
|
||||||
if self.capture_manager.capture.is_some() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let sorted_handlers = get_sorted_handlers(&self.pointer, distance_calculator);
|
|
||||||
self.pointer.set_handler_order(sorted_handlers.iter());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_keyboard_input(&mut self) {
|
fn send_keyboard_input(&mut self, sk: &impl StereoKitMultiThread) {
|
||||||
let rx = PULSE_RECEIVER_REGISTRY
|
let rx = PULSE_RECEIVER_REGISTRY
|
||||||
.get_valid_contents()
|
.get_valid_contents()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -185,12 +136,12 @@ impl MousePointer {
|
|||||||
|
|
||||||
if let Some(rx) = rx {
|
if let Some(rx) = rx {
|
||||||
let keys = (8_u32..254)
|
let keys = (8_u32..254)
|
||||||
.map(|i| unsafe { std::mem::transmute(i) })
|
.filter_map(|i| Key::try_from(i).ok())
|
||||||
.filter_map(|k| Some((map_key(k)?, Input::key(k))))
|
.filter_map(|k| Some((map_key(k)?, sk.input_key(k))))
|
||||||
.filter_map(|(i, k)| {
|
.filter_map(|(i, k)| {
|
||||||
if k.is_just_active() {
|
if k.contains(ButtonState::JUST_ACTIVE) {
|
||||||
Some(i as i32)
|
Some(i as i32)
|
||||||
} else if k.is_just_inactive() {
|
} else if k.contains(ButtonState::JUST_INACTIVE) {
|
||||||
Some(-(i as i32))
|
Some(-(i as i32))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -200,12 +151,8 @@ impl MousePointer {
|
|||||||
|
|
||||||
self.keyboard_datamap.keys = keys;
|
self.keyboard_datamap.keys = keys;
|
||||||
if !self.keyboard_datamap.keys.is_empty() {
|
if !self.keyboard_datamap.keys.is_empty() {
|
||||||
pulse_receiver_client::data(
|
rx.send_data(&self.node.uid, self.keyboard_datamap.serialize().unwrap())
|
||||||
&rx.node.upgrade().unwrap(),
|
.unwrap();
|
||||||
&self.node.0,
|
|
||||||
&Datamap::from_typed(&self.keyboard_datamap).unwrap(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,9 +231,9 @@ fn map_key(key: Key) -> Option<u32> {
|
|||||||
Key::F3 => Some(input_event_codes::KEY_F3!()),
|
Key::F3 => Some(input_event_codes::KEY_F3!()),
|
||||||
Key::F4 => Some(input_event_codes::KEY_F4!()),
|
Key::F4 => Some(input_event_codes::KEY_F4!()),
|
||||||
Key::F5 => Some(input_event_codes::KEY_F5!()),
|
Key::F5 => Some(input_event_codes::KEY_F5!()),
|
||||||
// Key::F6 => Some(input_event_codes::KEY_F6!()),
|
Key::F6 => Some(input_event_codes::KEY_F6!()),
|
||||||
// Key::F7 => Some(input_event_codes::KEY_F7!()),
|
Key::F7 => Some(input_event_codes::KEY_F7!()),
|
||||||
// Key::F8 => Some(input_event_codes::KEY_F8!()),
|
Key::F8 => Some(input_event_codes::KEY_F8!()),
|
||||||
Key::F9 => Some(input_event_codes::KEY_F9!()),
|
Key::F9 => Some(input_event_codes::KEY_F9!()),
|
||||||
Key::F10 => Some(input_event_codes::KEY_F10!()),
|
Key::F10 => Some(input_event_codes::KEY_F10!()),
|
||||||
Key::F11 => Some(input_event_codes::KEY_F11!()),
|
Key::F11 => Some(input_event_codes::KEY_F11!()),
|
||||||
@@ -301,7 +248,7 @@ fn map_key(key: Key) -> Option<u32> {
|
|||||||
Key::BracketClose => Some(input_event_codes::KEY_RIGHTBRACE!()),
|
Key::BracketClose => Some(input_event_codes::KEY_RIGHTBRACE!()),
|
||||||
Key::Minus => Some(input_event_codes::KEY_MINUS!()),
|
Key::Minus => Some(input_event_codes::KEY_MINUS!()),
|
||||||
Key::Equals => Some(input_event_codes::KEY_EQUAL!()),
|
Key::Equals => Some(input_event_codes::KEY_EQUAL!()),
|
||||||
Key::Backtick => Some(input_event_codes::KEY_GRAVE!()),
|
Key::Backtick => None,
|
||||||
Key::LCmd => Some(input_event_codes::KEY_LEFTMETA!()),
|
Key::LCmd => Some(input_event_codes::KEY_LEFTMETA!()),
|
||||||
Key::RCmd => Some(input_event_codes::KEY_RIGHTMETA!()),
|
Key::RCmd => Some(input_event_codes::KEY_RIGHTMETA!()),
|
||||||
Key::Multiply => Some(input_event_codes::KEY_NUMERIC_STAR!()),
|
Key::Multiply => Some(input_event_codes::KEY_NUMERIC_STAR!()),
|
||||||
|
|||||||
@@ -1,128 +1,78 @@
|
|||||||
use super::{get_sorted_handlers, CaptureManager};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::client::INTERNAL_CLIENT,
|
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
|
||||||
nodes::{
|
nodes::{
|
||||||
fields::{Field, FieldTrait},
|
input::{tip::Tip, InputMethod, InputType},
|
||||||
input::{InputDataType, InputHandler, InputMethod, Tip, INPUT_HANDLER_REGISTRY},
|
|
||||||
spatial::Spatial,
|
spatial::Spatial,
|
||||||
Node, OwnedNode,
|
Node,
|
||||||
},
|
},
|
||||||
objects::{ObjectHandle, SpatialRef},
|
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::{Mat4, Vec2, Vec3};
|
use glam::{Mat4, Vec2, Vec3};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use stardust_xr::values::Datamap;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use stereokit_rust::{
|
use stereokit::{
|
||||||
material::Material,
|
named_colors::WHITE, ButtonState, Handed, Model, RenderLayer, StereoKitDraw,
|
||||||
model::Model,
|
StereoKitMultiThread,
|
||||||
sk::MainThreadToken,
|
|
||||||
system::{Handed, Input},
|
|
||||||
util::Color128,
|
|
||||||
};
|
};
|
||||||
use zbus::Connection;
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize)]
|
||||||
struct ControllerDatamap {
|
struct ControllerDatamap {
|
||||||
select: f32,
|
select: f32,
|
||||||
middle: f32,
|
|
||||||
context: f32,
|
|
||||||
grab: f32,
|
grab: f32,
|
||||||
scroll: Vec2,
|
scroll: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SkController {
|
pub struct SkController {
|
||||||
object_handle: ObjectHandle<SpatialRef>,
|
_node: Arc<Node>,
|
||||||
input: Arc<InputMethod>,
|
input: Arc<InputMethod>,
|
||||||
handed: Handed,
|
|
||||||
model: Model,
|
model: Model,
|
||||||
material: Material,
|
handed: Handed,
|
||||||
capture_manager: CaptureManager,
|
datamap: TypedDatamap<ControllerDatamap>,
|
||||||
datamap: ControllerDatamap,
|
|
||||||
}
|
}
|
||||||
impl SkController {
|
impl SkController {
|
||||||
pub fn new(connection: &Connection, handed: Handed) -> Result<Self> {
|
pub fn new(sk: &impl StereoKitMultiThread, handed: Handed) -> Result<Self> {
|
||||||
let (spatial, object_handle) = SpatialRef::create(
|
let _node = Node::create(
|
||||||
connection,
|
&INTERNAL_CLIENT,
|
||||||
&("/org/stardustxr/Controller/".to_string()
|
"",
|
||||||
+ match handed {
|
if handed == Handed::Left {
|
||||||
Handed::Left => "left",
|
"controller_left"
|
||||||
_ => "right",
|
} else {
|
||||||
}),
|
"controller_right"
|
||||||
);
|
},
|
||||||
let model = Model::copy(Model::from_memory(
|
false,
|
||||||
"cursor.glb",
|
)
|
||||||
include_bytes!("cursor.glb"),
|
.add_to_scenegraph()?;
|
||||||
None,
|
Spatial::add_to(&_node, None, Mat4::IDENTITY, false)?;
|
||||||
)?);
|
let model = sk.model_create_mem("cursor.glb", include_bytes!("cursor.glb"), None)?;
|
||||||
let model_nodes = model.get_nodes();
|
let tip = InputType::Tip(Tip::default());
|
||||||
let mut model_node = model_nodes.visuals().next().unwrap();
|
let input = InputMethod::add_to(&_node, tip, None)?;
|
||||||
let material = Material::copy(&model_node.get_material().unwrap());
|
|
||||||
model_node.material(&material);
|
|
||||||
let tip = InputDataType::Tip(Tip::default());
|
|
||||||
let input = InputMethod::add_to(
|
|
||||||
&spatial.node().unwrap(),
|
|
||||||
tip,
|
|
||||||
Datamap::from_typed(ControllerDatamap::default())?,
|
|
||||||
)?;
|
|
||||||
Ok(SkController {
|
Ok(SkController {
|
||||||
object_handle,
|
_node,
|
||||||
input,
|
input,
|
||||||
handed,
|
handed,
|
||||||
model,
|
model,
|
||||||
material,
|
|
||||||
capture_manager: CaptureManager::default(),
|
|
||||||
datamap: Default::default(),
|
datamap: Default::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn update(&mut self, token: &MainThreadToken) {
|
pub fn update(&mut self, sk: &impl StereoKitDraw) {
|
||||||
let controller = Input::controller(self.handed);
|
let controller = sk.input_controller(self.handed);
|
||||||
let input_node = self.input.spatial.node().unwrap();
|
*self.input.enabled.lock() = controller.tracked.contains(ButtonState::ACTIVE);
|
||||||
input_node.set_enabled(controller.tracked.is_active());
|
if *self.input.enabled.lock() {
|
||||||
if input_node.enabled() {
|
|
||||||
let world_transform = Mat4::from_rotation_translation(
|
let world_transform = Mat4::from_rotation_translation(
|
||||||
controller.aim.orientation.into(),
|
controller.aim.orientation,
|
||||||
controller.aim.position.into(),
|
controller.aim.position,
|
||||||
);
|
);
|
||||||
self.material
|
sk.model_draw(
|
||||||
.color_tint(if self.capture_manager.capture.is_none() {
|
&self.model,
|
||||||
Color128::new_rgb(1.0, 1.0, 1.0)
|
|
||||||
} else {
|
|
||||||
Color128::new_rgb(0.0, 1.0, 0.75)
|
|
||||||
});
|
|
||||||
self.model.draw(
|
|
||||||
token,
|
|
||||||
world_transform * Mat4::from_scale(Vec3::ONE * 0.02),
|
world_transform * Mat4::from_scale(Vec3::ONE * 0.02),
|
||||||
None,
|
WHITE,
|
||||||
None,
|
RenderLayer::LAYER0,
|
||||||
);
|
);
|
||||||
self.input.spatial.set_local_transform(world_transform);
|
self.input.spatial.set_local_transform(world_transform);
|
||||||
}
|
}
|
||||||
|
self.datamap.select = controller.trigger;
|
||||||
self.datamap = ControllerDatamap {
|
self.datamap.grab = controller.grip;
|
||||||
select: controller.trigger,
|
self.datamap.scroll = controller.stick;
|
||||||
middle: controller.stick_click.is_active() as u32 as f32,
|
*self.input.datamap.lock() = self.datamap.to_datamap().ok();
|
||||||
context: controller.is_x2_pressed() as u32 as f32,
|
|
||||||
grab: controller.grip,
|
|
||||||
scroll: controller.stick.into(),
|
|
||||||
};
|
|
||||||
*self.input.datamap.lock() = Datamap::from_typed(&self.datamap).unwrap();
|
|
||||||
|
|
||||||
let distance_calculator = |space: &Arc<Spatial>, _data: &InputDataType, field: &Field| {
|
|
||||||
Some(field.distance(space, [0.0; 3].into()).abs())
|
|
||||||
};
|
|
||||||
|
|
||||||
self.capture_manager.update_capture(&self.input);
|
|
||||||
self.capture_manager
|
|
||||||
.set_new_capture(&self.input, distance_calculator);
|
|
||||||
self.capture_manager.apply_capture(&self.input);
|
|
||||||
|
|
||||||
if self.capture_manager.capture.is_some() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let sorted_handlers = get_sorted_handlers(&self.input, distance_calculator);
|
|
||||||
self.input.set_handler_order(sorted_handlers.iter());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,23 @@
|
|||||||
use crate::core::client::INTERNAL_CLIENT;
|
use crate::{
|
||||||
use crate::nodes::fields::{Field, FieldTrait};
|
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
|
||||||
use crate::nodes::input::{InputDataType, InputHandler, INPUT_HANDLER_REGISTRY};
|
nodes::{
|
||||||
use crate::nodes::OwnedNode;
|
input::{hand::Hand, InputMethod, InputType},
|
||||||
use crate::nodes::{
|
spatial::Spatial,
|
||||||
input::{Hand, InputMethod, Joint},
|
Node,
|
||||||
spatial::Spatial,
|
},
|
||||||
Node,
|
|
||||||
};
|
};
|
||||||
use crate::objects::{ObjectHandle, SpatialRef};
|
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::{Mat4, Quat, Vec3};
|
use glam::Mat4;
|
||||||
|
use nanoid::nanoid;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use stardust_xr::values::Datamap;
|
use stardust_xr::schemas::flat::{Hand as FlatHand, Joint};
|
||||||
use std::f32::INFINITY;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use stereokit_rust::sk::{DisplayMode, MainThreadToken, Sk};
|
use stereokit::{ButtonState, HandJoint, Handed, StereoKitMultiThread};
|
||||||
use stereokit_rust::system::{HandJoint, HandSource, Handed, Input, LinePoint, Lines};
|
|
||||||
use stereokit_rust::util::Color128;
|
|
||||||
use zbus::Connection;
|
|
||||||
|
|
||||||
use super::{get_sorted_handlers, CaptureManager};
|
|
||||||
|
|
||||||
fn convert_joint(joint: HandJoint) -> Joint {
|
fn convert_joint(joint: HandJoint) -> Joint {
|
||||||
Joint {
|
Joint {
|
||||||
position: Vec3::from(joint.position).into(),
|
position: joint.position.into(),
|
||||||
rotation: Quat::from(joint.orientation).into(),
|
rotation: joint.orientation.into(),
|
||||||
radius: joint.radius,
|
radius: joint.radius,
|
||||||
distance: 0.0,
|
distance: 0.0,
|
||||||
}
|
}
|
||||||
@@ -37,66 +30,52 @@ struct HandDatamap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct SkHand {
|
pub struct SkHand {
|
||||||
_node: OwnedNode,
|
_node: Arc<Node>,
|
||||||
palm_spatial: Arc<Spatial>,
|
|
||||||
palm_object: ObjectHandle<SpatialRef>,
|
|
||||||
handed: Handed,
|
|
||||||
input: Arc<InputMethod>,
|
input: Arc<InputMethod>,
|
||||||
capture_manager: CaptureManager,
|
handed: Handed,
|
||||||
datamap: HandDatamap,
|
datamap: TypedDatamap<HandDatamap>,
|
||||||
}
|
}
|
||||||
impl SkHand {
|
impl SkHand {
|
||||||
pub fn new(connection: &Connection, handed: Handed) -> Result<Self> {
|
pub fn new(handed: Handed) -> Result<Self> {
|
||||||
let (palm_spatial, palm_object) = SpatialRef::create(
|
let _node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
|
||||||
connection,
|
Spatial::add_to(&_node, None, Mat4::IDENTITY, false)?;
|
||||||
&("/org/stardustxr/Hand/".to_string()
|
let hand = InputType::Hand(Box::new(Hand {
|
||||||
+ match handed {
|
base: FlatHand {
|
||||||
Handed::Left => "left",
|
right: handed == Handed::Right,
|
||||||
_ => "right",
|
..Default::default()
|
||||||
} + "/palm"),
|
},
|
||||||
);
|
}));
|
||||||
let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
|
let input = InputMethod::add_to(&_node, hand, None)?;
|
||||||
Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false);
|
|
||||||
let hand = InputDataType::Hand(Hand {
|
|
||||||
right: handed == Handed::Right,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
let datamap = Datamap::from_typed(HandDatamap::default())?;
|
|
||||||
let input = InputMethod::add_to(&_node.0, hand, datamap)?;
|
|
||||||
|
|
||||||
Input::hand_visible(handed, false);
|
|
||||||
Ok(SkHand {
|
Ok(SkHand {
|
||||||
_node,
|
_node,
|
||||||
palm_spatial,
|
|
||||||
palm_object,
|
|
||||||
handed,
|
|
||||||
input,
|
input,
|
||||||
capture_manager: CaptureManager::default(),
|
handed,
|
||||||
datamap: Default::default(),
|
datamap: Default::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn update(&mut self, sk: &Sk, token: &MainThreadToken) {
|
pub fn update(&mut self, controller_enabled: bool, sk: &impl StereoKitMultiThread) {
|
||||||
let sk_hand = Input::hand(self.handed);
|
let sk_hand = sk.input_hand(self.handed);
|
||||||
let real_hand = Input::hand_source(self.handed) as u32 == HandSource::Articulated as u32;
|
if let InputType::Hand(hand) = &mut *self.input.specialization.lock() {
|
||||||
if let InputDataType::Hand(hand) = &mut *self.input.data.lock() {
|
let controller_active = controller_enabled
|
||||||
let input_node = self.input.spatial.node().unwrap();
|
&& sk
|
||||||
input_node.set_enabled(
|
.input_controller(self.handed)
|
||||||
(real_hand || sk.get_active_display_mode() == DisplayMode::Flatscreen)
|
.tracked
|
||||||
&& sk_hand.tracked.is_active(),
|
.contains(ButtonState::ACTIVE);
|
||||||
);
|
*self.input.enabled.lock() =
|
||||||
if input_node.enabled() {
|
!controller_active && sk_hand.tracked_state.contains(ButtonState::ACTIVE);
|
||||||
hand.thumb.tip = convert_joint(sk_hand.fingers[0][4]);
|
sk.input_hand_visible(self.handed, *self.input.enabled.lock());
|
||||||
hand.thumb.distal = convert_joint(sk_hand.fingers[0][3]);
|
if *self.input.enabled.lock() {
|
||||||
hand.thumb.proximal = convert_joint(sk_hand.fingers[0][2]);
|
hand.base.thumb.tip = convert_joint(sk_hand.fingers[0][4]);
|
||||||
hand.thumb.metacarpal = convert_joint(sk_hand.fingers[0][1]);
|
hand.base.thumb.distal = convert_joint(sk_hand.fingers[0][3]);
|
||||||
|
hand.base.thumb.proximal = convert_joint(sk_hand.fingers[0][2]);
|
||||||
|
hand.base.thumb.metacarpal = convert_joint(sk_hand.fingers[0][1]);
|
||||||
|
|
||||||
for (finger, mut sk_finger) in [
|
for (finger, sk_finger) in [
|
||||||
(&mut hand.index, sk_hand.fingers[1]),
|
(&mut hand.base.index, sk_hand.fingers[1]),
|
||||||
(&mut hand.middle, sk_hand.fingers[2]),
|
(&mut hand.base.middle, sk_hand.fingers[2]),
|
||||||
(&mut hand.ring, sk_hand.fingers[3]),
|
(&mut hand.base.ring, sk_hand.fingers[3]),
|
||||||
(&mut hand.little, sk_hand.fingers[4]),
|
(&mut hand.base.little, sk_hand.fingers[4]),
|
||||||
] {
|
] {
|
||||||
sk_finger[4].radius = 0.0;
|
|
||||||
finger.tip = convert_joint(sk_finger[4]);
|
finger.tip = convert_joint(sk_finger[4]);
|
||||||
finger.distal = convert_joint(sk_finger[3]);
|
finger.distal = convert_joint(sk_finger[3]);
|
||||||
finger.intermediate = convert_joint(sk_finger[2]);
|
finger.intermediate = convert_joint(sk_finger[2]);
|
||||||
@@ -104,134 +83,21 @@ impl SkHand {
|
|||||||
finger.metacarpal = convert_joint(sk_finger[0]);
|
finger.metacarpal = convert_joint(sk_finger[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
hand.palm.position = Vec3::from(sk_hand.palm.position).into();
|
hand.base.palm.position = sk_hand.palm.position.into();
|
||||||
hand.palm.rotation = Quat::from(sk_hand.palm.orientation).into();
|
hand.base.palm.rotation = sk_hand.palm.orientation.into();
|
||||||
hand.palm.radius =
|
hand.base.palm.radius =
|
||||||
(sk_hand.fingers[2][0].radius + sk_hand.fingers[2][1].radius) * 0.5;
|
(sk_hand.fingers[2][0].radius + sk_hand.fingers[2][1].radius) * 0.5;
|
||||||
|
|
||||||
self.palm_spatial
|
hand.base.wrist.position = sk_hand.wrist.position.into();
|
||||||
.set_local_transform(Mat4::from_rotation_translation(
|
hand.base.wrist.rotation = sk_hand.wrist.orientation.into();
|
||||||
hand.palm.rotation.into(),
|
hand.base.wrist.radius =
|
||||||
hand.palm.position.into(),
|
|
||||||
));
|
|
||||||
|
|
||||||
hand.wrist.position = Vec3::from(sk_hand.wrist.position).into();
|
|
||||||
hand.wrist.rotation = Quat::from(sk_hand.wrist.orientation).into();
|
|
||||||
hand.wrist.radius =
|
|
||||||
(sk_hand.fingers[0][0].radius + sk_hand.fingers[4][0].radius) * 0.5;
|
(sk_hand.fingers[0][0].radius + sk_hand.fingers[4][0].radius) * 0.5;
|
||||||
|
|
||||||
hand.elbow = None;
|
hand.base.elbow = None;
|
||||||
|
|
||||||
self.draw(
|
|
||||||
token,
|
|
||||||
if self.capture_manager.capture.is_none() {
|
|
||||||
Color128::new_rgb(1.0, 1.0, 1.0)
|
|
||||||
} else {
|
|
||||||
Color128::new_rgb(0.0, 1.0, 0.75)
|
|
||||||
},
|
|
||||||
hand,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.datamap.pinch_strength = sk_hand.pinch_activation;
|
self.datamap.pinch_strength = sk_hand.pinch_activation;
|
||||||
self.datamap.grab_strength = sk_hand.grip_activation;
|
self.datamap.grab_strength = sk_hand.grip_activation;
|
||||||
*self.input.datamap.lock() = Datamap::from_typed(&self.datamap).unwrap();
|
*self.input.datamap.lock() = self.datamap.to_datamap().ok();
|
||||||
|
|
||||||
let distance_calculator = |space: &Arc<Spatial>, data: &InputDataType, field: &Field| {
|
|
||||||
let InputDataType::Hand(hand) = data else {
|
|
||||||
return None;
|
|
||||||
};
|
|
||||||
let thumb_tip_distance = field.distance(space, hand.thumb.tip.position.into());
|
|
||||||
let index_tip_distance = field.distance(space, hand.index.tip.position.into());
|
|
||||||
let middle_tip_distance = field.distance(space, hand.middle.tip.position.into());
|
|
||||||
let ring_tip_distance = field.distance(space, hand.ring.tip.position.into());
|
|
||||||
|
|
||||||
Some(
|
|
||||||
(thumb_tip_distance * 0.3)
|
|
||||||
+ (index_tip_distance * 0.4)
|
|
||||||
+ (middle_tip_distance * 0.15)
|
|
||||||
+ (ring_tip_distance * 0.15),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
self.capture_manager.update_capture(&self.input);
|
|
||||||
self.capture_manager
|
|
||||||
.set_new_capture(&self.input, distance_calculator);
|
|
||||||
self.capture_manager.apply_capture(&self.input);
|
|
||||||
|
|
||||||
if self.capture_manager.capture.is_some() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let sorted_handlers = get_sorted_handlers(&self.input, distance_calculator);
|
|
||||||
self.input.set_handler_order(sorted_handlers.iter());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&self, token: &MainThreadToken, color: Color128, hand: &Hand) {
|
|
||||||
// thumb
|
|
||||||
Lines::add_list(
|
|
||||||
token,
|
|
||||||
&[
|
|
||||||
joint_to_line_point(&hand.thumb.tip, color),
|
|
||||||
joint_to_line_point(&hand.thumb.distal, color),
|
|
||||||
joint_to_line_point(&hand.thumb.proximal, color),
|
|
||||||
joint_to_line_point(&hand.thumb.metacarpal, color),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
// index
|
|
||||||
Lines::add_list(
|
|
||||||
token,
|
|
||||||
&[
|
|
||||||
joint_to_line_point(&hand.index.tip, color),
|
|
||||||
joint_to_line_point(&hand.index.distal, color),
|
|
||||||
joint_to_line_point(&hand.index.intermediate, color),
|
|
||||||
joint_to_line_point(&hand.index.proximal, color),
|
|
||||||
joint_to_line_point(&hand.index.metacarpal, color),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
// middle
|
|
||||||
Lines::add_list(
|
|
||||||
token,
|
|
||||||
&[
|
|
||||||
joint_to_line_point(&hand.middle.tip, color),
|
|
||||||
joint_to_line_point(&hand.middle.distal, color),
|
|
||||||
joint_to_line_point(&hand.middle.intermediate, color),
|
|
||||||
joint_to_line_point(&hand.middle.proximal, color),
|
|
||||||
joint_to_line_point(&hand.middle.metacarpal, color),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
// ring
|
|
||||||
Lines::add_list(
|
|
||||||
token,
|
|
||||||
&[
|
|
||||||
joint_to_line_point(&hand.ring.tip, color),
|
|
||||||
joint_to_line_point(&hand.ring.distal, color),
|
|
||||||
joint_to_line_point(&hand.ring.intermediate, color),
|
|
||||||
joint_to_line_point(&hand.ring.proximal, color),
|
|
||||||
joint_to_line_point(&hand.ring.metacarpal, color),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// palm
|
|
||||||
Lines::add_list(
|
|
||||||
token,
|
|
||||||
&[
|
|
||||||
joint_to_line_point(&hand.wrist, color),
|
|
||||||
joint_to_line_point(&hand.thumb.metacarpal, color),
|
|
||||||
joint_to_line_point(&hand.index.metacarpal, color),
|
|
||||||
joint_to_line_point(&hand.middle.metacarpal, color),
|
|
||||||
joint_to_line_point(&hand.ring.metacarpal, color),
|
|
||||||
joint_to_line_point(&hand.little.metacarpal, color),
|
|
||||||
joint_to_line_point(&hand.wrist, color),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn joint_to_line_point(joint: &Joint, color: Color128) -> LinePoint {
|
|
||||||
LinePoint {
|
|
||||||
pt: Vec3::from(joint.position).into(),
|
|
||||||
thickness: joint.radius * 2.0,
|
|
||||||
color: color.into(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,274 +1,2 @@
|
|||||||
#![allow(unused)]
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
core::client::INTERNAL_CLIENT,
|
|
||||||
nodes::{
|
|
||||||
fields::{Field, Shape, EXPORTED_FIELDS},
|
|
||||||
spatial::{Spatial, EXPORTED_SPATIALS},
|
|
||||||
Node, OwnedNode,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use glam::{vec3, Mat4};
|
|
||||||
use input::{
|
|
||||||
eye_pointer::EyePointer, mouse_pointer::MousePointer, sk_controller::SkController,
|
|
||||||
sk_hand::SkHand,
|
|
||||||
};
|
|
||||||
use play_space::PlaySpaceBounds;
|
|
||||||
use std::{marker::PhantomData, sync::Arc};
|
|
||||||
use stereokit_rust::{
|
|
||||||
sk::{DisplayMode, MainThreadToken, Sk},
|
|
||||||
system::{Handed, Input, Key, World},
|
|
||||||
util::Device,
|
|
||||||
};
|
|
||||||
use zbus::{interface, object_server::Interface, zvariant::OwnedObjectPath, Connection};
|
|
||||||
|
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod play_space;
|
pub mod play_space;
|
||||||
|
|
||||||
enum Inputs {
|
|
||||||
XR {
|
|
||||||
controller_left: SkController,
|
|
||||||
controller_right: SkController,
|
|
||||||
hand_left: SkHand,
|
|
||||||
hand_right: SkHand,
|
|
||||||
eye_pointer: Option<EyePointer>,
|
|
||||||
},
|
|
||||||
MousePointer(MousePointer),
|
|
||||||
// Controllers((SkController, SkController)),
|
|
||||||
Hands {
|
|
||||||
left: SkHand,
|
|
||||||
right: SkHand,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ServerObjects {
|
|
||||||
connection: Connection,
|
|
||||||
hmd: (Arc<Spatial>, ObjectHandle<SpatialRef>),
|
|
||||||
play_space: Option<(Arc<Spatial>, ObjectHandle<SpatialRef>)>,
|
|
||||||
inputs: Inputs,
|
|
||||||
disable_controllers: bool,
|
|
||||||
disable_hands: bool,
|
|
||||||
}
|
|
||||||
impl ServerObjects {
|
|
||||||
pub fn new(
|
|
||||||
connection: Connection,
|
|
||||||
sk: &Sk,
|
|
||||||
disable_controllers: bool,
|
|
||||||
disable_hands: bool,
|
|
||||||
) -> ServerObjects {
|
|
||||||
let hmd = SpatialRef::create(&connection, "/org/stardustxr/HMD");
|
|
||||||
|
|
||||||
let play_space = (World::has_bounds()
|
|
||||||
&& World::get_bounds_size().x != 0.0
|
|
||||||
&& World::get_bounds_size().y != 0.0)
|
|
||||||
.then(|| SpatialRef::create(&connection, "/org/stardustxr/PlaySpace"));
|
|
||||||
if play_space.is_some() {
|
|
||||||
let dbus_connection = connection.clone();
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
PlaySpaceBounds::create(&dbus_connection).await;
|
|
||||||
dbus_connection
|
|
||||||
.request_name("org.stardustxr.PlaySpace")
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
tokio::task::spawn({
|
|
||||||
let connection = connection.clone();
|
|
||||||
async move {
|
|
||||||
connection
|
|
||||||
.request_name("org.stardustxr.Controllers")
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
connection
|
|
||||||
.request_name("org.stardustxr.Hands")
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
|
|
||||||
Inputs::XR {
|
|
||||||
controller_left: SkController::new(&connection, Handed::Left).unwrap(),
|
|
||||||
controller_right: SkController::new(&connection, Handed::Right).unwrap(),
|
|
||||||
hand_left: SkHand::new(&connection, Handed::Left).unwrap(),
|
|
||||||
hand_right: SkHand::new(&connection, Handed::Right).unwrap(),
|
|
||||||
eye_pointer: Device::has_eye_gaze()
|
|
||||||
.then(EyePointer::new)
|
|
||||||
.transpose()
|
|
||||||
.unwrap(),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Inputs::MousePointer(MousePointer::new().unwrap())
|
|
||||||
};
|
|
||||||
|
|
||||||
ServerObjects {
|
|
||||||
connection,
|
|
||||||
hmd,
|
|
||||||
play_space,
|
|
||||||
inputs,
|
|
||||||
disable_controllers,
|
|
||||||
disable_hands,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self, sk: &Sk, token: &MainThreadToken) {
|
|
||||||
let hmd_pose = Input::get_head();
|
|
||||||
self.hmd
|
|
||||||
.0
|
|
||||||
.set_local_transform(Mat4::from_scale_rotation_translation(
|
|
||||||
vec3(1.0, 1.0, 1.0),
|
|
||||||
hmd_pose.orientation.into(),
|
|
||||||
hmd_pose.position.into(),
|
|
||||||
));
|
|
||||||
|
|
||||||
if let Some(play_space) = self.play_space.as_ref() {
|
|
||||||
let pose = World::get_bounds_pose();
|
|
||||||
play_space
|
|
||||||
.0
|
|
||||||
.set_local_transform(Mat4::from_rotation_translation(
|
|
||||||
pose.orientation.into(),
|
|
||||||
pose.position.into(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
if sk.get_active_display_mode() != DisplayMode::MixedReality {
|
|
||||||
if Input::key(Key::F6).is_just_inactive() {
|
|
||||||
self.inputs = Inputs::MousePointer(MousePointer::new().unwrap());
|
|
||||||
}
|
|
||||||
// if Input::key(Key::F7).is_just_inactive() {
|
|
||||||
// self.inputs = Inputs::Controllers((
|
|
||||||
// SkController::new(Handed::Left).unwrap(),
|
|
||||||
// SkController::new(Handed::Right).unwrap(),
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
if Input::key(Key::F8).is_just_inactive() {
|
|
||||||
self.inputs = Inputs::Hands {
|
|
||||||
left: SkHand::new(&self.connection, Handed::Left).unwrap(),
|
|
||||||
right: SkHand::new(&self.connection, Handed::Right).unwrap(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match &mut self.inputs {
|
|
||||||
Inputs::XR {
|
|
||||||
controller_left,
|
|
||||||
controller_right,
|
|
||||||
hand_left,
|
|
||||||
hand_right,
|
|
||||||
eye_pointer,
|
|
||||||
} => {
|
|
||||||
if !self.disable_controllers {
|
|
||||||
controller_left.update(token);
|
|
||||||
controller_right.update(token);
|
|
||||||
}
|
|
||||||
if !self.disable_hands {
|
|
||||||
hand_left.update(sk, token);
|
|
||||||
hand_right.update(sk, token);
|
|
||||||
}
|
|
||||||
if let Some(eye_pointer) = eye_pointer {
|
|
||||||
eye_pointer.update();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Inputs::MousePointer(mouse_pointer) => mouse_pointer.update(),
|
|
||||||
// Inputs::Controllers((left, right)) => {
|
|
||||||
// left.update(token);
|
|
||||||
// right.update(token);
|
|
||||||
// }
|
|
||||||
Inputs::Hands { left, right } => {
|
|
||||||
left.update(sk, token);
|
|
||||||
right.update(sk, token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ObjectHandle<I: Interface>(Connection, OwnedObjectPath, PhantomData<I>);
|
|
||||||
impl<I: Interface> Drop for ObjectHandle<I> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
let connection = self.0.clone();
|
|
||||||
let object_path = self.1.clone();
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
connection.object_server().remove::<I, _>(object_path);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SpatialRef(u64, OwnedNode);
|
|
||||||
impl SpatialRef {
|
|
||||||
pub fn create(connection: &Connection, path: &str) -> (Arc<Spatial>, ObjectHandle<SpatialRef>) {
|
|
||||||
let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false)));
|
|
||||||
let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
|
|
||||||
let uid: u64 = rand::random();
|
|
||||||
EXPORTED_SPATIALS.lock().insert(uid, node.0.clone());
|
|
||||||
|
|
||||||
tokio::task::spawn({
|
|
||||||
let connection = connection.clone();
|
|
||||||
let path = path.to_string();
|
|
||||||
async move {
|
|
||||||
connection
|
|
||||||
.object_server()
|
|
||||||
.at(path, Self(uid, node))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
(
|
|
||||||
spatial,
|
|
||||||
ObjectHandle(
|
|
||||||
connection.clone(),
|
|
||||||
OwnedObjectPath::try_from(path.to_string()).unwrap(),
|
|
||||||
PhantomData,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[interface(name = "org.stardustxr.SpatialRef")]
|
|
||||||
impl SpatialRef {
|
|
||||||
#[zbus(property)]
|
|
||||||
fn uid(&self) -> u64 {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FieldRef(u64, OwnedNode);
|
|
||||||
impl FieldRef {
|
|
||||||
pub fn create(
|
|
||||||
connection: &Connection,
|
|
||||||
path: &str,
|
|
||||||
shape: Shape,
|
|
||||||
) -> (Arc<Field>, ObjectHandle<FieldRef>) {
|
|
||||||
let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false)));
|
|
||||||
Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
|
|
||||||
let field = Field::add_to(&node.0, shape).unwrap();
|
|
||||||
let uid: u64 = rand::random();
|
|
||||||
EXPORTED_FIELDS.lock().insert(uid, node.0.clone());
|
|
||||||
|
|
||||||
tokio::task::spawn({
|
|
||||||
let connection = connection.clone();
|
|
||||||
let path = path.to_string();
|
|
||||||
async move {
|
|
||||||
connection
|
|
||||||
.object_server()
|
|
||||||
.at(path, Self(uid, node))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
(
|
|
||||||
field,
|
|
||||||
ObjectHandle(
|
|
||||||
connection.clone(),
|
|
||||||
OwnedObjectPath::try_from(path.to_string()).unwrap(),
|
|
||||||
PhantomData,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[interface(name = "org.stardustxr.FieldRef")]
|
|
||||||
impl FieldRef {
|
|
||||||
#[zbus(property)]
|
|
||||||
fn uid(&self) -> u64 {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,26 +1,73 @@
|
|||||||
use stereokit_rust::system::World;
|
use std::sync::Arc;
|
||||||
use zbus::{interface, Connection, ObjectServer};
|
|
||||||
|
|
||||||
pub struct PlaySpaceBounds;
|
use color_eyre::eyre::Result;
|
||||||
impl PlaySpaceBounds {
|
use glam::Mat4;
|
||||||
pub async fn create(connection: &Connection) {
|
use mint::Vector2;
|
||||||
connection
|
use nanoid::nanoid;
|
||||||
.object_server()
|
use serde::{Deserialize, Serialize};
|
||||||
.at("/org/stardustxr/PlaySpace", Self)
|
use stereokit::StereoKitMultiThread;
|
||||||
.await
|
|
||||||
.unwrap();
|
use crate::{
|
||||||
|
core::client::INTERNAL_CLIENT,
|
||||||
|
nodes::{
|
||||||
|
data::{Mask, PulseReceiver},
|
||||||
|
fields::{r#box::BoxField, Field},
|
||||||
|
spatial::Spatial,
|
||||||
|
Node,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
struct PlaySpaceMap {
|
||||||
|
play_space: (),
|
||||||
|
size: Vector2<f32>,
|
||||||
|
}
|
||||||
|
impl Default for PlaySpaceMap {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
play_space: (),
|
||||||
|
size: [0.0; 2].into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[interface(name = "org.stardustxr.PlaySpace")]
|
|
||||||
impl PlaySpaceBounds {
|
pub struct PlaySpace {
|
||||||
#[zbus(property)]
|
_node: Arc<Node>,
|
||||||
fn bounds(&self) -> Vec<(f64, f64)> {
|
spatial: Arc<Spatial>,
|
||||||
let bounds = World::get_bounds_size();
|
field: Arc<Field>,
|
||||||
vec![
|
_pulse_rx: Arc<PulseReceiver>,
|
||||||
((bounds.x).into(), (bounds.y).into()),
|
}
|
||||||
((bounds.x).into(), (-bounds.y).into()),
|
impl PlaySpace {
|
||||||
((-bounds.x).into(), (-bounds.y).into()),
|
pub fn new() -> Result<Self> {
|
||||||
((-bounds.x).into(), (bounds.y).into()),
|
let node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
|
||||||
]
|
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false)?;
|
||||||
|
let field = BoxField::add_to(&node, [0.0; 3].into())?;
|
||||||
|
|
||||||
|
let pulse_rx =
|
||||||
|
PulseReceiver::add_to(&node, field.clone(), Mask::from_struct::<PlaySpaceMap>())?;
|
||||||
|
|
||||||
|
Ok(PlaySpace {
|
||||||
|
_node: node,
|
||||||
|
spatial,
|
||||||
|
field,
|
||||||
|
_pulse_rx: pulse_rx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn update(&self, sk: &impl StereoKitMultiThread) {
|
||||||
|
let pose = sk.world_get_bounds_pose();
|
||||||
|
self.spatial
|
||||||
|
.set_local_transform(Mat4::from_rotation_translation(
|
||||||
|
pose.orientation,
|
||||||
|
pose.position,
|
||||||
|
));
|
||||||
|
let Field::Box(box_field) = self.field.as_ref() else {return};
|
||||||
|
box_field.set_size(
|
||||||
|
[
|
||||||
|
sk.world_get_bounds_size().x,
|
||||||
|
0.0,
|
||||||
|
sk.world_get_bounds_size().y,
|
||||||
|
]
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
114
src/session.rs
114
src/session.rs
@@ -1,114 +0,0 @@
|
|||||||
use crate::core::client::CLIENTS;
|
|
||||||
use crate::core::client_state::ClientStateParsed;
|
|
||||||
#[cfg(feature = "wayland")]
|
|
||||||
use crate::wayland::WAYLAND_DISPLAY;
|
|
||||||
use crate::{CliArgs, STARDUST_INSTANCE};
|
|
||||||
use directories::ProjectDirs;
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
use std::os::unix::fs::PermissionsExt;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::process::{Child, Command, Stdio};
|
|
||||||
use std::time::Duration;
|
|
||||||
use tokio::task::LocalSet;
|
|
||||||
use tracing::info;
|
|
||||||
|
|
||||||
pub async fn save_session(project_dirs: &ProjectDirs) {
|
|
||||||
let session_id = nanoid::nanoid!();
|
|
||||||
let state_dir = project_dirs.state_dir().unwrap();
|
|
||||||
let session_dir = state_dir.join(&session_id);
|
|
||||||
std::fs::create_dir_all(&session_dir).unwrap();
|
|
||||||
let _ = std::fs::remove_dir_all(state_dir.join("latest"));
|
|
||||||
std::os::unix::fs::symlink(&session_dir, state_dir.join("latest")).unwrap();
|
|
||||||
|
|
||||||
let local_set = LocalSet::new();
|
|
||||||
for client in CLIENTS.get_vec() {
|
|
||||||
let session_dir = session_dir.clone();
|
|
||||||
local_set.spawn_local(async move {
|
|
||||||
tokio::select! {
|
|
||||||
biased;
|
|
||||||
s = client.save_state() => {if let Some(s) = s { s.to_file(&session_dir) }},
|
|
||||||
_ = tokio::time::sleep(Duration::from_millis(100)) => (),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
local_set.await;
|
|
||||||
info!("Session ID for restore is {session_id}");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn launch_start(cli_args: &CliArgs, project_dirs: &ProjectDirs) -> Vec<Child> {
|
|
||||||
match (&cli_args.restore, &cli_args.startup_script) {
|
|
||||||
(Some(session_id), _) => restore_session(
|
|
||||||
&project_dirs.state_dir().unwrap().join(session_id),
|
|
||||||
cli_args.debug_launched_clients,
|
|
||||||
),
|
|
||||||
(None, Some(startup_script)) => run_script(
|
|
||||||
&startup_script.clone().canonicalize().unwrap_or_default(),
|
|
||||||
cli_args.debug_launched_clients,
|
|
||||||
),
|
|
||||||
(None, None) => run_script(
|
|
||||||
&project_dirs.config_dir().join("startup"),
|
|
||||||
cli_args.debug_launched_clients,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn restore_session(session_dir: &Path, debug_launched_clients: bool) -> Vec<Child> {
|
|
||||||
let Ok(clients) = session_dir.read_dir() else {
|
|
||||||
return Vec::new();
|
|
||||||
};
|
|
||||||
clients
|
|
||||||
.filter_map(Result::ok)
|
|
||||||
.filter_map(|c| ClientStateParsed::from_file(&c.path()))
|
|
||||||
.filter_map(ClientStateParsed::launch_command)
|
|
||||||
.filter_map(|c| run_client(c, debug_launched_clients))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run_script(script_path: &Path, debug_launched_clients: bool) -> Vec<Child> {
|
|
||||||
let _ = std::fs::set_permissions(script_path, std::fs::Permissions::from_mode(0o755));
|
|
||||||
let startup_command = Command::new(script_path);
|
|
||||||
run_client(startup_command, debug_launched_clients)
|
|
||||||
.map(|c| vec![c])
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run_client(mut command: Command, debug_launched_clients: bool) -> Option<Child> {
|
|
||||||
command.stdin(Stdio::null());
|
|
||||||
if !debug_launched_clients {
|
|
||||||
command.stdout(Stdio::null());
|
|
||||||
command.stderr(Stdio::null());
|
|
||||||
}
|
|
||||||
for (var, value) in connection_env() {
|
|
||||||
command.env(var, value);
|
|
||||||
}
|
|
||||||
let child = command.spawn().ok()?;
|
|
||||||
Some(child)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn connection_env() -> FxHashMap<String, String> {
|
|
||||||
macro_rules! var_env_insert {
|
|
||||||
($env:ident, $name:ident) => {
|
|
||||||
$env.insert(stringify!($name).to_string(), $name.get().unwrap().clone());
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut env: FxHashMap<String, String> = FxHashMap::default();
|
|
||||||
var_env_insert!(env, STARDUST_INSTANCE);
|
|
||||||
|
|
||||||
if let Some(flat_wayland_display) = std::env::var_os("WAYLAND_DISPLAY") {
|
|
||||||
env.insert(
|
|
||||||
"FLAT_WAYLAND_DISPLAY".to_string(),
|
|
||||||
flat_wayland_display.to_string_lossy().into_owned(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
#[cfg(feature = "wayland")]
|
|
||||||
{
|
|
||||||
var_env_insert!(env, WAYLAND_DISPLAY);
|
|
||||||
env.insert("GDK_BACKEND".to_string(), "wayland".to_string());
|
|
||||||
env.insert("QT_QPA_PLATFORM".to_string(), "wayland".to_string());
|
|
||||||
env.insert("MOZ_ENABLE_WAYLAND".to_string(), "1".to_string());
|
|
||||||
env.insert("CLUTTER_BACKEND".to_string(), "wayland".to_string());
|
|
||||||
env.insert("SDL_VIDEODRIVER".to_string(), "wayland".to_string());
|
|
||||||
}
|
|
||||||
env
|
|
||||||
}
|
|
||||||
@@ -1,22 +1,13 @@
|
|||||||
use super::{
|
use crate::wayland::surface::CoreSurface;
|
||||||
state::{ClientState, WaylandState},
|
|
||||||
utils::{ChildInfoExt, WlSurfaceExt},
|
use super::state::{ClientState, WaylandState};
|
||||||
xdg_shell::surface_panel_item,
|
|
||||||
};
|
|
||||||
use crate::{
|
|
||||||
nodes::items::panel::{ChildInfo, Geometry, SurfaceId},
|
|
||||||
wayland::surface::CoreSurface,
|
|
||||||
};
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use portable_atomic::{AtomicU32, Ordering};
|
use portable_atomic::{AtomicU32, Ordering};
|
||||||
use rand::Rng;
|
#[cfg(feature = "xwayland")]
|
||||||
|
use smithay::xwayland::XWaylandClientData;
|
||||||
use smithay::{
|
use smithay::{
|
||||||
backend::renderer::utils::{on_commit_buffer_handler, RendererSurfaceStateUserData},
|
|
||||||
delegate_compositor,
|
delegate_compositor,
|
||||||
reexports::wayland_server::{protocol::wl_surface::WlSurface, Client},
|
reexports::wayland_server::{protocol::wl_surface::WlSurface, Client},
|
||||||
wayland::compositor::{
|
wayland::compositor::{self, CompositorClientState, CompositorHandler, CompositorState},
|
||||||
self, add_post_commit_hook, CompositorClientState, CompositorHandler, CompositorState,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
@@ -28,10 +19,8 @@ impl CompositorHandler for WaylandState {
|
|||||||
|
|
||||||
fn commit(&mut self, surface: &WlSurface) {
|
fn commit(&mut self, surface: &WlSurface) {
|
||||||
debug!(?surface, "Surface commit");
|
debug!(?surface, "Surface commit");
|
||||||
|
|
||||||
on_commit_buffer_handler::<WaylandState>(surface);
|
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
compositor::with_states(surface, |data| {
|
let core_surface = compositor::with_states(surface, |data| {
|
||||||
let count_new = data
|
let count_new = data
|
||||||
.data_map
|
.data_map
|
||||||
.insert_if_missing_threadsafe(|| AtomicU32::new(0));
|
.insert_if_missing_threadsafe(|| AtomicU32::new(0));
|
||||||
@@ -43,83 +32,20 @@ impl CompositorHandler for WaylandState {
|
|||||||
|
|
||||||
data.data_map.get::<Arc<CoreSurface>>().cloned()
|
data.data_map.get::<Arc<CoreSurface>>().cloned()
|
||||||
});
|
});
|
||||||
|
if let Some(core_surface) = core_surface {
|
||||||
|
core_surface.commit(count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn client_compositor_state<'a>(&self, client: &'a Client) -> &'a CompositorClientState {
|
fn client_compositor_state<'a>(&self, client: &'a Client) -> &'a CompositorClientState {
|
||||||
&client.get_data::<ClientState>().unwrap().compositor_state
|
if let Some(client_state) = client.get_data::<ClientState>() {
|
||||||
}
|
&client_state.compositor_state
|
||||||
|
} else {
|
||||||
fn new_subsurface(&mut self, surface: &WlSurface, parent: &WlSurface) {
|
#[cfg(feature = "xwayland")]
|
||||||
let id = rand::thread_rng().gen_range(0..u64::MAX);
|
if let Some(xwayland_client_data) = client.get_data::<XWaylandClientData>() {
|
||||||
surface.insert_data(SurfaceId::Child(id));
|
return &xwayland_client_data.compositor_state;
|
||||||
CoreSurface::add_to(surface);
|
|
||||||
let Some(parent_surface_id) = parent.get_data::<SurfaceId>() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
surface.insert_data(Mutex::new(ChildInfo {
|
|
||||||
id,
|
|
||||||
parent: parent_surface_id,
|
|
||||||
geometry: Geometry {
|
|
||||||
origin: [0; 2].into(),
|
|
||||||
size: [256; 2].into(),
|
|
||||||
},
|
|
||||||
z_order: 1,
|
|
||||||
receives_input: false,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let Some(panel_item) = surface_panel_item(parent) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let panel_item_weak = Arc::downgrade(&panel_item);
|
|
||||||
add_post_commit_hook(surface, move |_: &mut WaylandState, _dh, surf| {
|
|
||||||
if surface_panel_item(surf).is_some() {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
surf.insert_data(panel_item_weak.clone());
|
unimplemented!()
|
||||||
|
|
||||||
let Some(panel_item) = surface_panel_item(surf) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
panel_item.backend.new_child(surf);
|
|
||||||
});
|
|
||||||
|
|
||||||
add_post_commit_hook(surface, move |_: &mut WaylandState, _dh, surf| {
|
|
||||||
let Some(view) = surf
|
|
||||||
.get_data_raw::<RendererSurfaceStateUserData, _, _>(|s| s.lock().ok()?.view())
|
|
||||||
.flatten()
|
|
||||||
else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let mut changed = false;
|
|
||||||
surf.with_child_info(|info| {
|
|
||||||
if info.geometry.origin.x != view.offset.x
|
|
||||||
&& info.geometry.origin.y != view.offset.y
|
|
||||||
{
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
if info.geometry.size.x != view.dst.w as u32
|
|
||||||
&& info.geometry.size.y != view.dst.h as u32
|
|
||||||
{
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
info.geometry.size = [view.dst.w as u32, view.dst.h as u32].into();
|
|
||||||
});
|
|
||||||
|
|
||||||
let Some(panel_item) = surface_panel_item(surf) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
if changed {
|
|
||||||
panel_item.backend.reposition_child(surf);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn destroyed(&mut self, surface: &WlSurface) {
|
|
||||||
let Some(panel_item) = surface_panel_item(surface) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
if surface.get_child_info().is_some() {
|
|
||||||
panel_item.backend.drop_child(surface);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ impl KdeDecorationHandler for WaylandState {
|
|||||||
decoration: &OrgKdeKwinServerDecoration,
|
decoration: &OrgKdeKwinServerDecoration,
|
||||||
mode: WEnum<KdeMode>,
|
mode: WEnum<KdeMode>,
|
||||||
) {
|
) {
|
||||||
let Ok(mode) = mode.into_result() else { return };
|
let Ok(mode) = mode.into_result() else {return};
|
||||||
decoration.mode(mode);
|
decoration.mode(mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,144 +0,0 @@
|
|||||||
// SPDX-License-Identifier: GPL-3.0-only
|
|
||||||
|
|
||||||
// Re-export only the actual code, and then only use this re-export
|
|
||||||
// The `generated` module below is just some boilerplate to properly isolate stuff
|
|
||||||
// and avoid exposing internal details.
|
|
||||||
//
|
|
||||||
// You can use all the types from my_protocol as if they went from `wayland_client::protocol`.
|
|
||||||
pub use generated::wl_drm;
|
|
||||||
|
|
||||||
#[allow(non_upper_case_globals, non_camel_case_types)]
|
|
||||||
mod generated {
|
|
||||||
use smithay::reexports::wayland_server::{self, protocol::*};
|
|
||||||
|
|
||||||
pub mod __interfaces {
|
|
||||||
use smithay::reexports::wayland_server::protocol::__interfaces::*;
|
|
||||||
wayland_scanner::generate_interfaces!("src/wayland/wayland-drm.xml");
|
|
||||||
}
|
|
||||||
use self::__interfaces::*;
|
|
||||||
|
|
||||||
wayland_scanner::generate_server_code!("src/wayland/wayland-drm.xml");
|
|
||||||
}
|
|
||||||
|
|
||||||
use super::state::WaylandState;
|
|
||||||
use smithay::{
|
|
||||||
backend::allocator::{
|
|
||||||
dmabuf::{Dmabuf, DmabufFlags},
|
|
||||||
Fourcc, Modifier,
|
|
||||||
},
|
|
||||||
reexports::wayland_server::{
|
|
||||||
Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
impl GlobalDispatch<wl_drm::WlDrm, (), WaylandState> for WaylandState {
|
|
||||||
fn bind(
|
|
||||||
state: &mut WaylandState,
|
|
||||||
_dh: &DisplayHandle,
|
|
||||||
_client: &Client,
|
|
||||||
resource: New<wl_drm::WlDrm>,
|
|
||||||
_global_data: &(),
|
|
||||||
data_init: &mut DataInit<'_, WaylandState>,
|
|
||||||
) {
|
|
||||||
let drm_instance = data_init.init(resource, ());
|
|
||||||
|
|
||||||
drm_instance.device("/dev/dri/renderD128".to_string());
|
|
||||||
if drm_instance.version() >= 2 {
|
|
||||||
drm_instance.capabilities(wl_drm::Capability::Prime as u32);
|
|
||||||
}
|
|
||||||
for format in state.drm_formats.iter() {
|
|
||||||
if let Ok(converted) = wl_drm::Format::try_from(*format as u32) {
|
|
||||||
drm_instance.format(converted as u32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn can_view(_client: Client, _global_dataa: &()) -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Dispatch<wl_drm::WlDrm, (), WaylandState> for WaylandState {
|
|
||||||
fn request(
|
|
||||||
state: &mut WaylandState,
|
|
||||||
_client: &Client,
|
|
||||||
drm: &wl_drm::WlDrm,
|
|
||||||
request: wl_drm::Request,
|
|
||||||
_data: &(),
|
|
||||||
_dh: &DisplayHandle,
|
|
||||||
data_init: &mut DataInit<'_, WaylandState>,
|
|
||||||
) {
|
|
||||||
match request {
|
|
||||||
wl_drm::Request::Authenticate { .. } => drm.authenticated(),
|
|
||||||
wl_drm::Request::CreateBuffer { .. } => drm.post_error(
|
|
||||||
wl_drm::Error::InvalidName,
|
|
||||||
String::from("Flink handles are unsupported, use PRIME"),
|
|
||||||
),
|
|
||||||
wl_drm::Request::CreatePlanarBuffer { .. } => drm.post_error(
|
|
||||||
wl_drm::Error::InvalidName,
|
|
||||||
String::from("Flink handles are unsupported, use PRIME"),
|
|
||||||
),
|
|
||||||
wl_drm::Request::CreatePrimeBuffer {
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
format,
|
|
||||||
offset0,
|
|
||||||
stride0,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let format = match Fourcc::try_from(format) {
|
|
||||||
Ok(format) => {
|
|
||||||
if !state.drm_formats.contains(&format) {
|
|
||||||
drm.post_error(
|
|
||||||
wl_drm::Error::InvalidFormat,
|
|
||||||
String::from("Format not advertised by wl_drm"),
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
format
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
drm.post_error(
|
|
||||||
wl_drm::Error::InvalidFormat,
|
|
||||||
String::from("Format unknown / not advertised by wl_drm"),
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if width < 1 || height < 1 {
|
|
||||||
drm.post_error(
|
|
||||||
wl_drm::Error::InvalidFormat,
|
|
||||||
String::from("width or height not positive"),
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut dma = Dmabuf::builder(
|
|
||||||
(width, height),
|
|
||||||
format,
|
|
||||||
Modifier::Invalid,
|
|
||||||
DmabufFlags::empty(),
|
|
||||||
);
|
|
||||||
dma.add_plane(name, 0, offset0 as u32, stride0 as u32);
|
|
||||||
match dma.build() {
|
|
||||||
Some(dmabuf) => {
|
|
||||||
state.dmabuf_tx.send((dmabuf.clone(), None)).unwrap();
|
|
||||||
data_init.init(id, dmabuf);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// Buffer import failed. The protocol documentation heavily implies killing the
|
|
||||||
// client is the right thing to do here.
|
|
||||||
drm.post_error(
|
|
||||||
wl_drm::Error::InvalidName,
|
|
||||||
"dmabuf global was destroyed on server",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,34 +5,32 @@ mod seat;
|
|||||||
mod state;
|
mod state;
|
||||||
mod surface;
|
mod surface;
|
||||||
// mod xdg_activation;
|
// mod xdg_activation;
|
||||||
mod drm;
|
|
||||||
mod utils;
|
|
||||||
mod xdg_shell;
|
mod xdg_shell;
|
||||||
|
#[cfg(feature = "xwayland")]
|
||||||
|
pub mod xwayland;
|
||||||
|
|
||||||
use self::{state::WaylandState, surface::CORE_SURFACES};
|
use self::{state::WaylandState, surface::CORE_SURFACES};
|
||||||
|
use crate::core::buffers::BufferManager;
|
||||||
|
use crate::wayland::seat::SeatData;
|
||||||
use crate::{core::task, wayland::state::ClientState};
|
use crate::{core::task, wayland::state::ClientState};
|
||||||
use color_eyre::eyre::{ensure, Result};
|
use color_eyre::eyre::Result;
|
||||||
|
use global_counter::primitive::exact::CounterU32;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use sk::StereoKitDraw;
|
||||||
use smithay::backend::allocator::dmabuf::Dmabuf;
|
use smithay::backend::allocator::dmabuf::Dmabuf;
|
||||||
use smithay::backend::egl::EGLContext;
|
use smithay::backend::renderer::ImportDma;
|
||||||
use smithay::backend::renderer::gles::GlesRenderer;
|
|
||||||
use smithay::backend::renderer::{ImportDma, Renderer};
|
|
||||||
use smithay::output::Output;
|
|
||||||
use smithay::reexports::wayland_server::backend::ClientId;
|
use smithay::reexports::wayland_server::backend::ClientId;
|
||||||
use smithay::reexports::wayland_server::DisplayHandle;
|
use smithay::reexports::wayland_server::DisplayHandle;
|
||||||
use smithay::reexports::wayland_server::{Display, ListeningSocket};
|
use smithay::reexports::wayland_server::{Display, ListeningSocket};
|
||||||
use smithay::wayland::dmabuf;
|
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::os::fd::{IntoRawFd, OwnedFd};
|
use std::os::fd::OwnedFd;
|
||||||
use std::os::unix::prelude::AsRawFd;
|
use std::os::unix::prelude::AsRawFd;
|
||||||
use std::{
|
use std::{
|
||||||
ffi::c_void,
|
|
||||||
os::unix::{net::UnixListener, prelude::FromRawFd},
|
os::unix::{net::UnixListener, prelude::FromRawFd},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
use stereokit_rust::system::{Backend, BackendGraphics};
|
use stereokit as sk;
|
||||||
use tokio::io::unix::AsyncFdReadyGuard;
|
|
||||||
use tokio::sync::mpsc::UnboundedReceiver;
|
use tokio::sync::mpsc::UnboundedReceiver;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::unix::AsyncFd, net::UnixListener as AsyncUnixListener, sync::mpsc, task::JoinHandle,
|
io::unix::AsyncFd, net::UnixListener as AsyncUnixListener, sync::mpsc, task::JoinHandle,
|
||||||
@@ -40,26 +38,7 @@ use tokio::{
|
|||||||
use tracing::{debug_span, info, instrument};
|
use tracing::{debug_span, info, instrument};
|
||||||
|
|
||||||
pub static WAYLAND_DISPLAY: OnceCell<String> = OnceCell::new();
|
pub static WAYLAND_DISPLAY: OnceCell<String> = OnceCell::new();
|
||||||
|
pub static SERIAL_COUNTER: CounterU32 = CounterU32::new(0);
|
||||||
struct EGLRawHandles {
|
|
||||||
display: *const c_void,
|
|
||||||
config: *const c_void,
|
|
||||||
context: *const c_void,
|
|
||||||
}
|
|
||||||
fn get_sk_egl() -> Result<EGLRawHandles> {
|
|
||||||
ensure!(
|
|
||||||
Backend::graphics() == BackendGraphics::OpenGLESEGL,
|
|
||||||
"StereoKit is not running using EGL!"
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(unsafe {
|
|
||||||
EGLRawHandles {
|
|
||||||
display: stereokit_rust::system::backend_opengl_egl_get_display() as *const c_void,
|
|
||||||
config: stereokit_rust::system::backend_opengl_egl_get_config() as *const c_void,
|
|
||||||
context: stereokit_rust::system::backend_opengl_egl_get_context() as *const c_void,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DisplayWrapper(Mutex<Display<WaylandState>>, DisplayHandle);
|
pub struct DisplayWrapper(Mutex<Display<WaylandState>>, DisplayHandle);
|
||||||
impl DisplayWrapper {
|
impl DisplayWrapper {
|
||||||
@@ -79,45 +58,25 @@ impl DisplayWrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UnownedFd(Option<AsyncFd<OwnedFd>>);
|
|
||||||
impl UnownedFd {
|
|
||||||
async fn readable(&self) -> std::io::Result<AsyncFdReadyGuard<'_, OwnedFd>> {
|
|
||||||
self.0.as_ref().unwrap().readable().await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for UnownedFd {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.0.take().unwrap().into_inner().into_raw_fd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Wayland {
|
pub struct Wayland {
|
||||||
display: Arc<DisplayWrapper>,
|
display: Arc<DisplayWrapper>,
|
||||||
pub socket_name: Option<String>,
|
pub socket_name: Option<String>,
|
||||||
join_handle: JoinHandle<Result<()>>,
|
join_handle: JoinHandle<Result<()>>,
|
||||||
renderer: GlesRenderer,
|
dmabuf_rx: UnboundedReceiver<Dmabuf>,
|
||||||
output: Output,
|
wayland_state: Arc<Mutex<WaylandState>>,
|
||||||
dmabuf_rx: UnboundedReceiver<(Dmabuf, Option<dmabuf::ImportNotifier>)>,
|
#[cfg(feature = "xwayland")]
|
||||||
|
pub xwayland_state: xwayland::XWaylandState,
|
||||||
}
|
}
|
||||||
impl Wayland {
|
impl Wayland {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new(buffer_manager: &BufferManager) -> Result<Self> {
|
||||||
let egl_raw_handles = get_sk_egl()?;
|
|
||||||
let renderer = unsafe {
|
|
||||||
GlesRenderer::new(EGLContext::from_raw(
|
|
||||||
egl_raw_handles.display,
|
|
||||||
egl_raw_handles.config,
|
|
||||||
egl_raw_handles.context,
|
|
||||||
)?)?
|
|
||||||
};
|
|
||||||
|
|
||||||
let display: Display<WaylandState> = Display::new()?;
|
let display: Display<WaylandState> = Display::new()?;
|
||||||
let display_handle = display.handle();
|
let display_handle = display.handle();
|
||||||
|
|
||||||
let (dmabuf_tx, dmabuf_rx) = mpsc::unbounded_channel();
|
let (dmabuf_tx, dmabuf_rx) = mpsc::unbounded_channel();
|
||||||
let display = Arc::new(DisplayWrapper(Mutex::new(display), display_handle.clone()));
|
let display = Arc::new(DisplayWrapper(Mutex::new(display), display_handle.clone()));
|
||||||
|
#[cfg(feature = "xwayland")]
|
||||||
let wayland_state = WaylandState::new(display_handle.clone(), &renderer, dmabuf_tx);
|
let xwayland_state = xwayland::XWaylandState::create(&display_handle)?;
|
||||||
let output = wayland_state.lock().output.clone();
|
let wayland_state = WaylandState::new(display_handle, &buffer_manager.renderer, dmabuf_tx);
|
||||||
|
|
||||||
let socket = ListeningSocket::bind_auto("wayland", 0..33)?;
|
let socket = ListeningSocket::bind_auto("wayland", 0..33)?;
|
||||||
let socket_name = socket
|
let socket_name = socket
|
||||||
@@ -129,15 +88,16 @@ impl Wayland {
|
|||||||
}
|
}
|
||||||
info!(socket_name, "Wayland active");
|
info!(socket_name, "Wayland active");
|
||||||
|
|
||||||
let join_handle = Wayland::start_loop(display.clone(), socket, wayland_state)?;
|
let join_handle = Wayland::start_loop(display.clone(), socket, wayland_state.clone())?;
|
||||||
|
|
||||||
Ok(Wayland {
|
Ok(Wayland {
|
||||||
display,
|
display,
|
||||||
socket_name,
|
socket_name,
|
||||||
join_handle,
|
join_handle,
|
||||||
renderer,
|
|
||||||
output,
|
|
||||||
dmabuf_rx,
|
dmabuf_rx,
|
||||||
|
wayland_state,
|
||||||
|
#[cfg(feature = "xwayland")]
|
||||||
|
xwayland_state,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,29 +110,30 @@ impl Wayland {
|
|||||||
AsyncUnixListener::from_std(unsafe { UnixListener::from_raw_fd(socket.as_raw_fd()) })?;
|
AsyncUnixListener::from_std(unsafe { UnixListener::from_raw_fd(socket.as_raw_fd()) })?;
|
||||||
|
|
||||||
let dispatch_poll_fd = display.poll_fd()?;
|
let dispatch_poll_fd = display.poll_fd()?;
|
||||||
let dispatch_poll_listener = UnownedFd(Some(AsyncFd::new(dispatch_poll_fd)?));
|
let dispatch_poll_listener = AsyncFd::new(dispatch_poll_fd)?;
|
||||||
|
|
||||||
let dh1 = display.handle();
|
let dh1 = display.handle();
|
||||||
let mut dh2 = dh1.clone();
|
let mut dh2 = dh1.clone();
|
||||||
|
|
||||||
task::new(|| "wayland loop", async move {
|
Ok(task::new(|| "wayland loop", async move {
|
||||||
let _socket = socket; // Keep the socket alive
|
let _socket = socket; // Keep the socket alive
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
acc = listen_async.accept() => { // New client connected
|
acc = listen_async.accept() => { // New client connected
|
||||||
let (stream, _) = acc?;
|
let (stream, _) = acc?;
|
||||||
let client_state = Arc::new(ClientState {
|
let client_state = Arc::new(ClientState {
|
||||||
pid: stream.peer_cred().ok().and_then(|c| c.pid()),
|
|
||||||
id: OnceCell::new(),
|
id: OnceCell::new(),
|
||||||
compositor_state: Default::default(),
|
compositor_state: Default::default(),
|
||||||
seat: state.lock().seat.clone(),
|
display: Arc::downgrade(&display),
|
||||||
|
seat: SeatData::new(&dh1)
|
||||||
});
|
});
|
||||||
let _client = dh2.insert_client(stream.into_std()?, client_state.clone())?;
|
let client = dh2.insert_client(stream.into_std()?, client_state.clone())?;
|
||||||
|
let _ = client_state.seat.client.set(client.id());
|
||||||
}
|
}
|
||||||
e = dispatch_poll_listener.readable() => { // Dispatch
|
e = dispatch_poll_listener.readable() => { // Dispatch
|
||||||
let mut guard = e?;
|
let mut guard = e?;
|
||||||
debug_span!("Dispatch wayland event").in_scope(|| -> Result<(), color_eyre::Report> {
|
debug_span!("Dispatch wayland event").in_scope(|| -> Result<(), color_eyre::Report> {
|
||||||
display.dispatch_clients(&mut state.lock())?;
|
display.dispatch_clients(&mut *state.lock())?;
|
||||||
display.flush_clients(None);
|
display.flush_clients(None);
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
@@ -180,35 +141,30 @@ impl Wayland {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "debug", name = "Wayland frame", skip(self))]
|
#[instrument(
|
||||||
pub fn update(&mut self) {
|
level = "debug",
|
||||||
while let Ok((dmabuf, notifier)) = self.dmabuf_rx.try_recv() {
|
name = "Wayland frame",
|
||||||
if self.renderer.import_dmabuf(&dmabuf, None).is_err() {
|
skip(self, sk, buffer_manager)
|
||||||
if let Some(notifier) = notifier {
|
)]
|
||||||
notifier.failed();
|
pub fn update(&mut self, sk: &impl StereoKitDraw, buffer_manager: &mut BufferManager) {
|
||||||
}
|
while let Ok(dmabuf) = self.dmabuf_rx.try_recv() {
|
||||||
}
|
let _ = buffer_manager.renderer.import_dmabuf(&dmabuf, None);
|
||||||
}
|
}
|
||||||
for core_surface in CORE_SURFACES.get_valid_contents() {
|
for core_surface in CORE_SURFACES.get_valid_contents() {
|
||||||
core_surface.process(&mut self.renderer);
|
core_surface.process(sk, &mut buffer_manager.renderer);
|
||||||
}
|
}
|
||||||
let _ = self.renderer.cleanup_texture_cache();
|
|
||||||
|
|
||||||
self.display.flush_clients(None);
|
self.display.flush_clients(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame_event(&self) {
|
pub fn frame_event(&self, sk: &impl StereoKitDraw) {
|
||||||
for core_surface in CORE_SURFACES.get_valid_contents() {
|
let output = self.wayland_state.lock().output.clone();
|
||||||
core_surface.frame(self.output.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn make_context_current(&self) {
|
for core_surface in CORE_SURFACES.get_valid_contents() {
|
||||||
unsafe {
|
core_surface.frame(sk, output.clone());
|
||||||
let _ = self.renderer.egl_context().make_current();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,82 +1,471 @@
|
|||||||
use super::{state::WaylandState, surface::CoreSurface, utils::WlSurfaceExt};
|
use super::{
|
||||||
|
state::{ClientState, WaylandState},
|
||||||
|
surface::CoreSurface,
|
||||||
|
SERIAL_COUNTER,
|
||||||
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::task,
|
core::task,
|
||||||
nodes::{
|
nodes::items::panel::{Backend, Geometry, PanelItem},
|
||||||
data::KEYMAPS,
|
|
||||||
items::panel::{Backend, Geometry, PanelItem},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
use color_eyre::eyre::{bail, eyre, Result};
|
||||||
use mint::Vector2;
|
use mint::Vector2;
|
||||||
|
use nanoid::nanoid;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rand::{seq::IteratorRandom, thread_rng};
|
||||||
use slotmap::KeyData;
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use smithay::{
|
use smithay::{
|
||||||
backend::input::{AxisRelativeDirection, ButtonState, KeyState},
|
input::keyboard::{KeymapFile, ModifiersState},
|
||||||
delegate_seat,
|
reexports::wayland_server::{
|
||||||
input::{
|
backend::{ClientId, GlobalId, ObjectId},
|
||||||
keyboard::{FilterResult, LedState},
|
protocol::{
|
||||||
pointer::{AxisFrame, ButtonEvent, CursorImageStatus, MotionEvent},
|
wl_keyboard::{self, KeyState, WlKeyboard},
|
||||||
touch::{self, DownEvent, UpEvent},
|
wl_pointer::{self, Axis, ButtonState, WlPointer},
|
||||||
Seat, SeatHandler,
|
wl_seat::{self, Capability, WlSeat, EVT_NAME_SINCE},
|
||||||
|
wl_surface::WlSurface,
|
||||||
|
wl_touch::{self, WlTouch},
|
||||||
|
},
|
||||||
|
Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, Weak as WlWeak,
|
||||||
},
|
},
|
||||||
reexports::wayland_server::{protocol::wl_surface::WlSurface, Resource, Weak as WlWeak},
|
|
||||||
utils::SERIAL_COUNTER,
|
|
||||||
wayland::compositor,
|
wayland::compositor,
|
||||||
};
|
};
|
||||||
use std::sync::{Arc, Weak};
|
use std::{
|
||||||
|
collections::VecDeque,
|
||||||
|
sync::Arc,
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
|
use tracing::{debug, warn};
|
||||||
impl SeatHandler for WaylandState {
|
use xkbcommon::xkb::{self, ffi::XKB_KEYMAP_FORMAT_TEXT_V1, Keycode, Keymap};
|
||||||
type PointerFocus = WlSurface;
|
|
||||||
type KeyboardFocus = WlSurface;
|
|
||||||
type TouchFocus = WlSurface;
|
|
||||||
|
|
||||||
fn seat_state(&mut self) -> &mut smithay::input::SeatState<Self> {
|
|
||||||
&mut self.seat_state
|
|
||||||
}
|
|
||||||
fn focus_changed(&mut self, _seat: &Seat<Self>, _focused: Option<&Self::KeyboardFocus>) {}
|
|
||||||
fn cursor_image(&mut self, _seat: &Seat<Self>, image: CursorImageStatus) {
|
|
||||||
self.seat.cursor_info_tx.send_modify(|c| match image {
|
|
||||||
CursorImageStatus::Hidden => c.surface = None,
|
|
||||||
CursorImageStatus::Surface(surface) => {
|
|
||||||
CoreSurface::add_to(&surface);
|
|
||||||
compositor::with_states(&surface, |data| {
|
|
||||||
if let Some(core_surface) = data.data_map.get::<Arc<CoreSurface>>() {
|
|
||||||
core_surface.set_material_offset(1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
c.surface = Some(surface.downgrade())
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
fn led_state_changed(&mut self, _seat: &Seat<Self>, _led_state: LedState) {}
|
|
||||||
}
|
|
||||||
delegate_seat!(WaylandState);
|
|
||||||
|
|
||||||
pub fn handle_cursor<B: Backend>(
|
pub fn handle_cursor<B: Backend>(
|
||||||
panel_item: &Arc<PanelItem<B>>,
|
panel_item: &Arc<PanelItem<B>>,
|
||||||
mut cursor: watch::Receiver<CursorInfo>,
|
mut cursor: watch::Receiver<Option<CursorInfo>>,
|
||||||
) {
|
) {
|
||||||
let panel_item_weak = Arc::downgrade(panel_item);
|
let panel_item_weak = Arc::downgrade(panel_item);
|
||||||
let _ = task::new(|| "cursor handler", async move {
|
let _ = task::new(|| "cursor handler", async move {
|
||||||
while cursor.changed().await.is_ok() {
|
while cursor.changed().await.is_ok() {
|
||||||
let Some(panel_item) = panel_item_weak.upgrade() else {
|
let Some(panel_item) = panel_item_weak.upgrade() else {continue};
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let cursor_info = cursor.borrow();
|
let cursor_info = cursor.borrow();
|
||||||
panel_item.set_cursor(cursor_info.cursor_data());
|
panel_item.set_cursor(cursor_info.as_ref().and_then(CursorInfo::cursor_data));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct KeyboardInfo {
|
||||||
|
keymap_string: String,
|
||||||
|
keymap: KeymapFile,
|
||||||
|
state: xkb::State,
|
||||||
|
mods: ModifiersState,
|
||||||
|
keys: FxHashSet<u32>,
|
||||||
|
}
|
||||||
|
impl KeyboardInfo {
|
||||||
|
pub fn new(keymap_string: String, keymap: &Keymap) -> Self {
|
||||||
|
KeyboardInfo {
|
||||||
|
keymap_string,
|
||||||
|
state: xkb::State::new(keymap),
|
||||||
|
keymap: KeymapFile::new(keymap),
|
||||||
|
mods: ModifiersState::default(),
|
||||||
|
keys: FxHashSet::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn process(&mut self, key: u32, pressed: bool, keyboard: &WlKeyboard) -> Result<usize> {
|
||||||
|
let xkb_key_state = if pressed {
|
||||||
|
xkb::KeyDirection::Down
|
||||||
|
} else {
|
||||||
|
xkb::KeyDirection::Up
|
||||||
|
};
|
||||||
|
let state_components = self.state.update_key(Keycode::new(key + 8), xkb_key_state);
|
||||||
|
if state_components != 0 {
|
||||||
|
self.mods.update_with(&self.state);
|
||||||
|
keyboard.modifiers(
|
||||||
|
0,
|
||||||
|
self.mods.serialized.depressed,
|
||||||
|
self.mods.serialized.latched,
|
||||||
|
self.mods.serialized.locked,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// if pressed {
|
||||||
|
// println!("Key {key} is being pressed with {state_components} modifiers");
|
||||||
|
// } else {
|
||||||
|
// println!("Key {key} is being released with {state_components} modifiers");
|
||||||
|
// }
|
||||||
|
|
||||||
|
let wl_key_state = if pressed {
|
||||||
|
KeyState::Pressed
|
||||||
|
} else {
|
||||||
|
KeyState::Released
|
||||||
|
};
|
||||||
|
keyboard.key(SERIAL_COUNTER.inc(), 0, key, wl_key_state);
|
||||||
|
match wl_key_state {
|
||||||
|
KeyState::Pressed => {
|
||||||
|
self.keys.insert(key);
|
||||||
|
}
|
||||||
|
KeyState::Released => {
|
||||||
|
self.keys.remove(&key);
|
||||||
|
}
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
Ok(self.keys.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsafe impl Send for KeyboardInfo {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum PointerEvent {
|
||||||
|
Motion(Vector2<f32>),
|
||||||
|
Button {
|
||||||
|
button: u32,
|
||||||
|
state: u32,
|
||||||
|
},
|
||||||
|
Scroll {
|
||||||
|
axis_continuous: Option<Vector2<f32>>,
|
||||||
|
axis_discrete: Option<Vector2<f32>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum KeyboardEvent {
|
||||||
|
Keymap,
|
||||||
|
Key { key: u32, state: bool },
|
||||||
|
}
|
||||||
|
|
||||||
|
const POINTER_EVENT_TIMEOUT: Duration = Duration::from_millis(50);
|
||||||
|
struct SurfaceInfo {
|
||||||
|
wl_surface: WlWeak<WlSurface>,
|
||||||
|
cursor_sender: watch::Sender<Option<CursorInfo>>,
|
||||||
|
pointer_queue: VecDeque<PointerEvent>,
|
||||||
|
pointer_latest_event: Instant,
|
||||||
|
keyboard_queue: VecDeque<KeyboardEvent>,
|
||||||
|
keyboard_info: Option<KeyboardInfo>,
|
||||||
|
}
|
||||||
|
impl SurfaceInfo {
|
||||||
|
fn new(wl_surface: &WlSurface, cursor_sender: watch::Sender<Option<CursorInfo>>) -> Self {
|
||||||
|
SurfaceInfo {
|
||||||
|
wl_surface: wl_surface.downgrade(),
|
||||||
|
cursor_sender,
|
||||||
|
pointer_queue: VecDeque::new(),
|
||||||
|
pointer_latest_event: Instant::now(),
|
||||||
|
keyboard_queue: VecDeque::new(),
|
||||||
|
keyboard_info: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn flush(&self) {
|
||||||
|
if let Some(client) = self.wl_surface.upgrade().ok().and_then(|s| s.client()) {
|
||||||
|
if let Some(client_data) = client.get_data::<ClientState>() {
|
||||||
|
client_data.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn handle_pointer_events(&mut self, pointer: &WlPointer, mut locked: bool) -> bool {
|
||||||
|
let Ok(focus) = self.wl_surface.upgrade() else { return false; };
|
||||||
|
let Some(core_surface) = CoreSurface::from_wl_surface(&focus) else { return false; };
|
||||||
|
let Some(focus_size) = core_surface.size() else { return false; };
|
||||||
|
|
||||||
|
if !self.pointer_queue.is_empty() {
|
||||||
|
self.pointer_latest_event = Instant::now();
|
||||||
|
}
|
||||||
|
while let Some(event) = self.pointer_queue.pop_front() {
|
||||||
|
match (locked, event) {
|
||||||
|
(false, PointerEvent::Motion(pos)) => {
|
||||||
|
pointer.enter(
|
||||||
|
SERIAL_COUNTER.inc(),
|
||||||
|
&focus,
|
||||||
|
(pos.x as f64).clamp(0.0, focus_size.x as f64),
|
||||||
|
(pos.y as f64).clamp(0.0, focus_size.y as f64),
|
||||||
|
);
|
||||||
|
locked = true;
|
||||||
|
}
|
||||||
|
(true, PointerEvent::Motion(pos)) => {
|
||||||
|
pointer.motion(
|
||||||
|
0,
|
||||||
|
(pos.x as f64).clamp(0.0, focus_size.x as f64),
|
||||||
|
(pos.y as f64).clamp(0.0, focus_size.y as f64),
|
||||||
|
);
|
||||||
|
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
|
||||||
|
pointer.frame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(true, PointerEvent::Button { button, state }) => {
|
||||||
|
pointer.button(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
button,
|
||||||
|
match state {
|
||||||
|
0 => ButtonState::Released,
|
||||||
|
1 => ButtonState::Pressed,
|
||||||
|
_ => continue,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
|
||||||
|
pointer.frame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(
|
||||||
|
true,
|
||||||
|
PointerEvent::Scroll {
|
||||||
|
axis_continuous,
|
||||||
|
axis_discrete,
|
||||||
|
},
|
||||||
|
) => {
|
||||||
|
if let Some(axis_continuous) = axis_continuous {
|
||||||
|
pointer.axis(0, Axis::HorizontalScroll, axis_continuous.x as f64);
|
||||||
|
pointer.axis(0, Axis::VerticalScroll, -axis_continuous.y as f64);
|
||||||
|
}
|
||||||
|
if pointer.version() >= wl_pointer::EVT_AXIS_DISCRETE_SINCE {
|
||||||
|
if let Some(axis_discrete) = axis_discrete {
|
||||||
|
pointer.axis_discrete(Axis::HorizontalScroll, axis_discrete.x as i32);
|
||||||
|
pointer.axis_discrete(Axis::VerticalScroll, -axis_discrete.y as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pointer.version() >= wl_pointer::EVT_AXIS_STOP_SINCE
|
||||||
|
&& axis_discrete.is_none()
|
||||||
|
&& axis_continuous.is_none()
|
||||||
|
{
|
||||||
|
pointer.axis_stop(0, Axis::HorizontalScroll);
|
||||||
|
pointer.axis_stop(0, Axis::VerticalScroll);
|
||||||
|
}
|
||||||
|
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
|
||||||
|
pointer.frame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(locked, event) => {
|
||||||
|
warn!(locked, ?event, "Invalid pointer event!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.pointer_latest_event.elapsed() > POINTER_EVENT_TIMEOUT {
|
||||||
|
pointer.leave(SERIAL_COUNTER.inc(), &focus);
|
||||||
|
locked = false;
|
||||||
|
}
|
||||||
|
self.flush();
|
||||||
|
|
||||||
|
locked
|
||||||
|
}
|
||||||
|
fn handle_keyboard_events(&mut self, keyboard: &WlKeyboard, mut locked: bool) -> bool {
|
||||||
|
let Ok(focus) = self.wl_surface.upgrade() else { return false; };
|
||||||
|
let Some(info) = self.keyboard_info.as_mut() else { return true; };
|
||||||
|
|
||||||
|
if !locked {
|
||||||
|
keyboard.enter(0, &focus, vec![]);
|
||||||
|
if keyboard.version() >= wl_keyboard::EVT_REPEAT_INFO_SINCE {
|
||||||
|
keyboard.repeat_info(0, 0);
|
||||||
|
}
|
||||||
|
locked = info.keymap.send(keyboard).is_ok();
|
||||||
|
}
|
||||||
|
while let Some(event) = self.keyboard_queue.pop_front() {
|
||||||
|
debug!(locked, ?event, "Process keyboard event");
|
||||||
|
match (locked, event) {
|
||||||
|
(true, KeyboardEvent::Keymap) => {
|
||||||
|
let _ = info.keymap.send(keyboard);
|
||||||
|
}
|
||||||
|
(true, KeyboardEvent::Key { key, state }) => {
|
||||||
|
if let Ok(key_count) = info.process(key, state, keyboard) {
|
||||||
|
if key_count == 0 {
|
||||||
|
keyboard.leave(SERIAL_COUNTER.inc(), &focus);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(locked, event) => {
|
||||||
|
warn!(locked, ?event, "Invalid keyboard event!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.flush();
|
||||||
|
locked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SeatData {
|
||||||
|
pub client: OnceCell<ClientId>,
|
||||||
|
global_id: OnceCell<GlobalId>,
|
||||||
|
surfaces: Mutex<FxHashMap<ObjectId, SurfaceInfo>>,
|
||||||
|
pointer: OnceCell<(WlPointer, Mutex<ObjectId>)>,
|
||||||
|
keyboard: OnceCell<(WlKeyboard, Mutex<ObjectId>)>,
|
||||||
|
touch: OnceCell<WlTouch>,
|
||||||
|
touches: Mutex<FxHashMap<ObjectId, u32>>,
|
||||||
|
}
|
||||||
|
impl SeatData {
|
||||||
|
pub fn new(dh: &DisplayHandle) -> Arc<Self> {
|
||||||
|
let seat_data = Arc::new(SeatData {
|
||||||
|
client: OnceCell::new(),
|
||||||
|
global_id: OnceCell::new(),
|
||||||
|
surfaces: Mutex::new(FxHashMap::default()),
|
||||||
|
pointer: OnceCell::new(),
|
||||||
|
keyboard: OnceCell::new(),
|
||||||
|
touch: OnceCell::new(),
|
||||||
|
touches: Mutex::new(FxHashMap::default()),
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = seat_data
|
||||||
|
.global_id
|
||||||
|
.set(dh.create_global::<WaylandState, _, _>(7, seat_data.clone()));
|
||||||
|
|
||||||
|
seat_data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_keymap(&self, keymap_str: String, surfaces: Vec<WlSurface>) -> Result<()> {
|
||||||
|
let context = xkb::Context::new(0);
|
||||||
|
let keymap =
|
||||||
|
Keymap::new_from_string(&context, keymap_str.clone(), XKB_KEYMAP_FORMAT_TEXT_V1, 0)
|
||||||
|
.ok_or_else(|| eyre!("Keymap is not valid"))?;
|
||||||
|
let mut panels = self.surfaces.lock();
|
||||||
|
let Some((_, focus)) = self.keyboard.get() else {bail!("Could not get keyboard")};
|
||||||
|
for surface in surfaces {
|
||||||
|
let Some(surface_info) = panels.get_mut(&surface.id()) else {continue};
|
||||||
|
if let Some(keyboard_info) = &mut surface_info.keyboard_info {
|
||||||
|
if &keyboard_info.keymap_string == &keymap_str {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
surface_info
|
||||||
|
.keyboard_info
|
||||||
|
.replace(KeyboardInfo::new(keymap_str.clone(), &keymap));
|
||||||
|
|
||||||
|
if *focus.lock() == surface.id() {
|
||||||
|
surface_info.keyboard_queue.push_back(KeyboardEvent::Keymap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pointer_event(&self, surface: &WlSurface, event: PointerEvent) {
|
||||||
|
let mut surfaces = self.surfaces.lock();
|
||||||
|
let Some(surface_info) = surfaces.get_mut(&surface.id()) else {return};
|
||||||
|
surface_info.pointer_queue.push_back(event);
|
||||||
|
drop(surfaces);
|
||||||
|
self.handle_pointer_events();
|
||||||
|
}
|
||||||
|
pub fn keyboard_event(&self, surface: &WlSurface, event: KeyboardEvent) {
|
||||||
|
let mut surfaces = self.surfaces.lock();
|
||||||
|
let Some(surface_info) = surfaces.get_mut(&surface.id()) else {return};
|
||||||
|
surface_info.keyboard_queue.push_back(event);
|
||||||
|
drop(surfaces);
|
||||||
|
self.handle_keyboard_events();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_pointer_events(&self) {
|
||||||
|
let mut surfaces = self.surfaces.lock();
|
||||||
|
let Some((pointer, pointer_focus)) = self.pointer.get() else {return};
|
||||||
|
let mut pointer_focus = pointer_focus.lock();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let locked = !pointer_focus.is_null();
|
||||||
|
// Pick a pointer to focus on if there is none
|
||||||
|
if pointer_focus.is_null() {
|
||||||
|
*pointer_focus = surfaces
|
||||||
|
.iter()
|
||||||
|
.filter(|(_k, v)| !v.pointer_queue.is_empty())
|
||||||
|
.map(|(k, _v)| k)
|
||||||
|
.choose(&mut thread_rng())
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(ObjectId::null());
|
||||||
|
}
|
||||||
|
if pointer_focus.is_null() {
|
||||||
|
// If there's still none, guess we're done with pointer events for the time being
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let Some(surface_info) = surfaces.get_mut(&pointer_focus) else {break};
|
||||||
|
if surface_info.handle_pointer_events(pointer, locked) {
|
||||||
|
// We haven't gotten to a point where we can switch the focus
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
*pointer_focus = ObjectId::null();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn handle_keyboard_events(&self) {
|
||||||
|
let mut surfaces = self.surfaces.lock();
|
||||||
|
let Some((keyboard, keyboard_focus)) = self.keyboard.get() else {return};
|
||||||
|
let mut keyboard_focus = keyboard_focus.lock();
|
||||||
|
loop {
|
||||||
|
let locked = !keyboard_focus.is_null();
|
||||||
|
// Pick a keyboard to focus on if there is none
|
||||||
|
if keyboard_focus.is_null() {
|
||||||
|
*keyboard_focus = surfaces
|
||||||
|
.iter()
|
||||||
|
.filter(|(_k, v)| v.keyboard_info.is_some())
|
||||||
|
.filter(|(_k, v)| !v.keyboard_queue.is_empty())
|
||||||
|
.map(|(k, _v)| k)
|
||||||
|
.choose(&mut thread_rng())
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(ObjectId::null());
|
||||||
|
}
|
||||||
|
// If there's still none, guess we're done with keyboard events for the time being
|
||||||
|
let Some(surface_info) = surfaces.get_mut(&keyboard_focus) else {break};
|
||||||
|
if surface_info.handle_keyboard_events(keyboard, locked) {
|
||||||
|
// We haven't gotten to a point where we can switch the focus
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
*keyboard_focus = ObjectId::null();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_surface(&self, surface: &WlSurface) -> watch::Receiver<Option<CursorInfo>> {
|
||||||
|
let (tx, rx) = watch::channel(None);
|
||||||
|
self.surfaces
|
||||||
|
.lock()
|
||||||
|
.insert(surface.id(), SurfaceInfo::new(surface, tx));
|
||||||
|
|
||||||
|
rx
|
||||||
|
}
|
||||||
|
pub fn drop_surface(&self, surface: &WlSurface) {
|
||||||
|
self.surfaces.lock().remove(&surface.id());
|
||||||
|
if let Some((_, pointer_focus)) = self.pointer.get() {
|
||||||
|
let mut pointer_focus = pointer_focus.lock();
|
||||||
|
if *pointer_focus == surface.id() {
|
||||||
|
*pointer_focus = ObjectId::null();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some((_, keyboard_focus)) = self.keyboard.get() {
|
||||||
|
let mut keyboard_focus = keyboard_focus.lock();
|
||||||
|
if *keyboard_focus == surface.id() {
|
||||||
|
*keyboard_focus = ObjectId::null();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.touches.lock().remove(&surface.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn touch_down(&self, surface: &WlSurface, id: u32, position: Vector2<f32>) {
|
||||||
|
let Some(touch) = self.touch.get() else {return};
|
||||||
|
touch.down(
|
||||||
|
SERIAL_COUNTER.inc(),
|
||||||
|
0,
|
||||||
|
surface,
|
||||||
|
id as i32,
|
||||||
|
position.x as f64,
|
||||||
|
position.y as f64,
|
||||||
|
);
|
||||||
|
self.touches.lock().insert(surface.id(), id);
|
||||||
|
}
|
||||||
|
pub fn touch_move(&self, id: u32, position: Vector2<f32>) {
|
||||||
|
let Some(touch) = self.touch.get() else {return};
|
||||||
|
touch.motion(0, id as i32, position.x as f64, position.y as f64);
|
||||||
|
}
|
||||||
|
pub fn touch_up(&self, id: u32) {
|
||||||
|
let Some(touch) = self.touch.get() else {return};
|
||||||
|
touch.up(SERIAL_COUNTER.inc(), 0, id as i32);
|
||||||
|
let mut touches = self.touches.lock();
|
||||||
|
touches.retain(|_, tid| *tid != id);
|
||||||
|
}
|
||||||
|
pub fn reset_touches(&self) {
|
||||||
|
let Some(touch) = self.touch.get() else {return};
|
||||||
|
for (_, touch_id) in self.touches.lock().drain() {
|
||||||
|
touch.up(SERIAL_COUNTER.inc(), 0, touch_id as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CursorInfo {
|
pub struct CursorInfo {
|
||||||
pub surface: Option<WlWeak<WlSurface>>,
|
pub surface: WlWeak<WlSurface>,
|
||||||
pub hotspot_x: i32,
|
pub hotspot_x: i32,
|
||||||
pub hotspot_y: i32,
|
pub hotspot_y: i32,
|
||||||
}
|
}
|
||||||
impl CursorInfo {
|
impl CursorInfo {
|
||||||
pub fn cursor_data(&self) -> Option<Geometry> {
|
pub fn cursor_data(&self) -> Option<Geometry> {
|
||||||
let cursor_size = self.surface.as_ref()?.upgrade().ok()?.get_size()?;
|
let cursor_size = CoreSurface::from_wl_surface(&self.surface.upgrade().ok()?)?.size()?;
|
||||||
Some(Geometry {
|
Some(Geometry {
|
||||||
origin: [self.hotspot_x, self.hotspot_y].into(),
|
origin: [self.hotspot_x, self.hotspot_y].into(),
|
||||||
size: cursor_size,
|
size: cursor_size,
|
||||||
@@ -84,224 +473,134 @@ impl CursorInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SeatWrapper {
|
impl GlobalDispatch<WlSeat, Arc<SeatData>, WaylandState> for WaylandState {
|
||||||
wayland_state: Weak<Mutex<WaylandState>>,
|
fn bind(
|
||||||
cursor_info_tx: watch::Sender<CursorInfo>,
|
_state: &mut WaylandState,
|
||||||
pub cursor_info_rx: watch::Receiver<CursorInfo>,
|
_handle: &DisplayHandle,
|
||||||
seat: Seat<WaylandState>,
|
_client: &Client,
|
||||||
touches: Mutex<FxHashMap<u32, WlWeak<WlSurface>>>,
|
resource: New<WlSeat>,
|
||||||
}
|
data: &Arc<SeatData>,
|
||||||
impl SeatWrapper {
|
data_init: &mut DataInit<'_, WaylandState>,
|
||||||
pub fn new(wayland_state: Weak<Mutex<WaylandState>>, seat: Seat<WaylandState>) -> Self {
|
|
||||||
let (cursor_info_tx, cursor_info_rx) = watch::channel(CursorInfo {
|
|
||||||
surface: None,
|
|
||||||
hotspot_x: 0,
|
|
||||||
hotspot_y: 0,
|
|
||||||
});
|
|
||||||
SeatWrapper {
|
|
||||||
wayland_state,
|
|
||||||
cursor_info_tx,
|
|
||||||
cursor_info_rx,
|
|
||||||
seat,
|
|
||||||
touches: Mutex::new(FxHashMap::default()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn unfocus(&self, surface: &WlSurface, state: &mut WaylandState) {
|
|
||||||
let pointer = self.seat.get_pointer().unwrap();
|
|
||||||
if pointer.current_focus() == Some(surface.clone()) {
|
|
||||||
pointer.motion(
|
|
||||||
state,
|
|
||||||
None,
|
|
||||||
&MotionEvent {
|
|
||||||
location: (0.0, 0.0).into(),
|
|
||||||
serial: SERIAL_COUNTER.next_serial(),
|
|
||||||
time: 0,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
let keyboard = self.seat.get_keyboard().unwrap();
|
|
||||||
if keyboard.current_focus() == Some(surface.clone()) {
|
|
||||||
keyboard.set_focus(state, None, SERIAL_COUNTER.next_serial());
|
|
||||||
}
|
|
||||||
for (id, touch_surface) in self.touches.lock().iter() {
|
|
||||||
if touch_surface.id() == surface.id() {
|
|
||||||
self.touch_up(*id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pointer_motion(&self, surface: WlSurface, position: Vector2<f32>) {
|
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let mut state = state.lock();
|
|
||||||
let Some(pointer) = self.seat.get_pointer() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
pointer.motion(
|
|
||||||
&mut state,
|
|
||||||
Some((surface, (0.0, 0.0).into())),
|
|
||||||
&MotionEvent {
|
|
||||||
location: (position.x as f64, position.y as f64).into(),
|
|
||||||
serial: SERIAL_COUNTER.next_serial(),
|
|
||||||
time: 0,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pointer.frame(&mut state);
|
|
||||||
}
|
|
||||||
pub fn pointer_button(&self, button: u32, pressed: bool) {
|
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let mut state = state.lock();
|
|
||||||
let Some(pointer) = self.seat.get_pointer() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
pointer.button(
|
|
||||||
&mut state,
|
|
||||||
&ButtonEvent {
|
|
||||||
button,
|
|
||||||
state: if pressed {
|
|
||||||
ButtonState::Pressed
|
|
||||||
} else {
|
|
||||||
ButtonState::Released
|
|
||||||
},
|
|
||||||
serial: SERIAL_COUNTER.next_serial(),
|
|
||||||
time: 0,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pointer.frame(&mut state);
|
|
||||||
}
|
|
||||||
pub fn pointer_scroll(
|
|
||||||
&self,
|
|
||||||
scroll_distance: Option<Vector2<f32>>,
|
|
||||||
scroll_steps: Option<Vector2<f32>>,
|
|
||||||
) {
|
) {
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
let resource = data_init.init(resource, data.clone());
|
||||||
return;
|
|
||||||
};
|
|
||||||
let mut state = state.lock();
|
|
||||||
let Some(pointer) = self.seat.get_pointer() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
pointer.axis(
|
|
||||||
&mut state,
|
|
||||||
AxisFrame {
|
|
||||||
source: None,
|
|
||||||
relative_direction: (
|
|
||||||
AxisRelativeDirection::Identical,
|
|
||||||
AxisRelativeDirection::Identical,
|
|
||||||
),
|
|
||||||
time: 0,
|
|
||||||
axis: scroll_distance
|
|
||||||
.map(|d| (d.x as f64, d.y as f64))
|
|
||||||
.unwrap_or((0.0, 0.0)),
|
|
||||||
v120: scroll_steps.map(|d| ((d.x * 120.0) as i32, (d.y * 120.0) as i32)),
|
|
||||||
stop: (false, false),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
pointer.frame(&mut state);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn keyboard_keys(&self, surface: WlSurface, keymap_id: u64, keys: Vec<i32>) {
|
if resource.version() >= EVT_NAME_SINCE {
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
resource.name(nanoid!());
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(keyboard) = self.seat.get_keyboard() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let keymaps = KEYMAPS.lock();
|
|
||||||
let Some(keymap) = keymaps.get(KeyData::from_ffi(keymap_id).into()).cloned() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
keyboard.set_focus(
|
|
||||||
&mut state.lock(),
|
|
||||||
Some(surface),
|
|
||||||
SERIAL_COUNTER.next_serial(),
|
|
||||||
);
|
|
||||||
if keyboard
|
|
||||||
.set_keymap_from_string(&mut state.lock(), keymap)
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for key in keys {
|
|
||||||
keyboard.input(
|
|
||||||
&mut state.lock(),
|
|
||||||
key.unsigned_abs(),
|
|
||||||
if key > 0 {
|
|
||||||
KeyState::Pressed
|
|
||||||
} else {
|
|
||||||
KeyState::Released
|
|
||||||
},
|
|
||||||
SERIAL_COUNTER.next_serial(),
|
|
||||||
0,
|
|
||||||
|_, _, _| FilterResult::Forward::<()>,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource.capabilities(Capability::Pointer | Capability::Keyboard | Capability::Touch);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn touch_down(&self, surface: WlSurface, id: u32, position: Vector2<f32>) {
|
fn can_view(client: Client, data: &Arc<SeatData>) -> bool {
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
let Some(seat_client) = data.client.get().cloned() else {return false};
|
||||||
return;
|
client.id() == seat_client
|
||||||
};
|
|
||||||
let Some(touch) = self.seat.get_touch() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
touch.down(
|
|
||||||
&mut state.lock(),
|
|
||||||
Some((surface, (0.0, 0.0).into())),
|
|
||||||
&DownEvent {
|
|
||||||
slot: Some(id).into(),
|
|
||||||
location: (position.x as f64, position.y as f64).into(),
|
|
||||||
serial: SERIAL_COUNTER.next_serial(),
|
|
||||||
time: 0,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
touch.frame(&mut state.lock());
|
|
||||||
}
|
}
|
||||||
pub fn touch_move(&self, id: u32, position: Vector2<f32>) {
|
}
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
|
||||||
return;
|
impl Dispatch<WlSeat, Arc<SeatData>, WaylandState> for WaylandState {
|
||||||
};
|
fn request(
|
||||||
let Some(surface) = self.touches.lock().get(&id).and_then(|c| c.upgrade().ok()) else {
|
_state: &mut WaylandState,
|
||||||
return;
|
_client: &Client,
|
||||||
};
|
_resource: &WlSeat,
|
||||||
let Some(touch) = self.seat.get_touch() else {
|
request: wl_seat::Request,
|
||||||
return;
|
data: &Arc<SeatData>,
|
||||||
};
|
_dh: &DisplayHandle,
|
||||||
touch.motion(
|
data_init: &mut DataInit<'_, WaylandState>,
|
||||||
&mut state.lock(),
|
) {
|
||||||
Some((surface, (0.0, 0.0).into())),
|
match request {
|
||||||
&touch::MotionEvent {
|
wl_seat::Request::GetPointer { id } => {
|
||||||
slot: Some(id).into(),
|
let pointer = data_init.init(id, data.clone());
|
||||||
location: (position.x as f64, position.y as f64).into(),
|
let _ = data.pointer.set((pointer, Mutex::new(ObjectId::null())));
|
||||||
time: 0,
|
}
|
||||||
},
|
wl_seat::Request::GetKeyboard { id } => {
|
||||||
);
|
let keyboard = data_init.init(id, data.clone());
|
||||||
touch.frame(&mut state.lock());
|
if keyboard.version() >= wl_keyboard::EVT_REPEAT_INFO_SINCE {
|
||||||
}
|
keyboard.repeat_info(0, 0);
|
||||||
pub fn touch_up(&self, id: u32) {
|
}
|
||||||
let Some(state) = self.wayland_state.upgrade() else {
|
let _ = data.keyboard.set((keyboard, Mutex::new(ObjectId::null())));
|
||||||
return;
|
}
|
||||||
};
|
wl_seat::Request::GetTouch { id } => {
|
||||||
let Some(touch) = self.seat.get_touch() else {
|
let _ = data.touch.set(data_init.init(id, data.clone()));
|
||||||
return;
|
}
|
||||||
};
|
wl_seat::Request::Release => (),
|
||||||
touch.up(
|
_ => unreachable!(),
|
||||||
&mut state.lock(),
|
}
|
||||||
&UpEvent {
|
}
|
||||||
slot: Some(id).into(),
|
}
|
||||||
serial: SERIAL_COUNTER.next_serial(),
|
|
||||||
time: 0,
|
impl Dispatch<WlPointer, Arc<SeatData>, WaylandState> for WaylandState {
|
||||||
},
|
fn request(
|
||||||
);
|
_state: &mut WaylandState,
|
||||||
touch.frame(&mut state.lock());
|
_client: &Client,
|
||||||
}
|
_resource: &WlPointer,
|
||||||
pub fn reset_input(&self) {
|
request: wl_pointer::Request,
|
||||||
for id in self.touches.lock().keys() {
|
seat_data: &Arc<SeatData>,
|
||||||
self.touch_up(*id)
|
dh: &DisplayHandle,
|
||||||
|
_data_init: &mut DataInit<'_, WaylandState>,
|
||||||
|
) {
|
||||||
|
match request {
|
||||||
|
wl_pointer::Request::SetCursor {
|
||||||
|
serial: _,
|
||||||
|
surface,
|
||||||
|
hotspot_x,
|
||||||
|
hotspot_y,
|
||||||
|
} => {
|
||||||
|
if let Some(surface) = surface.as_ref() {
|
||||||
|
CoreSurface::add_to(dh.clone(), surface, || (), |_| ());
|
||||||
|
compositor::with_states(surface, |data| {
|
||||||
|
if let Some(core_surface) = data.data_map.get::<Arc<CoreSurface>>() {
|
||||||
|
core_surface.set_material_offset(1);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some((_, focus)) = seat_data.pointer.get() else {return};
|
||||||
|
let focus = focus.lock();
|
||||||
|
let surfaces = seat_data.surfaces.lock();
|
||||||
|
let Some(surface_info) = surfaces.get(&focus) else {return};
|
||||||
|
let cursor_info = surface.map(|surface| CursorInfo {
|
||||||
|
surface: surface.downgrade(),
|
||||||
|
hotspot_x,
|
||||||
|
hotspot_y,
|
||||||
|
});
|
||||||
|
let _ = surface_info.cursor_sender.send_replace(cursor_info);
|
||||||
|
}
|
||||||
|
wl_pointer::Request::Release => (),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Dispatch<WlKeyboard, Arc<SeatData>, WaylandState> for WaylandState {
|
||||||
|
fn request(
|
||||||
|
_state: &mut WaylandState,
|
||||||
|
_client: &Client,
|
||||||
|
_resource: &WlKeyboard,
|
||||||
|
request: <WlKeyboard as Resource>::Request,
|
||||||
|
_data: &Arc<SeatData>,
|
||||||
|
_dh: &DisplayHandle,
|
||||||
|
_data_init: &mut DataInit<'_, WaylandState>,
|
||||||
|
) {
|
||||||
|
match request {
|
||||||
|
wl_keyboard::Request::Release => (),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Dispatch<WlTouch, Arc<SeatData>, WaylandState> for WaylandState {
|
||||||
|
fn request(
|
||||||
|
_state: &mut WaylandState,
|
||||||
|
_client: &Client,
|
||||||
|
_resource: &WlTouch,
|
||||||
|
request: <WlTouch as Resource>::Request,
|
||||||
|
_data: &Arc<SeatData>,
|
||||||
|
_dh: &DisplayHandle,
|
||||||
|
_data_init: &mut DataInit<'_, WaylandState>,
|
||||||
|
) {
|
||||||
|
match request {
|
||||||
|
wl_touch::Request::Release => (),
|
||||||
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,19 @@
|
|||||||
use super::seat::SeatWrapper;
|
use crate::wayland::seat::SeatData;
|
||||||
use crate::wayland::drm::wl_drm::WlDrm;
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use smithay::{
|
use smithay::{
|
||||||
backend::{
|
backend::{allocator::dmabuf::Dmabuf, egl::EGLDevice, renderer::gles::GlesRenderer},
|
||||||
allocator::{dmabuf::Dmabuf, Fourcc},
|
|
||||||
egl::EGLDevice,
|
|
||||||
renderer::gles::GlesRenderer,
|
|
||||||
},
|
|
||||||
delegate_dmabuf, delegate_output, delegate_shm,
|
delegate_dmabuf, delegate_output, delegate_shm,
|
||||||
input::{keyboard::XkbConfig, SeatState},
|
|
||||||
output::{Mode, Output, Scale, Subpixel},
|
output::{Mode, Output, Scale, Subpixel},
|
||||||
reexports::{
|
reexports::{
|
||||||
wayland_protocols::xdg::{
|
wayland_protocols::xdg::{
|
||||||
decoration::zv1::server::zxdg_decoration_manager_v1::ZxdgDecorationManagerV1,
|
decoration::zv1::server::zxdg_decoration_manager_v1::ZxdgDecorationManagerV1,
|
||||||
shell::server::xdg_toplevel::WmCapabilities,
|
shell::server::xdg_wm_base::XdgWmBase,
|
||||||
},
|
},
|
||||||
wayland_protocols_misc::server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as DecorationMode,
|
wayland_protocols_misc::server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as DecorationMode,
|
||||||
wayland_server::{
|
wayland_server::{
|
||||||
backend::{ClientData, ClientId, DisconnectReason},
|
backend::{ClientData, ClientId, DisconnectReason},
|
||||||
protocol::{
|
protocol::{wl_buffer::WlBuffer, wl_data_device_manager::WlDataDeviceManager},
|
||||||
wl_buffer::WlBuffer, wl_data_device_manager::WlDataDeviceManager,
|
|
||||||
wl_output::WlOutput,
|
|
||||||
},
|
|
||||||
DisplayHandle,
|
DisplayHandle,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -32,24 +23,29 @@ use smithay::{
|
|||||||
compositor::{CompositorClientState, CompositorState},
|
compositor::{CompositorClientState, CompositorState},
|
||||||
dmabuf::{
|
dmabuf::{
|
||||||
self, DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState,
|
self, DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState,
|
||||||
|
ImportError,
|
||||||
},
|
},
|
||||||
output::OutputHandler,
|
shell::kde::decoration::KdeDecorationState,
|
||||||
shell::{
|
|
||||||
kde::decoration::KdeDecorationState,
|
|
||||||
xdg::{WmCapabilitySet, XdgShellState},
|
|
||||||
},
|
|
||||||
shm::{ShmHandler, ShmState},
|
shm::{ShmHandler, ShmState},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Weak};
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
use super::DisplayWrapper;
|
||||||
|
|
||||||
pub struct ClientState {
|
pub struct ClientState {
|
||||||
pub pid: Option<i32>,
|
|
||||||
pub id: OnceCell<ClientId>,
|
pub id: OnceCell<ClientId>,
|
||||||
pub compositor_state: CompositorClientState,
|
pub compositor_state: CompositorClientState,
|
||||||
pub seat: Arc<SeatWrapper>,
|
pub display: Weak<DisplayWrapper>,
|
||||||
|
pub seat: Arc<SeatData>,
|
||||||
|
}
|
||||||
|
impl ClientState {
|
||||||
|
pub fn flush(&self) {
|
||||||
|
let Some(display) = self.display.upgrade() else {return};
|
||||||
|
let _ = display.flush_clients(self.id.get().cloned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl ClientData for ClientState {
|
impl ClientData for ClientState {
|
||||||
fn initialized(&self, client_id: ClientId) {
|
fn initialized(&self, client_id: ClientId) {
|
||||||
@@ -66,16 +62,15 @@ impl ClientData for ClientState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct WaylandState {
|
pub struct WaylandState {
|
||||||
|
pub weak_ref: Weak<Mutex<WaylandState>>,
|
||||||
|
pub display_handle: DisplayHandle,
|
||||||
|
|
||||||
pub compositor_state: CompositorState,
|
pub compositor_state: CompositorState,
|
||||||
// pub xdg_activation_state: XdgActivationState,
|
// pub xdg_activation_state: XdgActivationState,
|
||||||
pub kde_decoration_state: KdeDecorationState,
|
pub kde_decoration_state: KdeDecorationState,
|
||||||
pub shm_state: ShmState,
|
pub shm_state: ShmState,
|
||||||
dmabuf_state: (DmabufState, DmabufGlobal, Option<DmabufFeedback>),
|
dmabuf_state: (DmabufState, DmabufGlobal, Option<DmabufFeedback>),
|
||||||
pub drm_formats: Vec<Fourcc>,
|
dmabuf_tx: UnboundedSender<Dmabuf>,
|
||||||
pub dmabuf_tx: UnboundedSender<(Dmabuf, Option<dmabuf::ImportNotifier>)>,
|
|
||||||
pub seat_state: SeatState<Self>,
|
|
||||||
pub seat: Arc<SeatWrapper>,
|
|
||||||
pub xdg_shell: XdgShellState,
|
|
||||||
pub output: Output,
|
pub output: Output,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +78,7 @@ impl WaylandState {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
display_handle: DisplayHandle,
|
display_handle: DisplayHandle,
|
||||||
renderer: &GlesRenderer,
|
renderer: &GlesRenderer,
|
||||||
dmabuf_tx: UnboundedSender<(Dmabuf, Option<dmabuf::ImportNotifier>)>,
|
dmabuf_tx: UnboundedSender<Dmabuf>,
|
||||||
) -> Arc<Mutex<Self>> {
|
) -> Arc<Mutex<Self>> {
|
||||||
let compositor_state = CompositorState::new::<Self>(&display_handle);
|
let compositor_state = CompositorState::new::<Self>(&display_handle);
|
||||||
// let xdg_activation_state = XdgActivationState::new::<Self, _>(&display_handle);
|
// let xdg_activation_state = XdgActivationState::new::<Self, _>(&display_handle);
|
||||||
@@ -92,14 +87,13 @@ impl WaylandState {
|
|||||||
let shm_state = ShmState::new::<Self>(&display_handle, vec![]);
|
let shm_state = ShmState::new::<Self>(&display_handle, vec![]);
|
||||||
let render_node = EGLDevice::device_for_display(renderer.egl_context().display())
|
let render_node = EGLDevice::device_for_display(renderer.egl_context().display())
|
||||||
.and_then(|device| device.try_get_render_node());
|
.and_then(|device| device.try_get_render_node());
|
||||||
|
|
||||||
let dmabuf_formats = renderer
|
let dmabuf_formats = renderer
|
||||||
.egl_context()
|
.egl_context()
|
||||||
.dmabuf_render_formats()
|
.dmabuf_render_formats()
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let drm_formats = dmabuf_formats.iter().map(|f| f.code).collect();
|
|
||||||
|
|
||||||
let dmabuf_default_feedback = match render_node {
|
let dmabuf_default_feedback = match render_node {
|
||||||
Ok(Some(node)) => DmabufFeedbackBuilder::new(node.dev_id(), dmabuf_formats.clone())
|
Ok(Some(node)) => DmabufFeedbackBuilder::new(node.dev_id(), dmabuf_formats.clone())
|
||||||
.build()
|
.build()
|
||||||
@@ -129,12 +123,6 @@ impl WaylandState {
|
|||||||
(dmabuf_state, dmabuf_global, None)
|
(dmabuf_state, dmabuf_global, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut seat_state = SeatState::new();
|
|
||||||
let mut seat = seat_state.new_wl_seat(&display_handle, "seat0");
|
|
||||||
seat.add_pointer();
|
|
||||||
seat.add_keyboard(XkbConfig::default(), 200, 25).unwrap();
|
|
||||||
seat.add_touch();
|
|
||||||
|
|
||||||
let output = Output::new(
|
let output = Output::new(
|
||||||
"1x".to_owned(),
|
"1x".to_owned(),
|
||||||
smithay::output::PhysicalProperties {
|
smithay::output::PhysicalProperties {
|
||||||
@@ -146,7 +134,7 @@ impl WaylandState {
|
|||||||
);
|
);
|
||||||
let _output_global = output.create_global::<Self>(&display_handle);
|
let _output_global = output.create_global::<Self>(&display_handle);
|
||||||
let mode = Mode {
|
let mode = Mode {
|
||||||
size: (1024, 1024).into(),
|
size: (2048, 2048).into(),
|
||||||
refresh: 60000,
|
refresh: 60000,
|
||||||
};
|
};
|
||||||
output.change_current_state(
|
output.change_current_state(
|
||||||
@@ -156,32 +144,23 @@ impl WaylandState {
|
|||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
output.set_preferred(mode);
|
output.set_preferred(mode);
|
||||||
|
|
||||||
let mut xdg_shell = XdgShellState::new::<Self>(&display_handle);
|
|
||||||
let mut capabilities = WmCapabilitySet::default();
|
|
||||||
capabilities.set(WmCapabilities::Maximize);
|
|
||||||
capabilities.set(WmCapabilities::Fullscreen);
|
|
||||||
capabilities.unset(WmCapabilities::Minimize);
|
|
||||||
capabilities.unset(WmCapabilities::WindowMenu);
|
|
||||||
xdg_shell.replace_capabilities(capabilities);
|
|
||||||
display_handle.create_global::<Self, WlDataDeviceManager, _>(3, ());
|
display_handle.create_global::<Self, WlDataDeviceManager, _>(3, ());
|
||||||
|
display_handle.create_global::<Self, XdgWmBase, _>(5, ());
|
||||||
display_handle.create_global::<Self, ZxdgDecorationManagerV1, _>(1, ());
|
display_handle.create_global::<Self, ZxdgDecorationManagerV1, _>(1, ());
|
||||||
display_handle.create_global::<Self, WlDrm, _>(2, ());
|
|
||||||
|
|
||||||
info!("Init Wayland compositor");
|
info!("Init Wayland compositor");
|
||||||
|
|
||||||
Arc::new_cyclic(|weak| {
|
Arc::new_cyclic(|weak| {
|
||||||
Mutex::new(WaylandState {
|
Mutex::new(WaylandState {
|
||||||
|
weak_ref: weak.clone(),
|
||||||
|
display_handle,
|
||||||
|
|
||||||
compositor_state,
|
compositor_state,
|
||||||
// xdg_activation_state,
|
// xdg_activation_state,
|
||||||
kde_decoration_state,
|
kde_decoration_state,
|
||||||
shm_state,
|
shm_state,
|
||||||
drm_formats,
|
|
||||||
dmabuf_state,
|
dmabuf_state,
|
||||||
dmabuf_tx,
|
dmabuf_tx,
|
||||||
seat_state,
|
|
||||||
seat: Arc::new(SeatWrapper::new(weak.clone(), seat)),
|
|
||||||
xdg_shell,
|
|
||||||
output,
|
output,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -209,14 +188,10 @@ impl DmabufHandler for WaylandState {
|
|||||||
&mut self,
|
&mut self,
|
||||||
_global: &DmabufGlobal,
|
_global: &DmabufGlobal,
|
||||||
dmabuf: Dmabuf,
|
dmabuf: Dmabuf,
|
||||||
notifier: dmabuf::ImportNotifier,
|
) -> Result<(), dmabuf::ImportError> {
|
||||||
) {
|
self.dmabuf_tx.send(dmabuf).map_err(|_| ImportError::Failed)
|
||||||
self.dmabuf_tx.send((dmabuf, Some(notifier))).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl OutputHandler for WaylandState {
|
|
||||||
fn output_bound(&mut self, _output: Output, _wl_output: WlOutput) {}
|
|
||||||
}
|
|
||||||
delegate_dmabuf!(WaylandState);
|
delegate_dmabuf!(WaylandState);
|
||||||
delegate_shm!(WaylandState);
|
delegate_shm!(WaylandState);
|
||||||
delegate_output!(WaylandState);
|
delegate_output!(WaylandState);
|
||||||
|
|||||||
@@ -1,39 +1,34 @@
|
|||||||
use super::utils::WlSurfaceExt;
|
use super::state::WaylandState;
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{delta::Delta, destroy_queue, registry::Registry},
|
core::{delta::Delta, destroy_queue, registry::Registry},
|
||||||
nodes::{
|
nodes::drawable::{model::ModelPart, shaders::PANEL_SHADER_BYTES},
|
||||||
drawable::{
|
|
||||||
model::{MaterialWrapper, ModelPart},
|
|
||||||
shaders::PANEL_SHADER_BYTES,
|
|
||||||
},
|
|
||||||
items::camera::TexWrapper,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
use mint::Vector2;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use send_wrapper::SendWrapper;
|
use send_wrapper::SendWrapper;
|
||||||
use smithay::{
|
use smithay::{
|
||||||
backend::renderer::{
|
backend::renderer::{
|
||||||
gles::{GlesRenderer, GlesTexture},
|
gles::{GlesRenderer, GlesTexture},
|
||||||
utils::{import_surface_tree, RendererSurfaceStateUserData},
|
utils::{import_surface_tree, on_commit_buffer_handler, RendererSurfaceStateUserData},
|
||||||
Renderer, Texture,
|
Renderer, Texture,
|
||||||
},
|
},
|
||||||
desktop::utils::send_frames_surface_tree,
|
desktop::utils::send_frames_surface_tree,
|
||||||
output::Output,
|
output::Output,
|
||||||
reexports::wayland_server::{self, protocol::wl_surface::WlSurface, Resource},
|
reexports::wayland_server::{self, protocol::wl_surface::WlSurface, DisplayHandle, Resource},
|
||||||
|
wayland::compositor::{self, SurfaceData},
|
||||||
};
|
};
|
||||||
use std::{ffi::c_void, sync::Arc, time::Duration};
|
use std::{cell::RefCell, ffi::c_void, sync::Arc, time::Duration};
|
||||||
use stereokit_rust::{
|
use stereokit::{
|
||||||
material::{Material, Transparency},
|
Material, Shader, StereoKitDraw, Tex, TextureAddress, TextureFormat, TextureSample,
|
||||||
shader::Shader,
|
TextureType, Transparency,
|
||||||
tex::{Tex, TexAddress, TexFormat, TexSample, TexType},
|
|
||||||
util::Time,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub static CORE_SURFACES: Registry<CoreSurface> = Registry::new();
|
pub static CORE_SURFACES: Registry<CoreSurface> = Registry::new();
|
||||||
|
|
||||||
pub struct CoreSurfaceData {
|
pub struct CoreSurfaceData {
|
||||||
wl_tex: Option<SendWrapper<GlesTexture>>,
|
wl_tex: Option<SendWrapper<GlesTexture>>,
|
||||||
|
pub size: Vector2<u32>,
|
||||||
}
|
}
|
||||||
impl Drop for CoreSurfaceData {
|
impl Drop for CoreSurfaceData {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
@@ -42,133 +37,139 @@ impl Drop for CoreSurfaceData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct CoreSurface {
|
pub struct CoreSurface {
|
||||||
|
pub dh: DisplayHandle,
|
||||||
pub weak_surface: wayland_server::Weak<WlSurface>,
|
pub weak_surface: wayland_server::Weak<WlSurface>,
|
||||||
mapped_data: Mutex<Option<CoreSurfaceData>>,
|
mapped_data: Mutex<Option<CoreSurfaceData>>,
|
||||||
sk_tex: OnceCell<Mutex<TexWrapper>>,
|
sk_tex: OnceCell<Tex>,
|
||||||
sk_mat: OnceCell<Mutex<MaterialWrapper>>,
|
sk_mat: OnceCell<Arc<Material>>,
|
||||||
material_offset: Mutex<Delta<u32>>,
|
material_offset: Mutex<Delta<u32>>,
|
||||||
|
on_mapped: Box<dyn Fn() + Send + Sync>,
|
||||||
|
on_commit: Box<dyn Fn(u32) + Send + Sync>,
|
||||||
pub pending_material_applications: Registry<ModelPart>,
|
pub pending_material_applications: Registry<ModelPart>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CoreSurface {
|
impl CoreSurface {
|
||||||
pub fn add_to(surface: &WlSurface) {
|
pub fn add_to(
|
||||||
let core_surface = CORE_SURFACES.add(CoreSurface {
|
dh: DisplayHandle,
|
||||||
weak_surface: surface.downgrade(),
|
surface: &WlSurface,
|
||||||
mapped_data: Mutex::new(None),
|
on_mapped: impl Fn() + Send + Sync + 'static,
|
||||||
sk_tex: OnceCell::new(),
|
on_commit: impl Fn(u32) + Send + Sync + 'static,
|
||||||
sk_mat: OnceCell::new(),
|
) {
|
||||||
material_offset: Mutex::new(Delta::new(0)),
|
compositor::with_states(surface, |data| {
|
||||||
pending_material_applications: Registry::new(),
|
data.data_map.insert_if_missing_threadsafe(|| {
|
||||||
|
CORE_SURFACES.add(CoreSurface {
|
||||||
|
dh,
|
||||||
|
weak_surface: surface.downgrade(),
|
||||||
|
mapped_data: Mutex::new(None),
|
||||||
|
sk_tex: OnceCell::new(),
|
||||||
|
sk_mat: OnceCell::new(),
|
||||||
|
material_offset: Mutex::new(Delta::new(0)),
|
||||||
|
on_mapped: Box::new(on_mapped) as Box<dyn Fn() + Send + Sync>,
|
||||||
|
on_commit: Box::new(on_commit) as Box<dyn Fn(u32) + Send + Sync>,
|
||||||
|
pending_material_applications: Registry::new(),
|
||||||
|
})
|
||||||
|
});
|
||||||
});
|
});
|
||||||
surface.insert_data(core_surface);
|
}
|
||||||
|
|
||||||
|
pub fn commit(&self, count: u32) {
|
||||||
|
(self.on_commit)(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_wl_surface(surf: &WlSurface) -> Option<Arc<CoreSurface>> {
|
pub fn from_wl_surface(surf: &WlSurface) -> Option<Arc<CoreSurface>> {
|
||||||
surf.get_data()
|
compositor::with_states(surf, |data| {
|
||||||
|
data.data_map.get::<Arc<CoreSurface>>().cloned()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process(&self, renderer: &mut GlesRenderer) {
|
pub fn process(&self, sk: &impl StereoKitDraw, renderer: &mut GlesRenderer) {
|
||||||
let Some(wl_surface) = self.wl_surface() else {
|
let Some(wl_surface) = self.wl_surface() else {return};
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let sk_tex = self.sk_tex.get_or_init(|| {
|
let sk_tex = self
|
||||||
Mutex::new(TexWrapper(Tex::new(
|
.sk_tex
|
||||||
TexType::ImageNomips,
|
.get_or_init(|| sk.tex_create(TextureType::IMAGE_NO_MIPS, TextureFormat::RGBA32));
|
||||||
TexFormat::RGBA32Linear,
|
|
||||||
nanoid::nanoid!(),
|
|
||||||
)))
|
|
||||||
});
|
|
||||||
self.sk_mat.get_or_init(|| {
|
self.sk_mat.get_or_init(|| {
|
||||||
let shader = Shader::from_memory(PANEL_SHADER_BYTES).unwrap();
|
let shader = sk.shader_create_mem(&PANEL_SHADER_BYTES);
|
||||||
// let _ = renderer.with_context(|c| unsafe {
|
// let _ = renderer.with_context(|c| unsafe {
|
||||||
// shader_inject(c, &mut shader, SIMULA_VERT_STR, SIMULA_FRAG_STR)
|
// shader_inject(c, &mut shader, SIMULA_VERT_STR, SIMULA_FRAG_STR)
|
||||||
// });
|
// });
|
||||||
|
|
||||||
let mut mat = Material::new(shader, None);
|
let mat = sk.material_create(shader.as_ref().unwrap_or(Shader::UI.as_ref()));
|
||||||
mat.diffuse_tex(&sk_tex.lock().0);
|
sk.material_set_texture(&mat, "diffuse", sk_tex.as_ref());
|
||||||
mat.transparency(Transparency::Blend);
|
sk.material_set_transparency(&mat, Transparency::Blend);
|
||||||
Mutex::new(MaterialWrapper(mat))
|
Arc::new(mat)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Let smithay handle buffer management (has to be done here as RendererSurfaceStates is not thread safe)
|
||||||
|
on_commit_buffer_handler::<WaylandState>(&wl_surface);
|
||||||
// Import all surface buffers into textures
|
// Import all surface buffers into textures
|
||||||
if import_surface_tree(renderer, &wl_surface).is_err() {
|
if import_surface_tree(renderer, &wl_surface).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_textures(renderer);
|
let mapped = compositor::with_states(&wl_surface, |data| {
|
||||||
self.apply_surface_materials();
|
data.data_map
|
||||||
}
|
.get::<RendererSurfaceStateUserData>()
|
||||||
|
.map(|surface_states| surface_states.borrow().buffer().is_some())
|
||||||
pub fn update_textures(&self, renderer: &mut GlesRenderer) {
|
.unwrap_or(false)
|
||||||
let Some(wl_surface) = self.wl_surface() else {
|
});
|
||||||
return;
|
|
||||||
};
|
|
||||||
let mapped = wl_surface
|
|
||||||
.get_data_raw::<RendererSurfaceStateUserData, _, _>(|surface_states| {
|
|
||||||
surface_states.lock().unwrap().buffer().is_some()
|
|
||||||
})
|
|
||||||
.unwrap_or(false);
|
|
||||||
|
|
||||||
if !mapped {
|
if !mapped {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut mapped_data = self.mapped_data.lock();
|
let mut mapped_data = self.mapped_data.lock();
|
||||||
|
let just_mapped = mapped_data.is_none();
|
||||||
|
self.with_states(|data| {
|
||||||
|
let Some(renderer_surface_state) = data
|
||||||
|
.data_map
|
||||||
|
.get::<RendererSurfaceStateUserData>()
|
||||||
|
.map(RefCell::borrow) else {return};
|
||||||
|
let Some(smithay_tex) = renderer_surface_state
|
||||||
|
.texture::<GlesRenderer>(renderer.id())
|
||||||
|
.cloned() else {return};
|
||||||
|
|
||||||
let Some(smithay_tex) = wl_surface
|
let Some(sk_tex) = self.sk_tex.get() else {return};
|
||||||
.get_data_raw::<RendererSurfaceStateUserData, _, _>(|surface_states| {
|
let Some(sk_mat) = self.sk_mat.get() else {return};
|
||||||
surface_states
|
unsafe {
|
||||||
.lock()
|
sk.tex_set_surface(
|
||||||
.unwrap()
|
sk_tex.as_ref(),
|
||||||
.texture::<GlesRenderer>(renderer.id())
|
smithay_tex.tex_id() as usize as *mut c_void,
|
||||||
.cloned()
|
TextureType::IMAGE_NO_MIPS,
|
||||||
})
|
smithay::backend::renderer::gles::ffi::RGBA8.into(),
|
||||||
.flatten()
|
smithay_tex.width() as i32,
|
||||||
else {
|
smithay_tex.height() as i32,
|
||||||
return;
|
1,
|
||||||
};
|
false,
|
||||||
|
);
|
||||||
|
sk.tex_set_sample(sk_tex.as_ref(), TextureSample::Point);
|
||||||
|
sk.tex_set_address(sk_tex.as_ref(), TextureAddress::Clamp);
|
||||||
|
}
|
||||||
|
if let Some(material_offset) = self.material_offset.lock().delta() {
|
||||||
|
sk.material_set_queue_offset(sk_mat.as_ref().as_ref(), *material_offset as i32);
|
||||||
|
}
|
||||||
|
|
||||||
let Some(sk_tex) = self.sk_tex.get() else {
|
let Some(surface_size) = renderer_surface_state.surface_size() else {return};
|
||||||
return;
|
let new_mapped_data = CoreSurfaceData {
|
||||||
};
|
size: Vector2::from([surface_size.w as u32, surface_size.h as u32]),
|
||||||
let Some(sk_mat) = self.sk_mat.get() else {
|
wl_tex: Some(SendWrapper::new(smithay_tex)),
|
||||||
return;
|
};
|
||||||
};
|
*mapped_data = Some(new_mapped_data);
|
||||||
sk_tex
|
});
|
||||||
.lock()
|
drop(mapped_data);
|
||||||
.0
|
if just_mapped {
|
||||||
.set_native_surface(
|
(self.on_mapped)();
|
||||||
smithay_tex.tex_id() as usize as *mut c_void,
|
|
||||||
TexType::ImageNomips,
|
|
||||||
smithay::backend::renderer::gles::ffi::RGBA8.into(),
|
|
||||||
smithay_tex.width() as i32,
|
|
||||||
smithay_tex.height() as i32,
|
|
||||||
1,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.sample_mode(TexSample::Point)
|
|
||||||
.address_mode(TexAddress::Clamp);
|
|
||||||
|
|
||||||
if let Some(material_offset) = self.material_offset.lock().delta() {
|
|
||||||
sk_mat.lock().0.queue_offset(*material_offset as i32);
|
|
||||||
}
|
}
|
||||||
|
self.apply_surface_materials();
|
||||||
let new_mapped_data = CoreSurfaceData {
|
|
||||||
wl_tex: Some(SendWrapper::new(smithay_tex)),
|
|
||||||
};
|
|
||||||
*mapped_data = Some(new_mapped_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame(&self, output: Output) {
|
pub fn frame(&self, sk: &impl StereoKitDraw, output: Output) {
|
||||||
let Some(wl_surface) = self.wl_surface() else {
|
let Some(wl_surface) = self.wl_surface() else {return};
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
send_frames_surface_tree(
|
send_frames_surface_tree(
|
||||||
&wl_surface,
|
&wl_surface,
|
||||||
&output,
|
&output,
|
||||||
Duration::from_secs_f64(Time::get_total_unscaled()),
|
Duration::from_secs_f64(sk.time_get()),
|
||||||
None,
|
None,
|
||||||
|_, _| Some(output.clone()),
|
|_, _| Some(output.clone()),
|
||||||
);
|
);
|
||||||
@@ -184,9 +185,8 @@ impl CoreSurface {
|
|||||||
|
|
||||||
fn apply_surface_materials(&self) {
|
fn apply_surface_materials(&self) {
|
||||||
if let Some(sk_mat) = self.sk_mat.get() {
|
if let Some(sk_mat) = self.sk_mat.get() {
|
||||||
let sk_mat = sk_mat.lock();
|
|
||||||
for model_node in self.pending_material_applications.get_valid_contents() {
|
for model_node in self.pending_material_applications.get_valid_contents() {
|
||||||
model_node.replace_material_now(&sk_mat.0);
|
model_node.replace_material(sk_mat.clone());
|
||||||
}
|
}
|
||||||
self.pending_material_applications.clear();
|
self.pending_material_applications.clear();
|
||||||
}
|
}
|
||||||
@@ -195,6 +195,18 @@ impl CoreSurface {
|
|||||||
pub fn wl_surface(&self) -> Option<WlSurface> {
|
pub fn wl_surface(&self) -> Option<WlSurface> {
|
||||||
self.weak_surface.upgrade().ok()
|
self.weak_surface.upgrade().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_states<F, T>(&self, f: F) -> Option<T>
|
||||||
|
where
|
||||||
|
F: FnOnce(&SurfaceData) -> T,
|
||||||
|
{
|
||||||
|
self.wl_surface()
|
||||||
|
.map(|wl_surface| compositor::with_states(&wl_surface, f))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> Option<Vector2<u32>> {
|
||||||
|
self.mapped_data.lock().as_ref().map(|d| d.size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl Drop for CoreSurface {
|
impl Drop for CoreSurface {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
|||||||
@@ -1,121 +0,0 @@
|
|||||||
use mint::Vector2;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use smithay::{
|
|
||||||
backend::renderer::utils::RendererSurfaceStateUserData,
|
|
||||||
reexports::wayland_server::protocol::wl_surface::WlSurface,
|
|
||||||
wayland::{
|
|
||||||
compositor,
|
|
||||||
shell::xdg::{SurfaceCachedState, XdgToplevelSurfaceData},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::nodes::items::panel::{ChildInfo, Geometry, ToplevelInfo};
|
|
||||||
|
|
||||||
use super::xdg_shell::surface_panel_item;
|
|
||||||
pub trait WlSurfaceExt {
|
|
||||||
fn insert_data<T: Send + Sync + 'static>(&self, data: T) -> bool;
|
|
||||||
fn get_data<T: Send + Sync + Clone + 'static>(&self) -> Option<T>;
|
|
||||||
fn get_data_raw<T: Send + Sync + 'static, O, F: FnOnce(&T) -> O>(&self, f: F) -> Option<O>;
|
|
||||||
fn get_current_surface_state(&self) -> SurfaceCachedState;
|
|
||||||
fn get_pending_surface_state(&self) -> SurfaceCachedState;
|
|
||||||
fn get_size(&self) -> Option<Vector2<u32>>;
|
|
||||||
fn get_geometry(&self) -> Option<Geometry>;
|
|
||||||
}
|
|
||||||
impl WlSurfaceExt for WlSurface {
|
|
||||||
fn insert_data<T: Send + Sync + 'static>(&self, data: T) -> bool {
|
|
||||||
compositor::with_states(self, |d| {
|
|
||||||
d.data_map.insert_if_missing_threadsafe(move || data)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn get_data<T: Send + Sync + Clone + 'static>(&self) -> Option<T> {
|
|
||||||
compositor::with_states(self, |d| d.data_map.get::<T>().cloned())
|
|
||||||
}
|
|
||||||
fn get_data_raw<T: Send + Sync + 'static, O, F: FnOnce(&T) -> O>(&self, f: F) -> Option<O> {
|
|
||||||
compositor::with_states(self, |d| Some((f)(d.data_map.get::<T>()?)))
|
|
||||||
}
|
|
||||||
fn get_current_surface_state(&self) -> SurfaceCachedState {
|
|
||||||
compositor::with_states(self, |states| {
|
|
||||||
*states.cached_state.get::<SurfaceCachedState>().current()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn get_pending_surface_state(&self) -> SurfaceCachedState {
|
|
||||||
compositor::with_states(self, |states| {
|
|
||||||
*states.cached_state.get::<SurfaceCachedState>().pending()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn get_size(&self) -> Option<Vector2<u32>> {
|
|
||||||
self.get_data_raw::<RendererSurfaceStateUserData, _, _>(|surface_states| {
|
|
||||||
surface_states.lock().unwrap().surface_size()
|
|
||||||
})
|
|
||||||
.flatten()
|
|
||||||
.map(|size| Vector2::from([size.w as u32, size.h as u32]))
|
|
||||||
}
|
|
||||||
fn get_geometry(&self) -> Option<Geometry> {
|
|
||||||
self.get_current_surface_state().geometry.map(|r| r.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ToplevelInfoExt {
|
|
||||||
fn get_toplevel_info(&self) -> Option<ToplevelInfo>;
|
|
||||||
fn with_toplevel_info<O, F: FnOnce(&mut ToplevelInfo) -> O>(&self, f: F) -> Option<O>;
|
|
||||||
|
|
||||||
fn get_parent(&self) -> Option<u64>;
|
|
||||||
fn get_app_id(&self) -> Option<String>;
|
|
||||||
fn get_title(&self) -> Option<String>;
|
|
||||||
fn min_size(&self) -> Option<Vector2<u32>>;
|
|
||||||
fn max_size(&self) -> Option<Vector2<u32>>;
|
|
||||||
}
|
|
||||||
impl ToplevelInfoExt for WlSurface {
|
|
||||||
fn get_toplevel_info(&self) -> Option<ToplevelInfo> {
|
|
||||||
self.get_data_raw::<Mutex<ToplevelInfo>, _, _>(|c| c.lock().clone())
|
|
||||||
}
|
|
||||||
fn with_toplevel_info<O, F: FnOnce(&mut ToplevelInfo) -> O>(&self, f: F) -> Option<O> {
|
|
||||||
self.get_data_raw::<Mutex<ToplevelInfo>, _, _>(|r| (f)(&mut r.lock()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_parent(&self) -> Option<u64> {
|
|
||||||
self.get_data_raw::<XdgToplevelSurfaceData, _, _>(|d| d.lock().unwrap().parent.clone())
|
|
||||||
.flatten()
|
|
||||||
.and_then(|p| surface_panel_item(&p))
|
|
||||||
.and_then(|p| p.node.upgrade())
|
|
||||||
.map(|p| p.get_id())
|
|
||||||
}
|
|
||||||
fn get_app_id(&self) -> Option<String> {
|
|
||||||
self.get_data_raw::<XdgToplevelSurfaceData, _, _>(|d| d.lock().ok()?.app_id.clone())
|
|
||||||
.flatten()
|
|
||||||
}
|
|
||||||
fn get_title(&self) -> Option<String> {
|
|
||||||
self.get_data_raw::<XdgToplevelSurfaceData, _, _>(|d| d.lock().ok()?.title.clone())
|
|
||||||
.flatten()
|
|
||||||
}
|
|
||||||
fn min_size(&self) -> Option<Vector2<u32>> {
|
|
||||||
let state = self.get_pending_surface_state();
|
|
||||||
let size = state.min_size;
|
|
||||||
if size.w == 0 && size.h == 0 {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Vector2::from([size.w as u32, size.h as u32]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn max_size(&self) -> Option<Vector2<u32>> {
|
|
||||||
let state = self.get_pending_surface_state();
|
|
||||||
let size = state.max_size;
|
|
||||||
if size.w == 0 && size.h == 0 {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Vector2::from([size.w as u32, size.h as u32]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub trait ChildInfoExt {
|
|
||||||
fn get_child_info(&self) -> Option<ChildInfo>;
|
|
||||||
fn with_child_info<O, F: FnOnce(&mut ChildInfo) -> O>(&self, f: F) -> Option<O>;
|
|
||||||
}
|
|
||||||
impl ChildInfoExt for WlSurface {
|
|
||||||
fn get_child_info(&self) -> Option<ChildInfo> {
|
|
||||||
self.get_data_raw::<Mutex<ChildInfo>, _, _>(|c| c.lock().clone())
|
|
||||||
}
|
|
||||||
fn with_child_info<O, F: FnOnce(&mut ChildInfo) -> O>(&self, f: F) -> Option<O> {
|
|
||||||
self.get_data_raw::<Mutex<ChildInfo>, _, _>(|r| (f)(&mut r.lock()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,189 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<protocol name="drm">
|
|
||||||
|
|
||||||
<copyright>
|
|
||||||
Copyright © 2008-2011 Kristian Høgsberg
|
|
||||||
Copyright © 2010-2011 Intel Corporation
|
|
||||||
|
|
||||||
Permission to use, copy, modify, distribute, and sell this
|
|
||||||
software and its documentation for any purpose is hereby granted
|
|
||||||
without fee, provided that\n the above copyright notice appear in
|
|
||||||
all copies and that both that copyright notice and this permission
|
|
||||||
notice appear in supporting documentation, and that the name of
|
|
||||||
the copyright holders not be used in advertising or publicity
|
|
||||||
pertaining to distribution of the software without specific,
|
|
||||||
written prior permission. The copyright holders make no
|
|
||||||
representations about the suitability of this software for any
|
|
||||||
purpose. It is provided "as is" without express or implied
|
|
||||||
warranty.
|
|
||||||
|
|
||||||
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
||||||
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
||||||
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
|
||||||
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
||||||
THIS SOFTWARE.
|
|
||||||
</copyright>
|
|
||||||
|
|
||||||
<!-- drm support. This object is created by the server and published
|
|
||||||
using the display's global event. -->
|
|
||||||
<interface name="wl_drm" version="2">
|
|
||||||
<enum name="error">
|
|
||||||
<entry name="authenticate_fail" value="0" />
|
|
||||||
<entry name="invalid_format" value="1" />
|
|
||||||
<entry name="invalid_name" value="2" />
|
|
||||||
</enum>
|
|
||||||
|
|
||||||
<enum name="format">
|
|
||||||
<!-- The drm format codes match the #defines in drm_fourcc.h.
|
|
||||||
The formats actually supported by the compositor will be
|
|
||||||
reported by the format event. New codes must not be added,
|
|
||||||
unless directly taken from drm_fourcc.h. -->
|
|
||||||
<entry name="c8" value="0x20203843" />
|
|
||||||
<entry name="rgb332" value="0x38424752" />
|
|
||||||
<entry name="bgr233" value="0x38524742" />
|
|
||||||
<entry name="xrgb4444" value="0x32315258" />
|
|
||||||
<entry name="xbgr4444" value="0x32314258" />
|
|
||||||
<entry name="rgbx4444" value="0x32315852" />
|
|
||||||
<entry name="bgrx4444" value="0x32315842" />
|
|
||||||
<entry name="argb4444" value="0x32315241" />
|
|
||||||
<entry name="abgr4444" value="0x32314241" />
|
|
||||||
<entry name="rgba4444" value="0x32314152" />
|
|
||||||
<entry name="bgra4444" value="0x32314142" />
|
|
||||||
<entry name="xrgb1555" value="0x35315258" />
|
|
||||||
<entry name="xbgr1555" value="0x35314258" />
|
|
||||||
<entry name="rgbx5551" value="0x35315852" />
|
|
||||||
<entry name="bgrx5551" value="0x35315842" />
|
|
||||||
<entry name="argb1555" value="0x35315241" />
|
|
||||||
<entry name="abgr1555" value="0x35314241" />
|
|
||||||
<entry name="rgba5551" value="0x35314152" />
|
|
||||||
<entry name="bgra5551" value="0x35314142" />
|
|
||||||
<entry name="rgb565" value="0x36314752" />
|
|
||||||
<entry name="bgr565" value="0x36314742" />
|
|
||||||
<entry name="rgb888" value="0x34324752" />
|
|
||||||
<entry name="bgr888" value="0x34324742" />
|
|
||||||
<entry name="xrgb8888" value="0x34325258" />
|
|
||||||
<entry name="xbgr8888" value="0x34324258" />
|
|
||||||
<entry name="rgbx8888" value="0x34325852" />
|
|
||||||
<entry name="bgrx8888" value="0x34325842" />
|
|
||||||
<entry name="argb8888" value="0x34325241" />
|
|
||||||
<entry name="abgr8888" value="0x34324241" />
|
|
||||||
<entry name="rgba8888" value="0x34324152" />
|
|
||||||
<entry name="bgra8888" value="0x34324142" />
|
|
||||||
<entry name="xrgb2101010" value="0x30335258" />
|
|
||||||
<entry name="xbgr2101010" value="0x30334258" />
|
|
||||||
<entry name="rgbx1010102" value="0x30335852" />
|
|
||||||
<entry name="bgrx1010102" value="0x30335842" />
|
|
||||||
<entry name="argb2101010" value="0x30335241" />
|
|
||||||
<entry name="abgr2101010" value="0x30334241" />
|
|
||||||
<entry name="rgba1010102" value="0x30334152" />
|
|
||||||
<entry name="bgra1010102" value="0x30334142" />
|
|
||||||
<entry name="yuyv" value="0x56595559" />
|
|
||||||
<entry name="yvyu" value="0x55595659" />
|
|
||||||
<entry name="uyvy" value="0x59565955" />
|
|
||||||
<entry name="vyuy" value="0x59555956" />
|
|
||||||
<entry name="ayuv" value="0x56555941" />
|
|
||||||
<entry name="xyuv8888" value="0x56555958" />
|
|
||||||
<entry name="nv12" value="0x3231564e" />
|
|
||||||
<entry name="nv21" value="0x3132564e" />
|
|
||||||
<entry name="nv16" value="0x3631564e" />
|
|
||||||
<entry name="nv61" value="0x3136564e" />
|
|
||||||
<entry name="yuv410" value="0x39565559" />
|
|
||||||
<entry name="yvu410" value="0x39555659" />
|
|
||||||
<entry name="yuv411" value="0x31315559" />
|
|
||||||
<entry name="yvu411" value="0x31315659" />
|
|
||||||
<entry name="yuv420" value="0x32315559" />
|
|
||||||
<entry name="yvu420" value="0x32315659" />
|
|
||||||
<entry name="yuv422" value="0x36315559" />
|
|
||||||
<entry name="yvu422" value="0x36315659" />
|
|
||||||
<entry name="yuv444" value="0x34325559" />
|
|
||||||
<entry name="yvu444" value="0x34325659" />
|
|
||||||
<entry name="abgr16f" value="0x48344241" />
|
|
||||||
<entry name="xbgr16f" value="0x48344258" />
|
|
||||||
</enum>
|
|
||||||
|
|
||||||
<!-- Call this request with the magic received from drmGetMagic().
|
|
||||||
It will be passed on to the drmAuthMagic() or
|
|
||||||
DRIAuthConnection() call. This authentication must be
|
|
||||||
completed before create_buffer could be used. -->
|
|
||||||
<request name="authenticate">
|
|
||||||
<arg name="id" type="uint" />
|
|
||||||
</request>
|
|
||||||
|
|
||||||
<!-- Create a wayland buffer for the named DRM buffer. The DRM
|
|
||||||
surface must have a name using the flink ioctl -->
|
|
||||||
<request name="create_buffer">
|
|
||||||
<arg name="id" type="new_id" interface="wl_buffer" />
|
|
||||||
<arg name="name" type="uint" />
|
|
||||||
<arg name="width" type="int" />
|
|
||||||
<arg name="height" type="int" />
|
|
||||||
<arg name="stride" type="uint" />
|
|
||||||
<arg name="format" type="uint" />
|
|
||||||
</request>
|
|
||||||
|
|
||||||
<!-- Create a wayland buffer for the named DRM buffer. The DRM
|
|
||||||
surface must have a name using the flink ioctl -->
|
|
||||||
<request name="create_planar_buffer">
|
|
||||||
<arg name="id" type="new_id" interface="wl_buffer" />
|
|
||||||
<arg name="name" type="uint" />
|
|
||||||
<arg name="width" type="int" />
|
|
||||||
<arg name="height" type="int" />
|
|
||||||
<arg name="format" type="uint" />
|
|
||||||
<arg name="offset0" type="int" />
|
|
||||||
<arg name="stride0" type="int" />
|
|
||||||
<arg name="offset1" type="int" />
|
|
||||||
<arg name="stride1" type="int" />
|
|
||||||
<arg name="offset2" type="int" />
|
|
||||||
<arg name="stride2" type="int" />
|
|
||||||
</request>
|
|
||||||
|
|
||||||
<!-- Notification of the path of the drm device which is used by
|
|
||||||
the server. The client should use this device for creating
|
|
||||||
local buffers. Only buffers created from this device should
|
|
||||||
be be passed to the server using this drm object's
|
|
||||||
create_buffer request. -->
|
|
||||||
<event name="device">
|
|
||||||
<arg name="name" type="string" />
|
|
||||||
</event>
|
|
||||||
|
|
||||||
<event name="format">
|
|
||||||
<arg name="format" type="uint" />
|
|
||||||
</event>
|
|
||||||
|
|
||||||
<!-- Raised if the authenticate request succeeded -->
|
|
||||||
<event name="authenticated" />
|
|
||||||
|
|
||||||
<enum name="capability" since="2">
|
|
||||||
<description summary="wl_drm capability bitmask">
|
|
||||||
Bitmask of capabilities.
|
|
||||||
</description>
|
|
||||||
<entry name="prime" value="1" summary="wl_drm prime available" />
|
|
||||||
</enum>
|
|
||||||
|
|
||||||
<event name="capabilities">
|
|
||||||
<arg name="value" type="uint" />
|
|
||||||
</event>
|
|
||||||
|
|
||||||
<!-- Version 2 additions -->
|
|
||||||
|
|
||||||
<!-- Create a wayland buffer for the prime fd. Use for regular and planar
|
|
||||||
buffers. Pass 0 for offset and stride for unused planes. -->
|
|
||||||
<request name="create_prime_buffer" since="2">
|
|
||||||
<arg name="id" type="new_id" interface="wl_buffer" />
|
|
||||||
<arg name="name" type="fd" />
|
|
||||||
<arg name="width" type="int" />
|
|
||||||
<arg name="height" type="int" />
|
|
||||||
<arg name="format" type="uint" />
|
|
||||||
<arg name="offset0" type="int" />
|
|
||||||
<arg name="stride0" type="int" />
|
|
||||||
<arg name="offset1" type="int" />
|
|
||||||
<arg name="stride1" type="int" />
|
|
||||||
<arg name="offset2" type="int" />
|
|
||||||
<arg name="stride2" type="int" />
|
|
||||||
</request>
|
|
||||||
|
|
||||||
</interface>
|
|
||||||
|
|
||||||
</protocol>
|
|
||||||
File diff suppressed because it is too large
Load Diff
437
src/wayland/xwayland.rs
Normal file
437
src/wayland/xwayland.rs
Normal file
@@ -0,0 +1,437 @@
|
|||||||
|
use super::{
|
||||||
|
seat::{KeyboardEvent, PointerEvent, SeatData},
|
||||||
|
state::ClientState,
|
||||||
|
};
|
||||||
|
use crate::{
|
||||||
|
nodes::{
|
||||||
|
data::KEYMAPS,
|
||||||
|
drawable::model::ModelPart,
|
||||||
|
items::panel::{Backend, Geometry, PanelItem, PanelItemInitData, SurfaceID, ToplevelInfo},
|
||||||
|
},
|
||||||
|
wayland::surface::CoreSurface,
|
||||||
|
};
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
|
use mint::Vector2;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
|
use smithay::{
|
||||||
|
reexports::{
|
||||||
|
calloop::{EventLoop, LoopSignal},
|
||||||
|
wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle, Resource},
|
||||||
|
x11rb::protocol::xproto::Window,
|
||||||
|
},
|
||||||
|
utils::{Logical, Rectangle},
|
||||||
|
wayland::compositor,
|
||||||
|
xwayland::{
|
||||||
|
xwm::{Reorder, ResizeEdge, XwmId},
|
||||||
|
X11Surface, X11Wm, XWayland, XWaylandEvent, XwmHandler,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use std::{ffi::OsStr, iter::empty, sync::Arc, time::Duration};
|
||||||
|
use tokio::sync::oneshot;
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
|
pub static DISPLAY: OnceCell<String> = OnceCell::new();
|
||||||
|
|
||||||
|
pub struct XWaylandState {
|
||||||
|
pub display: u32,
|
||||||
|
event_loop_signal: LoopSignal,
|
||||||
|
}
|
||||||
|
impl XWaylandState {
|
||||||
|
pub fn create(dh: &DisplayHandle) -> Result<Self> {
|
||||||
|
let dh = dh.clone();
|
||||||
|
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let mut event_loop: EventLoop<XWaylandHandler> = EventLoop::try_new()?;
|
||||||
|
let (xwayland, connection) = XWayland::new(&dh);
|
||||||
|
let handle = event_loop.handle();
|
||||||
|
event_loop
|
||||||
|
.handle()
|
||||||
|
.insert_source(connection, {
|
||||||
|
let dh = dh.clone();
|
||||||
|
move |event, _, handler| match event {
|
||||||
|
XWaylandEvent::Ready {
|
||||||
|
connection,
|
||||||
|
client,
|
||||||
|
client_fd: _,
|
||||||
|
display: _,
|
||||||
|
} => {
|
||||||
|
handler.seat = client.get_data::<ClientState>().map(|s| s.seat.clone());
|
||||||
|
handler.wm =
|
||||||
|
X11Wm::start_wm(handle.clone(), dh.clone(), connection, client)
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
XWaylandEvent::Exited => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map_err(|e| e.error)?;
|
||||||
|
|
||||||
|
let display = xwayland.start(
|
||||||
|
event_loop.handle(),
|
||||||
|
None,
|
||||||
|
empty::<(&OsStr, &OsStr)>(),
|
||||||
|
true,
|
||||||
|
|_| (),
|
||||||
|
)?;
|
||||||
|
let _ = tx.send(XWaylandState {
|
||||||
|
display,
|
||||||
|
event_loop_signal: event_loop.get_signal(),
|
||||||
|
});
|
||||||
|
let mut handler = XWaylandHandler {
|
||||||
|
wayland_display_handle: dh,
|
||||||
|
wm: None,
|
||||||
|
seat: None,
|
||||||
|
};
|
||||||
|
event_loop.run(Duration::from_millis(100), &mut handler, |_| ())
|
||||||
|
});
|
||||||
|
|
||||||
|
let state = rx.blocking_recv()?;
|
||||||
|
let _ = DISPLAY.set(format!(":{}", state.display));
|
||||||
|
|
||||||
|
Ok(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for XWaylandState {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.event_loop_signal.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct XWaylandHandler {
|
||||||
|
wayland_display_handle: DisplayHandle,
|
||||||
|
wm: Option<X11Wm>,
|
||||||
|
seat: Option<Arc<SeatData>>,
|
||||||
|
}
|
||||||
|
impl XWaylandHandler {
|
||||||
|
fn panel_item(&self, window: &X11Surface) -> Option<Arc<PanelItem<X11Backend>>> {
|
||||||
|
compositor::with_states(&window.wl_surface()?, |s| {
|
||||||
|
s.data_map.get::<Arc<PanelItem<X11Backend>>>().cloned()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XwmHandler for XWaylandHandler {
|
||||||
|
fn xwm_state(&mut self, _xwm: XwmId) -> &mut X11Wm {
|
||||||
|
self.wm.as_mut().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_window(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "New X window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_override_redirect_window(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "New X override redirect window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_window_request(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "X map window request");
|
||||||
|
window.set_mapped(true).unwrap();
|
||||||
|
}
|
||||||
|
fn map_window_notify(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "X map window notify");
|
||||||
|
|
||||||
|
let _ = window.set_maximized(true);
|
||||||
|
|
||||||
|
let dh = self.wayland_display_handle.clone();
|
||||||
|
let seat = self.seat.clone().unwrap();
|
||||||
|
CoreSurface::add_to(
|
||||||
|
self.wayland_display_handle.clone(),
|
||||||
|
&window.wl_surface().unwrap(),
|
||||||
|
{
|
||||||
|
let window = window.clone();
|
||||||
|
move || {
|
||||||
|
let Some(wl_surface) = window.wl_surface() else {return};
|
||||||
|
let seat = seat.clone();
|
||||||
|
window.user_data().insert_if_missing_threadsafe(|| {
|
||||||
|
let panel_item = PanelItem::create(
|
||||||
|
Box::new(X11Backend {
|
||||||
|
toplevel_parent: None,
|
||||||
|
toplevel: window.clone(),
|
||||||
|
seat,
|
||||||
|
_pointer_grab: Mutex::new(None),
|
||||||
|
_keyboard_grab: Mutex::new(None),
|
||||||
|
}),
|
||||||
|
wl_surface
|
||||||
|
.client()
|
||||||
|
.and_then(|c| c.get_credentials(&dh).ok())
|
||||||
|
.map(|c| c.pid),
|
||||||
|
);
|
||||||
|
panel_item
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
move |_| {
|
||||||
|
let Some(panel_item) = window.user_data().get::<Arc<PanelItem<X11Backend>>>() else {return};
|
||||||
|
panel_item.toplevel_size_changed(
|
||||||
|
[
|
||||||
|
window.geometry().size.w as u32,
|
||||||
|
window.geometry().size.h as u32,
|
||||||
|
]
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
fn mapped_override_redirect_window(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "X map override redirect window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unmapped_window(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "Unmap X window");
|
||||||
|
}
|
||||||
|
fn destroyed_window(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
debug!(?window, "Destroy X window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn configure_request(
|
||||||
|
&mut self,
|
||||||
|
_xwm: XwmId,
|
||||||
|
window: X11Surface,
|
||||||
|
x: Option<i32>,
|
||||||
|
y: Option<i32>,
|
||||||
|
w: Option<u32>,
|
||||||
|
h: Option<u32>,
|
||||||
|
reorder: Option<Reorder>,
|
||||||
|
) {
|
||||||
|
debug!(?window, x, y, w, h, ?reorder, "Configure X window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn configure_notify(
|
||||||
|
&mut self,
|
||||||
|
_xwm: XwmId,
|
||||||
|
window: X11Surface,
|
||||||
|
geometry: Rectangle<i32, Logical>,
|
||||||
|
above: Option<Window>,
|
||||||
|
) {
|
||||||
|
debug!(?window, ?geometry, above, "Configure X window");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_request(&mut self, _xwm: XwmId, window: X11Surface, button: u32) {
|
||||||
|
let Some(panel_item) = self.panel_item(&window) else {return};
|
||||||
|
debug!(?window, button, "X window requests move");
|
||||||
|
panel_item.toplevel_move_request();
|
||||||
|
}
|
||||||
|
fn resize_request(
|
||||||
|
&mut self,
|
||||||
|
_xwm: XwmId,
|
||||||
|
window: X11Surface,
|
||||||
|
button: u32,
|
||||||
|
resize_edge: ResizeEdge,
|
||||||
|
) {
|
||||||
|
let Some(panel_item) = self.panel_item(&window) else {return};
|
||||||
|
debug!(?window, button, ?resize_edge, "X window requests resize");
|
||||||
|
let (up, down, left, right) = match resize_edge {
|
||||||
|
ResizeEdge::Top => (true, false, false, false),
|
||||||
|
ResizeEdge::Bottom => (false, true, false, false),
|
||||||
|
ResizeEdge::Left => (false, false, true, false),
|
||||||
|
ResizeEdge::TopLeft => (true, false, true, false),
|
||||||
|
ResizeEdge::BottomLeft => (false, true, true, false),
|
||||||
|
ResizeEdge::Right => (false, false, false, true),
|
||||||
|
ResizeEdge::TopRight => (true, false, false, true),
|
||||||
|
ResizeEdge::BottomRight => (false, true, false, true),
|
||||||
|
// _ => (false, false, false, false),
|
||||||
|
};
|
||||||
|
panel_item.toplevel_resize_request(up, down, left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fullscreen_request(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
let _ = window.set_fullscreen(true);
|
||||||
|
let Some(panel_item) = self.panel_item(&window) else {return};
|
||||||
|
panel_item.toplevel_fullscreen_active(true);
|
||||||
|
}
|
||||||
|
fn unfullscreen_request(&mut self, _xwm: XwmId, window: X11Surface) {
|
||||||
|
let _ = window.set_fullscreen(false);
|
||||||
|
let Some(panel_item) = self.panel_item(&window) else {return};
|
||||||
|
panel_item.toplevel_fullscreen_active(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct X11Backend {
|
||||||
|
pub toplevel_parent: Option<X11Surface>,
|
||||||
|
pub toplevel: X11Surface,
|
||||||
|
pub seat: Arc<SeatData>,
|
||||||
|
_pointer_grab: Mutex<Option<SurfaceID>>,
|
||||||
|
_keyboard_grab: Mutex<Option<SurfaceID>>,
|
||||||
|
}
|
||||||
|
impl X11Backend {
|
||||||
|
fn wl_surface_from_id(&self, id: &SurfaceID) -> Option<WlSurface> {
|
||||||
|
match id {
|
||||||
|
SurfaceID::Cursor => None,
|
||||||
|
SurfaceID::Toplevel => self.toplevel.wl_surface(),
|
||||||
|
SurfaceID::Child(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn flush_client(&self) {
|
||||||
|
// let Some(client) = self.toplevel.wl_surface().and_then(|s| s.client()) else {return};
|
||||||
|
// if let Some(client_state) = client.get_data::<ClientState>() {
|
||||||
|
// client_state.flush();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
impl Backend for X11Backend {
|
||||||
|
// fn start_data(&self, id: &str) -> Result<Message> {
|
||||||
|
// let size = (
|
||||||
|
// self.toplevel.geometry().size.w as u32,
|
||||||
|
// self.toplevel.geometry().size.h as u32,
|
||||||
|
// );
|
||||||
|
// let toplevel_state = (
|
||||||
|
// None::<String>,
|
||||||
|
// self.toplevel.title(),
|
||||||
|
// None::<String>,
|
||||||
|
// (
|
||||||
|
// self.toplevel.geometry().size.w as u32,
|
||||||
|
// self.toplevel.geometry().size.h as u32,
|
||||||
|
// ),
|
||||||
|
// self.toplevel.min_size().map(|s| (s.w as u32, s.h as u32)),
|
||||||
|
// self.toplevel.max_size().map(|s| (s.w as u32, s.w as u32)),
|
||||||
|
// ((0_i32, 0_i32), size),
|
||||||
|
// vec![0_u32; 0],
|
||||||
|
// );
|
||||||
|
// let info = (
|
||||||
|
// None::<(Vector2<u32>, Vector2<i32>)>,
|
||||||
|
// toplevel_state,
|
||||||
|
// Vec::<PopupData>::new(),
|
||||||
|
// None::<SurfaceID>,
|
||||||
|
// None::<SurfaceID>,
|
||||||
|
// );
|
||||||
|
// Ok(serialize((id, info))?.into())
|
||||||
|
// }
|
||||||
|
// fn serialize_toplevel(&self) -> Result<Message> {
|
||||||
|
// let toplevel_state = (
|
||||||
|
// None::<String>,
|
||||||
|
// self.toplevel.title(),
|
||||||
|
// None::<String>,
|
||||||
|
// (
|
||||||
|
// self.toplevel.geometry().size.w,
|
||||||
|
// self.toplevel.geometry().size.h,
|
||||||
|
// ),
|
||||||
|
// self.toplevel.min_size().map(|s| (s.w, s.h)),
|
||||||
|
// self.toplevel.max_size().map(|s| (s.w, s.w)),
|
||||||
|
// );
|
||||||
|
// let data = serialize(&toplevel_state)?;
|
||||||
|
// Ok(data.into())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn set_toplevel_capabilities(&self, _capabilities: Vec<u8>) {}
|
||||||
|
|
||||||
|
// fn set_toplevel_size(
|
||||||
|
// &self,
|
||||||
|
// size: Option<Vector2<u32>>,
|
||||||
|
// states: Vec<u32>,
|
||||||
|
// _bounds: Option<Vector2<u32>>,
|
||||||
|
// ) {
|
||||||
|
// let _ = self.toplevel.configure(
|
||||||
|
// size.map(|s| Rectangle::from_loc_and_size((0, 0), (s.x as i32, s.y as i32))),
|
||||||
|
// );
|
||||||
|
// let _ = self.toplevel.set_maximized(states.contains(&1));
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn start_data(&self) -> Result<PanelItemInitData> {
|
||||||
|
Ok(PanelItemInitData {
|
||||||
|
cursor: None,
|
||||||
|
toplevel: ToplevelInfo {
|
||||||
|
parent: None,
|
||||||
|
title: Some(self.toplevel.title()),
|
||||||
|
app_id: Some(self.toplevel.instance()),
|
||||||
|
size: [
|
||||||
|
self.toplevel.geometry().size.w as u32,
|
||||||
|
self.toplevel.geometry().size.h as u32,
|
||||||
|
]
|
||||||
|
.into(),
|
||||||
|
min_size: self
|
||||||
|
.toplevel
|
||||||
|
.min_size()
|
||||||
|
.map(|s| [s.w as u32, s.h as u32].into()),
|
||||||
|
max_size: self
|
||||||
|
.toplevel
|
||||||
|
.max_size()
|
||||||
|
.map(|s| [s.w as u32, s.h as u32].into()),
|
||||||
|
logical_rectangle: Geometry {
|
||||||
|
origin: [0, 0].into(),
|
||||||
|
size: [
|
||||||
|
self.toplevel.geometry().size.w as u32,
|
||||||
|
self.toplevel.geometry().size.h as u32,
|
||||||
|
]
|
||||||
|
.into(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
children: FxHashMap::default(),
|
||||||
|
pointer_grab: self._pointer_grab.lock().clone(),
|
||||||
|
keyboard_grab: self._keyboard_grab.lock().clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn close_toplevel(&self) {}
|
||||||
|
|
||||||
|
fn auto_size_toplevel(&self) {
|
||||||
|
let _ = self.toplevel.configure(None);
|
||||||
|
}
|
||||||
|
fn set_toplevel_size(&self, size: Vector2<u32>) {
|
||||||
|
let _ = self.toplevel.configure(Some(Rectangle {
|
||||||
|
loc: self.toplevel.geometry().loc,
|
||||||
|
size: (size.x as i32, size.y as i32).into(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
fn set_toplevel_focused_visuals(&self, focused: bool) {
|
||||||
|
let _ = self.toplevel.set_activated(focused);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_surface_material(&self, surface: SurfaceID, model_part: &Arc<ModelPart>) {
|
||||||
|
let Some(wl_surface) = self.wl_surface_from_id(&surface) else {return};
|
||||||
|
let Some(core_surface) = CoreSurface::from_wl_surface(&wl_surface) else {return};
|
||||||
|
|
||||||
|
core_surface.apply_material(model_part);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pointer_motion(&self, surface: &SurfaceID, position: Vector2<f32>) {
|
||||||
|
let Some(surface) = self.wl_surface_from_id(surface) else {return};
|
||||||
|
self.seat
|
||||||
|
.pointer_event(&surface, PointerEvent::Motion(position));
|
||||||
|
}
|
||||||
|
fn pointer_button(&self, surface: &SurfaceID, button: u32, pressed: bool) {
|
||||||
|
let Some(surface) = self.wl_surface_from_id(surface) else {return};
|
||||||
|
self.seat.pointer_event(
|
||||||
|
&surface,
|
||||||
|
PointerEvent::Button {
|
||||||
|
button,
|
||||||
|
state: if pressed { 1 } else { 0 },
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fn pointer_scroll(
|
||||||
|
&self,
|
||||||
|
surface: &SurfaceID,
|
||||||
|
scroll_distance: Option<Vector2<f32>>,
|
||||||
|
scroll_steps: Option<Vector2<f32>>,
|
||||||
|
) {
|
||||||
|
let Some(surface) = self.wl_surface_from_id(surface) else {return};
|
||||||
|
self.seat.pointer_event(
|
||||||
|
&surface,
|
||||||
|
PointerEvent::Scroll {
|
||||||
|
axis_continuous: scroll_distance,
|
||||||
|
axis_discrete: scroll_steps,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keyboard_keys(&self, surface: &SurfaceID, keymap_id: &str, keys: Vec<i32>) {
|
||||||
|
let Some(surface) = self.wl_surface_from_id(surface) else {return};
|
||||||
|
let keymaps = KEYMAPS.lock();
|
||||||
|
let Some(keymap) = keymaps.get(keymap_id).cloned() else {return};
|
||||||
|
if self.seat.set_keymap(keymap, vec![surface.clone()]).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for key in keys {
|
||||||
|
self.seat.keyboard_event(
|
||||||
|
&surface,
|
||||||
|
KeyboardEvent::Key {
|
||||||
|
key: key.abs() as u32,
|
||||||
|
state: if key < 0 { 1 } else { 0 },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user