blob: 1d3c434f76f922bbea6f6aff8af5f9f02b59e906 [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
22pub use input::{DeviceId, MotionAction, MotionFlags};
23pub 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,
54 action: u32,
55 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070056 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000057 ) -> String;
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070058 fn reset_device(verifier: &mut InputVerifier, device_id: i32);
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000059 }
60
61 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
62 pub struct RustPointerProperties {
63 pub id: i32,
64 }
65}
66
67use crate::ffi::RustPointerProperties;
68
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070069fn create(name: String) -> Box<InputVerifier> {
70 Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents")))
71}
72
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000073fn process_movement(
74 verifier: &mut InputVerifier,
75 device_id: i32,
76 action: u32,
77 pointer_properties: &[RustPointerProperties],
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -070078 flags: u32,
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000079) -> String {
80 let result = verifier.process_movement(
81 DeviceId(device_id),
82 action,
83 pointer_properties,
84 MotionFlags::from_bits(flags).unwrap(),
85 );
86 match result {
87 Ok(()) => "".to_string(),
88 Err(e) => e,
89 }
90}
91
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -070092fn reset_device(verifier: &mut InputVerifier, device_id: i32) {
93 verifier.reset_device(DeviceId(device_id));
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000094}