1 /*
2  * Copyright (C) 2024 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 #define LOG_TAG "WindowManagerGlobal"
18 
19 #include "android_view_WindowManagerGlobal.h"
20 
21 #include <android_runtime/AndroidRuntime.h>
22 #include <android_runtime/android_view_SurfaceControl.h>
23 #include <android_runtime/android_window_InputTransferToken.h>
24 #include <jni.h>
25 #include <nativehelper/ScopedLocalRef.h>
26 
27 #include "android_util_Binder.h"
28 #include "android_view_InputChannel.h"
29 #include "jni_wrappers.h"
30 
31 namespace android {
32 
33 static struct {
34     jclass clazz;
35     jmethodID createInputChannel;
36     jmethodID removeInputChannel;
37 } gWindowManagerGlobal;
38 
createInputChannel(const sp<IBinder> & clientToken,const InputTransferToken & hostInputTransferToken,const SurfaceControl & surfaceControl,const InputTransferToken & clientInputTransferToken)39 std::shared_ptr<InputChannel> createInputChannel(
40         const sp<IBinder>& clientToken, const InputTransferToken& hostInputTransferToken,
41         const SurfaceControl& surfaceControl, const InputTransferToken& clientInputTransferToken) {
42     JNIEnv* env = AndroidRuntime::getJNIEnv();
43     ScopedLocalRef<jobject> hostInputTransferTokenObj(
44             env,
45             android_window_InputTransferToken_getJavaInputTransferToken(env,
46                                                                         hostInputTransferToken));
47     ScopedLocalRef<jobject>
48             surfaceControlObj(env,
49                               android_view_SurfaceControl_getJavaSurfaceControl(env,
50                                                                                 surfaceControl));
51     ScopedLocalRef<jobject> clientTokenObj(env, javaObjectForIBinder(env, clientToken));
52     ScopedLocalRef<jobject> clientInputTransferTokenObj(
53             env,
54             android_window_InputTransferToken_getJavaInputTransferToken(env,
55                                                                         clientInputTransferToken));
56     ScopedLocalRef<jobject>
57             inputChannelObj(env,
58                             env->CallStaticObjectMethod(gWindowManagerGlobal.clazz,
59                                                         gWindowManagerGlobal.createInputChannel,
60                                                         clientTokenObj.get(),
61                                                         hostInputTransferTokenObj.get(),
62                                                         surfaceControlObj.get(),
63                                                         clientInputTransferTokenObj.get()));
64 
65     return android_view_InputChannel_getInputChannel(env, inputChannelObj.get());
66 }
67 
removeInputChannel(const sp<IBinder> & clientToken)68 void removeInputChannel(const sp<IBinder>& clientToken) {
69     JNIEnv* env = AndroidRuntime::getJNIEnv();
70 
71     ScopedLocalRef<jobject> clientTokenObj(env, javaObjectForIBinder(env, clientToken));
72     env->CallStaticObjectMethod(gWindowManagerGlobal.clazz, gWindowManagerGlobal.removeInputChannel,
73                                 clientTokenObj.get());
74 }
75 
register_android_view_WindowManagerGlobal(JNIEnv * env)76 int register_android_view_WindowManagerGlobal(JNIEnv* env) {
77     jclass windowManagerGlobalClass = FindClassOrDie(env, "android/view/WindowManagerGlobal");
78     gWindowManagerGlobal.clazz = MakeGlobalRefOrDie(env, windowManagerGlobalClass);
79     gWindowManagerGlobal.createInputChannel =
80             GetStaticMethodIDOrDie(env, windowManagerGlobalClass, "createInputChannel",
81                                    "(Landroid/os/IBinder;Landroid/window/"
82                                    "InputTransferToken;Landroid/view/SurfaceControl;Landroid/"
83                                    "window/InputTransferToken;)Landroid/view/InputChannel;");
84     gWindowManagerGlobal.removeInputChannel =
85             GetStaticMethodIDOrDie(env, windowManagerGlobalClass, "removeInputChannel",
86                                    "(Landroid/os/IBinder;)V");
87 
88     return NO_ERROR;
89 }
90 
91 } // namespace android