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 | |
Vaibhav Devmurari | 099fb59 | 2024-06-26 14:26:30 +0000 | [diff] [blame] | 19 | mod data_store; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 20 | mod input; |
| 21 | mod input_verifier; |
Vaibhav Devmurari | a8359fc | 2024-06-10 20:01:25 +0000 | [diff] [blame] | 22 | mod keyboard_classification_config; |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 23 | mod keyboard_classifier; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 24 | |
Vaibhav Devmurari | 099fb59 | 2024-06-26 14:26:30 +0000 | [diff] [blame] | 25 | pub use data_store::{DataStore, DefaultFileReaderWriter}; |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 26 | pub use input::{ |
Vaibhav Devmurari | 983e76b | 2024-08-30 15:16:38 +0000 | [diff] [blame] | 27 | DeviceClass, DeviceId, InputDevice, KeyboardType, ModifierState, MotionAction, MotionFlags, |
| 28 | Source, |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 29 | }; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 30 | pub use input_verifier::InputVerifier; |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 31 | pub use keyboard_classifier::KeyboardClassifier; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 32 | |
| 33 | #[cxx::bridge(namespace = "android::input")] |
Chris Wailes | c98800e | 2024-09-05 13:31:13 -0700 | [diff] [blame] | 34 | #[allow(clippy::needless_maybe_sized)] |
Andrew Walbran | 3799422 | 2023-08-03 12:16:28 +0000 | [diff] [blame] | 35 | #[allow(unsafe_op_in_unsafe_fn)] |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 36 | mod ffi { |
| 37 | #[namespace = "android"] |
| 38 | unsafe extern "C++" { |
| 39 | include!("ffi/FromRustToCpp.h"); |
| 40 | fn shouldLog(tag: &str) -> bool; |
| 41 | } |
| 42 | |
| 43 | #[namespace = "android::input::verifier"] |
| 44 | extern "Rust" { |
| 45 | /// Used to validate the incoming motion stream. |
| 46 | /// This class is not thread-safe. |
| 47 | /// State is stored in the "InputVerifier" object |
| 48 | /// that can be created via the 'create' method. |
| 49 | /// Usage: |
| 50 | /// |
| 51 | /// ```ignore |
| 52 | /// Box<InputVerifier> verifier = create("inputChannel name"); |
| 53 | /// result = process_movement(verifier, ...); |
| 54 | /// if (result) { |
| 55 | /// crash(result.error_message()); |
| 56 | /// } |
| 57 | /// ``` |
| 58 | type InputVerifier; |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 59 | #[cxx_name = create] |
| 60 | fn create_input_verifier(name: String) -> Box<InputVerifier>; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 61 | fn process_movement( |
| 62 | verifier: &mut InputVerifier, |
| 63 | device_id: i32, |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 64 | source: u32, |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 65 | action: u32, |
| 66 | pointer_properties: &[RustPointerProperties], |
Siarhei Vishniakou | 227a7f8 | 2023-07-18 18:30:32 -0700 | [diff] [blame] | 67 | flags: u32, |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 68 | ) -> String; |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 69 | fn reset_device(verifier: &mut InputVerifier, device_id: i32); |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 72 | #[namespace = "android::input::keyboardClassifier"] |
| 73 | extern "Rust" { |
| 74 | /// Used to classify a keyboard into alphabetic and non-alphabetic |
| 75 | type KeyboardClassifier; |
| 76 | #[cxx_name = create] |
| 77 | fn create_keyboard_classifier() -> Box<KeyboardClassifier>; |
| 78 | #[cxx_name = notifyKeyboardChanged] |
| 79 | fn notify_keyboard_changed( |
| 80 | classifier: &mut KeyboardClassifier, |
| 81 | device_id: i32, |
| 82 | identifier: RustInputDeviceIdentifier, |
| 83 | device_classes: u32, |
| 84 | ); |
| 85 | #[cxx_name = getKeyboardType] |
| 86 | fn get_keyboard_type(classifier: &mut KeyboardClassifier, device_id: i32) -> u32; |
| 87 | #[cxx_name = isFinalized] |
| 88 | fn is_finalized(classifier: &mut KeyboardClassifier, device_id: i32) -> bool; |
| 89 | #[cxx_name = processKey] |
| 90 | fn process_key( |
| 91 | classifier: &mut KeyboardClassifier, |
| 92 | device_id: i32, |
| 93 | evdev_code: i32, |
| 94 | modifier_state: u32, |
| 95 | ); |
| 96 | } |
| 97 | |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 98 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 99 | pub struct RustPointerProperties { |
| 100 | pub id: i32, |
| 101 | } |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 102 | |
| 103 | #[derive(Debug)] |
| 104 | pub struct RustInputDeviceIdentifier { |
| 105 | pub name: String, |
| 106 | pub location: String, |
| 107 | pub unique_id: String, |
| 108 | pub bus: u16, |
| 109 | pub vendor: u16, |
| 110 | pub product: u16, |
| 111 | pub version: u16, |
| 112 | pub descriptor: String, |
| 113 | } |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 116 | use crate::ffi::{RustInputDeviceIdentifier, RustPointerProperties}; |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 117 | |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 118 | fn create_input_verifier(name: String) -> Box<InputVerifier> { |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 119 | Box::new(InputVerifier::new(&name, ffi::shouldLog("InputVerifierLogEvents"))) |
| 120 | } |
| 121 | |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 122 | fn process_movement( |
| 123 | verifier: &mut InputVerifier, |
| 124 | device_id: i32, |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 125 | source: u32, |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 126 | action: u32, |
| 127 | pointer_properties: &[RustPointerProperties], |
Siarhei Vishniakou | 227a7f8 | 2023-07-18 18:30:32 -0700 | [diff] [blame] | 128 | flags: u32, |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 129 | ) -> String { |
Linnan Li | 13ccabe | 2024-04-19 17:14:43 +0000 | [diff] [blame] | 130 | let motion_flags = MotionFlags::from_bits(flags); |
| 131 | if motion_flags.is_none() { |
| 132 | panic!( |
| 133 | "The conversion of flags 0x{:08x} failed, please check if some flags have not been \ |
| 134 | added to MotionFlags.", |
| 135 | flags |
| 136 | ); |
| 137 | } |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 138 | let result = verifier.process_movement( |
| 139 | DeviceId(device_id), |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 140 | Source::from_bits(source).unwrap(), |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 141 | action, |
| 142 | pointer_properties, |
Linnan Li | 13ccabe | 2024-04-19 17:14:43 +0000 | [diff] [blame] | 143 | motion_flags.unwrap(), |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 144 | ); |
| 145 | match result { |
| 146 | Ok(()) => "".to_string(), |
| 147 | Err(e) => e, |
| 148 | } |
| 149 | } |
| 150 | |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 151 | fn reset_device(verifier: &mut InputVerifier, device_id: i32) { |
| 152 | verifier.reset_device(DeviceId(device_id)); |
Prabir Pradhan | 0762b1f | 2023-06-22 23:08:18 +0000 | [diff] [blame] | 153 | } |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 154 | |
| 155 | fn create_keyboard_classifier() -> Box<KeyboardClassifier> { |
Vaibhav Devmurari | 099fb59 | 2024-06-26 14:26:30 +0000 | [diff] [blame] | 156 | // Future design: Make this data store singleton by passing it to C++ side and making it global |
| 157 | // and pass by reference to components that need to store persistent data. |
| 158 | // |
| 159 | // Currently only used by rust keyboard classifier so keeping it here. |
| 160 | let data_store = DataStore::new(Box::new(DefaultFileReaderWriter::new( |
| 161 | "/data/system/inputflinger-data.json".to_string(), |
| 162 | ))); |
| 163 | Box::new(KeyboardClassifier::new(data_store)) |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | fn notify_keyboard_changed( |
| 167 | classifier: &mut KeyboardClassifier, |
| 168 | device_id: i32, |
| 169 | identifier: RustInputDeviceIdentifier, |
| 170 | device_classes: u32, |
| 171 | ) { |
| 172 | let classes = DeviceClass::from_bits(device_classes); |
| 173 | if classes.is_none() { |
| 174 | panic!( |
| 175 | "The conversion of device class 0x{:08x} failed, please check if some device classes |
| 176 | have not been added to DeviceClass.", |
| 177 | device_classes |
| 178 | ); |
| 179 | } |
| 180 | classifier.notify_keyboard_changed(InputDevice { |
| 181 | device_id: DeviceId(device_id), |
| 182 | identifier, |
| 183 | classes: classes.unwrap(), |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | fn get_keyboard_type(classifier: &mut KeyboardClassifier, device_id: i32) -> u32 { |
| 188 | classifier.get_keyboard_type(DeviceId(device_id)) as u32 |
| 189 | } |
| 190 | |
| 191 | fn is_finalized(classifier: &mut KeyboardClassifier, device_id: i32) -> bool { |
| 192 | classifier.is_finalized(DeviceId(device_id)) |
| 193 | } |
| 194 | |
| 195 | fn process_key( |
| 196 | classifier: &mut KeyboardClassifier, |
| 197 | device_id: i32, |
| 198 | evdev_code: i32, |
| 199 | meta_state: u32, |
| 200 | ) { |
| 201 | let modifier_state = ModifierState::from_bits(meta_state); |
| 202 | if modifier_state.is_none() { |
| 203 | panic!( |
| 204 | "The conversion of meta state 0x{:08x} failed, please check if some meta state |
| 205 | have not been added to ModifierState.", |
| 206 | meta_state |
| 207 | ); |
| 208 | } |
| 209 | classifier.process_key(DeviceId(device_id), evdev_code, modifier_state.unwrap()); |
| 210 | } |