blob: abab3f9e7c328912463c6bdc3296c87079600ebb [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")]
Chris Wailesc98800e2024-09-05 13:31:13 -070026#[allow(clippy::needless_maybe_sized)]
Andrew Walbran37994222023-08-03 12:16:28 +000027#[allow(unsafe_op_in_unsafe_fn)]
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000028mod ffi {
29 #[namespace = "android"]
30 unsafe extern "C++" {
31 include!("ffi/FromRustToCpp.h");
32 fn shouldLog(tag: &str) -> bool;
33 }
34
35 #[namespace = "android::input::verifier"]
36 extern "Rust" {
37 /// Used to validate the incoming motion stream.
38 /// This class is not thread-safe.
39 /// State is stored in the "InputVerifier" object
40 /// that can be created via the 'create' method.
41 /// Usage:
42 ///
43 /// ```ignore
44 /// Box<InputVerifier> verifier = create("inputChannel name");
45 /// result = process_movement(verifier, ...);
46 /// if (result) {
47 /// crash(result.error_message());
48 /// }
49 /// ```
50 type InputVerifier;
51 fn create(name: String) -> Box<InputVerifier>;
52 fn process_movement(
53 verifier: &mut InputVerifier,
54 device_id: i32,
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070055 source: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000056 action: u32,
57 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070058 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000059 ) -> String;
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070060 fn reset_device(verifier: &mut InputVerifier, device_id: i32);
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000061 }
62
63 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
64 pub struct RustPointerProperties {
65 pub id: i32,
66 }
67}
68
69use crate::ffi::RustPointerProperties;
70
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070071fn create(name: String) -> Box<InputVerifier> {
72 Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents")))
73}
74
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000075fn process_movement(
76 verifier: &mut InputVerifier,
77 device_id: i32,
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070078 source: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000079 action: u32,
80 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070081 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000082) -> String {
83 let result = verifier.process_movement(
84 DeviceId(device_id),
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070085 Source::from_bits(source).unwrap(),
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000086 action,
87 pointer_properties,
88 MotionFlags::from_bits(flags).unwrap(),
89 );
90 match result {
91 Ok(()) => "".to_string(),
92 Err(e) => e,
93 }
94}
95
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070096fn reset_device(verifier: &mut InputVerifier, device_id: i32) {
97 verifier.reset_device(DeviceId(device_id));
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000098}