1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#[cfg(feature = "native_lib")]
use wayland_sys::server::wl_client;

use imp::ClientInner;

use {Interface, NewResource, UserDataMap};

/// A handle to a client connected to your server
///
/// There can be several handles referring to the same client
#[derive(Clone)]
pub struct Client {
    inner: ClientInner,
}

impl Client {
    #[cfg(feature = "native_lib")]
    /// Create a client from a `wayland-server.so` pointer
    pub unsafe fn from_ptr(ptr: *mut wl_client) -> Client {
        Client {
            inner: ClientInner::from_ptr(ptr),
        }
    }

    #[cfg(feature = "native_lib")]
    /// Retrieve a pointer to the underlying `wl_client` of `wayland-server.so`
    pub fn c_ptr(&self) -> *mut wl_client {
        self.inner.ptr()
    }

    pub(crate) fn make(inner: ClientInner) -> Client {
        Client { inner }
    }

    /// Check whether this client is still connected to the server
    pub fn alive(&self) -> bool {
        self.inner.alive()
    }

    /// Check whether this client handle refers to the same client as
    /// an other
    pub fn equals(&self, other: &Client) -> bool {
        self.inner.equals(&other.inner)
    }

    /// Flush the pending events to this client
    pub fn flush(&self) {
        self.inner.flush()
    }

    /// Kills this client
    ///
    /// Does nothing if the client is already dead
    pub fn kill(&self) {
        self.inner.kill()
    }

    /// Access the map handling user data associated to this client
    ///
    /// See `UserDataMap` documentation for details about its use.
    pub fn data_map(&self) -> &UserDataMap {
        self.inner.user_data_map()
    }

    /// Add a destructor for this client
    ///
    /// This closure will be called when the client disconnects or is killed,
    /// It has access to the user data map associated to this client.
    ///
    /// You can add several destructors which will all be called sequentially. Note
    /// that if you accidentally add two copies of the same closure, it'll be called
    /// twice.
    ///
    /// The destructors will be executed on the thread containing the wayland event loop.
    pub fn add_destructor<F: FnOnce(&UserDataMap) + Send + 'static>(&self, destructor: F) {
        self.inner.add_destructor(destructor)
    }

    /// Create a new resource for this client
    ///
    /// To ensure the state coherence between client and server, this
    /// resource should immediately be implemented and sent to the client
    /// through and appropriate event. Failure to do so will likely cause
    /// protocol errors.
    pub fn create_resource<I: Interface>(&self, version: u32) -> Option<NewResource<I>> {
        self.inner.create_resource::<I>(version).map(NewResource::wrap)
    }
}