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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::borrow::Borrow;
use std::cmp;
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::num::NonZeroU32;

use crate::connection::GenericCallback;
use crate::interface::Interface;
use crate::protocol::WlDisplay;

/// A Wayland object.
///
/// The [`Debug`] representation is `<interface>@<id>v<version>`.
///
/// [`Eq`], [`Ord`] and [`Hash`] implementations are delegated to the object's ID for performance
/// reasons. This is fine because two different objects with the same ID must not exist at the same
/// time.
#[derive(Clone, Copy)]
pub struct Object {
    pub id: ObjectId,
    pub interface: &'static Interface,
    pub version: u32,
}

impl PartialEq for Object {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Object {}

impl PartialEq<ObjectId> for Object {
    #[inline]
    fn eq(&self, other: &ObjectId) -> bool {
        self.id == *other
    }
}

impl PartialEq<Object> for ObjectId {
    #[inline]
    fn eq(&self, other: &Object) -> bool {
        *self == other.id
    }
}

impl PartialOrd for Object {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Object {
    #[inline]
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.id.cmp(&other.id)
    }
}

impl Hash for Object {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl Borrow<ObjectId> for Object {
    fn borrow(&self) -> &ObjectId {
        &self.id
    }
}

impl Debug for Object {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}@{}v{}",
            self.interface.name.to_string_lossy(),
            self.id.0,
            self.version
        )
    }
}

/// A Wayland object ID.
///
/// Uniquely identifies an object at each point of time. Note that an ID may have a limited
/// lifetime. Also an ID which once pointed to a certain object, may point to a different object in
/// the future, due to ID reuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObjectId(pub(crate) NonZeroU32);

impl ObjectId {
    pub(crate) const DISPLAY: Self = Self(unsafe { NonZeroU32::new_unchecked(1) });
    pub(crate) const MAX_CLIENT: Self = Self(unsafe { NonZeroU32::new_unchecked(0xFEFFFFFF) });
    pub(crate) const MIN_SERVER: Self = Self(unsafe { NonZeroU32::new_unchecked(0xFF000000) });

    /// Returns the numeric representation of the ID
    pub fn as_u32(self) -> u32 {
        self.0.get()
    }

    /// Whether the object with this ID was created by the server
    pub fn created_by_server(self) -> bool {
        self >= Self::MIN_SERVER
    }

    /// Whether the object with this ID was created by the client
    pub fn created_by_client(self) -> bool {
        self <= Self::MAX_CLIENT
    }
}

pub(crate) struct ObjectManager<D> {
    vacant_ids: Vec<ObjectId>,
    client_objects: Vec<Option<ObjectState<D>>>,
    server_objects: Vec<Option<ObjectState<D>>>,
}

pub(crate) struct ObjectState<D> {
    pub object: Object,
    pub is_alive: bool,
    pub cb: Option<GenericCallback<D>>,
}

impl<D> ObjectManager<D> {
    pub fn new() -> Self {
        let mut this = Self {
            vacant_ids: Vec::new(),
            client_objects: Vec::with_capacity(16),
            server_objects: Vec::new(),
        };

        // Dummy NULL object
        this.client_objects.push(None);

        // Display
        this.client_objects.push(Some(ObjectState {
            object: WlDisplay::INSTANCE.into(),
            is_alive: true,
            cb: None,
        }));

        this
    }

    pub fn clear_callbacks<D2>(self) -> ObjectManager<D2> {
        let map = |x: ObjectState<D>| ObjectState {
            object: x.object,
            is_alive: x.is_alive,
            cb: None,
        };
        ObjectManager {
            vacant_ids: self.vacant_ids,
            client_objects: self
                .client_objects
                .into_iter()
                .map(|x| x.map(map))
                .collect(),
            server_objects: self
                .server_objects
                .into_iter()
                .map(|x| x.map(map))
                .collect(),
        }
    }

    pub fn alloc_client_object(
        &mut self,
        interface: &'static Interface,
        version: u32,
    ) -> &mut ObjectState<D> {
        let id = self.vacant_ids.pop().unwrap_or_else(|| {
            let id = self.client_objects.len() as u32;
            self.client_objects.push(None);
            ObjectId(NonZeroU32::new(id).unwrap())
        });

        assert!(id.created_by_client());
        let obj = self.client_objects.get_mut(id.0.get() as usize).unwrap();
        assert!(obj.is_none());

        obj.insert(ObjectState {
            object: Object {
                id,
                interface,
                version,
            },
            is_alive: true,
            cb: None,
        })
    }

    pub fn register_server_object(&mut self, object: Object) -> &mut ObjectState<D> {
        assert!(object.id.created_by_server());

        let index = (object.id.as_u32() - ObjectId::MIN_SERVER.as_u32()) as usize;

        while index >= self.server_objects.len() {
            self.server_objects.push(None);
        }

        self.server_objects[index].insert(ObjectState {
            object,
            is_alive: true,
            cb: None,
        })
    }

    pub fn get_object_mut(&mut self, id: ObjectId) -> Option<&mut ObjectState<D>> {
        if id.created_by_client() {
            self.client_objects
                .get_mut(id.as_u32() as usize)
                .and_then(Option::as_mut)
        } else {
            self.server_objects
                .get_mut((id.as_u32() - ObjectId::MIN_SERVER.as_u32()) as usize)
                .and_then(Option::as_mut)
        }
    }

    /// Call it only on client-created objects in response to `wl_display.delete_id`.
    pub fn delete_client_object(&mut self, id: ObjectId) {
        assert!(id.created_by_client());
        *self.client_objects.get_mut(id.as_u32() as usize).unwrap() = None;
        self.vacant_ids.push(id);
    }
}