blob: d7d476a04d211f4a0d6c4165a9004bfa1fa1f10c [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) {
38 ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getName().string()));
39 if (!nameObj.get()) {
40 return NULL;
41 }
42
43 ScopedLocalRef<jstring> descriptorObj(env,
44 env->NewStringUTF(deviceInfo.getDescriptor().string()));
45 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,
57 gInputDeviceClassInfo.ctor, deviceInfo.getId(), nameObj.get(),
58 descriptorObj.get(), deviceInfo.getSources(), deviceInfo.getKeyboardType(),
59 kcmObj.get()));
60
61 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
62 for (size_t i = 0; i < ranges.size(); i++) {
63 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
64 env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange,
65 range.axis, range.source, range.min, range.max, range.flat, range.fuzz);
66 if (env->ExceptionCheck()) {
67 return NULL;
68 }
69 }
70
71 return env->NewLocalRef(inputDeviceObj.get());
72}
73
74
75#define FIND_CLASS(var, className) \
76 var = env->FindClass(className); \
77 LOG_FATAL_IF(! var, "Unable to find class " className);
78
79#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
80 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
81 LOG_FATAL_IF(! var, "Unable to find method " methodName);
82
83int register_android_view_InputDevice(JNIEnv* env)
84{
85 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
86 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
87
88 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
89 "<init>", "(ILjava/lang/String;Ljava/lang/String;IILandroid/view/KeyCharacterMap;)V");
90
91 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
92 "addMotionRange", "(IIFFFF)V");
93
94 return 0;
95}
96
97}; // namespace android