gdk4_wayland/
wayland_device.rs

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