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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::ffi::CStr;
use std::sync::{Arc, Mutex};

use libc::{c_void, c_char};

use core::{From, Registry};
use core::compositor::WSurface;
use core::seat::{Pointer, Keyboard, Touch};

use ffi::interfaces::seat::{wl_seat, wl_seat_destroy, wl_seat_listener, wl_seat_add_listener};
use ffi::enums::{SeatCapability, CAPABILITY_POINTER, CAPABILITY_KEYBOARD, CAPABILITY_TOUCH};
use ffi::{FFI, Bind, abi};

struct SeatData {
    name: String,
    pointer: bool,
    keyboard: bool,
    touch: bool
}

impl SeatData {
    fn new() -> SeatData {
        SeatData {
            name: String::new(),
            pointer: false,
            keyboard: false,
            touch: false
        }
    }

    fn set_caps(&mut self, caps: SeatCapability) {
        self.pointer = caps.intersects(CAPABILITY_POINTER);
        self.keyboard = caps.intersects(CAPABILITY_KEYBOARD);
        self.touch = caps.intersects(CAPABILITY_TOUCH);
    }

    fn set_name(&mut self, name: String) {
        self.name = name;
    }
}

/// The data used by the listener callbacks.
struct SeatListener {
    /// Handler of the "new global object" event
    capabilities_handler: Box<Fn(SeatCapability, &mut SeatData)>,
    /// Handler of the "removed global handler" event
    name_handler: Box<Fn(&[u8], &mut SeatData)>,
    /// access to the data
    pub data: Mutex<SeatData>
}

impl SeatListener {
    fn default_handlers(data: SeatData) -> SeatListener {
        SeatListener {
            capabilities_handler: Box::new(move |caps, data| {
                data.set_caps(caps);
            }),
            name_handler: Box::new(move |name, data| {
                data.set_name(String::from_utf8_lossy(name).into_owned());
            }),
            data: Mutex::new(data)
        }
    }
}

struct InternalSeat {
    _registry: Registry,
    ptr: *mut wl_seat,
    listener: Box<SeatListener>
}

// InternalSeat is self owning
unsafe impl Send for InternalSeat {}

/// A global wayland Seat.
///
/// This structure is a handle to a wayland seat, which can up to a pointer, a keyboard
/// and a touch device.
///
/// Like other global objects, this handle can be cloned.
#[derive(Clone)]
pub struct Seat {
    internal: Arc<Mutex<InternalSeat>>
}

impl Seat {
    pub fn get_pointer(&self) -> Option<Pointer<WSurface>> {
        // avoid deadlock
        let has_pointer = {
            let internal = self.internal.lock().unwrap();
            let data = internal.listener.data.lock().unwrap();
            data.pointer
        };
        if has_pointer {
            Some(From::from(self.clone()))
        } else {
            None
        }
    }

    pub fn get_keyboard(&self) -> Option<Keyboard> {
        // avoid deadlock
        let has_keyboard = {
            let internal = self.internal.lock().unwrap();
            let data = internal.listener.data.lock().unwrap();
            data.keyboard
        };
        if has_keyboard {
            Some(From::from(self.clone()))
        } else {
            None
        }
    }

    pub fn get_touch(&self) -> Option<Touch> {
        // avoid deadlock
        let has_touch = {
            let internal = self.internal.lock().unwrap();
            let data = internal.listener.data.lock().unwrap();
            data.touch
        };
        if has_touch {
            Some(From::from(self.clone()))
        } else {
            None
        }
    }
}


impl Bind<Registry> for Seat {

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

    unsafe fn wrap(ptr: *mut wl_seat, registry: Registry) -> Seat {
        let listener_data = SeatListener::default_handlers(SeatData::new());
        let s = Seat {
            internal: Arc::new(Mutex::new(InternalSeat {
                _registry: registry,
                ptr: ptr,
                listener: Box::new(listener_data)
            }))
        };
        {
            let internal = s.internal.lock().unwrap();
            wl_seat_add_listener(
                internal.ptr,
                &SEAT_LISTENER as *const _,
                &*internal.listener as *const _ as *mut _
            );
        }
        s
    }
}

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

impl FFI for Seat {
    type Ptr = wl_seat;

    fn ptr(&self) -> *const wl_seat {
        self.internal.lock().unwrap().ptr as *const wl_seat
    }

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


//
// C-wrappers for the callback closures, to send to wayland
//
extern "C" fn seat_capabilities_handler(data: *mut c_void,
                                        _registry: *mut wl_seat,
                                        capabilities: SeatCapability,
                                       ) {
    let listener = unsafe { &*(data as *const SeatListener) };
    let mut data = listener.data.lock().unwrap();
    (listener.capabilities_handler)(capabilities, &mut *data);
}

extern "C" fn seat_name_handler(data: *mut c_void,
                                _registry: *mut wl_seat,
                                name: *const c_char
                               ) {
    let listener = unsafe { &*(data as *const SeatListener) };
    let name_str = unsafe { CStr::from_ptr(name) };
    let mut data = listener.data.lock().unwrap();
    (listener.name_handler)(name_str.to_bytes(), &mut *data);
}

static SEAT_LISTENER: wl_seat_listener = wl_seat_listener {
    capabilities: seat_capabilities_handler,
    name: seat_name_handler
};