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

use libc::c_void;

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

use ffi::FFI;
use ffi::abi::{wl_fixed_t, wl_fixed_to_double};
use ffi::interfaces::seat::wl_seat_get_touch;
use ffi::interfaces::touch::{wl_touch_listener, wl_touch, wl_touch_add_listener,
                             wl_touch_release, wl_touch_destroy};
use ffi::interfaces::surface::wl_surface;

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

#[inline]
pub fn wrap_touch_id(p: usize) -> TouchId {
    TouchId { p: p }
}

struct TouchListener {
    down_handler: Box<Fn(TouchId, Serial, u32, SurfaceId, i32, f64, f64) + 'static + Send + Sync>,
    up_handler: Box<Fn(TouchId, Serial, u32, i32) + 'static + Send + Sync>,
    motion_handler: Box<Fn(TouchId, u32, i32, f64, f64) + 'static + Send + Sync>,
    frame_handler: Box<Fn(TouchId) + 'static + Send + Sync>,
    cancel_handler: Box<Fn(TouchId) + 'static + Send + Sync>,
}

impl TouchListener {
    fn new() -> TouchListener {
        TouchListener {
            down_handler: Box::new(move |_,_,_,_,_,_,_| {}),
            up_handler: Box::new(move |_,_,_,_| {}),
            motion_handler: Box::new(move |_,_,_,_,_| {}),
            frame_handler: Box::new(move |_| {}),
            cancel_handler: Box::new(move |_| {})
        }
    }
}

//
// The public struct
//

pub struct Touch {
    seat: Seat,
    ptr: *mut wl_touch,
    listener: Box<TouchListener>
}

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

impl From<Seat> for Touch {
    fn from(seat: Seat) -> Touch {
        let ptr = unsafe { wl_seat_get_touch(seat.ptr_mut()) };
        let p = Touch {
            seat: seat,
            ptr: ptr,
            listener: Box::new(TouchListener::new())
        };
        unsafe {
            wl_touch_add_listener(
                p.ptr,
                &TOUCH_LISTENER as *const _,
                &*p.listener as *const _ as *mut _
            );
        }
        p
    }
}

impl Touch {
    /// Returns the unique \TouchId` associated with this touch device.
    ///
    /// This struct can be tested for equality, and will be provided in event callbacks
    /// as a mean to identify the toch device associated with the events.
    pub fn get_id(&self) -> TouchId {
        wrap_touch_id(self.ptr as usize)
    }

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

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

    /// Defines the action to be executed when a new touch point appears
    ///
    /// Arguments are:
    ///
    /// - Id of the touch
    /// - serial number of the event
    /// - timestamp with miliseconds of the event
    /// - id of the surface of the event
    /// - unique id for this touch point (will be used to identify it when moving
    ///   or when disappearing)
    /// - x coordinate of the touch point on the surface
    /// - y coordinate of the touch point on the surface
    pub fn set_down_action<F>(&mut self, f: F)
        where F: Fn(TouchId, Serial, u32, SurfaceId, i32, f64, f64) + 'static + Send + Sync
    {
        self.listener.down_handler = Box::new(f)
    }

    /// Defines the action to be executed when a touch point dissapears
    ///
    /// Arguments are:
    ///
    /// - Id of the touch
    /// - serial number of the event
    /// - timestamp with miliseconds of the event
    /// - unique id for this touch point (this id is then released and can be
    ///   used for new touch points)
    pub fn set_up_action<F>(&mut self, f: F)
        where F: Fn(TouchId, Serial, u32, i32) + 'static + Send + Sync
    {
        self.listener.up_handler = Box::new(f)
    }

    /// Defines the action to be executed a touch point moves
    ///
    /// Arguments are:
    ///
    /// - Id of the touch
    /// - timestamp with miliseconds of the event
    /// - unique id for this touch point 
    /// - new x coordinate of the touch point on the surface
    /// - new y coordinate of the touch point on the surface
    pub fn set_motion_action<F>(&mut self, f: F)
        where F: Fn(TouchId, u32, i32, f64, f64) + 'static + Send + Sync
    {
        self.listener.motion_handler = Box::new(f)
    }

    pub fn set_frame_action<F>(&mut self, f: F)
        where F: Fn(TouchId) + 'static + Send + Sync
    {
        self.listener.frame_handler = Box::new(f)
    }

    /// Defines the action to be executed when a touch sequence is cancelled.
    ///
    /// This happens when the compositer recognises the sequence as a special
    /// sequence that it will interpret itself.
    ///
    /// All current touch points are cancelled and their ids released (and thus
    /// can be used for new touch points).
    pub fn set_cancel_action<F>(&mut self, f: F)
        where F: Fn(TouchId) + 'static + Send + Sync
    {
        self.listener.cancel_handler = Box::new(f)
    }
}

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

impl FFI for Touch {
    type Ptr = wl_touch;

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

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


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

extern "C" fn touch_down_handler(data: *mut c_void,
                                 touch: *mut wl_touch,
                                 serial: u32,
                                 time: u32,
                                 surface: *mut wl_surface,
                                 id: i32,
                                 x: wl_fixed_t,
                                 y: wl_fixed_t
                                ) {
    let listener = unsafe { &*(data as *const TouchListener) };
    (*listener.down_handler)(
        wrap_touch_id(touch as usize),
        wrap_serial(serial),
        time,
        wrap_surface_id(surface as usize),
        id,
        wl_fixed_to_double(x),
        wl_fixed_to_double(y)
    );
}

extern "C" fn touch_up_handler(data: *mut c_void,
                               touch: *mut wl_touch,
                               serial: u32,
                               time: u32,
                               id: i32
                              ) {
    let listener = unsafe { &*(data as *const TouchListener) };
    (*listener.up_handler)(
        wrap_touch_id(touch as usize),
        wrap_serial(serial),
        time,
        id
    );
}

extern "C" fn touch_motion_handler(data: *mut c_void,
                                   touch: *mut wl_touch,
                                   time: u32,
                                   id: i32,
                                   x: wl_fixed_t,
                                   y: wl_fixed_t
                                  ) {
    let listener = unsafe { &*(data as *const TouchListener) };
    (*listener.motion_handler)(
        wrap_touch_id(touch as usize),
        time,
        id,
        wl_fixed_to_double(x),
        wl_fixed_to_double(y)
    );
}

extern "C" fn touch_frame_handler(data: *mut c_void,
                                  touch: *mut wl_touch
                                 ) {
    let listener = unsafe { &*(data as *const TouchListener) };
    (*listener.frame_handler)(
        wrap_touch_id(touch as usize)
    );
}

extern "C" fn touch_cancel_handler(data: *mut c_void,
                                   touch: *mut wl_touch
                                  ) {
    let listener = unsafe { &*(data as *const TouchListener) };
    (*listener.cancel_handler)(
        wrap_touch_id(touch as usize),
    );
}

static TOUCH_LISTENER : wl_touch_listener = wl_touch_listener {
    down: touch_down_handler,
    up: touch_up_handler,
    motion : touch_motion_handler,
    frame: touch_frame_handler,
    cancel: touch_cancel_handler
};