blob: 494fad7900ef6694200f2862777d9fe90d6559de [file] [log] [blame]
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001/*
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 Brown9d3b1a42013-07-01 19:07:15 -070017#include <input/Input.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070018
19#include <android_runtime/AndroidRuntime.h>
Steven Moreland889f9952017-07-17 12:08:45 -070020#include <jni.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070021#include <nativehelper/JNIHelp.h>
22
Steven Moreland2279b252017-07-19 09:50:45 -070023#include <nativehelper/ScopedLocalRef.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070024
25#include "android_view_InputDevice.h"
26#include "android_view_KeyCharacterMap.h"
27
Andreas Gampe987f79f2014-11-18 17:29:46 -080028#include "core_jni_helpers.h"
29
Jeff Brown9f25b7f2012-04-10 14:30:49 -070030namespace android {
31
32static struct {
33 jclass clazz;
34
35 jmethodID ctor;
36 jmethodID addMotionRange;
37} gInputDeviceClassInfo;
38
39jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -070040 ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().string()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070041 if (!nameObj.get()) {
42 return NULL;
43 }
44
45 ScopedLocalRef<jstring> descriptorObj(env,
Jeff Brown5bbd4b42012-04-20 19:28:00 -070046 env->NewStringUTF(deviceInfo.getIdentifier().descriptor.string()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070047 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 Wright54e56942013-08-12 16:39:59 -070058 const InputDeviceIdentifier& ident = deviceInfo.getIdentifier();
59
Tim Kilbourn6d85cf22015-04-08 10:23:35 -070060 // Not sure why, but JNI is complaining when I pass this through directly.
61 jboolean hasMic = deviceInfo.hasMic() ? JNI_TRUE : JNI_FALSE;
62
Jeff Brown9f25b7f2012-04-10 14:30:49 -070063 ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz,
Michael Wright54e56942013-08-12 16:39:59 -070064 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 Kilbourn6d85cf22015-04-08 10:23:35 -070067 descriptorObj.get(), deviceInfo.isExternal(), deviceInfo.getSources(),
68 deviceInfo.getKeyboardType(), kcmObj.get(), deviceInfo.hasVibrator(),
69 hasMic, deviceInfo.hasButtonUnderPad()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070070
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);
Michael Wrightc6091c62013-04-01 20:56:04 -070074 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
75 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
Jeff Brown9f25b7f2012-04-10 14:30:49 -070076 if (env->ExceptionCheck()) {
77 return NULL;
78 }
79 }
80
81 return env->NewLocalRef(inputDeviceObj.get());
82}
83
84
Jeff Brown9f25b7f2012-04-10 14:30:49 -070085int register_android_view_InputDevice(JNIEnv* env)
86{
Andreas Gampe987f79f2014-11-18 17:29:46 -080087 gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
88 gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
Jeff Brown9f25b7f2012-04-10 14:30:49 -070089
Andreas Gampe987f79f2014-11-18 17:29:46 -080090 gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
Tim Kilbourn6d85cf22015-04-08 10:23:35 -070091 "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZZ)V");
Jeff Brown9f25b7f2012-04-10 14:30:49 -070092
Andreas Gampe987f79f2014-11-18 17:29:46 -080093 gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz,
Michael Wrightc6091c62013-04-01 20:56:04 -070094 "addMotionRange", "(IIFFFFF)V");
Jeff Brown9f25b7f2012-04-10 14:30:49 -070095
96 return 0;
97}
98
99}; // namespace android