blob: 9f4e3e516adaa5e0230f39413b961244126e523f [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) {
Siarhei Vishniakou80278762018-07-06 11:50:18 +010040 ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().c_str()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070041 if (!nameObj.get()) {
42 return NULL;
43 }
44
45 ScopedLocalRef<jstring> descriptorObj(env,
Siarhei Vishniakou80278762018-07-06 11:50:18 +010046 env->NewStringUTF(deviceInfo.getIdentifier().descriptor.c_str()));
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
Arthur Hungf8358d32019-03-11 11:54:56 +080071 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
72 for (const InputDeviceInfo::MotionRange& range: ranges) {
Michael Wrightc6091c62013-04-01 20:56:04 -070073 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
74 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
Jeff Brown9f25b7f2012-04-10 14:30:49 -070075 if (env->ExceptionCheck()) {
76 return NULL;
77 }
78 }
79
80 return env->NewLocalRef(inputDeviceObj.get());
81}
82
83
Jeff Brown9f25b7f2012-04-10 14:30:49 -070084int register_android_view_InputDevice(JNIEnv* env)
85{
Andreas Gampe987f79f2014-11-18 17:29:46 -080086 gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
87 gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
Jeff Brown9f25b7f2012-04-10 14:30:49 -070088
Andreas Gampe987f79f2014-11-18 17:29:46 -080089 gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
Tim Kilbourn6d85cf22015-04-08 10:23:35 -070090 "(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZZ)V");
Jeff Brown9f25b7f2012-04-10 14:30:49 -070091
Andreas Gampe987f79f2014-11-18 17:29:46 -080092 gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz,
Michael Wrightc6091c62013-04-01 20:56:04 -070093 "addMotionRange", "(IIFFFFF)V");
Jeff Brown9f25b7f2012-04-10 14:30:49 -070094
95 return 0;
96}
97
98}; // namespace android