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
use std::os::raw::c_void;
use std::ops::Deref;

use wayland_sys::egl::*;
use sys::wayland::client::WlSurface;
use Proxy;

/// Checks if the wayland-egl lib is available and can be used
///
/// Trying to create an `WlEglSurface` while this function returns
/// `false` will result in a panic.
pub fn is_available() -> bool {
    is_lib_available()
}

unsafe impl Send for WlEglSurface {}
unsafe impl Sync for WlEglSurface {}

pub struct WlEglSurface {
    ptr: *mut wl_egl_window,
    surface: WlSurface
}

impl WlEglSurface {
    pub fn new(surface: WlSurface, width: i32, height: i32) -> WlEglSurface {
        let ptr = unsafe { ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_create,
            surface.ptr(), width, height) };
        WlEglSurface {
            ptr: ptr,
            surface: surface
        }
    }

    pub fn destroy(mut self) -> WlSurface {
        unsafe { ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_destroy, self.ptr); }
        let surface = ::std::mem::replace(&mut self.surface, unsafe { ::std::mem::uninitialized() });
        ::std::mem::forget(self);
        surface
    }

    pub fn get_size(&self) -> (i32, i32) {
        let mut w = 0i32;
        let mut h = 0i32;
        unsafe { ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_get_attached_size,
            self.ptr, &mut w as *mut i32, &mut h as *mut i32); }
        (w, h)
    }

    pub fn resize(&self, width: i32, height: i32, dx: i32, dy: i32) {
        unsafe { ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_resize,
            self.ptr, width, height, dx, dy) }
    }

    pub fn egl_surface_ptr(&self) -> *const c_void {
        self.ptr as *const c_void
    }

    /// DEPRECATED
    pub unsafe fn egl_surfaceptr(&self) -> *mut c_void {
        self.ptr as *mut c_void
    }
}

impl Drop for WlEglSurface {
    fn drop(&mut self) {
        unsafe { ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_destroy, self.ptr); }
    }
}

impl Deref for WlEglSurface {
    type Target = WlSurface;
    fn deref(&self) -> &WlSurface {
        &self.surface
    }
}