289 Commits

Author SHA1 Message Date
UnderSampled
c6bc1f3ee8 Add support for specifying colorspace for Camera render 2023-10-14 12:28:27 -04:00
UnderSampled
27d8eb5f51 Merge branch 'develop' into camera-items 2023-10-11 00:22:48 -04:00
Nova
5c8563f165 fix(pointer): proper distance 2023-10-08 19:18:51 -04:00
Nova
98e2740116 feat: optimization 2023-10-08 18:44:52 -04:00
Nova
838ed9cbc0 fix(data): lesser key with empty vector being treated as different than typed vector 2023-10-07 13:38:06 -04:00
Nova
2f575954e9 fix: cargo.lock 2023-10-06 11:59:13 -04:00
UnderSampled
828a298476 Specify sRGB for Camera render texture 2023-10-06 11:59:13 -04:00
Nova
9058592ce1 fix(camera item): swap camera/projection matrix 2023-10-06 11:59:13 -04:00
Nova
c96eb14235 refactor(camera item): add todo warning 2023-10-06 11:59:13 -04:00
Nova
6fc1bb2afa refactor(camera item): pipe messageresponsesender through 2023-10-06 11:59:13 -04:00
UnderSampled
1ee485f3c5 Move BufferInfo to (experimental) Core 2023-10-06 11:59:13 -04:00
UnderSampled
9c62f4fd24 Import Dmabufs into CameraItems during render call. Consider replacing with a separate import_buffers call, or create buffers locally and expose export_buffers (slightly less state required) if this is too slow. 2023-10-06 11:58:28 -04:00
UnderSampled
a1f07f9260 Add camera render code for client buffers 2023-10-06 11:58:18 -04:00
UnderSampled
cd5fc51e98 Begin reworking camera items to accept external buffers and render requests 2023-10-06 11:58:18 -04:00
Nova
12328ef1e2 remove: sk.kmp 2023-10-01 21:54:56 -04:00
Nova
d1a67296fc fix: scroll 2023-10-01 21:47:13 -04:00
Nova
0d11a9ff03 fix(wayland): don't log key events :p 2023-10-01 01:35:52 -04:00
Nova
c35e44565f fix(wayland): keyboard input 2023-10-01 01:35:04 -04:00
Nova
a15045bba8 fix: use core version on git 2023-10-01 00:40:05 -04:00
Nova
13b9509d90 feat: proper ctrl+c stop 2023-09-29 00:26:53 -04:00
Nova
68d3fc309b feat: save client states on sigint 2023-09-28 12:08:45 -04:00
Nova
2f5584d545 feat(state): save state to ~/.local/state/stardust/<uid> on graceful disconnect 2023-09-28 10:31:23 -04:00
Nova
cb7f3ef4ac feat: client state (save/restore) 2023-09-28 09:55:45 -04:00
Nova
7095e1df84 refactor(core): remove dashmap 2023-09-28 01:30:26 -04:00
Nova
ee8f789f74 feat(suis/hand): distance per joint 2023-09-27 01:16:44 -04:00
Nova
1519acc799 feat: upgrade smithay 2023-09-25 22:38:00 -04:00
Nova
1d90657def feat(wayland): touch support 2023-09-24 11:44:26 -04:00
Nova
cf6285abe0 fix(objects/input): disable_controller still lets hands exist 2023-09-24 11:44:16 -04:00
Nova
9ae8efb9d6 fix(item): remove acceptor arg from release 2023-09-16 13:53:15 -04:00
Nova
6304541985 fix(panel): remove start data local signal 2023-09-16 13:52:53 -04:00
Nova
1babfdf10d fix(wayland): don't clone topleveldata 2023-09-16 13:52:31 -04:00
Nova
218089a21d fix(main): don't enable eye pointer for flatscreen mode with hands 2023-09-16 04:05:34 -04:00
Nova
120b0a246e refactor(wayland/seat): boolean for keypress instead of u32 2023-09-08 20:23:40 -04:00
Nova
a5b1370c3e fix(flake.lock): update flatland 2023-09-08 12:31:10 -04:00
Nova
c9edcaf281 fix(panel): remove unnecessary inverse global transform on startup settings 2023-09-08 12:28:55 -04:00
Nova
ce8e943c1f fix(flake.lock): update flatland 2023-09-08 00:37:15 -04:00
Nova
0dc5b1a92f feat: version bump 2023-09-04 17:12:42 -04:00
Nova
93d35e17e6 refactor: disable xwayland by default 2023-09-04 13:18:06 -04:00
Nova
b2c4e8e72c fix(xdg_shell): indicate fullscreen active 2023-09-04 13:17:48 -04:00
Nova
52af8095d6 fix(wayland): remove unwraps 2023-09-04 12:15:48 -04:00
Nova
102fc2ef79 fix(object/mouse pointer): update keyboard protocol 2023-09-04 11:40:44 -04:00
Nova
9042a004d3 fix(wayland): no more external dmabufs 2023-09-03 17:55:22 -04:00
Nova
5360230ca7 fix(wayland): closing toplevels 2023-09-03 11:02:42 -04:00
Nova
a4d3ec537f fix(wayland): new API 2023-09-03 10:38:31 -04:00
Nova
ba3faf2c76 feat(wayland+xwayland): initial window setup 2023-09-03 10:38:31 -04:00
Nova
aa3c892f39 refactor(panel item): nuke unnecessary functions 2023-09-03 10:38:31 -04:00
Nova
de0295fbf8 refactor: panel items 2023-09-03 10:38:31 -04:00
Nova
10034cdab2 feat: async methods 2023-09-02 19:49:53 -04:00
Stephen Christie
faf6a8f22a feat: Start converting method calls to async 2023-08-27 17:11:20 -04:00
matthewcroughan
821e4ae499 nix: refactor
- Remove unnecessary fenix input
- Remove unnecessary arguments to buildRustPackage
- Remove obsolete/unnecessary hacks
- Minimize code duplication and maximize re-use by using flake.parts
- Split out stardust-xr-server derivation into its own nix file in nix/stardust-xr-server
- Automatically get name of package from Cargo.toml
- Advertise support for riscv64-linux in flake outputs
2023-08-24 14:39:14 +01:00
Nova
38709f5eaf fix(data): ignore types for masks if one is null 2023-08-23 13:57:42 -04:00
Nova
601a140a70 fix: panel items not dropping on toplevel close 2023-08-14 03:05:22 -04:00
matthewcroughan
4976c1ce4f nix: add new runtime dependencies, xwayland and bash 2023-08-08 07:15:48 +01:00
matthewcroughan
d3e558c2b4 flake.lock: Update
Flake lock file updates:

• Updated input 'fenix':
    'github:nix-community/fenix/5816c7bbcc385d2e65877631497df3f7d66b354a' (2023-05-11)
  → 'github:nix-community/fenix/bd0c7ee0836a814751c3fcf66eaadfbe1a35b715' (2023-08-07)
• Updated input 'fenix/rust-analyzer-src':
    'github:rust-lang/rust-analyzer/b7cdd93f3e1533e96d4cfa1ac8573e6210a2bedf' (2023-05-09)
  → 'github:rust-lang/rust-analyzer/baee6b338b0ea076cd7a9f18d47f175dd2ba0e5d' (2023-08-06)
• Updated input 'flatland':
    'github:StardustXR/flatland/24613a496841bdf38e5f136608d5295860a75fce' (2023-05-11)
  → 'github:StardustXR/flatland/da6e286300c2a1f6e0ba103f9a79c53b9c3e70dc' (2023-08-06)
• Updated input 'hercules-ci-effects':
    'github:hercules-ci/hercules-ci-effects/15ff4f63e5f28070391a5b09a82f6d5c6cc5c9d0' (2023-04-19)
  → 'github:hercules-ci/hercules-ci-effects/0a63bfa3f00a3775ea3a6722b247880f1ffe91ce' (2023-07-15)
• Updated input 'hercules-ci-effects/flake-parts':
    'github:hercules-ci/flake-parts/c13d60b89adea3dc20704c045ec4d50dd964d447' (2023-03-09)
  → 'github:hercules-ci/flake-parts/8e8d955c22df93dbe24f19ea04f47a74adbdc5ec' (2023-07-04)
• Updated input 'hercules-ci-effects/flake-parts/nixpkgs-lib':
    'github:NixOS/nixpkgs/130fa0baaa2b93ec45523fdcde942f6844ee9f6e?dir=lib' (2023-03-09)
  → 'github:NixOS/nixpkgs/4bc72cae107788bf3f24f30db2e2f685c9298dc9?dir=lib' (2023-06-29)
• Updated input 'hercules-ci-effects/hercules-ci-agent':
    'github:hercules-ci/hercules-ci-agent/0b90d1a87c117a5861785cb85833dd1c9df0b6ef' (2023-03-10)
  → 'github:hercules-ci/hercules-ci-agent/367dd8cd649b57009a6502e878005a1e54ad78c5' (2023-07-05)
• Updated input 'hercules-ci-effects/hercules-ci-agent/flake-parts':
    'github:hercules-ci/flake-parts/c13d60b89adea3dc20704c045ec4d50dd964d447' (2023-03-09)
  → 'github:hercules-ci/flake-parts/8e8d955c22df93dbe24f19ea04f47a74adbdc5ec' (2023-07-04)
• Updated input 'hercules-ci-effects/hercules-ci-agent/haskell-flake':
    'github:hercules-ci/haskell-flake/1e1660e6dd00838ba73bc7952e6e73be67da18d1' (2023-03-06)
  → 'github:srid/haskell-flake/74210fa80a49f1b6f67223debdbf1494596ff9f2' (2023-05-22)
• Removed input 'hercules-ci-effects/hercules-ci-agent/nix-darwin'
• Removed input 'hercules-ci-effects/hercules-ci-agent/nix-darwin/nixpkgs'
• Updated input 'hercules-ci-effects/hercules-ci-agent/nixpkgs':
    'github:NixOS/nixpkgs/c90c4025bb6e0c4eaf438128a3b2640314b1c58d' (2023-03-08)
  → 'github:NixOS/nixpkgs/0fbe93c5a7cac99f90b60bdf5f149383daaa615f' (2023-07-02)
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/flake-compat'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/flake-utils'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/gitignore'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/gitignore/nixpkgs'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/nixpkgs'
• Removed input 'hercules-ci-effects/hercules-ci-agent/pre-commit-hooks-nix/nixpkgs-stable'
• Updated input 'hercules-ci-effects/nixpkgs':
    'github:NixOS/nixpkgs/1544ef240132d4357d9a39a40c8e6afd1678b052' (2023-03-15)
  → 'github:NixOS/nixpkgs/27fcd46fa18df36d270174246e7bd8f1787129ff' (2023-07-15)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/897876e4c484f1e8f92009fd11b7d988a121a4e7' (2023-05-06)
  → 'github:NixOS/nixpkgs/5a8e9243812ba528000995b294292d3b5e120947' (2023-08-07)
2023-08-08 07:15:43 +01:00
Nova
a525b2c39b fix(panel): pressed/released in the right order 2023-08-07 21:44:44 -04:00
Nova
b7cb0b747b fix(fields): remove radius from normal/closest point 2023-08-07 16:45:37 -04:00
Nova
d56091472e fix: controller 2023-08-06 19:27:40 -04:00
Nova
8c2c1b0c14 refactor(wayland): replace popups with child surfaces 2023-08-06 18:32:43 -04:00
Nova
2c979c84c4 fix: properly destroy xwayland 2023-08-06 11:19:42 -04:00
Nova
2c3e4bd135 feat: camera item 2023-08-06 10:11:06 -04:00
Nova
4616b9b3bd feat(registry): is_empty 2023-08-06 10:11:01 -04:00
Nova
0257655eba refactor(drawable): remove sendwrapper 2023-08-06 10:10:50 -04:00
Nova
ccdab7578a fix(main): don't pass through std anything from child processes 2023-08-05 20:20:57 -04:00
Nova
fc2abfe59c feat(wayland): cleanup 2023-08-04 21:20:32 -04:00
Nova
7282684104 feat: fd passing 2023-07-31 23:44:17 -04:00
Nova
dac3a3092a refactor(objects): overhaul input 2023-07-26 08:56:34 -04:00
Nova
4ffab6580d refactor (wayland): move seat to client 2023-07-25 14:46:03 -04:00
Nova
1047f5242b refactor: trait away panel item backends 2023-07-23 19:59:35 -04:00
Nova
eedf5446e8 feat(xwayland): xwayland feature 2023-07-23 09:04:22 -04:00
Nova
824b1bdd26 feat: todo fuure plans 2023-07-23 08:30:09 -04:00
Nova
814d631bb8 fix: deadlock on close stereokit 2023-07-23 08:30:09 -04:00
Nova
fe28d6670e refactor(wayland): less crashy 2023-07-23 08:30:09 -04:00
Nova
37413a3e74 feat(xwayland): x window capabilities 2023-07-23 08:30:09 -04:00
Nova
8ad5fc6584 feat(xwayland): first x window 2023-07-23 08:30:09 -04:00
Nova
06dfba48d7 feat(xwayland): serialize start 2023-07-23 08:30:09 -04:00
Nova
f7ad007d54 refactor(wayland): separate backend from panel item 2023-07-23 08:30:09 -04:00
Nova
7a556439ca feat(wayland): initial xwayland support 2023-07-23 08:30:09 -04:00
Nova
945514aa31 fix: data uses flexbuffer type instead of value for mask map 2023-07-23 08:29:53 -04:00
Nova
3b557cd851 fix: wayland stability 2023-07-23 08:29:04 -04:00
Nova
95ecef3426 feat: version bump 2023-07-22 18:31:09 -04:00
Nova
a85504a71a fix: states 2023-07-19 06:04:15 -07:00
Nova
eb2ace5d29 fix: surface not mapping 2023-07-19 06:04:08 -07:00
Nova
1923b71985 feat: formatting 2023-07-19 06:03:28 -07:00
Nova
a87823dcc8 feat: play space 2023-07-16 10:42:35 -07:00
technobaboo
1c0290aff5 feat: make readme more readable 2023-07-11 11:44:38 -07:00
technobaboo
e131b2b00f fix(ci): add semicolons 2023-07-11 11:16:15 -07:00
technobaboo
728573191f fix(ci): appimagetool 2023-07-11 11:11:07 -07:00
technobaboo
cee1137269 fix(ci): xcb glx 2023-07-11 10:59:26 -07:00
technobaboo
c6af3298a2 fix(ci): ninja-build instead of ninja 2023-07-11 10:55:08 -07:00
technobaboo
11074c9d05 feat: ci take 2 2023-07-11 10:53:31 -07:00
Nova
aa6ec1afd8 refactor: use dmabuf v4 instead of bind_display 2023-06-27 05:53:45 -04:00
Nova
a524f2020b refactor: disable shader injection 2023-06-26 20:49:21 -04:00
Nova
e2ce337036 feat: match stereokit to log level 2023-06-26 20:43:02 -04:00
Nova
34c57254a9 fix: unwrap in main fn 2023-06-26 20:33:04 -04:00
Nova
d7bf01c417 feat: hardware accelerated wayland apps 2023-06-26 20:31:38 -04:00
Nova
59d049e7bd feat: proper dmabuf import 2023-06-26 20:09:20 -04:00
Nova
c05921fd71 feat: shaders!! working!! 2023-06-26 19:49:10 -04:00
Nova
5cbd4d807f feat: it borken 2023-06-26 04:37:38 -04:00
Nova
23fe6ead25 feat: glsl simula text shaders 2023-06-25 10:05:18 -04:00
Nova
b3247f5ba8 fix: ctrl+c in tty 2023-06-21 01:47:10 -04:00
Nova
269e5afcc9 fix: mouse pointer keyboard ray direction 2023-06-14 23:00:17 -04:00
Nova
20070eb778 feat: version bump 2023-06-11 01:37:21 -04:00
Nova
c8206b14dd feat: spatial bounds 2023-06-11 00:38:05 -04:00
Nova
e9089e83a9 feat(input): custom pointers 2023-05-31 08:50:15 -04:00
Nova
34faa98dc8 fix(node): better send remote signal 2023-05-31 08:48:59 -04:00
Nova
187d6ee7d1 fix(pointer): proper direction 2023-05-31 08:48:24 -04:00
Nova
f7a9976760 fix(main): make eye pointer not work in flatscreen 2023-05-31 08:47:16 -04:00
Nova
f42c8963e7 fix(scenegraph): recurse through aliases 2023-05-31 08:47:02 -04:00
Nova
f17b4c8ee2 feat: readd dmabufs 2023-05-30 02:20:27 -04:00
Nova
47410fe63a fix: janky dmabuf hack 2023-05-27 09:48:34 -04:00
Nova
04d0a77093 feat: eye gaze support 2023-05-23 18:56:46 -04:00
matthewcroughan
97c2b82fa7 ci: print flatland revision for gnome-graphical-test in Discord message 2023-05-20 11:08:43 +01:00
matthewcroughan
343962fd6f github: remove workflows
Since Hercules CI is in use now, GitHub Actions are not necessarily required
2023-05-20 11:08:43 +01:00
matthewcroughan
2c07278c72 flake: add hercules-ci
This commit also adds a HCI Effect for posting to Discord the result of the gnome-graphical-test on every single commit
2023-05-20 11:08:43 +01:00
matthewcroughan
bdadc527d2 nix: add graphical-gnome-test
This VM integration test spawns Gnome, monado-service,
stardust-xr-server, flatland and weston-cliptest and tests that the
functionality works correctly. The result is a screenshot. If any
program in the test produces an exit code above 0 it will fail the test,
graphical rendering bugs should be visible in the resulting screenshot
2023-05-20 11:08:43 +01:00
matthewcroughan
7045904693 flake.lock: Update
Flake lock file updates:

• Updated input 'fenix':
    'github:nix-community/fenix/3a0b59a2ea946a533c62ac417596835779087f0e' (2023-04-20)
  → 'github:nix-community/fenix/5816c7bbcc385d2e65877631497df3f7d66b354a' (2023-05-11)
• Updated input 'fenix/rust-analyzer-src':
    'github:rust-lang/rust-analyzer/2400b36a2ed40f68a26473f69ac208ba10d98af9' (2023-04-19)
  → 'github:rust-lang/rust-analyzer/b7cdd93f3e1533e96d4cfa1ac8573e6210a2bedf' (2023-05-09)
• Added input 'flatland':
    'github:StardustXR/flatland/24613a496841bdf38e5f136608d5295860a75fce' (2023-05-11)
• Added input 'flatland/fenix':
    'github:nix-community/fenix/ee59e1c769657b1e27e608f8b981fa8f6b715583' (2023-03-14)
• Added input 'flatland/fenix/nixpkgs':
    follows 'flatland/nixpkgs'
• Added input 'flatland/fenix/rust-analyzer-src':
    'github:rust-lang/rust-analyzer/95497533524537b1cc7a2870ce94b0b14503be8b' (2023-03-13)
• Added input 'flatland/nixpkgs':
    'github:NixOS/nixpkgs/67f26c1cfc5d5783628231e776a81c1ade623e0b' (2023-03-13)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/da45bf6ec7bbcc5d1e14d3795c025199f28e0de0' (2023-04-30)
  → 'github:NixOS/nixpkgs/897876e4c484f1e8f92009fd11b7d988a121a4e7' (2023-05-06)
2023-05-20 11:08:43 +01:00
matthewcroughan
c45dc36310 flake: filter nix code and README out of src
This means that changing the Nix code doesn't cause the Rust code to need to be recompiled when using nix build
2023-05-20 11:08:43 +01:00
Nova
ed724dcb0f feat: appimage support!! 2023-05-19 18:12:22 -04:00
Nova
c4234becb9 feat: clean up main function 2023-05-19 18:11:52 -04:00
Nova
cac2be7a4f feat: flat wayland display env var 2023-05-18 06:00:53 -04:00
Nova
84ba4b47ec fix: better pointer compare distance algorithm 2023-05-10 23:44:36 -04:00
Nova
aea681b5c6 fix(model): copy on create to make unique 2023-05-10 23:44:23 -04:00
Nova
7c17dbddb2 feat: instant model loading 2023-05-10 20:10:31 -04:00
Nova
3f56d1e029 fix(wayland): stop crash 2023-05-10 19:14:57 -04:00
Nova
2a145de758 fix: remove dbg statement 2023-05-10 19:02:09 -04:00
Nova
54ad2667c3 fix: upgrade smithay 2023-05-10 16:51:39 -04:00
Nova
fa6ea5adc5 fix: model nodes 2023-05-10 16:49:56 -04:00
Nova
ee5a226c5e feat: model nodes 2023-05-10 08:38:21 -04:00
Matthew Croughan
3e0860073d fix: nix flake smithay lock issues
* flake: use allowBuiltInFetchGit to prevent narHash reproducibility issues

using the builtin fetcher allows fetching git dependencies with only the ref and without storing the narHash for the fixed-output-derivation

* gitignore: add nix result symlinks
2023-05-06 17:37:46 +00:00
Nova
ec7f9a1681 feat: order inputs 2023-05-02 21:58:53 -04:00
Astavie
779431be04 fix: nix overlay
* nix flake

* workflow

* remove flake-utils

* update flake

* fix

* remove cargo hash

* fix overlay
2023-05-01 23:29:34 +00:00
Astavie
683b0c20a1 fix: nix flake
* nix flake

* workflow

* remove flake-utils

* update flake

* fix

* remove cargo hash
2023-05-01 21:56:49 +00:00
Nova King
341da038ea feat: FUNDING.yml 2023-05-01 17:31:03 +00:00
Nova
367066381d better panel item startup settings order 2023-05-01 12:59:49 -04:00
Nova
2f5760c902 fix: launch env vars to launch as much stuff in wayland as possible 2023-05-01 00:05:00 -04:00
Nova
9072c34d50 fix(objects/hand): hand enabled when controller not 2023-04-30 18:28:40 -04:00
Nova
aefa6dc62f feat: new stereokit 2023-04-30 13:25:13 -04:00
Saphira Kai
35f42559ac remove broken Debug derivation for XdgSurfaceData 2023-04-24 13:12:44 -03:00
Nova
63ead46a2c feat(startup): get environment 2023-04-24 09:53:20 -04:00
Nova
5682718713 feat: cargo lock update 2023-04-24 08:31:28 -04:00
Nova
58b9ee0f01 feat: dependency updates 2023-04-24 08:31:07 -04:00
Nova
57039a3ccb feat(wayland): popups, more compatibility, more stability
get_parent


grab


popups

fix head thingy


popup list


feat: remove set_active

feat(wayland): commit_popup

feat(wayland): cleanup


moar changess


actually fix the problem with everything oh my god


proper popup state


fix: multi thread event loop


fix: match popup surface ID


make wayland input system go over surfaces instead of toplevels


feat: massive refactor of all wayland things
2023-04-24 06:30:39 -04:00
Nova
0fec416731 fix: mouse pointer 2023-04-23 09:34:43 -04:00
Nova
f2c9f91a6b feat: custom startup script 2023-04-23 09:34:43 -04:00
Astavie
aef78f12b5 fix: nix flake
* nix flake

* workflow

* remove flake-utils

* update flake
2023-04-20 10:35:50 +00:00
Nova
c19a5bcd91 feat: desktop file 2023-03-25 03:06:50 -04:00
Nova
de3ada1b5d feat: upgrade stereokit-rs 2023-03-23 14:12:48 -04:00
Astavie
d95b3f55e5 feat: nix support & github workflow
* nix flake

* workflow

* remove flake-utils
2023-03-14 19:35:40 +00:00
Nova
643c4959fb fix: unignore cargo.lock 2023-03-13 13:44:12 -07:00
Nova
1b055c9406 fix(input/hand): correct serialization transform matrix order 2023-03-08 01:45:03 -05:00
Nova
42782d0f87 feat(input): new system 2023-02-25 16:39:30 -05:00
Nova
b5fc321e36 refactor(input): make all inputs have nodes 2023-02-24 11:43:06 -05:00
Nova
931f857458 feat: update everything, clean dependencies 2023-02-23 08:41:40 -05:00
Nova
8ecdd3acc6 refactor(model): remove shader use 2023-02-23 07:17:36 -05:00
Nova
951ca7c4db fix(model): make default pbr shader clip 2023-02-20 18:03:02 -05:00
Nova
fee12854ca fix(items): proper drop 2023-02-20 10:24:59 -05:00
Nova
14b9129fca fix(mouse pointer): keyboard 2023-02-18 02:06:17 -05:00
Nova
55ac288391 fix(cargo.toml): upgrade stereokit 2023-02-17 13:10:23 -05:00
Nova
0c15f54bdc fix(mouse pointer): proper pointer transform 2023-02-17 13:10:08 -05:00
Nova
c42f69cb69 fix: order of operations on wayland material properties 2023-02-16 14:03:13 -05:00
Nova
7818ae4add fix(item): send acceptors to new item ui 2023-02-16 14:02:43 -05:00
Nova
c09d565dc5 fix: update stereokit 2023-02-16 14:02:21 -05:00
Nova
9076a0b897 feat: disabled/enabled 2023-02-16 00:30:25 -05:00
Nova
37f283b393 fix: node aspect drawable 2023-02-09 03:58:56 -05:00
Nova
40e7c6cd20 refactor: node aspect drawable 2023-02-08 21:06:24 -05:00
Nova
4620e0ca09 fix: cargo fmt 2023-02-07 18:04:20 -05:00
Nova
33d12787c9 fix(dev profile): optimization level 0 2023-02-07 16:15:37 -05:00
Nova
d7b895f0f4 feat(model): pbr clip shader 2023-02-07 16:15:20 -05:00
awtterpip
fb027de390 fixed bug where sound wasn't stoppable 2023-02-07 10:34:10 -06:00
awtterpip
19c0ec42ac gave audio interface proper name 2023-02-07 09:13:55 -06:00
piper
baea2c71a3 feat: audio! 2023-02-04 20:39:08 -05:00
Nova
0694848c9b fix(stereokit): version 2023-02-04 20:38:10 -05:00
Nova
fd95988a42 refactor(cargo.toml): profile optimizations 2023-02-02 16:32:43 -05:00
Nova
24d8fefcaa feat(spatial): fields_distance, normal, closest_point 2023-02-01 19:21:35 -05:00
Nova
a434c2aa55 fix(items): make acceptor fields non-optional 2023-01-28 11:12:51 -05:00
Nova
c46e01e280 refactor: change logic_step to frame event 2023-01-26 09:08:40 -05:00
Nova
529b86fa91 refactor: remove many unwrap calls 2023-01-25 11:50:53 -05:00
Nova
b5e87d5911 feat(material): auto copy on change parameter 2023-01-25 09:23:01 -05:00
Nova
063be773e0 feat: make event loop multithreaded 2023-01-25 09:17:52 -05:00
Nova
3dadbccfff fix(spatial): moving object relative to itself 2023-01-25 05:40:37 -05:00
Nova
cbf5af88b9 fix(spatial): parse_transform returned result 2023-01-25 05:40:19 -05:00
Nova
f46e870864 refactor(wayland): remove SeatData wrapper 2023-01-22 02:38:40 -05:00
Nova
2358ee899a fix(wayland): update pointer scroll 2023-01-22 02:30:12 -05:00
Nova
4f50772b92 fix(panel_item): allow surfaces with size of 0,0 2023-01-22 00:58:25 -05:00
Nova
c52472fe3c refactor(spatial): get/set parent methods 2023-01-22 00:42:04 -05:00
Nova
52011bd92f fix(spatial): reference space can be self 2023-01-22 00:24:38 -05:00
Nova
be97de8a2e feat: update/clean dependencies 2023-01-22 00:24:13 -05:00
Nova
e9e4d3599d feat: input multiplexing 2023-01-21 18:07:25 -05:00
Nova
6d7d7be3f1 feat: startup script 2023-01-17 18:51:46 -05:00
Nova
d5a951d3c6 feat(spatial): keep track of children 2023-01-16 11:17:12 -05:00
Nova
1b0ef4b7df refactor(main): make arrays tuples 2023-01-16 11:03:46 -05:00
Nova
66879148c6 feat: update stereokit to have fancy tracing 2023-01-15 04:23:26 -05:00
Nova
7108cfa365 fix(input): grab issues 2023-01-15 04:04:09 -05:00
Nova
05e60c43ce feat(stereokit): log filtering 2023-01-15 04:03:21 -05:00
Nova
75359427c9 feat: even more tracing 2023-01-15 01:13:22 -05:00
Nova
ba938dfaf9 feat: adaptive sleep delay 2023-01-14 23:48:49 -05:00
Nova
c3721872e0 feat: spatial tracing 2023-01-14 22:59:00 -05:00
Nova
73f3d99877 feat: span tracing!!! 2023-01-14 22:32:41 -05:00
Nova
1a5edee751 refactor(input): more compact registry contains 2023-01-14 20:29:33 -05:00
Nova
27d75d3098 fix(event loop, client): better async 2023-01-14 12:38:05 -05:00
Nova
198b342dec refactor(cargo.toml): remove unneeded deps and features 2023-01-14 11:17:52 -05:00
Nova
457ff55904 fix(input): reduce latency by several frames 2023-01-14 11:03:47 -05:00
Nova
795780130c feat(tokio): profiling 2023-01-14 10:38:39 -05:00
Nova
7a69d64e46 refactor(wayland): remove commented out code\ 2023-01-07 10:15:56 -05:00
Nova
5350adef98 feat(model): use resource ID for texture 2023-01-06 09:05:30 -05:00
Nova
1ae7e4afaa fix: remove stereokit patch 2023-01-05 21:49:03 -05:00
Nova
e0e5f3b3bc feat(model): set material parameter 2023-01-05 21:46:25 -05:00
Nova
d1522a05e8 fix(drawable/lines): properly make cyclic point 2023-01-05 08:28:29 -05:00
Nova
4f1d03196a fix: remove opt level 3 for dev 2023-01-05 07:59:20 -05:00
Nova
3cf65e5c7b feat: update stereokit 2023-01-04 23:51:48 -05:00
Nova
3f08e1e212 feat(wayland/surface): geometry resizing, unused 2023-01-04 21:36:55 -05:00
Nova
a0d97c8385 feat(delta): mark_changed 2023-01-04 21:36:25 -05:00
Nova
95629422a7 fix(wayland): SSD all the things 2023-01-04 08:26:10 -05:00
Nova
46b3d21fca fix(wayland): toplevel states bytemucked to u8 2023-01-04 08:25:54 -05:00
Nova
2ce9bf8cd4 feat(wayland): proper surface geometry 2023-01-04 07:25:33 -05:00
Nova
1f745fddde feat(wayland): serial counter 2023-01-04 07:25:22 -05:00
Nova
45998df389 feat(delta): const 2023-01-04 07:23:23 -05:00
Nova
fe22451be5 feat(wayland): set toplevel capabilities 2023-01-03 10:08:39 -05:00
Nova
57ea51fb40 feat(wayland): recommended_state 2023-01-02 18:53:58 -05:00
Nova
2d0865e73c refactor(wayland): remove xdg output manager 2023-01-02 03:49:29 -05:00
Nova
8556046b25 refactor(wayland): comment out xdg activation protocol 2023-01-02 03:43:50 -05:00
Nova
c0c0dddf23 fix(wayland): drop panel item correctly 2023-01-01 14:37:11 -05:00
Nova
c2665938d9 feat(wayland): switch pointer focus dynamically 2023-01-01 14:36:46 -05:00
Nova
6b7a074fea fix(wayland): set seat cursor 2023-01-01 14:34:18 -05:00
Nova
bdd5cbe8b0 fix(wayland): xdg surface size when not set 2023-01-01 14:33:07 -05:00
Nova
6068ad5089 feat(wayland/xdg_shell): set surface states None 2022-12-26 11:15:37 -05:00
Nova
2de49d981b feat(wayland): make state fields optional 2022-12-26 08:22:09 -05:00
Nova
246cf5ceea fix(wayland): panel item configure toplevel 2022-12-25 16:19:22 -05:00
Nova
17583471bf feat(wayland): configure and commit for toplevel] 2022-12-25 16:01:23 -05:00
Nova
868621a68c feat(input): allow scaling input handlers 2022-12-21 05:34:34 -05:00
Nova
769fa24c1d feat: update stardust-xr 2022-12-17 02:29:32 -05:00
Nova
89f4aae1ff refactor(data): identical values for mask 2022-12-17 02:28:06 -05:00
Nova
aa79cb2147 refactor(startup): auto acceptor on item add to 2022-12-10 10:26:26 -05:00
Nova
0cb039c1be refactor(startup): generate startup token 2022-12-10 09:46:47 -05:00
Nova
697320d1c0 refactor(startup): rename to STARTUP_SETTINGS 2022-12-10 09:27:37 -05:00
Nova
9f5ad2b9af feat(startup): automatic acceptors 2022-12-10 09:18:02 -05:00
Nova
2315aca8b5 feat(startup settings): use /proc/{pid}/environ 2022-12-10 09:17:37 -05:00
Nova
004454d75e fix(wayland): dmabuf formats 2022-12-10 00:30:53 -05:00
Nova
4286bc119a refactor(wayland): remove manual dmabuf importing 2022-12-09 07:04:50 -05:00
Nova
e7f7f1f062 fix: drain all dmabufs 2022-12-08 05:40:54 -05:00
Nova
226d674365 feat: optimization level 3 for debug 2022-12-07 14:47:25 -05:00
Nova
0dc9dc23e9 feat(wayland): dmabuf 2022-12-07 14:30:48 -05:00
Nova
48719c9862 feat(resources): list of extensions to check 2022-12-05 22:44:04 -05:00
Nova
125ab4f74d refactor(sk_hand): use snake case for datamap keys 2022-12-03 17:18:27 -05:00
Nova
42d2a41d57 feat: tracing 2022-12-02 20:46:28 -05:00
Nova
692ee95500 switch to color_eyre instead of anyhow 2022-12-02 13:58:54 -05:00
Nova
9ec6d31d67 fix(input): O(n log n) instead of O(n^2) 2022-12-02 11:09:23 -05:00
Nova
e30ad3957a fix(items): give aliases a proper lifetime 2022-11-30 22:34:16 -05:00
Nova
35e6e3f1f9 fix(alias): make output optional 2022-11-30 07:04:06 -05:00
Nova
597cadd9f2 fix(lines): use sRGB colors 2022-11-28 00:32:41 -05:00
Nova
358aaa917a fix(lines): convert f32 to u8 colors correctly 2022-11-26 15:22:23 -05:00
Nova
6ef154e9b3 fix(lines): accept f32 colors 2022-11-26 00:43:04 -05:00
Nova
5874eaef78 fix: text not working 2022-11-26 00:34:37 -05:00
Nova
5b15df42c3 fix(wayland): update smithay version 2022-11-25 23:58:22 -05:00
Nova
c10735acd1 feat(fields): torus field 2022-11-24 18:57:11 -05:00
Nova
2cb000c445 refactor(fields): use let else for getting field 2022-11-24 18:56:59 -05:00
Nova
77bc08a78b feat(drawable): lines 2022-11-21 16:39:28 -05:00
Nova
fe4930e919 refactor(items): move capture to item acceptor 2022-11-20 13:34:15 -05:00
Nova
d3653e17cb feat(items): acceptor 2022-11-19 11:25:10 -05:00
Nova
418e9fdc54 fix(wayland): cursor material queue higher 2022-11-14 11:53:08 -05:00
Nova
bd62e7d342 refactor(stereokit): upgrade version 2022-11-14 09:05:36 -05:00
Nova
9da111a3e0 refactor(wayland): make code cleaner 2022-11-12 11:53:11 -05:00
Nova
b278a43d6d fix: upgrade rust version 2022-11-11 13:25:57 -05:00
Nova
892a4165f8 fix(wayland): pointer_motion works when inactive 2022-11-11 13:25:48 -05:00
Nova
088e8e2c9c fix(wayland): remove unwraps 2022-11-11 12:52:51 -05:00
Nova
043a1a1b29 refactor(wayland): s/ObjectId/Weak<WlSurface>/ 2022-11-09 13:05:21 -05:00
Nova
41e76c9d0a feat: wayland feature 2022-11-09 11:13:07 -05:00
Nova
6b578fe044 fix(wayland): account for surface data map panic 2022-11-09 11:02:48 -05:00
Nova
e2b00f23ee fix(panel item): set cursor full of snake 2022-11-08 21:16:03 -05:00
Nova
215b91a4bd refactor(items): genericize item acceptors/ui 2022-11-08 20:25:43 -05:00
Nova
9e0ba504ac fix: make aliased signals snake case 2022-11-08 20:17:15 -05:00
Nova
3cdb6da09c refactor: use snake case for method names 2022-11-08 06:10:03 -05:00
Nova
3821e14fbd fix(wayland): upgrade smithay version 2022-11-06 16:49:30 -05:00
Nova
e3e7714f1a feat(objects/input): add keyboard to mouse_pointer 2022-11-05 17:56:52 -04:00
Nova
cdbfc27792 refactor(data): simplify 2022-11-05 17:56:34 -04:00
Nova
cbb09a9d97 refactor: remove item alias remote_methods 2022-11-05 17:56:09 -04:00
Nova
12c2443e5d feat(field): ray_march 2022-11-05 17:55:27 -04:00
Nova
8b38f61e35 feat(field): expose ray marching to clients 2022-11-01 07:59:49 -04:00
Nova
fad9b7d50c refactor(client): use new messenger 2022-10-30 00:14:24 -04:00
Nova
60f642364f refactor(node): return Result<&T> from get aspect 2022-10-29 07:32:51 -04:00
Nova
454318b0af feat: terrible hack for moses 2022-10-26 05:19:20 -04:00
Nova
4e858e4212 fix(wayland): remove wayland crate pinning 2022-10-25 16:21:22 -04:00
Nova
e4d0159572 refactor: use master smithay branch 2022-10-25 16:07:36 -04:00
Nova
8ea05e998d fix(wayland): set default output size 2022-10-25 12:56:59 -04:00
Nova
af3833b263 fix(data): send "data" to receiver 2022-10-25 07:32:25 -04:00
Nova
94a78314bb feat: pulse sender/receiver 2022-10-21 06:21:56 -04:00
Nova
a572b215b9 fix: clippy 2022-10-21 06:21:49 -04:00
Nova
b189a6a73c feat: zones 2022-10-20 11:32:33 -04:00
Nova King
44b91bf0e8 Merge pull request #2 from philpax/fix-build
fix: remove path dependency for stereokit
2022-10-19 05:55:53 +00:00
72 changed files with 5044 additions and 3312 deletions

46
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,46 @@
name: Build
on:
push:
branches:
- '*'
jobs:
build_and_package:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install runtime dependencies
run: sudo apt install -y --no-install-recommends libxkbcommon-dev libstdc++6 libopenxr-dev libx11-dev libxfixes-dev libgl1-mesa-dev libegl1-mesa-dev libgbm-dev libfontconfig-dev libjsoncpp-dev libxcb1-dev libglx-dev libxcb-glx0-dev libdrm-dev libwayland-dev libfreetype-dev libpng-dev
- name: Install build dependencies
run: sudo apt install -y --no-install-recommends cmake ninja-build
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Build server
run: cargo build --release
- name: Install appimagetool
run: |
wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-$(uname -m).AppImage -O /usr/local/bin/appimagetool; \
chmod +x /usr/local/bin/appimagetool; \
sed -i 's|AI\x02|\x00\x00\x00|' /usr/local/bin/appimagetool
- name: Install cargo-appimage
run: cargo install cargo-appimage
- name: Generate AppImage
run: cargo appimage
- name: Upload AppImage
uses: actions/upload-artifact@v2
with:
name: appimage
path: '*.AppImage'

3
.gitignore vendored
View File

@@ -11,4 +11,5 @@
*result*
/libs/
*.AppImage
*.AppImage
*.blend1

1377
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "stardust-xr-server"
version = "0.42.0"
version = "0.43.0"
authors = ["Nova King <technobaboo@proton.me>"]
description = "Stardust XR reference display server"
license = "GPLv2"
@@ -15,8 +15,9 @@ path = "src/main.rs"
[features]
default = ["wayland"]
wayland = ["dep:smithay", "dep:xkbcommon"]
xwayland = ["smithay/xwayland"]
profile_tokio = ["dep:console-subscriber", "tokio/tracing"]
profile_app = ["dep:tracing-chrome"]
profile_app = ["dep:tracing-tracy"]
[package.metadata.appimage]
auto_link = true
@@ -38,7 +39,6 @@ lto = true
[dependencies]
color-eyre = { version = "0.6.2", default-features = false }
clap = { version = "4.2.4", features = ["derive"] }
dashmap = "5.4.0"
glam = { version = "0.23.0", features = ["mint"] }
lazy_static = "1.4.0"
mint = "0.5.9"
@@ -47,45 +47,56 @@ 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"] }
tokio = { version = "1.27.0", features = ["rt-multi-thread", "signal", "time"] }
send_wrapper = "0.6.0"
prisma = "0.1.1"
xkbcommon = { version = "0.5.0", default-features = false, optional = true }
stardust-xr = "0.11.4"
directories = "5.0.0"
serde = { version = "1.0.160", features = ["derive"] }
serde_repr = "0.1.16"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
global_counter = "0.2.2"
rand = "0.8.5"
atty = "0.2.14"
xkbcommon = { version = "0.6.0", default-features = false, optional = true }
ctrlc = "3.4.1"
libc = "0.2.148"
input-event-codes = "5.16.8"
drm-fourcc = { version = "2.2.0", features = ["serde"] }
[dependencies.smithay]
# git = "https://github.com/technobaboo/smithay.git" # Until we get stereokit to understand OES samplers and external textures
git = "https://github.com/smithay/smithay.git" # Until we get stereokit to understand OES samplers and external textures
# path = "../smithay"
default-features = false
features = [
"desktop",
"backend_drm",
"backend_egl",
"renderer_gl",
"wayland_frontend",
]
version = "*"
optional = true
[dependencies.stereokit]
default-features = false
features = ["linux-egl"]
version = "0.16.7"
[dependencies.smithay]
git = "https://github.com/technobaboo/smithay.git" # Until we get stereokit to understand OES samplers and external textures
# git = "https://github.com/smithay/smithay.git" # Until we get stereokit to understand OES samplers and external textures
# path = "../smithay"
default-features = false
features = ["desktop", "renderer_gl", "wayland_frontend"]
version = "*"
optional = true
version = "0.16.9"
[dependencies.console-subscriber]
version = "0.1.8"
optional = true
[dependencies.tracing-chrome]
version = "0.7.1"
[dependencies.tracing-tracy]
version = "0.10.4"
optional = true
[dependencies.stardust-xr]
git = "https://github.com/StardustXR/core.git"
branch = "camera-item"
# [patch.crates-io.stereokit]
# path = "../stereokit-rs"
# [patch.crates-io.stereokit-sys]
# path = "../stereokit-sys"
# [patch.crates-io.stardust-xr]
# path = "../core/core"
# [patch.crates-io.stardust-xr-schemas]
# path = "../core/schemas"

View File

@@ -15,11 +15,52 @@ This project is a usable Linux display server that reinvents human-computer inte
```bash
cargo build
```
The latest stable server is automatically built to an appimage at https://github.com/StardustXR/server/releases for easy testing.
## Install
```bash
cargo install
```
## Usage
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:
![A pitch black void with a single bleach white hand in the middle](/img/xr_mode_windowed_blank.png)
Stardust won't do anything interesting without clients! Try some from https://github.com/StardustXR.
### Default Sky
You can set a default skytex/skylight by putting your favorite HDRI equirectangular sky in `~/.config/stardust/skytex.hdr`. Certain clients can override this.
Flatscreen mode when the default skybox is [Zhengyang Gate](https://polyhaven.com/a/zhengyang_gate):
![A pitch black window representing Stardust in flatscreen mode](/img/flatscreen_3.png)
### Windowed Mode
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.
![A black void representing Stardust in XR mode with a hand skeleton in the middle](/img/flatscreen_2.png)
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.
### Flags
#### Flatscreen (-f)
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.
Flatscreen mode upon initial startup:
![A pitch black window representing Stardust in flatscreen mode](/img/flatscreen_1.png)
#### Overlay (-o \<PRIORITY>)
The server will, if in XR mode, be overlaid using the OpenXR overlay extension with the given priority.
#### Disable controller (--disable-controller)
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
@@ -41,5 +82,4 @@ cargo install
##### Everything
`nix flake check` will build every test underneath of the `checks` attribute in the `flake.nix`
`nix flake check` will build every test underneath of the `checks` attribute in the `flake.nix`

256
flake.lock generated
View File

@@ -1,33 +1,12 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1683786056,
"narHash": "sha256-Wrz/X9D0t8akhvEGj5a93xgpxI3vAcdPGcwn6tKHooc=",
"owner": "nix-community",
"repo": "fenix",
"rev": "5816c7bbcc385d2e65877631497df3f7d66b354a",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"fenix_2": {
"inputs": {
"nixpkgs": [
"flatland",
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src_2"
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1678775037,
@@ -43,32 +22,34 @@
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1678379998,
"narHash": "sha256-TZdfNqftHhDuIFwBcN9MUThx5sQXCTeZk9je5byPKRw=",
"lastModified": 1690933134,
"narHash": "sha256-ab989mN63fQZBFrkk4Q8bYxQCktuHmBIBqUG1jl6/FQ=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "c13d60b89adea3dc20704c045ec4d50dd964d447",
"rev": "59cf3f1447cfc75087e7273b04b31e689a8599fb",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"flake-parts_2": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib_2"
},
"locked": {
"lastModified": 1688466019,
"narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec",
"type": "github"
},
"original": {
@@ -76,7 +57,7 @@
"type": "indirect"
}
},
"flake-parts_2": {
"flake-parts_3": {
"inputs": {
"nixpkgs-lib": [
"hercules-ci-effects",
@@ -85,11 +66,11 @@
]
},
"locked": {
"lastModified": 1678379998,
"narHash": "sha256-TZdfNqftHhDuIFwBcN9MUThx5sQXCTeZk9je5byPKRw=",
"lastModified": 1688466019,
"narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "c13d60b89adea3dc20704c045ec4d50dd964d447",
"rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec",
"type": "github"
},
"original": {
@@ -98,32 +79,17 @@
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flatland": {
"inputs": {
"fenix": "fenix_2",
"fenix": "fenix",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1683766358,
"narHash": "sha256-wX1Lpj95kkHUZAloB1fGs+ixaRycaOJq4F77+HvaJCQ=",
"lastModified": 1694190588,
"narHash": "sha256-tcvp09A54AbXkkXS/P6Ed5TUWzMEuQ9h/fnfLz7gnJc=",
"owner": "StardustXR",
"repo": "flatland",
"rev": "24613a496841bdf38e5f136608d5295860a75fce",
"rev": "3867067452761959a95497d6589f9336b347270c",
"type": "github"
},
"original": {
@@ -132,59 +98,34 @@
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"hercules-ci-effects",
"hercules-ci-agent",
"pre-commit-hooks-nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"haskell-flake": {
"locked": {
"lastModified": 1678138103,
"narHash": "sha256-D0lao82bV3t2gEFjHiU6RN233t+1MnkQV+bq8MEu2ic=",
"owner": "hercules-ci",
"lastModified": 1684780604,
"narHash": "sha256-2uMZsewmRn7rRtAnnQNw1lj0uZBMh4m6Cs/7dV5YF08=",
"owner": "srid",
"repo": "haskell-flake",
"rev": "1e1660e6dd00838ba73bc7952e6e73be67da18d1",
"rev": "74210fa80a49f1b6f67223debdbf1494596ff9f2",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"ref": "0.1-extraLibraries",
"owner": "srid",
"ref": "0.3.0",
"repo": "haskell-flake",
"type": "github"
}
},
"hercules-ci-agent": {
"inputs": {
"flake-parts": "flake-parts_2",
"flake-parts": "flake-parts_3",
"haskell-flake": "haskell-flake",
"nix-darwin": "nix-darwin",
"nixpkgs": "nixpkgs_2",
"pre-commit-hooks-nix": "pre-commit-hooks-nix"
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1678446614,
"narHash": "sha256-Z6Gsba5ahn/N0QlF0vJfIEfnZgCs4qr1IZtXAqjbE7s=",
"lastModified": 1688568579,
"narHash": "sha256-ON0M56wtY/TIIGPkXDlJboAmuYwc73Hi8X9iJGtxOhM=",
"owner": "hercules-ci",
"repo": "hercules-ci-agent",
"rev": "0b90d1a87c117a5861785cb85833dd1c9df0b6ef",
"rev": "367dd8cd649b57009a6502e878005a1e54ad78c5",
"type": "github"
},
"original": {
@@ -194,16 +135,16 @@
},
"hercules-ci-effects": {
"inputs": {
"flake-parts": "flake-parts",
"flake-parts": "flake-parts_2",
"hercules-ci-agent": "hercules-ci-agent",
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1681898675,
"narHash": "sha256-nIJ7CAdiHv4i1no/VgDoeTJLzbLYwu5+/Ycoyzn0S78=",
"lastModified": 1689397210,
"narHash": "sha256-fVxZnqxMbsDkB4GzGAs/B41K0wt/e+B/fLxmTFF/S20=",
"owner": "hercules-ci",
"repo": "hercules-ci-effects",
"rev": "15ff4f63e5f28070391a5b09a82f6d5c6cc5c9d0",
"rev": "0a63bfa3f00a3775ea3a6722b247880f1ffe91ce",
"type": "github"
},
"original": {
@@ -212,28 +153,6 @@
"type": "github"
}
},
"nix-darwin": {
"inputs": {
"nixpkgs": [
"hercules-ci-effects",
"hercules-ci-agent",
"nixpkgs"
]
},
"locked": {
"lastModified": 1673295039,
"narHash": "sha256-AsdYgE8/GPwcelGgrntlijMg4t3hLFJFCRF3tL5WVjA=",
"owner": "LnL7",
"repo": "nix-darwin",
"rev": "87b9d090ad39b25b2400029c64825fc2a8868943",
"type": "github"
},
"original": {
"owner": "LnL7",
"repo": "nix-darwin",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1678703398,
@@ -253,11 +172,11 @@
"nixpkgs-lib": {
"locked": {
"dir": "lib",
"lastModified": 1678375444,
"narHash": "sha256-XIgHfGvjFvZQ8hrkfocanCDxMefc/77rXeHvYdzBMc8=",
"lastModified": 1690881714,
"narHash": "sha256-h/nXluEqdiQHs1oSgkOOWF+j8gcJMWhwnZ9PFabN6q0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "130fa0baaa2b93ec45523fdcde942f6844ee9f6e",
"rev": "9e1960bc196baf6881340d53dccb203a951745a2",
"type": "github"
},
"original": {
@@ -268,29 +187,31 @@
"type": "github"
}
},
"nixpkgs-stable": {
"nixpkgs-lib_2": {
"locked": {
"lastModified": 1673800717,
"narHash": "sha256-SFHraUqLSu5cC6IxTprex/nTsI81ZQAtDvlBvGDWfnA=",
"dir": "lib",
"lastModified": 1688049487,
"narHash": "sha256-100g4iaKC9MalDjUW9iN6Jl/OocTDtXdeAj7pEGIRh4=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2f9fd351ec37f5d479556cd48be4ca340da59b8f",
"rev": "4bc72cae107788bf3f24f30db2e2f685c9298dc9",
"type": "github"
},
"original": {
"dir": "lib",
"owner": "NixOS",
"ref": "nixos-22.11",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1678293141,
"narHash": "sha256-lLlQHaR0y+q6nd6kfpydPTGHhl1rS9nU9OQmztzKOYs=",
"lastModified": 1688322751,
"narHash": "sha256-eW62dC5f33oKZL7VWlomttbUnOTHrAbte9yNUNW8rbk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c90c4025bb6e0c4eaf438128a3b2640314b1c58d",
"rev": "0fbe93c5a7cac99f90b60bdf5f149383daaa615f",
"type": "github"
},
"original": {
@@ -302,11 +223,11 @@
},
"nixpkgs_3": {
"locked": {
"lastModified": 1678891326,
"narHash": "sha256-cjgrjKx7y+hO9I8O2b6QvBaTt9w7Xhk/5hsnJYTUb2I=",
"lastModified": 1689393711,
"narHash": "sha256-l3gyTPy/qWLDk1CY1EgYwlsxcGxN2aVd7MlCzQa69rk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1544ef240132d4357d9a39a40c8e6afd1678b052",
"rev": "27fcd46fa18df36d270174246e7bd8f1787129ff",
"type": "github"
},
"original": {
@@ -317,72 +238,29 @@
},
"nixpkgs_4": {
"locked": {
"lastModified": 1683408522,
"narHash": "sha256-9kcPh6Uxo17a3kK3XCHhcWiV1Yu1kYj22RHiymUhMkU=",
"owner": "NixOS",
"lastModified": 1692734709,
"narHash": "sha256-SCFnyHCyYjwEmgUsHDDuU0TsbVMKeU1vwkR+r7uS2Rg=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "897876e4c484f1e8f92009fd11b7d988a121a4e7",
"rev": "b85ed9dcbf187b909ef7964774f8847d554fab3b",
"type": "github"
},
"original": {
"owner": "NixOS",
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks-nix": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"hercules-ci-effects",
"hercules-ci-agent",
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1678376203,
"narHash": "sha256-3tyYGyC8h7fBwncLZy5nCUjTJPrHbmNwp47LlNLOHSM=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "1a20b9708962096ec2481eeb2ddca29ed747770a",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-parts": "flake-parts",
"flatland": "flatland",
"hercules-ci-effects": "hercules-ci-effects",
"nixpkgs": "nixpkgs_4"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1683653808,
"narHash": "sha256-GiKwJySG/YCPIKwz9wSm9fJa5e4CU3GvTYAKZzjBhFo=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "b7cdd93f3e1533e96d4cfa1ac8573e6210a2bedf",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"rust-analyzer-src_2": {
"flake": false,
"locked": {
"lastModified": 1678695923,

183
flake.nix
View File

@@ -3,130 +3,77 @@
extra-substituters = [ "https://stardustxr.cachix.org" ];
extra-trusted-public-keys = [ "stardustxr.cachix.org-1:mWSn8Ap2RLsIWT/8gsj+VfbJB6xoOkPaZpbjO+r9HBo=" ];
};
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects";
# 22.11 does not include PR #218472, hence we use the unstable version
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
# Since we do not have a monorepo, we have to fetch Flatland in order to use
# it to create VM Tests
inputs.flatland.url = "github:StardustXR/flatland";
inputs.fenix.url = github:nix-community/fenix;
inputs.fenix.inputs.nixpkgs.follows = "nixpkgs";
inputs.hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects";
outputs = { self, nixpkgs, fenix, hercules-ci-effects, flatland, ... }:
# Since we do not have a monorepo, we have to fetch Flatland in order to use
# it to create VM Tests
flatland.url = "github:StardustXR/flatland";
};
outputs = inputs@{ self, flake-parts, nixpkgs, hercules-ci-effects, flatland, ... }:
let
name = "server";
pkgs = system: import nixpkgs {
inherit system;
name = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.name;
src = builtins.path {
name = "${name}-source";
path = toString ./.;
filter = path: type:
nixpkgs.lib.all
(n: builtins.baseNameOf path != n)
[
"flake.nix"
"flake.lock"
"nix"
"README.md"
];
};
shell = pkgs: pkgs.mkShell {
inputsFrom = [ self.packages.${pkgs.system}.default ];
# ---- START package specific dev settings ----
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
# ---- END package specific dev settings ----
};
package = pkgs:
let
toolchain = fenix.packages.${pkgs.system}.minimal.toolchain;
in
(pkgs.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
}).buildRustPackage rec {
pname = "stardust-xr-${name}";
src = builtins.path {
name = "stardust-xr-source";
path = toString ./.;
filter = path: type:
nixpkgs.lib.all
(n: builtins.baseNameOf path != n)
[
"flake.nix"
"flake.lock"
"nix"
"README.md"
];
};
# ---- START package specific settings ----
version = "0.10.2";
cargoLock = {
lockFile = ./Cargo.lock;
allowBuiltinFetchGit = true;
};
postPatch = ''
sk=$(echo $cargoDepsCopy/stereokit-sys-*/StereoKit)
mkdir -p $sk/build/cpm
cp ${pkgs.fetchurl {
url = "https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.32.2/CPM.cmake";
hash = "sha256-yDHlpqmpAE8CWiwJRoWyaqbuBAg0090G8WJIC2KLHp8=";
}} $sk/build/cpm/CPM_0.32.2.cmake
'';
CPM_SOURCE_CACHE = "./build";
nativeBuildInputs = with pkgs; [
cmake pkg-config llvmPackages.libcxxClang
];
buildInputs = with pkgs; [
openxr-loader libGL mesa xorg.libX11 fontconfig libxkbcommon
];
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
# ---- END package specific settings ----
};
in
{
overlays.default = final: prev: {
stardust-xr = (prev.stardust-xr or {}) // {
${name} = package final;
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
flake-parts.flakeModules.easyOverlay
];
systems = [ "aarch64-linux" "x86_64-linux" "riscv64-linux" ];
perSystem = { config, self', inputs', pkgs, system, ... }: {
_module.args.pkgs = import inputs.nixpkgs { inherit system; overlays = [ inputs.self.overlays.default ]; };
overlayAttrs = config.packages;
packages = {
default = self'.packages.${name};
gnome-graphical-test = self'.checks.gnome-graphical-test;
"${name}" = pkgs.callPackage ./nix/stardust-xr-server.nix { inherit name src; };
};
checks.gnome-graphical-test = pkgs.nixosTest (import ./nix/gnome-graphical-test.nix { inherit pkgs self; });
devShells.default = pkgs.mkShell {
inputsFrom = [ self'.packages.default ];
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
};
};
packages."x86_64-linux".default = package (pkgs "x86_64-linux");
packages."aarch64-linux".default = package (pkgs "aarch64-linux");
packages."x86_64-linux".gnome-graphical-test = self.checks.x86_64-linux.gnome-graphical-test;
packages."aarch64-linux".gnome-graphical-test = self.checks.aarch64-linux.gnome-graphical-test;
checks."x86_64-linux".gnome-graphical-test = (pkgs "x86_64-linux").nixosTest (import ./nix/gnome-graphical-test.nix { pkgs = (pkgs "x86_64-linux"); inherit self; });
checks."aarch64-linux".gnome-graphical-test = (pkgs "aarch64-linux").nixosTest (import ./nix/gnome-graphical-test.nix { pkgs = (pkgs "aarch64-linux"); inherit self; });
devShells."x86_64-linux".default = shell (pkgs "x86_64-linux");
devShells."aarch64-linux".default = shell (pkgs "aarch64-linux");
herculesCI.ciSystems = [ "x86_64-linux" ];
effects = let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
hci-effects = hercules-ci-effects.lib.withPkgs pkgs;
in { branch, rev, ... }: {
gnome-graphical-test = hci-effects.mkEffect {
secretsMap."stardustxrDiscord" = "stardustxrDiscord";
secretsMap."stardustxrIpfs" = "stardustxrIpfs";
effectScript = ''
readSecretString stardustxrDiscord .webhook > .webhook
readSecretString stardustxrIpfs .basicauth > .basicauth
set -x
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)
export CID=$(echo "$RESPONSE" | ${pkgs.jq}/bin/jq -r .Hash)
set +x
export ADDRESS="https://ipfs.stardustxr.org/ipfs/$CID"
${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\`" \
--field "Branch;${branch}" \
--field "Commit ID;${rev}" \
--field "Flatland Revision;${flatland.rev}" \
--field "Reproducer;\`nix build github:stardustxr/server/${rev}#gnome-graphical-test\`" \
--image "$ADDRESS"
'';
flake = {
herculesCI.ciSystems = [ "x86_64-linux" ];
effects = let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
hci-effects = hercules-ci-effects.lib.withPkgs pkgs;
in { branch, rev, ... }: {
gnome-graphical-test = hci-effects.mkEffect {
secretsMap."stardustxrDiscord" = "stardustxrDiscord";
secretsMap."stardustxrIpfs" = "stardustxrIpfs";
effectScript = ''
readSecretString stardustxrDiscord .webhook > .webhook
readSecretString stardustxrIpfs .basicauth > .basicauth
set -x
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)
export CID=$(echo "$RESPONSE" | ${pkgs.jq}/bin/jq -r .Hash)
set +x
export ADDRESS="https://ipfs.stardustxr.org/ipfs/$CID"
${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\`" \
--field "Branch;${branch}" \
--field "Commit ID;${rev}" \
--field "Flatland Revision;${flatland.rev}" \
--field "Reproducer;\`nix build github:stardustxr/server/${rev}#gnome-graphical-test\`" \
--image "$ADDRESS"
'';
};
};
};
};

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
img/flatscreen_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 789 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,40 @@
{ rustPlatform
, src
, name
, openxr-loader
, libGL
, mesa
, xorg
, fontconfig
, libxkbcommon
, libclang
, cmake
, cpm-cmake
, pkg-config
, llvmPackages
}:
rustPlatform.buildRustPackage rec {
inherit src name;
cargoLock = {
lockFile = (src + "/Cargo.lock");
allowBuiltinFetchGit = true;
};
CPM_SOURCE_CACHE = "./build";
postPatch = ''
sk=$(echo $cargoDepsCopy/stereokit-sys-*/StereoKit)
mkdir -p $sk/build/cpm
# 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
cp ${cpm-cmake}/share/cpm/CPM.cmake $sk/build/cpm/CPM_0.32.2.cmake
'';
nativeBuildInputs = [
cmake pkg-config llvmPackages.libcxxClang
];
buildInputs = [
openxr-loader libGL mesa xorg.libX11 fontconfig libxkbcommon
];
LIBCLANG_PATH = "${libclang.lib}/lib";
}

50
src/core/buffers.rs Normal file
View 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();
}
}
}

View File

@@ -1,20 +1,20 @@
use super::scenegraph::Scenegraph;
use super::{
client_state::{ClientState, CLIENT_STATES},
scenegraph::Scenegraph,
};
use crate::{
core::{registry::OwnedRegistry, task},
nodes::{
audio, data, drawable, fields, hmd, input, items,
root::Root,
spatial,
startup::{self, StartupSettings, STARTUP_SETTINGS},
Node,
},
nodes::{audio, data, drawable, fields, hmd, input, items, root::Root, spatial, Node},
};
use color_eyre::eyre::{eyre, Result};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use stardust_xr::messenger::{self, MessageSenderHandle};
use stardust_xr::{
messenger::{self, MessageSenderHandle},
schemas::flex::serialize,
};
use std::{fs, iter::FromIterator, path::PathBuf, sync::Arc};
use tokio::{net::UnixStream, task::JoinHandle};
use tracing::info;
@@ -34,7 +34,7 @@ lazy_static! {
scenegraph: Default::default(),
root: OnceCell::new(),
base_resource_prefixes: Default::default(),
startup_settings: None,
state: Arc::new(ClientState::default()),
});
}
@@ -46,13 +46,13 @@ pub fn get_env(pid: i32) -> Result<FxHashMap<String, String>, std::io::Error> {
.map(|(k, v)| (k.to_string(), v.to_string())),
))
}
pub fn startup_settings(env: &FxHashMap<String, String>) -> Option<StartupSettings> {
pub fn state(env: &FxHashMap<String, String>) -> Option<Arc<ClientState>> {
let token = env.get("STARDUST_STARTUP_TOKEN")?;
STARTUP_SETTINGS.lock().get(token).cloned()
CLIENT_STATES.lock().get(token).cloned()
}
pub struct Client {
pid: Option<i32>,
pub pid: Option<i32>,
// env: Option<FxHashMap<String, String>>,
exe: Option<PathBuf>,
dispatch_join_handle: OnceCell<JoinHandle<Result<()>>>,
@@ -63,7 +63,7 @@ pub struct Client {
pub scenegraph: Arc<Scenegraph>,
pub root: OnceCell<Arc<Root>>,
pub base_resource_prefixes: Mutex<Vec<PathBuf>>,
pub startup_settings: Option<StartupSettings>,
pub state: Arc<ClientState>,
}
impl Client {
pub fn from_connection(connection: UnixStream) -> Result<Arc<Self>> {
@@ -80,7 +80,10 @@ impl Client {
let (mut messenger_tx, mut messenger_rx) = messenger::create(connection);
let scenegraph = Arc::new(Scenegraph::default());
let startup_settings = env.as_ref().and_then(startup_settings);
let state = env
.as_ref()
.and_then(state)
.unwrap_or_else(|| Arc::new(ClientState::default()));
let client = CLIENTS.add(Client {
pid,
@@ -95,7 +98,7 @@ impl Client {
scenegraph: scenegraph.clone(),
root: OnceCell::new(),
base_resource_prefixes: Default::default(),
startup_settings,
state,
});
let _ = client.scenegraph.client.set(Arc::downgrade(&client));
let _ = client.root.set(Root::create(&client)?);
@@ -107,7 +110,13 @@ impl Client {
data::create_interface(&client)?;
items::create_interface(&client)?;
input::create_interface(&client)?;
startup::create_interface(&client)?;
client
.root
.get()
.unwrap()
.node
.send_remote_signal("restore_state", serialize(client.state.apply_to(&client))?)?;
let pid_printable = pid
.map(|pid| pid.to_string())
@@ -164,6 +173,27 @@ impl Client {
Ok(client)
}
pub fn get_cmdline(&self) -> Option<Vec<String>> {
let pid = self.pid?;
let exe_proc_path = format!("/proc/{pid}/exe");
let cmdline_proc_path = format!("/proc/{pid}/cmdline");
let exe = std::fs::read_link(exe_proc_path).ok()?;
let cmdline = std::fs::read_to_string(cmdline_proc_path).ok()?;
let mut cmdline_split: Vec<_> = cmdline.split('\0').map(ToString::to_string).collect();
cmdline_split.pop();
*cmdline_split.get_mut(0).unwrap() = exe.to_str()?.to_string();
Some(cmdline_split)
}
pub fn get_cwd(&self) -> Option<PathBuf> {
let pid = self.pid?;
let cwd_proc_path = format!("/proc/{pid}/cwd");
std::fs::read_link(cwd_proc_path).ok()
}
pub async fn save_state(&self) -> Option<ClientState> {
let internal = self.root.get()?.save_state().await.ok()?;
Some(ClientState::from_deserialized(self, internal))
}
#[inline]
pub fn get_node(&self, name: &'static str, path: &str) -> Result<Arc<Node>> {
self.scenegraph

108
src/core/client_state.rs Normal file
View File

@@ -0,0 +1,108 @@
use super::client::{get_env, Client};
use crate::nodes::{spatial::Spatial, Node};
use glam::Mat4;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::{io::Write, path::PathBuf, sync::Arc};
lazy_static::lazy_static! {
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientState>>> = Default::default();
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LaunchInfo {
pub cmdline: Vec<String>,
pub cwd: PathBuf,
pub env: FxHashMap<String, String>,
}
impl LaunchInfo {
fn from_client(client: &Client) -> Option<Self> {
Some(LaunchInfo {
cmdline: client.get_cmdline()?,
cwd: client.get_cwd()?,
env: get_env(client.pid?).ok()?,
})
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ClientState {
pub launch_info: Option<LaunchInfo>,
pub data: Option<Vec<u8>>,
pub root: Mat4,
pub spatial_anchors: FxHashMap<String, Mat4>,
}
impl ClientState {
pub fn from_deserialized(client: &Client, state: ClientStateInternal) -> Self {
ClientState {
launch_info: LaunchInfo::from_client(client),
data: state.data,
root: Self::spatial_transform(client, &state.root.unwrap_or_default())
.unwrap_or_default(),
spatial_anchors: state
.spatial_anchors
.into_iter()
.filter_map(|(k, v)| Some((k, Self::spatial_transform(client, &v)?)))
.collect(),
}
}
fn spatial_transform(client: &Client, path: &str) -> Option<Mat4> {
let node = client.scenegraph.get_node(path)?;
let spatial = node.spatial.get()?;
Some(spatial.global_transform())
}
pub fn token(self) -> String {
let token = nanoid::nanoid!();
CLIENT_STATES.lock().insert(token.clone(), Arc::new(self));
token
}
pub fn to_file(self) {
let project_dirs = directories::ProjectDirs::from("", "", "stardust").unwrap();
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 apply_to(&self, client: &Arc<Client>) -> ClientStateInternal {
if let Some(root) = client.root.get() {
root.set_transform(self.root)
}
ClientStateInternal {
data: self.data.clone(),
root: Some("/".to_string()),
spatial_anchors: self
.spatial_anchors
.iter()
.map(|(k, v)| {
(k.clone(), {
let node = Node::create(client, "/spatial/anchor", k, true)
.add_to_scenegraph()
.unwrap();
Spatial::add_to(&node, None, *v, false).unwrap();
k.clone()
})
})
.collect(),
}
}
}
impl Default for ClientState {
fn default() -> Self {
Self {
launch_info: None,
data: None,
root: Mat4::IDENTITY,
spatial_anchors: Default::default(),
}
}
}
#[derive(Default, Serialize, Deserialize)]
pub struct ClientStateInternal {
data: Option<Vec<u8>>,
root: Option<String>,
spatial_anchors: FxHashMap<String, String>,
}

View File

@@ -1,4 +1,5 @@
pub mod client;
pub mod client_state;
pub mod delta;
pub mod destroy_queue;
pub mod eventloop;
@@ -7,3 +8,5 @@ pub mod registry;
pub mod resource;
pub mod scenegraph;
pub mod task;
pub mod typed_datamap;
pub mod buffers;

View File

@@ -7,33 +7,32 @@ use std::{
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);
}
// #[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();
// }
// }
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)]
#[derive(Default, Debug)]
pub struct LifeLinkedNodeMap<K: Hash + Eq> {
nodes: Mutex<FxHashMap<K, Weak<Node>>>,
}

View File

@@ -54,6 +54,14 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
pub fn clear(&self) {
self.lock().clear();
}
pub fn is_empty(&self) -> bool {
let registry = self.0.lock();
let Some(registry) = &*registry else {return true};
if registry.is_empty() {
return true;
}
registry.values().all(|v| v.strong_count() == 0)
}
}
impl<T: Send + Sync + ?Sized> Clone for Registry<T> {
fn clone(&self) -> Self {

View File

@@ -1,20 +1,21 @@
use crate::core::client::Client;
use crate::nodes::Node;
use crate::{core::client::Client, nodes::Message};
use color_eyre::eyre::Result;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::Ordering;
use rustc_hash::FxHashMap;
use stardust_xr::scenegraph;
use stardust_xr::scenegraph::ScenegraphError;
use std::os::fd::OwnedFd;
use std::sync::{Arc, Weak};
use tracing::{debug, debug_span, instrument};
use core::hash::BuildHasherDefault;
use dashmap::DashMap;
use rustc_hash::FxHasher;
use tokio::sync::oneshot;
use tracing::{debug, debug_span};
#[derive(Default)]
pub struct Scenegraph {
pub(super) client: OnceCell<Weak<Client>>,
nodes: DashMap<String, Arc<Node>, BuildHasherDefault<FxHasher>>,
nodes: Mutex<FxHashMap<String, Arc<Node>>>,
}
impl Scenegraph {
@@ -30,32 +31,60 @@ impl Scenegraph {
pub fn add_node_raw(&self, node: Arc<Node>) {
debug!(node = ?&*node, "Add node");
let path = node.get_path().to_string();
self.nodes.insert(path, node);
self.nodes.lock().insert(path, node);
}
#[instrument(level = "debug", skip(self))]
pub fn get_node(&self, path: &str) -> Option<Arc<Node>> {
let mut node = self.nodes.get(path)?.clone();
let mut node = self.nodes.lock().get(path)?.clone();
while let Some(alias) = node.alias.get() {
node = alias.original.upgrade()?;
if alias.enabled.load(Ordering::Relaxed) {
node = alias.original.upgrade()?;
} else {
return None;
}
}
Some(node)
}
pub fn remove_node(&self, path: &str) -> Option<Arc<Node>> {
debug!(path, "Remove node");
let (_, node) = self.nodes.remove(path)?;
Some(node)
self.nodes.lock().remove(path)
}
}
pub struct MethodResponseSender(oneshot::Sender<Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>>);
impl MethodResponseSender {
pub fn send(self, t: Result<Message, ScenegraphError>) {
let _ = self.0.send(t.map(|m| (m.data, m.fds)));
}
pub fn wrap_sync<F: FnOnce() -> color_eyre::eyre::Result<Message>>(self, f: F) {
self.send(f().map_err(|e| ScenegraphError::MethodError {
error: e.to_string(),
}))
}
}
impl scenegraph::Scenegraph for Scenegraph {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
let Some(client) = self.get_client() else {return Err(ScenegraphError::SignalNotFound)};
fn send_signal(
&self,
path: &str,
method: &str,
data: &[u8],
fds: Vec<OwnedFd>,
) -> Result<(), ScenegraphError> {
let Some(client) = self.get_client() else {
return Err(ScenegraphError::SignalNotFound);
};
debug_span!("Handle signal", path, method).in_scope(|| {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.send_local_signal(client, method, data)
.send_local_signal(
client,
method,
Message {
data: data.to_vec(),
fds,
},
)
})
}
fn execute_method(
@@ -63,12 +92,26 @@ impl scenegraph::Scenegraph for Scenegraph {
path: &str,
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
let Some(client) = self.get_client() else {return Err(ScenegraphError::MethodNotFound)};
debug_span!("Handle method", path, method).in_scope(|| {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.execute_local_method(client, method, data)
})
fds: Vec<OwnedFd>,
response: oneshot::Sender<Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError>>,
) {
let Some(client) = self.get_client() else {
let _ = response.send(Err(ScenegraphError::MethodNotFound));
return;
};
debug!(path, method, "Handle method");
let Some(node) = self.get_node(path) else {
let _ = response.send(Err(ScenegraphError::NodeNotFound));
return;
};
node.execute_local_method(
client,
method,
Message {
data: data.to_vec(),
fds,
},
MethodResponseSender(response),
);
}
}

View File

@@ -1,10 +1,8 @@
use color_eyre::eyre::Result;
use std::future::Future;
use tokio::task::JoinHandle;
use tracing::instrument;
#[allow(unused_variables)]
#[instrument(level = "debug", skip_all)]
pub fn new<
F: FnOnce() -> S,
S: AsRef<str>,

56
src/core/typed_datamap.rs Normal file
View 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
}
}

View File

@@ -4,21 +4,26 @@ mod objects;
#[cfg(feature = "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::{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 color_eyre::eyre::Result;
use directories::ProjectDirs;
use once_cell::sync::OnceCell;
use smithay::reexports::nix;
use stardust_xr::server;
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::Command;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::Duration;
use stereokit::{
@@ -26,7 +31,10 @@ use stereokit::{
TextureFormat, TextureType,
};
use stereokit::{DisplayBlend, Sk};
use tokio::sync::Notify;
use tokio::task::LocalSet;
use tokio::{runtime::Handle, sync::oneshot};
use tracing::metadata::LevelFilter;
use tracing::{debug_span, error, info};
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
@@ -52,20 +60,25 @@ struct CliArgs {
static STARDUST_INSTANCE: OnceCell<String> = OnceCell::new();
static SK_MULTITHREAD: OnceCell<Sk> = OnceCell::new();
static STOP_NOTIFIER: Notify = Notify::const_new();
struct EventLoopInfo {
tokio_handle: Handle,
socket_path: PathBuf,
}
fn main() -> Result<()> {
fn main() {
ctrlc::set_handler(|| {
if atty::isnt(atty::Stream::Stdout) {
STOP_NOTIFIER.notify_waiters()
}
})
.unwrap();
let registry = tracing_subscriber::registry();
#[cfg(feature = "profile_app")]
let (chrome_layer, _guard) = tracing_chrome::ChromeLayerBuilder::new()
.include_args(true)
.build();
#[cfg(feature = "profile_app")]
let registry = registry.with(chrome_layer);
let registry = registry.with(tracing_tracy::TracyLayer::new().with_filter(LevelFilter::DEBUG));
#[cfg(feature = "profile_tokio")]
let (console_layer, _) = console_subscriber::ConsoleLayer::builder().build();
@@ -94,7 +107,15 @@ fn main() -> Result<()> {
},
blend_preference: DisplayBlend::AnyTransparent,
depth_mode: DepthMode::D32,
log_filter: LogLevel::None,
log_filter: match EnvFilter::from_default_env().max_level_hint() {
Some(LevelFilter::ERROR) => LogLevel::Error,
Some(LevelFilter::WARN) => LogLevel::Warning,
Some(LevelFilter::INFO) => LogLevel::Inform,
Some(LevelFilter::DEBUG) => LogLevel::Diagnostic,
Some(LevelFilter::TRACE) => LogLevel::Diagnostic,
Some(LevelFilter::OFF) => LogLevel::None,
None => LogLevel::Warning,
},
overlay_app: cli_args.overlay_priority.is_some(),
overlay_priority: cli_args.overlay_priority.unwrap_or(u32::MAX),
disable_desktop_input_window: true,
@@ -106,8 +127,8 @@ fn main() -> Result<()> {
info!("Init StereoKit");
sk.material_set_shader(
sk.material_find("default/material_pbr")?,
sk.shader_find("default/shader_pbr_clip")?,
sk.material_find("default/material_pbr").unwrap(),
sk.shader_find("default/shader_pbr_clip").unwrap(),
);
// Skytex/light stuff
@@ -135,7 +156,11 @@ fn main() -> Result<()> {
}
}
let mouse_pointer = cli_args.flatscreen.then(MousePointer::new).transpose()?;
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();
@@ -145,58 +170,89 @@ fn main() -> Result<()> {
.flatten();
let mut controllers = (!cli_args.flatscreen && !cli_args.disable_controller)
.then(|| {
let left = SkController::new(Handed::Left).ok();
let right = SkController::new(Handed::Right).ok();
let left = SkController::new(&sk, Handed::Left).ok();
let right = SkController::new(&sk, Handed::Right).ok();
left.zip(right)
})
.flatten();
let eye_pointer = (!cli_args.flatscreen && sk.device_has_eye_gaze())
.then(EyePointer::new)
.transpose()?;
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 (event_stop_tx, event_stop_rx) = oneshot::channel::<()>();
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, event_stop_rx))?;
let event_loop_info = info_receiver.blocking_recv()?;
.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")]
let mut wayland = wayland::Wayland::new()?;
let mut wayland = wayland::Wayland::new(&buffer_manager).expect("Could not initialize wayland");
info!("Stardust ready!");
if let Some(project_dirs) = project_dirs.as_ref() {
let mut startup_child = (|| {
let project_dirs = project_dirs.as_ref()?;
let startup_script_path = cli_args
.startup_script
.clone()
.and_then(|p| p.canonicalize().ok())
.unwrap_or_else(|| project_dirs.config_dir().join("startup"));
let _startup = Command::new(startup_script_path)
.env(
"FLAT_WAYLAND_DISPLAY",
std::env::var_os("WAYLAND_DISPLAY").unwrap_or_default(),
)
.env("WAYLAND_DISPLAY", &wayland.socket_name)
.env(
"STARDUST_INSTANCE",
event_loop_info
.socket_path
.file_name()
.expect("Stardust socket path not found"),
)
.env("GDK_BACKEND", "wayland")
.env("QT_QPA_PLATFORM", "wayland")
.env("MOZ_ENABLE_WAYLAND", "1")
.env("CLUTTER_BACKEND", "wayland")
.env("SDL_VIDEODRIVER", "wayland")
.spawn();
}
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 sleep_duration = Duration::ZERO;
@@ -206,17 +262,18 @@ fn main() -> Result<()> {
let _span = debug_span!("StereoKit step");
let _span = _span.enter();
camera::send_rendered();
hmd::frame(sk);
#[cfg(feature = "wayland")]
wayland.frame_event(sk);
destroy_queue::clear();
if let Some(mouse_pointer) = &mouse_pointer {
if let Some(mouse_pointer) = &mut mouse_pointer {
mouse_pointer.update(sk);
}
if let Some((left_hand, right_hand)) = &mut hands {
left_hand.update(sk);
right_hand.update(sk);
left_hand.update(!cli_args.disable_controller, sk);
right_hand.update(!cli_args.disable_controller, sk);
}
if let Some((left_controller, right_controller)) = &mut controllers {
left_controller.update(sk);
@@ -225,6 +282,9 @@ fn main() -> Result<()> {
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(
@@ -235,14 +295,20 @@ fn main() -> Result<()> {
);
#[cfg(feature = "wayland")]
wayland.update(sk);
wayland.update(sk, &mut buffer_manager);
drawable::draw(sk);
audio::update(sk);
#[cfg(feature = "wayland")]
wayland.make_context_current();
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();
}
},
)
});
@@ -250,12 +316,13 @@ fn main() -> Result<()> {
#[cfg(feature = "wayland")]
drop(wayland);
let _ = event_stop_tx.send(());
STOP_NOTIFIER.notify_waiters();
event_thread
.join()
.expect("Failed to cleanly shut down event loop")?;
.expect("Failed to cleanly shut down event loop")
.unwrap();
info!("Cleanly shut down Stardust");
Ok(())
}
fn adaptive_sleep(
@@ -283,10 +350,7 @@ fn adaptive_sleep(
#[tokio::main]
// #[tokio::main(flavor = "current_thread")]
async fn event_loop(
info_sender: oneshot::Sender<EventLoopInfo>,
stop_rx: oneshot::Receiver<()>,
) -> color_eyre::eyre::Result<()> {
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");
@@ -301,11 +365,9 @@ async fn event_loop(
socket_path,
});
let result = tokio::select! {
biased;
_ = tokio::signal::ctrl_c() => Ok(()),
_ = stop_rx => Ok(()),
};
STOP_NOTIFIER.notified().await;
println!("Stopping...");
save_clients().await;
info!("Cleanly shut down event loop");
@@ -313,5 +375,19 @@ async fn event_loop(
stereokit::sys::sk_quit();
}
result
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;
}

View File

@@ -1,6 +1,7 @@
use super::Node;
use crate::core::client::Client;
use color_eyre::eyre::{ensure, Result};
use portable_atomic::AtomicBool;
use std::sync::{Arc, Weak};
#[derive(Debug, Default, Clone)]
@@ -12,6 +13,7 @@ pub struct AliasInfo {
#[allow(dead_code)]
pub struct Alias {
pub enabled: Arc<AtomicBool>,
pub(super) node: Weak<Node>,
pub original: Weak<Node>,
@@ -35,6 +37,7 @@ impl Alias {
let node = Node::create(client, parent, name, true).add_to_scenegraph()?;
let alias = Alias {
enabled: Arc::new(AtomicBool::new(true)),
node: Arc::downgrade(&node),
original: Arc::downgrade(original),
info,

View File

@@ -1,4 +1,4 @@
use super::Node;
use super::{Message, Node};
use crate::core::client::Client;
use crate::core::destroy_queue;
use crate::core::registry::Registry;
@@ -83,13 +83,13 @@ impl Sound {
}
}
fn play_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
fn play_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
let sound = node.sound.get().unwrap();
sound.play.lock().replace(());
Ok(())
}
pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
let sound = node.sound.get().unwrap();
sound.stop.lock().replace(());
Ok(())
@@ -108,7 +108,7 @@ pub fn create_interface(client: &Arc<Client>) -> Result<()> {
node.add_to_scenegraph().map(|_| ())
}
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateSoundInfo<'a> {
name: &'a str,
@@ -116,7 +116,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Re
transform: Transform,
resource: ResourceID,
}
let info: CreateSoundInfo = deserialize(data)?;
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);

View File

@@ -1,21 +1,30 @@
use super::alias::AliasInfo;
use super::fields::Field;
use super::spatial::{parse_transform, Spatial};
use super::{Alias, Node};
use super::{Alias, Message, Node};
use crate::core::client::Client;
use crate::core::node_collections::LifeLinkedNodeMap;
use crate::core::registry::Registry;
use crate::core::scenegraph::MethodResponseSender;
use crate::nodes::fields::{find_field, FIELD_ALIAS_INFO};
use crate::nodes::spatial::find_spatial_parent;
use color_eyre::eyre::{ensure, eyre, Result};
use color_eyre::eyre::{bail, ensure, eyre, Result};
use glam::vec3a;
use lazy_static::lazy_static;
use mint::{Quaternion, Vector3};
use nanoid::nanoid;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use stardust_xr::schemas::flex::{deserialize, flexbuffers, serialize};
use stardust_xr::values::Transform;
use std::fmt::Display;
use std::sync::{Arc, Weak};
lazy_static! {
pub static ref KEYMAPS: Mutex<FxHashMap<String, String>> = Mutex::new(FxHashMap::default());
}
static PULSE_SENDER_REGISTRY: Registry<PulseSender> = Registry::new();
pub static PULSE_RECEIVER_REGISTRY: Registry<PulseReceiver> = Registry::new();
@@ -24,7 +33,16 @@ pub fn mask_matches(mask_map_lesser: &Mask, mask_map_greater: &Mask) -> bool {
for key in mask_map_lesser.get_mask()?.iter_keys() {
let lesser_key = mask_map_lesser.get_mask()?.index(key)?;
let greater_key = mask_map_greater.get_mask()?.index(key)?;
if lesser_key.to_string() != greater_key.to_string() {
// otherwise zero-length vectors don't count the same as a single type vector
if lesser_key.flexbuffer_type().is_heterogenous_vector()
&& lesser_key.as_vector().len() == 0
&& greater_key.flexbuffer_type().is_vector()
{
continue;
}
if !lesser_key.flexbuffer_type().is_null()
&& lesser_key.flexbuffer_type() != greater_key.flexbuffer_type()
{
return Err(flexbuffers::ReaderError::InvalidPackedType {}.into());
}
}
@@ -35,6 +53,11 @@ pub fn mask_matches(mask_map_lesser: &Mask, mask_map_greater: &Mask) -> bool {
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"))?
@@ -42,6 +65,14 @@ impl Mask {
.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> {
@@ -80,9 +111,9 @@ impl PulseSender {
if !mask_matches(&self.mask, &receiver.mask) {
return;
}
let Some(tx_node) = self.node.upgrade() else { return };
let Some(tx_client) = tx_node.get_client() else { return };
let Some(rx_node) = receiver.node.upgrade() else { return };
let Some(tx_node) = self.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
let rx_alias = Alias::create(
&tx_client,
@@ -137,7 +168,7 @@ impl PulseSender {
};
let Ok(data) = serialize(info) else {return};
let _ = tx_node.send_remote_signal("new_receiver", &data);
let _ = tx_node.send_remote_signal("new_receiver", data);
}
fn handle_drop_receiver(&self, receiver: &PulseReceiver) {
@@ -146,11 +177,11 @@ impl PulseSender {
self.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("drop_receiver", &data);
let _ = tx_node.send_remote_signal("drop_receiver", data);
}
fn send_data_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let info: SendDataInfo = deserialize(data)?;
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)?;
@@ -164,7 +195,9 @@ impl PulseSender {
data_mask.get_mask()?;
ensure!(
mask_matches(receiver_mask, &data_mask),
"Message does not contain the same keys as the receiver's 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)
}
@@ -182,7 +215,7 @@ pub struct PulseReceiver {
pub mask: Mask,
}
impl PulseReceiver {
pub fn add_to(node: &Arc<Node>, field: Arc<Field>, mask: Mask) -> Result<()> {
pub fn add_to(node: &Arc<Node>, field: Arc<Field>, mask: Mask) -> Result<Arc<PulseReceiver>> {
ensure!(
node.spatial.get().is_some(),
"Internal: Node does not have a spatial attached!"
@@ -199,13 +232,13 @@ impl PulseReceiver {
for sender in PULSE_SENDER_REGISTRY.get_valid_contents() {
sender.handle_new_receiver(&receiver);
}
let _ = node.pulse_receiver.set(receiver);
Ok(())
let _ = node.pulse_receiver.set(receiver.clone());
Ok(receiver)
}
pub fn send_data(&self, uid: &str, data: Vec<u8>) -> Result<()> {
if let Some(node) = self.node.upgrade() {
node.send_remote_signal("data", &serialize(SendDataInfo { uid, data })?)?;
node.send_remote_signal("data", serialize(SendDataInfo { uid, data })?)?;
}
Ok(())
}
@@ -224,13 +257,15 @@ pub fn create_interface(client: &Arc<Client>) -> Result<()> {
let node = Node::create(client, "", "data", false);
node.add_local_signal("create_pulse_sender", create_pulse_sender_flex);
node.add_local_signal("create_pulse_receiver", create_pulse_receiver_flex);
node.add_local_method("register_keymap", register_keymap_flex);
node.add_local_method("get_keymap", get_keymap_flex);
node.add_to_scenegraph().map(|_| ())
}
pub fn create_pulse_sender_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreatePulseSenderInfo<'a> {
@@ -239,7 +274,7 @@ pub fn create_pulse_sender_flex(
transform: Transform,
mask: Vec<u8>,
}
let info: CreatePulseSenderInfo = deserialize(data)?;
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);
@@ -256,7 +291,7 @@ pub fn create_pulse_sender_flex(
pub fn create_pulse_receiver_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreatePulseReceiverInfo<'a> {
@@ -266,7 +301,7 @@ pub fn create_pulse_receiver_flex(
field_path: &'a str,
mask: Vec<u8>,
}
let info: CreatePulseReceiverInfo = deserialize(data)?;
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);
@@ -279,3 +314,42 @@ pub fn create_pulse_receiver_flex(
PulseReceiver::add_to(&node, field, mask)?;
Ok(())
}
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();
if let Some(found_keymap_id) = keymaps
.iter()
.filter(|(_k, v)| *v == &keymap)
.map(|(k, _v)| k)
.last()
{
return Ok(serialize(found_keymap_id)?.into());
}
let generated_id = nanoid!();
keymaps.insert(generated_id.clone(), keymap);
Ok(serialize(generated_id)?.into())
});
}
pub fn get_keymap_flex(
_node: &Node,
_calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
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())
});
}

View File

@@ -2,7 +2,7 @@ use crate::{
core::{client::Client, registry::Registry},
nodes::{
spatial::{find_spatial_parent, parse_transform, Spatial},
Node,
Message, Node,
},
};
use color_eyre::eyre::{bail, ensure, Result};
@@ -97,10 +97,14 @@ impl Lines {
draw_ctx.line_add_listv(points.make_contiguous());
}
pub fn set_points_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn set_points_flex(
node: &Node,
_calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
let mut points: Vec<LinePointRaw> = deserialize(data)?;
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);
@@ -109,10 +113,14 @@ impl Lines {
lines.data.lock().points = points;
Ok(())
}
pub fn set_cyclic_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?;
lines.data.lock().cyclic = deserialize(message.as_ref())?;
Ok(())
}
}
@@ -130,7 +138,7 @@ pub fn draw_all(draw_ctx: &impl StereoKitDraw) {
}
}
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateTextInfo<'a> {
name: &'a str,
@@ -139,7 +147,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Re
points: Vec<LinePointRaw>,
cyclic: bool,
}
let mut info: CreateTextInfo = deserialize(data)?;
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);

View File

@@ -1,5 +1,6 @@
pub mod lines;
pub mod model;
pub mod shaders;
pub mod text;
use self::{
@@ -8,7 +9,7 @@ use self::{
text::Text,
};
use super::Node;
use super::{Message, Node};
use crate::core::client::Client;
use color_eyre::eyre::Result;
use parking_lot::Mutex;
@@ -55,14 +56,14 @@ pub fn draw(sk: &impl StereoKitDraw) {
static QUEUED_SKYLIGHT: Mutex<Option<PathBuf>> = Mutex::new(None);
static QUEUED_SKYTEX: Mutex<Option<PathBuf>> = Mutex::new(None);
fn set_sky_file_flex(_node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn set_sky_file_flex(_node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct SkyFileInfo {
path: PathBuf,
skytex: Option<bool>,
skylight: Option<bool>,
}
let info: SkyFileInfo = deserialize(data)?;
let info: SkyFileInfo = deserialize(message.as_ref())?;
info.path.metadata()?;
if info.skytex.unwrap_or_default() {
QUEUED_SKYTEX.lock().replace(info.path.clone());

View File

@@ -5,6 +5,7 @@ use crate::core::registry::Registry;
use crate::core::resource::ResourceID;
use crate::nodes::drawable::Drawable;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use crate::nodes::Message;
use crate::SK_MULTITHREAD;
use color_eyre::eyre::{bail, ensure, eyre, Result};
use glam::Mat4;
@@ -13,7 +14,6 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use rustc_hash::FxHashMap;
use send_wrapper::SendWrapper;
use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize;
use stardust_xr::values::Transform;
@@ -121,7 +121,7 @@ pub struct ModelPart {
space: Arc<Spatial>,
model: Weak<Model>,
pending_material_parameters: Mutex<FxHashMap<String, MaterialParameter>>,
pending_material_replacement: Mutex<Option<Arc<SendWrapper<Material>>>>,
pending_material_replacement: Mutex<Option<Arc<Material>>>,
}
impl ModelPart {
fn create_for_model(sk: &impl StereoKitMultiThread, model: &Arc<Model>, sk_model: &SKModel) {
@@ -213,11 +213,11 @@ impl ModelPart {
fn set_material_parameter_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
let Some(Drawable::ModelPart(model_part)) = node.drawable.get() else {bail!("Not a drawable??")};
let (name, value): (String, MaterialParameter) = deserialize(data)?;
let (name, value): (String, MaterialParameter) = deserialize(message.as_ref())?;
model_part
.pending_material_parameters
@@ -227,7 +227,7 @@ impl ModelPart {
Ok(())
}
pub fn replace_material(&self, replacement: Arc<SendWrapper<Material>>) {
pub fn replace_material(&self, replacement: Arc<Material>) {
self.pending_material_replacement
.lock()
.replace(replacement);
@@ -342,7 +342,7 @@ pub fn draw_all(sk: &impl StereoKitDraw) {
}
}
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateModelInfo<'a> {
name: &'a str,
@@ -350,7 +350,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Re
transform: Transform,
resource: ResourceID,
}
let info: CreateModelInfo = deserialize(data)?;
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);

View File

@@ -0,0 +1,144 @@
#![allow(dead_code)]
use smithay::backend::renderer::gles::{
ffi::{self, Gles2, FRAGMENT_SHADER, VERTEX_SHADER},
GlesError,
};
use std::mem::transmute;
use stereokit::Shader;
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 {
asset_type: i32,
asset_state: i32,
id: u64,
index: u64,
refs: i32,
debug: *mut u8,
}
struct FfiSkgShader {
meta: *mut u8,
vertex: u32,
pixel: u32,
program: u32,
compute: u32,
}
struct FfiShader {
header: FfiAssetHeader,
shader: FfiSkgShader,
}
unsafe fn compile_shader(
gl: &ffi::Gles2,
variant: ffi::types::GLuint,
src: &str,
) -> Result<ffi::types::GLuint, GlesError> {
let shader = gl.CreateShader(variant);
if shader == 0 {
return Err(GlesError::CreateShaderObject);
}
gl.ShaderSource(
shader,
1,
&src.as_ptr() as *const *const u8 as *const *const ffi::types::GLchar,
&(src.len() as i32) as *const _,
);
gl.CompileShader(shader);
let mut status = ffi::FALSE as i32;
gl.GetShaderiv(shader, ffi::COMPILE_STATUS, &mut status as *mut _);
if status == ffi::FALSE as i32 {
let mut max_len = 0;
gl.GetShaderiv(shader, ffi::INFO_LOG_LENGTH, &mut max_len as *mut _);
let mut error = Vec::with_capacity(max_len as usize);
let mut len = 0;
gl.GetShaderInfoLog(
shader,
max_len as _,
&mut len as *mut _,
error.as_mut_ptr() as *mut _,
);
error.set_len(len as usize);
error!(
"[GL] {}",
std::str::from_utf8(&error).unwrap_or("<Error Message no utf8>")
);
gl.DeleteShader(shader);
return Err(GlesError::ShaderCompileError);
}
Ok(shader)
}
unsafe fn link_program(
gl: &ffi::Gles2,
vert: ffi::types::GLuint,
frag: ffi::types::GLuint,
) -> Result<ffi::types::GLuint, GlesError> {
let program = gl.CreateProgram();
gl.AttachShader(program, vert);
gl.AttachShader(program, frag);
gl.LinkProgram(program);
// gl.DetachShader(program, vert);
// gl.DetachShader(program, frag);
// gl.DeleteShader(vert);
// gl.DeleteShader(frag);
let mut status = ffi::FALSE as i32;
gl.GetProgramiv(program, ffi::LINK_STATUS, &mut status as *mut _);
if status == ffi::FALSE as i32 {
let mut max_len = 0;
gl.GetProgramiv(program, ffi::INFO_LOG_LENGTH, &mut max_len as *mut _);
let mut error = Vec::with_capacity(max_len as usize);
let mut len = 0;
gl.GetProgramInfoLog(
program,
max_len as _,
&mut len as *mut _,
error.as_mut_ptr() as *mut _,
);
error.set_len(len as usize);
error!(
"[GL] {}",
std::str::from_utf8(&error).unwrap_or("<Error Message no utf8>")
);
gl.DeleteProgram(program);
return Err(GlesError::ProgramLinkError);
}
Ok(program)
}
pub unsafe fn shader_inject(
c: &Gles2,
sk_shader: &mut Shader,
vert_str: &str,
frag_str: &str,
) -> Result<(), GlesError> {
let gl_vert = compile_shader(c, VERTEX_SHADER, vert_str)?;
let gl_frag = compile_shader(c, FRAGMENT_SHADER, frag_str)?;
let gl_prog = link_program(c, gl_vert, gl_frag)?;
let shader: *mut FfiShader = transmute(sk_shader.0.as_mut());
if let Some(shader) = shader.as_mut() {
shader.shader.vertex = gl_vert;
shader.shader.pixel = gl_frag;
shader.shader.program = gl_prog;
}
Ok(())
}

View File

@@ -3,7 +3,7 @@ use crate::{
nodes::{
drawable::Drawable,
spatial::{find_spatial_parent, parse_transform, Spatial},
Node,
Message, Node,
},
};
use color_eyre::eyre::{bail, ensure, eyre, Result};
@@ -13,7 +13,6 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use prisma::{Flatten, Rgba};
use send_wrapper::SendWrapper;
use serde::Deserialize;
use stardust_xr::{schemas::flex::deserialize, values::Transform};
use std::{ffi::OsStr, path::PathBuf, sync::Arc};
@@ -35,7 +34,7 @@ pub struct Text {
enabled: Arc<AtomicBool>,
space: Arc<Spatial>,
font_path: Option<PathBuf>,
style: OnceCell<SendWrapper<TextStyle>>,
style: OnceCell<TextStyle>,
data: Mutex<TextData>,
}
@@ -91,18 +90,16 @@ impl Text {
}
fn draw(&self, sk: &impl StereoKitDraw) {
let style = self.style.get_or_try_init(
|| -> Result<SendWrapper<TextStyle>, color_eyre::eyre::Error> {
let style = self
.style
.get_or_try_init(|| -> Result<TextStyle, color_eyre::eyre::Error> {
let font = self
.font_path
.as_deref()
.and_then(|path| sk.font_create(path).ok())
.unwrap_or_else(|| sk.font_find("default/font").unwrap());
Ok(SendWrapper::new(unsafe {
sk.text_make_style(font, 1.0, WHITE)
}))
},
);
Ok(unsafe { sk.text_make_style(font, 1.0, WHITE) })
});
if let Ok(style) = style {
let data = self.data.lock();
@@ -118,7 +115,7 @@ impl Text {
transform,
bounds / data.character_height,
data.fit,
**style,
*style,
data.bounds_align,
data.text_align,
vec3(0.0, 0.0, 0.0),
@@ -133,7 +130,7 @@ impl Text {
sk.text_add_at(
&data.text,
transform,
**style,
*style,
data.bounds_align,
data.text_align,
vec3(0.0, 0.0, 0.0),
@@ -151,18 +148,22 @@ impl Text {
pub fn set_character_height_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")};
text.data.lock().character_height = deserialize(data)?;
text.data.lock().character_height = deserialize(message.as_ref())?;
Ok(())
}
pub fn set_text_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn set_text_flex(
node: &Node,
_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(data)?;
text.data.lock().text = deserialize(message.as_ref())?;
Ok(())
}
}
@@ -183,7 +184,7 @@ pub fn draw_all(sk: &impl StereoKitDraw) {
}
}
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateTextInfo<'a> {
name: &'a str,
@@ -198,7 +199,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Re
bounds_align: TextAlign,
color: [f32; 4],
}
let info: CreateTextInfo = deserialize(data)?;
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);

View File

@@ -1,6 +1,7 @@
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;
@@ -16,7 +17,7 @@ pub struct BoxField {
}
impl BoxField {
pub fn add_to(node: &Arc<Node>, size: Vector3<f32>) -> Result<()> {
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!"
@@ -31,17 +32,22 @@ impl BoxField {
};
box_field.add_field_methods(node);
node.add_local_signal("set_size", BoxField::set_size_flex);
let _ = node.field.set(Arc::new(Field::Box(box_field)));
Ok(())
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>, data: &[u8]) -> Result<()> {
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(data)?);
box_field.set_size(deserialize(message.as_ref())?);
Ok(())
}
@@ -63,7 +69,11 @@ impl FieldTrait for BoxField {
}
}
pub fn create_box_field_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_box_field_flex(
_node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
name: &'a str,
@@ -71,7 +81,7 @@ pub fn create_box_field_flex(_node: &Node, calling_client: Arc<Client>, data: &[
transform: Transform,
size: Vector3<f32>,
}
let info: CreateFieldInfo = deserialize(data)?;
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);

View File

@@ -1,6 +1,7 @@
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;
@@ -43,9 +44,13 @@ impl CylinderField {
self.radius.store(radius.abs(), Ordering::Relaxed);
}
pub fn set_size_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?;
let (length, radius) = deserialize(message.as_ref())?;
cylinder_field.set_size(length, radius);
Ok(())
}
@@ -67,7 +72,7 @@ impl FieldTrait for CylinderField {
pub fn create_cylinder_field_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
@@ -77,7 +82,7 @@ pub fn create_cylinder_field_flex(
length: f32,
radius: f32,
}
let info: CreateFieldInfo = deserialize(data)?;
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);

View File

@@ -1,7 +1,7 @@
mod r#box;
pub mod r#box;
mod cylinder;
mod sphere;
pub mod torus;
mod torus;
use self::cylinder::{create_cylinder_field_flex, CylinderField};
use self::r#box::{create_box_field_flex, BoxField};
@@ -10,8 +10,9 @@ use self::torus::{create_torus_field_flex, TorusField};
use super::alias::AliasInfo;
use super::spatial::Spatial;
use super::Node;
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};
@@ -133,76 +134,98 @@ 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>, data: &[u8]) -> Result<Vec<u8>> {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
}
let args: FieldInfoArgs = deserialize(data)?;
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
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)?)
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>, data: &[u8]) -> Result<Vec<u8>> {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
radius: Option<f32>,
}
let args: FieldInfoArgs = deserialize(data)?;
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
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(),
args.radius.unwrap_or(0.001),
);
Ok(serialize(mint::Vector3::from(normal))?)
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>,
data: &[u8],
) -> Result<Vec<u8>> {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
radius: Option<f32>,
}
let args: FieldInfoArgs = deserialize(data)?;
let reference_space = find_reference_space(&calling_client, args.reference_space_path)?;
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(),
args.radius.unwrap_or(0.001),
);
Ok(serialize(mint::Vector3::from(closest_point))?)
}
fn field_ray_march_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<Vec<u8>> {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
ray_origin: Vector3<f32>,
ray_direction: Vector3<f32>,
}
let args: FieldInfoArgs = deserialize(data)?;
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,
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())
});
Ok(serialize(ray_march_result)?)
}
pub enum Field {

View File

@@ -1,6 +1,7 @@
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;
@@ -39,9 +40,13 @@ impl SphereField {
self.radius.store(radius, Ordering::Relaxed);
}
pub fn set_radius_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?);
sphere_field.set_radius(deserialize(message.as_ref())?);
Ok(())
}
}
@@ -64,7 +69,7 @@ impl FieldTrait for SphereField {
pub fn create_sphere_field_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
@@ -73,7 +78,7 @@ pub fn create_sphere_field_flex(
origin: Option<Vector3<f32>>,
radius: f32,
}
let info: CreateFieldInfo = deserialize(data)?;
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(

View File

@@ -1,6 +1,7 @@
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;
@@ -43,9 +44,13 @@ impl TorusField {
self.radius_b.store(radius_b.abs(), Ordering::Relaxed);
}
pub fn set_size_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?;
let (radius_a, radius_b) = deserialize(message.as_ref())?;
torus_field.set_size(radius_a, radius_b);
Ok(())
@@ -67,7 +72,7 @@ impl FieldTrait for TorusField {
pub fn create_torus_field_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
@@ -77,7 +82,7 @@ pub fn create_torus_field_flex(
radius_a: f32,
radius_b: f32,
}
let info: CreateFieldInfo = deserialize(data)?;
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);

View File

@@ -7,7 +7,6 @@ use color_eyre::eyre::Result;
use glam::{vec3, Mat4};
use std::sync::Arc;
use stereokit::StereoKitMultiThread;
use tracing::instrument;
lazy_static::lazy_static! {
static ref HMD: Arc<Node> = create();
@@ -20,7 +19,6 @@ fn create() -> Arc<Node> {
node
}
#[instrument(level = "debug", name = "Update HMD Pose", skip(sk))]
pub fn frame(sk: &impl StereoKitMultiThread) {
let spatial = HMD
.spatial

View File

@@ -31,7 +31,7 @@ impl InputSpecialization for Hand {
}
fn serialize(
&self,
_distance_link: &DistanceLink,
distance_link: &DistanceLink,
local_to_handler_matrix: Mat4,
) -> InputDataType {
let mut hand = self.base;
@@ -68,6 +68,10 @@ impl InputSpecialization for Hand {
let (_, rotation, position) = joint_matrix.to_scale_rotation_translation();
joint.position = position.into();
joint.rotation = rotation.into();
joint.distance = distance_link
.handler
.field
.distance(&distance_link.handler.spatial, position.into());
}
InputDataType::Hand(Box::new(hand))

View File

@@ -10,10 +10,10 @@ use super::{
alias::{Alias, AliasInfo},
fields::{find_field, Field, FIELD_ALIAS_INFO},
spatial::{find_spatial_parent, parse_transform, Spatial},
Node,
Message, Node,
};
use crate::core::registry::Registry;
use crate::core::{client::Client, node_collections::LifeLinkedNodeMap};
use crate::core::{node_collections::LifeLinkedNodeList, registry::Registry};
use color_eyre::eyre::{ensure, Result};
use glam::Mat4;
use once_cell::sync::OnceCell;
@@ -99,6 +99,7 @@ impl InputMethod {
};
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());
@@ -109,21 +110,21 @@ impl InputMethod {
.cloned()
}
fn capture_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn capture_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
let method = InputMethod::get(node)?;
let handler = InputHandler::find(&calling_client, deserialize(data)?)?;
let handler = InputHandler::find(&calling_client, deserialize(message.as_ref())?)?;
method.captures.add_raw(&handler);
node.send_remote_signal("capture", data)
node.send_remote_signal("capture", message)
}
fn set_datamap_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn set_datamap_flex(node: &Node, _calling_client: Arc<Client>, message: Message) -> Result<()> {
let method = InputMethod::get(node)?;
method.datamap.lock().replace(Datamap::new(data.to_vec())?);
method.datamap.lock().replace(Datamap::new(message.data)?);
Ok(())
}
fn set_handlers_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn set_handlers_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
let method = InputMethod::get(node)?;
let handler_paths: Vec<&str> = deserialize(data)?;
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())
@@ -137,6 +138,26 @@ impl InputMethod {
Ok(())
}
fn make_alias(&self, handler: &InputHandler) {
let Some(method_node) = self.node.upgrade() else {return};
let Some(handler_node) = handler.node.upgrade() else {return};
let Some(client) = handler_node.get_client() else {return};
let Ok(method_alias) = Alias::create(
&client,
handler_node.get_path(),
&self.uid,
&method_node,
AliasInfo {
server_signals: vec!["capture"],
..Default::default()
},
) else {return};
method_alias.enabled.store(false, Ordering::Relaxed);
handler
.method_aliases
.add(self as *const InputMethod as usize, &method_alias);
}
fn compare_distance(&self, to: &Field) -> f32 {
self.specialization
.lock()
@@ -147,9 +168,9 @@ impl InputMethod {
}
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 };
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,
@@ -178,7 +199,7 @@ impl InputMethod {
}
let Ok(data) = serialize(&handler.uid) else {return};
let _ = method_node.send_remote_signal("handler_created", &data);
let _ = method_node.send_remote_signal("handler_created", data);
}
fn handle_drop_handler(&self, handler: &InputHandler) {
let uid = handler.uid.as_str();
@@ -186,7 +207,7 @@ impl InputMethod {
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);
let _ = tx_node.send_remote_signal("handler_destroyed", data);
}
}
impl Drop for InputMethod {
@@ -201,25 +222,12 @@ pub struct DistanceLink {
handler: Arc<InputHandler>,
}
impl DistanceLink {
fn from(method: Arc<InputMethod>, handler: Arc<InputHandler>) -> Option<Self> {
let handler_node = handler.node.upgrade()?;
let method_alias = Alias::create(
&handler_node.get_client()?,
handler_node.get_path(),
&method.uid,
&method.node.upgrade()?,
AliasInfo {
server_signals: vec!["capture"],
..Default::default()
},
)
.ok()?;
handler.method_aliases.add(Arc::downgrade(&method_alias));
Some(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) {
@@ -249,7 +257,7 @@ pub struct InputHandler {
node: Weak<Node>,
spatial: Arc<Spatial>,
field: Arc<Field>,
method_aliases: LifeLinkedNodeList,
method_aliases: LifeLinkedNodeMap<usize>,
}
impl InputHandler {
pub fn add_to(node: &Arc<Node>, field: &Arc<Field>) -> Result<()> {
@@ -264,9 +272,10 @@ impl InputHandler {
node: Arc::downgrade(node),
spatial: node.spatial.get().unwrap().clone(),
field: field.clone(),
method_aliases: LifeLinkedNodeList::default(),
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);
@@ -284,7 +293,7 @@ impl InputHandler {
#[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));
let _ = node.send_remote_signal("input", distance_link.serialize(order, datamap));
}
}
impl PartialEq for InputHandler {
@@ -312,7 +321,7 @@ pub fn create_interface(client: &Arc<Client>) -> Result<()> {
pub fn create_input_handler_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateInputHandlerInfo<'a> {
@@ -321,7 +330,7 @@ pub fn create_input_handler_flex(
transform: Transform,
field_path: &'a str,
}
let info: CreateInputHandlerInfo = deserialize(data)?;
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)?;
@@ -343,10 +352,12 @@ pub fn process_input() {
.filter(|method| method.datamap.lock().is_some())
});
let handlers = INPUT_HANDLER_REGISTRY.get_valid_contents();
for handler in &handlers {
handler.method_aliases.clear();
}
const LIMIT: usize = 50;
for method in methods {
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")
@@ -357,14 +368,16 @@ pub fn process_input() {
.iter()
.filter_map(|h| h.upgrade())
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
.filter_map(|handler| DistanceLink::from(method.clone(), handler))
.map(|handler| DistanceLink::from(method.clone(), handler))
.collect()
} else {
let mut distance_links: Vec<_> = handlers
.iter()
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
.filter_map(|handler| {
DistanceLink::from(method.clone(), handler.clone())
.map(|handler| {
debug_span!("Create distance link").in_scope(|| {
DistanceLink::from(method.clone(), handler.clone())
})
})
.collect();
@@ -375,6 +388,7 @@ pub fn process_input() {
});
});
distance_links.truncate(LIMIT);
distance_links
}
});
@@ -382,6 +396,18 @@ pub fn process_input() {
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,

View File

@@ -3,7 +3,7 @@ use crate::core::client::Client;
use crate::nodes::fields::{Field, Ray, RayMarchResult};
use crate::nodes::input::{InputMethod, InputType};
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use crate::nodes::Node;
use crate::nodes::{Message, Node};
use glam::{vec3, Mat4};
use serde::Deserialize;
use stardust_xr::schemas::flat::{Datamap, InputDataType, Pointer as FlatPointer};
@@ -34,9 +34,13 @@ impl Pointer {
impl InputSpecialization for Pointer {
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
let ray_info = self.ray_march(space, field);
ray_info
.deepest_point_distance
.hypot(ray_info.min_distance.recip())
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);
@@ -63,7 +67,7 @@ impl InputSpecialization for Pointer {
pub fn create_pointer_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> color_eyre::eyre::Result<()> {
#[derive(Deserialize)]
struct CreatePointerInfo<'a> {
@@ -72,7 +76,7 @@ pub fn create_pointer_flex(
transform: Transform,
datamap: Option<Vec<u8>>,
}
let info: CreatePointerInfo = deserialize(data)?;
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);

View File

@@ -3,7 +3,7 @@ use crate::core::client::Client;
use crate::nodes::fields::Field;
use crate::nodes::input::{InputMethod, InputType};
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use crate::nodes::Node;
use crate::nodes::{Message, Node};
use color_eyre::eyre::Result;
use glam::{vec3a, Mat4};
use serde::Deserialize;
@@ -17,9 +17,9 @@ pub struct Tip {
pub radius: f32,
}
impl Tip {
fn set_radius(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?;
tip.radius = deserialize(message.as_ref())?;
}
Ok(())
}
@@ -45,7 +45,7 @@ impl InputSpecialization for Tip {
}
}
pub fn create_tip_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_tip_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateTipInfo<'a> {
name: &'a str,
@@ -54,7 +54,7 @@ pub fn create_tip_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -
radius: f32,
datamap: Option<Vec<u8>>,
}
let info: CreateTipInfo = deserialize(data)?;
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);

380
src/nodes/items/camera.rs Normal file
View File

@@ -0,0 +1,380 @@
use super::{Item, ItemType};
use crate::{
core::{
buffers::BufferManager,
client::{Client, INTERNAL_CLIENT},
registry::Registry,
scenegraph::MethodResponseSender,
},
nodes::{
drawable::{model::ModelPart, shaders::UNLIT_SHADER_BYTES, Drawable},
items::TypeInfo,
spatial::{find_spatial_parent, parse_transform, Spatial},
Message, Node,
},
};
use color_eyre::eyre::{bail, Result};
use glam::Mat4;
use lazy_static::lazy_static;
use mint::{RowMatrix4, Vector2};
use nanoid::nanoid;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use serde::Deserialize;
use smithay::backend::{
allocator::{
dmabuf::{Dmabuf, DmabufFlags},
Buffer,
},
renderer::{gles, ImportDma},
};
use stardust_xr::{
scenegraph::ScenegraphError,
schemas::flex::{deserialize, serialize},
values::{BufferColorspace, 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};
lazy_static! {
pub(super) static ref ITEM_TYPE_INFO_CAMERA: TypeInfo = TypeInfo {
type_name: "camera",
aliased_local_signals: vec!["apply_preview_material"],
aliased_local_methods: vec!["render"],
aliased_remote_signals: vec![],
ui: Default::default(),
items: Registry::new(),
acceptors: Registry::new(),
};
}
struct FrameInfo {
proj_matrix: Mat4,
preview_size: Vector2<u32>,
}
pub struct CameraItem {
space: Arc<Spatial>,
frame_info: Mutex<FrameInfo>,
sk_tex: OnceCell<Tex>,
sk_mat: OnceCell<Arc<Material>>,
render_requests_tx: mpsc::UnboundedSender<(Dmabuf, BufferColorspace, MethodResponseSender)>,
render_requests_rx:
Mutex<mpsc::UnboundedReceiver<(Dmabuf, BufferColorspace, MethodResponseSender)>>,
rendered_notifiers: Mutex<Vec<MethodResponseSender>>,
applied_preview_to: Registry<ModelPart>,
apply_preview_to: Registry<ModelPart>,
}
impl CameraItem {
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(
node,
nanoid!(),
&ITEM_TYPE_INFO_CAMERA,
ItemType::Camera(camera_specialization),
);
node.add_local_method("render", CameraItem::render_flex);
node.add_local_signal(
"apply_preview_material",
CameraItem::apply_preview_material_flex,
);
}
fn render_flex(
node: &Node,
_calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
let Some(item) = node.item.get() else {
let _ = response.send(Err(ScenegraphError::MethodError {
error: "Item not found".to_string(),
}));
return;
};
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, _colorspace, sender))) =
camera
.render_requests_tx
.send((buffer_to_render, buffer_info.colorspace, response))
{
sender.send(Err(ScenegraphError::MethodError {
error: "Internal: sender broke????".to_string(),
}));
}
}
fn apply_preview_material_flex(
node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
let Some(item) = node.item.get() else {
bail!("Item not found?")
};
let ItemType::Camera(camera) = &item.specialization else {
bail!("Wrong item type?")
};
let model_part_path = deserialize(&message.data)?;
let model_part_node = calling_client.get_node("Model part", model_part_path)?;
let Drawable::ModelPart(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(())
}
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();
self.render_preview(sk, &*frame_info);
self.render_dmabuf(sk, buffer_manager, &*frame_info);
}
fn render_preview(&self, sk: &impl StereoKitDraw, frame_info: &FrameInfo) {
if !self.apply_preview_to.is_empty() {
let sk_tex = self.sk_tex.get_or_init(|| {
sk.tex_gen_color(
Color128::default(),
frame_info.preview_size.x as i32,
frame_info.preview_size.y as i32,
TextureType::RENDER_TARGET,
TextureFormat::RGBA32Linear,
)
});
let sk_mat = self.sk_mat.get_or_init(|| {
let shader = sk.shader_create_mem(&UNLIT_SHADER_BYTES).unwrap();
let mat = sk.material_create(&shader);
sk.material_set_texture(&mat, "diffuse", sk_tex.as_ref());
sk.material_set_transparency(&mat, Transparency::Blend);
Arc::new(mat)
});
for model_part in self.apply_preview_to.take_valid_contents() {
model_part.replace_material(sk_mat.clone())
}
}
if !self.applied_preview_to.is_empty() {
sk.render_to(
self.sk_tex.get().unwrap(),
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,
},
);
}
}
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, colorspace, 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 texture_format;
let native_format;
match colorspace {
BufferColorspace::SRGB => match buffer_to_render.format().code {
drm_fourcc::DrmFourcc::Argb8888 => {
texture_format = TextureFormat::RGBA32;
native_format = gles::ffi::SRGB8_ALPHA8;
}
drm_fourcc::DrmFourcc::Abgr8888 => {
texture_format = TextureFormat::BGRA32;
native_format = gles::ffi::SRGB8_ALPHA8;
}
_ => {
let _ = render_response_sender.send(Err(ScenegraphError::MethodError {
error: "Unsupported pixel format".to_string(),
}));
continue;
}
},
BufferColorspace::Linear => match buffer_to_render.format().code {
drm_fourcc::DrmFourcc::Argb8888 => {
texture_format = TextureFormat::RGBA32Linear;
native_format = gles::ffi::RGBA8;
}
drm_fourcc::DrmFourcc::Abgr8888 => {
texture_format = TextureFormat::BGRA32Linear;
native_format = gles::ffi::RGBA8;
}
_ => {
let _ = render_response_sender.send(Err(ScenegraphError::MethodError {
error: "Unsupported pixel format".to_string(),
}));
continue;
}
},
}
// TODO: This doesn't seem to care about the texture format, it seems to always output with sRGB.
let sk_tex = sk.tex_create(TextureType::IMAGE_NO_MIPS, texture_format);
unsafe {
sk.tex_set_surface(
&sk_tex,
smithay_tex.tex_id() as usize as *mut c_void,
TextureType::RENDER_TARGET,
native_format.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()));
}
}
}
pub fn update(sk: &impl StereoKitDraw, buffer_manager: &mut BufferManager) {
for camera in ITEM_TYPE_INFO_CAMERA.items.get_valid_contents() {
let ItemType::Camera(camera) = &camera.specialization else {
continue;
};
camera.update(sk, buffer_manager);
}
}
pub fn send_rendered() {
for camera in ITEM_TYPE_INFO_CAMERA.items.get_valid_contents() {
let ItemType::Camera(camera) = &camera.specialization else {
continue;
};
camera.send_rendered();
}
}
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(())
}

View File

@@ -1,13 +1,14 @@
use super::{Item, ItemSpecialization, ItemType};
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},
Node,
Message, Node,
},
};
use color_eyre::eyre::{eyre, Result};
@@ -15,7 +16,7 @@ use lazy_static::lazy_static;
use nanoid::nanoid;
use serde::Deserialize;
use stardust_xr::{
schemas::flex::{deserialize, flexbuffers, serialize},
schemas::flex::{deserialize, serialize},
values::Transform,
};
use std::sync::Arc;
@@ -46,23 +47,29 @@ impl EnvironmentItem {
node.add_local_method("get_path", EnvironmentItem::get_path_flex);
}
fn get_path_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<Vec<u8>> {
let ItemType::Environment(environment_item) = &node.item.get().unwrap().specialization else {
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(flexbuffers::singleton(environment_item.path.as_str()))
Ok(serialize(environment_item.path.as_str())?.into())
});
}
}
impl ItemSpecialization for EnvironmentItem {
fn serialize_start_data(&self, id: &str) -> Vec<u8> {
serialize((id, self.path.as_str())).unwrap()
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>,
data: &[u8],
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateEnvironmentItemInfo<'a> {
@@ -71,7 +78,7 @@ pub(super) fn create_environment_item_flex(
transform: Transform,
item_data: String,
}
let info: CreateEnvironmentItemInfo = deserialize(data)?;
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);

View File

@@ -1,26 +1,27 @@
pub mod camera;
mod environment;
pub mod panel;
use self::camera::CameraItem;
use self::environment::{EnvironmentItem, ITEM_TYPE_INFO_ENVIRONMENT};
use self::panel::{PanelItemTrait, ITEM_TYPE_INFO_PANEL};
use super::fields::Field;
use super::spatial::{find_spatial_parent, parse_transform, Spatial};
use super::{Alias, Node};
use super::{Alias, Message, Node};
use crate::core::client::Client;
use crate::core::node_collections::LifeLinkedNodeMap;
use crate::core::registry::Registry;
use crate::nodes::alias::AliasInfo;
use crate::nodes::fields::find_field;
#[cfg(feature = "wayland")]
use crate::wayland::panel_item::{PanelItem, ITEM_TYPE_INFO_PANEL};
use color_eyre::eyre::{ensure, eyre, Result};
use lazy_static::lazy_static;
use nanoid::nanoid;
use parking_lot::Mutex;
use portable_atomic::Ordering;
use serde::Deserialize;
use stardust_xr::schemas::flex::{deserialize, flexbuffers, serialize};
use stardust_xr::schemas::flex::{deserialize, serialize};
use stardust_xr::values::Transform;
use std::hash::Hash;
use std::ops::Deref;
use std::sync::{Arc, Weak};
lazy_static! {
@@ -38,8 +39,8 @@ lazy_static! {
}
pub fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
if let Some(acceptor) = item.captured_acceptor.lock().upgrade() {
release(item, Some(&acceptor));
if item.captured_acceptor.lock().strong_count() > 0 {
release(item);
}
*item.captured_acceptor.lock() = Arc::downgrade(acceptor);
acceptor.handle_capture(item);
@@ -47,9 +48,9 @@ pub fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
ui.handle_capture_item(item, acceptor);
}
}
fn release(item: &Item, acceptor: Option<&ItemAcceptor>) {
fn release(item: &Item) {
let mut captured_acceptor = item.captured_acceptor.lock();
if let Some(acceptor) = captured_acceptor.upgrade().as_deref().or(acceptor) {
if let Some(acceptor) = captured_acceptor.upgrade().as_ref() {
*captured_acceptor = Weak::default();
acceptor.handle_release(item);
if let Some(ui) = item.type_info.ui.lock().upgrade() {
@@ -108,15 +109,15 @@ impl Item {
}
let _ = node.item.set(item.clone());
if let Some(auto_acceptor) = node.get_client().and_then(|client| {
client
.startup_settings
.as_ref()
.and_then(|settings| settings.acceptors.get(type_info))
.and_then(|acceptor| acceptor.upgrade())
}) {
capture(&item, &auto_acceptor);
}
// if let Some(auto_acceptor) = node.get_client().and_then(|client| {
// client
// .state
// .as_ref()
// .and_then(|settings| settings.acceptors.get(type_info))
// .and_then(|acceptor| acceptor.upgrade())
// }) {
// capture(&item, &auto_acceptor);
// }
item
}
@@ -154,9 +155,9 @@ impl Item {
self.make_alias_named(client, parent, &self.uid)
}
fn release_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
fn release_flex(node: &Node, _calling_client: Arc<Client>, _message: Message) -> Result<()> {
let item = node.get_aspect("Item", "item", |n| &n.item)?;
release(item, None);
release(item);
Ok(())
}
@@ -164,33 +165,37 @@ impl Item {
impl Drop for Item {
fn drop(&mut self) {
self.type_info.items.remove(self);
release(self, None);
release(self);
if let Some(ui) = self.type_info.ui.lock().upgrade() {
ui.handle_destroy_item(self);
}
}
}
pub trait ItemSpecialization {
fn serialize_start_data(&self, id: &str) -> Vec<u8>;
}
pub enum ItemType {
Camera(CameraItem),
Environment(EnvironmentItem),
#[cfg(feature = "wayland")]
Panel(Arc<PanelItem>),
Panel(Arc<dyn PanelItemTrait>),
}
impl Deref for ItemType {
type Target = dyn ItemSpecialization;
fn deref(&self) -> &Self::Target {
impl ItemType {
fn serialize_start_data(&self, id: &str) -> Result<Message> {
match self {
ItemType::Environment(item) => item,
#[cfg(feature = "wayland")]
ItemType::Panel(item) => item.as_ref(),
ItemType::Camera(c) => c.serialize_start_data(id),
ItemType::Environment(e) => e.serialize_start_data(id),
ItemType::Panel(p) => p.serialize_start_data(id),
}
}
}
// impl Deref for ItemType {
// type Target = dyn ItemSpecialization;
// fn deref(&self) -> &Self::Target {
// match self {
// ItemType::Environment(item) => item,
// ItemType::Panel(item) => item.as_ref(),
// }
// }
// }
pub struct ItemUI {
node: Weak<Node>,
@@ -225,49 +230,44 @@ impl ItemUI {
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, flexbuffers::singleton(name).as_slice());
.send_remote_signal(state, serialized_data);
}
fn handle_create_item(&self, item: &Item) {
let Some(node) = self.node.upgrade() else { return };
let Some(client) = node.get_client() else { return };
let Some(node) = self.node.upgrade() else {return};
let Some(client) = node.get_client() else {return};
if let Ok(alias_node) = item.make_alias(&client, &(node.get_path().to_string() + "/item")) {
self.item_aliases.add(item.uid.clone(), &alias_node);
}
let _ = node.send_remote_signal(
"create_item",
&item.specialization.serialize_start_data(&item.uid),
);
let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return};
let _ = node.send_remote_signal("create_item", serialized_data);
}
fn handle_destroy_item(&self, item: &Item) {
self.item_aliases.remove(&item.uid);
self.send_state("destroy_item", item.uid.as_str());
}
fn handle_capture_item(&self, item: &Item, acceptor: &ItemAcceptor) {
let Some(node) = self.node.upgrade() else { return };
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal(
"capture_item",
&serialize((item.uid.as_str(), acceptor.uid.as_str())).unwrap(),
);
let Ok(message) = serialize((item.uid.as_str(), acceptor.uid.as_str())) else {return};
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 Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal(
"release_item",
&serialize((item.uid.as_str(), acceptor.uid.as_str())).unwrap(),
);
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) {
let Some(node) = self.node.upgrade() else { return };
let Some(client) = node.get_client() else { return };
let Some(node) = self.node.upgrade() else {return};
let Some(client) = node.get_client() else {return};
let Ok((alias, field_alias)) = acceptor.make_aliases(
&client,
@@ -276,7 +276,8 @@ impl ItemUI {
self.acceptor_aliases.add(acceptor.uid.clone(), &alias);
self.acceptor_field_aliases
.add(acceptor.uid.clone(), &field_alias);
let _ = node.send_remote_signal("create_acceptor", &serialize(&acceptor.uid).unwrap());
let Ok(message) = serialize(&acceptor.uid) else {return};
let _ = node.send_remote_signal("create_acceptor", message);
}
fn handle_destroy_acceptor(&self, acceptor: &ItemAcceptor) {
self.send_state("destroy_acceptor", acceptor.uid.as_str());
@@ -315,13 +316,13 @@ impl ItemAcceptor {
let _ = node.item_acceptor.set(acceptor);
}
fn capture_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
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(data)?;
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);
@@ -353,31 +354,31 @@ impl ItemAcceptor {
Ok((acceptor_alias, acceptor_field_alias))
}
fn handle_capture(&self, item: &Arc<Item>) {
let Some(node) = self.node.upgrade() else { return };
let Some(client) = node.get_client() else { return };
let Some(node) = self.node.upgrade() else {return};
let Some(client) = node.get_client() else {return};
self.accepted_registry.add_raw(item);
if let Ok(alias_node) = item.make_alias(&client, &node.path) {
self.accepted_aliases.add(item.uid.clone(), &alias_node);
}
let _ = node.send_remote_signal(
"capture",
&item.specialization.serialize_start_data(&item.uid),
);
let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return};
let _ = node.send_remote_signal("capture", serialized_data);
}
fn handle_release(&self, item: &Item) {
let Some(node) = self.node.upgrade() else { return };
let Some(node) = self.node.upgrade() else {return};
self.accepted_registry.remove(item);
self.accepted_aliases.remove(&item.uid);
let _ = node.send_remote_signal("release", &serialize(&item.uid).unwrap());
let Ok(message) = serialize(&item.uid) else {return};
let _ = node.send_remote_signal("release", message);
}
}
impl Drop for ItemAcceptor {
fn drop(&mut self) {
self.type_info.acceptors.remove(self);
for item in self.accepted_registry.get_valid_contents() {
release(&item, Some(self));
release(&item);
}
if let Some(ui) = self.type_info.ui.lock().upgrade() {
ui.handle_destroy_acceptor(self);
@@ -387,6 +388,7 @@ 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,
@@ -405,12 +407,16 @@ fn type_info(name: &str) -> Result<&'static TypeInfo> {
}
}
pub fn register_item_ui_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn register_item_ui_flex(
_node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct RegisterItemUIInfo<'a> {
item_type: &'a str,
}
let info: RegisterItemUIInfo = deserialize(data)?;
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()?;
@@ -418,7 +424,11 @@ pub fn register_item_ui_flex(_node: &Node, calling_client: Arc<Client>, data: &[
Ok(())
}
fn create_item_acceptor_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn create_item_acceptor_flex(
_node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateItemAcceptorInfo<'a> {
name: &'a str,
@@ -427,7 +437,7 @@ fn create_item_acceptor_flex(_node: &Node, calling_client: Arc<Client>, data: &[
field_path: &'a str,
item_type: &'a str,
}
let info: CreateItemAcceptorInfo = deserialize(data)?;
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)?;

542
src/nodes/items/panel.rs Normal file
View File

@@ -0,0 +1,542 @@
use crate::{
core::{
client::{get_env, state, Client, INTERNAL_CLIENT},
registry::Registry,
},
nodes::{
drawable::{model::ModelPart, Drawable},
items::{Item, ItemType, TypeInfo},
spatial::Spatial,
Message, Node,
},
};
use color_eyre::eyre::{bail, eyre, Result};
use glam::Mat4;
use lazy_static::lazy_static;
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 tracing::debug;
lazy_static! {
pub static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo {
type_name: "panel",
aliased_local_signals: vec![
"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(),
items: Registry::new(),
acceptors: Registry::new(),
};
}
/// 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 {
fn start_data(&self) -> Result<PanelItemInitData>;
fn apply_surface_material(&self, surface: SurfaceID, model_part: &Arc<ModelPart>);
fn close_toplevel(&self);
fn auto_size_toplevel(&self);
fn set_toplevel_size(&self, size: Vector2<u32>);
fn set_toplevel_focused_visuals(&self, focused: bool);
fn pointer_motion(&self, surface: &SurfaceID, position: Vector2<f32>);
fn pointer_button(&self, surface: &SurfaceID, button: u32, pressed: bool);
fn pointer_scroll(
&self,
surface: &SurfaceID,
scroll_distance: Option<Vector2<f32>>,
scroll_steps: Option<Vector2<f32>>,
);
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_move(&self, id: u32, position: Vector2<f32>);
fn touch_up(&self, id: u32);
fn reset_touches(&self);
}
pub fn panel_item_from_node(node: &Node) -> Option<Arc<dyn PanelItemTrait>> {
let ItemType::Panel(panel_item) = &node.item.get()?.specialization else {return None};
Some(panel_item.clone())
}
pub trait PanelItemTrait: Backend + Send + Sync + 'static {
fn uid(&self) -> &str;
fn serialize_start_data(&self, id: &str) -> Result<Message>;
}
pub struct PanelItem<B: Backend + ?Sized> {
pub uid: String,
node: Weak<Node>,
pub backend: Box<B>,
}
impl<B: Backend + ?Sized> PanelItem<B> {
pub fn create(backend: Box<B>, pid: Option<i32>) -> Arc<PanelItem<B>> {
debug!(?pid, "Create panel item");
let startup_settings = pid
.and_then(|pid| get_env(pid).ok())
.and_then(|env| state(&env));
let uid = nanoid!();
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 {
spatial.set_local_transform(startup_settings.root);
}
let panel_item = Arc::new(PanelItem {
uid: uid.clone(),
node: Arc::downgrade(&node),
backend,
});
let generic_panel_item: Arc<dyn PanelItemTrait> = panel_item.clone();
Item::add_to(
&node,
uid,
&ITEM_TYPE_INFO_PANEL,
ItemType::Panel(generic_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
impl<B: Backend + ?Sized> PanelItem<B> {
pub fn toplevel_parent_changed(&self, parent: &str) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_parent_changed", serialize(parent).unwrap());
}
pub fn toplevel_title_changed(&self, title: &str) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_title_changed", serialize(title).unwrap());
}
pub fn toplevel_app_id_changed(&self, app_id: &str) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_app_id_changed", serialize(app_id).unwrap());
}
pub fn toplevel_fullscreen_active(&self, active: bool) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_fullscreen_active", serialize(active).unwrap());
}
pub fn toplevel_move_request(&self) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_move_request", Vec::<u8>::new());
}
pub fn toplevel_resize_request(&self, up: bool, down: bool, left: bool, right: bool) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal(
"toplevel_resize_request",
serialize((up, down, left, right)).unwrap(),
);
}
pub fn toplevel_size_changed(&self, size: Vector2<u32>) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("toplevel_size_changed", serialize(size).unwrap());
}
pub fn set_cursor(&self, geometry: Option<Geometry>) {
let Some(node) = self.node.upgrade() else {return};
let _ = node.send_remote_signal("set_cursor", serialize(geometry).unwrap());
}
pub fn new_child(&self, uid: &str, info: ChildInfo) {
let Some(node) = self.node.upgrade() else {return};
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(())
}
};
}
macro_rules! flex_deserialize {
($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(deserialize(message.as_ref())?);
Ok(())
}
};
}
impl<B: Backend + ?Sized> PanelItem<B> {
fn apply_surface_material_flex(
node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
#[derive(Debug, Deserialize)]
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);
Ok(())
}
flex_no_args!(close_toplevel_flex, close_toplevel);
flex_no_args!(auto_size_toplevel_flex, auto_size_toplevel);
flex_deserialize!(set_toplevel_size_changed_flex, set_toplevel_size);
fn pointer_motion_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, position): (SurfaceID, Vector2<f32>) = deserialize(message.as_ref())?;
debug!(?surface_id, ?position, "Pointer deactivate");
panel_item.pointer_motion(&surface_id, position);
Ok(())
}
fn pointer_button_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, button, state): (SurfaceID, u32, u32) = deserialize(message.as_ref())?;
debug!(?surface_id, button, state, "Pointer button");
panel_item.pointer_button(&surface_id, button, state != 0);
Ok(())
}
fn pointer_scroll_flex(
node: &Node,
_calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) };
#[derive(Debug, Deserialize)]
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(())
}
fn keyboard_keys_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, keymap_id, keys): (SurfaceID, &str, Vec<i32>) =
deserialize(message.as_ref())?;
debug!(?keys, "Set keyboard key state");
panel_item.keyboard_keys(&surface_id, keymap_id, keys);
Ok(())
}
pub fn grab_keyboard(&self, sid: Option<SurfaceID>) {
let Some(node) = self.node.upgrade() else {return};
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(
&self,
surface: &SurfaceID,
scroll_distance: Option<Vector2<f32>>,
scroll_steps: Option<Vector2<f32>>,
) {
self.backend
.pointer_scroll(surface, scroll_distance, scroll_steps)
}
fn keyboard_keys(&self, surface: &SurfaceID, keymap_id: &str, keys: Vec<i32>) {
self.backend.keyboard_keys(surface, keymap_id, keys)
}
fn touch_down(&self, surface: &SurfaceID, id: u32, position: Vector2<f32>) {
self.backend.touch_down(surface, id, position)
}
fn touch_move(&self, id: u32, position: Vector2<f32>) {
self.backend.touch_move(id, position)
}
fn touch_up(&self, id: u32) {
self.backend.touch_up(id)
}
fn reset_touches(&self) {
self.backend.reset_touches()
}
}
impl<B: Backend + ?Sized> Drop for PanelItem<B> {
fn drop(&mut self) {
// Dropped panel item, basically just a debug breakpoint place
}
}

View File

@@ -8,26 +8,25 @@ pub mod input;
pub mod items;
pub mod root;
pub mod spatial;
pub mod startup;
use color_eyre::eyre::{eyre, Result};
use core::hash::BuildHasherDefault;
use dashmap::DashMap;
use nanoid::nanoid;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use rustc_hash::FxHasher;
use rustc_hash::FxHashMap;
use stardust_xr::messenger::MessageSenderHandle;
use stardust_xr::scenegraph::ScenegraphError;
use stardust_xr::schemas::flex::deserialize;
use std::fmt::Debug;
use std::future::Future;
use std::os::fd::OwnedFd;
use std::sync::{Arc, Weak};
use std::vec::Vec;
use tracing::{debug_span, instrument};
use crate::core::client::Client;
use crate::core::registry::Registry;
use crate::core::scenegraph::MethodResponseSender;
use self::alias::Alias;
use self::audio::Sound;
@@ -38,10 +37,28 @@ use self::input::{InputHandler, InputMethod};
use self::items::{Item, ItemAcceptor, ItemUI};
use self::spatial::zone::Zone;
use self::spatial::Spatial;
use self::startup::StartupSettings;
pub type Signal = fn(&Node, Arc<Client>, &[u8]) -> Result<()>;
pub type Method = fn(&Node, Arc<Client>, &[u8]) -> Result<Vec<u8>>;
#[derive(Default)]
pub struct Message {
pub data: Vec<u8>,
pub fds: Vec<OwnedFd>,
}
impl From<Vec<u8>> for Message {
fn from(data: Vec<u8>) -> Self {
Message {
data,
fds: Vec::new(),
}
}
}
impl AsRef<[u8]> for Message {
fn as_ref(&self) -> &[u8] {
&self.data
}
}
pub type Signal = fn(&Node, Arc<Client>, Message) -> Result<()>;
pub type Method = fn(&Node, Arc<Client>, Message, MethodResponseSender);
pub struct Node {
pub enabled: Arc<AtomicBool>,
@@ -50,8 +67,8 @@ pub struct Node {
client: Weak<Client>,
message_sender_handle: Option<MessageSenderHandle>,
// trailing_slash_pos: usize,
local_signals: DashMap<String, Signal, BuildHasherDefault<FxHasher>>,
local_methods: DashMap<String, Method, BuildHasherDefault<FxHasher>>,
local_signals: Mutex<FxHashMap<String, Signal>>,
local_methods: Mutex<FxHashMap<String, Method>>,
destroyable: bool,
pub alias: OnceCell<Arc<Alias>>,
@@ -79,9 +96,6 @@ pub struct Node {
// Sound
pub sound: OnceCell<Arc<Sound>>,
// Startup
pub startup_settings: OnceCell<Mutex<StartupSettings>>,
}
impl Node {
@@ -125,7 +139,6 @@ impl Node {
item_acceptor: OnceCell::new(),
item_ui: OnceCell::new(),
sound: OnceCell::new(),
startup_settings: OnceCell::new(),
};
node.add_local_signal("set_enabled", Node::set_enabled_flex);
node.add_local_signal("destroy", Node::destroy_flex);
@@ -144,11 +157,33 @@ impl Node {
}
}
pub fn set_enabled_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
node.enabled.store(deserialize(data)?, Ordering::Relaxed);
pub fn set_enabled_flex(
node: &Node,
_calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
node.enabled
.store(deserialize(message.as_ref())?, Ordering::Relaxed);
Ok(())
}
pub fn destroy_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
// very much up for debate if we should allow this, as you can match objects using this
// pub fn get_client_pid_flex(
// node: &Node,
// _calling_client: Arc<Client>,
// _message: Message,
// ) -> Result<Message> {
// let client = node
// .client
// .upgrade()
// .ok_or_else(|| eyre!("Could not get client for node?"))?;
// let pid = client.pid.ok_or_else(|| eyre!("Client PID is unknown"))?;
// Ok(serialize(pid)?.into())
// }
pub fn destroy_flex(
node: &Node,
_calling_client: Arc<Client>,
_message: Message,
) -> Result<()> {
if node.destroyable {
node.destroy();
}
@@ -156,10 +191,10 @@ impl Node {
}
pub fn add_local_signal(&self, name: &str, signal: Signal) {
self.local_signals.insert(name.to_string(), signal);
self.local_signals.lock().insert(name.to_string(), signal);
}
pub fn add_local_method(&self, name: &str, method: Method) {
self.local_methods.insert(name.to_string(), method);
self.local_methods.lock().insert(name.to_string(), method);
}
pub fn get_aspect<F, T>(
@@ -180,7 +215,7 @@ impl Node {
&self,
calling_client: Arc<Client>,
method: &str,
data: &[u8],
message: Message,
) -> Result<(), ScenegraphError> {
if let Some(alias) = self.alias.get() {
if !alias.info.server_signals.iter().any(|e| e == &method) {
@@ -190,13 +225,15 @@ impl Node {
.original
.upgrade()
.ok_or(ScenegraphError::BrokenAlias)?
.send_local_signal(calling_client, method, data)
.send_local_signal(calling_client, method, message)
} else {
let signal = self
.local_signals
.lock()
.get(method)
.cloned()
.ok_or(ScenegraphError::SignalNotFound)?;
signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError {
signal(self, calling_client, message).map_err(|error| ScenegraphError::SignalError {
error: error.to_string(),
})
}
@@ -205,63 +242,83 @@ impl Node {
&self,
calling_client: Arc<Client>,
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
message: Message,
response: MethodResponseSender,
) {
if let Some(alias) = self.alias.get() {
if !alias.info.server_methods.iter().any(|e| e == &method) {
return Err(ScenegraphError::MethodNotFound);
response.send(Err(ScenegraphError::MethodNotFound));
return;
}
alias
.original
.upgrade()
.ok_or(ScenegraphError::BrokenAlias)?
.execute_local_method(calling_client, method, data)
let Some(alias) = alias.original.upgrade() else {
response.send(Err(ScenegraphError::BrokenAlias));
return;
};
alias.execute_local_method(
calling_client,
method,
Message {
data: message.data.clone(),
fds: Vec::new(),
},
response,
)
} else {
let method = self
.local_methods
.get(method)
.ok_or(ScenegraphError::MethodNotFound)?;
debug_span!("Handle method").in_scope(|| {
method(self, calling_client, data).map_err(|error| ScenegraphError::MethodError {
error: error.to_string(),
})
})
let Some(method) = self.local_methods.lock().get(method).cloned() else {
response.send(Err(ScenegraphError::MethodNotFound));
return;
};
method(self, calling_client, message, response);
}
}
#[instrument(level = "debug", skip_all)]
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
pub fn send_remote_signal(&self, method: &str, message: impl Into<Message>) -> Result<()> {
let message = message.into();
self.aliases
.get_valid_contents()
.iter()
.filter(|alias| alias.info.client_signals.iter().any(|e| e == &method))
.filter_map(|alias| alias.node.upgrade())
.for_each(|node| {
let _ = node.send_remote_signal(method, data);
// Beware! file descriptors will not be sent to aliases!!!
let _ = node.send_remote_signal(
method,
Message {
data: message.data.clone(),
fds: Vec::new(),
},
);
});
let path = self.path.clone();
let method = method.to_string();
let data = data.to_vec();
if let Some(handle) = self.message_sender_handle.as_ref() {
handle.signal(path.as_str(), method.as_str(), data.as_slice())?;
handle.signal(path.as_str(), method.as_str(), &message.data, message.fds)?;
}
Ok(())
}
// #[instrument(level = "debug", skip_all)]
// pub fn execute_remote_method(
// &self,
// method: &str,
// data: Vec<u8>,
// ) -> Result<impl Future<Output = Result<Vec<u8>>>> {
// let message_sender_handle = self
// .message_sender_handle
// .as_ref()
// .ok_or(eyre!("Messenger does not exist for this node"))?;
pub fn execute_remote_method(
&self,
method: &str,
message: impl Into<Message>,
) -> Result<impl Future<Output = Result<Message>>> {
let message = message.into();
let message_sender_handle = self
.message_sender_handle
.as_ref()
.ok_or(eyre!("Messenger does not exist for this node"))?;
// let future = message_sender_handle.method(self.path.as_str(), method, &data)?;
let future =
message_sender_handle.method(self.path.as_str(), method, &message.data, message.fds)?;
// Ok(async { future.await.map_err(|e| eyre!(e)) })
// }
Ok(async {
match future.await {
Ok(m) => {
let (data, fds) = m.into_components();
Ok(Message { data, fds })
}
Err(e) => Err(eyre!(e)),
}
})
}
}
impl Debug for Node {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View File

@@ -1,19 +1,25 @@
use super::spatial::Spatial;
use super::Node;
use super::{Message, Node};
use crate::core::client::Client;
use crate::core::client_state::{ClientState, ClientStateInternal};
use crate::core::registry::Registry;
use crate::core::scenegraph::MethodResponseSender;
use crate::wayland::WAYLAND_DISPLAY;
use crate::STARDUST_INSTANCE;
use color_eyre::eyre::Result;
use glam::Mat4;
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;
static ROOT_REGISTRY: Registry<Root> = Registry::new();
pub struct Root {
node: Arc<Node>,
pub node: Arc<Node>,
send_frame_event: AtomicBool,
}
impl Root {
@@ -21,17 +27,13 @@ impl Root {
let node = Node::create(client, "", "", false);
node.add_local_signal("subscribe_frame", Root::subscribe_frame_flex);
node.add_local_signal("set_base_prefixes", Root::set_base_prefixes_flex);
let node = node.add_to_scenegraph()?;
let _ = Spatial::add_to(
&node,
None,
client
.startup_settings
.as_ref()
.map(|settings| settings.transform)
.unwrap_or(Mat4::IDENTITY),
false,
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 _ = Spatial::add_to(&node, None, client.state.root, false);
Ok(ROOT_REGISTRY.add(Root {
node,
@@ -39,7 +41,11 @@ impl Root {
}))
}
fn subscribe_frame_flex(_node: &Node, calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
fn subscribe_frame_flex(
_node: &Node,
calling_client: Arc<Client>,
_message: Message,
) -> Result<()> {
calling_client
.root
.get()
@@ -54,7 +60,7 @@ impl Root {
if let Ok(data) = serialize((delta, 0.0)) {
for root in ROOT_REGISTRY.get_valid_contents() {
if root.send_frame_event.load(Ordering::Relaxed) {
let _ = root.node.send_remote_signal("frame", &data);
let _ = root.node.send_remote_signal("frame", data.clone());
}
}
}
@@ -63,11 +69,36 @@ impl Root {
fn set_base_prefixes_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
*calling_client.base_resource_prefixes.lock() = deserialize(data)?;
*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) {
let spatial = self.node.spatial.get().unwrap();
spatial.set_spatial_parent(None).unwrap();
spatial.set_local_transform(transform);
}
pub fn save_state(&self) -> impl Future<Output = Result<ClientStateInternal>> {
let future = self
.node
.execute_remote_method("save_state", Message::default());
async move { Ok(deserialize(&future?.await?.data)?) }
}
}
impl Drop for Root {
@@ -75,3 +106,33 @@ impl Drop for Root {
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())
});
}

View File

@@ -1,9 +1,10 @@
pub mod zone;
use self::zone::{create_zone_flex, Zone};
use super::Node;
use super::{Message, Node};
use crate::core::client::Client;
use crate::core::registry::Registry;
use crate::core::scenegraph::MethodResponseSender;
use color_eyre::eyre::{ensure, eyre, Result};
use glam::{vec3a, Mat4, Quat};
use mint::Vector3;
@@ -16,7 +17,6 @@ use std::fmt::Debug;
use std::ptr;
use std::sync::{Arc, OnceLock, Weak};
use stereokit::{bounds_grow_to_fit_box, Bounds};
use tracing::instrument;
static ZONEABLE_REGISTRY: Registry<Spatial> = Registry::new();
@@ -80,7 +80,6 @@ impl Spatial {
self.node.upgrade()
}
#[instrument(level = "debug", skip_all)]
pub fn space_to_space_matrix(from: Option<&Spatial>, to: Option<&Spatial>) -> Mat4 {
let space_to_world_matrix = from.map_or(Mat4::IDENTITY, |from| from.global_transform());
let world_to_space_matrix = to.map_or(Mat4::IDENTITY, |to| to.global_transform().inverse());
@@ -88,7 +87,6 @@ impl Spatial {
}
// the output bounds are probably way bigger than they need to be
#[instrument(level = "debug")]
pub fn get_bounding_box(&self) -> Bounds {
let Some(node) = self.node() else {return Bounds::default()};
let mut bounds = self
@@ -106,7 +104,6 @@ impl Spatial {
bounds
}
#[instrument(level = "debug", skip_all)]
pub fn local_transform(&self) -> Mat4 {
*self.transform.lock()
}
@@ -116,11 +113,9 @@ impl Spatial {
None => *self.transform.lock(),
}
}
#[instrument]
pub fn set_local_transform(&self, transform: Mat4) {
*self.transform.lock() = transform;
}
#[instrument(level = "debug", skip(self, reference_space))]
pub fn set_local_transform_components(
&self,
reference_space: Option<&Spatial>,
@@ -164,7 +159,6 @@ impl Spatial {
);
}
#[instrument(level = "debug", skip_all)]
pub fn is_ancestor_of(&self, spatial: Arc<Spatial>) -> bool {
let mut current_ancestor = spatial;
loop {
@@ -196,7 +190,6 @@ impl Spatial {
*self.parent.lock() = new_parent;
}
#[instrument(level = "debug", skip_all)]
pub fn set_spatial_parent(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
let is_ancestor = parent
.as_ref()
@@ -210,7 +203,6 @@ impl Spatial {
Ok(())
}
#[instrument(level = "debug", skip_all)]
pub fn set_spatial_parent_in_place(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
let is_ancestor = parent
.as_ref()
@@ -232,72 +224,84 @@ impl Spatial {
pub fn get_bounding_box_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let this_spatial = node
.spatial
.get()
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
let relative_spatial_path: Option<&str> = deserialize(data)?;
let bounds = if let Some(relative_spatial_path) = relative_spatial_path {
let relative_spatial = find_reference_space(&calling_client, relative_spatial_path)?;
let center =
Spatial::space_to_space_matrix(Some(&this_spatial), Some(&relative_spatial))
.transform_point3([0.0; 3].into());
let bounds: Bounds = Bounds {
center,
dimensions: [0.0; 3].into(),
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
let this_spatial = node
.spatial
.get()
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
let relative_spatial_path: Option<&str> = deserialize(message.as_ref())?;
let bounds = if let Some(relative_spatial_path) = relative_spatial_path {
let relative_spatial =
find_reference_space(&calling_client, relative_spatial_path)?;
let center =
Spatial::space_to_space_matrix(Some(&this_spatial), Some(&relative_spatial))
.transform_point3([0.0; 3].into());
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()
};
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()
};
serialize((
mint::Vector3::from(bounds.center),
mint::Vector3::from(bounds.dimensions),
))
.map_err(|e| e.into())
Ok(serialize((
mint::Vector3::from(bounds.center),
mint::Vector3::from(bounds.dimensions),
))?
.into())
});
}
pub fn get_transform_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let this_spatial = node
.spatial
.get()
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
let relative_spatial = find_reference_space(&calling_client, deserialize(data)?)?;
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
let this_spatial = node
.spatial
.get()
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
let relative_spatial =
find_reference_space(&calling_client, deserialize(message.as_ref())?)?;
let (scale, rotation, position) = Spatial::space_to_space_matrix(
Some(this_spatial.as_ref()),
Some(relative_spatial.as_ref()),
)
.to_scale_rotation_translation();
let (scale, rotation, position) = Spatial::space_to_space_matrix(
Some(this_spatial.as_ref()),
Some(relative_spatial.as_ref()),
)
.to_scale_rotation_translation();
serialize((
mint::Vector3::from(position),
mint::Quaternion::from(rotation),
mint::Vector3::from(scale),
))
.map_err(|e| e.into())
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>, data: &[u8]) -> Result<()> {
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(data)?;
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))
@@ -312,25 +316,29 @@ impl Spatial {
pub fn set_spatial_parent_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
let parent = find_spatial_parent(&calling_client, deserialize(data)?)?;
let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?;
node.spatial.get().unwrap().set_spatial_parent(Some(parent))
}
pub fn set_spatial_parent_in_place_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
message: Message,
) -> Result<()> {
let parent = find_spatial_parent(&calling_client, deserialize(data)?)?;
let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?;
node.spatial
.get()
.unwrap()
.set_spatial_parent_in_place(Some(parent))?;
Ok(())
}
pub fn set_zoneable_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let zoneable: bool = deserialize(data)?;
pub fn set_zoneable_flex(
node: &Node,
_calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
let zoneable: bool = deserialize(message.as_ref())?;
let spatial = node.spatial.get().unwrap();
if zoneable {
ZONEABLE_REGISTRY.add_raw(spatial);
@@ -344,74 +352,82 @@ impl Spatial {
pub fn field_distance_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(data)?;
let spatial = node.spatial.get().unwrap();
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| f.distance(spatial, point.into())))
.collect::<Vec<Option<f32>>>();
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)?)
Ok(serialize(output)?.into())
});
}
pub fn field_normal_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(data)?;
let spatial = node.spatial.get().unwrap();
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<_>>();
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)?)
Ok(serialize(output)?.into())
});
}
pub fn field_closest_point_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let (point, fields): (Vector3<f32>, Vec<Option<&str>>) = deserialize(data)?;
let spatial = node.spatial.get().unwrap();
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<_>>();
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)?)
Ok(serialize(output)?.into())
});
}
#[instrument]
pub(self) fn zone_distance(&self) -> f32 {
self.zone
.lock()
@@ -490,7 +506,11 @@ pub fn create_interface(client: &Arc<Client>) -> Result<()> {
node.add_to_scenegraph().map(|_| ())
}
pub fn create_spatial_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_spatial_flex(
_node: &Node,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateSpatialInfo<'a> {
name: &'a str,
@@ -498,7 +518,7 @@ pub fn create_spatial_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8
transform: Transform,
zoneable: bool,
}
let info: CreateSpatialInfo = deserialize(data)?;
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);

View File

@@ -5,7 +5,7 @@ use crate::{
alias::{Alias, AliasInfo},
fields::{find_field, Field},
spatial::{find_spatial_parent, parse_transform},
Node,
Message, Node,
},
};
use color_eyre::eyre::Result;
@@ -31,17 +31,19 @@ pub fn capture(spatial: &Arc<Spatial>, zone: &Arc<Zone>) {
*spatial.old_parent.lock() = spatial.get_parent();
*spatial.zone.lock() = Arc::downgrade(zone);
zone.captured.add_raw(spatial);
let node = zone.spatial.node.upgrade().unwrap();
let _ = node.send_remote_signal("capture", &serialize(&spatial.uid).unwrap());
let Some(node) = zone.spatial.node.upgrade() else {return};
let Ok(message) = serialize(&spatial.uid) else {return};
let _ = node.send_remote_signal("capture", message);
}
}
pub fn release(spatial: &Spatial) {
let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take());
let mut spatial_zone = spatial.zone.lock();
if let Some(spatial_zone) = spatial_zone.upgrade() {
let node = spatial_zone.spatial.node.upgrade().unwrap();
let Some(node) = spatial_zone.spatial.node.upgrade() else {return};
spatial_zone.captured.remove(spatial);
let _ = node.send_remote_signal("release", &serialize(&spatial.uid).unwrap());
let Ok(message) = serialize(&spatial.uid) else {return};
let _ = node.send_remote_signal("release", message);
}
*spatial_zone = Weak::new();
}
@@ -66,20 +68,20 @@ impl Zone {
let _ = node.zone.set(zone.clone());
zone
}
fn capture_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
fn capture_flex(node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
let zone = node.zone.get().unwrap();
let capture_path: &str = deserialize(data)?;
let capture_path: &str = deserialize(message.as_ref())?;
let spatial = find_spatial(&calling_client, "Spatial", capture_path)?;
capture(&spatial, zone);
Ok(())
}
fn release_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let capture_path: &str = deserialize(data)?;
fn release_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
let capture_path: &str = deserialize(message.as_ref())?;
let spatial = find_spatial(&calling_client, "Spatial", capture_path)?;
release(&spatial);
Ok(())
}
fn update(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
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
@@ -129,10 +131,10 @@ impl Zone {
.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)?)?;
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)?)?;
node.send_remote_signal("leave", serialize(left_uid)?)?;
}
*old_zoneables = zoneables;
@@ -148,7 +150,7 @@ impl Drop for Zone {
}
}
pub fn create_zone_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
pub fn create_zone_flex(_node: &Node, calling_client: Arc<Client>, message: Message) -> Result<()> {
#[derive(Deserialize)]
struct CreateZoneInfo<'a> {
name: &'a str,
@@ -156,7 +158,7 @@ pub fn create_zone_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8])
transform: Transform,
field_path: &'a str,
}
let info: CreateZoneInfo = deserialize(data)?;
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)?;

View File

@@ -1,146 +0,0 @@
use crate::{core::client::Client, wayland::WAYLAND_DISPLAY, STARDUST_INSTANCE};
use super::{
items::{ItemAcceptor, TypeInfo},
spatial::find_spatial,
Node,
};
use color_eyre::eyre::Result;
use glam::Mat4;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use stardust_xr::schemas::flex::{deserialize, serialize};
use std::{
fmt::Debug,
sync::{Arc, Weak},
};
lazy_static::lazy_static! {
pub static ref STARTUP_SETTINGS: Mutex<FxHashMap<String, StartupSettings>> = Default::default();
}
#[derive(Default, Clone)]
pub struct StartupSettings {
pub transform: Mat4,
pub acceptors: FxHashMap<&'static TypeInfo, Weak<ItemAcceptor>>,
}
impl StartupSettings {
pub fn add_to(node: &Arc<Node>) {
let _ = node
.startup_settings
.set(Mutex::new(StartupSettings::default()));
}
fn set_root_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let spatial = find_spatial(&calling_client, "Root spatial", deserialize(data)?)?;
node.startup_settings.get().unwrap().lock().transform = spatial.global_transform();
Ok(())
}
fn add_automatic_acceptor_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let acceptor_node = calling_client.get_node("Item acceptor", deserialize(data)?)?;
let acceptor =
acceptor_node.get_aspect("Item acceptor", "item acceptor", |n| &n.item_acceptor)?;
let mut startup_settings = node.startup_settings.get().unwrap().lock();
startup_settings
.acceptors
.insert(acceptor.type_info, Arc::downgrade(acceptor));
Ok(())
}
fn generate_startup_token_flex(
node: &Node,
_calling_client: Arc<Client>,
_data: &[u8],
) -> Result<Vec<u8>> {
let id = nanoid::nanoid!();
let data = serialize(&id)?;
STARTUP_SETTINGS
.lock()
.insert(id, node.startup_settings.get().unwrap().lock().clone());
Ok(data)
}
}
impl Debug for StartupSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StartupSettings")
.field("transform", &self.transform)
.field(
"acceptors",
&self
.acceptors
.iter()
.map(|(k, _)| k.type_name)
.collect::<Vec<_>>(),
)
.finish()
}
}
pub fn create_interface(client: &Arc<Client>) -> Result<()> {
let node = Node::create(client, "", "startup", false);
node.add_local_signal("create_startup_settings", create_startup_settings_flex);
node.add_local_method(
"get_connection_environment",
get_connection_environment_flex,
);
node.add_to_scenegraph().map(|_| ())
}
pub fn create_startup_settings_flex(
_node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let node = Node::create(
&calling_client,
"/startup/settings",
deserialize(data)?,
true,
)
.add_to_scenegraph()?;
StartupSettings::add_to(&node);
node.add_local_signal("set_root", StartupSettings::set_root_flex);
node.add_local_signal(
"add_automatic_acceptor",
StartupSettings::add_automatic_acceptor_flex,
);
node.add_local_method(
"generate_startup_token",
StartupSettings::generate_startup_token_flex,
);
Ok(())
}
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>,
_data: &[u8],
) -> Result<Vec<u8>> {
let mut env: FxHashMap<String, String> = FxHashMap::default();
var_env_insert!(env, STARDUST_INSTANCE);
#[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());
}
Ok(serialize(env)?)
}

Binary file not shown.

Binary file not shown.

View File

@@ -13,7 +13,6 @@ use serde::Serialize;
use stardust_xr::schemas::{flat::Datamap, flex::flexbuffers};
use std::sync::Arc;
use stereokit::StereoKitMultiThread;
use tracing::instrument;
#[derive(Debug, Clone, Serialize)]
pub struct KeyboardEvent {
@@ -36,7 +35,6 @@ impl EyePointer {
Ok(EyePointer { spatial, pointer })
}
#[instrument(level = "debug", name = "Update Flatscreen Pointer Ray", skip_all)]
pub fn update(&self, sk: &impl StereoKitMultiThread) {
let ray = sk.input_eyes();
self.spatial

View File

@@ -1,7 +1,7 @@
use crate::{
core::client::INTERNAL_CLIENT,
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
nodes::{
data::{mask_matches, Mask, PulseSender, PULSE_RECEIVER_REGISTRY},
data::{mask_matches, Mask, PulseSender, KEYMAPS, PULSE_RECEIVER_REGISTRY},
fields::Ray,
input::{pointer::Pointer, InputMethod, InputType},
spatial::Spatial,
@@ -9,28 +9,45 @@ use crate::{
},
};
use color_eyre::eyre::Result;
use glam::{vec3, Mat4, Vec3};
use glam::{vec2, vec3, Mat4, Vec2, Vec3};
use nanoid::nanoid;
use serde::Serialize;
use stardust_xr::schemas::{flat::Datamap, flex::flexbuffers};
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, sync::Arc};
use stereokit::{ray_from_mouse, ButtonState, Key, StereoKitMultiThread};
use tracing::instrument;
use xkbcommon::xkb::{Context, Keymap, FORMAT_TEXT_V1};
const SK_KEYMAP: &str = include_str!("sk.kmp");
#[derive(Default, Deserialize, Serialize)]
struct MouseEvent {
select: f32,
grab: f32,
scroll_continuous: Vec2,
scroll_discrete: Vec2,
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeyboardEvent {
pub keyboard: String,
pub keymap: Option<String>,
pub keys_up: Option<Vec<u32>>,
pub keys_down: Option<Vec<u32>>,
pub keyboard: (),
pub xkbv1: (),
pub keymap_id: String,
pub keys: Vec<i32>,
}
impl Default for KeyboardEvent {
fn default() -> Self {
Self {
keyboard: (),
xkbv1: (),
keymap_id: "flatscreen".to_string(),
keys: Default::default(),
}
}
}
pub struct MousePointer {
node: Arc<Node>,
spatial: Arc<Spatial>,
pointer: Arc<InputMethod>,
mouse_datamap: TypedDatamap<MouseEvent>,
keyboard_datamap: TypedDatamap<KeyboardEvent>,
keyboard_sender: Arc<PulseSender>,
}
impl MousePointer {
@@ -40,24 +57,26 @@ impl MousePointer {
let pointer =
InputMethod::add_to(&node, InputType::Pointer(Pointer::default()), None).unwrap();
let keyboard_mask = {
let mut fbb = flexbuffers::Builder::default();
let mut map = fbb.start_map();
map.push("keyboard", "xkbv1");
map.end_map();
Mask(fbb.take_buffer())
};
let keyboard_sender = PulseSender::add_to(&node, keyboard_mask).unwrap();
KEYMAPS.lock().insert(
"flatscreen".to_string(),
Keymap::new_from_names(&Context::new(0), "evdev", "", "", "", None, 0)
.unwrap()
.get_as_string(FORMAT_TEXT_V1),
);
let keyboard_sender =
PulseSender::add_to(&node, Mask::from_struct::<KeyboardEvent>()).unwrap();
Ok(MousePointer {
node,
spatial,
pointer,
mouse_datamap: Default::default(),
keyboard_datamap: Default::default(),
keyboard_sender,
})
}
#[instrument(level = "debug", name = "Update Flatscreen Pointer Ray", skip_all)]
pub fn update(&self, sk: &impl StereoKitMultiThread) {
pub fn update(&mut self, sk: &impl StereoKitMultiThread) {
let mouse = sk.input_mouse();
let ray = ray_from_mouse(mouse.pos).unwrap();
@@ -71,35 +90,26 @@ impl MousePointer {
);
{
// Set pointer input datamap
let mut fbb = flexbuffers::Builder::default();
let mut map = fbb.start_map();
map.push(
"select",
self.mouse_datamap.select =
if sk.input_key(Key::MouseLeft).contains(ButtonState::ACTIVE) {
1.0f32
} else {
0.0f32
},
);
map.push(
"grab",
if sk.input_key(Key::MouseRight).contains(ButtonState::ACTIVE) {
1.0f32
} else {
0.0f32
},
);
let mut scroll_vec = map.start_vector("scroll");
scroll_vec.push(0_f32);
scroll_vec.push(mouse.scroll_change / 120.0);
scroll_vec.end_vector();
map.end_map();
*self.pointer.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
};
self.mouse_datamap.grab = if sk.input_key(Key::MouseRight).contains(ButtonState::ACTIVE)
{
1.0f32
} else {
0.0f32
};
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.send_keyboard_input(sk);
}
fn send_keyboard_input(&self, sk: &impl StereoKitMultiThread) {
fn send_keyboard_input(&mut self, sk: &impl StereoKitMultiThread) {
let rx = PULSE_RECEIVER_REGISTRY
.get_valid_contents()
.into_iter()
@@ -107,7 +117,7 @@ impl MousePointer {
.map(|rx| {
let result = rx.field.ray_march(Ray {
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: self.spatial.clone(),
});
(rx, result)
@@ -125,29 +135,127 @@ impl MousePointer {
.map(|(rx, _)| rx);
if let Some(rx) = rx {
let mut keys_up = vec![];
let mut keys_down = vec![];
let keys = (8_u32..254)
.filter_map(|i| Some((i, Key::try_from(i).ok()?)))
.map(|(i, k)| (i - 8, sk.input_key(k)));
for (key, state) in keys {
if state.contains(ButtonState::JUST_ACTIVE) {
keys_down.push(key);
} else if state.contains(ButtonState::JUST_INACTIVE) {
keys_up.push(key);
}
}
.filter_map(|i| Key::try_from(i).ok())
.filter_map(|k| Some((map_key(k)?, sk.input_key(k))))
.filter_map(|(i, k)| {
if k.contains(ButtonState::JUST_ACTIVE) {
Some(i as i32)
} else if k.contains(ButtonState::JUST_INACTIVE) {
Some(-(i as i32))
} else {
None
}
})
.collect();
let key_event = KeyboardEvent {
keyboard: "xkbv1".to_string(),
keymap: Some(SK_KEYMAP.to_string()),
keys_up: Some(keys_up),
keys_down: Some(keys_down),
};
let mut serializer = flexbuffers::FlexbufferSerializer::new();
let _ = key_event.serialize(&mut serializer);
rx.send_data(&self.node.uid, serializer.take_buffer())
.unwrap();
self.keyboard_datamap.keys = keys;
if !self.keyboard_datamap.keys.is_empty() {
rx.send_data(&self.node.uid, self.keyboard_datamap.serialize().unwrap())
.unwrap();
}
}
}
}
fn map_key(key: Key) -> Option<u32> {
match key {
Key::Backspace => Some(input_event_codes::KEY_BACKSPACE!()),
Key::Tab => Some(input_event_codes::KEY_TAB!()),
Key::Return => Some(input_event_codes::KEY_ENTER!()),
Key::Shift => Some(input_event_codes::KEY_LEFTSHIFT!()),
Key::Ctrl => Some(input_event_codes::KEY_LEFTCTRL!()),
Key::Alt => Some(input_event_codes::KEY_LEFTALT!()),
Key::CapsLock => Some(input_event_codes::KEY_CAPSLOCK!()),
Key::Esc => Some(input_event_codes::KEY_ESC!()),
Key::Space => Some(input_event_codes::KEY_SPACE!()),
Key::End => Some(input_event_codes::KEY_END!()),
Key::Home => Some(input_event_codes::KEY_HOME!()),
Key::Left => Some(input_event_codes::KEY_LEFT!()),
Key::Right => Some(input_event_codes::KEY_RIGHT!()),
Key::Up => Some(input_event_codes::KEY_UP!()),
Key::Down => Some(input_event_codes::KEY_DOWN!()),
Key::PageUp => Some(input_event_codes::KEY_PAGEUP!()),
Key::PageDown => Some(input_event_codes::KEY_PAGEDOWN!()),
Key::PrintScreen => Some(input_event_codes::KEY_PRINT!()),
Key::KeyInsert => Some(input_event_codes::KEY_INSERT!()),
Key::Del => Some(input_event_codes::KEY_DELETE!()),
Key::Key0 => Some(input_event_codes::KEY_0!()),
Key::Key1 => Some(input_event_codes::KEY_1!()),
Key::Key2 => Some(input_event_codes::KEY_2!()),
Key::Key3 => Some(input_event_codes::KEY_3!()),
Key::Key4 => Some(input_event_codes::KEY_4!()),
Key::Key5 => Some(input_event_codes::KEY_5!()),
Key::Key6 => Some(input_event_codes::KEY_6!()),
Key::Key7 => Some(input_event_codes::KEY_7!()),
Key::Key8 => Some(input_event_codes::KEY_8!()),
Key::Key9 => Some(input_event_codes::KEY_9!()),
Key::A => Some(input_event_codes::KEY_A!()),
Key::B => Some(input_event_codes::KEY_B!()),
Key::C => Some(input_event_codes::KEY_C!()),
Key::D => Some(input_event_codes::KEY_D!()),
Key::E => Some(input_event_codes::KEY_E!()),
Key::F => Some(input_event_codes::KEY_F!()),
Key::G => Some(input_event_codes::KEY_G!()),
Key::H => Some(input_event_codes::KEY_H!()),
Key::I => Some(input_event_codes::KEY_I!()),
Key::J => Some(input_event_codes::KEY_J!()),
Key::K => Some(input_event_codes::KEY_K!()),
Key::L => Some(input_event_codes::KEY_L!()),
Key::M => Some(input_event_codes::KEY_M!()),
Key::N => Some(input_event_codes::KEY_N!()),
Key::O => Some(input_event_codes::KEY_O!()),
Key::P => Some(input_event_codes::KEY_P!()),
Key::Q => Some(input_event_codes::KEY_Q!()),
Key::R => Some(input_event_codes::KEY_R!()),
Key::S => Some(input_event_codes::KEY_S!()),
Key::T => Some(input_event_codes::KEY_T!()),
Key::U => Some(input_event_codes::KEY_U!()),
Key::V => Some(input_event_codes::KEY_V!()),
Key::W => Some(input_event_codes::KEY_W!()),
Key::X => Some(input_event_codes::KEY_X!()),
Key::Y => Some(input_event_codes::KEY_Y!()),
Key::Z => Some(input_event_codes::KEY_Z!()),
Key::Numpad0 => Some(input_event_codes::KEY_NUMERIC_0!()),
Key::Numpad1 => Some(input_event_codes::KEY_NUMERIC_1!()),
Key::Numpad2 => Some(input_event_codes::KEY_NUMERIC_2!()),
Key::Numpad3 => Some(input_event_codes::KEY_NUMERIC_3!()),
Key::Numpad4 => Some(input_event_codes::KEY_NUMERIC_4!()),
Key::Numpad5 => Some(input_event_codes::KEY_NUMERIC_5!()),
Key::Numpad6 => Some(input_event_codes::KEY_NUMERIC_6!()),
Key::Numpad7 => Some(input_event_codes::KEY_NUMERIC_7!()),
Key::Numpad8 => Some(input_event_codes::KEY_NUMERIC_8!()),
Key::Numpad9 => Some(input_event_codes::KEY_NUMERIC_9!()),
Key::F1 => Some(input_event_codes::KEY_F1!()),
Key::F2 => Some(input_event_codes::KEY_F2!()),
Key::F3 => Some(input_event_codes::KEY_F3!()),
Key::F4 => Some(input_event_codes::KEY_F4!()),
Key::F5 => Some(input_event_codes::KEY_F5!()),
Key::F6 => Some(input_event_codes::KEY_F6!()),
Key::F7 => Some(input_event_codes::KEY_F7!()),
Key::F8 => Some(input_event_codes::KEY_F8!()),
Key::F9 => Some(input_event_codes::KEY_F9!()),
Key::F10 => Some(input_event_codes::KEY_F10!()),
Key::F11 => Some(input_event_codes::KEY_F11!()),
Key::F12 => Some(input_event_codes::KEY_F12!()),
Key::Comma => Some(input_event_codes::KEY_COMMA!()),
Key::Period => Some(input_event_codes::KEY_DOT!()),
Key::SlashFwd => Some(input_event_codes::KEY_SLASH!()),
Key::SlashBack => Some(input_event_codes::KEY_BACKSLASH!()),
Key::Semicolon => Some(input_event_codes::KEY_SEMICOLON!()),
Key::Apostrophe => Some(input_event_codes::KEY_APOSTROPHE!()),
Key::BracketOpen => Some(input_event_codes::KEY_LEFTBRACE!()),
Key::BracketClose => Some(input_event_codes::KEY_RIGHTBRACE!()),
Key::Minus => Some(input_event_codes::KEY_MINUS!()),
Key::Equals => Some(input_event_codes::KEY_EQUAL!()),
Key::Backtick => None,
Key::LCmd => Some(input_event_codes::KEY_LEFTMETA!()),
Key::RCmd => Some(input_event_codes::KEY_RIGHTMETA!()),
Key::Multiply => Some(input_event_codes::KEY_NUMERIC_STAR!()),
Key::Add => Some(input_event_codes::KEY_KPPLUS!()),
Key::Subtract => Some(input_event_codes::KEY_MINUS!()),
Key::Decimal => Some(input_event_codes::KEY_DOT!()),
Key::Divide => Some(input_event_codes::KEY_SLASH!()),
_ => None,
}
}

View File

@@ -1,275 +0,0 @@
xkb_keymap {
default xkb_keycodes "basic" {
minimum = 8;
maximum = 255;
<backspace> = 8;
<tab> = 9;
<return> = 13;
<shift> = 16;
<ctrl> = 17;
<alt> = 18;
<caps_lock> = 20;
<esc> = 27;
<space> = 32;
<end> = 35;
<home> = 36;
<left> = 37;
<right> = 39;
<up> = 38;
<down> = 40;
<page_up> = 33;
<page_down> = 34;
<printscreen> = 42;
<key_insert> = 45;
<del> = 46;
<0> = 48;
<1> = 49;
<2> = 50;
<3> = 51;
<4> = 52;
<5> = 53;
<6> = 54;
<7> = 55;
<8> = 56;
<9> = 57;
<a> = 65;
<b> = 66;
<c> = 67;
<d> = 68;
<e> = 69;
<f> = 70;
<g> = 71;
<h> = 72;
<i> = 73;
<j> = 74;
<k> = 75;
<l> = 76;
<m> = 77;
<n> = 78;
<o> = 79;
<p> = 80;
<q> = 81;
<r> = 82;
<s> = 83;
<t> = 84;
<u> = 85;
<v> = 86;
<w> = 87;
<x> = 88;
<y> = 89;
<z> = 90;
<num0> = 96;
<num1> = 97;
<num2> = 98;
<num3> = 99;
<num4> = 100;
<num5> = 101;
<num6> = 102;
<num7> = 103;
<num8> = 104;
<num9> = 105;
<f1> = 112;
<f2> = 113;
<f3> = 114;
<f4> = 115;
<f5> = 116;
<f6> = 117;
<f7> = 118;
<f8> = 119;
<f9> = 120;
<f10> = 121;
<f11> = 122;
<f12> = 123;
<comma> = 188;
<period> = 190;
<slash_fwd> = 191;
<slash_back> = 220;
<semicolon> = 186;
<apostrophe> = 222;
<bracket_open> = 219;
<bracket_close> = 221;
<minus> = 189;
<equals> = 187;
<backtick> = 192;
<lcmd> = 91;
<rcmd> = 92;
<multiply> = 106;
<add> = 107;
<subtract> = 109;
<decimal> = 110;
<divide> = 111;
};
partial default xkb_types "basic" {
virtual_modifiers Alt;
type "ONE_LEVEL" {
modifiers= none;
level_name[1]= "Any";
};
type "TWO_LEVEL" {
modifiers= Shift;
map[Shift]= 2;
level_name[1]= "Base";
level_name[2]= "Shift";
};
type "ALPHABETIC" {
modifiers= Shift+Lock;
map[Shift]= 2;
map[Lock]= 2;
level_name[1]= "Base";
level_name[2]= "Caps";
};
type "SHIFT+ALT" {
modifiers= Shift+Alt;
map[Shift+Alt]= 2;
level_name[1]= "Base";
level_name[2]= "Shift+Alt";
};
type "PC_CONTROL_LEVEL2" {
modifiers= Control;
map[Control]= 2;
level_name[1]= "Base";
level_name[2]= "Control";
};
};
partial default xkb_compatibility "basic" {
interpret.useModMapMods= AnyLevel;
interpret.repeat= False;
interpret ISO_Level2_Latch+Exactly(Shift) {
useModMapMods=level1;
action= LatchMods(modifiers=Shift,clearLocks,latchToLock);
};
interpret Caps_Lock+AnyOfOrNone(all) {
action= LockMods(modifiers=Lock);
};
indicator "Caps Lock" {
whichModState= locked;
modifiers= Lock;
};
};
default xkb_symbols "basic" {
name[Group1]="English (US)";
key <backspace> { [ BackSpace, BackSpace ] };
key <tab> { [ Tab, ISO_Left_Tab ] };
key <return> { [ Return ] };
key <shift> { [ Shift_L ] };
key <shift> { [ Shift_R ] };
key <ctrl> { [ Control_L ] };
key <ctrl> { [ Control_R ] };
key <alt> { [ Alt_L ] };
key <alt> { [ Alt_R ] };
key <caps_lock> { [ Caps_Lock ] };
key <esc> { [ Escape ] };
key <space> { [ space ] };
key <end> { [ End ] };
key <home> { [ Home ] };
key <left> { [ Left ] };
key <right> { [ Right ] };
key <up> { [ Up ] };
key <down> { [ Down ] };
key <page_up> { [ Page_Up ] };
key <page_down> { [ Page_Down ] };
key <printscreen> { [ Print ] };
key <key_insert> { [ Insert ] };
key <del> { [ Delete ] };
key <1> { [ 1, exclam ] };
key <2> { [ 2, at ] };
key <3> { [ 3, numbersign ] };
key <4> { [ 4, dollar ] };
key <5> { [ 5, percent ] };
key <6> { [ 6, asciicircum ] };
key <7> { [ 7, ampersand ] };
key <8> { [ 8, asterisk ] };
key <9> { [ 9, parenleft ] };
key <0> { [ 0, parenright ] };
key <a> { [ a, A ] };
key <b> { [ b, B ] };
key <c> { [ c, C ] };
key <d> { [ d, D ] };
key <e> { [ e, E ] };
key <f> { [ f, F ] };
key <g> { [ g, G ] };
key <h> { [ h, H ] };
key <i> { [ i, I ] };
key <j> { [ j, J ] };
key <k> { [ k, K ] };
key <l> { [ l, L ] };
key <m> { [ m, M ] };
key <n> { [ n, N ] };
key <o> { [ o, O ] };
key <p> { [ p, P ] };
key <q> { [ q, Q ] };
key <r> { [ r, R ] };
key <s> { [ s, S ] };
key <t> { [ t, T ] };
key <u> { [ u, U ] };
key <v> { [ v, V ] };
key <w> { [ w, W ] };
key <x> { [ x, X ] };
key <y> { [ y, Y ] };
key <z> { [ z, Z ] };
key <num0> { [ KP_0 ] };
key <num1> { [ KP_1 ] };
key <num2> { [ KP_2 ] };
key <num3> { [ KP_3 ] };
key <num4> { [ KP_4 ] };
key <num5> { [ KP_5 ] };
key <num6> { [ KP_6 ] };
key <num7> { [ KP_7 ] };
key <num8> { [ KP_8 ] };
key <num9> { [ KP_9 ] };
key <f1> { [ F1 ] };
key <f2> { [ F2 ] };
key <f3> { [ F3 ] };
key <f4> { [ F4 ] };
key <f5> { [ F5 ] };
key <f6> { [ F6 ] };
key <f7> { [ F7 ] };
key <f8> { [ F8 ] };
key <f9> { [ F9 ] };
key <f10> { [ F10 ] };
key <f11> { [ F11 ] };
key <f12> { [ F12 ] };
key <comma> { [ comma, less ] };
key <period> { [ period, greater ] };
key <slash_fwd> { [ slash, question ] };
key <slash_back> { [ backslash, bar ] };
key <semicolon> { [ semicolon, colon ] };
key <apostrophe> { [ apostrophe ] };
key <bracket_open> { [ bracketleft, braceleft ] };
key <bracket_close> { [ bracketright, braceright ] };
key <minus> { [ minus, underscore ] };
key <equals> { [ equal, plus ] };
key <backtick> { [ grave, asciitilde ] };
key <lcmd> { [ Super_L ] };
key <rcmd> { [ Super_R ] };
key <multiply> { [ KP_Multiply ] };
key <add> { [ KP_Add ] };
key <subtract> { [ KP_Subtract ] };
key <decimal> { [ KP_Decimal ] };
key <divide> { [ KP_Divide ] };
modifier_map Shift { <shift> };
modifier_map Lock { <caps_lock> };
modifier_map Control { <caps_lock> };
modifier_map Mod1 { <alt> };
modifier_map Mod4 { <lcmd>, <rcmd> };
};
};

View File

@@ -1,5 +1,5 @@
use crate::{
core::client::INTERNAL_CLIENT,
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
nodes::{
input::{tip::Tip, InputMethod, InputType},
spatial::Spatial,
@@ -7,51 +7,72 @@ use crate::{
},
};
use color_eyre::eyre::Result;
use glam::Mat4;
use nanoid::nanoid;
use stardust_xr::{
schemas::{flat::Datamap, flex::flexbuffers},
values::Transform,
};
use glam::{Mat4, Vec2, Vec3};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use stereokit::{ButtonState, Handed, StereoKitMultiThread};
use tracing::instrument;
use stereokit::{
named_colors::WHITE, ButtonState, Handed, Model, RenderLayer, StereoKitDraw,
StereoKitMultiThread,
};
#[derive(Default, Deserialize, Serialize)]
struct ControllerDatamap {
select: f32,
grab: f32,
scroll: Vec2,
}
pub struct SkController {
_node: Arc<Node>,
input: Arc<InputMethod>,
model: Model,
handed: Handed,
datamap: TypedDatamap<ControllerDatamap>,
}
impl SkController {
pub fn new(handed: Handed) -> Result<Self> {
let _node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
pub fn new(sk: &impl StereoKitMultiThread, handed: Handed) -> Result<Self> {
let _node = Node::create(
&INTERNAL_CLIENT,
"",
if handed == Handed::Left {
"controller_left"
} else {
"controller_right"
},
false,
)
.add_to_scenegraph()?;
Spatial::add_to(&_node, None, Mat4::IDENTITY, false)?;
let model = sk.model_create_mem("cursor.glb", include_bytes!("cursor.glb"), None)?;
let tip = InputType::Tip(Tip::default());
let input = InputMethod::add_to(&_node, tip, None)?;
Ok(SkController {
_node,
input,
handed,
model,
datamap: Default::default(),
})
}
#[instrument(level = "debug", name = "Update StereoKit Tip Input Method", skip_all)]
pub fn update(&mut self, sk: &impl StereoKitMultiThread) {
pub fn update(&mut self, sk: &impl StereoKitDraw) {
let controller = sk.input_controller(self.handed);
*self.input.enabled.lock() = controller.tracked.contains(ButtonState::ACTIVE);
if *self.input.enabled.lock() {
self.input.spatial.set_local_transform_components(
None,
Transform::from_position_rotation(
controller.pose.position,
controller.pose.orientation,
),
let world_transform = Mat4::from_rotation_translation(
controller.aim.orientation,
controller.aim.position,
);
sk.model_draw(
&self.model,
world_transform * Mat4::from_scale(Vec3::ONE * 0.02),
WHITE,
RenderLayer::LAYER0,
);
self.input.spatial.set_local_transform(world_transform);
}
let mut fbb = flexbuffers::Builder::default();
let mut map = fbb.start_map();
map.push("select", controller.trigger);
map.push("grab", controller.grip);
map.end_map();
*self.input.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
self.datamap.select = controller.trigger;
self.datamap.grab = controller.grip;
self.datamap.scroll = controller.stick;
*self.input.datamap.lock() = self.datamap.to_datamap().ok();
}
}

View File

@@ -1,5 +1,5 @@
use crate::{
core::client::INTERNAL_CLIENT,
core::{client::INTERNAL_CLIENT, typed_datamap::TypedDatamap},
nodes::{
input::{hand::Hand, InputMethod, InputType},
spatial::Spatial,
@@ -9,26 +9,31 @@ use crate::{
use color_eyre::eyre::Result;
use glam::Mat4;
use nanoid::nanoid;
use stardust_xr::schemas::{
flat::{Datamap, Hand as FlatHand, Joint},
flex::flexbuffers,
};
use serde::{Deserialize, Serialize};
use stardust_xr::schemas::flat::{Hand as FlatHand, Joint};
use std::sync::Arc;
use stereokit::{ButtonState, HandJoint, Handed, StereoKitMultiThread};
use tracing::instrument;
fn convert_joint(joint: HandJoint) -> Joint {
Joint {
position: joint.position.into(),
rotation: joint.orientation.into(),
radius: joint.radius,
distance: 0.0,
}
}
#[derive(Default, Deserialize, Serialize)]
struct HandDatamap {
pinch_strength: f32,
grab_strength: f32,
}
pub struct SkHand {
_node: Arc<Node>,
input: Arc<InputMethod>,
handed: Handed,
datamap: TypedDatamap<HandDatamap>,
}
impl SkHand {
pub fn new(handed: Handed) -> Result<Self> {
@@ -45,15 +50,20 @@ impl SkHand {
_node,
input,
handed,
datamap: Default::default(),
})
}
#[instrument(level = "debug", name = "Update Hand Input Method", skip_all)]
pub fn update(&mut self, sk: &impl StereoKitMultiThread) {
pub fn update(&mut self, controller_enabled: bool, sk: &impl StereoKitMultiThread) {
let sk_hand = sk.input_hand(self.handed);
if let InputType::Hand(hand) = &mut *self.input.specialization.lock() {
let controller = sk.input_controller(self.handed);
*self.input.enabled.lock() = controller.tracked.contains(ButtonState::INACTIVE)
&& sk_hand.tracked_state.contains(ButtonState::ACTIVE);
let controller_active = controller_enabled
&& sk
.input_controller(self.handed)
.tracked
.contains(ButtonState::ACTIVE);
*self.input.enabled.lock() =
!controller_active && sk_hand.tracked_state.contains(ButtonState::ACTIVE);
sk.input_hand_visible(self.handed, *self.input.enabled.lock());
if *self.input.enabled.lock() {
hand.base.thumb.tip = convert_joint(sk_hand.fingers[0][4]);
hand.base.thumb.distal = convert_joint(sk_hand.fingers[0][3]);
@@ -86,11 +96,8 @@ impl SkHand {
hand.base.elbow = None;
}
}
let mut fbb = flexbuffers::Builder::default();
let mut map = fbb.start_map();
map.push("grab_strength", sk_hand.grip_activation);
map.push("pinch_strength", sk_hand.pinch_activation);
map.end_map();
*self.input.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
self.datamap.pinch_strength = sk_hand.pinch_activation;
self.datamap.grab_strength = sk_hand.grip_activation;
*self.input.datamap.lock() = self.datamap.to_datamap().ok();
}
}

View File

@@ -1 +1,2 @@
pub mod input;
pub mod play_space;

73
src/objects/play_space.rs Normal file
View File

@@ -0,0 +1,73 @@
use std::sync::Arc;
use color_eyre::eyre::Result;
use glam::Mat4;
use mint::Vector2;
use nanoid::nanoid;
use serde::{Deserialize, Serialize};
use stereokit::StereoKitMultiThread;
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(),
}
}
}
pub struct PlaySpace {
_node: Arc<Node>,
spatial: Arc<Spatial>,
field: Arc<Field>,
_pulse_rx: Arc<PulseReceiver>,
}
impl PlaySpace {
pub fn new() -> Result<Self> {
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(),
);
}
}

View File

@@ -2,6 +2,8 @@ use crate::wayland::surface::CoreSurface;
use super::state::{ClientState, WaylandState};
use portable_atomic::{AtomicU32, Ordering};
#[cfg(feature = "xwayland")]
use smithay::xwayland::XWaylandClientData;
use smithay::{
delegate_compositor,
reexports::wayland_server::{protocol::wl_surface::WlSurface, Client},
@@ -23,11 +25,9 @@ impl CompositorHandler for WaylandState {
.data_map
.insert_if_missing_threadsafe(|| AtomicU32::new(0));
if !count_new {
count = data
.data_map
.get::<AtomicU32>()
.unwrap()
.fetch_add(1, Ordering::Relaxed);
if let Some(stored_count) = data.data_map.get::<AtomicU32>() {
count = stored_count.fetch_add(1, Ordering::Relaxed);
}
}
data.data_map.get::<Arc<CoreSurface>>().cloned()
@@ -38,7 +38,15 @@ impl CompositorHandler for WaylandState {
}
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 {
#[cfg(feature = "xwayland")]
if let Some(xwayland_client_data) = client.get_data::<XWaylandClientData>() {
return &xwayland_client_data.compositor_state;
}
unimplemented!()
}
}
}

View File

@@ -88,9 +88,10 @@ impl KdeDecorationHandler for WaylandState {
&mut self,
_surface: &WlSurface,
decoration: &OrgKdeKwinServerDecoration,
_mode: WEnum<KdeMode>,
mode: WEnum<KdeMode>,
) {
decoration.mode(KdeMode::Server);
let Ok(mode) = mode.into_result() else {return};
decoration.mode(mode);
}
}
delegate_kde_decoration!(WaylandState);

View File

@@ -1,144 +1,140 @@
mod compositor;
mod data_device;
mod decoration;
pub mod panel_item;
mod seat;
mod shaders;
mod state;
mod surface;
// mod xdg_activation;
mod xdg_shell;
#[cfg(feature = "xwayland")]
pub mod xwayland;
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 color_eyre::eyre::{ensure, Result};
use color_eyre::eyre::Result;
use global_counter::primitive::exact::CounterU32;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use sk::StereoKitDraw;
use smithay::backend::egl::EGLContext;
use smithay::backend::renderer::gles::GlesRenderer;
use smithay::reexports::wayland_server::{backend::GlobalId, Display, ListeningSocket};
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::renderer::ImportDma;
use smithay::reexports::wayland_server::backend::ClientId;
use smithay::reexports::wayland_server::DisplayHandle;
use smithay::reexports::wayland_server::{Display, ListeningSocket};
use std::ffi::OsStr;
use std::os::fd::OwnedFd;
use std::os::unix::prelude::AsRawFd;
use std::{
ffi::c_void,
os::unix::{net::UnixListener, prelude::FromRawFd},
sync::Arc,
};
use stereokit as sk;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::{
io::unix::AsyncFd, net::UnixListener as AsyncUnixListener, sync::mpsc, task::JoinHandle,
};
use tracing::{debug, debug_span, info, instrument};
use tracing::{debug_span, info, instrument};
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!(
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 DisplayWrapper(Mutex<Display<WaylandState>>, DisplayHandle);
impl DisplayWrapper {
pub fn handle(&self) -> DisplayHandle {
self.1.clone()
}
pub fn dispatch_clients(&self, state: &mut WaylandState) -> Result<usize, std::io::Error> {
self.0.lock().dispatch_clients(state)
}
pub fn flush_clients(&self, client: Option<ClientId>) {
if let Some(mut lock) = self.0.try_lock() {
let _ = lock.backend().flush(client);
}
})
}
pub fn poll_fd(&self) -> Result<OwnedFd, std::io::Error> {
self.0.lock().backend().poll_fd().try_clone_to_owned()
}
}
static GLOBAL_DESTROY_QUEUE: OnceCell<mpsc::Sender<GlobalId>> = OnceCell::new();
pub struct Wayland {
display: Arc<Mutex<Display<WaylandState>>>,
pub socket_name: String,
display: Arc<DisplayWrapper>,
pub socket_name: Option<String>,
join_handle: JoinHandle<Result<()>>,
renderer: GlesRenderer,
state: Arc<Mutex<WaylandState>>,
dmabuf_rx: UnboundedReceiver<Dmabuf>,
wayland_state: Arc<Mutex<WaylandState>>,
#[cfg(feature = "xwayland")]
pub xwayland_state: xwayland::XWaylandState,
}
impl Wayland {
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,
)?)?
};
pub fn new(buffer_manager: &BufferManager) -> Result<Self> {
let display: Display<WaylandState> = Display::new()?;
let display_handle = display.handle();
let display = Arc::new(Mutex::new(display));
let state = WaylandState::new(display.clone(), display_handle, &renderer);
let (global_destroy_queue_in, global_destroy_queue) = mpsc::channel(8);
GLOBAL_DESTROY_QUEUE.set(global_destroy_queue_in).unwrap();
let (dmabuf_tx, dmabuf_rx) = mpsc::unbounded_channel();
let display = Arc::new(DisplayWrapper(Mutex::new(display), display_handle.clone()));
#[cfg(feature = "xwayland")]
let xwayland_state = xwayland::XWaylandState::create(&display_handle)?;
let wayland_state = WaylandState::new(display_handle, &buffer_manager.renderer, dmabuf_tx);
let socket = ListeningSocket::bind_auto("wayland", 0..33)?;
let socket_name = socket.socket_name().unwrap().to_str().unwrap().to_string();
WAYLAND_DISPLAY
.set(socket_name.clone())
.expect("seriously message nova this time they screwed up big time");
let socket_name = socket
.socket_name()
.and_then(OsStr::to_str)
.map(ToString::to_string);
if let Some(socket_name) = &socket_name {
let _ = WAYLAND_DISPLAY.set(socket_name.clone());
}
info!(socket_name, "Wayland active");
let join_handle =
Wayland::start_loop(display.clone(), socket, state.clone(), global_destroy_queue)?;
let join_handle = Wayland::start_loop(display.clone(), socket, wayland_state.clone())?;
Ok(Wayland {
display,
socket_name,
join_handle,
renderer,
state,
dmabuf_rx,
wayland_state,
#[cfg(feature = "xwayland")]
xwayland_state,
})
}
fn start_loop(
display: Arc<Mutex<Display<WaylandState>>>,
display: Arc<DisplayWrapper>,
socket: ListeningSocket,
state: Arc<Mutex<WaylandState>>,
mut global_destroy_queue: mpsc::Receiver<GlobalId>,
) -> Result<JoinHandle<Result<()>>> {
let listen_async =
AsyncUnixListener::from_std(unsafe { UnixListener::from_raw_fd(socket.as_raw_fd()) })?;
let dispatch_poll_fd = display.lock().backend().poll_fd().try_clone_to_owned()?;
let dispatch_poll_fd = display.poll_fd()?;
let dispatch_poll_listener = AsyncFd::new(dispatch_poll_fd)?;
let dh1 = display.lock().handle();
let dh1 = display.handle();
let mut dh2 = dh1.clone();
Ok(task::new(|| "wayland loop", async move {
let _socket = socket; // Keep the socket alive
loop {
tokio::select! {
e = global_destroy_queue.recv() => { // New global to destroy
debug!(?e, "destroy global");
dh1.remove_global::<WaylandState>(e.unwrap());
}
acc = listen_async.accept() => { // New client connected
let (stream, _) = acc?;
let client = dh2.insert_client(stream.into_std()?, Arc::new(ClientState::default()))?;
state.lock().new_client(client.id(), &dh2);
let client_state = Arc::new(ClientState {
id: OnceCell::new(),
compositor_state: Default::default(),
display: Arc::downgrade(&display),
seat: SeatData::new(&dh1)
});
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
let mut guard = e?;
debug_span!("Dispatch wayland event").in_scope(|| -> Result<(), color_eyre::Report> {
let mut display = display.lock();
display.dispatch_clients(&mut *state.lock())?;
display.flush_clients()?;
display.flush_clients(None);
Ok(())
})?;
guard.clear_ready();
@@ -148,26 +144,27 @@ impl Wayland {
})?)
}
#[instrument(level = "debug", name = "Wayland frame", skip(self, sk))]
pub fn update(&mut self, sk: &impl StereoKitDraw) {
#[instrument(
level = "debug",
name = "Wayland frame",
skip(self, sk, buffer_manager)
)]
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() {
core_surface.process(sk, &mut self.renderer);
core_surface.process(sk, &mut buffer_manager.renderer);
}
self.display.lock().flush_clients().unwrap();
self.display.flush_clients(None);
}
pub fn frame_event(&self, sk: &impl StereoKitDraw) {
let state = self.state.lock();
let output = self.wayland_state.lock().output.clone();
for core_surface in CORE_SURFACES.get_valid_contents() {
core_surface.frame(sk, state.output.clone());
}
}
pub fn make_context_current(&self) {
unsafe {
self.renderer.egl_context().make_current().unwrap();
core_surface.frame(sk, output.clone());
}
}
}

View File

@@ -1,655 +0,0 @@
use super::{
seat::{Cursor, SeatData},
surface::CoreSurface,
xdg_shell::{PopupData, ToplevelData, XdgSurfaceData},
SERIAL_COUNTER,
};
use crate::{
core::{
client::{get_env, startup_settings, Client, INTERNAL_CLIENT},
registry::Registry,
},
nodes::{
drawable::Drawable,
items::{self, Item, ItemSpecialization, ItemType, TypeInfo},
spatial::Spatial,
Node,
},
wayland::seat::{KeyboardEvent, PointerEvent},
};
use color_eyre::eyre::{bail, eyre, Result};
use glam::Mat4;
use lazy_static::lazy_static;
use mint::Vector2;
use nanoid::nanoid;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::{
de::{Deserializer, Error, SeqAccess, Visitor},
ser::Serializer,
Deserialize, Serialize,
};
use smithay::{
reexports::{
wayland_protocols::xdg::shell::server::{
xdg_popup::XdgPopup,
xdg_surface::XdgSurface,
xdg_toplevel::{XdgToplevel, EVT_CONFIGURE_BOUNDS_SINCE, EVT_WM_CAPABILITIES_SINCE},
},
wayland_server::{
backend::Credentials, protocol::wl_surface::WlSurface, Resource, Weak as WlWeak,
},
},
wayland::compositor,
};
use stardust_xr::schemas::flex::{deserialize, serialize};
use std::sync::{Arc, Weak};
use tracing::debug;
use xkbcommon::xkb::{self, ffi::XKB_KEYMAP_FORMAT_TEXT_V1, Keymap};
lazy_static! {
pub static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo {
type_name: "panel",
aliased_local_signals: vec![
"apply_surface_material",
"configure_toplevel",
"set_toplevel_capabilities",
"pointer_scroll",
"pointer_button",
"pointer_motion",
"keyboard_key",
"keyboard_set_keymap_names",
"keyboard_set_keymap_string",
"close",
],
aliased_local_methods: vec![],
aliased_remote_signals: vec![
"commit_toplevel",
"recommend_toplevel_state",
"set_cursor",
"new_popup",
"reposition_popup",
"drop_popup",
],
ui: Default::default(),
items: Registry::new(),
acceptors: Registry::new(),
};
}
/// An ID for a surface inside this panel item
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum SurfaceID {
Cursor,
Toplevel,
Popup(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),
"Popup" => {
let Some(text) = seq.next_element()? else {
return Err(A::Error::missing_field("popup_text"));
};
Ok(SurfaceID::Popup(text))
}
_ => Err(A::Error::unknown_variant(
discrim,
&["Cursor", "Toplevel", "Popup"],
)),
}
}
}
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::Popup(text) => ["Popup", text].serialize(serializer),
}
}
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(tag = "type", content = "content")]
pub enum RecommendedState {
Maximize(bool),
Fullscreen(bool),
Minimize,
Move,
Resize(u32),
}
pub struct PanelItem {
pub uid: String,
node: Weak<Node>,
cursor: Mutex<Option<WlWeak<WlSurface>>>,
pub seat_data: Arc<SeatData>,
toplevel: WlWeak<XdgToplevel>,
popups: Mutex<FxHashMap<String, WlWeak<XdgPopup>>>,
pointer_grab: Mutex<Option<SurfaceID>>,
keyboard_grab: Mutex<Option<SurfaceID>>,
}
impl PanelItem {
pub fn create(
toplevel: XdgToplevel,
wl_surface: WlSurface,
client_credentials: Option<Credentials>,
seat_data: Arc<SeatData>,
) -> (Arc<Node>, Arc<PanelItem>) {
debug!(?toplevel, ?client_credentials, "Create panel item");
let startup_settings = client_credentials
.and_then(|cred| get_env(cred.pid).ok())
.and_then(|env| startup_settings(&env));
let uid = nanoid!();
let node = Arc::new(Node::create(
&INTERNAL_CLIENT,
"/item/panel/item",
&uid,
true,
));
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
let panel_item = Arc::new(PanelItem {
uid: uid.clone(),
node: Arc::downgrade(&node),
cursor: Mutex::new(None),
seat_data,
toplevel: toplevel.downgrade(),
popups: Mutex::new(FxHashMap::default()),
pointer_grab: Mutex::new(None),
keyboard_grab: Mutex::new(None),
});
if let Some(startup_settings) = &startup_settings {
spatial.set_local_transform(
spatial.global_transform().inverse() * startup_settings.transform,
);
}
panel_item
.seat_data
.new_surface(&wl_surface, Arc::downgrade(&panel_item));
let item = Item::add_to(
&node,
uid,
&ITEM_TYPE_INFO_PANEL,
ItemType::Panel(panel_item.clone()),
);
if let Some(startup_settings) = &startup_settings {
if let Some(acceptor) = startup_settings
.acceptors
.get(&*ITEM_TYPE_INFO_PANEL)
.and_then(|acc| acc.upgrade())
{
items::capture(&item, &acceptor);
}
}
node.add_local_signal(
"apply_surface_material",
PanelItem::apply_surface_material_flex,
);
node.add_local_signal("configure_toplevel", PanelItem::configure_toplevel_flex);
node.add_local_signal(
"set_toplevel_capabilities",
PanelItem::set_toplevel_capabilities_flex,
);
node.add_local_signal("pointer_scroll", PanelItem::pointer_scroll_flex);
node.add_local_signal("pointer_button", PanelItem::pointer_button_flex);
node.add_local_signal("pointer_motion", PanelItem::pointer_motion_flex);
node.add_local_signal(
"keyboard_set_keymap_string",
PanelItem::keyboard_set_keymap_string_flex,
);
node.add_local_signal(
"keyboard_set_keymap_names",
PanelItem::keyboard_set_keymap_names_flex,
);
node.add_local_signal("keyboard_key", PanelItem::keyboard_key_flex);
(node, panel_item)
}
pub fn from_node(node: &Node) -> Option<Arc<PanelItem>> {
let ItemType::Panel(panel_item) = &node.item.get()?.specialization else {return None};
Some(panel_item.clone())
}
fn toplevel(&self) -> XdgToplevel {
self.toplevel.upgrade().unwrap()
}
fn toplevel_xdg_surface(&self) -> XdgSurface {
let toplevel = self.toplevel();
let data = ToplevelData::get(&toplevel).lock();
data.xdg_surface()
}
fn toplevel_wl_surface(&self) -> WlSurface {
XdgSurfaceData::get(&self.toplevel_xdg_surface())
.lock()
.wl_surface()
}
fn core_surface(&self) -> Option<Arc<CoreSurface>> {
compositor::with_states(&self.toplevel_wl_surface(), |data| {
data.data_map.get::<Arc<CoreSurface>>().cloned()
})
}
fn flush_clients(&self) {
if let Some(core_surface) = self.core_surface() {
core_surface.flush_clients();
}
}
fn wl_surface_from_id(&self, id: &SurfaceID) -> Option<WlSurface> {
match id {
SurfaceID::Cursor => self.cursor.lock().clone()?.upgrade().ok(),
SurfaceID::Toplevel => Some(self.toplevel_wl_surface()),
SurfaceID::Popup(popup) => {
let popups = self.popups.lock();
let popup = popups.get(popup)?.upgrade().ok()?;
let surf = PopupData::get(&popup).lock().wl_surface();
Some(surf)
}
}
}
fn wl_surface_from_id_result(&self, id: &SurfaceID) -> Result<WlSurface> {
self.wl_surface_from_id(id)
.ok_or(eyre!("Surface with ID not found"))
}
fn apply_surface_material_flex(
node: &Node,
calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
#[derive(Debug, Deserialize)]
struct SurfaceMaterialInfo<'a> {
surface: SurfaceID,
model_node_path: &'a str,
}
let info: SurfaceMaterialInfo = deserialize(data)?;
let Some(wl_surface) = panel_item.wl_surface_from_id(&info.surface) else { return Ok(()) };
let Some(core_surface) = CoreSurface::from_wl_surface(&wl_surface) else { return Ok(()) };
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_node)) = model_node.drawable.get() else {bail!("Node is not a model")};
debug!(?info, "Apply surface material");
core_surface.apply_material(model_node);
Ok(())
}
fn pointer_motion_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let (surface_id, position): (SurfaceID, Vector2<f64>) = deserialize(data)?;
let wl_surface = panel_item.wl_surface_from_id_result(&surface_id)?;
debug!(?surface_id, ?position, "Pointer deactivate");
panel_item
.seat_data
.pointer_event(&wl_surface, PointerEvent::Motion(position));
panel_item.flush_clients();
Ok(())
}
fn pointer_button_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let (surface_id, button, state): (SurfaceID, u32, u32) = deserialize(data)?;
let wl_surface = panel_item.wl_surface_from_id_result(&surface_id)?;
debug!(?surface_id, button, state, "Pointer button");
panel_item
.seat_data
.pointer_event(&wl_surface, PointerEvent::Button { button, state });
panel_item.flush_clients();
Ok(())
}
fn pointer_scroll_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
#[derive(Debug, Deserialize)]
struct PointerScrollInfo {
surface_id: SurfaceID,
axis_continuous: Option<Vector2<f32>>,
axis_discrete: Option<Vector2<f32>>,
}
let info: PointerScrollInfo = deserialize(data)?;
let wl_surface = panel_item.wl_surface_from_id_result(&info.surface_id)?;
debug!(?info, "Pointer scroll");
panel_item.seat_data.pointer_event(
&wl_surface,
PointerEvent::Scroll {
axis_continuous: info.axis_continuous,
axis_discrete: info.axis_discrete,
},
);
panel_item.flush_clients();
Ok(())
}
fn keyboard_set_keymap_string_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let context = xkb::Context::new(0);
let keymap =
Keymap::new_from_string(&context, deserialize(data)?, XKB_KEYMAP_FORMAT_TEXT_V1, 0)
.ok_or_else(|| eyre!("Keymap is not valid"))?;
PanelItem::keyboard_set_keymap_flex(node, &keymap)
}
fn keyboard_set_keymap_names_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
#[derive(Debug, Deserialize)]
struct Names<'a> {
rules: &'a str,
model: &'a str,
layout: &'a str,
variant: &'a str,
options: Option<String>,
}
let names: Names = deserialize(data)?;
let context = xkb::Context::new(0);
let keymap = Keymap::new_from_names(
&context,
names.rules,
names.model,
names.layout,
names.variant,
names.options,
XKB_KEYMAP_FORMAT_TEXT_V1,
)
.ok_or_else(|| eyre!("Keymap is not valid"))?;
PanelItem::keyboard_set_keymap_flex(node, &keymap)
}
fn keyboard_set_keymap_flex(node: &Node, keymap: &Keymap) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let toplevel = panel_item.toplevel_wl_surface();
debug!(?toplevel, "Keyboard set keymap");
let mut surfaces = vec![toplevel];
surfaces.extend(panel_item.popups.lock().values().filter_map(|p| {
let popup = p.upgrade().ok()?;
let popup_data = PopupData::get(&popup).lock();
let xdg_surface = popup_data.xdg_surface();
let xdg_surface_data = XdgSurfaceData::get(&xdg_surface).lock();
Some(xdg_surface_data.wl_surface())
}));
panel_item.seat_data.set_keymap(keymap, surfaces);
Ok(())
}
fn keyboard_key_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let (surface_id, key, state): (SurfaceID, u32, u32) = deserialize(data)?;
let wl_surface = panel_item.wl_surface_from_id_result(&surface_id)?;
debug!(key, state, "Set keyboard key state");
panel_item
.seat_data
.keyboard_event(&wl_surface, KeyboardEvent::Key { key, state });
Ok(())
}
fn configure_toplevel_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let Some(core_surface) = panel_item.core_surface() else { return Ok(()) };
let Ok(xdg_toplevel) = panel_item.toplevel.upgrade() else { return Ok(()) };
let xdg_surface = panel_item.toplevel_xdg_surface();
#[derive(Debug, Deserialize)]
struct ConfigureToplevelInfo {
size: Option<Vector2<u32>>,
states: Vec<u32>,
bounds: Option<Vector2<u32>>,
}
let info: ConfigureToplevelInfo = deserialize(data)?;
debug!(info = ?&info, "Configure toplevel info");
if let Some(bounds) = info.bounds {
if xdg_toplevel.version() > EVT_CONFIGURE_BOUNDS_SINCE {
xdg_toplevel.configure_bounds(bounds.x as i32, bounds.y as i32);
}
}
let size = info.size.unwrap_or(Vector2::from([0; 2]));
xdg_toplevel.configure(
size.x as i32,
size.y as i32,
info.states
.into_iter()
.flat_map(|state| state.to_ne_bytes())
.collect::<Vec<_>>(),
);
xdg_surface.configure(SERIAL_COUNTER.inc());
core_surface.flush_clients();
Ok(())
}
fn set_toplevel_capabilities_flex(
node: &Node,
_calling_client: Arc<Client>,
data: &[u8],
) -> Result<()> {
let Some(panel_item) = PanelItem::from_node(node) else { return Ok(()) };
let Some(core_surface) = panel_item.core_surface() else { return Ok(()) };
let Ok(xdg_toplevel) = panel_item.toplevel.upgrade() else { return Ok(()) };
if xdg_toplevel.version() < EVT_WM_CAPABILITIES_SINCE {
return Ok(());
}
let xdg_surface = panel_item.toplevel_xdg_surface();
let capabilities: Vec<u8> = deserialize(data)?;
debug!("Set toplevel capabilities");
xdg_toplevel.wm_capabilities(capabilities);
xdg_surface.configure(SERIAL_COUNTER.inc());
core_surface.flush_clients();
Ok(())
}
pub fn commit_toplevel(&self) {
// let mapped_size = self.core_surface().and_then(|c| c.size());
let toplevel = self.toplevel();
let state = ToplevelData::get(&toplevel);
let state = state.lock();
// let mut queued_state = state.queued_state.take().unwrap();
// queued_state.mapped = mapped_size.is_some();
// if let Some(size) = mapped_size {
// queued_state.size = size;
// queued_state.geometry.update_to_surface_size(size);
// }
// *state = (*queued_state).clone();
// state.queued_state = Some(queued_state);
debug!(state = ?&*state, "Commit toplevel");
let Some(node) = self.node.upgrade() else { return };
let _ = node.send_remote_signal("commit_toplevel", &serialize(&*state).unwrap());
}
pub fn recommend_toplevel_state(&self, state: RecommendedState) {
let Some(node) = self.node.upgrade() else { return };
let data = serialize(state).unwrap();
debug!(?state, "Recommend toplevel state");
let _ = node.send_remote_signal("recommend_toplevel_state", &data);
}
pub fn new_popup(&self, popup: &XdgPopup, data: &PopupData) {
let uid = data.uid.clone();
self.popups.lock().insert(uid.clone(), popup.downgrade());
let Some(node) = self.node.upgrade() else { return };
let _ = node.send_remote_signal("new_popup", &serialize(&(&uid, data)).unwrap());
}
// pub fn commit_popup(&self, data: &PopupData) {
// let xdg_surf = data.xdg_surface.upgrade().unwrap();
// let surf = xdg_surf
// .data::<XdgSurfaceData>()
// .unwrap()
// .wl_surface
// .upgrade()
// .unwrap();
// let core_surface =
// compositor::with_states(&surf, |s| s.data_map.get::<Arc<CoreSurface>>().cloned())
// .unwrap();
// let mut popup_state = data.state.lock();
// popup_state.mapped = core_surface.size().is_some();
// }
pub fn reposition_popup(&self, popup_state: &PopupData) {
let Some(node) = self.node.upgrade() else { return };
let _ = node.send_remote_signal(
"reposition_popup",
&serialize(popup_state.positioner_data().unwrap()).unwrap(),
);
}
pub fn drop_popup(&self, uid: &str) {
if let Some(popup) = self
.popups
.lock()
.remove(uid)
.and_then(|popup| popup.upgrade().ok()?.data::<Arc<PopupData>>().cloned())
{
self.seat_data.drop_surface(&popup.wl_surface());
}
let Some(node) = self.node.upgrade() else { return };
let _ = node.send_remote_signal("drop_popup", &serialize(uid).unwrap());
}
pub fn grab_keyboard(&self, sid: Option<SurfaceID>) {
let Some(node) = self.node.upgrade() else { return };
let _ = node.send_remote_signal("grab_keyboard", &serialize(sid).unwrap());
}
pub fn set_cursor(&self, surface: Option<&WlSurface>, hotspot_x: i32, hotspot_y: i32) {
let Some(node) = self.node.upgrade() else { return };
debug!(?surface, hotspot_x, hotspot_y, "Set cursor size");
let mut data = serialize(()).unwrap();
let cursor_size = surface
.and_then(|c| CoreSurface::from_wl_surface(c))
.and_then(|c| c.size());
if let Some(size) = cursor_size {
data = serialize((size, (hotspot_x, hotspot_y))).unwrap();
}
let _ = node.send_remote_signal("set_cursor", &data);
*self.cursor.lock() = surface.map(|surf| surf.downgrade());
}
pub fn on_drop(&self) {
let toplevel = self.toplevel_wl_surface();
self.seat_data.drop_surface(&toplevel);
debug!("Drop panel item");
}
}
impl ItemSpecialization for PanelItem {
fn serialize_start_data(&self, id: &str) -> Vec<u8> {
let cursor = self.cursor.lock().as_ref().and_then(|c| c.upgrade().ok());
let cursor_size = cursor
.as_ref()
.and_then(|c| CoreSurface::from_wl_surface(&c))
.and_then(|c| c.size());
let cursor_hotspot = cursor
.and_then(|c| {
compositor::with_states(&c, |data| data.data_map.get::<Arc<Cursor>>().cloned())
})
.map(|cursor| cursor.hotspot);
let toplevel = self.toplevel();
let toplevel_state = ToplevelData::get(&toplevel);
let toplevel_state = toplevel_state.lock().clone();
let popups = self
.popups
.lock()
.values()
.filter_map(|v| Some(v.upgrade().ok()?.data::<Mutex<PopupData>>()?.lock().clone()))
.collect::<Vec<_>>();
let pointer_grab = self.pointer_grab.lock().clone();
let keyboard_grab = self.keyboard_grab.lock().clone();
serialize((
id,
(
cursor_size.zip(cursor_hotspot),
toplevel_state,
popups,
pointer_grab,
keyboard_grab,
),
))
.unwrap()
}
}
impl Drop for PanelItem {
fn drop(&mut self) {
// Dropped panel item, basically just a debug breakpoint place
}
}

View File

@@ -1,9 +1,13 @@
use super::{
panel_item::PanelItem, state::WaylandState, surface::CoreSurface, GLOBAL_DESTROY_QUEUE,
state::{ClientState, WaylandState},
surface::CoreSurface,
SERIAL_COUNTER,
};
use crate::core::task;
use color_eyre::eyre::Result;
use crate::{
core::task,
nodes::items::panel::{Backend, Geometry, PanelItem},
};
use color_eyre::eyre::{bail, eyre, Result};
use mint::Vector2;
use nanoid::nanoid;
use once_cell::sync::OnceCell;
@@ -27,39 +31,51 @@ use smithay::{
};
use std::{
collections::VecDeque,
sync::{Arc, Weak},
sync::Arc,
time::{Duration, Instant},
};
use tokio::sync::watch;
use tracing::{debug, warn};
use xkbcommon::xkb::{self, Keymap};
use xkbcommon::xkb::{self, ffi::XKB_KEYMAP_FORMAT_TEXT_V1, Keycode, Keymap};
pub fn handle_cursor<B: Backend>(
panel_item: &Arc<PanelItem<B>>,
mut cursor: watch::Receiver<Option<CursorInfo>>,
) {
let panel_item_weak = Arc::downgrade(panel_item);
let _ = task::new(|| "cursor handler", async move {
while cursor.changed().await.is_ok() {
let Some(panel_item) = panel_item_weak.upgrade() else {continue};
let cursor_info = cursor.borrow();
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: &Keymap) -> Self {
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, state: u32, keyboard: &WlKeyboard) -> Result<usize> {
let wl_key_state = match state {
0 => KeyState::Released,
1 => KeyState::Pressed,
_ => color_eyre::eyre::bail!("Invalid key state!"),
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 xkb_key_state = match state {
0 => xkb::KeyDirection::Up,
1 => xkb::KeyDirection::Down,
_ => color_eyre::eyre::bail!("Invalid key state!"),
};
let state_components = self.state.update_key(key + 8, xkb_key_state);
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(
@@ -70,6 +86,17 @@ impl KeyboardInfo {
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 => {
@@ -87,7 +114,7 @@ unsafe impl Send for KeyboardInfo {}
#[derive(Debug, Clone, Copy)]
pub enum PointerEvent {
Motion(Vector2<f64>),
Motion(Vector2<f32>),
Button {
button: u32,
state: u32,
@@ -100,29 +127,36 @@ pub enum PointerEvent {
#[derive(Debug, Clone)]
pub enum KeyboardEvent {
Keymap,
Key { key: u32, state: u32 },
Key { key: u32, state: bool },
}
const POINTER_EVENT_TIMEOUT: Duration = Duration::from_secs(1);
const POINTER_EVENT_TIMEOUT: Duration = Duration::from_millis(50);
struct SurfaceInfo {
wl_surface: WlWeak<WlSurface>,
panel_item: Weak<PanelItem>,
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, panel_item: Weak<PanelItem>) -> Self {
fn new(wl_surface: &WlSurface, cursor_sender: watch::Sender<Option<CursorInfo>>) -> Self {
SurfaceInfo {
wl_surface: wl_surface.downgrade(),
panel_item,
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; };
@@ -137,18 +171,20 @@ impl SurfaceInfo {
pointer.enter(
SERIAL_COUNTER.inc(),
&focus,
pos.x.clamp(0.0, focus_size.x as f64),
pos.y.clamp(0.0, focus_size.y as f64),
(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.clamp(0.0, focus_size.x as f64),
pos.y.clamp(0.0, focus_size.y as f64),
(pos.x as f64).clamp(0.0, focus_size.x as f64),
(pos.y as f64).clamp(0.0, focus_size.y as f64),
);
pointer.frame();
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
pointer.frame();
}
}
(true, PointerEvent::Button { button, state }) => {
pointer.button(
@@ -161,7 +197,9 @@ impl SurfaceInfo {
_ => continue,
},
);
pointer.frame();
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
pointer.frame();
}
}
(
true,
@@ -172,17 +210,24 @@ impl SurfaceInfo {
) => {
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);
pointer.axis(0, Axis::VerticalScroll, -axis_continuous.y as f64);
}
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_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 axis_discrete.is_none() && axis_continuous.is_none() {
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);
}
pointer.frame();
if pointer.version() >= wl_pointer::EVT_FRAME_SINCE {
pointer.frame();
}
}
(locked, event) => {
warn!(locked, ?event, "Invalid pointer event!");
@@ -193,6 +238,7 @@ impl SurfaceInfo {
pointer.leave(SERIAL_COUNTER.inc(), &focus);
locked = false;
}
self.flush();
locked
}
@@ -202,7 +248,9 @@ impl SurfaceInfo {
if !locked {
keyboard.enter(0, &focus, vec![]);
keyboard.repeat_info(0, 0);
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() {
@@ -224,50 +272,62 @@ impl SurfaceInfo {
}
}
}
self.flush();
locked
}
}
pub struct SeatData {
client: ClientId,
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, client: ClientId) -> Arc<Self> {
pub fn new(dh: &DisplayHandle) -> Arc<Self> {
let seat_data = Arc::new(SeatData {
client,
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()),
});
seat_data
let _ = seat_data
.global_id
.set(dh.create_global::<WaylandState, _, _>(7, seat_data.clone()))
.unwrap();
.set(dh.create_global::<WaylandState, _, _>(7, seat_data.clone()));
seat_data
}
pub fn set_keymap(&self, keymap: &Keymap, surfaces: Vec<WlSurface>) {
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 {return};
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));
.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) {
@@ -343,10 +403,13 @@ impl SeatData {
}
}
pub fn new_surface(&self, surface: &WlSurface, panel_item: Weak<PanelItem>) {
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, panel_item));
.insert(surface.id(), SurfaceInfo::new(surface, tx));
rx
}
pub fn drop_surface(&self, surface: &WlSurface) {
self.surfaces.lock().remove(&surface.id());
@@ -362,14 +425,51 @@ impl SeatData {
*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);
}
}
}
impl Drop for SeatData {
fn drop(&mut self) {
let id = self.global_id.take().unwrap();
let _ = task::new(|| "global destroy queue garbage collection", async move {
GLOBAL_DESTROY_QUEUE.get().unwrap().send(id).await
});
pub struct CursorInfo {
pub surface: WlWeak<WlSurface>,
pub hotspot_x: i32,
pub hotspot_y: i32,
}
impl CursorInfo {
pub fn cursor_data(&self) -> Option<Geometry> {
let cursor_size = CoreSurface::from_wl_surface(&self.surface.upgrade().ok()?)?.size()?;
Some(Geometry {
origin: [self.hotspot_x, self.hotspot_y].into(),
size: cursor_size,
})
}
}
@@ -388,11 +488,12 @@ impl GlobalDispatch<WlSeat, Arc<SeatData>, WaylandState> for WaylandState {
resource.name(nanoid!());
}
resource.capabilities(Capability::Pointer | Capability::Keyboard);
resource.capabilities(Capability::Pointer | Capability::Keyboard | Capability::Touch);
}
fn can_view(client: Client, data: &Arc<SeatData>) -> bool {
client.id() == data.client
let Some(seat_client) = data.client.get().cloned() else {return false};
client.id() == seat_client
}
}
@@ -413,7 +514,9 @@ impl Dispatch<WlSeat, Arc<SeatData>, WaylandState> for WaylandState {
}
wl_seat::Request::GetKeyboard { id } => {
let keyboard = data_init.init(id, data.clone());
keyboard.repeat_info(0, 0);
if keyboard.version() >= wl_keyboard::EVT_REPEAT_INFO_SINCE {
keyboard.repeat_info(0, 0);
}
let _ = data.keyboard.set((keyboard, Mutex::new(ObjectId::null())));
}
wl_seat::Request::GetTouch { id } => {
@@ -425,12 +528,9 @@ impl Dispatch<WlSeat, Arc<SeatData>, WaylandState> for WaylandState {
}
}
pub struct Cursor {
pub hotspot: Vector2<i32>,
}
impl Dispatch<WlPointer, Arc<SeatData>, WaylandState> for WaylandState {
fn request(
state: &mut WaylandState,
_state: &mut WaylandState,
_client: &Client,
_resource: &WlPointer,
request: wl_pointer::Request,
@@ -446,16 +546,8 @@ impl Dispatch<WlPointer, Arc<SeatData>, WaylandState> for WaylandState {
hotspot_y,
} => {
if let Some(surface) = surface.as_ref() {
CoreSurface::add_to(&state.display, dh.clone(), surface, |_| ());
CoreSurface::add_to(dh.clone(), surface, || (), |_| ());
compositor::with_states(surface, |data| {
data.data_map.insert_if_missing_threadsafe(|| {
Arc::new(Mutex::new(Cursor {
hotspot: Vector2::from([hotspot_x, hotspot_y]),
}))
});
let mut cursor = data.data_map.get::<Arc<Mutex<Cursor>>>().unwrap().lock();
cursor.hotspot = Vector2::from([hotspot_x, hotspot_y]);
if let Some(core_surface) = data.data_map.get::<Arc<CoreSurface>>() {
core_surface.set_material_offset(1);
}
@@ -466,8 +558,12 @@ impl Dispatch<WlPointer, Arc<SeatData>, WaylandState> for WaylandState {
let focus = focus.lock();
let surfaces = seat_data.surfaces.lock();
let Some(surface_info) = surfaces.get(&focus) else {return};
let Some(panel_item) = surface_info.panel_item.upgrade() else {return};
panel_item.set_cursor(surface.as_ref(), hotspot_x, hotspot_y);
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!(),

View File

@@ -1,7 +0,0 @@
#![allow(dead_code)]
// Basic gamma correction shader
// pub const PANEL_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");

View File

@@ -1,11 +1,8 @@
use crate::wayland::seat::SeatData;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use smithay::{
backend::{
allocator::dmabuf::Dmabuf,
renderer::{gles::GlesRenderer, ImportDma},
},
backend::{allocator::dmabuf::Dmabuf, egl::EGLDevice, renderer::gles::GlesRenderer},
delegate_dmabuf, delegate_output, delegate_shm,
output::{Mode, Output, Scale, Subpixel},
reexports::{
@@ -17,28 +14,43 @@ use smithay::{
wayland_server::{
backend::{ClientData, ClientId, DisconnectReason},
protocol::{wl_buffer::WlBuffer, wl_data_device_manager::WlDataDeviceManager},
Display, DisplayHandle,
DisplayHandle,
},
},
utils::{Size, Transform},
wayland::{
buffer::BufferHandler,
compositor::{CompositorClientState, CompositorState},
dmabuf::{self, DmabufGlobal, DmabufHandler, DmabufState},
dmabuf::{
self, DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState,
ImportError,
},
shell::kde::decoration::KdeDecorationState,
shm::{ShmHandler, ShmState},
},
};
use std::sync::{Arc, Weak};
use tracing::info;
use tokio::sync::mpsc::UnboundedSender;
use tracing::{info, warn};
use super::DisplayWrapper;
#[derive(Default)]
pub struct ClientState {
pub id: OnceCell<ClientId>,
pub compositor_state: CompositorClientState,
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 {
fn initialized(&self, client_id: ClientId) {
info!("Wayland client {:?} connected", client_id);
let _ = self.id.set(client_id);
}
fn disconnected(&self, client_id: ClientId, reason: DisconnectReason) {
@@ -51,36 +63,66 @@ impl ClientData for ClientState {
pub struct WaylandState {
pub weak_ref: Weak<Mutex<WaylandState>>,
pub display: Arc<Mutex<Display<WaylandState>>>,
pub display_handle: DisplayHandle,
pub compositor_state: CompositorState,
// pub xdg_activation_state: XdgActivationState,
pub kde_decoration_state: KdeDecorationState,
pub shm_state: ShmState,
pub dmabuf_state: DmabufState,
pub dmabuf_global: DmabufGlobal,
pub pending_dmabufs: Vec<Dmabuf>,
dmabuf_state: (DmabufState, DmabufGlobal, Option<DmabufFeedback>),
dmabuf_tx: UnboundedSender<Dmabuf>,
pub output: Output,
pub seats: FxHashMap<ClientId, Arc<SeatData>>,
}
impl WaylandState {
pub fn new(
display: Arc<Mutex<Display<WaylandState>>>,
display_handle: DisplayHandle,
renderer: &GlesRenderer,
dmabuf_tx: UnboundedSender<Dmabuf>,
) -> Arc<Mutex<Self>> {
let compositor_state = CompositorState::new::<Self>(&display_handle);
// let xdg_activation_state = XdgActivationState::new::<Self, _>(&display_handle);
let kde_decoration_state =
KdeDecorationState::new::<Self>(&display_handle, DecorationMode::Server);
let shm_state = ShmState::new::<Self>(&display_handle, vec![]);
let mut dmabuf_state = DmabufState::new();
let dmabuf_global = dmabuf_state.create_global::<Self>(
&display_handle,
renderer.dmabuf_formats().collect::<Vec<_>>(),
);
let render_node = EGLDevice::device_for_display(renderer.egl_context().display())
.and_then(|device| device.try_get_render_node());
let dmabuf_formats = renderer
.egl_context()
.dmabuf_render_formats()
.iter()
.cloned()
.collect::<Vec<_>>();
let dmabuf_default_feedback = match render_node {
Ok(Some(node)) => DmabufFeedbackBuilder::new(node.dev_id(), dmabuf_formats.clone())
.build()
.ok(),
Ok(None) => {
warn!("failed to query render node, dmabuf will use v3");
None
}
Err(err) => {
warn!(?err, "failed to egl device for display, dmabuf will use v3");
None
}
};
// if we failed to build dmabuf feedback we fall back to dmabuf v3
// Note: egl on Mesa requires either v4 or wl_drm (initialized with bind_wl_display)
let dmabuf_state = if let Some(default_feedback) = dmabuf_default_feedback {
let mut dmabuf_state = DmabufState::new();
let dmabuf_global = dmabuf_state.create_global_with_default_feedback::<WaylandState>(
&display_handle,
&default_feedback,
);
(dmabuf_state, dmabuf_global, Some(default_feedback))
} else {
let mut dmabuf_state = DmabufState::new();
let dmabuf_global =
dmabuf_state.create_global::<WaylandState>(&display_handle, dmabuf_formats.clone());
(dmabuf_state, dmabuf_global, None)
};
let output = Output::new(
"1x".to_owned(),
smithay::output::PhysicalProperties {
@@ -92,7 +134,7 @@ impl WaylandState {
);
let _output_global = output.create_global::<Self>(&display_handle);
let mode = Mode {
size: (4096, 4096).into(),
size: (2048, 2048).into(),
refresh: 60000,
};
output.change_current_state(
@@ -111,7 +153,6 @@ impl WaylandState {
Arc::new_cyclic(|weak| {
Mutex::new(WaylandState {
weak_ref: weak.clone(),
display,
display_handle,
compositor_state,
@@ -119,18 +160,11 @@ impl WaylandState {
kde_decoration_state,
shm_state,
dmabuf_state,
dmabuf_global,
pending_dmabufs: Vec::new(),
dmabuf_tx,
output,
seats: FxHashMap::default(),
})
})
}
pub fn new_client(&mut self, client: ClientId, dh: &DisplayHandle) {
let seat_data = SeatData::new(dh, client.clone());
self.seats.insert(client, seat_data);
}
}
impl Drop for WaylandState {
fn drop(&mut self) {
@@ -147,15 +181,15 @@ impl ShmHandler for WaylandState {
}
impl DmabufHandler for WaylandState {
fn dmabuf_state(&mut self) -> &mut DmabufState {
&mut self.dmabuf_state
&mut self.dmabuf_state.0
}
fn dmabuf_imported(
&mut self,
_global: &DmabufGlobal,
_dmabuf: Dmabuf,
dmabuf: Dmabuf,
) -> Result<(), dmabuf::ImportError> {
Ok(())
self.dmabuf_tx.send(dmabuf).map_err(|_| ImportError::Failed)
}
}
delegate_dmabuf!(WaylandState);

View File

@@ -1,7 +1,7 @@
use super::{shaders::PANEL_SHADER_BYTES, state::WaylandState};
use super::state::WaylandState;
use crate::{
core::{delta::Delta, destroy_queue, registry::Registry},
nodes::drawable::model::ModelPart,
nodes::drawable::{model::ModelPart, shaders::PANEL_SHADER_BYTES},
};
use mint::Vector2;
use once_cell::sync::OnceCell;
@@ -15,19 +15,13 @@ use smithay::{
},
desktop::utils::send_frames_surface_tree,
output::Output,
reexports::wayland_server::{
self, protocol::wl_surface::WlSurface, Display, DisplayHandle, Resource,
},
reexports::wayland_server::{self, protocol::wl_surface::WlSurface, DisplayHandle, Resource},
wayland::compositor::{self, SurfaceData},
};
use std::{
ffi::c_void,
sync::{Arc, Weak},
time::Duration,
};
use std::{cell::RefCell, ffi::c_void, sync::Arc, time::Duration};
use stereokit::{
Material, StereoKitDraw, Tex, TextureAddress, TextureFormat, TextureSample, TextureType,
Transparency,
Material, Shader, StereoKitDraw, Tex, TextureAddress, TextureFormat, TextureSample,
TextureType, Transparency,
};
pub static CORE_SURFACES: Registry<CoreSurface> = Registry::new();
@@ -43,34 +37,34 @@ impl Drop for CoreSurfaceData {
}
pub struct CoreSurface {
display: Weak<Mutex<Display<WaylandState>>>,
pub dh: DisplayHandle,
pub weak_surface: wayland_server::Weak<WlSurface>,
mapped_data: Mutex<Option<CoreSurfaceData>>,
sk_tex: OnceCell<SendWrapper<Tex>>,
sk_mat: OnceCell<Arc<SendWrapper<Material>>>,
sk_tex: OnceCell<Tex>,
sk_mat: OnceCell<Arc<Material>>,
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>,
}
impl CoreSurface {
pub fn add_to(
display: &Arc<Mutex<Display<WaylandState>>>,
dh: DisplayHandle,
surface: &WlSurface,
on_mapped: impl Fn() + Send + Sync + 'static,
on_commit: impl Fn(u32) + Send + Sync + 'static,
) {
compositor::with_states(surface, |data| {
data.data_map.insert_if_missing_threadsafe(|| {
CORE_SURFACES.add(CoreSurface {
display: Arc::downgrade(display),
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(),
})
@@ -89,17 +83,21 @@ impl CoreSurface {
}
pub fn process(&self, sk: &impl StereoKitDraw, renderer: &mut GlesRenderer) {
let Some(wl_surface) = self.wl_surface() else { return };
let Some(wl_surface) = self.wl_surface() else {return};
let sk_tex = self.sk_tex.get_or_init(|| {
SendWrapper::new(sk.tex_create(TextureType::IMAGE_NO_MIPS, TextureFormat::RGBA32))
});
let sk_tex = self
.sk_tex
.get_or_init(|| sk.tex_create(TextureType::IMAGE_NO_MIPS, TextureFormat::RGBA32));
self.sk_mat.get_or_init(|| {
let shader = sk.shader_create_mem(&PANEL_SHADER_BYTES).unwrap();
let mat = sk.material_create(&shader);
let shader = sk.shader_create_mem(&PANEL_SHADER_BYTES);
// let _ = renderer.with_context(|c| unsafe {
// shader_inject(c, &mut shader, SIMULA_VERT_STR, SIMULA_FRAG_STR)
// });
let mat = sk.material_create(shader.as_ref().unwrap_or(Shader::UI.as_ref()));
sk.material_set_texture(&mat, "diffuse", sk_tex.as_ref());
sk.material_set_transparency(&mat, Transparency::Blend);
Arc::new(SendWrapper::new(mat))
Arc::new(mat)
});
// Let smithay handle buffer management (has to be done here as RendererSurfaceStates is not thread safe)
@@ -121,21 +119,18 @@ impl CoreSurface {
}
let mut mapped_data = self.mapped_data.lock();
let just_mapped = mapped_data.is_none();
self.with_states(|data| {
// let just_mapped = mapped_data.is_none();
// if just_mapped {
let renderer_surface_state = data
let Some(renderer_surface_state) = data
.data_map
.get::<RendererSurfaceStateUserData>()
.unwrap()
.borrow();
let smithay_tex = renderer_surface_state
.map(RefCell::borrow) else {return};
let Some(smithay_tex) = renderer_surface_state
.texture::<GlesRenderer>(renderer.id())
.unwrap()
.clone();
.cloned() else {return};
let sk_tex = self.sk_tex.get().unwrap();
let sk_mat = self.sk_mat.get().unwrap();
let Some(sk_tex) = self.sk_tex.get() else {return};
let Some(sk_mat) = self.sk_mat.get() else {return};
unsafe {
sk.tex_set_surface(
sk_tex.as_ref(),
@@ -154,18 +149,22 @@ impl CoreSurface {
sk.material_set_queue_offset(sk_mat.as_ref().as_ref(), *material_offset as i32);
}
let surface_size = renderer_surface_state.surface_size().unwrap();
let Some(surface_size) = renderer_surface_state.surface_size() else {return};
let new_mapped_data = CoreSurfaceData {
size: Vector2::from([surface_size.w as u32, surface_size.h as u32]),
wl_tex: Some(SendWrapper::new(smithay_tex)),
};
*mapped_data = Some(new_mapped_data);
});
drop(mapped_data);
if just_mapped {
(self.on_mapped)();
}
self.apply_surface_materials();
}
pub fn frame(&self, sk: &impl StereoKitDraw, output: Output) {
let Some(wl_surface) = self.wl_surface() else { return };
let Some(wl_surface) = self.wl_surface() else {return};
send_frames_surface_tree(
&wl_surface,
@@ -180,15 +179,17 @@ impl CoreSurface {
*self.material_offset.lock().value_mut() = material_offset;
}
pub fn apply_material(&self, model_node: &Arc<ModelPart>) {
self.pending_material_applications.add_raw(model_node)
pub fn apply_material(&self, model_part: &Arc<ModelPart>) {
self.pending_material_applications.add_raw(model_part)
}
fn apply_surface_materials(&self) {
for model_node in self.pending_material_applications.get_valid_contents() {
model_node.replace_material(self.sk_mat.clone().get().unwrap().clone());
if let Some(sk_mat) = self.sk_mat.get() {
for model_node in self.pending_material_applications.get_valid_contents() {
model_node.replace_material(sk_mat.clone());
}
self.pending_material_applications.clear();
}
self.pending_material_applications.clear();
}
pub fn wl_surface(&self) -> Option<WlSurface> {
@@ -206,15 +207,6 @@ impl CoreSurface {
pub fn size(&self) -> Option<Vector2<u32>> {
self.mapped_data.lock().as_ref().map(|d| d.size)
}
pub fn flush_clients(&self) {
self.display
.upgrade()
.unwrap()
.lock()
.flush_clients()
.unwrap();
}
}
impl Drop for CoreSurface {
fn drop(&mut self) {

File diff suppressed because it is too large Load Diff

437
src/wayland/xwayland.rs Normal file
View 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 },
},
);
}
}
}