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