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 <jni.h>
21 #include <nativehelper/JNIHelp.h>
22 
23 #include <nativehelper/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 
android_view_InputDevice_create(JNIEnv * env,const InputDeviceInfo & deviceInfo)39 jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) {
40     ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().c_str()));
41     if (!nameObj.get()) {
42         return NULL;
43     }
44 
45     ScopedLocalRef<jstring> descriptorObj(env,
46             env->NewStringUTF(deviceInfo.getIdentifier().descriptor.c_str()));
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 std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
72     for (const InputDeviceInfo::MotionRange& range: ranges) {
73         env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
74                 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
75         if (env->ExceptionCheck()) {
76             return NULL;
77         }
78     }
79 
80     return env->NewLocalRef(inputDeviceObj.get());
81 }
82 
83 
register_android_view_InputDevice(JNIEnv * env)84 int register_android_view_InputDevice(JNIEnv* env)
85 {
86     gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
87     gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
88 
89     gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
90             "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZZ)V");
91 
92     gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz,
93             "addMotionRange", "(IIFFFFF)V");
94 
95     return 0;
96 }
97 
98 }; // namespace android
99