Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | mod input; |
| 20 | mod input_verifier; |
| 21 | |
| 22 | pub use input::{DeviceId, MotionAction, MotionFlags}; |
| 23 | pub use input_verifier::InputVerifier; |
| 24 | |
| 25 | #[cxx::bridge(namespace = "android::input")] |
| 26 | mod 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; |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 57 | fn reset_device(verifier: &mut InputVerifier, device_id: i32); |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 61 | pub struct RustPointerProperties { |
| 62 | pub id: i32, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | use crate::ffi::RustPointerProperties; |
| 67 | |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 68 | fn create(name: String) -> Box<InputVerifier> { |
| 69 | Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents"))) |
| 70 | } |
| 71 | |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 72 | fn process_movement( |
| 73 | verifier: &mut InputVerifier, |
| 74 | device_id: i32, |
| 75 | action: u32, |
| 76 | pointer_properties: &[RustPointerProperties], |
| 77 | flags: i32, |
| 78 | ) -> String { |
| 79 | let result = verifier.process_movement( |
| 80 | DeviceId(device_id), |
| 81 | action, |
| 82 | pointer_properties, |
| 83 | MotionFlags::from_bits(flags).unwrap(), |
| 84 | ); |
| 85 | match result { |
| 86 | Ok(()) => "".to_string(), |
| 87 | Err(e) => e, |
| 88 | } |
| 89 | } |
| 90 | |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 91 | fn reset_device(verifier: &mut InputVerifier, device_id: i32) { |
| 92 | verifier.reset_device(DeviceId(device_id)); |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 93 | } |