blob: e3a54a8e3bc7f20548d2f9f0e6eee989b38ee7b1 [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
17#include <androidfw/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
28namespace android {
29
30static struct {
31 jclass clazz;
32
33 jmethodID ctor;
34 jmethodID addMotionRange;
35} gInputDeviceClassInfo;
36
37jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -070038 ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().string()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070039 if (!nameObj.get()) {
40 return NULL;
41 }
42
43 ScopedLocalRef<jstring> descriptorObj(env,
Jeff Brown5bbd4b42012-04-20 19:28:00 -070044 env->NewStringUTF(deviceInfo.getIdentifier().descriptor.string()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070045 if (!descriptorObj.get()) {
46 return NULL;
47 }
48
49 ScopedLocalRef<jobject> kcmObj(env,
50 android_view_KeyCharacterMap_create(env, deviceInfo.getId(),
51 deviceInfo.getKeyCharacterMap()));
52 if (!kcmObj.get()) {
53 return NULL;
54 }
55
56 ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz,
Jeff Brownaf9e8d32012-04-12 17:32:48 -070057 gInputDeviceClassInfo.ctor, deviceInfo.getId(), deviceInfo.getGeneration(),
Jeff Browndaa37532012-05-01 15:54:03 -070058 nameObj.get(), descriptorObj.get(), deviceInfo.isExternal(),
Jeff Brownaf9e8d32012-04-12 17:32:48 -070059 deviceInfo.getSources(), deviceInfo.getKeyboardType(),
Jeff Browna47425a2012-04-13 04:09:27 -070060 kcmObj.get(), deviceInfo.hasVibrator()));
Jeff Brown9f25b7f2012-04-10 14:30:49 -070061
62 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
63 for (size_t i = 0; i < ranges.size(); i++) {
64 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
Michael Wrightc6091c62013-04-01 20:56:04 -070065 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
66 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
Jeff Brown9f25b7f2012-04-10 14:30:49 -070067 if (env->ExceptionCheck()) {
68 return NULL;
69 }
70 }
71
72 return env->NewLocalRef(inputDeviceObj.get());
73}
74
75
76#define FIND_CLASS(var, className) \
77 var = env->FindClass(className); \
78 LOG_FATAL_IF(! var, "Unable to find class " className);
79
80#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
81 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
82 LOG_FATAL_IF(! var, "Unable to find method " methodName);
83
84int register_android_view_InputDevice(JNIEnv* env)
85{
86 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
87 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
88
89 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
Jeff Browndaa37532012-05-01 15:54:03 -070090 "<init>", "(IILjava/lang/String;Ljava/lang/String;ZIILandroid/view/KeyCharacterMap;Z)V");
Jeff Brown9f25b7f2012-04-10 14:30:49 -070091
92 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, 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