wayland_bridge/
dispatcher.rs1use crate::alpha_compositing::*;
6use crate::aura_shell::*;
7use crate::compositor::*;
8use crate::data_device_manager::*;
9use crate::display::*;
10use crate::linux_dmabuf::*;
11use crate::object::*;
12use crate::output::*;
13use crate::pointer_constraints::*;
14use crate::registry::*;
15use crate::relative_pointer::*;
16use crate::seat::*;
17use crate::secure_output::*;
18use crate::shm::*;
19use crate::subcompositor::*;
20use crate::viewporter::*;
21use crate::xdg_shell::*;
22use anyhow::{Error, Result};
23use fuchsia_sync::Mutex;
24
25use std::io::Read;
26use std::sync::Arc;
27use wayland_server_protocol::{
28 WlCompositor, WlDataDeviceManager, WlOutput, WlSeat, WlShm, WlSubcompositor,
29};
30use wp_viewporter_server_protocol::WpViewporter;
31use xdg_shell_server_protocol::XdgWmBase;
32use zaura_shell_server_protocol::ZauraShell;
33use zcr_alpha_compositing_v1_server_protocol::ZcrAlphaCompositingV1;
34use zcr_secure_output_v1_server_protocol::ZcrSecureOutputV1;
35use zwp_linux_dmabuf_v1_server_protocol::ZwpLinuxDmabufV1;
36use zwp_pointer_constraints_v1_server_protocol::ZwpPointerConstraintsV1;
37use zwp_relative_pointer_v1_server_protocol::ZwpRelativePointerManagerV1;
38
39fn read_file_into_vmo(filename: &str) -> Result<zx::Vmo> {
41 let mut file = std::fs::File::open(filename)?;
42 let keymap_len = file.metadata()?.len();
43
44 let mut buffer = Vec::new();
45 file.read_to_end(&mut buffer)?;
46 let vmo = zx::Vmo::create(keymap_len)?;
47 vmo.write(&buffer, 0)?;
48 Ok(vmo)
49}
50
51pub struct WaylandDispatcher {
54 pub display: Display,
57}
58
59impl WaylandDispatcher {
60 pub fn new_local(client: Arc<Mutex<Box<dyn LocalViewProducerClient>>>) -> Result<Self, Error> {
61 let registry = WaylandDispatcher::new_registry()?;
62 let display = Display::new_local(registry, client)?;
63 Ok(WaylandDispatcher { display })
64 }
65
66 pub fn new() -> Result<Self, Error> {
67 let registry = WaylandDispatcher::new_registry()?;
68 let display = Display::new(registry)?;
69 Ok(WaylandDispatcher { display })
70 }
71
72 fn new_registry() -> Result<Registry, Error> {
73 let mut registry = RegistryBuilder::new();
74 registry.add_global(WlCompositor, move |_, _, _| {
75 Ok(Box::new(RequestDispatcher::new(Compositor::new())))
76 });
77 registry.add_global(WlSubcompositor, move |_, _, _| {
78 Ok(Box::new(RequestDispatcher::new(Subcompositor::new())))
79 });
80 registry.add_global(WlOutput, move |id, _, client| {
81 let output = Output::new();
82 let display_info = client.display().display_info();
83 Output::post_output_info(id, client, &display_info)?;
85 Output::post_output_done(id, client)?;
86 Ok(Box::new(RequestDispatcher::new(output)))
87 });
88 {
89 registry.add_global(WlSeat, move |id, version, client| {
90 let vmo = read_file_into_vmo("/pkg/data/keymap.xkb")?;
91 let seat = Seat::new(version, vmo);
92 seat.post_seat_info(id, version, client)?;
93 Ok(Box::new(RequestDispatcher::new(seat)))
94 });
95 }
96 registry.add_global(WlShm, move |id, _, client| {
97 let shm = Shm::new();
98 shm.post_formats(id, client)?;
100 Ok(Box::new(RequestDispatcher::new(shm)))
101 });
102 registry.add_global(WlDataDeviceManager, move |_, _, _| {
103 Ok(Box::new(RequestDispatcher::new(DataDeviceManager::new())))
104 });
105 registry.add_global(XdgWmBase, move |_, _, _| {
106 let xdg_shell = XdgShell::new();
107 Ok(Box::new(RequestDispatcher::new(xdg_shell)))
108 });
109 registry.add_global(ZwpLinuxDmabufV1, move |id, version, client| {
110 let linux_dmabuf = LinuxDmabuf::new(version);
111 linux_dmabuf.post_formats(id, client)?;
113 Ok(Box::new(RequestDispatcher::new(linux_dmabuf)))
114 });
115 registry.add_global(ZcrAlphaCompositingV1, move |_, _, _| {
116 Ok(Box::new(RequestDispatcher::new(AlphaCompositing::new())))
117 });
118 registry.add_global(ZcrSecureOutputV1, move |_, _, _| {
119 Ok(Box::new(RequestDispatcher::new(SecureOutput::new())))
120 });
121 registry.add_global(WpViewporter, move |_, _, _| {
122 Ok(Box::new(RequestDispatcher::new(Viewporter::new())))
123 });
124 registry.add_global(ZauraShell, move |_, _, _| {
125 Ok(Box::new(RequestDispatcher::new(AuraShell::new())))
126 });
127 registry.add_global(ZwpRelativePointerManagerV1, move |_, _, _| {
128 Ok(Box::new(RequestDispatcher::new(RelativePointerManager)))
129 });
130 registry.add_global(ZwpPointerConstraintsV1, move |_, _, _| {
131 Ok(Box::new(RequestDispatcher::new(PointerConstraints)))
132 });
133
134 Ok(registry.build())
135 }
136}