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 17 #include <input/Input.h> 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 #include "core_jni_helpers.h" 29 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) { 40 ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().string())); 41 if (!nameObj.get()) { 42 return NULL; 43 } 44 45 ScopedLocalRef<jstring> descriptorObj(env, 46 env->NewStringUTF(deviceInfo.getIdentifier().descriptor.string())); 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 58 const InputDeviceIdentifier& ident = deviceInfo.getIdentifier(); 59 60 // Not sure why, but JNI is complaining when I pass this through directly. 61 jboolean hasMic = deviceInfo.hasMic() ? JNI_TRUE : JNI_FALSE; 62 63 ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz, 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), 67 descriptorObj.get(), deviceInfo.isExternal(), deviceInfo.getSources(), 68 deviceInfo.getKeyboardType(), kcmObj.get(), deviceInfo.hasVibrator(), 69 hasMic, deviceInfo.hasButtonUnderPad())); 70 71 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); 72 for (size_t i = 0; i < ranges.size(); i++) { 73 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); 74 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis, 75 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution); 76 if (env->ExceptionCheck()) { 77 return NULL; 78 } 79 } 80 81 return env->NewLocalRef(inputDeviceObj.get()); 82 } 83 84 85 int register_android_view_InputDevice(JNIEnv* env) 86 { 87 gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice"); 88 gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz); 89 90 gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>", 91 "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZZ)V"); 92 93 gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, 94 "addMotionRange", "(IIFFFFF)V"); 95 96 return 0; 97 } 98 99 }; // namespace android 100