com_android_server_input_InputApplicationHandle.cpp revision 17cc33a35729733aaa0a7706f38b1c45f0b1590a
1928e054931d357326613c78e62f4d850b7c442ffJeff Brown/*
2928e054931d357326613c78e62f4d850b7c442ffJeff Brown * Copyright (C) 2011 The Android Open Source Project
3928e054931d357326613c78e62f4d850b7c442ffJeff Brown *
4928e054931d357326613c78e62f4d850b7c442ffJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5928e054931d357326613c78e62f4d850b7c442ffJeff Brown * you may not use this file except in compliance with the License.
6928e054931d357326613c78e62f4d850b7c442ffJeff Brown * You may obtain a copy of the License at
7928e054931d357326613c78e62f4d850b7c442ffJeff Brown *
8928e054931d357326613c78e62f4d850b7c442ffJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9928e054931d357326613c78e62f4d850b7c442ffJeff Brown *
10928e054931d357326613c78e62f4d850b7c442ffJeff Brown * Unless required by applicable law or agreed to in writing, software
11928e054931d357326613c78e62f4d850b7c442ffJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12928e054931d357326613c78e62f4d850b7c442ffJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13928e054931d357326613c78e62f4d850b7c442ffJeff Brown * See the License for the specific language governing permissions and
14928e054931d357326613c78e62f4d850b7c442ffJeff Brown * limitations under the License.
15928e054931d357326613c78e62f4d850b7c442ffJeff Brown */
16928e054931d357326613c78e62f4d850b7c442ffJeff Brown
17928e054931d357326613c78e62f4d850b7c442ffJeff Brown#define LOG_TAG "InputApplicationHandle"
18928e054931d357326613c78e62f4d850b7c442ffJeff Brown
19928e054931d357326613c78e62f4d850b7c442ffJeff Brown#include "JNIHelp.h"
20928e054931d357326613c78e62f4d850b7c442ffJeff Brown#include "jni.h"
21928e054931d357326613c78e62f4d850b7c442ffJeff Brown#include <android_runtime/AndroidRuntime.h>
22928e054931d357326613c78e62f4d850b7c442ffJeff Brown#include <utils/threads.h>
23928e054931d357326613c78e62f4d850b7c442ffJeff Brown
24928e054931d357326613c78e62f4d850b7c442ffJeff Brown#include "com_android_server_InputApplicationHandle.h"
25928e054931d357326613c78e62f4d850b7c442ffJeff Brown
26928e054931d357326613c78e62f4d850b7c442ffJeff Brownnamespace android {
27928e054931d357326613c78e62f4d850b7c442ffJeff Brown
28928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic struct {
29928e054931d357326613c78e62f4d850b7c442ffJeff Brown    jfieldID ptr;
30928e054931d357326613c78e62f4d850b7c442ffJeff Brown} gInputApplicationHandleClassInfo;
31928e054931d357326613c78e62f4d850b7c442ffJeff Brown
32928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic Mutex gHandleMutex;
33928e054931d357326613c78e62f4d850b7c442ffJeff Brown
34928e054931d357326613c78e62f4d850b7c442ffJeff Brown
35928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- NativeInputApplicationHandle ---
36928e054931d357326613c78e62f4d850b7c442ffJeff Brown
37928e054931d357326613c78e62f4d850b7c442ffJeff BrownNativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
38928e054931d357326613c78e62f4d850b7c442ffJeff Brown        mObjWeak(objWeak) {
39928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
40928e054931d357326613c78e62f4d850b7c442ffJeff Brown
41928e054931d357326613c78e62f4d850b7c442ffJeff BrownNativeInputApplicationHandle::~NativeInputApplicationHandle() {
42928e054931d357326613c78e62f4d850b7c442ffJeff Brown    JNIEnv* env = AndroidRuntime::getJNIEnv();
43928e054931d357326613c78e62f4d850b7c442ffJeff Brown    env->DeleteWeakGlobalRef(mObjWeak);
44928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
45928e054931d357326613c78e62f4d850b7c442ffJeff Brown
46928e054931d357326613c78e62f4d850b7c442ffJeff Brownjobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
47928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return env->NewLocalRef(mObjWeak);
48928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
49928e054931d357326613c78e62f4d850b7c442ffJeff Brown
50928e054931d357326613c78e62f4d850b7c442ffJeff Brown
51928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- Global functions ---
52928e054931d357326613c78e62f4d850b7c442ffJeff Brown
53928e054931d357326613c78e62f4d850b7c442ffJeff Brownsp<InputApplicationHandle> android_server_InputApplicationHandle_getHandle(
54928e054931d357326613c78e62f4d850b7c442ffJeff Brown        JNIEnv* env, jobject inputApplicationHandleObj) {
55928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (!inputApplicationHandleObj) {
56928e054931d357326613c78e62f4d850b7c442ffJeff Brown        return NULL;
57928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
58928e054931d357326613c78e62f4d850b7c442ffJeff Brown
59928e054931d357326613c78e62f4d850b7c442ffJeff Brown    AutoMutex _l(gHandleMutex);
60928e054931d357326613c78e62f4d850b7c442ffJeff Brown
61928e054931d357326613c78e62f4d850b7c442ffJeff Brown    int ptr = env->GetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
62928e054931d357326613c78e62f4d850b7c442ffJeff Brown    NativeInputApplicationHandle* handle;
63928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (ptr) {
64928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
65928e054931d357326613c78e62f4d850b7c442ffJeff Brown    } else {
66928e054931d357326613c78e62f4d850b7c442ffJeff Brown        jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
67928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle = new NativeInputApplicationHandle(objWeak);
68928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle->incStrong(inputApplicationHandleObj);
69928e054931d357326613c78e62f4d850b7c442ffJeff Brown        env->SetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
70928e054931d357326613c78e62f4d850b7c442ffJeff Brown                reinterpret_cast<int>(handle));
71928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
72928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return handle;
73928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
74928e054931d357326613c78e62f4d850b7c442ffJeff Brown
75928e054931d357326613c78e62f4d850b7c442ffJeff Brown
76928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- JNI ---
77928e054931d357326613c78e62f4d850b7c442ffJeff Brown
78928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic void android_server_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
79928e054931d357326613c78e62f4d850b7c442ffJeff Brown    AutoMutex _l(gHandleMutex);
80928e054931d357326613c78e62f4d850b7c442ffJeff Brown
81928e054931d357326613c78e62f4d850b7c442ffJeff Brown    int ptr = env->GetIntField(obj, gInputApplicationHandleClassInfo.ptr);
82928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (ptr) {
83928e054931d357326613c78e62f4d850b7c442ffJeff Brown        env->SetIntField(obj, gInputApplicationHandleClassInfo.ptr, 0);
84928e054931d357326613c78e62f4d850b7c442ffJeff Brown
85928e054931d357326613c78e62f4d850b7c442ffJeff Brown        NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
86928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle->decStrong(obj);
87928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
88928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
89928e054931d357326613c78e62f4d850b7c442ffJeff Brown
90928e054931d357326613c78e62f4d850b7c442ffJeff Brown
91928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic JNINativeMethod gInputApplicationHandleMethods[] = {
92928e054931d357326613c78e62f4d850b7c442ffJeff Brown    /* name, signature, funcPtr */
93928e054931d357326613c78e62f4d850b7c442ffJeff Brown    { "nativeDispose", "()V",
94928e054931d357326613c78e62f4d850b7c442ffJeff Brown            (void*) android_server_InputApplicationHandle_nativeDispose },
95928e054931d357326613c78e62f4d850b7c442ffJeff Brown};
96928e054931d357326613c78e62f4d850b7c442ffJeff Brown
97928e054931d357326613c78e62f4d850b7c442ffJeff Brown#define FIND_CLASS(var, className) \
98928e054931d357326613c78e62f4d850b7c442ffJeff Brown        var = env->FindClass(className); \
9917cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro        LOG_FATAL_IF(! var, "Unable to find class " className);
100928e054931d357326613c78e62f4d850b7c442ffJeff Brown
101928e054931d357326613c78e62f4d850b7c442ffJeff Brown#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
102928e054931d357326613c78e62f4d850b7c442ffJeff Brown        var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
103928e054931d357326613c78e62f4d850b7c442ffJeff Brown        LOG_FATAL_IF(! var, "Unable to find field " fieldName);
104928e054931d357326613c78e62f4d850b7c442ffJeff Brown
105928e054931d357326613c78e62f4d850b7c442ffJeff Brownint register_android_server_InputApplicationHandle(JNIEnv* env) {
106a924dc0db952fe32509435fdb8dc9c84a9e181f3Dianne Hackborn    int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputApplicationHandle",
107928e054931d357326613c78e62f4d850b7c442ffJeff Brown            gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
108928e054931d357326613c78e62f4d850b7c442ffJeff Brown    LOG_FATAL_IF(res < 0, "Unable to register native methods.");
109928e054931d357326613c78e62f4d850b7c442ffJeff Brown
11017cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    jclass clazz;
11117cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    FIND_CLASS(clazz, "com/android/server/wm/InputApplicationHandle");
112928e054931d357326613c78e62f4d850b7c442ffJeff Brown
11317cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
114928e054931d357326613c78e62f4d850b7c442ffJeff Brown            "ptr", "I");
115928e054931d357326613c78e62f4d850b7c442ffJeff Brown
116928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return 0;
117928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
118928e054931d357326613c78e62f4d850b7c442ffJeff Brown
119928e054931d357326613c78e62f4d850b7c442ffJeff Brown} /* namespace android */
120