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
use std::os::unix::io::AsRawFd;
use std::sync::{Arc, Mutex};

use libc::c_int;

use core::{From, Registry};
use core::shm::ShmPool;

use ffi::interfaces::shm::{wl_shm, wl_shm_destroy};
use ffi::{FFI, Bind, abi};

pub use ffi::enums::ShmFormat;

struct InternalShm {
    _registry: Registry,
    ptr: *mut wl_shm
}

// InternalShm is self-owned
unsafe impl Send for InternalShm {}

/// The shared memory controller.
///
/// This object can be queried for memory_pools used for the buffers.
///
/// Like other global objects, this handle can be cloned.
#[derive(Clone)]
pub struct Shm {
    internal: Arc<Mutex<InternalShm>>
}

impl Shm {
    /// Creates a shared memory pool from given file descriptor.
    ///
    /// The server will internally `mmap` the sepcified `size` number of bytes from
    /// this file descriptor.
    /// The created ShmPool will have access to these bytes exactly.
    pub fn pool_from_fd<F: AsRawFd>(&self, fd: &F, size: i32) -> ShmPool {
        From::from((self.clone(), fd.as_raw_fd(), size))
    }

    /// Creates a sahred memory pool from given raw file descriptor.
    ///
    /// Same as `pool_from_fd(..)` otherwise
    pub fn pool_from_raw_fd(&self, fd: c_int, size: i32) -> ShmPool {
        From::from((self.clone(), fd, size))
    }
}

impl Bind<Registry> for Shm {

    fn interface() -> &'static abi::wl_interface {
        #[cfg(feature = "dlopen")] use ffi::abi::WAYLAND_CLIENT_HANDLE;
        #[cfg(not(feature = "dlopen"))] use ffi::abi::wl_shm_interface;
        ffi_dispatch_static!(WAYLAND_CLIENT_HANDLE, wl_shm_interface)
    }

    unsafe fn wrap(ptr: *mut wl_shm, registry: Registry) -> Shm {
        Shm {
            internal: Arc::new(Mutex::new(InternalShm {
                _registry:registry,
                ptr: ptr
            }))
        }
    }
}

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

impl FFI for Shm {
    type Ptr = wl_shm;
   fn ptr(&self) -> *const wl_shm {
        self.internal.lock().unwrap().ptr as *const wl_shm
    }

    unsafe fn ptr_mut(&self) -> *mut wl_shm {
        self.internal.lock().unwrap().ptr
    }
}