jay-compositor 1.15.0

The Jay compositor
use crate::client::Client;
use crate::ifs::wl_seat::tablet::PadButtonState;
use crate::ifs::wl_seat::tablet::TabletPad;
use crate::ifs::wl_seat::tablet::zwp_tablet_pad_group_v2::ZwpTabletPadGroupV2;
use crate::ifs::wl_seat::tablet::zwp_tablet_seat_v2::ZwpTabletSeatV2;
use crate::ifs::wl_seat::tablet::zwp_tablet_v2::ZwpTabletV2;
use crate::ifs::wl_surface::WlSurface;
use crate::leaks::Tracker;
use crate::object::BreakLoops;
use crate::object::Version;
use crate::wire::ZwpTabletPadV2Id;
use crate::wire::zwp_tablet_pad_v2::*;
use jay_proc::Object;
use std::cell::Cell;
use std::convert::Infallible;
use std::rc::Rc;

#[derive(Object)]
#[break_loops]
pub struct ZwpTabletPadV2 {
    pub id: ZwpTabletPadV2Id,
    pub client: Rc<Client>,
    pub tracker: Tracker<Self>,
    pub version: Version,
    pub seat: Rc<ZwpTabletSeatV2>,
    pub pad: Rc<TabletPad>,
    pub entered: Cell<bool>,
}

impl ZwpTabletPadV2 {
    fn detach(&self) {
        self.pad.bindings.remove(&self.seat);
    }

    pub fn send_group(&self, group: &ZwpTabletPadGroupV2) {
        self.client.event(Group {
            self_id: self.id,
            pad_group: group.id,
        });
    }

    pub fn send_path(&self, path: &str) {
        self.client.event(Path {
            self_id: self.id,
            path,
        });
    }

    pub fn send_buttons(&self, buttons: u32) {
        self.client.event(Buttons {
            self_id: self.id,
            buttons,
        });
    }

    pub fn send_done(&self) {
        self.client.event(Done { self_id: self.id });
    }

    pub fn send_button(&self, time: u32, button: u32, state: PadButtonState) {
        self.client.event(Button {
            self_id: self.id,
            time,
            button,
            state: match state {
                PadButtonState::Released => 0,
                PadButtonState::Pressed => 1,
            },
        });
    }

    pub fn send_enter(&self, serial: u64, tablet: &ZwpTabletV2, surface: &WlSurface) {
        self.entered.set(true);
        self.client.event(Enter {
            self_id: self.id,
            serial: serial as _,
            tablet: tablet.id,
            surface: surface.id,
        });
    }

    pub fn send_leave(&self, serial: u64, surface: &WlSurface) {
        self.entered.set(false);
        self.client.event(Leave {
            self_id: self.id,
            serial: serial as _,
            surface: surface.id,
        });
    }

    pub fn send_removed(&self) {
        self.client.event(Removed { self_id: self.id });
    }
}

impl ZwpTabletPadV2RequestHandler for ZwpTabletPadV2 {
    type Error = Infallible;

    fn set_feedback(&self, _req: SetFeedback<'_>, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        self.detach();
        self.client.remove_obj(self);
        Ok(())
    }
}

impl BreakLoops for ZwpTabletPadV2 {
    fn break_loops(self: Rc<Self>) {
        self.detach();
    }
}