blob: 92ef7f1ae096a0f6dc3ebd5f75ec366b6f93dda2 [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
Jeff Brown9302c872011-07-13 22:51:29 -070024#include <android_view_InputChannel.h>
25#include <android/graphics/Region.h>
Michael Wrighta407d6a2014-02-05 18:02:40 -080026#include <ui/Region.h>
Jeff Brown9302c872011-07-13 22:51:29 -070027
Jeff Brown4532e612012-04-05 14:27:12 -070028#include "com_android_server_input_InputWindowHandle.h"
29#include "com_android_server_input_InputApplicationHandle.h"
Jeff Brown928e0542011-01-10 11:17:36 -080030
31namespace android {
32
33static struct {
Jeff Brown928e0542011-01-10 11:17:36 -080034 jfieldID ptr;
35 jfieldID inputApplicationHandle;
Jeff Brown9302c872011-07-13 22:51:29 -070036 jfieldID inputChannel;
37 jfieldID name;
38 jfieldID layoutParamsFlags;
39 jfieldID layoutParamsType;
40 jfieldID dispatchingTimeoutNanos;
41 jfieldID frameLeft;
42 jfieldID frameTop;
43 jfieldID frameRight;
44 jfieldID frameBottom;
45 jfieldID scaleFactor;
46 jfieldID touchableRegion;
47 jfieldID visible;
48 jfieldID canReceiveKeys;
49 jfieldID hasFocus;
50 jfieldID hasWallpaper;
51 jfieldID paused;
52 jfieldID layer;
53 jfieldID ownerPid;
54 jfieldID ownerUid;
55 jfieldID inputFeatures;
Jeff Brown83d616a2012-09-09 20:33:43 -070056 jfieldID displayId;
Jeff Brown928e0542011-01-10 11:17:36 -080057} gInputWindowHandleClassInfo;
58
59static Mutex gHandleMutex;
60
61
62// --- NativeInputWindowHandle ---
63
64NativeInputWindowHandle::NativeInputWindowHandle(
65 const sp<InputApplicationHandle>& inputApplicationHandle, jweak objWeak) :
66 InputWindowHandle(inputApplicationHandle),
67 mObjWeak(objWeak) {
68}
69
70NativeInputWindowHandle::~NativeInputWindowHandle() {
71 JNIEnv* env = AndroidRuntime::getJNIEnv();
72 env->DeleteWeakGlobalRef(mObjWeak);
73}
74
75jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
76 return env->NewLocalRef(mObjWeak);
77}
78
Jeff Browncc4f7db2011-08-30 20:34:48 -070079bool NativeInputWindowHandle::updateInfo() {
Jeff Brown9302c872011-07-13 22:51:29 -070080 JNIEnv* env = AndroidRuntime::getJNIEnv();
81 jobject obj = env->NewLocalRef(mObjWeak);
82 if (!obj) {
Jeff Browncc4f7db2011-08-30 20:34:48 -070083 releaseInfo();
Jeff Brown9302c872011-07-13 22:51:29 -070084 return false;
85 }
86
Jeff Browncc4f7db2011-08-30 20:34:48 -070087 if (!mInfo) {
88 mInfo = new InputWindowInfo();
Michael Wrighta407d6a2014-02-05 18:02:40 -080089 } else {
90 mInfo->touchableRegion.clear();
Jeff Browncc4f7db2011-08-30 20:34:48 -070091 }
92
Jeff Brown9302c872011-07-13 22:51:29 -070093 jobject inputChannelObj = env->GetObjectField(obj,
94 gInputWindowHandleClassInfo.inputChannel);
95 if (inputChannelObj) {
Jeff Browncc4f7db2011-08-30 20:34:48 -070096 mInfo->inputChannel = android_view_InputChannel_getInputChannel(env, inputChannelObj);
Jeff Brown9302c872011-07-13 22:51:29 -070097 env->DeleteLocalRef(inputChannelObj);
98 } else {
Jeff Browncc4f7db2011-08-30 20:34:48 -070099 mInfo->inputChannel.clear();
Jeff Brown9302c872011-07-13 22:51:29 -0700100 }
101
102 jstring nameObj = jstring(env->GetObjectField(obj,
103 gInputWindowHandleClassInfo.name));
104 if (nameObj) {
105 const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700106 mInfo->name.setTo(nameStr);
Jeff Brown9302c872011-07-13 22:51:29 -0700107 env->ReleaseStringUTFChars(nameObj, nameStr);
108 env->DeleteLocalRef(nameObj);
109 } else {
Jeff Browncc4f7db2011-08-30 20:34:48 -0700110 mInfo->name.setTo("<null>");
Jeff Brown9302c872011-07-13 22:51:29 -0700111 }
112
Jeff Browncc4f7db2011-08-30 20:34:48 -0700113 mInfo->layoutParamsFlags = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700114 gInputWindowHandleClassInfo.layoutParamsFlags);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700115 mInfo->layoutParamsType = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700116 gInputWindowHandleClassInfo.layoutParamsType);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700117 mInfo->dispatchingTimeout = env->GetLongField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700118 gInputWindowHandleClassInfo.dispatchingTimeoutNanos);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700119 mInfo->frameLeft = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700120 gInputWindowHandleClassInfo.frameLeft);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700121 mInfo->frameTop = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700122 gInputWindowHandleClassInfo.frameTop);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700123 mInfo->frameRight = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700124 gInputWindowHandleClassInfo.frameRight);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700125 mInfo->frameBottom = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700126 gInputWindowHandleClassInfo.frameBottom);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700127 mInfo->scaleFactor = env->GetFloatField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700128 gInputWindowHandleClassInfo.scaleFactor);
129
130 jobject regionObj = env->GetObjectField(obj,
131 gInputWindowHandleClassInfo.touchableRegion);
132 if (regionObj) {
133 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
Michael Wrighta407d6a2014-02-05 18:02:40 -0800134 for (SkRegion::Iterator it(*region); !it.done(); it.next()) {
135 const SkIRect& rect = it.rect();
136 mInfo->addTouchableRegion(Rect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom));
137 }
Jeff Brown9302c872011-07-13 22:51:29 -0700138 env->DeleteLocalRef(regionObj);
Jeff Brown9302c872011-07-13 22:51:29 -0700139 }
140
Jeff Browncc4f7db2011-08-30 20:34:48 -0700141 mInfo->visible = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700142 gInputWindowHandleClassInfo.visible);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700143 mInfo->canReceiveKeys = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700144 gInputWindowHandleClassInfo.canReceiveKeys);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700145 mInfo->hasFocus = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700146 gInputWindowHandleClassInfo.hasFocus);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700147 mInfo->hasWallpaper = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700148 gInputWindowHandleClassInfo.hasWallpaper);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700149 mInfo->paused = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700150 gInputWindowHandleClassInfo.paused);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700151 mInfo->layer = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700152 gInputWindowHandleClassInfo.layer);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700153 mInfo->ownerPid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700154 gInputWindowHandleClassInfo.ownerPid);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700155 mInfo->ownerUid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700156 gInputWindowHandleClassInfo.ownerUid);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700157 mInfo->inputFeatures = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700158 gInputWindowHandleClassInfo.inputFeatures);
Jeff Brown83d616a2012-09-09 20:33:43 -0700159 mInfo->displayId = env->GetIntField(obj,
160 gInputWindowHandleClassInfo.displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700161
162 env->DeleteLocalRef(obj);
163 return true;
164}
165
Jeff Brown928e0542011-01-10 11:17:36 -0800166
167// --- Global functions ---
168
169sp<NativeInputWindowHandle> android_server_InputWindowHandle_getHandle(
170 JNIEnv* env, jobject inputWindowHandleObj) {
171 if (!inputWindowHandleObj) {
172 return NULL;
173 }
174
175 AutoMutex _l(gHandleMutex);
176
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000177 jlong ptr = env->GetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800178 NativeInputWindowHandle* handle;
179 if (ptr) {
180 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
181 } else {
182 jobject inputApplicationHandleObj = env->GetObjectField(inputWindowHandleObj,
183 gInputWindowHandleClassInfo.inputApplicationHandle);
184 sp<InputApplicationHandle> inputApplicationHandle =
185 android_server_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
186 env->DeleteLocalRef(inputApplicationHandleObj);
187
188 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
189 handle = new NativeInputWindowHandle(inputApplicationHandle, objWeak);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800190 handle->incStrong((void*)android_server_InputWindowHandle_getHandle);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000191 env->SetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
192 reinterpret_cast<jlong>(handle));
Jeff Brown928e0542011-01-10 11:17:36 -0800193 }
194 return handle;
195}
196
197
198// --- JNI ---
199
200static void android_server_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
201 AutoMutex _l(gHandleMutex);
202
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000203 jlong ptr = env->GetLongField(obj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800204 if (ptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000205 env->SetLongField(obj, gInputWindowHandleClassInfo.ptr, 0);
Jeff Brown928e0542011-01-10 11:17:36 -0800206
207 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800208 handle->decStrong((void*)android_server_InputWindowHandle_getHandle);
Jeff Brown928e0542011-01-10 11:17:36 -0800209 }
210}
211
212
Daniel Micay76f6a862015-09-19 17:31:01 -0400213static const JNINativeMethod gInputWindowHandleMethods[] = {
Jeff Brown928e0542011-01-10 11:17:36 -0800214 /* name, signature, funcPtr */
215 { "nativeDispose", "()V",
216 (void*) android_server_InputWindowHandle_nativeDispose },
217};
218
219#define FIND_CLASS(var, className) \
220 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800221 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800222
223#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
224 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
225 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
226
227int register_android_server_InputWindowHandle(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -0700228 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputWindowHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800229 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +0100230 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown928e0542011-01-10 11:17:36 -0800231 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
232
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800233 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -0700234 FIND_CLASS(clazz, "com/android/server/input/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800235
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800236 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000237 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800238
239 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800240 clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700241 "inputApplicationHandle", "Lcom/android/server/input/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800242
Jeff Brown9302c872011-07-13 22:51:29 -0700243 GET_FIELD_ID(gInputWindowHandleClassInfo.inputChannel, clazz,
244 "inputChannel", "Landroid/view/InputChannel;");
245
246 GET_FIELD_ID(gInputWindowHandleClassInfo.name, clazz,
247 "name", "Ljava/lang/String;");
248
249 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsFlags, clazz,
250 "layoutParamsFlags", "I");
251
252 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsType, clazz,
253 "layoutParamsType", "I");
254
255 GET_FIELD_ID(gInputWindowHandleClassInfo.dispatchingTimeoutNanos, clazz,
256 "dispatchingTimeoutNanos", "J");
257
258 GET_FIELD_ID(gInputWindowHandleClassInfo.frameLeft, clazz,
259 "frameLeft", "I");
260
261 GET_FIELD_ID(gInputWindowHandleClassInfo.frameTop, clazz,
262 "frameTop", "I");
263
264 GET_FIELD_ID(gInputWindowHandleClassInfo.frameRight, clazz,
265 "frameRight", "I");
266
267 GET_FIELD_ID(gInputWindowHandleClassInfo.frameBottom, clazz,
268 "frameBottom", "I");
269
270 GET_FIELD_ID(gInputWindowHandleClassInfo.scaleFactor, clazz,
271 "scaleFactor", "F");
272
273 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegion, clazz,
274 "touchableRegion", "Landroid/graphics/Region;");
275
276 GET_FIELD_ID(gInputWindowHandleClassInfo.visible, clazz,
277 "visible", "Z");
278
279 GET_FIELD_ID(gInputWindowHandleClassInfo.canReceiveKeys, clazz,
280 "canReceiveKeys", "Z");
281
282 GET_FIELD_ID(gInputWindowHandleClassInfo.hasFocus, clazz,
283 "hasFocus", "Z");
284
285 GET_FIELD_ID(gInputWindowHandleClassInfo.hasWallpaper, clazz,
286 "hasWallpaper", "Z");
287
288 GET_FIELD_ID(gInputWindowHandleClassInfo.paused, clazz,
289 "paused", "Z");
290
291 GET_FIELD_ID(gInputWindowHandleClassInfo.layer, clazz,
292 "layer", "I");
293
294 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerPid, clazz,
295 "ownerPid", "I");
296
297 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerUid, clazz,
298 "ownerUid", "I");
299
300 GET_FIELD_ID(gInputWindowHandleClassInfo.inputFeatures, clazz,
301 "inputFeatures", "I");
Jeff Brown83d616a2012-09-09 20:33:43 -0700302
303 GET_FIELD_ID(gInputWindowHandleClassInfo.displayId, clazz,
304 "displayId", "I");
Jeff Brown928e0542011-01-10 11:17:36 -0800305 return 0;
306}
307
308} /* namespace android */