use wayland_sys::server::{wl_display, wl_resource, wl_client};
use libc::{uintptr_t, size_t};
use std::ptr;
use types::{Size, Geometry, Point};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct WlcResource(uintptr_t);
#[cfg_attr(feature = "static-wlc", link(name = "wlc", kind = "static"))]
#[cfg_attr(not(feature = "static-wlc"), link(name = "wlc"))]
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; pub fn wlc_surface_get_wl_resource(resource: uintptr_t) -> *mut wl_resource;
pub fn wlc_view_get_wl_client(handle: uintptr_t) -> *mut wl_client;
}
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 {
fn from(ptr: uintptr_t) -> WlcResource {
WlcResource(ptr)
}
}
impl Into<WlcResource> for wl_resource {
fn into(self) -> WlcResource {
unsafe { WlcResource(wlc_resource_from_wl_surface_resource(&self)) }
}
}
impl WlcResource {
pub fn get_surface_size(self) -> Size {
unsafe { *wlc_surface_get_size(self.0).clone() }
}
pub fn get_raw(self) -> uintptr_t {
self.0
}
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
}
}
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
}
}