clutter/auto/
event.rs

1#![allow(unused_macros)]
2
3use glib::translate::*;
4use libc::c_void;
5use std::{fmt, mem, ptr};
6
7// use AxisUse;
8use crate::{EventSequence, EventType, InputDevice, ModifierType};
9
10glib_wrapper! {
11    /// A generic Clutter event.
12    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
13    pub struct Event(Boxed<ffi::ClutterEvent>);
14
15    match fn {
16        copy => |ptr| ffi::clutter_event_copy(ptr),
17        free => |ptr| ffi::clutter_event_free(ptr),
18        get_type => || ffi::clutter_event_get_type(),
19    }
20}
21
22event_wrapper!(Event, ClutterAnyEvent);
23
24impl Event {
25    /// Creates a new event.
26    pub fn new(type_: EventType) -> Event {
27        assert_initialized_main_thread!();
28        unsafe { from_glib_none(ffi::clutter_event_new(type_.to_glib())) }
29    }
30
31    pub fn get() -> Option<Event> {
32        assert_initialized_main_thread!();
33        unsafe { from_glib_none(ffi::clutter_event_get()) }
34    }
35
36    pub fn put(&self) {
37        unsafe { ffi::clutter_event_put(self.to_glib_none().0) }
38    }
39
40    // /// Set the event handler.
41    // ///
42    // /// The callback `handler` is called for each event. If `None`, event
43    // /// handling is disabled.
44    // pub fn set_handler<F: Fn(&mut Event) + 'static>(handler: Option<F>) {
45    //     assert_initialized_main_thread!();
46    //     unsafe extern "C" fn event_handler_trampoline<F: Fn(&mut Event) + 'static>(
47    //         event: *mut ffi::ClutterEvent,
48    //         ptr: glib_sys::gpointer,
49    //     ) {
50    //         if ptr != ptr::null_mut() {
51    //             let f: &F = &*(ptr as *mut _);
52    //             let mut event = from_glib_none(event);
53    //             f(&mut event)
54    //         }
55    //     }
56    //     unsafe extern "C" fn event_handler_destroy<F: Fn(&mut Event) + 'static>(
57    //         ptr: glib_sys::gpointer,
58    //     ) {
59    //         if ptr != ptr::null_mut() {
60    //             // convert back to Box and free
61    //             let _boxed: Box<F> = Box::from_raw(ptr as *mut _);
62    //         }
63    //     }
64    //     if let Some(handler) = handler {
65    //         // allocate and convert to target type
66    //         // double box to reduce a fat pointer to a simple pointer
67    //         let boxed: Box<F> = Box::new(handler);
68    //         let ptr: *mut c_void = Box::into_raw(boxed) as *mut _;
69    //         unsafe {
70    //             ffi::clutter_event_handler_set(
71    //                 Some(event_handler_trampoline::<F>),
72    //                 ptr,
73    //                 Some(event_handler_destroy::<F>),
74    //             )
75    //         }
76    //     } else {
77    //         unsafe { ffi::gdk_event_handler_set(None, ptr::null_mut(), None) }
78    //     }
79    // }
80
81    // pub fn get_axis(&self, axis_use: AxisUse) -> Option<f64> {
82    //     let mut value = 0f64;
83    //     if unsafe {
84    //         from_glib(ffi::gdk_event_get_axis(
85    //             self.to_glib_none().0,
86    //             axis_use.to_glib(),
87    //             &mut value,
88    //         ))
89    //     } {
90    //         Some(value)
91    //     } else {
92    //         None
93    //     }
94    // }
95
96    // pub fn get_button(&self) -> Option<u32> {
97    //     let mut button = 0u32;
98    //     if unsafe {
99    //         from_glib(ffi::clutter_event_get_button(
100    //             self.to_glib_none().0,
101    //             &mut button,
102    //         ))
103    //     } {
104    //         Some(button)
105    //     } else {
106    //         None
107    //     }
108    // }
109
110    // pub fn get_click_count(&self) -> Option<u32> {
111    //     let mut click_count = 0u32;
112    //     if unsafe {
113    //         from_glib(ffi::clutter_event_get_click_count(
114    //             self.to_glib_none().0,
115    //             &mut click_count,
116    //         ))
117    //     } {
118    //         Some(click_count)
119    //     } else {
120    //         None
121    //     }
122    // }
123
124    // pub fn get_coords(&self) -> Option<(f32, f32)> {
125    //     let mut x_win = 0_f32;
126    //     let mut y_win = 0_f32;
127    //     if unsafe {
128    //         from_glib(ffi::clutter_event_get_coords(
129    //             self.to_glib_none().0,
130    //             &mut x_win,
131    //             &mut y_win,
132    //         ))
133    //     } {
134    //         Some((x_win, y_win))
135    //     } else {
136    //         None
137    //     }
138    // }
139
140    // pub fn get_keycode(&self) -> Option<u16> {
141    //     let mut keycode = 0u16;
142    //     if unsafe {
143    //         from_glib(ffi::clutter_event_get_keycode(
144    //             self.to_glib_none().0,
145    //             &mut keycode,
146    //         ))
147    //     } {
148    //         Some(keycode)
149    //     } else {
150    //         None
151    //     }
152    // }
153
154    // pub fn get_keyval(&self) -> Option<u32> {
155    //     let mut keyval = 0u32;
156    //     if unsafe {
157    //         from_glib(ffi::clutter_event_get_keyval(
158    //             self.to_glib_none().0,
159    //             &mut keyval,
160    //         ))
161    //     } {
162    //         Some(keyval)
163    //     } else {
164    //         None
165    //     }
166    // }
167
168    // pub fn get_root_coords(&self) -> Option<(f32, f32)> {
169    //     let mut x_root = 0_f32;
170    //     let mut y_root = 0_f32;
171    //     if unsafe {
172    //         from_glib(ffi::clutter_event_get_coords(
173    //             self.to_glib_none().0,
174    //             &mut x_root,
175    //             &mut y_root,
176    //         ))
177    //     } {
178    //         Some((x_root, y_root))
179    //     } else {
180    //         None
181    //     }
182    // }
183
184    // pub fn get_scroll_direction(&self) -> Option<ScrollDirection> {
185    //     unsafe {
186    //         let mut direction = mem::MaybeUninit::uninit();
187    //         if from_glib(ffi::clutter_event_get_scroll_direction(
188    //             self.to_glib_none().0,
189    //             direction.as_mut_ptr(),
190    //         )) {
191    //             Some(from_glib(direction.assume_init()))
192    //         } else {
193    //             None
194    //         }
195    //     }
196    // }
197
198    // pub fn get_scroll_deltas(&self) -> Option<(f64, f64)> {
199    //     let mut delta_x = 0_f64;
200    //     let mut delta_y = 0_f64;
201    //     if unsafe {
202    //         from_glib(ffi::clutter_event_get_scroll_delta(
203    //             self.to_glib_none().0,
204    //             &mut delta_x,
205    //             &mut delta_y,
206    //         ))
207    //     } {
208    //         Some((delta_x, delta_y))
209    //     } else {
210    //         None
211    //     }
212    // }
213
214    // pub fn is_scroll_stop_event(&self) -> bool {
215    //     unsafe {
216    //         from_glib(ffi::gdk_event_is_scroll_stop_event(
217    //             self.to_glib_none().0,
218    //         ))
219    //     }
220    // }
221
222    // pub fn get_state(&self) -> Option<ModifierType> {
223    //     unsafe {
224    //         let mut state = mem::MaybeUninit::uninit();
225    //         if from_glib(ffi::clutter_event_get_scroll_direction(
226    //             self.to_glib_none().0,
227    //             state.as_mut_ptr(),
228    //         )) {
229    //             Some(from_glib(state.assume_init() as u32))
230    //         } else {
231    //             None
232    //         }
233    //     }
234    // }
235
236    pub fn get_time(&self) -> u32 {
237        unsafe { ffi::clutter_event_get_time(self.to_glib_none().0) }
238    }
239
240    /// Returns the associated `Window` if applicable.
241    // pub fn get_window(&self) -> Option<Window> {
242    //     unsafe { from_glib_none(ffi::clutter_event_get_window(self.to_glib_none().0)) }
243    // }
244
245    pub fn get_event_sequence(&self) -> Option<EventSequence> {
246        unsafe { from_glib_none(ffi::clutter_event_get_event_sequence(self.to_glib_none().0)) }
247    }
248
249    // pub fn triggers_context_menu(&self) -> bool {
250    //     unsafe {
251    //         from_glib(ffi::clutter_event_triggers_context_menu(
252    //             self.to_glib_none().0,
253    //         ))
254    //     }
255    // }
256
257    // pub fn get_seat(&self) -> Option<Seat> {
258    //     unsafe { from_glib_none(ffi::clutter_event_get_seat(self.to_glib_none().0)) }
259    // }
260
261    // pub fn get_scancode(&mut self) -> i32 {
262    //     unsafe { ffi::clutter_event_get_scancode(self.to_glib_none_mut().0) }
263    // }
264
265    // pub fn get_pointer_emulated(&mut self) -> bool {
266    //     unsafe {
267    //         from_glib(ffi::clutter_event_is_pointer_emulated(
268    //             self.to_glib_none_mut().0,
269    //         ))
270    //     }
271    // }
272
273    // pub fn set_screen(&mut self, screen: Option<&Screen>) {
274    //     unsafe {
275    //         ffi::gdk_event_set_screen(self.to_glib_none_mut().0, screen.to_glib_none().0)
276    //     }
277    // }
278
279    // pub fn get_screen(&self) -> Option<Screen> {
280    //     unsafe { from_glib_none(ffi::clutter_event_get_screen(self.to_glib_none().0)) }
281    // }
282
283    pub fn set_device(&mut self, device: Option<&InputDevice>) {
284        unsafe { ffi::clutter_event_set_device(self.to_glib_none_mut().0, device.to_glib_none().0) }
285    }
286
287    pub fn get_device(&self) -> Option<InputDevice> {
288        unsafe { from_glib_none(ffi::clutter_event_get_device(self.to_glib_none().0)) }
289    }
290
291    pub fn set_source_device(&mut self, device: Option<&InputDevice>) {
292        unsafe {
293            ffi::clutter_event_set_source_device(self.to_glib_none_mut().0, device.to_glib_none().0)
294        }
295    }
296
297    pub fn get_source_device(&self) -> Option<InputDevice> {
298        unsafe { from_glib_none(ffi::clutter_event_get_source_device(self.to_glib_none().0)) }
299    }
300
301    // pub fn set_device_tool(&mut self, device: Option<&DeviceTool>) {
302    //     unsafe {
303    //         ffi::clutter_event_set_device_tool(self.to_glib_none_mut().0, device.to_glib_none().0)
304    //     }
305    // }
306
307    // pub fn get_device_tool(&self) -> Option<DeviceTool> {
308    //     unsafe {
309    //         from_glib_none(ffi::clutter_event_get_device_tool(
310    //             self.to_glib_none().0,
311    //         ))
312    //     }
313    // }
314
315    // /// Returns the event type.
316    // pub fn get_event_type(&self) -> EventType {
317    //     from_glib(self.as_ref().type_)
318    // }
319
320    // /// Returns whether the event was sent explicitly.
321    // #[cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
322    // pub fn get_send_event(&self) -> bool {
323    //     from_glib(self.as_ref().send_event as i32)
324    // }
325
326    /// Returns `true` if the event type matches `T`.
327    pub fn is<T: FromEvent>(&self) -> bool {
328        T::is(self)
329    }
330
331    /// Tries to downcast to a specific event type.
332    pub fn downcast<T: FromEvent>(self) -> Result<T, Self> {
333        T::from(self)
334    }
335
336    /// Tries to downcast to a specific event type.
337    pub fn downcast_ref<T: FromEvent>(&self) -> Option<&T> {
338        if T::is(self) {
339            unsafe { Some(&*(self as *const _ as *const _)) }
340        } else {
341            None
342        }
343    }
344
345    /// Tries to downcast to a specific event type.
346    pub fn downcast_mut<T: FromEvent>(&mut self) -> Option<&mut T> {
347        if T::is(self) {
348            unsafe { Some(&mut *(self as *mut _ as *mut _)) }
349        } else {
350            None
351        }
352    }
353}
354
355impl fmt::Debug for Event {
356    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
357        fmt.debug_struct("Event")
358            .field("inner", &self.0)
359            // .field("type", &self.get_event_type())
360            .finish()
361    }
362}
363
364/// A helper trait implemented by all event subtypes.
365pub trait FromEvent: Sized {
366    fn is(ev: &Event) -> bool;
367    fn from(ev: Event) -> Result<Self, Event>;
368}
369
370impl FromEvent for Event {
371    #[inline]
372    fn is(_ev: &Event) -> bool {
373        skip_assert_initialized!();
374        true
375    }
376
377    #[inline]
378    fn from(ev: Event) -> Result<Self, Event> {
379        skip_assert_initialized!();
380        Ok(ev)
381    }
382}