1use crate::{ffi, VulkanDevice, VulkanDisplay};
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{connect_raw, SignalHandlerId},
11    translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16    #[doc(alias = "GstVulkanWindow")]
83    pub struct VulkanWindow(Object<ffi::GstVulkanWindow, ffi::GstVulkanWindowClass>) @extends gst::Object;
84
85    match fn {
86        type_ => || ffi::gst_vulkan_window_get_type(),
87    }
88}
89
90impl VulkanWindow {
91    pub const NONE: Option<&'static VulkanWindow> = None;
92
93    #[doc(alias = "gst_vulkan_window_new")]
100    pub fn new(display: &impl IsA<VulkanDisplay>) -> VulkanWindow {
101        skip_assert_initialized!();
102        unsafe {
103            from_glib_full(ffi::gst_vulkan_window_new(
104                display.as_ref().to_glib_none().0,
105            ))
106        }
107    }
108}
109
110unsafe impl Send for VulkanWindow {}
111unsafe impl Sync for VulkanWindow {}
112
113pub trait VulkanWindowExt: IsA<VulkanWindow> + 'static {
119    #[doc(alias = "gst_vulkan_window_close")]
121    fn close(&self) {
122        unsafe {
123            ffi::gst_vulkan_window_close(self.as_ref().to_glib_none().0);
124        }
125    }
126
127    #[doc(alias = "gst_vulkan_window_get_display")]
132    #[doc(alias = "get_display")]
133    fn display(&self) -> VulkanDisplay {
134        unsafe {
135            from_glib_full(ffi::gst_vulkan_window_get_display(
136                self.as_ref().to_glib_none().0,
137            ))
138        }
139    }
140
141    #[doc(alias = "gst_vulkan_window_get_presentation_support")]
151    #[doc(alias = "get_presentation_support")]
152    fn is_presentation_support(
153        &self,
154        device: &impl IsA<VulkanDevice>,
155        queue_family_idx: u32,
156    ) -> bool {
157        unsafe {
158            from_glib(ffi::gst_vulkan_window_get_presentation_support(
159                self.as_ref().to_glib_none().0,
160                device.as_ref().to_glib_none().0,
161                queue_family_idx,
162            ))
163        }
164    }
165
166    #[doc(alias = "gst_vulkan_window_get_surface_dimensions")]
182    #[doc(alias = "get_surface_dimensions")]
183    fn surface_dimensions(&self) -> (u32, u32) {
184        unsafe {
185            let mut width = std::mem::MaybeUninit::uninit();
186            let mut height = std::mem::MaybeUninit::uninit();
187            ffi::gst_vulkan_window_get_surface_dimensions(
188                self.as_ref().to_glib_none().0,
189                width.as_mut_ptr(),
190                height.as_mut_ptr(),
191            );
192            (width.assume_init(), height.assume_init())
193        }
194    }
195
196    #[doc(alias = "gst_vulkan_window_handle_events")]
204    fn handle_events(&self, handle_events: bool) {
205        unsafe {
206            ffi::gst_vulkan_window_handle_events(
207                self.as_ref().to_glib_none().0,
208                handle_events.into_glib(),
209            );
210        }
211    }
212
213    #[doc(alias = "gst_vulkan_window_open")]
218    fn open(&self) -> Result<(), glib::Error> {
219        unsafe {
220            let mut error = std::ptr::null_mut();
221            let is_ok = ffi::gst_vulkan_window_open(self.as_ref().to_glib_none().0, &mut error);
222            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
223            if error.is_null() {
224                Ok(())
225            } else {
226                Err(from_glib_full(error))
227            }
228        }
229    }
230
231    #[doc(alias = "gst_vulkan_window_redraw")]
233    fn redraw(&self) {
234        unsafe {
235            ffi::gst_vulkan_window_redraw(self.as_ref().to_glib_none().0);
236        }
237    }
238
239    #[doc(alias = "gst_vulkan_window_resize")]
247    fn resize(&self, width: i32, height: i32) {
248        unsafe {
249            ffi::gst_vulkan_window_resize(self.as_ref().to_glib_none().0, width, height);
250        }
251    }
252
253    #[doc(alias = "gst_vulkan_window_send_key_event")]
254    fn send_key_event(&self, event_type: &str, key_str: &str) {
255        unsafe {
256            ffi::gst_vulkan_window_send_key_event(
257                self.as_ref().to_glib_none().0,
258                event_type.to_glib_none().0,
259                key_str.to_glib_none().0,
260            );
261        }
262    }
263
264    #[doc(alias = "gst_vulkan_window_send_mouse_event")]
265    fn send_mouse_event(&self, event_type: &str, button: i32, posx: f64, posy: f64) {
266        unsafe {
267            ffi::gst_vulkan_window_send_mouse_event(
268                self.as_ref().to_glib_none().0,
269                event_type.to_glib_none().0,
270                button,
271                posx,
272                posy,
273            );
274        }
275    }
276
277    #[doc(alias = "close")]
283    fn connect_close<F: Fn(&Self) -> bool + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
284        unsafe extern "C" fn close_trampoline<
285            P: IsA<VulkanWindow>,
286            F: Fn(&P) -> bool + Send + Sync + 'static,
287        >(
288            this: *mut ffi::GstVulkanWindow,
289            f: glib::ffi::gpointer,
290        ) -> glib::ffi::gboolean {
291            let f: &F = &*(f as *const F);
292            f(VulkanWindow::from_glib_borrow(this).unsafe_cast_ref()).into_glib()
293        }
294        unsafe {
295            let f: Box_<F> = Box_::new(f);
296            connect_raw(
297                self.as_ptr() as *mut _,
298                c"close".as_ptr() as *const _,
299                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
300                    close_trampoline::<Self, F> as *const (),
301                )),
302                Box_::into_raw(f),
303            )
304        }
305    }
306
307    #[doc(alias = "draw")]
308    fn connect_draw<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
309        unsafe extern "C" fn draw_trampoline<
310            P: IsA<VulkanWindow>,
311            F: Fn(&P) + Send + Sync + 'static,
312        >(
313            this: *mut ffi::GstVulkanWindow,
314            f: glib::ffi::gpointer,
315        ) {
316            let f: &F = &*(f as *const F);
317            f(VulkanWindow::from_glib_borrow(this).unsafe_cast_ref())
318        }
319        unsafe {
320            let f: Box_<F> = Box_::new(f);
321            connect_raw(
322                self.as_ptr() as *mut _,
323                c"draw".as_ptr() as *const _,
324                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
325                    draw_trampoline::<Self, F> as *const (),
326                )),
327                Box_::into_raw(f),
328            )
329        }
330    }
331
332    #[doc(alias = "key-event")]
338    fn connect_key_event<F: Fn(&Self, &str, &str) + Send + Sync + 'static>(
339        &self,
340        f: F,
341    ) -> SignalHandlerId {
342        unsafe extern "C" fn key_event_trampoline<
343            P: IsA<VulkanWindow>,
344            F: Fn(&P, &str, &str) + Send + Sync + 'static,
345        >(
346            this: *mut ffi::GstVulkanWindow,
347            id: *mut std::ffi::c_char,
348            key: *mut std::ffi::c_char,
349            f: glib::ffi::gpointer,
350        ) {
351            let f: &F = &*(f as *const F);
352            f(
353                VulkanWindow::from_glib_borrow(this).unsafe_cast_ref(),
354                &glib::GString::from_glib_borrow(id),
355                &glib::GString::from_glib_borrow(key),
356            )
357        }
358        unsafe {
359            let f: Box_<F> = Box_::new(f);
360            connect_raw(
361                self.as_ptr() as *mut _,
362                c"key-event".as_ptr() as *const _,
363                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
364                    key_event_trampoline::<Self, F> as *const (),
365                )),
366                Box_::into_raw(f),
367            )
368        }
369    }
370
371    #[doc(alias = "mouse-event")]
381    fn connect_mouse_event<F: Fn(&Self, &str, i32, f64, f64) + Send + Sync + 'static>(
382        &self,
383        f: F,
384    ) -> SignalHandlerId {
385        unsafe extern "C" fn mouse_event_trampoline<
386            P: IsA<VulkanWindow>,
387            F: Fn(&P, &str, i32, f64, f64) + Send + Sync + 'static,
388        >(
389            this: *mut ffi::GstVulkanWindow,
390            id: *mut std::ffi::c_char,
391            button: std::ffi::c_int,
392            x: std::ffi::c_double,
393            y: std::ffi::c_double,
394            f: glib::ffi::gpointer,
395        ) {
396            let f: &F = &*(f as *const F);
397            f(
398                VulkanWindow::from_glib_borrow(this).unsafe_cast_ref(),
399                &glib::GString::from_glib_borrow(id),
400                button,
401                x,
402                y,
403            )
404        }
405        unsafe {
406            let f: Box_<F> = Box_::new(f);
407            connect_raw(
408                self.as_ptr() as *mut _,
409                c"mouse-event".as_ptr() as *const _,
410                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
411                    mouse_event_trampoline::<Self, F> as *const (),
412                )),
413                Box_::into_raw(f),
414            )
415        }
416    }
417
418    #[doc(alias = "resize")]
419    fn connect_resize<F: Fn(&Self, u32, u32) + Send + Sync + 'static>(
420        &self,
421        f: F,
422    ) -> SignalHandlerId {
423        unsafe extern "C" fn resize_trampoline<
424            P: IsA<VulkanWindow>,
425            F: Fn(&P, u32, u32) + Send + Sync + 'static,
426        >(
427            this: *mut ffi::GstVulkanWindow,
428            object: std::ffi::c_uint,
429            p0: std::ffi::c_uint,
430            f: glib::ffi::gpointer,
431        ) {
432            let f: &F = &*(f as *const F);
433            f(
434                VulkanWindow::from_glib_borrow(this).unsafe_cast_ref(),
435                object,
436                p0,
437            )
438        }
439        unsafe {
440            let f: Box_<F> = Box_::new(f);
441            connect_raw(
442                self.as_ptr() as *mut _,
443                c"resize".as_ptr() as *const _,
444                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
445                    resize_trampoline::<Self, F> as *const (),
446                )),
447                Box_::into_raw(f),
448            )
449        }
450    }
451
452    #[doc(alias = "display")]
453    fn connect_display_notify<F: Fn(&Self) + Send + Sync + 'static>(
454        &self,
455        f: F,
456    ) -> SignalHandlerId {
457        unsafe extern "C" fn notify_display_trampoline<
458            P: IsA<VulkanWindow>,
459            F: Fn(&P) + Send + Sync + 'static,
460        >(
461            this: *mut ffi::GstVulkanWindow,
462            _param_spec: glib::ffi::gpointer,
463            f: glib::ffi::gpointer,
464        ) {
465            let f: &F = &*(f as *const F);
466            f(VulkanWindow::from_glib_borrow(this).unsafe_cast_ref())
467        }
468        unsafe {
469            let f: Box_<F> = Box_::new(f);
470            connect_raw(
471                self.as_ptr() as *mut _,
472                c"notify::display".as_ptr() as *const _,
473                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
474                    notify_display_trampoline::<Self, F> as *const (),
475                )),
476                Box_::into_raw(f),
477            )
478        }
479    }
480}
481
482impl<O: IsA<VulkanWindow>> VulkanWindowExt for O {}