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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Provides userspace with access to the touch panel.
//!
//! Usage
//! -----
//!
//! You need a touch that provides the `hil::touch::Touch` trait.
//! An optional gesture client and a screen can be connected to it.
//!
//! ```rust,ignore
//! let touch =
//!     components::touch::TouchComponent::new(board_kernel, ts, Some(ts), Some(screen)).finalize(());
//! ```

use core::cell::Cell;
use core::mem;

use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::hil::screen::ScreenRotation;
use kernel::hil::touch::{GestureEvent, TouchClient, TouchEvent, TouchStatus};
use kernel::processbuffer::WriteableProcessBuffer;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::{ErrorCode, ProcessId};

/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::Touch as usize;

/// Ids for read-write allow buffers
mod rw_allow {
    /// Allow a buffer for the multi touch. See header for format
    // Buffer data format
    //  0         1           2              4              6           7             8          ...
    // +---------+-----------+--------------+--------------+-----------+---------------+-------- ...
    // | id (u8) | type (u8) | x (u16)      | y (u16)      | size (u8) | pressure (u8) |         ...
    // +---------+-----------+--------------+--------------+-----------+---------------+-------- ...
    // | Touch 0                                                                       | Touch 1 ...
    pub const EVENTS: usize = 2;
    /// The number of allow buffers the kernel stores for this grant
    pub const COUNT: u8 = 3;
}

fn touch_status_to_number(status: &TouchStatus) -> usize {
    match status {
        TouchStatus::Released => 0,
        TouchStatus::Pressed => 1,
        TouchStatus::Moved => 2,
        TouchStatus::Unstarted => 3,
    }
}

pub struct App {
    ack: bool,
    dropped_events: usize,
    x: u16,
    y: u16,
    status: usize,
    touch_enable: bool,
    multi_touch_enable: bool,
}

impl Default for App {
    fn default() -> App {
        App {
            ack: true,
            dropped_events: 0,
            x: 0,
            y: 0,
            status: touch_status_to_number(&TouchStatus::Unstarted),
            touch_enable: false,
            multi_touch_enable: false,
        }
    }
}

pub struct Touch<'a> {
    touch: Option<&'a dyn hil::touch::Touch<'a>>,
    multi_touch: Option<&'a dyn hil::touch::MultiTouch<'a>>,
    /// Screen under the touch panel
    /// Most of the touch panels have a screen that can be rotated
    /// 90 deg (clockwise), 180 deg (upside-down), 270 deg(clockwise).
    /// The touch gets the rotation from the screen and
    /// updates the touch (x, y) position
    screen: Option<&'a dyn hil::screen::Screen<'a>>,
    apps: Grant<App, UpcallCount<3>, AllowRoCount<0>, AllowRwCount<{ rw_allow::COUNT }>>,
    screen_rotation_offset: Cell<ScreenRotation>,
}

impl<'a> Touch<'a> {
    pub fn new(
        touch: Option<&'a dyn hil::touch::Touch<'a>>,
        multi_touch: Option<&'a dyn hil::touch::MultiTouch<'a>>,
        screen: Option<&'a dyn hil::screen::Screen<'a>>,
        grant: Grant<App, UpcallCount<3>, AllowRoCount<0>, AllowRwCount<{ rw_allow::COUNT }>>,
    ) -> Touch<'a> {
        Touch {
            touch,
            multi_touch,
            screen,
            screen_rotation_offset: Cell::new(ScreenRotation::Normal),
            apps: grant,
        }
    }

    pub fn set_screen_rotation_offset(&self, screen_rotation_offset: ScreenRotation) {
        self.screen_rotation_offset.set(screen_rotation_offset);
    }

    fn touch_enable(&self) -> Result<(), ErrorCode> {
        let mut enabled = false;
        for app in self.apps.iter() {
            if app.enter(|app, _| app.touch_enable) {
                enabled = true;
                break;
            }
        }
        self.touch.map_or_else(
            || {
                // if there is no single touch device
                // try to use the multi touch device and
                // report only a single touch
                self.multi_touch
                    .map_or(Err(ErrorCode::NODEVICE), |multi_touch| {
                        if enabled {
                            multi_touch.enable()
                        } else {
                            multi_touch.disable()
                        }
                    })
            },
            |touch| {
                if enabled {
                    touch.enable()
                } else {
                    touch.disable()
                }
            },
        )
    }

    fn multi_touch_enable(&self) -> Result<(), ErrorCode> {
        let mut enabled = false;
        for app in self.apps.iter() {
            if app.enter(|app, _| app.multi_touch_enable) {
                enabled = true;
                break;
            }
        }
        self.multi_touch
            .map_or(Err(ErrorCode::NODEVICE), |multi_touch| {
                if enabled {
                    multi_touch.enable()
                } else {
                    multi_touch.disable()
                }
            })
    }

    /// Updates the (x, y) pf the touch event based on the
    /// screen rotation (if there si a screen)
    fn update_rotation(&self, touch_event: &mut TouchEvent) {
        if let Some(screen) = self.screen {
            let rotation = screen.get_rotation() + self.screen_rotation_offset.get();
            let (mut width, mut height) = screen.get_resolution();

            let (x, y) = match rotation {
                ScreenRotation::Rotated90 => {
                    mem::swap(&mut width, &mut height);
                    (touch_event.y, height as u16 - touch_event.x)
                }
                ScreenRotation::Rotated180 => {
                    (width as u16 - touch_event.x, height as u16 - touch_event.y)
                }
                ScreenRotation::Rotated270 => {
                    mem::swap(&mut width, &mut height);
                    (width as u16 - touch_event.y, touch_event.x)
                }
                _ => (touch_event.x, touch_event.y),
            };

            touch_event.x = x;
            touch_event.y = y;
        }
    }
}

impl<'a> hil::touch::TouchClient for Touch<'a> {
    fn touch_event(&self, mut event: TouchEvent) {
        // update rotation if there is a screen attached
        self.update_rotation(&mut event);
        // debug!(
        //     "touch {:?} x {} y {} size {:?} pressure {:?}",
        //     event.status, event.x, event.y, event.size, event.pressure
        // );
        for app in self.apps.iter() {
            app.enter(|app, kernel_data| {
                let event_status = touch_status_to_number(&event.status);
                if app.x != event.x || app.y != event.y || app.status != event_status {
                    app.x = event.x;
                    app.y = event.y;
                    app.status = event_status;

                    let pressure_size = match event.pressure {
                        Some(pressure) => (pressure as usize) << 16,
                        None => 0,
                    } | match event.size {
                        Some(size) => size as usize,
                        None => 0,
                    };
                    kernel_data
                        .schedule_upcall(
                            0,
                            (
                                event_status,
                                (event.x as usize) << 16 | event.y as usize,
                                pressure_size,
                            ),
                        )
                        .ok();
                }
            });
        }
    }
}

impl<'a> hil::touch::MultiTouchClient for Touch<'a> {
    fn touch_events(&self, touch_events: &[TouchEvent], num_events: usize) {
        let len = if touch_events.len() < num_events {
            touch_events.len()
        } else {
            num_events
        };
        if len > 0 {
            // report the first touch as single touch
            // for applications that only use single touches
            self.touch_event(touch_events[0]);
            // debug!("{} touch(es)", len);
            for app in self.apps.iter() {
                app.enter(|app, kernel_data| {
                    if app.ack {
                        app.dropped_events = 0;

                        let num = kernel_data
                            .get_readwrite_processbuffer(rw_allow::EVENTS)
                            .and_then(|events| {
                                events.mut_enter(|buffer| {
                                    let num = if buffer.len() / 8 < len {
                                        buffer.len() / 8
                                    } else {
                                        len
                                    };

                                    for event_index in 0..num {
                                        let mut event = touch_events[event_index];
                                        self.update_rotation(&mut event);
                                        let event_status = touch_status_to_number(&event.status);
                                        // debug!(
                                        //     " multitouch {:?} x {} y {} size {:?} pressure {:?}",
                                        //     event.status, event.x, event.y, event.size, event.pressure
                                        // );
                                        // one touch entry is 8 bytes long
                                        let offset = event_index * 8;
                                        if buffer.len() > event_index + 8 {
                                            buffer[offset].set(event.id as u8);
                                            buffer[offset + 1].set(event_status as u8);
                                            buffer[offset + 2].set((event.x & 0xFF) as u8);
                                            buffer[offset + 3].set(((event.x & 0xFFFF) >> 8) as u8);
                                            buffer[offset + 4].set((event.y & 0xFF) as u8);
                                            buffer[offset + 5].set(((event.y & 0xFFFF) >> 8) as u8);
                                            buffer[offset + 6].set(
                                                if let Some(size) = event.size {
                                                    size as u8
                                                } else {
                                                    0
                                                },
                                            );
                                            buffer[offset + 7].set(
                                                if let Some(pressure) = event.pressure {
                                                    pressure as u8
                                                } else {
                                                    0
                                                },
                                            );
                                        } else {
                                            break;
                                        }
                                    }
                                    num
                                })
                            })
                            .unwrap_or(0);
                        let dropped_events = app.dropped_events;
                        if num > 0 {
                            app.ack = false;
                            kernel_data
                                .schedule_upcall(
                                    2,
                                    (num, dropped_events, if num < len { len - num } else { 0 }),
                                )
                                .ok();
                        }
                    } else {
                        app.dropped_events += 1;
                    }
                });
            }
        }
    }
}

impl<'a> hil::touch::GestureClient for Touch<'a> {
    fn gesture_event(&self, event: GestureEvent) {
        for app in self.apps.iter() {
            app.enter(|_app, kernel_data| {
                let gesture_id = match event {
                    GestureEvent::SwipeUp => 1,
                    GestureEvent::SwipeDown => 2,
                    GestureEvent::SwipeLeft => 3,
                    GestureEvent::SwipeRight => 4,
                    GestureEvent::ZoomIn => 5,
                    GestureEvent::ZoomOut => 6,
                };
                kernel_data.schedule_upcall(1, (gesture_id, 0, 0)).ok();
            });
        }
    }
}

impl<'a> SyscallDriver for Touch<'a> {
    fn command(
        &self,
        command_num: usize,
        _data1: usize,
        _data2: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        match command_num {
            // driver existence check
            0 => CommandReturn::success(),

            // touch enable
            1 => {
                self.apps
                    .enter(processid, |app, _| {
                        app.touch_enable = true;
                    })
                    .unwrap_or(());
                let _ = self.touch_enable();
                CommandReturn::success()
            }

            // touch disable
            2 => {
                self.apps
                    .enter(processid, |app, _| {
                        app.touch_enable = false;
                    })
                    .unwrap_or(());
                let _ = self.touch_enable();
                CommandReturn::success()
            }

            // multi touch ack
            10 => {
                self.apps
                    .enter(processid, |app, _| {
                        app.ack = true;
                    })
                    .unwrap_or(());
                CommandReturn::success()
            }

            // multi touch enable
            11 => {
                self.apps
                    .enter(processid, |app, _| {
                        app.multi_touch_enable = true;
                    })
                    .unwrap_or(());
                let _ = self.multi_touch_enable();
                CommandReturn::success()
            }

            // multi touch disable
            12 => {
                self.apps
                    .enter(processid, |app, _| {
                        app.multi_touch_enable = false;
                    })
                    .unwrap_or(());
                let _ = self.multi_touch_enable();
                CommandReturn::success()
            }

            // number of touches
            100 => {
                let num_touches = if let Some(multi_touch) = self.multi_touch {
                    multi_touch.get_num_touches()
                } else {
                    usize::from(self.touch.is_some())
                };
                CommandReturn::success_u32(num_touches as u32)
            }

            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
        }
    }

    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
        self.apps.enter(processid, |_, _| {})
    }
}