~whynothugo/shotman

ec67a15775b80c35130a57e2d91ad1c6603aed0c — Hugo Osvaldo Barrera a month ago 99baca7
Adapt to recent changes in wl_buffer protocol

Destroying buffers now implicitly detaches them, so the lifetime of each
buffer needs to be manually tracked.

See: https://gitlab.freedesktop.org/wayland/wayland/-/merge_requests/363
1 files changed, 49 insertions(+), 17 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +49 -17
@@ 91,6 91,8 @@ enum DesiredTarget {
struct OutputDetector;
/// Related to types associated with the thumbnail window.
struct Window;
#[derive(Default)]
struct Screenshot;

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();


@@ 470,7 472,8 @@ impl State {
        let detector_buffer =
            globals
                .single_pixel_manager
                .create_u32_rgba_buffer(0, 0, 0, 0, qh, ());
                .create_u32_rgba_buffer(0, 0, 0, 0, qh, OutputDetector);
        detector_surface.attach(Some(&detector_buffer), 0, 0);

        let detector = Detector {
            surface: detector_surface,


@@ 665,17 668,17 @@ impl State {
        } else {
            (u32::MAX / 2, u32::MAX / 2, u32::MAX / 2, u32::MAX)
        };
        let window_buffer =
            self.globals
                .single_pixel_manager
                .create_u32_rgba_buffer(r, g, b, a, qh, ());
        let window_buffer = self
            .globals
            .single_pixel_manager
            .create_u32_rgba_buffer(r, g, b, a, qh, Window);
        self.window_surface.attach(Some(&window_buffer), 0, 0);
        self.window_surface.damage_buffer(0, 0, 1, 1);
        self.window_viewport
            .set_destination(i32::try_from(width)?, i32::try_from(height)?);
        window_buffer.destroy();

        self.window_surface.commit();
        window_buffer.destroy();

        Ok(())
    }


@@ 1057,17 1060,46 @@ impl Dispatch<wl_shm::WlShm, ()> for State {
    }
}

impl Dispatch<wl_buffer::WlBuffer, ()> for State {
impl Dispatch<wl_buffer::WlBuffer, Window> for State {
    fn event(
        _: &mut Self,
        buffer: &wl_buffer::WlBuffer,
        event: wl_buffer::Event,
        (): &(),
        _: &Window,
        _: &Connection,
        _: &QueueHandle<Self>,
    ) {
        if let wl_buffer::Event::Release = event {
            debug!("Buffer released: {}.", buffer.id());
        }
    }
}

impl Dispatch<wl_buffer::WlBuffer, Screenshot> for State {
    fn event(
        _: &mut Self,
        buffer: &wl_buffer::WlBuffer,
        event: wl_buffer::Event,
        _: &Screenshot,
        _: &Connection,
        _: &QueueHandle<Self>,
    ) {
        if let wl_buffer::Event::Release = event {
            debug!("Buffer released: {}.", buffer.id());
        }
    }
}

impl Dispatch<wl_buffer::WlBuffer, OutputDetector> for State {
    fn event(
        _: &mut Self,
        buffer: &wl_buffer::WlBuffer,
        event: wl_buffer::Event,
        _: &OutputDetector,
        _: &Connection,
        _: &QueueHandle<Self>,
    ) {
        if let wl_buffer::Event::Release = event {
            debug!("Buffer released: {}, destroying it.", buffer.id());
            buffer.destroy();
        }
    }


@@ 1120,11 1152,6 @@ impl Dispatch<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, OutputDetector> for Sta
    ) {
        if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event {
            wlr_layer_surface.ack_configure(serial);

            state
                .detector
                .surface
                .attach(Some(&state.detector.buffer), 0, 0);
            state.detector.surface.commit();
        }
    }


@@ 1331,9 1358,14 @@ impl Dispatch<ZwlrScreencopyFrameV1, ()> for State {
                }
                .into();

                let (buffer, fd) =
                    buffers::empty_buffer(&state.globals.wl_shm, width, height, format, qh)
                        .unwrap();
                let (buffer, fd) = buffers::empty_buffer::<_, _, Screenshot>(
                    &state.globals.wl_shm,
                    width,
                    height,
                    format,
                    qh,
                )
                .unwrap();

                state.shot_memfile = Some(Arc::new(fd));
                if state.shot_buffer.set(buffer).is_err() {