• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "KeyEvent-JNI"
18 
19 #include "JNIHelp.h"
20 
21 #include <android_runtime/AndroidRuntime.h>
22 #include <android_runtime/Log.h>
23 #include <utils/Log.h>
24 #include <input/Input.h>
25 #include "android_view_KeyEvent.h"
26 
27 namespace android {
28 
29 // ----------------------------------------------------------------------------
30 
31 static struct {
32     jclass clazz;
33 
34     jmethodID obtain;
35     jmethodID recycle;
36 
37     jfieldID mDeviceId;
38     jfieldID mSource;
39     jfieldID mMetaState;
40     jfieldID mAction;
41     jfieldID mKeyCode;
42     jfieldID mScanCode;
43     jfieldID mRepeatCount;
44     jfieldID mFlags;
45     jfieldID mDownTime;
46     jfieldID mEventTime;
47     jfieldID mCharacters;
48 } gKeyEventClassInfo;
49 
50 // ----------------------------------------------------------------------------
51 
android_view_KeyEvent_fromNative(JNIEnv * env,const KeyEvent * event)52 jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) {
53     jobject eventObj = env->CallStaticObjectMethod(gKeyEventClassInfo.clazz,
54             gKeyEventClassInfo.obtain,
55             nanoseconds_to_milliseconds(event->getDownTime()),
56             nanoseconds_to_milliseconds(event->getEventTime()),
57             event->getAction(),
58             event->getKeyCode(),
59             event->getRepeatCount(),
60             event->getMetaState(),
61             event->getDeviceId(),
62             event->getScanCode(),
63             event->getFlags(),
64             event->getSource(),
65             NULL);
66     if (env->ExceptionCheck()) {
67         ALOGE("An exception occurred while obtaining a key event.");
68         LOGE_EX(env);
69         env->ExceptionClear();
70         return NULL;
71     }
72     return eventObj;
73 }
74 
android_view_KeyEvent_toNative(JNIEnv * env,jobject eventObj,KeyEvent * event)75 status_t android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj,
76         KeyEvent* event) {
77     jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId);
78     jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource);
79     jint metaState = env->GetIntField(eventObj, gKeyEventClassInfo.mMetaState);
80     jint action = env->GetIntField(eventObj, gKeyEventClassInfo.mAction);
81     jint keyCode = env->GetIntField(eventObj, gKeyEventClassInfo.mKeyCode);
82     jint scanCode = env->GetIntField(eventObj, gKeyEventClassInfo.mScanCode);
83     jint repeatCount = env->GetIntField(eventObj, gKeyEventClassInfo.mRepeatCount);
84     jint flags = env->GetIntField(eventObj, gKeyEventClassInfo.mFlags);
85     jlong downTime = env->GetLongField(eventObj, gKeyEventClassInfo.mDownTime);
86     jlong eventTime = env->GetLongField(eventObj, gKeyEventClassInfo.mEventTime);
87 
88     event->initialize(deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount,
89             milliseconds_to_nanoseconds(downTime),
90             milliseconds_to_nanoseconds(eventTime));
91     return OK;
92 }
93 
android_view_KeyEvent_recycle(JNIEnv * env,jobject eventObj)94 status_t android_view_KeyEvent_recycle(JNIEnv* env, jobject eventObj) {
95     env->CallVoidMethod(eventObj, gKeyEventClassInfo.recycle);
96     if (env->ExceptionCheck()) {
97         ALOGW("An exception occurred while recycling a key event.");
98         LOGW_EX(env);
99         env->ExceptionClear();
100         return UNKNOWN_ERROR;
101     }
102     return OK;
103 }
104 
native_isSystemKey(JNIEnv * env,jobject clazz,jint keyCode)105 static jboolean native_isSystemKey(JNIEnv* env, jobject clazz, jint keyCode) {
106     return KeyEvent::isSystemKey(keyCode);
107 }
108 
native_hasDefaultAction(JNIEnv * env,jobject clazz,jint keyCode)109 static jboolean native_hasDefaultAction(JNIEnv* env, jobject clazz, jint keyCode) {
110     return KeyEvent::hasDefaultAction(keyCode);
111 }
112 
113 
114 // ----------------------------------------------------------------------------
115 
116 static const JNINativeMethod g_methods[] = {
117     { "native_isSystemKey", "(I)Z", (void*)native_isSystemKey },
118     { "native_hasDefaultAction", "(I)Z", (void*)native_hasDefaultAction },
119 };
120 
121 #define FIND_CLASS(var, className) \
122         var = env->FindClass(className); \
123         LOG_FATAL_IF(! var, "Unable to find class " className); \
124         var = jclass(env->NewGlobalRef(var));
125 
126 #define GET_STATIC_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
127         var = env->GetStaticMethodID(clazz, methodName, fieldDescriptor); \
128         LOG_FATAL_IF(! var, "Unable to find static method" methodName);
129 
130 #define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
131         var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
132         LOG_FATAL_IF(! var, "Unable to find method" methodName);
133 
134 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
135         var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
136         LOG_FATAL_IF(! var, "Unable to find field " fieldName);
137 
register_android_view_KeyEvent(JNIEnv * env)138 int register_android_view_KeyEvent(JNIEnv* env) {
139     FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
140 
141     GET_STATIC_METHOD_ID(gKeyEventClassInfo.obtain, gKeyEventClassInfo.clazz,
142             "obtain", "(JJIIIIIIIILjava/lang/String;)Landroid/view/KeyEvent;");
143     GET_METHOD_ID(gKeyEventClassInfo.recycle, gKeyEventClassInfo.clazz,
144             "recycle", "()V");
145 
146     GET_FIELD_ID(gKeyEventClassInfo.mDeviceId, gKeyEventClassInfo.clazz,
147             "mDeviceId", "I");
148     GET_FIELD_ID(gKeyEventClassInfo.mSource, gKeyEventClassInfo.clazz,
149             "mSource", "I");
150     GET_FIELD_ID(gKeyEventClassInfo.mMetaState, gKeyEventClassInfo.clazz,
151             "mMetaState", "I");
152     GET_FIELD_ID(gKeyEventClassInfo.mAction, gKeyEventClassInfo.clazz,
153             "mAction", "I");
154     GET_FIELD_ID(gKeyEventClassInfo.mKeyCode, gKeyEventClassInfo.clazz,
155             "mKeyCode", "I");
156     GET_FIELD_ID(gKeyEventClassInfo.mScanCode, gKeyEventClassInfo.clazz,
157             "mScanCode", "I");
158     GET_FIELD_ID(gKeyEventClassInfo.mRepeatCount, gKeyEventClassInfo.clazz,
159             "mRepeatCount", "I");
160     GET_FIELD_ID(gKeyEventClassInfo.mFlags, gKeyEventClassInfo.clazz,
161             "mFlags", "I");
162     GET_FIELD_ID(gKeyEventClassInfo.mDownTime, gKeyEventClassInfo.clazz,
163             "mDownTime", "J");
164     GET_FIELD_ID(gKeyEventClassInfo.mEventTime, gKeyEventClassInfo.clazz,
165             "mEventTime", "J");
166     GET_FIELD_ID(gKeyEventClassInfo.mCharacters, gKeyEventClassInfo.clazz,
167             "mCharacters", "Ljava/lang/String;");
168 
169     return AndroidRuntime::registerNativeMethods(
170         env, "android/view/KeyEvent", g_methods, NELEM(g_methods));
171 }
172 
173 } // namespace android
174