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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::mem;
use std::os::unix::io::OwnedFd;
use std::sync::MutexGuard;
use std::sync::{
    atomic::{AtomicI32, Ordering},
    Arc, Mutex,
};

use wayland_client::{
    globals::{BindError, GlobalList},
    protocol::{
        wl_callback, wl_compositor, wl_output, wl_region,
        wl_surface::{self, WlSurface},
    },
    Connection, Dispatch, Proxy, QueueHandle, WEnum,
};

use crate::{
    error::GlobalError,
    globals::{GlobalData, ProvidesBoundGlobal},
    output::{OutputData, OutputHandler, OutputState, ScaleWatcherHandle},
};

pub trait CompositorHandler: Sized {
    /// The surface has either been moved into or out of an output and the output has a different scale factor.
    fn scale_factor_changed(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        surface: &wl_surface::WlSurface,
        new_factor: i32,
    );

    /// The surface has either been moved into or out of an output and the output has different transform.
    fn transform_changed(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        surface: &wl_surface::WlSurface,
        new_transform: wl_output::Transform,
    );

    /// A frame callback has been completed.
    ///
    /// Frame callbacks are used to avoid updating surfaces that are not currently visible.  If a
    /// frame callback is requested prior to committing a surface, the client should avoid drawing
    /// to that surface until the callback completes.  See the
    /// [`WlSurface::frame`](wl_surface::WlSurface::frame) request for more details.
    ///
    /// This function will be called if you request a frame callback by passing the surface itself
    /// as the userdata (`surface.frame(&queue, &surface)`); you can also implement [`Dispatch`]
    /// for other values to more easily dispatch rendering for specific surface types.
    fn frame(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        surface: &wl_surface::WlSurface,
        time: u32,
    );
}

pub trait SurfaceDataExt: Send + Sync {
    fn surface_data(&self) -> &SurfaceData;
}

impl SurfaceDataExt for SurfaceData {
    fn surface_data(&self) -> &SurfaceData {
        self
    }
}

#[derive(Debug)]
pub struct CompositorState {
    wl_compositor: wl_compositor::WlCompositor,
}

impl CompositorState {
    /// The maximum API version for WlCompositor that this object will bind.
    // Note: if bumping this version number, check if the changes to the wayland XML cause an API
    // break in the rust interfaces.  If it does, be sure to remove other ProvidesBoundGlobal
    // impls; if it does not, consider adding one for the previous (compatible) version.
    pub const API_VERSION_MAX: u32 = 6;

    pub fn bind<State>(
        globals: &GlobalList,
        qh: &QueueHandle<State>,
    ) -> Result<CompositorState, BindError>
    where
        State: Dispatch<wl_compositor::WlCompositor, GlobalData, State> + 'static,
    {
        let wl_compositor = globals.bind(qh, 1..=Self::API_VERSION_MAX, GlobalData)?;
        Ok(CompositorState { wl_compositor })
    }

    pub fn wl_compositor(&self) -> &wl_compositor::WlCompositor {
        &self.wl_compositor
    }

    pub fn create_surface<D>(&self, qh: &QueueHandle<D>) -> wl_surface::WlSurface
    where
        D: Dispatch<wl_surface::WlSurface, SurfaceData> + 'static,
    {
        self.create_surface_with_data(qh, Default::default())
    }

    pub fn create_surface_with_data<D, U>(
        &self,
        qh: &QueueHandle<D>,
        data: U,
    ) -> wl_surface::WlSurface
    where
        D: Dispatch<wl_surface::WlSurface, U> + 'static,
        U: SurfaceDataExt + 'static,
    {
        self.wl_compositor.create_surface(qh, data)
    }
}

/// Data associated with a [`WlSurface`](wl_surface::WlSurface).
#[derive(Debug)]
pub struct SurfaceData {
    /// The scale factor of the output with the highest scale factor.
    pub(crate) scale_factor: AtomicI32,

    /// Parent surface used when creating subsurfaces.
    ///
    /// For top-level surfaces this is always `None`.
    pub(crate) parent_surface: Option<WlSurface>,

    /// The inner mutable storage.
    inner: Mutex<SurfaceDataInner>,
}

impl SurfaceData {
    /// Create a new surface that initially reports the given scale factor and parent.
    pub fn new(parent_surface: Option<WlSurface>, scale_factor: i32) -> Self {
        Self {
            scale_factor: AtomicI32::new(scale_factor),
            parent_surface,
            inner: Default::default(),
        }
    }

    /// The scale factor of the output with the highest scale factor.
    pub fn scale_factor(&self) -> i32 {
        self.scale_factor.load(Ordering::Relaxed)
    }

    /// The suggest transform for the surface.
    pub fn transform(&self) -> wl_output::Transform {
        self.inner.lock().unwrap().transform
    }

    /// The parent surface used for this surface.
    ///
    /// The surface is `Some` for primarily for subsurfaces,
    /// since they must have a parent surface.
    pub fn parent_surface(&self) -> Option<&WlSurface> {
        self.parent_surface.as_ref()
    }

    /// The outputs the surface is currently inside.
    pub fn outputs(&self) -> impl Iterator<Item = wl_output::WlOutput> {
        self.inner.lock().unwrap().outputs.clone().into_iter()
    }
}

impl Default for SurfaceData {
    fn default() -> Self {
        Self::new(None, 1)
    }
}

#[derive(Debug)]
struct SurfaceDataInner {
    /// The transform of the given surface.
    transform: wl_output::Transform,

    /// The outputs the surface is currently inside.
    outputs: Vec<wl_output::WlOutput>,

    /// A handle to the OutputInfo callback that dispatches scale updates.
    watcher: Option<ScaleWatcherHandle>,
}

impl Default for SurfaceDataInner {
    fn default() -> Self {
        Self { transform: wl_output::Transform::Normal, outputs: Vec::new(), watcher: None }
    }
}

/// An owned [`WlSurface`](wl_surface::WlSurface).
///
/// This destroys the surface on drop.
#[derive(Debug)]
pub struct Surface(wl_surface::WlSurface);

impl Surface {
    pub fn new<D>(
        compositor: &impl ProvidesBoundGlobal<
            wl_compositor::WlCompositor,
            { CompositorState::API_VERSION_MAX },
        >,
        qh: &QueueHandle<D>,
    ) -> Result<Self, GlobalError>
    where
        D: Dispatch<wl_surface::WlSurface, SurfaceData> + 'static,
    {
        Self::with_data(compositor, qh, Default::default())
    }

    pub fn with_data<D, U>(
        compositor: &impl ProvidesBoundGlobal<
            wl_compositor::WlCompositor,
            { CompositorState::API_VERSION_MAX },
        >,
        qh: &QueueHandle<D>,
        data: U,
    ) -> Result<Self, GlobalError>
    where
        D: Dispatch<wl_surface::WlSurface, U> + 'static,
        U: Send + Sync + 'static,
    {
        Ok(Surface(compositor.bound_global()?.create_surface(qh, data)))
    }

    pub fn wl_surface(&self) -> &wl_surface::WlSurface {
        &self.0
    }
}

impl From<wl_surface::WlSurface> for Surface {
    fn from(surface: wl_surface::WlSurface) -> Self {
        Surface(surface)
    }
}

impl Drop for Surface {
    fn drop(&mut self) {
        self.0.destroy();
    }
}

#[macro_export]
macro_rules! delegate_compositor {
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
        $crate::delegate_compositor!(@{ $(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty }; surface: []);
        $crate::delegate_compositor!(@{ $(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty }; surface-only: $crate::compositor::SurfaceData);
    };
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty, surface: [$($surface: ty),*$(,)?]) => {
        $crate::delegate_compositor!(@{ $(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty }; surface: [ $($surface),* ]);
    };
    (@{$($ty:tt)*}; surface: []) => {
        $crate::reexports::client::delegate_dispatch!($($ty)*:
            [
                $crate::reexports::client::protocol::wl_compositor::WlCompositor: $crate::globals::GlobalData
            ] => $crate::compositor::CompositorState
        );
        $crate::reexports::client::delegate_dispatch!($($ty)*:
            [
                $crate::reexports::client::protocol::wl_callback::WlCallback: $crate::reexports::client::protocol::wl_surface::WlSurface
            ] => $crate::compositor::CompositorState
        );
    };
    (@{$($ty:tt)*}; surface-only: $surface:ty) => {
        $crate::reexports::client::delegate_dispatch!($($ty)*:
            [
                    $crate::reexports::client::protocol::wl_surface::WlSurface: $surface
            ] => $crate::compositor::CompositorState
        );
    };
    (@$ty:tt; surface: [ $($surface:ty),+ ]) => {
        $crate::delegate_compositor!(@$ty; surface: []);
        $(
            $crate::delegate_compositor!(@$ty; surface-only: $surface);
        )*
    };
}

impl<D, U> Dispatch<wl_surface::WlSurface, U, D> for CompositorState
where
    D: Dispatch<wl_surface::WlSurface, U> + CompositorHandler + OutputHandler + 'static,
    U: SurfaceDataExt + 'static,
{
    fn event(
        state: &mut D,
        surface: &wl_surface::WlSurface,
        event: wl_surface::Event,
        data: &U,
        conn: &Connection,
        qh: &QueueHandle<D>,
    ) {
        let data = data.surface_data();
        let mut inner = data.inner.lock().unwrap();

        match event {
            wl_surface::Event::Enter { output } => {
                inner.outputs.push(output);
            }
            wl_surface::Event::Leave { output } => {
                inner.outputs.retain(|o| o != &output);
            }
            wl_surface::Event::PreferredBufferScale { factor } => {
                let current_scale = data.scale_factor.load(Ordering::Relaxed);
                drop(inner);
                data.scale_factor.store(factor, Ordering::Relaxed);
                if current_scale != factor {
                    state.scale_factor_changed(conn, qh, surface, factor);
                }
                return;
            }
            wl_surface::Event::PreferredBufferTransform { transform } => {
                // Only handle known values.
                if let WEnum::Value(transform) = transform {
                    let old_transform = std::mem::replace(&mut inner.transform, transform);
                    drop(inner);
                    if old_transform != transform {
                        state.transform_changed(conn, qh, surface, transform);
                    }
                }
                return;
            }
            _ => unreachable!(),
        }

        // NOTE: with v6 we don't need any special handling of the scale factor, everything
        // was handled from the above, so return.
        if surface.version() >= 6 {
            return;
        }

        inner.watcher.get_or_insert_with(|| {
            // Avoid storing the WlSurface inside the closure as that would create a reference
            // cycle.  Instead, store the ID and re-create the proxy.
            let id = surface.id();
            OutputState::add_scale_watcher(state, move |state, conn, qh, _| {
                let id = id.clone();
                if let Ok(surface) = wl_surface::WlSurface::from_id(conn, id) {
                    if let Some(data) = surface.data::<U>() {
                        let data = data.surface_data();
                        let inner = data.inner.lock().unwrap();
                        dispatch_surface_state_updates(state, conn, qh, &surface, data, inner);
                    }
                }
            })
        });

        dispatch_surface_state_updates(state, conn, qh, surface, data, inner);
    }
}

fn dispatch_surface_state_updates<D, U>(
    state: &mut D,
    conn: &Connection,
    qh: &QueueHandle<D>,
    surface: &WlSurface,
    data: &SurfaceData,
    mut inner: MutexGuard<SurfaceDataInner>,
) where
    D: Dispatch<wl_surface::WlSurface, U> + CompositorHandler + OutputHandler + 'static,
    U: SurfaceDataExt + 'static,
{
    let current_scale = data.scale_factor.load(Ordering::Relaxed);
    let (factor, transform) = match inner
        .outputs
        .iter()
        .filter_map(|output| {
            output
                .data::<OutputData>()
                .map(|data| data.with_output_info(|info| (info.scale_factor, info.transform)))
        })
        // NOTE: reduce will only work for more than 1 element, thus we map transform to normal
        // since we can't guess which one to use. With the exactly one output, the corrent
        // transform will be passed instead.
        .reduce(|acc, props| (acc.0.max(props.0), wl_output::Transform::Normal))
    {
        None => return,
        Some(props) => props,
    };

    data.scale_factor.store(factor, Ordering::Relaxed);
    let old_transform = mem::replace(&mut inner.transform, transform);
    // Drop the mutex before we send of any events.
    drop(inner);

    if factor != current_scale {
        state.scale_factor_changed(conn, qh, surface, factor);
    }

    if transform != old_transform {
        state.transform_changed(conn, qh, surface, transform);
    }
}

/// A trivial wrapper around a [`WlRegion`][wl_region::WlRegion].
///
/// This destroys the region on drop.
#[derive(Debug)]
pub struct Region(wl_region::WlRegion);

impl Region {
    pub fn new(
        compositor: &impl ProvidesBoundGlobal<
            wl_compositor::WlCompositor,
            { CompositorState::API_VERSION_MAX },
        >,
    ) -> Result<Region, GlobalError> {
        compositor
            .bound_global()
            .map(|c| {
                c.send_constructor(wl_compositor::Request::CreateRegion {}, Arc::new(RegionData))
                    .unwrap_or_else(|_| Proxy::inert(c.backend().clone()))
            })
            .map(Region)
    }

    pub fn add(&self, x: i32, y: i32, width: i32, height: i32) {
        self.0.add(x, y, width, height)
    }

    pub fn subtract(&self, x: i32, y: i32, width: i32, height: i32) {
        self.0.subtract(x, y, width, height)
    }

    pub fn wl_region(&self) -> &wl_region::WlRegion {
        &self.0
    }
}

impl Drop for Region {
    fn drop(&mut self) {
        self.0.destroy()
    }
}

struct RegionData;

impl wayland_client::backend::ObjectData for RegionData {
    fn event(
        self: Arc<Self>,
        _: &wayland_client::backend::Backend,
        _: wayland_client::backend::protocol::Message<wayland_client::backend::ObjectId, OwnedFd>,
    ) -> Option<Arc<(dyn wayland_client::backend::ObjectData + 'static)>> {
        unreachable!("wl_region has no events");
    }
    fn destroyed(&self, _: wayland_client::backend::ObjectId) {}
}

impl<D> Dispatch<wl_compositor::WlCompositor, GlobalData, D> for CompositorState
where
    D: Dispatch<wl_compositor::WlCompositor, GlobalData> + CompositorHandler,
{
    fn event(
        _: &mut D,
        _: &wl_compositor::WlCompositor,
        _: wl_compositor::Event,
        _: &GlobalData,
        _: &Connection,
        _: &QueueHandle<D>,
    ) {
        unreachable!("wl_compositor has no events")
    }
}

impl ProvidesBoundGlobal<wl_compositor::WlCompositor, { CompositorState::API_VERSION_MAX }>
    for CompositorState
{
    fn bound_global(&self) -> Result<wl_compositor::WlCompositor, GlobalError> {
        Ok(self.wl_compositor.clone())
    }
}

impl<D> Dispatch<wl_callback::WlCallback, wl_surface::WlSurface, D> for CompositorState
where
    D: Dispatch<wl_callback::WlCallback, wl_surface::WlSurface> + CompositorHandler,
{
    fn event(
        state: &mut D,
        _: &wl_callback::WlCallback,
        event: wl_callback::Event,
        surface: &wl_surface::WlSurface,
        conn: &Connection,
        qh: &QueueHandle<D>,
    ) {
        match event {
            wl_callback::Event::Done { callback_data } => {
                state.frame(conn, qh, surface, callback_data);
            }

            _ => unreachable!(),
        }
    }
}