blob: fb3f520c019900722d036663585ee77c63f1c70f [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 {
Linnan Li13ccabe2024-04-19 17:14:43 +000082 let motion_flags = MotionFlags::from_bits(flags);
83 if motion_flags.is_none() {
84 panic!(
85 "The conversion of flags 0x{:08x} failed, please check if some flags have not been \
86 added to MotionFlags.",
87 flags
88 );
89 }
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000090 let result = verifier.process_movement(
91 DeviceId(device_id),
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070092 Source::from_bits(source).unwrap(),
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000093 action,
94 pointer_properties,
Linnan Li13ccabe2024-04-19 17:14:43 +000095 motion_flags.unwrap(),
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000096 );
97 match result {
98 Ok(()) => "".to_string(),
99 Err(e) => e,
100 }
101}
102
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -0700103fn reset_device(verifier: &mut InputVerifier, device_id: i32) {
104 verifier.reset_device(DeviceId(device_id));
Prabir Pradhan0762b1f2023-06-22 23:08:18 +0000105}