Skip to main content

gdk4_wayland/
wayland_device.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use glib::translate::*;
7#[cfg(feature = "wayland_crate")]
8#[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
9use wayland_client::{
10    Proxy,
11    backend::ObjectId,
12    protocol::{wl_keyboard::WlKeyboard, wl_pointer::WlPointer, wl_seat::WlSeat},
13};
14
15#[cfg(feature = "wayland_crate")]
16#[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
17use crate::prelude::*;
18use crate::{WaylandDevice, ffi};
19
20impl WaylandDevice {
21    #[doc(alias = "gdk_wayland_device_get_wl_keyboard")]
22    #[doc(alias = "get_wl_keyboard")]
23    pub fn wl_keyboard_raw(&self) -> Option<NonNull<c_void>> {
24        NonNull::new(unsafe { ffi::gdk_wayland_device_get_wl_keyboard(self.to_glib_none().0) })
25    }
26
27    /// Returns the Wayland `wl_keyboard` of a [`gdk::Device`][crate::gdk::Device].
28    ///
29    /// # Returns
30    ///
31    /// a Wayland `wl_keyboard`
32    #[doc(alias = "gdk_wayland_device_get_wl_keyboard")]
33    #[doc(alias = "get_wl_keyboard")]
34    #[cfg(feature = "wayland_crate")]
35    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
36    pub fn wl_keyboard(&self) -> Option<WlKeyboard> {
37        let display = self.display().downcast::<crate::WaylandDisplay>().unwrap();
38        let ptr = self.wl_keyboard_raw()?;
39        unsafe {
40            let cnx = display.connection();
41            let id = ObjectId::from_ptr(WlKeyboard::interface(), ptr.as_ptr() as *mut _).unwrap();
42
43            WlKeyboard::from_id(&cnx, id).ok()
44        }
45    }
46
47    #[doc(alias = "gdk_wayland_device_get_wl_pointer")]
48    #[doc(alias = "get_wl_pointer")]
49    pub fn wl_pointer_raw(&self) -> Option<NonNull<c_void>> {
50        NonNull::new(unsafe { ffi::gdk_wayland_device_get_wl_pointer(self.to_glib_none().0) })
51    }
52
53    /// Returns the Wayland `wl_pointer` of a [`gdk::Device`][crate::gdk::Device].
54    ///
55    /// # Returns
56    ///
57    /// a Wayland `wl_pointer`
58    #[doc(alias = "gdk_wayland_device_get_wl_pointer")]
59    #[doc(alias = "get_wl_pointer")]
60    #[cfg(feature = "wayland_crate")]
61    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
62    pub fn wl_pointer(&self) -> Option<WlPointer> {
63        let display = self.display().downcast::<crate::WaylandDisplay>().unwrap();
64        let ptr = self.wl_pointer_raw()?;
65        unsafe {
66            let cnx = display.connection();
67            let id = ObjectId::from_ptr(WlPointer::interface(), ptr.as_ptr() as *mut _).unwrap();
68
69            WlPointer::from_id(&cnx, id).ok()
70        }
71    }
72
73    #[doc(alias = "gdk_wayland_device_get_wl_seat")]
74    #[doc(alias = "get_wl_seat")]
75    pub fn wl_seat_raw(&self) -> Option<NonNull<c_void>> {
76        NonNull::new(unsafe { ffi::gdk_wayland_device_get_wl_seat(self.to_glib_none().0) })
77    }
78
79    /// Returns the Wayland `wl_seat` of a [`gdk::Device`][crate::gdk::Device].
80    ///
81    /// # Returns
82    ///
83    /// a Wayland `wl_seat`
84    #[doc(alias = "gdk_wayland_device_get_wl_seat")]
85    #[doc(alias = "get_wl_seat")]
86    #[cfg(feature = "wayland_crate")]
87    #[cfg_attr(docsrs, doc(cfg(feature = "wayland_crate")))]
88    pub fn wl_seat(&self) -> Option<WlSeat> {
89        let display = self.display().downcast::<crate::WaylandDisplay>().unwrap();
90        let ptr = self.wl_seat_raw()?;
91        unsafe {
92            let cnx = display.connection();
93            let id = ObjectId::from_ptr(WlSeat::interface(), ptr.as_ptr() as *mut _).unwrap();
94
95            WlSeat::from_id(&cnx, id).ok()
96        }
97    }
98}