Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +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 InputFlinger |
| 18 | //! |
| 19 | //! We use cxxbridge to create IInputFlingerRust - the Rust component of inputflinger - and |
| 20 | //! pass it back to C++ as a local AIDL interface. |
| 21 | |
Vaibhav Devmurari | 953e6a4 | 2023-11-21 18:24:19 +0000 | [diff] [blame] | 22 | mod bounce_keys_filter; |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 23 | mod input_filter; |
Vaibhav Devmurari | 3935e6f | 2024-01-19 14:44:47 +0000 | [diff] [blame] | 24 | mod input_filter_thread; |
| 25 | mod slow_keys_filter; |
Vaibhav Devmurari | 9314468 | 2023-12-11 16:38:56 +0000 | [diff] [blame] | 26 | mod sticky_keys_filter; |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | use crate::input_filter::InputFilter; |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 29 | use binder::{ |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 30 | unstable_api::{new_spibinder, AIBinder}, |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 31 | BinderFeatures, Interface, StatusCode, Strong, |
| 32 | }; |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 33 | use com_android_server_inputflinger::aidl::com::android::server::inputflinger::{ |
| 34 | IInputFilter::{BnInputFilter, IInputFilter, IInputFilterCallbacks::IInputFilterCallbacks}, |
| 35 | IInputFlingerRust::{ |
| 36 | BnInputFlingerRust, IInputFlingerRust, |
| 37 | IInputFlingerRustBootstrapCallback::IInputFlingerRustBootstrapCallback, |
| 38 | }, |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 39 | }; |
| 40 | use log::debug; |
| 41 | |
| 42 | const LOG_TAG: &str = "inputflinger_bootstrap"; |
| 43 | |
| 44 | #[cxx::bridge] |
Andrew Walbran | 9d4234c | 2023-08-18 11:16:04 +0000 | [diff] [blame] | 45 | #[allow(unsafe_op_in_unsafe_fn)] |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 46 | mod ffi { |
| 47 | extern "C++" { |
| 48 | include!("InputFlingerBootstrap.h"); |
| 49 | type IInputFlingerRustBootstrapCallbackAIBinder; |
| 50 | } |
| 51 | |
| 52 | extern "Rust" { |
| 53 | unsafe fn create_inputflinger_rust( |
| 54 | callback: *mut IInputFlingerRustBootstrapCallbackAIBinder, |
| 55 | ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Create the IInputFlingerRust implementation. |
| 60 | /// This is the singular entry point from C++ into Rust. |
| 61 | /// The `callback` parameter must be a valid pointer to an AIBinder implementation of |
| 62 | /// the `IInputFlingerRustBootstrapCallback` interface. The IInputFlingerRust implementation that |
| 63 | /// is created will be passed back through the callback from within this function. |
| 64 | /// NOTE: This function must not hold a strong reference to the callback beyond its scope. |
| 65 | /// |
| 66 | /// # Safety |
| 67 | /// |
Thomas Leu | 8946b31 | 2024-02-16 16:32:59 +0100 | [diff] [blame^] | 68 | /// The provided `callback` must be a valid pointer to an `AIBinder` interface of type |
| 69 | /// `IInputFlingerRustBootstrapCallback`, and the caller must give this function ownership of one |
| 70 | /// strong refcount to the interface. See `binder::unstable_api::new_spibinder`. |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 71 | unsafe fn create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder) { |
| 72 | logger::init( |
Jeff Vander Stoep | 046a3cc | 2024-01-31 12:05:42 +0100 | [diff] [blame] | 73 | logger::Config::default() |
| 74 | .with_tag_on_device(LOG_TAG) |
| 75 | .with_max_level(log::LevelFilter::Trace), |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 76 | ); |
| 77 | |
| 78 | let callback = callback as *mut AIBinder; |
| 79 | if callback.is_null() { |
| 80 | panic!("create_inputflinger_rust cannot be called with a null callback"); |
| 81 | } |
| 82 | |
Andrew Walbran | 9d4234c | 2023-08-18 11:16:04 +0000 | [diff] [blame] | 83 | // SAFETY: Our caller guaranteed that `callback` is a valid pointer to an `AIBinder` and its |
| 84 | // reference count has been incremented.. |
| 85 | let Some(callback) = (unsafe { new_spibinder(callback) }) else { |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 86 | panic!("Failed to get SpAIBinder from raw callback pointer"); |
| 87 | }; |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 88 | |
| 89 | let callback: Result<Strong<dyn IInputFlingerRustBootstrapCallback>, StatusCode> = |
| 90 | callback.into_interface(); |
| 91 | match callback { |
| 92 | Ok(callback) => { |
| 93 | debug!("Creating InputFlingerRust"); |
| 94 | let service = |
| 95 | BnInputFlingerRust::new_binder(InputFlingerRust {}, BinderFeatures::default()); |
| 96 | callback.onProvideInputFlingerRust(&service).unwrap(); |
| 97 | } |
| 98 | Err(status) => { |
| 99 | panic!("Failed to convert AIBinder into the callback interface: {}", status); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | struct InputFlingerRust {} |
| 105 | |
| 106 | impl Interface for InputFlingerRust {} |
| 107 | |
Vaibhav Devmurari | 5766aee | 2023-11-03 17:21:25 +0000 | [diff] [blame] | 108 | impl IInputFlingerRust for InputFlingerRust { |
| 109 | fn createInputFilter( |
| 110 | &self, |
| 111 | callbacks: &Strong<dyn IInputFilterCallbacks>, |
| 112 | ) -> binder::Result<Strong<dyn IInputFilter>> { |
| 113 | debug!("Creating InputFilter"); |
| 114 | let filter = BnInputFilter::new_binder( |
| 115 | InputFilter::new(callbacks.clone()), |
| 116 | BinderFeatures::default(), |
| 117 | ); |
| 118 | Result::Ok(filter) |
| 119 | } |
| 120 | } |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 121 | |
| 122 | impl Drop for InputFlingerRust { |
| 123 | fn drop(&mut self) { |
| 124 | debug!("Destroying InputFlingerRust"); |
| 125 | } |
| 126 | } |