blob: 5b74e435c7eee970e2c5661284727661677dac90 [file] [log] [blame]
Jeff Brown928e0542011-01-10 11:17:36 -08001/*
2 * Copyright (C) 2011 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 "InputWindowHandle"
18
19#include "JNIHelp.h"
20#include "jni.h"
21#include <android_runtime/AndroidRuntime.h>
22#include <utils/threads.h>
23
24#include "com_android_server_InputWindowHandle.h"
25#include "com_android_server_InputApplicationHandle.h"
26
27namespace android {
28
29static struct {
30 jclass clazz;
31
32 jfieldID ptr;
33 jfieldID inputApplicationHandle;
34} gInputWindowHandleClassInfo;
35
36static Mutex gHandleMutex;
37
38
39// --- NativeInputWindowHandle ---
40
41NativeInputWindowHandle::NativeInputWindowHandle(
42 const sp<InputApplicationHandle>& inputApplicationHandle, jweak objWeak) :
43 InputWindowHandle(inputApplicationHandle),
44 mObjWeak(objWeak) {
45}
46
47NativeInputWindowHandle::~NativeInputWindowHandle() {
48 JNIEnv* env = AndroidRuntime::getJNIEnv();
49 env->DeleteWeakGlobalRef(mObjWeak);
50}
51
52jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
53 return env->NewLocalRef(mObjWeak);
54}
55
56
57// --- Global functions ---
58
59sp<NativeInputWindowHandle> android_server_InputWindowHandle_getHandle(
60 JNIEnv* env, jobject inputWindowHandleObj) {
61 if (!inputWindowHandleObj) {
62 return NULL;
63 }
64
65 AutoMutex _l(gHandleMutex);
66
67 int ptr = env->GetIntField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
68 NativeInputWindowHandle* handle;
69 if (ptr) {
70 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
71 } else {
72 jobject inputApplicationHandleObj = env->GetObjectField(inputWindowHandleObj,
73 gInputWindowHandleClassInfo.inputApplicationHandle);
74 sp<InputApplicationHandle> inputApplicationHandle =
75 android_server_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
76 env->DeleteLocalRef(inputApplicationHandleObj);
77
78 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
79 handle = new NativeInputWindowHandle(inputApplicationHandle, objWeak);
80 handle->incStrong(inputWindowHandleObj);
81 env->SetIntField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
82 reinterpret_cast<int>(handle));
83 }
84 return handle;
85}
86
87
88// --- JNI ---
89
90static void android_server_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
91 AutoMutex _l(gHandleMutex);
92
93 int ptr = env->GetIntField(obj, gInputWindowHandleClassInfo.ptr);
94 if (ptr) {
95 env->SetIntField(obj, gInputWindowHandleClassInfo.ptr, 0);
96
97 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
98 handle->decStrong(obj);
99 }
100}
101
102
103static JNINativeMethod gInputWindowHandleMethods[] = {
104 /* name, signature, funcPtr */
105 { "nativeDispose", "()V",
106 (void*) android_server_InputWindowHandle_nativeDispose },
107};
108
109#define FIND_CLASS(var, className) \
110 var = env->FindClass(className); \
111 LOG_FATAL_IF(! var, "Unable to find class " className); \
112 var = jclass(env->NewGlobalRef(var));
113
114#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
115 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
116 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
117
118int register_android_server_InputWindowHandle(JNIEnv* env) {
Dianne Hackborna924dc0d2011-02-17 14:22:17 -0800119 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputWindowHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800120 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
121 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
122
Dianne Hackborna924dc0d2011-02-17 14:22:17 -0800123 FIND_CLASS(gInputWindowHandleClassInfo.clazz, "com/android/server/wm/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800124
125 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, gInputWindowHandleClassInfo.clazz,
126 "ptr", "I");
127
128 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
129 gInputWindowHandleClassInfo.clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -0800130 "inputApplicationHandle", "Lcom/android/server/wm/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800131
132 return 0;
133}
134
135} /* namespace android */