Skip to main content

gdkwayland/
wayland_device.rs

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