Skip to content

Commit 40cf8c5

Browse files
committed
Generate Rust type binding wl_registry
Needed for `wl_fixes` bindings.
1 parent 2a6aea1 commit 40cf8c5

File tree

2 files changed

+213
-2
lines changed

2 files changed

+213
-2
lines changed

wayland-scanner/src/server_gen.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn generate_server_objects(protocol: &Protocol) -> TokenStream {
1212
protocol
1313
.interfaces
1414
.iter()
15-
.filter(|iface| iface.name != "wl_display" && iface.name != "wl_registry")
15+
.filter(|iface| iface.name != "wl_display")
1616
.map(generate_objects_for)
1717
.collect()
1818
}
@@ -39,7 +39,11 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
3939
&interface.events,
4040
);
4141

42-
let parse_body = crate::common::gen_parse_body(interface, Side::Server);
42+
let parse_body = if interface.name == "wl_registry" {
43+
quote! { unimplemented!("`wl_registry` is implemented internally in `wayland-server`") }
44+
} else {
45+
crate::common::gen_parse_body(interface, Side::Server)
46+
};
4347
let write_body = crate::common::gen_write_body(interface, Side::Server);
4448
let methods = gen_methods(interface);
4549

wayland-scanner/tests/scanner_assets/test-server-code.rs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,210 @@
1+
#[doc = "global registry object\n\nThe singleton global registry object. The server has a number of\nglobal objects that are available to all clients. These objects\ntypically represent an actual object in the server (for example,\nan input device) or they are singleton objects that provide\nextension functionality.\n\nWhen a client creates a registry object, the registry object\nwill emit a global event for each global currently in the\nregistry. Globals come and go as a result of device or\nmonitor hotplugs, reconfiguration or other events, and the\nregistry will send out global and global_remove events to\nkeep the client up to date with the changes. To mark the end\nof the initial burst of events, the client can use the\nwl_display.sync request immediately after calling\nwl_display.get_registry.\n\nA client can bind to a global object by using the bind\nrequest. This creates a client-side handle that lets the object\nemit events to the client and lets the client invoke requests on\nthe object."]
2+
pub mod wl_registry {
3+
use super::wayland_server::{
4+
backend::{
5+
protocol::{same_interface, Argument, Interface, Message, WEnum},
6+
smallvec, InvalidId, ObjectData, ObjectId, WeakHandle,
7+
},
8+
Dispatch, DispatchError, DisplayHandle, New, Resource, ResourceData, Weak,
9+
};
10+
use std::os::unix::io::OwnedFd;
11+
use std::sync::Arc;
12+
#[doc = r" The minimal object version supporting this request"]
13+
pub const REQ_BIND_SINCE: u32 = 1u32;
14+
#[doc = r" The wire opcode for this request"]
15+
pub const REQ_BIND_OPCODE: u16 = 0u16;
16+
#[doc = r" The minimal object version supporting this event"]
17+
pub const EVT_GLOBAL_SINCE: u32 = 1u32;
18+
#[doc = r" The wire opcode for this event"]
19+
pub const EVT_GLOBAL_OPCODE: u16 = 0u16;
20+
#[doc = r" The minimal object version supporting this event"]
21+
pub const EVT_GLOBAL_REMOVE_SINCE: u32 = 1u32;
22+
#[doc = r" The wire opcode for this event"]
23+
pub const EVT_GLOBAL_REMOVE_OPCODE: u16 = 1u16;
24+
#[derive(Debug)]
25+
#[non_exhaustive]
26+
pub enum Request {
27+
#[doc = "bind an object to the display\n\nBinds a new, client-created object to the server using the\nspecified name as the identifier."]
28+
Bind {
29+
#[doc = "unique numeric name of the object"]
30+
name: u32,
31+
#[doc = "bounded object"]
32+
id: (String, u32, super::wayland_server::ObjectId),
33+
},
34+
}
35+
impl Request {
36+
#[doc = "Get the opcode number of this message"]
37+
pub fn opcode(&self) -> u16 {
38+
match *self {
39+
Request::Bind { .. } => 0u16,
40+
}
41+
}
42+
}
43+
#[derive(Debug)]
44+
#[non_exhaustive]
45+
pub enum Event<'a> {
46+
#[doc = "announce global object\n\nNotify the client of global objects.\n\nThe event notifies the client that a global object with\nthe given name is now available, and it implements the\ngiven version of the given interface."]
47+
Global {
48+
#[doc = "numeric name of the global object"]
49+
name: u32,
50+
#[doc = "interface implemented by the object"]
51+
interface: String,
52+
#[doc = "interface version"]
53+
version: u32,
54+
},
55+
#[doc = "announce removal of global object\n\nNotify the client of removed global objects.\n\nThis event notifies the client that the global identified\nby name is no longer available. If the client bound to\nthe global using the bind request, the client should now\ndestroy that object.\n\nThe object remains valid and requests to the object will be\nignored until the client destroys it, to avoid races between\nthe global going away and a client sending a request to it."]
56+
GlobalRemove {
57+
#[doc = "numeric name of the global object"]
58+
name: u32,
59+
},
60+
#[doc(hidden)]
61+
__phantom_lifetime {
62+
phantom: std::marker::PhantomData<&'a ()>,
63+
never: std::convert::Infallible,
64+
},
65+
}
66+
impl<'a> Event<'a> {
67+
#[doc = "Get the opcode number of this message"]
68+
pub fn opcode(&self) -> u16 {
69+
match *self {
70+
Event::Global { .. } => 0u16,
71+
Event::GlobalRemove { .. } => 1u16,
72+
Event::__phantom_lifetime { never, .. } => match never {},
73+
}
74+
}
75+
}
76+
#[doc = "global registry object\n\nThe singleton global registry object. The server has a number of\nglobal objects that are available to all clients. These objects\ntypically represent an actual object in the server (for example,\nan input device) or they are singleton objects that provide\nextension functionality.\n\nWhen a client creates a registry object, the registry object\nwill emit a global event for each global currently in the\nregistry. Globals come and go as a result of device or\nmonitor hotplugs, reconfiguration or other events, and the\nregistry will send out global and global_remove events to\nkeep the client up to date with the changes. To mark the end\nof the initial burst of events, the client can use the\nwl_display.sync request immediately after calling\nwl_display.get_registry.\n\nA client can bind to a global object by using the bind\nrequest. This creates a client-side handle that lets the object\nemit events to the client and lets the client invoke requests on\nthe object.\n\nSee also the [Request] enum for this interface."]
77+
#[derive(Debug, Clone)]
78+
pub struct WlRegistry {
79+
id: ObjectId,
80+
version: u32,
81+
data: Option<Arc<dyn std::any::Any + Send + Sync + 'static>>,
82+
handle: WeakHandle,
83+
}
84+
impl std::cmp::PartialEq for WlRegistry {
85+
#[inline]
86+
fn eq(&self, other: &WlRegistry) -> bool {
87+
self.id == other.id
88+
}
89+
}
90+
impl std::cmp::Eq for WlRegistry {}
91+
impl PartialEq<Weak<WlRegistry>> for WlRegistry {
92+
#[inline]
93+
fn eq(&self, other: &Weak<WlRegistry>) -> bool {
94+
self.id == other.id()
95+
}
96+
}
97+
impl std::borrow::Borrow<ObjectId> for WlRegistry {
98+
#[inline]
99+
fn borrow(&self) -> &ObjectId {
100+
&self.id
101+
}
102+
}
103+
impl std::hash::Hash for WlRegistry {
104+
#[inline]
105+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
106+
self.id.hash(state)
107+
}
108+
}
109+
impl super::wayland_server::Resource for WlRegistry {
110+
type Request = Request;
111+
type Event<'event> = Event<'event>;
112+
#[inline]
113+
fn interface() -> &'static Interface {
114+
&super::WL_REGISTRY_INTERFACE
115+
}
116+
#[inline]
117+
fn id(&self) -> ObjectId {
118+
self.id.clone()
119+
}
120+
#[inline]
121+
fn version(&self) -> u32 {
122+
self.version
123+
}
124+
#[inline]
125+
fn data<U: 'static>(&self) -> Option<&U> {
126+
self.data
127+
.as_ref()
128+
.and_then(|arc| (&**arc).downcast_ref::<ResourceData<Self, U>>())
129+
.map(|data| &data.udata)
130+
}
131+
#[inline]
132+
fn object_data(&self) -> Option<&Arc<dyn std::any::Any + Send + Sync>> {
133+
self.data.as_ref()
134+
}
135+
fn handle(&self) -> &WeakHandle {
136+
&self.handle
137+
}
138+
#[inline]
139+
fn from_id(conn: &DisplayHandle, id: ObjectId) -> Result<Self, InvalidId> {
140+
if !same_interface(id.interface(), Self::interface()) && !id.is_null() {
141+
return Err(InvalidId);
142+
}
143+
let version = conn.object_info(id.clone()).map(|info| info.version).unwrap_or(0);
144+
let data = conn.get_object_data(id.clone()).ok();
145+
Ok(WlRegistry { id, data, version, handle: conn.backend_handle().downgrade() })
146+
}
147+
fn send_event(&self, evt: Self::Event<'_>) -> Result<(), InvalidId> {
148+
let handle = DisplayHandle::from(self.handle.upgrade().ok_or(InvalidId)?);
149+
handle.send_event(self, evt)
150+
}
151+
fn parse_request(
152+
conn: &DisplayHandle,
153+
msg: Message<ObjectId, OwnedFd>,
154+
) -> Result<(Self, Self::Request), DispatchError> {
155+
unimplemented!("`wl_registry` is implemented internally in `wayland-server`")
156+
}
157+
fn write_event<'a>(
158+
&self,
159+
conn: &DisplayHandle,
160+
msg: Self::Event<'a>,
161+
) -> Result<Message<ObjectId, std::os::unix::io::BorrowedFd<'a>>, InvalidId> {
162+
match msg {
163+
Event::Global { name, interface, version } => Ok(Message {
164+
sender_id: self.id.clone(),
165+
opcode: 0u16,
166+
args: {
167+
let mut vec = smallvec::SmallVec::new();
168+
vec.push(Argument::Uint(name));
169+
vec.push(Argument::Str(Some(Box::new(
170+
std::ffi::CString::new(interface).unwrap(),
171+
))));
172+
vec.push(Argument::Uint(version));
173+
vec
174+
},
175+
}),
176+
Event::GlobalRemove { name } => Ok(Message {
177+
sender_id: self.id.clone(),
178+
opcode: 1u16,
179+
args: {
180+
let mut vec = smallvec::SmallVec::new();
181+
vec.push(Argument::Uint(name));
182+
vec
183+
},
184+
}),
185+
Event::__phantom_lifetime { never, .. } => match never {},
186+
}
187+
}
188+
fn __set_object_data(
189+
&mut self,
190+
odata: std::sync::Arc<dyn std::any::Any + Send + Sync + 'static>,
191+
) {
192+
self.data = Some(odata);
193+
}
194+
}
195+
impl WlRegistry {
196+
#[doc = "announce global object\n\nNotify the client of global objects.\n\nThe event notifies the client that a global object with\nthe given name is now available, and it implements the\ngiven version of the given interface."]
197+
#[allow(clippy::too_many_arguments)]
198+
pub fn global(&self, name: u32, interface: String, version: u32) {
199+
let _ = self.send_event(Event::Global { name, interface, version });
200+
}
201+
#[doc = "announce removal of global object\n\nNotify the client of removed global objects.\n\nThis event notifies the client that the global identified\nby name is no longer available. If the client bound to\nthe global using the bind request, the client should now\ndestroy that object.\n\nThe object remains valid and requests to the object will be\nignored until the client destroys it, to avoid races between\nthe global going away and a client sending a request to it."]
202+
#[allow(clippy::too_many_arguments)]
203+
pub fn global_remove(&self, name: u32) {
204+
let _ = self.send_event(Event::GlobalRemove { name });
205+
}
206+
}
207+
}
1208
#[doc = "callback object\n\nClients can handle the 'done' event to get notified when\nthe related request is done."]
2209
pub mod wl_callback {
3210
use super::wayland_server::{

0 commit comments

Comments
 (0)