1use std::sync::Arc;
2
3use wayland_backend::{
4 protocol::ProtocolError,
5 server::{ClientData, ClientId, DisconnectReason, InvalidId, ObjectData},
6};
7
8use crate::{dispatch::ResourceData, Dispatch, DisplayHandle, Resource};
9
10#[derive(Clone, Debug)]
12pub struct Client {
13 pub(crate) id: ClientId,
14 pub(crate) data: Arc<dyn ClientData>,
15}
16
17impl Client {
18 pub(crate) fn from_id(handle: &DisplayHandle, id: ClientId) -> Result<Self, InvalidId> {
19 let data = handle.handle.get_client_data(id.clone())?;
20 Ok(Self { id, data })
21 }
22
23 pub fn id(&self) -> ClientId {
25 self.id.clone()
26 }
27
28 pub fn get_data<Data: ClientData + 'static>(&self) -> Option<&Data> {
32 (*self.data).downcast_ref()
33 }
34
35 pub fn get_credentials(
43 &self,
44 handle: &DisplayHandle,
45 ) -> Result<crate::backend::Credentials, InvalidId> {
46 handle.handle.get_client_credentials(self.id.clone())
47 }
48
49 pub fn create_resource<
55 I: Resource + 'static,
56 U: Send + Sync + 'static,
57 D: Dispatch<I, U> + 'static,
58 >(
59 &self,
60 handle: &DisplayHandle,
61 version: u32,
62 user_data: U,
63 ) -> Result<I, InvalidId> {
64 let id = handle.handle.create_object::<D>(
65 self.id.clone(),
66 I::interface(),
67 version,
68 Arc::new(ResourceData::<I, U>::new(user_data)) as Arc<_>,
69 )?;
70 I::from_id(handle, id)
71 }
72
73 pub fn create_resource_from_objdata<I: Resource + 'static, D: 'static>(
82 &self,
83 handle: &DisplayHandle,
84 version: u32,
85 obj_data: Arc<dyn ObjectData<D>>,
86 ) -> Result<I, InvalidId> {
87 let id =
88 handle.handle.create_object::<D>(self.id.clone(), I::interface(), version, obj_data)?;
89 I::from_id(handle, id)
90 }
91
92 pub fn object_from_protocol_id<I: Resource + 'static>(
97 &self,
98 handle: &DisplayHandle,
99 protocol_id: u32,
100 ) -> Result<I, InvalidId> {
101 let object_id =
102 handle.handle.object_for_protocol_id(self.id.clone(), I::interface(), protocol_id)?;
103 I::from_id(handle, object_id)
104 }
105
106 pub fn kill(&self, handle: &DisplayHandle, error: ProtocolError) {
108 handle.handle.kill_client(self.id.clone(), DisconnectReason::ProtocolError(error))
109 }
110
111 #[cfg(feature = "libwayland_1_22")]
113 pub fn global_name(
114 &self,
115 handle: &DisplayHandle,
116 global: crate::backend::GlobalId,
117 ) -> Option<u32> {
118 handle.handle.global_name(global, self.id.clone())
119 }
120
121 #[cfg(feature = "libwayland_1_23")]
123 pub fn set_max_buffer_size(&self, handle: &DisplayHandle, max_buffer_size: usize) {
124 handle.handle.set_client_max_buffer_size(self.id.clone(), max_buffer_size);
125 }
126}
127
128impl PartialEq for Client {
129 fn eq(&self, other: &Self) -> bool {
130 self.id == other.id
131 }
132}