Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 ctor; |
| 34 | |
| 35 | jfieldID mMetaState; |
| 36 | jfieldID mAction; |
| 37 | jfieldID mKeyCode; |
| 38 | jfieldID mScanCode; |
| 39 | jfieldID mRepeatCount; |
| 40 | jfieldID mDeviceId; |
| 41 | jfieldID mFlags; |
| 42 | jfieldID mDownTime; |
| 43 | jfieldID mEventTime; |
| 44 | jfieldID mCharacters; |
| 45 | } gKeyEventClassInfo; |
| 46 | |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
| 49 | jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) { |
| 50 | return env->NewObject(gKeyEventClassInfo.clazz, gKeyEventClassInfo.ctor, |
| 51 | nanoseconds_to_milliseconds(event->getDownTime()), |
| 52 | nanoseconds_to_milliseconds(event->getEventTime()), |
| 53 | event->getAction(), |
| 54 | event->getKeyCode(), |
| 55 | event->getRepeatCount(), |
| 56 | event->getMetaState(), |
| 57 | event->getDeviceId(), |
| 58 | event->getScanCode(), |
| 59 | event->getFlags()); |
| 60 | } |
| 61 | |
| 62 | void android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj, int32_t nature, |
| 63 | KeyEvent* event) { |
| 64 | jint metaState = env->GetIntField(eventObj, gKeyEventClassInfo.mMetaState); |
| 65 | jint action = env->GetIntField(eventObj, gKeyEventClassInfo.mAction); |
| 66 | jint keyCode = env->GetIntField(eventObj, gKeyEventClassInfo.mKeyCode); |
| 67 | jint scanCode = env->GetIntField(eventObj, gKeyEventClassInfo.mScanCode); |
| 68 | jint repeatCount = env->GetIntField(eventObj, gKeyEventClassInfo.mRepeatCount); |
| 69 | jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId); |
| 70 | jint flags = env->GetIntField(eventObj, gKeyEventClassInfo.mFlags); |
| 71 | jlong downTime = env->GetLongField(eventObj, gKeyEventClassInfo.mDownTime); |
| 72 | jlong eventTime = env->GetLongField(eventObj, gKeyEventClassInfo.mEventTime); |
| 73 | |
| 74 | event->initialize(deviceId, nature, action, flags, keyCode, scanCode, metaState, repeatCount, |
| 75 | milliseconds_to_nanoseconds(downTime), |
| 76 | milliseconds_to_nanoseconds(eventTime)); |
| 77 | } |
| 78 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame^] | 79 | static jboolean native_isSystemKey(JNIEnv* env, jobject clazz, jint keyCode) { |
| 80 | return KeyEvent::isSystemKey(keyCode); |
| 81 | } |
| 82 | |
| 83 | static jboolean native_hasDefaultAction(JNIEnv* env, jobject clazz, jint keyCode) { |
| 84 | return KeyEvent::hasDefaultAction(keyCode); |
| 85 | } |
| 86 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 87 | // ---------------------------------------------------------------------------- |
| 88 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame^] | 89 | static const JNINativeMethod g_methods[] = { |
| 90 | { "native_isSystemKey", "(I)Z", (void*)native_isSystemKey }, |
| 91 | { "native_hasDefaultAction", "(I)Z", (void*)native_hasDefaultAction }, |
| 92 | }; |
| 93 | |
| 94 | static const char* const kKeyEventPathName = "android/view/KeyEvent"; |
| 95 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 96 | #define FIND_CLASS(var, className) \ |
| 97 | var = env->FindClass(className); \ |
| 98 | LOG_FATAL_IF(! var, "Unable to find class " className); \ |
| 99 | var = jclass(env->NewGlobalRef(var)); |
| 100 | |
| 101 | #define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \ |
| 102 | var = env->GetMethodID(clazz, methodName, fieldDescriptor); \ |
| 103 | LOG_FATAL_IF(! var, "Unable to find method" methodName); |
| 104 | |
| 105 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 106 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 107 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 108 | |
| 109 | int register_android_view_KeyEvent(JNIEnv* env) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame^] | 110 | FIND_CLASS(gKeyEventClassInfo.clazz, kKeyEventPathName); |
| 111 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | GET_METHOD_ID(gKeyEventClassInfo.ctor, gKeyEventClassInfo.clazz, |
| 113 | "<init>", "(JJIIIIIII)V"); |
| 114 | |
| 115 | GET_FIELD_ID(gKeyEventClassInfo.mMetaState, gKeyEventClassInfo.clazz, |
| 116 | "mMetaState", "I"); |
| 117 | GET_FIELD_ID(gKeyEventClassInfo.mAction, gKeyEventClassInfo.clazz, |
| 118 | "mAction", "I"); |
| 119 | GET_FIELD_ID(gKeyEventClassInfo.mKeyCode, gKeyEventClassInfo.clazz, |
| 120 | "mKeyCode", "I"); |
| 121 | GET_FIELD_ID(gKeyEventClassInfo.mScanCode, gKeyEventClassInfo.clazz, |
| 122 | "mScanCode", "I"); |
| 123 | GET_FIELD_ID(gKeyEventClassInfo.mRepeatCount, gKeyEventClassInfo.clazz, |
| 124 | "mRepeatCount", "I"); |
| 125 | GET_FIELD_ID(gKeyEventClassInfo.mDeviceId, gKeyEventClassInfo.clazz, |
| 126 | "mDeviceId", "I"); |
| 127 | GET_FIELD_ID(gKeyEventClassInfo.mFlags, gKeyEventClassInfo.clazz, |
| 128 | "mFlags", "I"); |
| 129 | GET_FIELD_ID(gKeyEventClassInfo.mDownTime, gKeyEventClassInfo.clazz, |
| 130 | "mDownTime", "J"); |
| 131 | GET_FIELD_ID(gKeyEventClassInfo.mEventTime, gKeyEventClassInfo.clazz, |
| 132 | "mEventTime", "J"); |
| 133 | GET_FIELD_ID(gKeyEventClassInfo.mCharacters, gKeyEventClassInfo.clazz, |
| 134 | "mCharacters", "Ljava/lang/String;"); |
| 135 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame^] | 136 | return AndroidRuntime::registerNativeMethods( |
| 137 | env, kKeyEventPathName, |
| 138 | g_methods, NELEM(g_methods)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace android |