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