blob: fa16898835e330219bb2fd04c43eed45b7c5540e [file] [log] [blame]
Prabir Pradhan44e6e832023-06-06 00:03:25 +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 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 Devmurari953e6a42023-11-21 18:24:19 +000022mod bounce_keys_filter;
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000023mod input_filter;
Vaibhav Devmurari93144682023-12-11 16:38:56 +000024mod sticky_keys_filter;
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000025
26use crate::input_filter::InputFilter;
Prabir Pradhan44e6e832023-06-06 00:03:25 +000027use binder::{
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000028 unstable_api::{new_spibinder, AIBinder},
Prabir Pradhan44e6e832023-06-06 00:03:25 +000029 BinderFeatures, Interface, StatusCode, Strong,
30};
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000031use com_android_server_inputflinger::aidl::com::android::server::inputflinger::{
32 IInputFilter::{BnInputFilter, IInputFilter, IInputFilterCallbacks::IInputFilterCallbacks},
33 IInputFlingerRust::{
34 BnInputFlingerRust, IInputFlingerRust,
35 IInputFlingerRustBootstrapCallback::IInputFlingerRustBootstrapCallback,
36 },
Prabir Pradhan44e6e832023-06-06 00:03:25 +000037};
38use log::debug;
39
40const LOG_TAG: &str = "inputflinger_bootstrap";
41
42#[cxx::bridge]
Andrew Walbran9d4234c2023-08-18 11:16:04 +000043#[allow(unsafe_op_in_unsafe_fn)]
Prabir Pradhan44e6e832023-06-06 00:03:25 +000044mod ffi {
45 extern "C++" {
46 include!("InputFlingerBootstrap.h");
47 type IInputFlingerRustBootstrapCallbackAIBinder;
48 }
49
50 extern "Rust" {
51 unsafe fn create_inputflinger_rust(
52 callback: *mut IInputFlingerRustBootstrapCallbackAIBinder,
53 );
54 }
55}
56
57/// Create the IInputFlingerRust implementation.
58/// This is the singular entry point from C++ into Rust.
59/// The `callback` parameter must be a valid pointer to an AIBinder implementation of
60/// the `IInputFlingerRustBootstrapCallback` interface. The IInputFlingerRust implementation that
61/// is created will be passed back through the callback from within this function.
62/// NOTE: This function must not hold a strong reference to the callback beyond its scope.
63///
64/// # Safety
65///
66/// This function is safe iff `callback` is a valid pointer to an `AIBinder` interface of type
67/// `IInputFlingerRustBootstrapCallback`. The pointer must have had its reference count manually
68/// incremented using `AIBinder_incStrong`. See `binder::unstable_api::new_spibinder`.
69unsafe fn create_inputflinger_rust(callback: *mut ffi::IInputFlingerRustBootstrapCallbackAIBinder) {
70 logger::init(
71 logger::Config::default().with_tag_on_device(LOG_TAG).with_min_level(log::Level::Trace),
72 );
73
74 let callback = callback as *mut AIBinder;
75 if callback.is_null() {
76 panic!("create_inputflinger_rust cannot be called with a null callback");
77 }
78
Andrew Walbran9d4234c2023-08-18 11:16:04 +000079 // SAFETY: Our caller guaranteed that `callback` is a valid pointer to an `AIBinder` and its
80 // reference count has been incremented..
81 let Some(callback) = (unsafe { new_spibinder(callback) }) else {
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000082 panic!("Failed to get SpAIBinder from raw callback pointer");
83 };
Prabir Pradhan44e6e832023-06-06 00:03:25 +000084
85 let callback: Result<Strong<dyn IInputFlingerRustBootstrapCallback>, StatusCode> =
86 callback.into_interface();
87 match callback {
88 Ok(callback) => {
89 debug!("Creating InputFlingerRust");
90 let service =
91 BnInputFlingerRust::new_binder(InputFlingerRust {}, BinderFeatures::default());
92 callback.onProvideInputFlingerRust(&service).unwrap();
93 }
94 Err(status) => {
95 panic!("Failed to convert AIBinder into the callback interface: {}", status);
96 }
97 }
98}
99
100struct InputFlingerRust {}
101
102impl Interface for InputFlingerRust {}
103
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000104impl IInputFlingerRust for InputFlingerRust {
105 fn createInputFilter(
106 &self,
107 callbacks: &Strong<dyn IInputFilterCallbacks>,
108 ) -> binder::Result<Strong<dyn IInputFilter>> {
109 debug!("Creating InputFilter");
110 let filter = BnInputFilter::new_binder(
111 InputFilter::new(callbacks.clone()),
112 BinderFeatures::default(),
113 );
114 Result::Ok(filter)
115 }
116}
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000117
118impl Drop for InputFlingerRust {
119 fn drop(&mut self) {
120 debug!("Destroying InputFlingerRust");
121 }
122}