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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// 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 glib;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::GString;
use glib_sys;
use gst;
use gst_gl_sys;
use libc;
use std::boxed::Box as Box_;
use std::mem;
use std::mem::transmute;
use GLContext;
use GLDisplay;

glib_wrapper! {
    /// GstGLWindow represents a window that elements can render into. A window can
    /// either be a user visible window (onscreen) or hidden (offscreen).
    ///
    /// # Implements
    ///
    /// [`GLWindowExt`](trait.GLWindowExt.html), [`gst::ObjectExt`](../gst/trait.ObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct GLWindow(Object<gst_gl_sys::GstGLWindow, gst_gl_sys::GstGLWindowClass, GLWindowClass>) @extends gst::Object;

    match fn {
        get_type => || gst_gl_sys::gst_gl_window_get_type(),
    }
}

impl GLWindow {
    /// ## `display`
    /// a `GLDisplay`
    ///
    /// # Returns
    ///
    /// a new `GLWindow` using `display`'s connection
    pub fn new<P: IsA<GLDisplay>>(display: &P) -> GLWindow {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(gst_gl_sys::gst_gl_window_new(
                display.as_ref().to_glib_none().0,
            ))
        }
    }
}

unsafe impl Send for GLWindow {}
unsafe impl Sync for GLWindow {}

pub const NONE_GL_WINDOW: Option<&GLWindow> = None;

/// Trait containing all `GLWindow` methods.
///
/// # Implementors
///
/// [`GLWindow`](struct.GLWindow.html)
pub trait GLWindowExt: 'static {
    /// Checks if `self` controls the GL viewport.
    ///
    /// Feature: `v1_16`
    ///
    ///
    /// # Returns
    ///
    /// `true` if `self` controls the GL viewport, otherwise `false`
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn controls_viewport(&self) -> bool;

    /// Redraw the window contents. Implementations should invoke the draw callback.
    fn draw(&self);

    ///
    /// # Returns
    ///
    /// the `GLContext` associated with this `self`
    fn get_context(&self) -> Option<GLContext>;

    /// ## `width`
    /// resulting surface width
    /// ## `height`
    /// resulting surface height
    fn get_surface_dimensions(&self) -> (u32, u32);

    /// Tell a `self` that it should handle events from the window system. These
    /// events are forwarded upstream as navigation events. In some window systems
    /// events are not propagated in the window hierarchy if a client is listening
    /// for them. This method allows you to disable events handling completely
    /// from the `self`.
    /// ## `handle_events`
    /// a `gboolean` indicating if events should be handled or not.
    fn handle_events(&self, handle_events: bool);

    /// Queue resizing of `self`.
    fn queue_resize(&self);

    /// Quit the runloop's execution.
    fn quit(&self);

    /// Resize `self` to the given `width` and `height`.
    /// ## `width`
    /// new width
    /// ## `height`
    /// new height
    fn resize(&self, width: u32, height: u32);

    /// Start the execution of the runloop.
    fn run(&self);

    fn send_key_event(&self, event_type: &str, key_str: &str);

    fn send_mouse_event(&self, event_type: &str, button: i32, posx: f64, posy: f64);

    /// Set the preferred width and height of the window. Implementations are free
    /// to ignore this information.
    /// ## `width`
    /// new preferred width
    /// ## `height`
    /// new preferred height
    fn set_preferred_size(&self, width: i32, height: i32);

    /// Tell a `self` that it should render into a specific region of the window
    /// according to the `gst_video::VideoOverlay` interface.
    /// ## `x`
    /// x position
    /// ## `y`
    /// y position
    /// ## `width`
    /// width
    /// ## `height`
    /// height
    ///
    /// # Returns
    ///
    /// whether the specified region could be set
    fn set_render_rectangle(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
    ) -> Result<(), glib::error::BoolError>;

    /// Present the window to the screen.
    fn show(&self);

    /// Will be emitted when a key event is received by the GstGLwindow.
    /// ## `id`
    /// the name of the event
    /// ## `key`
    /// the id of the key pressed
    fn connect_key_event<F: Fn(&Self, &str, &str) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// Will be emitted when a mouse event is received by the GstGLwindow.
    /// ## `id`
    /// the name of the event
    /// ## `button`
    /// the id of the button
    /// ## `x`
    /// the x coordinate of the mouse event
    /// ## `y`
    /// the y coordinate of the mouse event
    fn connect_mouse_event<F: Fn(&Self, &str, i32, f64, f64) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<GLWindow>> GLWindowExt for O {
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn controls_viewport(&self) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_window_controls_viewport(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn draw(&self) {
        unsafe {
            gst_gl_sys::gst_gl_window_draw(self.as_ref().to_glib_none().0);
        }
    }

    fn get_context(&self) -> Option<GLContext> {
        unsafe {
            from_glib_full(gst_gl_sys::gst_gl_window_get_context(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_surface_dimensions(&self) -> (u32, u32) {
        unsafe {
            let mut width = mem::MaybeUninit::uninit();
            let mut height = mem::MaybeUninit::uninit();
            gst_gl_sys::gst_gl_window_get_surface_dimensions(
                self.as_ref().to_glib_none().0,
                width.as_mut_ptr(),
                height.as_mut_ptr(),
            );
            let width = width.assume_init();
            let height = height.assume_init();
            (width, height)
        }
    }

    fn handle_events(&self, handle_events: bool) {
        unsafe {
            gst_gl_sys::gst_gl_window_handle_events(
                self.as_ref().to_glib_none().0,
                handle_events.to_glib(),
            );
        }
    }

    fn queue_resize(&self) {
        unsafe {
            gst_gl_sys::gst_gl_window_queue_resize(self.as_ref().to_glib_none().0);
        }
    }

    fn quit(&self) {
        unsafe {
            gst_gl_sys::gst_gl_window_quit(self.as_ref().to_glib_none().0);
        }
    }

    fn resize(&self, width: u32, height: u32) {
        unsafe {
            gst_gl_sys::gst_gl_window_resize(self.as_ref().to_glib_none().0, width, height);
        }
    }

    fn run(&self) {
        unsafe {
            gst_gl_sys::gst_gl_window_run(self.as_ref().to_glib_none().0);
        }
    }

    fn send_key_event(&self, event_type: &str, key_str: &str) {
        unsafe {
            gst_gl_sys::gst_gl_window_send_key_event(
                self.as_ref().to_glib_none().0,
                event_type.to_glib_none().0,
                key_str.to_glib_none().0,
            );
        }
    }

    fn send_mouse_event(&self, event_type: &str, button: i32, posx: f64, posy: f64) {
        unsafe {
            gst_gl_sys::gst_gl_window_send_mouse_event(
                self.as_ref().to_glib_none().0,
                event_type.to_glib_none().0,
                button,
                posx,
                posy,
            );
        }
    }

    fn set_preferred_size(&self, width: i32, height: i32) {
        unsafe {
            gst_gl_sys::gst_gl_window_set_preferred_size(
                self.as_ref().to_glib_none().0,
                width,
                height,
            );
        }
    }

    fn set_render_rectangle(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                gst_gl_sys::gst_gl_window_set_render_rectangle(
                    self.as_ref().to_glib_none().0,
                    x,
                    y,
                    width,
                    height
                ),
                "Failed to set the specified region"
            )
        }
    }

    fn show(&self) {
        unsafe {
            gst_gl_sys::gst_gl_window_show(self.as_ref().to_glib_none().0);
        }
    }

    fn connect_key_event<F: Fn(&Self, &str, &str) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn key_event_trampoline<
            P,
            F: Fn(&P, &str, &str) + Send + Sync + 'static,
        >(
            this: *mut gst_gl_sys::GstGLWindow,
            id: *mut libc::c_char,
            key: *mut libc::c_char,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GLWindow>,
        {
            let f: &F = &*(f as *const F);
            f(
                &GLWindow::from_glib_borrow(this).unsafe_cast(),
                &GString::from_glib_borrow(id),
                &GString::from_glib_borrow(key),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"key-event\0".as_ptr() as *const _,
                Some(transmute(key_event_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_mouse_event<F: Fn(&Self, &str, i32, f64, f64) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn mouse_event_trampoline<
            P,
            F: Fn(&P, &str, i32, f64, f64) + Send + Sync + 'static,
        >(
            this: *mut gst_gl_sys::GstGLWindow,
            id: *mut libc::c_char,
            button: libc::c_int,
            x: libc::c_double,
            y: libc::c_double,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GLWindow>,
        {
            let f: &F = &*(f as *const F);
            f(
                &GLWindow::from_glib_borrow(this).unsafe_cast(),
                &GString::from_glib_borrow(id),
                button,
                x,
                y,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"mouse-event\0".as_ptr() as *const _,
                Some(transmute(mouse_event_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }
}