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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::slice;
use std::sync::Mutex;

use libc::c_void;

use core::From;
use core::seat::Seat;
use core::ids::{SurfaceId, wrap_surface_id, Serial, wrap_serial};

use ffi::abi::wl_array;
use ffi::interfaces::keyboard::{wl_keyboard, wl_keyboard_destroy, wl_keyboard_release,
                                wl_keyboard_listener, wl_keyboard_add_listener};
use ffi::interfaces::surface::wl_surface;
use ffi::interfaces::seat::wl_seat_get_keyboard;
pub use ffi::enums::KeymapFormat;
pub use ffi::enums::KeyState;

use ffi::FFI;

/// An opaque unique identifier to a keyboard, can be tested for equality.
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct KeyboardId {
    p: usize
}

#[inline]
pub fn wrap_keyboard_id(p: usize) -> KeyboardId {
    KeyboardId { p: p }
}

struct KeyboardData {
    format: KeymapFormat,
    keymap: Option<(i32, u32)>,
    repeat_info: Option<(i32, i32)>
}

impl KeyboardData {
    fn new() -> KeyboardData {
        KeyboardData {
            format: KeymapFormat::NoKeymap,
            keymap: None,
            repeat_info: None
        }
    }
}

struct KeyboardListener {
    keymap_handler: Box<Fn(KeymapFormat, i32, u32, &mut KeyboardData) + 'static + Send + Sync>,
    repeat_info_handler: Box<Fn(i32, i32, &mut KeyboardData) + 'static + Send + Sync>,
    enter_handler: Box<Fn(KeyboardId, Serial, SurfaceId, &[u32]) + 'static + Send + Sync>,
    leave_handler: Box<Fn(KeyboardId, Serial, SurfaceId) + 'static + Send + Sync>,
    key_handler: Box<Fn(KeyboardId, Serial, u32, u32, KeyState) + 'static + Send + Sync>,
    modifiers_handler: Box<Fn(KeyboardId, Serial, u32, u32, u32, u32) + 'static + Send + Sync>,
    data: Mutex<Box<KeyboardData>>
}

impl KeyboardListener {
    pub fn new(data: KeyboardData) -> KeyboardListener {
        KeyboardListener {
            keymap_handler: Box::new(move |kf, fd, s, data| {
                data.format = kf;
                data.keymap = Some((fd, s));
            }),
            repeat_info_handler: Box::new(move |r, d, data| {
                data.repeat_info = Some((r, d));
            }),
            enter_handler: Box::new(move |_, _, _, _| {}),
            leave_handler: Box::new(move |_, _, _| {}),
            key_handler: Box::new(move |_, _, _, _, _| {}),
            modifiers_handler: Box::new(move |_, _, _, _, _, _| {}),
            data: Mutex::new(Box::new(data))
        }
    }
}

/// A keyboard interface.
///
/// This struct is a handle to the keyboard interface of a given `Seat`.It allows
/// you to change the keyboard display and handle all keyboard-related events.
///
/// The events are handled in a callback way. Each callback are provided
/// a `KeyboardId` identifying the keyboard. This is to allow specific situation where
/// you need the keyboard to not be borrowed (to change its surface for example) and
/// have more than one cursor.
pub struct Keyboard {
    seat: Seat,
    ptr: *mut wl_keyboard,
    listener: Box<KeyboardListener>,
}

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

impl From<Seat> for Keyboard {
    fn from(seat: Seat) -> Keyboard {
        let ptr = unsafe { wl_seat_get_keyboard(seat.ptr_mut()) };
        let p = Keyboard {
            seat: seat,
            ptr: ptr,
            listener: Box::new(KeyboardListener::new(KeyboardData::new()))
        };
        unsafe {
            wl_keyboard_add_listener(
                p.ptr,
                &KEYBOARD_LISTENER as *const _,
                &*p.listener as *const _ as *mut _
            );
        }
        p
    }
}

impl Keyboard {
    /// Returns the unique `KeyboardId` associated to this keyboard.
    ///
    /// This struct can be tested for equality, and will be provided in event callbacks
    /// as a mean to identify the keyboard associated with the events.
    pub fn get_id(&self) -> KeyboardId {
        wrap_keyboard_id(self.ptr as usize)
    }

    /// Get access to the seat associated with this keyboard
    pub fn get_seat(&self) -> &Seat {
        &self.seat
    }

    /// Provides the file descriptor giving access to the keymap definition
    /// currently used by the compositor for this keyboard.
    ///
    /// Provided data is `(fd, size)`. `size` being the number of bytes to read from `fd`.
    ///
    /// It gives away ownership of the Fd, and thus other calls of this methos will
    /// return `None`.
    ///
    /// Will also return `None` if the event providing this information has not been received
    /// yet. In such case you'll need to wait for more events to be processed. Using
    /// `Display::dispatch_pending()` for example.
    pub fn keymap_fd(&self) -> Option<(i32, u32)> {
        let mut data = self.listener.data.lock().unwrap();
        data.keymap.take()
    }

    /// Provides the repeat information of this keayboard.
    ///
    /// Provided data is `(rate, delay)`. `rate` is the rate of repeating keys, in characters
    /// per second, and `delay` is the delay in miliseconds between the moment the key is
    /// pressed and the moment it starts repeating.
    ///
    /// Will return `None` if the event providing this information has not been received
    /// yet. In such case you'll need to wait for more events to be processed. Using
    /// `Display::dispatch_pending()` for example.
    pub fn repeat_info(&self) -> Option<(i32, i32)> {
        let data = self.listener.data.lock().unwrap();
        data.repeat_info
    }

    pub fn release(self) {
        unsafe {
            wl_keyboard_release(self.ptr);
            ::std::mem::forget(self);
        }
    }

    /// Defines the action to be executed when a surface gains keyboard focus.
    ///
    /// Arguments are:
    ///
    /// - Id of the keyboard
    /// - Id of the surface
    /// - a slice of the keycodes of the currenlty pressed keys
    pub fn set_enter_action<F>(&mut self, f: F)
        where F: Fn(KeyboardId, Serial, SurfaceId, &[u32]) + 'static + Send + Sync
    {
        self.listener.enter_handler = Box::new(f)
    }

    /// Defines the action to be executed when a surface loses keyboard focus.
    ///
    /// This event is generated *before* the `enter` event is generated for the new
    /// surface the focus goes to.
    ///
    /// Arguments are:
    ///
    /// - Id of the keyboard
    /// - Id of the surface
    pub fn set_leave_action<F>(&mut self, f: F)
        where F: Fn(KeyboardId, Serial, SurfaceId) + 'static + Send + Sync
    {
        self.listener.leave_handler = Box::new(f)
    }

    /// Defines the action to be executed when a keystroke is done.
    ///
    /// Arguments are:
    ///
    /// - Id of the Keyboard
    /// - time of event (timestamp with milisecond granularity)
    /// - raw keycode of the key
    /// - new key status
    pub fn set_key_action<F>(&mut self, f: F)
        where F: Fn(KeyboardId, Serial, u32, u32, KeyState) + 'static + Send + Sync
    {
        self.listener.key_handler = Box::new(f);
    }

    /// Defines the action to be executed when a modifier is changed.
    ///
    /// This event providesthe new state of the modifiers.
    ///
    /// Arguments are:
    ///
    /// - Id of the Keyboard
    /// - mods_depressed
    /// - mods_latched
    /// - mods_locked
    /// - group
    pub fn set_modifiers_action<F>(&mut self, f: F)
        where F: Fn(KeyboardId, Serial, u32, u32, u32, u32) + 'static + Send + Sync
    {
        self.listener.modifiers_handler = Box::new(f);
    }
}

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

impl FFI for Keyboard {
    type Ptr = wl_keyboard;

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

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

//
// C-wrappers for the callback closures, to send to wayland
//

extern "C" fn keyboard_keymap_handler(data: *mut c_void,
                                      _keyboard: *mut wl_keyboard,
                                      format: KeymapFormat,
                                      fd: i32,
                                      size: u32
                                     ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    let mut data = listener.data.lock().unwrap();
    (*listener.keymap_handler)(format, fd, size, &mut *data);
}

extern "C" fn keyboard_enter_handler(data: *mut c_void,
                                     keyboard: *mut wl_keyboard,
                                     serial: u32,
                                     surface: *mut wl_surface,
                                     keys: *mut wl_array,
                                   ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    let keys = unsafe { slice::from_raw_parts((*keys).data as *const _, (*keys).size as usize) };
    (*listener.enter_handler)(
        wrap_keyboard_id(keyboard as usize),
        wrap_serial(serial),
        wrap_surface_id(surface as usize),
        keys
    );
}

extern "C" fn keyboard_leave_handler(data: *mut c_void,
                                     keyboard: *mut wl_keyboard,
                                     serial: u32,
                                     surface: *mut wl_surface
                                    ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    (*listener.leave_handler)(
        wrap_keyboard_id(keyboard as usize),
        wrap_serial(serial),
        wrap_surface_id(surface as usize)
    );
}

extern "C" fn keyboard_key_handler(data: *mut c_void,
                                   keyboard: *mut wl_keyboard,
                                   serial: u32,
                                   time: u32,
                                   key: u32,
                                   state: KeyState
                                  ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    (*listener.key_handler)(
        wrap_keyboard_id(keyboard as usize),
        wrap_serial(serial),
        time,
        key,
        state
    );
}
extern "C" fn keyboard_modifiers_handler(data: *mut c_void,
                                         keyboard: *mut wl_keyboard,
                                         serial: u32,
                                         mods_depressed: u32,
                                         mods_latched: u32,
                                         mods_locked: u32,
                                         group: u32
                                        ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    (*listener.modifiers_handler)(
        wrap_keyboard_id(keyboard as usize),
        wrap_serial(serial),
        mods_depressed,
        mods_latched,
        mods_locked,
        group
    );

}
extern "C" fn keyboard_repeat_info_handler(data: *mut c_void,
                                           _keyboard: *mut wl_keyboard,
                                           rate: i32,
                                           delay: i32
                                           ) {
    let listener = unsafe { &*(data as *const KeyboardListener) };
    let mut data = listener.data.lock().unwrap();
    (*listener.repeat_info_handler)(rate, delay, &mut *data);
}

static KEYBOARD_LISTENER: wl_keyboard_listener = wl_keyboard_listener {
    keymap: keyboard_keymap_handler,
    enter: keyboard_enter_handler,
    leave: keyboard_leave_handler,
    key: keyboard_key_handler,
    modifiers: keyboard_modifiers_handler,
    repeat_info: keyboard_repeat_info_handler
};