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
//! # The wlc-wayland feature (dummy form)
use wayland_sys::server::{wl_display, wl_resource};

use libc::{uintptr_t, size_t};

use std::ptr;

use types::{Size, Geometry, Point};

/// ## Requires `wlc-wayland` feature
///
/// A wlc resource for Wayland interop
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct WlcResource(uintptr_t);

/// Functions defined in wlc-wayland.h
extern "C" {
    pub fn wlc_get_wl_display() -> *mut wl_display;
    pub fn wlc_resource_from_wl_surface_resource(resource: *const wl_resource) -> uintptr_t;
    pub fn wlc_surface_get_size(resource: uintptr_t) -> *const Size;
    pub fn wlc_surface_get_subsurfaces(parent: uintptr_t, out_size: *mut size_t)
                                       -> *const uintptr_t;
    pub fn wlc_get_subsurface_geometry(surface: uintptr_t, out_geo: *mut Geometry);
    pub fn wlc_view_get_surface(view: uintptr_t) -> uintptr_t; // returns wlc_resource
    pub fn wlc_surface_get_wl_resource(resource: uintptr_t) -> *mut wl_resource;
}

/// Get the wayland display for the current session.
pub fn get_display() -> *mut wl_display {
    unsafe { wlc_get_wl_display() }
}

impl Into<*mut wl_resource> for WlcResource {
    fn into(self) -> *mut wl_resource {
        unsafe {wlc_surface_get_wl_resource(self.0) }
    }
}

impl From<uintptr_t> for WlcResource {
    /// ## Requires `wlc-wayland` feature
    ///
    /// Creates a new WlcResource from the given pointer.
    fn from(ptr: uintptr_t) -> WlcResource {
        WlcResource(ptr)
    }
}

impl Into<WlcResource> for wl_resource {
    /// ## Requires `wlc-wayland` feature
    ///
    /// Creates a new WlResource (wayland resource) from a WlcResource
    fn into(self) -> WlcResource {
        unsafe { WlcResource(wlc_resource_from_wl_surface_resource(&self)) }
    }
}

impl WlcResource {
    /// # Requires `wlc-wayland` feature
    ///
    /// Gets the size of this surface
    pub fn get_surface_size(self) -> Size {
        unsafe { *wlc_surface_get_size(self.0).clone() }
    }

    /// Gets the inner uintptr_t value that resource uses.
    pub fn get_raw(self) -> uintptr_t {
        self.0
    }

    /// ## Requires `wlc-wayland` feature
    ///
    /// Gets a list of subsurfaces from the given view
    pub fn get_subsurfaces(self) -> Vec<WlcResource> {
        unsafe {
            let mut out_memb: size_t = 0;
            let subs = wlc_surface_get_subsurfaces(self.0, &mut out_memb as *mut usize);
            if subs.is_null() {
                return Vec::new()
            }
            let mut result = Vec::with_capacity(out_memb);
            for index in 0isize .. out_memb as isize {
                result.push(WlcResource::from(ptr::read(subs.offset(index))))
            }
            return result
        }
    }

    /// # Requires `wlc-wayland` feature
    ///
    /// Gets the subsurface geometry of this WlcResource
    pub fn get_subsurface_geometry(self) -> Geometry {
        let mut geo = Geometry {
            origin: Point { x: 0, y: 0},
            size: Size { w: 0, h: 0}
        };
        unsafe {
            wlc_get_subsurface_geometry(self.0, &mut geo as *mut Geometry);
        }
        geo
    }
}