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
use core::FromOpt;
use core::shm::ShmPool;

use ffi::interfaces::shm_pool::wl_shm_pool_create_buffer;
use ffi::interfaces::buffer::{wl_buffer, wl_buffer_destroy};
use ffi::FFI;

/// A view into a memory pool.
///
/// A buffer represents a given view into a memory pool. They only
/// serve to notify the wayland server about how the contents of the
/// memory pool must be read. To actually modify the data, you need
/// to directly access the object you created the memory pool from.
pub struct Buffer {
    ptr: *mut wl_buffer
}

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

impl<'a> FromOpt<(&'a ShmPool, i32, i32, i32, i32, u32)> for Buffer {
    fn from((pool, offset, width, height, stride, format): (&'a ShmPool, i32, i32, i32, i32, u32))
            -> Option<Buffer> {
        let ptr = unsafe { wl_shm_pool_create_buffer(
            pool.ptr_mut(), offset, width, height, stride, format) };
        if ptr.is_null() {
            None
        } else {
            Some(Buffer {
                ptr: ptr
            })
        }
    }
}

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


impl FFI for Buffer {
    type Ptr = wl_buffer;

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

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