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
use core::From;
use core::compositor::Compositor;

use ffi::interfaces::compositor::wl_compositor_create_region;
use ffi::interfaces::region::{wl_region, wl_region_destroy, wl_region_add, wl_region_subtract};
use ffi::FFI;

/// Region represent a set of pixel.
///
/// They are a way to selecta fraciton of the pixels of a surface (in
/// a similar way of the 'select' tool of a drawing software).
///
/// They are created independently of the Surface, and then attached to it.
/// (see the the documentation of Surface for methos requiring a Region)
pub struct Region {
    _compositor: Compositor,
    ptr: *mut wl_region
}

// Region is self owned
unsafe impl Send for Region {}
// The wayland library guaranties this.
unsafe impl Sync for Region {}

impl Region {
    /// Adds given rectangle to the region.
    ///
    /// (x, y) are he coordinate of the top-left corner.
    pub fn add(&self, x: i32, y: i32, width: i32, height: i32) {
        unsafe { wl_region_add(self.ptr, x, y, width, height) }
    }

    /// Subtract given rectangle from the region.
    ///
    /// (x, y) are he coordinate of the top-left corner.
    pub fn subtract(&self, x: i32, y: i32, width: i32, height: i32) {
        unsafe { wl_region_subtract(self.ptr, x, y, width, height) }
    }
}

impl From<Compositor> for Region {
    fn from(compositor: Compositor) -> Region {
        let ptr = unsafe { wl_compositor_create_region(compositor.ptr_mut()) };
        Region {
            _compositor: compositor,
            ptr: ptr
        }
    }
}

impl Drop for Region {
    fn drop(&mut self) {
        unsafe { wl_region_destroy(self.ptr) };
    }
}

impl FFI for Region {
    type Ptr = wl_region;

    fn ptr(&self) -> *const wl_region {
        self.ptr as *const wl_region
    }

    unsafe fn ptr_mut(&self) -> *mut wl_region {
        self.ptr
    }
}