Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 17 | #include <input/Input.h> |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android_runtime/AndroidRuntime.h> |
| 20 | #include <nativehelper/jni.h> |
| 21 | #include <nativehelper/JNIHelp.h> |
| 22 | |
| 23 | #include <ScopedLocalRef.h> |
| 24 | |
| 25 | #include "android_view_InputDevice.h" |
| 26 | #include "android_view_KeyCharacterMap.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | static struct { |
| 31 | jclass clazz; |
| 32 | |
| 33 | jmethodID ctor; |
| 34 | jmethodID addMotionRange; |
| 35 | } gInputDeviceClassInfo; |
| 36 | |
| 37 | jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) { |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 38 | ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().string())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 39 | if (!nameObj.get()) { |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | ScopedLocalRef<jstring> descriptorObj(env, |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 44 | env->NewStringUTF(deviceInfo.getIdentifier().descriptor.string())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 45 | if (!descriptorObj.get()) { |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | ScopedLocalRef<jobject> kcmObj(env, |
| 50 | android_view_KeyCharacterMap_create(env, deviceInfo.getId(), |
| 51 | deviceInfo.getKeyCharacterMap())); |
| 52 | if (!kcmObj.get()) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Michael Wright | 54e5694 | 2013-08-12 16:39:59 -0700 | [diff] [blame^] | 56 | const InputDeviceIdentifier& ident = deviceInfo.getIdentifier(); |
| 57 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 58 | ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz, |
Michael Wright | 54e5694 | 2013-08-12 16:39:59 -0700 | [diff] [blame^] | 59 | gInputDeviceClassInfo.ctor, deviceInfo.getId(), deviceInfo.getGeneration(), |
| 60 | deviceInfo.getControllerNumber(), nameObj.get(), |
| 61 | static_cast<int32_t>(ident.vendor), static_cast<int32_t>(ident.product), |
| 62 | descriptorObj.get(), deviceInfo.isExternal(), deviceInfo.getSources(), |
| 63 | deviceInfo.getKeyboardType(), kcmObj.get(), deviceInfo.hasVibrator(), |
| 64 | deviceInfo.hasButtonUnderPad())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 65 | |
| 66 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
| 67 | for (size_t i = 0; i < ranges.size(); i++) { |
| 68 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 69 | env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis, |
| 70 | range.source, range.min, range.max, range.flat, range.fuzz, range.resolution); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 71 | if (env->ExceptionCheck()) { |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return env->NewLocalRef(inputDeviceObj.get()); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | #define FIND_CLASS(var, className) \ |
| 81 | var = env->FindClass(className); \ |
| 82 | LOG_FATAL_IF(! var, "Unable to find class " className); |
| 83 | |
| 84 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 85 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 86 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 87 | |
| 88 | int register_android_view_InputDevice(JNIEnv* env) |
| 89 | { |
| 90 | FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice"); |
| 91 | gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz)); |
| 92 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 93 | GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz, |
| 94 | "<init>", |
Michael Wright | 54e5694 | 2013-08-12 16:39:59 -0700 | [diff] [blame^] | 95 | "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZ)V"); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 96 | |
| 97 | GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz, |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 98 | "addMotionRange", "(IIFFFFF)V"); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | }; // namespace android |