Trait wayland_backend::sys::server::ClientData
source · pub trait ClientData: DowncastSync {
fn initialized(&self, _client_id: ClientId) { ... }
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) { ... }
fn debug(&self, f: &mut Formatter<'_>) -> Result { ... }
}
client_system
or server_system
) and crate feature server_system
only.Expand description
A trait representing your data associated to a client
Provided Methods§
sourcefn initialized(&self, _client_id: ClientId)
fn initialized(&self, _client_id: ClientId)
Notification that the client was initialized
sourcefn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason)
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason)
Notification that the client is disconnected
Examples found in repository?
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
fn kill_client(&mut self, client_id: InnerClientId, reason: DisconnectReason) {
if !client_id.alive.load(Ordering::Acquire) {
return;
}
if let Some(udata) = unsafe { client_user_data(client_id.ptr) } {
let udata = unsafe { &*udata };
udata.alive.store(false, Ordering::Release);
udata.data.disconnected(ClientId { id: client_id.clone() }, reason);
}
// wl_client_destroy invokes destructors
PENDING_DESTRUCTORS.set(&(&mut self.pending_destructors as *mut _ as *mut _), || unsafe {
ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_client_destroy, client_id.ptr);
});
}
fn global_info(&self, id: InnerGlobalId) -> Result<GlobalInfo, InvalidId> {
if !id.alive.load(Ordering::Acquire) {
return Err(InvalidId);
}
let udata = unsafe {
&*(ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_global_get_user_data, id.ptr)
as *mut GlobalUserData<D>)
};
Ok(GlobalInfo {
interface: udata.interface,
version: udata.version,
disabled: udata.disabled,
})
}
fn is_known_global(&self, global_ptr: *const wl_global) -> bool {
self.known_globals.iter().any(|ginfo| (ginfo.ptr as *const wl_global) == global_ptr)
}
fn display_ptr(&self) -> *mut wl_display {
self.display
}
}
unsafe fn init_client<D: 'static>(
client: *mut wl_client,
data: Arc<dyn ClientData>,
) -> InnerClientId {
let alive = Arc::new(AtomicBool::new(true));
let client_data = Box::into_raw(Box::new(ClientUserData { alive: alive.clone(), data }));
let listener = signal::rust_listener_create(client_destroy_notify);
// Safety: we just created listener and client_data, they are valid
unsafe {
signal::rust_listener_set_user_data(listener, client_data as *mut c_void);
}
ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_client_add_destroy_listener, client, listener);
InnerClientId { ptr: client, alive }
}
unsafe fn client_id_from_ptr(client: *mut wl_client) -> Option<InnerClientId> {
// Safety: the provided pointer is a valid and initialized wl_client for type parameter D
unsafe {
client_user_data(client)
.map(|udata| InnerClientId { ptr: client, alive: (*udata).alive.clone() })
}
}
unsafe fn client_user_data(client: *mut wl_client) -> Option<*mut ClientUserData> {
if client.is_null() {
return None;
}
let listener = ffi_dispatch!(
WAYLAND_SERVER_HANDLE,
wl_client_get_destroy_listener,
client,
client_destroy_notify
);
if !listener.is_null() {
// Safety: the pointer we got must be valid if the client is still alive
unsafe { Some(signal::rust_listener_get_user_data(listener) as *mut ClientUserData) }
} else {
None
}
}
unsafe extern "C" fn client_destroy_notify(listener: *mut wl_listener, client_ptr: *mut c_void) {
// Safety: if this function is invoked by libwayland its arguments must be valid
let data = unsafe {
Box::from_raw(signal::rust_listener_get_user_data(listener) as *mut ClientUserData)
};
unsafe {
signal::rust_listener_destroy(listener);
}
// only notify the killing if it was not already
if data.alive.load(Ordering::Acquire) {
data.alive.store(false, Ordering::Release);
data.data.disconnected(
ClientId {
id: InnerClientId { ptr: client_ptr as *mut wl_client, alive: data.alive.clone() },
},
DisconnectReason::ConnectionClosed,
);
}
}
Implementations§
source§impl dyn ClientData
impl dyn ClientData
sourcepub fn is<__T: ClientData>(&self) -> bool
pub fn is<__T: ClientData>(&self) -> bool
Returns true if the trait object wraps an object of type __T
.
sourcepub fn downcast<__T: ClientData>(self: Box<Self>) -> Result<Box<__T>, Box<Self>>
pub fn downcast<__T: ClientData>(self: Box<Self>) -> Result<Box<__T>, Box<Self>>
Returns a boxed object from a boxed trait object if the underlying object is of type
__T
. Returns the original boxed trait if it isn’t.
sourcepub fn downcast_rc<__T: ClientData>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>
pub fn downcast_rc<__T: ClientData>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>
Returns an Rc
-ed object from an Rc
-ed trait object if the underlying object is of
type __T
. Returns the original Rc
-ed trait if it isn’t.
sourcepub fn downcast_ref<__T: ClientData>(&self) -> Option<&__T>
pub fn downcast_ref<__T: ClientData>(&self) -> Option<&__T>
Returns a reference to the object within the trait object if it is of type __T
, or
None
if it isn’t.
sourcepub fn downcast_mut<__T: ClientData>(&mut self) -> Option<&mut __T>
pub fn downcast_mut<__T: ClientData>(&mut self) -> Option<&mut __T>
Returns a mutable reference to the object within the trait object if it is of type
__T
, or None
if it isn’t.
sourcepub fn downcast_arc<__T>(self: Arc<Self>) -> Result<Arc<__T>, Arc<Self>>where
__T: Any + Send + Sync + ClientData,
pub fn downcast_arc<__T>(self: Arc<Self>) -> Result<Arc<__T>, Arc<Self>>where
__T: Any + Send + Sync + ClientData,
Returns an Arc
-ed object from an Arc
-ed trait object if the underlying object is of
type __T
. Returns the original Arc
-ed trait if it isn’t.