blob: 01d959942ca8b5c75aa5cdf86a256b45cfef1adb [file] [log] [blame]
Prabir Pradhan0762b1f2023-06-22 23:08:18 +00001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! The rust component of libinput.
18
19mod input;
20mod input_verifier;
21
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070022pub use input::{DeviceId, MotionAction, MotionFlags, Source};
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000023pub use input_verifier::InputVerifier;
24
25#[cxx::bridge(namespace = "android::input")]
Andrew Walbran37994222023-08-03 12:16:28 +000026#[allow(unsafe_op_in_unsafe_fn)]
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000027mod ffi {
28 #[namespace = "android"]
29 unsafe extern "C++" {
30 include!("ffi/FromRustToCpp.h");
31 fn shouldLog(tag: &str) -> bool;
32 }
33
34 #[namespace = "android::input::verifier"]
35 extern "Rust" {
36 /// Used to validate the incoming motion stream.
37 /// This class is not thread-safe.
38 /// State is stored in the "InputVerifier" object
39 /// that can be created via the 'create' method.
40 /// Usage:
41 ///
42 /// ```ignore
43 /// Box<InputVerifier> verifier = create("inputChannel name");
44 /// result = process_movement(verifier, ...);
45 /// if (result) {
46 /// crash(result.error_message());
47 /// }
48 /// ```
49 type InputVerifier;
50 fn create(name: String) -> Box<InputVerifier>;
51 fn process_movement(
52 verifier: &mut InputVerifier,
53 device_id: i32,
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070054 source: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000055 action: u32,
56 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070057 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000058 ) -> String;
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070059 fn reset_device(verifier: &mut InputVerifier, device_id: i32);
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000060 }
61
62 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
63 pub struct RustPointerProperties {
64 pub id: i32,
65 }
66}
67
68use crate::ffi::RustPointerProperties;
69
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070070fn create(name: String) -> Box<InputVerifier> {
71 Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents")))
72}
73
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000074fn process_movement(
75 verifier: &mut InputVerifier,
76 device_id: i32,
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070077 source: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000078 action: u32,
79 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070080 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000081) -> String {
82 let result = verifier.process_movement(
83 DeviceId(device_id),
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070084 Source::from_bits(source).unwrap(),
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000085 action,
86 pointer_properties,
87 MotionFlags::from_bits(flags).unwrap(),
88 );
89 match result {
90 Ok(()) => "".to_string(),
91 Err(e) => e,
92 }
93}
94
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070095fn reset_device(verifier: &mut InputVerifier, device_id: i32) {
96 verifier.reset_device(DeviceId(device_id));
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000097}