blob: 46ec1f4346db9994ac1c982caada0a86e3740d2a [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
213static JNINativeMethod gInputWindowHandleMethods[] = {
214 /* 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));
230 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
231
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800232 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -0700233 FIND_CLASS(clazz, "com/android/server/input/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800234
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800235 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000236 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800237
238 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800239 clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700240 "inputApplicationHandle", "Lcom/android/server/input/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800241
Jeff Brown9302c872011-07-13 22:51:29 -0700242 GET_FIELD_ID(gInputWindowHandleClassInfo.inputChannel, clazz,
243 "inputChannel", "Landroid/view/InputChannel;");
244
245 GET_FIELD_ID(gInputWindowHandleClassInfo.name, clazz,
246 "name", "Ljava/lang/String;");
247
248 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsFlags, clazz,
249 "layoutParamsFlags", "I");
250
251 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsType, clazz,
252 "layoutParamsType", "I");
253
254 GET_FIELD_ID(gInputWindowHandleClassInfo.dispatchingTimeoutNanos, clazz,
255 "dispatchingTimeoutNanos", "J");
256
257 GET_FIELD_ID(gInputWindowHandleClassInfo.frameLeft, clazz,
258 "frameLeft", "I");
259
260 GET_FIELD_ID(gInputWindowHandleClassInfo.frameTop, clazz,
261 "frameTop", "I");
262
263 GET_FIELD_ID(gInputWindowHandleClassInfo.frameRight, clazz,
264 "frameRight", "I");
265
266 GET_FIELD_ID(gInputWindowHandleClassInfo.frameBottom, clazz,
267 "frameBottom", "I");
268
269 GET_FIELD_ID(gInputWindowHandleClassInfo.scaleFactor, clazz,
270 "scaleFactor", "F");
271
272 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegion, clazz,
273 "touchableRegion", "Landroid/graphics/Region;");
274
275 GET_FIELD_ID(gInputWindowHandleClassInfo.visible, clazz,
276 "visible", "Z");
277
278 GET_FIELD_ID(gInputWindowHandleClassInfo.canReceiveKeys, clazz,
279 "canReceiveKeys", "Z");
280
281 GET_FIELD_ID(gInputWindowHandleClassInfo.hasFocus, clazz,
282 "hasFocus", "Z");
283
284 GET_FIELD_ID(gInputWindowHandleClassInfo.hasWallpaper, clazz,
285 "hasWallpaper", "Z");
286
287 GET_FIELD_ID(gInputWindowHandleClassInfo.paused, clazz,
288 "paused", "Z");
289
290 GET_FIELD_ID(gInputWindowHandleClassInfo.layer, clazz,
291 "layer", "I");
292
293 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerPid, clazz,
294 "ownerPid", "I");
295
296 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerUid, clazz,
297 "ownerUid", "I");
298
299 GET_FIELD_ID(gInputWindowHandleClassInfo.inputFeatures, clazz,
300 "inputFeatures", "I");
Jeff Brown83d616a2012-09-09 20:33:43 -0700301
302 GET_FIELD_ID(gInputWindowHandleClassInfo.displayId, clazz,
303 "displayId", "I");
Jeff Brown928e0542011-01-10 11:17:36 -0800304 return 0;
305}
306
307} /* namespace android */