blob: 25b2ecbcda4220b04d22a531aef5110d61990342 [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")]
26mod ffi {
27 #[namespace = "android"]
28 unsafe extern "C++" {
29 include!("ffi/FromRustToCpp.h");
30 fn shouldLog(tag: &str) -> bool;
31 }
32
33 #[namespace = "android::input::verifier"]
34 extern "Rust" {
35 /// Used to validate the incoming motion stream.
36 /// This class is not thread-safe.
37 /// State is stored in the "InputVerifier" object
38 /// that can be created via the 'create' method.
39 /// Usage:
40 ///
41 /// ```ignore
42 /// Box<InputVerifier> verifier = create("inputChannel name");
43 /// result = process_movement(verifier, ...);
44 /// if (result) {
45 /// crash(result.error_message());
46 /// }
47 /// ```
48 type InputVerifier;
49 fn create(name: String) -> Box<InputVerifier>;
50 fn process_movement(
51 verifier: &mut InputVerifier,
52 device_id: i32,
53 action: u32,
54 pointer_properties: &[RustPointerProperties],
55 flags: i32,
56 ) -> String;
57 }
58
59 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
60 pub struct RustPointerProperties {
61 pub id: i32,
62 }
63}
64
65use crate::ffi::RustPointerProperties;
66
67fn process_movement(
68 verifier: &mut InputVerifier,
69 device_id: i32,
70 action: u32,
71 pointer_properties: &[RustPointerProperties],
72 flags: i32,
73) -> String {
74 let result = verifier.process_movement(
75 DeviceId(device_id),
76 action,
77 pointer_properties,
78 MotionFlags::from_bits(flags).unwrap(),
79 );
80 match result {
81 Ok(()) => "".to_string(),
82 Err(e) => e,
83 }
84}
85
86fn create(name: String) -> Box<InputVerifier> {
87 Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents")))
88}