clutter/auto/
device_manager.rs

1use crate::{Backend, InputDevice, InputDeviceType};
2use glib::{
3    object::{Cast, IsA},
4    signal::{connect_raw, SignalHandlerId},
5    translate::*,
6    StaticType, Value,
7};
8use std::boxed::Box as Box_;
9use std::{fmt, mem::transmute};
10
11glib_wrapper! {
12    pub struct DeviceManager(Object<ffi::ClutterDeviceManager, ffi::ClutterDeviceManagerClass, DeviceManagerClass>);
13
14    match fn {
15        get_type => || ffi::clutter_device_manager_get_type(),
16    }
17}
18
19impl DeviceManager {
20    /// Retrieves the device manager singleton
21    ///
22    /// # Returns
23    ///
24    /// the `DeviceManager` singleton.
25    ///  The returned instance is owned by Clutter and it should not be
26    ///  modified or freed
27    pub fn get_default() -> Option<DeviceManager> {
28        unsafe { from_glib_none(ffi::clutter_device_manager_get_default()) }
29    }
30}
31
32pub const NONE_DEVICE_MANAGER: Option<&DeviceManager> = None;
33
34/// Trait containing all `DeviceManager` methods.
35///
36/// # Implementors
37///
38/// [`DeviceManager`](struct.DeviceManager.html)
39pub trait DeviceManagerExt: 'static {
40    /// Retrieves the core `InputDevice` of type `device_type`
41    ///
42    /// Core devices are devices created automatically by the default
43    /// Clutter backend
44    /// ## `device_type`
45    /// the type of the core device
46    ///
47    /// # Returns
48    ///
49    /// a `InputDevice` or `None`. The
50    ///  returned device is owned by the `DeviceManager` and should
51    ///  not be modified or freed
52    fn get_core_device(&self, device_type: InputDeviceType) -> Option<InputDevice>;
53
54    /// Retrieves the `InputDevice` with the given `device_id`
55    /// ## `device_id`
56    /// the integer id of a device
57    ///
58    /// # Returns
59    ///
60    /// a `InputDevice` or `None`. The
61    ///  returned device is owned by the `DeviceManager` and should
62    ///  never be modified or freed
63    fn get_device(&self, device_id: i32) -> Option<InputDevice>;
64
65    /// Lists all currently registered input devices
66    ///
67    /// # Returns
68    ///
69    ///
70    ///  a newly allocated list of `InputDevice` objects. Use
71    ///  `glib::SList::free` to deallocate it when done
72    fn list_devices(&self) -> Vec<InputDevice>;
73
74    /// Lists all currently registered input devices
75    ///
76    /// # Returns
77    ///
78    ///
79    ///  a pointer to the internal list of `InputDevice` objects. The
80    ///  returned list is owned by the `DeviceManager` and should never
81    ///  be modified or freed
82    fn peek_devices(&self) -> Vec<InputDevice>;
83
84    fn get_property_backend(&self) -> Option<Backend>;
85
86    /// The ::device-added signal is emitted each time a device has been
87    /// added to the `DeviceManager`
88    /// ## `device`
89    /// the newly added `InputDevice`
90    fn connect_device_added<F: Fn(&Self, &InputDevice) + 'static>(&self, f: F) -> SignalHandlerId;
91
92    /// The ::device-removed signal is emitted each time a device has been
93    /// removed from the `DeviceManager`
94    /// ## `device`
95    /// the removed `InputDevice`
96    fn connect_device_removed<F: Fn(&Self, &InputDevice) + 'static>(&self, f: F)
97        -> SignalHandlerId;
98}
99
100impl<O: IsA<DeviceManager>> DeviceManagerExt for O {
101    fn get_core_device(&self, device_type: InputDeviceType) -> Option<InputDevice> {
102        unsafe {
103            from_glib_none(ffi::clutter_device_manager_get_core_device(
104                self.as_ref().to_glib_none().0,
105                device_type.to_glib(),
106            ))
107        }
108    }
109
110    fn get_device(&self, device_id: i32) -> Option<InputDevice> {
111        unsafe {
112            from_glib_none(ffi::clutter_device_manager_get_device(
113                self.as_ref().to_glib_none().0,
114                device_id,
115            ))
116        }
117    }
118
119    fn list_devices(&self) -> Vec<InputDevice> {
120        unsafe {
121            FromGlibPtrContainer::from_glib_container(ffi::clutter_device_manager_list_devices(
122                self.as_ref().to_glib_none().0,
123            ))
124        }
125    }
126
127    fn peek_devices(&self) -> Vec<InputDevice> {
128        unsafe {
129            FromGlibPtrContainer::from_glib_none(ffi::clutter_device_manager_peek_devices(
130                self.as_ref().to_glib_none().0,
131            ))
132        }
133    }
134
135    fn get_property_backend(&self) -> Option<Backend> {
136        unsafe {
137            let mut value = Value::from_type(<Backend as StaticType>::static_type());
138            gobject_sys::g_object_get_property(
139                self.to_glib_none().0 as *mut gobject_sys::GObject,
140                b"backend\0".as_ptr() as *const _,
141                value.to_glib_none_mut().0,
142            );
143            value
144                .get()
145                .expect("Return Value for property `backend` getter")
146        }
147    }
148
149    fn connect_device_added<F: Fn(&Self, &InputDevice) + 'static>(&self, f: F) -> SignalHandlerId {
150        unsafe extern "C" fn device_added_trampoline<P, F: Fn(&P, &InputDevice) + 'static>(
151            this: *mut ffi::ClutterDeviceManager,
152            device: *mut ffi::ClutterInputDevice,
153            f: glib_sys::gpointer,
154        ) where
155            P: IsA<DeviceManager>,
156        {
157            let f: &F = &*(f as *const F);
158            f(
159                &DeviceManager::from_glib_borrow(this).unsafe_cast_ref(),
160                &from_glib_borrow(device),
161            )
162        }
163        unsafe {
164            let f: Box_<F> = Box_::new(f);
165            connect_raw(
166                self.as_ptr() as *mut _,
167                b"device-added\0".as_ptr() as *const _,
168                Some(transmute::<_, unsafe extern "C" fn()>(
169                    device_added_trampoline::<Self, F> as *const (),
170                )),
171                Box_::into_raw(f),
172            )
173        }
174    }
175
176    fn connect_device_removed<F: Fn(&Self, &InputDevice) + 'static>(
177        &self,
178        f: F,
179    ) -> SignalHandlerId {
180        unsafe extern "C" fn device_removed_trampoline<P, F: Fn(&P, &InputDevice) + 'static>(
181            this: *mut ffi::ClutterDeviceManager,
182            device: *mut ffi::ClutterInputDevice,
183            f: glib_sys::gpointer,
184        ) where
185            P: IsA<DeviceManager>,
186        {
187            let f: &F = &*(f as *const F);
188            f(
189                &DeviceManager::from_glib_borrow(this).unsafe_cast_ref(),
190                &from_glib_borrow(device),
191            )
192        }
193        unsafe {
194            let f: Box_<F> = Box_::new(f);
195            connect_raw(
196                self.as_ptr() as *mut _,
197                b"device-removed\0".as_ptr() as *const _,
198                Some(transmute::<_, unsafe extern "C" fn()>(
199                    device_removed_trampoline::<Self, F> as *const (),
200                )),
201                Box_::into_raw(f),
202            )
203        }
204    }
205}
206
207impl fmt::Display for DeviceManager {
208    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
209        write!(f, "DeviceManager")
210    }
211}