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
244532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown#include "com_android_server_input_InputApplicationHandle.h"
25928e054931d357326613c78e62f4d850b7c442ffJeff Brown
26928e054931d357326613c78e62f4d850b7c442ffJeff Brownnamespace android {
27928e054931d357326613c78e62f4d850b7c442ffJeff Brown
28928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic struct {
29928e054931d357326613c78e62f4d850b7c442ffJeff Brown    jfieldID ptr;
309302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    jfieldID name;
319302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    jfieldID dispatchingTimeoutNanos;
32928e054931d357326613c78e62f4d850b7c442ffJeff Brown} gInputApplicationHandleClassInfo;
33928e054931d357326613c78e62f4d850b7c442ffJeff Brown
34928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic Mutex gHandleMutex;
35928e054931d357326613c78e62f4d850b7c442ffJeff Brown
36928e054931d357326613c78e62f4d850b7c442ffJeff Brown
37928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- NativeInputApplicationHandle ---
38928e054931d357326613c78e62f4d850b7c442ffJeff Brown
39928e054931d357326613c78e62f4d850b7c442ffJeff BrownNativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
40928e054931d357326613c78e62f4d850b7c442ffJeff Brown        mObjWeak(objWeak) {
41928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
42928e054931d357326613c78e62f4d850b7c442ffJeff Brown
43928e054931d357326613c78e62f4d850b7c442ffJeff BrownNativeInputApplicationHandle::~NativeInputApplicationHandle() {
44928e054931d357326613c78e62f4d850b7c442ffJeff Brown    JNIEnv* env = AndroidRuntime::getJNIEnv();
45928e054931d357326613c78e62f4d850b7c442ffJeff Brown    env->DeleteWeakGlobalRef(mObjWeak);
46928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
47928e054931d357326613c78e62f4d850b7c442ffJeff Brown
48928e054931d357326613c78e62f4d850b7c442ffJeff Brownjobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
49928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return env->NewLocalRef(mObjWeak);
50928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
51928e054931d357326613c78e62f4d850b7c442ffJeff Brown
52cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brownbool NativeInputApplicationHandle::updateInfo() {
539302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    JNIEnv* env = AndroidRuntime::getJNIEnv();
549302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    jobject obj = env->NewLocalRef(mObjWeak);
559302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    if (!obj) {
56cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        releaseInfo();
579302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        return false;
589302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    }
599302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
60cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown    if (!mInfo) {
61cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        mInfo = new InputApplicationInfo();
62cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown    }
63cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown
649302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    jstring nameObj = jstring(env->GetObjectField(obj,
659302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            gInputApplicationHandleClassInfo.name));
669302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    if (nameObj) {
679302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
68cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        mInfo->name.setTo(nameStr);
699302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        env->ReleaseStringUTFChars(nameObj, nameStr);
709302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown        env->DeleteLocalRef(nameObj);
719302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    } else {
72cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown        mInfo->name.setTo("<null>");
739302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    }
749302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
75cc4f7db698f88b633a286d8ab1105b28a474cd09Jeff Brown    mInfo->dispatchingTimeout = env->GetLongField(obj,
769302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
779302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
789302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    env->DeleteLocalRef(obj);
799302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    return true;
809302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown}
819302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
82928e054931d357326613c78e62f4d850b7c442ffJeff Brown
83928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- Global functions ---
84928e054931d357326613c78e62f4d850b7c442ffJeff Brown
85928e054931d357326613c78e62f4d850b7c442ffJeff Brownsp<InputApplicationHandle> android_server_InputApplicationHandle_getHandle(
86928e054931d357326613c78e62f4d850b7c442ffJeff Brown        JNIEnv* env, jobject inputApplicationHandleObj) {
87928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (!inputApplicationHandleObj) {
88928e054931d357326613c78e62f4d850b7c442ffJeff Brown        return NULL;
89928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
90928e054931d357326613c78e62f4d850b7c442ffJeff Brown
91928e054931d357326613c78e62f4d850b7c442ffJeff Brown    AutoMutex _l(gHandleMutex);
92928e054931d357326613c78e62f4d850b7c442ffJeff Brown
937e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat    jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
94928e054931d357326613c78e62f4d850b7c442ffJeff Brown    NativeInputApplicationHandle* handle;
95928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (ptr) {
96928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
97928e054931d357326613c78e62f4d850b7c442ffJeff Brown    } else {
98928e054931d357326613c78e62f4d850b7c442ffJeff Brown        jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
99928e054931d357326613c78e62f4d850b7c442ffJeff Brown        handle = new NativeInputApplicationHandle(objWeak);
100b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian        handle->incStrong((void*)android_server_InputApplicationHandle_getHandle);
1017e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat        env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
1027e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat                reinterpret_cast<jlong>(handle));
103928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
104928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return handle;
105928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
106928e054931d357326613c78e62f4d850b7c442ffJeff Brown
107928e054931d357326613c78e62f4d850b7c442ffJeff Brown
108928e054931d357326613c78e62f4d850b7c442ffJeff Brown// --- JNI ---
109928e054931d357326613c78e62f4d850b7c442ffJeff Brown
110928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic void android_server_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
111928e054931d357326613c78e62f4d850b7c442ffJeff Brown    AutoMutex _l(gHandleMutex);
112928e054931d357326613c78e62f4d850b7c442ffJeff Brown
1137e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat    jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
114928e054931d357326613c78e62f4d850b7c442ffJeff Brown    if (ptr) {
1157e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat        env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
116928e054931d357326613c78e62f4d850b7c442ffJeff Brown
117928e054931d357326613c78e62f4d850b7c442ffJeff Brown        NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
118b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian        handle->decStrong((void*)android_server_InputApplicationHandle_getHandle);
119928e054931d357326613c78e62f4d850b7c442ffJeff Brown    }
120928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
121928e054931d357326613c78e62f4d850b7c442ffJeff Brown
122928e054931d357326613c78e62f4d850b7c442ffJeff Brown
123928e054931d357326613c78e62f4d850b7c442ffJeff Brownstatic JNINativeMethod gInputApplicationHandleMethods[] = {
124928e054931d357326613c78e62f4d850b7c442ffJeff Brown    /* name, signature, funcPtr */
125928e054931d357326613c78e62f4d850b7c442ffJeff Brown    { "nativeDispose", "()V",
126928e054931d357326613c78e62f4d850b7c442ffJeff Brown            (void*) android_server_InputApplicationHandle_nativeDispose },
127928e054931d357326613c78e62f4d850b7c442ffJeff Brown};
128928e054931d357326613c78e62f4d850b7c442ffJeff Brown
129928e054931d357326613c78e62f4d850b7c442ffJeff Brown#define FIND_CLASS(var, className) \
130928e054931d357326613c78e62f4d850b7c442ffJeff Brown        var = env->FindClass(className); \
13117cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro        LOG_FATAL_IF(! var, "Unable to find class " className);
132928e054931d357326613c78e62f4d850b7c442ffJeff Brown
133928e054931d357326613c78e62f4d850b7c442ffJeff Brown#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
134928e054931d357326613c78e62f4d850b7c442ffJeff Brown        var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
135928e054931d357326613c78e62f4d850b7c442ffJeff Brown        LOG_FATAL_IF(! var, "Unable to find field " fieldName);
136928e054931d357326613c78e62f4d850b7c442ffJeff Brown
137928e054931d357326613c78e62f4d850b7c442ffJeff Brownint register_android_server_InputApplicationHandle(JNIEnv* env) {
1384532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    int res = jniRegisterNativeMethods(env, "com/android/server/input/InputApplicationHandle",
139928e054931d357326613c78e62f4d850b7c442ffJeff Brown            gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
140928e054931d357326613c78e62f4d850b7c442ffJeff Brown    LOG_FATAL_IF(res < 0, "Unable to register native methods.");
141928e054931d357326613c78e62f4d850b7c442ffJeff Brown
14217cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    jclass clazz;
1434532e6158474a263d9d26c2b42240bcf7ce9b172Jeff Brown    FIND_CLASS(clazz, "com/android/server/input/InputApplicationHandle");
144928e054931d357326613c78e62f4d850b7c442ffJeff Brown
14517cc33a35729733aaa0a7706f38b1c45f0b1590aCarl Shapiro    GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
1467e2a9dcf1a4044ed3f81c5b736b5027231395b73Ashok Bhat            "ptr", "J");
147928e054931d357326613c78e62f4d850b7c442ffJeff Brown
1489302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
1499302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            "name", "Ljava/lang/String;");
1509302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
1519302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown    GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos,
1529302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            clazz,
1539302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown            "dispatchingTimeoutNanos", "J");
1549302c8796fc4dcda08d4bd1e11733848fd4fafafJeff Brown
155928e054931d357326613c78e62f4d850b7c442ffJeff Brown    return 0;
156928e054931d357326613c78e62f4d850b7c442ffJeff Brown}
157928e054931d357326613c78e62f4d850b7c442ffJeff Brown
158928e054931d357326613c78e62f4d850b7c442ffJeff Brown} /* namespace android */
159