blob: 9fa9fe4914def6ed31850fa28eed581cf61d1be6 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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 "InputChannel-JNI"
18
19#include "JNIHelp.h"
20
21#include <android_runtime/AndroidRuntime.h>
22#include <binder/Parcel.h>
23#include <utils/Log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080024#include <androidfw/InputTransport.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070025#include "android_view_InputChannel.h"
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080026#include "android_os_Parcel.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070027#include "android_util_Binder.h"
28
29namespace android {
30
31// ----------------------------------------------------------------------------
32
33static struct {
34 jclass clazz;
35
36 jfieldID mPtr; // native object attached to the DVM InputChannel
37 jmethodID ctor;
38} gInputChannelClassInfo;
39
40// ----------------------------------------------------------------------------
41
42class NativeInputChannel {
43public:
44 NativeInputChannel(const sp<InputChannel>& inputChannel);
45 ~NativeInputChannel();
46
47 inline sp<InputChannel> getInputChannel() { return mInputChannel; }
48
49 void setDisposeCallback(InputChannelObjDisposeCallback callback, void* data);
50 void invokeAndRemoveDisposeCallback(JNIEnv* env, jobject obj);
51
52private:
53 sp<InputChannel> mInputChannel;
54 InputChannelObjDisposeCallback mDisposeCallback;
55 void* mDisposeData;
56};
57
58// ----------------------------------------------------------------------------
59
60NativeInputChannel::NativeInputChannel(const sp<InputChannel>& inputChannel) :
61 mInputChannel(inputChannel), mDisposeCallback(NULL) {
62}
63
64NativeInputChannel::~NativeInputChannel() {
65}
66
67void NativeInputChannel::setDisposeCallback(InputChannelObjDisposeCallback callback, void* data) {
68 mDisposeCallback = callback;
69 mDisposeData = data;
70}
71
72void NativeInputChannel::invokeAndRemoveDisposeCallback(JNIEnv* env, jobject obj) {
73 if (mDisposeCallback) {
74 mDisposeCallback(env, obj, mInputChannel, mDisposeData);
75 mDisposeCallback = NULL;
76 mDisposeData = NULL;
77 }
78}
79
80// ----------------------------------------------------------------------------
81
82static NativeInputChannel* android_view_InputChannel_getNativeInputChannel(JNIEnv* env,
83 jobject inputChannelObj) {
84 jint intPtr = env->GetIntField(inputChannelObj, gInputChannelClassInfo.mPtr);
85 return reinterpret_cast<NativeInputChannel*>(intPtr);
86}
87
88static void android_view_InputChannel_setNativeInputChannel(JNIEnv* env, jobject inputChannelObj,
89 NativeInputChannel* nativeInputChannel) {
90 env->SetIntField(inputChannelObj, gInputChannelClassInfo.mPtr,
91 reinterpret_cast<jint>(nativeInputChannel));
92}
93
94sp<InputChannel> android_view_InputChannel_getInputChannel(JNIEnv* env, jobject inputChannelObj) {
95 NativeInputChannel* nativeInputChannel =
96 android_view_InputChannel_getNativeInputChannel(env, inputChannelObj);
97 return nativeInputChannel != NULL ? nativeInputChannel->getInputChannel() : NULL;
98}
99
100void android_view_InputChannel_setDisposeCallback(JNIEnv* env, jobject inputChannelObj,
101 InputChannelObjDisposeCallback callback, void* data) {
102 NativeInputChannel* nativeInputChannel =
103 android_view_InputChannel_getNativeInputChannel(env, inputChannelObj);
104 if (nativeInputChannel == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000105 ALOGW("Cannot set dispose callback because input channel object has not been initialized.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106 } else {
107 nativeInputChannel->setDisposeCallback(callback, data);
108 }
109}
110
111static jobject android_view_InputChannel_createInputChannel(JNIEnv* env,
112 NativeInputChannel* nativeInputChannel) {
113 jobject inputChannelObj = env->NewObject(gInputChannelClassInfo.clazz,
114 gInputChannelClassInfo.ctor);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700115 if (inputChannelObj) {
116 android_view_InputChannel_setNativeInputChannel(env, inputChannelObj, nativeInputChannel);
117 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700118 return inputChannelObj;
119}
120
121static jobjectArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* env,
122 jclass clazz, jstring nameObj) {
123 const char* nameChars = env->GetStringUTFChars(nameObj, NULL);
124 String8 name(nameChars);
125 env->ReleaseStringUTFChars(nameObj, nameChars);
126
Jeff Brown5c225b12010-06-16 01:53:36 -0700127 sp<InputChannel> serverChannel;
128 sp<InputChannel> clientChannel;
129 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700130
131 if (result) {
Jeff Browncc4f7db2011-08-30 20:34:48 -0700132 String8 message;
133 message.appendFormat("Could not open input channel pair. status=%d", result);
134 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700135 return NULL;
136 }
137
Jeff Browncc4f7db2011-08-30 20:34:48 -0700138 jobjectArray channelPair = env->NewObjectArray(2, gInputChannelClassInfo.clazz, NULL);
139 if (env->ExceptionCheck()) {
140 return NULL;
141 }
142
Jeff Brown46b9ac02010-04-22 18:58:52 -0700143 jobject serverChannelObj = android_view_InputChannel_createInputChannel(env,
144 new NativeInputChannel(serverChannel));
Jeff Browncc4f7db2011-08-30 20:34:48 -0700145 if (env->ExceptionCheck()) {
146 return NULL;
147 }
148
Jeff Brown46b9ac02010-04-22 18:58:52 -0700149 jobject clientChannelObj = android_view_InputChannel_createInputChannel(env,
150 new NativeInputChannel(clientChannel));
Jeff Browncc4f7db2011-08-30 20:34:48 -0700151 if (env->ExceptionCheck()) {
152 return NULL;
153 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700154
Jeff Brown46b9ac02010-04-22 18:58:52 -0700155 env->SetObjectArrayElement(channelPair, 0, serverChannelObj);
156 env->SetObjectArrayElement(channelPair, 1, clientChannelObj);
157 return channelPair;
158}
159
160static void android_view_InputChannel_nativeDispose(JNIEnv* env, jobject obj, jboolean finalized) {
161 NativeInputChannel* nativeInputChannel =
162 android_view_InputChannel_getNativeInputChannel(env, obj);
163 if (nativeInputChannel) {
164 if (finalized) {
Steve Block8564c8d2012-01-05 23:22:43 +0000165 ALOGW("Input channel object '%s' was finalized without being disposed!",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700166 nativeInputChannel->getInputChannel()->getName().string());
167 }
168
169 nativeInputChannel->invokeAndRemoveDisposeCallback(env, obj);
170
171 android_view_InputChannel_setNativeInputChannel(env, obj, NULL);
172 delete nativeInputChannel;
173 }
174}
175
176static void android_view_InputChannel_nativeTransferTo(JNIEnv* env, jobject obj,
177 jobject otherObj) {
Jeff Browncc4f7db2011-08-30 20:34:48 -0700178 if (android_view_InputChannel_getNativeInputChannel(env, otherObj) != NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700179 jniThrowException(env, "java/lang/IllegalStateException",
180 "Other object already has a native input channel.");
181 return;
182 }
183
184 NativeInputChannel* nativeInputChannel =
185 android_view_InputChannel_getNativeInputChannel(env, obj);
186 android_view_InputChannel_setNativeInputChannel(env, otherObj, nativeInputChannel);
187 android_view_InputChannel_setNativeInputChannel(env, obj, NULL);
188}
189
190static void android_view_InputChannel_nativeReadFromParcel(JNIEnv* env, jobject obj,
191 jobject parcelObj) {
Jeff Browncc4f7db2011-08-30 20:34:48 -0700192 if (android_view_InputChannel_getNativeInputChannel(env, obj) != NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700193 jniThrowException(env, "java/lang/IllegalStateException",
194 "This object already has a native input channel.");
195 return;
196 }
197
198 Parcel* parcel = parcelForJavaObject(env, parcelObj);
199 if (parcel) {
200 bool isInitialized = parcel->readInt32();
201 if (isInitialized) {
202 String8 name = parcel->readString8();
Jeff Brown91e32892012-02-14 15:56:29 -0800203 int rawFd = parcel->readFileDescriptor();
204 int dupFd = dup(rawFd);
205 if (dupFd < 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800206 ALOGE("Error %d dup channel fd %d.", errno, rawFd);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700207 jniThrowRuntimeException(env,
208 "Could not read input channel file descriptors from parcel.");
209 return;
210 }
211
Jeff Browncbee6d62012-02-03 20:11:27 -0800212 InputChannel* inputChannel = new InputChannel(name, dupFd);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213 NativeInputChannel* nativeInputChannel = new NativeInputChannel(inputChannel);
214
215 android_view_InputChannel_setNativeInputChannel(env, obj, nativeInputChannel);
216 }
217 }
218}
219
220static void android_view_InputChannel_nativeWriteToParcel(JNIEnv* env, jobject obj,
221 jobject parcelObj) {
222 Parcel* parcel = parcelForJavaObject(env, parcelObj);
223 if (parcel) {
224 NativeInputChannel* nativeInputChannel =
225 android_view_InputChannel_getNativeInputChannel(env, obj);
226 if (nativeInputChannel) {
227 sp<InputChannel> inputChannel = nativeInputChannel->getInputChannel();
228
229 parcel->writeInt32(1);
230 parcel->writeString8(inputChannel->getName());
Jeff Browncbee6d62012-02-03 20:11:27 -0800231 parcel->writeDupFileDescriptor(inputChannel->getFd());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700232 } else {
233 parcel->writeInt32(0);
234 }
235 }
236}
237
238static jstring android_view_InputChannel_nativeGetName(JNIEnv* env, jobject obj) {
239 NativeInputChannel* nativeInputChannel =
240 android_view_InputChannel_getNativeInputChannel(env, obj);
241 if (! nativeInputChannel) {
242 return NULL;
243 }
244
245 jstring name = env->NewStringUTF(nativeInputChannel->getInputChannel()->getName().string());
246 return name;
247}
248
Jeff Brown1951ce82013-04-04 22:45:12 -0700249static void android_view_InputChannel_nativeDup(JNIEnv* env, jobject obj, jobject otherObj) {
250 NativeInputChannel* nativeInputChannel =
251 android_view_InputChannel_getNativeInputChannel(env, obj);
252 if (nativeInputChannel) {
253 android_view_InputChannel_setNativeInputChannel(env, otherObj,
254 new NativeInputChannel(nativeInputChannel->getInputChannel()->dup()));
255 }
256}
257
Jeff Brown46b9ac02010-04-22 18:58:52 -0700258// ----------------------------------------------------------------------------
259
260static JNINativeMethod gInputChannelMethods[] = {
261 /* name, signature, funcPtr */
262 { "nativeOpenInputChannelPair", "(Ljava/lang/String;)[Landroid/view/InputChannel;",
263 (void*)android_view_InputChannel_nativeOpenInputChannelPair },
264 { "nativeDispose", "(Z)V",
265 (void*)android_view_InputChannel_nativeDispose },
266 { "nativeTransferTo", "(Landroid/view/InputChannel;)V",
267 (void*)android_view_InputChannel_nativeTransferTo },
268 { "nativeReadFromParcel", "(Landroid/os/Parcel;)V",
269 (void*)android_view_InputChannel_nativeReadFromParcel },
270 { "nativeWriteToParcel", "(Landroid/os/Parcel;)V",
271 (void*)android_view_InputChannel_nativeWriteToParcel },
272 { "nativeGetName", "()Ljava/lang/String;",
273 (void*)android_view_InputChannel_nativeGetName },
Jeff Brown1951ce82013-04-04 22:45:12 -0700274 { "nativeDup", "(Landroid/view/InputChannel;)V",
275 (void*)android_view_InputChannel_nativeDup },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700276};
277
278#define FIND_CLASS(var, className) \
279 var = env->FindClass(className); \
280 LOG_FATAL_IF(! var, "Unable to find class " className); \
281 var = jclass(env->NewGlobalRef(var));
282
283#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
284 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
285 LOG_FATAL_IF(! var, "Unable to find method " methodName);
286
287#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
288 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
289 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
290
291int register_android_view_InputChannel(JNIEnv* env) {
292 int res = jniRegisterNativeMethods(env, "android/view/InputChannel",
293 gInputChannelMethods, NELEM(gInputChannelMethods));
294 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
295
296 FIND_CLASS(gInputChannelClassInfo.clazz, "android/view/InputChannel");
297
298 GET_FIELD_ID(gInputChannelClassInfo.mPtr, gInputChannelClassInfo.clazz,
299 "mPtr", "I");
300
301 GET_METHOD_ID(gInputChannelClassInfo.ctor, gInputChannelClassInfo.clazz,
302 "<init>", "()V");
303
304 return 0;
305}
306
307} // namespace android