1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{
    AxisUse, Device, DeviceTool, Display, EventSequence, EventType, ModifierType, Seat, Surface,
    TimeCoord,
};
use glib::translate::*;
use std::{fmt, mem, ptr};

glib::wrapper! {
    /// [`Event`][crate::Event]s are immutable data structures, created by GDK to
    /// represent windowing system events.
    ///
    /// In GTK applications the events are handled automatically by toplevel
    /// widgets and passed on to the event controllers of appropriate widgets,
    /// so using [`Event`][crate::Event] and its related API is rarely needed.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    #[doc(alias = "GdkEvent")]
    pub struct Event(Shared<ffi::GdkEvent>);

    match fn {
        ref => |ptr| ffi::gdk_event_ref(ptr),
        unref => |ptr| ffi::gdk_event_unref(ptr),
    }
}

impl glib::StaticType for Event {
    fn static_type() -> glib::Type {
        unsafe { from_glib(ffi::gdk_event_get_type()) }
    }
}

impl Event {
    pub const NONE: Option<&'static Event> = None;

    /// Extracts all axis values from an event.
    ///
    /// To find out which axes are used, use [`DeviceTool::axes()`][crate::DeviceTool::axes()]
    /// on the device tool returned by [`device_tool()`][Self::device_tool()].
    ///
    /// # Returns
    ///
    /// [`true`] on success, otherwise [`false`]
    ///
    /// ## `axes`
    /// the array of values for all axes
    #[doc(alias = "gdk_event_get_axes")]
    #[doc(alias = "get_axes")]
    pub fn axes(&self) -> Option<Vec<f64>> {
        unsafe {
            let mut axes = ptr::null_mut();
            let mut n_axes = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gdk_event_get_axes(
                self.as_ref().to_glib_none().0,
                &mut axes,
                n_axes.as_mut_ptr(),
            ));
            if ret {
                Some(FromGlibContainer::from_glib_none_num(
                    axes,
                    n_axes.assume_init() as _,
                ))
            } else {
                None
            }
        }
    }

    /// Extract the axis value for a particular axis use from
    /// an event structure.
    ///
    /// To find out which axes are used, use [`DeviceTool::axes()`][crate::DeviceTool::axes()]
    /// on the device tool returned by [`device_tool()`][Self::device_tool()].
    /// ## `axis_use`
    /// the axis use to look for
    ///
    /// # Returns
    ///
    /// [`true`] if the specified axis was found, otherwise [`false`]
    ///
    /// ## `value`
    /// location to store the value found
    #[doc(alias = "gdk_event_get_axis")]
    #[doc(alias = "get_axis")]
    pub fn axis(&self, axis_use: AxisUse) -> Option<f64> {
        unsafe {
            let mut value = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gdk_event_get_axis(
                self.as_ref().to_glib_none().0,
                axis_use.into_glib(),
                value.as_mut_ptr(),
            ));
            if ret {
                Some(value.assume_init())
            } else {
                None
            }
        }
    }

    /// Returns the device of an event.
    ///
    /// # Returns
    ///
    /// a [`Device`][crate::Device]
    #[doc(alias = "gdk_event_get_device")]
    #[doc(alias = "get_device")]
    pub fn device(&self) -> Option<Device> {
        unsafe { from_glib_none(ffi::gdk_event_get_device(self.as_ref().to_glib_none().0)) }
    }

    /// Returns a [`DeviceTool`][crate::DeviceTool] representing the tool that
    /// caused the event.
    ///
    /// If the was not generated by a device that supports
    /// different tools (such as a tablet), this function will
    /// return [`None`].
    ///
    /// Note: the [`DeviceTool`][crate::DeviceTool] will be constant during
    /// the application lifetime, if settings must be stored
    /// persistently across runs, see [`DeviceTool::serial()`][crate::DeviceTool::serial()].
    ///
    /// # Returns
    ///
    /// The current device tool
    #[doc(alias = "gdk_event_get_device_tool")]
    #[doc(alias = "get_device_tool")]
    pub fn device_tool(&self) -> Option<DeviceTool> {
        unsafe {
            from_glib_none(ffi::gdk_event_get_device_tool(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Retrieves the display associated to the @self.
    ///
    /// # Returns
    ///
    /// a [`Display`][crate::Display]
    #[doc(alias = "gdk_event_get_display")]
    #[doc(alias = "get_display")]
    pub fn display(&self) -> Option<Display> {
        unsafe { from_glib_none(ffi::gdk_event_get_display(self.as_ref().to_glib_none().0)) }
    }

    /// Returns the event sequence to which the event belongs.
    ///
    /// Related touch events are connected in a sequence. Other
    /// events typically don't have event sequence information.
    ///
    /// # Returns
    ///
    /// the event sequence that the event belongs to
    #[doc(alias = "gdk_event_get_event_sequence")]
    #[doc(alias = "get_event_sequence")]
    pub fn event_sequence(&self) -> EventSequence {
        unsafe {
            from_glib_none(ffi::gdk_event_get_event_sequence(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Retrieves the type of the event.
    ///
    /// # Returns
    ///
    /// a [`Event`][crate::Event]Type
    #[doc(alias = "gdk_event_get_event_type")]
    #[doc(alias = "get_event_type")]
    pub fn event_type(&self) -> EventType {
        unsafe {
            from_glib(ffi::gdk_event_get_event_type(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Retrieves the history of the device that @self is for, as a list of
    /// time and coordinates.
    ///
    /// The history includes positions that are not delivered as separate events
    /// to the application because they occurred in the same frame as @self.
    ///
    /// Note that only motion and scroll events record history, and motion
    /// events do it only if one of the mouse buttons is down, or the device
    /// has a tool.
    ///
    /// # Returns
    ///
    /// an
    ///   array of time and coordinates
    #[doc(alias = "gdk_event_get_history")]
    #[doc(alias = "get_history")]
    pub fn history(&self) -> Vec<TimeCoord> {
        unsafe {
            let mut out_n_coords = mem::MaybeUninit::uninit();
            let ret = FromGlibContainer::from_glib_container_num(
                ffi::gdk_event_get_history(
                    self.as_ref().to_glib_none().0,
                    out_n_coords.as_mut_ptr(),
                ),
                out_n_coords.assume_init() as _,
            );
            ret
        }
    }

    /// Returns the modifier state field of an event.
    ///
    /// # Returns
    ///
    /// the modifier state of @self
    #[doc(alias = "gdk_event_get_modifier_state")]
    #[doc(alias = "get_modifier_state")]
    pub fn modifier_state(&self) -> ModifierType {
        unsafe {
            from_glib(ffi::gdk_event_get_modifier_state(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Returns whether this event is an 'emulated' pointer event.
    ///
    /// Emulated pointer events typically originate from a touch events.
    ///
    /// # Returns
    ///
    /// [`true`] if this event is emulated
    #[doc(alias = "gdk_event_get_pointer_emulated")]
    #[doc(alias = "get_pointer_emulated")]
    pub fn is_pointer_emulated(&self) -> bool {
        unsafe {
            from_glib(ffi::gdk_event_get_pointer_emulated(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Extract the event surface relative x/y coordinates from an event.
    ///
    /// This position is in [surface coordinates](coordinates.html).
    ///
    /// # Returns
    ///
    ///
    /// ## `x`
    /// location to put event surface x coordinate
    ///
    /// ## `y`
    /// location to put event surface y coordinate
    #[doc(alias = "gdk_event_get_position")]
    #[doc(alias = "get_position")]
    pub fn position(&self) -> Option<(f64, f64)> {
        unsafe {
            let mut x = mem::MaybeUninit::uninit();
            let mut y = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gdk_event_get_position(
                self.as_ref().to_glib_none().0,
                x.as_mut_ptr(),
                y.as_mut_ptr(),
            ));
            if ret {
                Some((x.assume_init(), y.assume_init()))
            } else {
                None
            }
        }
    }

    /// Returns the seat that originated the event.
    ///
    /// # Returns
    ///
    /// a [`Seat`][crate::Seat].
    #[doc(alias = "gdk_event_get_seat")]
    #[doc(alias = "get_seat")]
    pub fn seat(&self) -> Option<Seat> {
        unsafe { from_glib_none(ffi::gdk_event_get_seat(self.as_ref().to_glib_none().0)) }
    }

    /// Extracts the surface associated with an event.
    ///
    /// # Returns
    ///
    /// The [`Surface`][crate::Surface] associated with the event
    #[doc(alias = "gdk_event_get_surface")]
    #[doc(alias = "get_surface")]
    pub fn surface(&self) -> Option<Surface> {
        unsafe { from_glib_none(ffi::gdk_event_get_surface(self.as_ref().to_glib_none().0)) }
    }

    /// Returns the timestamp of @self.
    ///
    /// Not all events have timestamps. In that case, this function
    /// returns `GDK_CURRENT_TIME`.
    ///
    /// # Returns
    ///
    /// timestamp field from @self
    #[doc(alias = "gdk_event_get_time")]
    #[doc(alias = "get_time")]
    pub fn time(&self) -> u32 {
        unsafe { ffi::gdk_event_get_time(self.as_ref().to_glib_none().0) }
    }

    /// Returns whether a [`Event`][crate::Event] should trigger a context menu,
    /// according to platform conventions.
    ///
    /// The right mouse button typically triggers context menus.
    ///
    /// This function should always be used instead of simply checking for
    /// event->button == `GDK_BUTTON_SECONDARY`.
    ///
    /// # Returns
    ///
    /// [`true`] if the event should trigger a context menu.
    #[doc(alias = "gdk_event_triggers_context_menu")]
    pub fn triggers_context_menu(&self) -> bool {
        unsafe {
            from_glib(ffi::gdk_event_triggers_context_menu(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Event")
    }
}