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> |
Steven Moreland | 889f995 | 2017-07-17 12:08:45 -0700 | [diff] [blame] | 20 | #include <jni.h> |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 21 | #include <nativehelper/JNIHelp.h> |
| 22 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 23 | #include <nativehelper/ScopedLocalRef.h> |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 24 | |
| 25 | #include "android_view_InputDevice.h" |
| 26 | #include "android_view_KeyCharacterMap.h" |
| 27 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 28 | #include "core_jni_helpers.h" |
| 29 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | static struct { |
| 33 | jclass clazz; |
| 34 | |
| 35 | jmethodID ctor; |
| 36 | jmethodID addMotionRange; |
| 37 | } gInputDeviceClassInfo; |
| 38 | |
| 39 | jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) { |
Siarhei Vishniakou | 8027876 | 2018-07-06 11:50:18 +0100 | [diff] [blame] | 40 | ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().c_str())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 41 | if (!nameObj.get()) { |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | ScopedLocalRef<jstring> descriptorObj(env, |
Siarhei Vishniakou | 8027876 | 2018-07-06 11:50:18 +0100 | [diff] [blame] | 46 | env->NewStringUTF(deviceInfo.getIdentifier().descriptor.c_str())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 47 | if (!descriptorObj.get()) { |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | ScopedLocalRef<jobject> kcmObj(env, |
| 52 | android_view_KeyCharacterMap_create(env, deviceInfo.getId(), |
| 53 | deviceInfo.getKeyCharacterMap())); |
| 54 | if (!kcmObj.get()) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
Michael Wright | 54e5694 | 2013-08-12 16:39:59 -0700 | [diff] [blame] | 58 | const InputDeviceIdentifier& ident = deviceInfo.getIdentifier(); |
| 59 | |
Tim Kilbourn | 6d85cf2 | 2015-04-08 10:23:35 -0700 | [diff] [blame] | 60 | // Not sure why, but JNI is complaining when I pass this through directly. |
| 61 | jboolean hasMic = deviceInfo.hasMic() ? JNI_TRUE : JNI_FALSE; |
| 62 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 63 | ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz, |
Michael Wright | 54e5694 | 2013-08-12 16:39:59 -0700 | [diff] [blame] | 64 | gInputDeviceClassInfo.ctor, deviceInfo.getId(), deviceInfo.getGeneration(), |
| 65 | deviceInfo.getControllerNumber(), nameObj.get(), |
| 66 | static_cast<int32_t>(ident.vendor), static_cast<int32_t>(ident.product), |
Tim Kilbourn | 6d85cf2 | 2015-04-08 10:23:35 -0700 | [diff] [blame] | 67 | descriptorObj.get(), deviceInfo.isExternal(), deviceInfo.getSources(), |
| 68 | deviceInfo.getKeyboardType(), kcmObj.get(), deviceInfo.hasVibrator(), |
| 69 | hasMic, deviceInfo.hasButtonUnderPad())); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 70 | |
Arthur Hung | f8358d3 | 2019-03-11 11:54:56 +0800 | [diff] [blame] | 71 | const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
| 72 | for (const InputDeviceInfo::MotionRange& range: ranges) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 73 | env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis, |
| 74 | range.source, range.min, range.max, range.flat, range.fuzz, range.resolution); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 75 | if (env->ExceptionCheck()) { |
| 76 | return NULL; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return env->NewLocalRef(inputDeviceObj.get()); |
| 81 | } |
| 82 | |
| 83 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 84 | int register_android_view_InputDevice(JNIEnv* env) |
| 85 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 86 | gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice"); |
| 87 | gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 89 | gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>", |
Tim Kilbourn | 6d85cf2 | 2015-04-08 10:23:35 -0700 | [diff] [blame] | 90 | "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZZ)V"); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 91 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 92 | gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 93 | "addMotionRange", "(IIFFFFF)V"); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | }; // namespace android |