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