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>
64 inputDeviceObj(env,
65 env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor,
66 deviceInfo.getId(), deviceInfo.getGeneration(),
67 deviceInfo.getControllerNumber(), nameObj.get(),
68 static_cast<int32_t>(ident.vendor),
69 static_cast<int32_t>(ident.product), descriptorObj.get(),
70 deviceInfo.isExternal(), deviceInfo.getSources(),
71 deviceInfo.getKeyboardType(), kcmObj.get(),
72 deviceInfo.hasVibrator(), hasMic,
73 deviceInfo.hasButtonUnderPad(), deviceInfo.hasSensor(),
74 deviceInfo.hasBattery()));
75
76 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
77 for (const InputDeviceInfo::MotionRange& range: ranges) {
78 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
79 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
80 if (env->ExceptionCheck()) {
81 return NULL;
82 }
83 }
84
85 return env->NewLocalRef(inputDeviceObj.get());
86 }
87
88
register_android_view_InputDevice(JNIEnv * env)89 int register_android_view_InputDevice(JNIEnv* env)
90 {
91 gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
92 gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
93
94 gInputDeviceClassInfo.ctor =
95 GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
96 "(IIILjava/lang/String;IILjava/lang/"
97 "String;ZIILandroid/view/KeyCharacterMap;ZZZZZ)V");
98
99 gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz,
100 "addMotionRange", "(IIFFFFF)V");
101
102 return 0;
103 }
104
105 }; // namespace android
106