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