1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2023 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker //! # The rust component of InputFlinger
18*38e8c45fSAndroid Build Coastguard Worker //!
19*38e8c45fSAndroid Build Coastguard Worker //! We use cxxbridge to create IInputFlingerRust - the Rust component of inputflinger - and
20*38e8c45fSAndroid Build Coastguard Worker //! pass it back to C++ as a local AIDL interface.
21*38e8c45fSAndroid Build Coastguard Worker
22*38e8c45fSAndroid Build Coastguard Worker mod bounce_keys_filter;
23*38e8c45fSAndroid Build Coastguard Worker mod input_filter;
24*38e8c45fSAndroid Build Coastguard Worker mod input_filter_thread;
25*38e8c45fSAndroid Build Coastguard Worker mod slow_keys_filter;
26*38e8c45fSAndroid Build Coastguard Worker mod sticky_keys_filter;
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker use crate::input_filter::InputFilter;
29*38e8c45fSAndroid Build Coastguard Worker use binder::{
30*38e8c45fSAndroid Build Coastguard Worker unstable_api::{new_spibinder, AIBinder},
31*38e8c45fSAndroid Build Coastguard Worker BinderFeatures, Interface, StatusCode, Strong,
32*38e8c45fSAndroid Build Coastguard Worker };
33*38e8c45fSAndroid Build Coastguard Worker use com_android_server_inputflinger::aidl::com::android::server::inputflinger::{
34*38e8c45fSAndroid Build Coastguard Worker IInputFilter::{BnInputFilter, IInputFilter, IInputFilterCallbacks::IInputFilterCallbacks},
35*38e8c45fSAndroid Build Coastguard Worker IInputFlingerRust::{
36*38e8c45fSAndroid Build Coastguard Worker BnInputFlingerRust, IInputFlingerRust,
37*38e8c45fSAndroid Build Coastguard Worker IInputFlingerRustBootstrapCallback::IInputFlingerRustBootstrapCallback,
38*38e8c45fSAndroid Build Coastguard Worker },
39*38e8c45fSAndroid Build Coastguard Worker };
40*38e8c45fSAndroid Build Coastguard Worker use log::debug;
41*38e8c45fSAndroid Build Coastguard Worker
42*38e8c45fSAndroid Build Coastguard Worker const LOG_TAG: &str = "inputflinger_bootstrap";
43*38e8c45fSAndroid Build Coastguard Worker
44*38e8c45fSAndroid Build Coastguard Worker #[cxx::bridge]
45*38e8c45fSAndroid Build Coastguard Worker #[allow(unsafe_op_in_unsafe_fn)]
46*38e8c45fSAndroid Build Coastguard Worker mod ffi {
47*38e8c45fSAndroid Build Coastguard Worker extern "C++" {
48*38e8c45fSAndroid Build Coastguard Worker include!("InputFlingerBootstrap.h");
49*38e8c45fSAndroid Build Coastguard Worker type IInputFlingerRustBootstrapCallbackAIBinder;
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker
52*38e8c45fSAndroid Build Coastguard Worker extern "Rust" {
create_inputflinger_rust( callback: *mut IInputFlingerRustBootstrapCallbackAIBinder, )53*38e8c45fSAndroid Build Coastguard Worker unsafe fn create_inputflinger_rust(
54*38e8c45fSAndroid Build Coastguard Worker callback: *mut IInputFlingerRustBootstrapCallbackAIBinder,
55*38e8c45fSAndroid Build Coastguard Worker );
56*38e8c45fSAndroid Build Coastguard Worker }
57*38e8c45fSAndroid Build Coastguard Worker }
58*38e8c45fSAndroid Build Coastguard Worker
59*38e8c45fSAndroid Build Coastguard Worker /// Create the IInputFlingerRust implementation.
60*38e8c45fSAndroid Build Coastguard Worker /// This is the singular entry point from C++ into Rust.
61*38e8c45fSAndroid Build Coastguard Worker /// The `callback` parameter must be a valid pointer to an AIBinder implementation of
62*38e8c45fSAndroid Build Coastguard Worker /// the `IInputFlingerRustBootstrapCallback` interface. The IInputFlingerRust implementation that
63*38e8c45fSAndroid Build Coastguard Worker /// is created will be passed back through the callback from within this function.
64*38e8c45fSAndroid Build Coastguard Worker /// NOTE: This function must not hold a strong reference to the callback beyond its scope.
65*38e8c45fSAndroid Build Coastguard Worker ///
66*38e8c45fSAndroid Build Coastguard Worker /// # Safety
67*38e8c45fSAndroid Build Coastguard Worker ///
68*38e8c45fSAndroid Build Coastguard Worker /// The provided `callback` must be a valid pointer to an `AIBinder` interface of type
69*38e8c45fSAndroid Build Coastguard Worker /// `IInputFlingerRustBootstrapCallback`, and the caller must give this function ownership of one
70*38e8c45fSAndroid Build Coastguard Worker /// strong refcount to the interface. See `binder::unstable_api::new_spibinder`.
create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder)71*38e8c45fSAndroid Build Coastguard Worker unsafe fn create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder) {
72*38e8c45fSAndroid Build Coastguard Worker logger::init(
73*38e8c45fSAndroid Build Coastguard Worker logger::Config::default()
74*38e8c45fSAndroid Build Coastguard Worker .with_tag_on_device(LOG_TAG)
75*38e8c45fSAndroid Build Coastguard Worker .with_max_level(log::LevelFilter::Trace),
76*38e8c45fSAndroid Build Coastguard Worker );
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker let callback = callback as *mut AIBinder;
79*38e8c45fSAndroid Build Coastguard Worker if callback.is_null() {
80*38e8c45fSAndroid Build Coastguard Worker panic!("create_inputflinger_rust cannot be called with a null callback");
81*38e8c45fSAndroid Build Coastguard Worker }
82*38e8c45fSAndroid Build Coastguard Worker
83*38e8c45fSAndroid Build Coastguard Worker // SAFETY: Our caller guaranteed that `callback` is a valid pointer to an `AIBinder` and its
84*38e8c45fSAndroid Build Coastguard Worker // reference count has been incremented..
85*38e8c45fSAndroid Build Coastguard Worker let Some(callback) = (unsafe { new_spibinder(callback) }) else {
86*38e8c45fSAndroid Build Coastguard Worker panic!("Failed to get SpAIBinder from raw callback pointer");
87*38e8c45fSAndroid Build Coastguard Worker };
88*38e8c45fSAndroid Build Coastguard Worker
89*38e8c45fSAndroid Build Coastguard Worker let callback: Result<Strong<dyn IInputFlingerRustBootstrapCallback>, StatusCode> =
90*38e8c45fSAndroid Build Coastguard Worker callback.into_interface();
91*38e8c45fSAndroid Build Coastguard Worker match callback {
92*38e8c45fSAndroid Build Coastguard Worker Ok(callback) => {
93*38e8c45fSAndroid Build Coastguard Worker debug!("Creating InputFlingerRust");
94*38e8c45fSAndroid Build Coastguard Worker let service =
95*38e8c45fSAndroid Build Coastguard Worker BnInputFlingerRust::new_binder(InputFlingerRust {}, BinderFeatures::default());
96*38e8c45fSAndroid Build Coastguard Worker callback.onProvideInputFlingerRust(&service).unwrap();
97*38e8c45fSAndroid Build Coastguard Worker }
98*38e8c45fSAndroid Build Coastguard Worker Err(status) => {
99*38e8c45fSAndroid Build Coastguard Worker panic!("Failed to convert AIBinder into the callback interface: {}", status);
100*38e8c45fSAndroid Build Coastguard Worker }
101*38e8c45fSAndroid Build Coastguard Worker }
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker
104*38e8c45fSAndroid Build Coastguard Worker struct InputFlingerRust {}
105*38e8c45fSAndroid Build Coastguard Worker
106*38e8c45fSAndroid Build Coastguard Worker impl Interface for InputFlingerRust {}
107*38e8c45fSAndroid Build Coastguard Worker
108*38e8c45fSAndroid Build Coastguard Worker impl IInputFlingerRust for InputFlingerRust {
createInputFilter( &self, callbacks: &Strong<dyn IInputFilterCallbacks>, ) -> binder::Result<Strong<dyn IInputFilter>>109*38e8c45fSAndroid Build Coastguard Worker fn createInputFilter(
110*38e8c45fSAndroid Build Coastguard Worker &self,
111*38e8c45fSAndroid Build Coastguard Worker callbacks: &Strong<dyn IInputFilterCallbacks>,
112*38e8c45fSAndroid Build Coastguard Worker ) -> binder::Result<Strong<dyn IInputFilter>> {
113*38e8c45fSAndroid Build Coastguard Worker debug!("Creating InputFilter");
114*38e8c45fSAndroid Build Coastguard Worker let filter = BnInputFilter::new_binder(
115*38e8c45fSAndroid Build Coastguard Worker InputFilter::new(callbacks.clone()),
116*38e8c45fSAndroid Build Coastguard Worker BinderFeatures::default(),
117*38e8c45fSAndroid Build Coastguard Worker );
118*38e8c45fSAndroid Build Coastguard Worker Result::Ok(filter)
119*38e8c45fSAndroid Build Coastguard Worker }
120*38e8c45fSAndroid Build Coastguard Worker }
121*38e8c45fSAndroid Build Coastguard Worker
122*38e8c45fSAndroid Build Coastguard Worker impl Drop for InputFlingerRust {
drop(&mut self)123*38e8c45fSAndroid Build Coastguard Worker fn drop(&mut self) {
124*38e8c45fSAndroid Build Coastguard Worker debug!("Destroying InputFlingerRust");
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker }
127