1//
2// Copyright 2010 The Android Open Source Project
3//
4// Provides a pipe-based transport for native events in the NDK.
5//
6#define LOG_TAG "Input"
7
8//#define LOG_NDEBUG 0
9
10#include <ui/Input.h>
11
12namespace android {
13
14// class InputEvent
15
16void InputEvent::initialize(int32_t deviceId, int32_t source) {
17    mDeviceId = deviceId;
18    mSource = source;
19}
20
21void InputEvent::initialize(const InputEvent& from) {
22    mDeviceId = from.mDeviceId;
23    mSource = from.mSource;
24}
25
26// class KeyEvent
27
28bool KeyEvent::hasDefaultAction(int32_t keyCode) {
29    switch (keyCode) {
30        case AKEYCODE_HOME:
31        case AKEYCODE_BACK:
32        case AKEYCODE_CALL:
33        case AKEYCODE_ENDCALL:
34        case AKEYCODE_VOLUME_UP:
35        case AKEYCODE_VOLUME_DOWN:
36        case AKEYCODE_POWER:
37        case AKEYCODE_CAMERA:
38        case AKEYCODE_HEADSETHOOK:
39        case AKEYCODE_MENU:
40        case AKEYCODE_NOTIFICATION:
41        case AKEYCODE_FOCUS:
42        case AKEYCODE_SEARCH:
43        case AKEYCODE_MEDIA_PLAY_PAUSE:
44        case AKEYCODE_MEDIA_STOP:
45        case AKEYCODE_MEDIA_NEXT:
46        case AKEYCODE_MEDIA_PREVIOUS:
47        case AKEYCODE_MEDIA_REWIND:
48        case AKEYCODE_MEDIA_FAST_FORWARD:
49        case AKEYCODE_MUTE:
50            return true;
51    }
52
53    return false;
54}
55
56bool KeyEvent::hasDefaultAction() const {
57    return hasDefaultAction(getKeyCode());
58}
59
60bool KeyEvent::isSystemKey(int32_t keyCode) {
61    switch (keyCode) {
62        case AKEYCODE_MENU:
63        case AKEYCODE_SOFT_RIGHT:
64        case AKEYCODE_HOME:
65        case AKEYCODE_BACK:
66        case AKEYCODE_CALL:
67        case AKEYCODE_ENDCALL:
68        case AKEYCODE_VOLUME_UP:
69        case AKEYCODE_VOLUME_DOWN:
70        case AKEYCODE_MUTE:
71        case AKEYCODE_POWER:
72        case AKEYCODE_HEADSETHOOK:
73        case AKEYCODE_MEDIA_PLAY_PAUSE:
74        case AKEYCODE_MEDIA_STOP:
75        case AKEYCODE_MEDIA_NEXT:
76        case AKEYCODE_MEDIA_PREVIOUS:
77        case AKEYCODE_MEDIA_REWIND:
78        case AKEYCODE_MEDIA_FAST_FORWARD:
79        case AKEYCODE_CAMERA:
80        case AKEYCODE_FOCUS:
81        case AKEYCODE_SEARCH:
82            return true;
83    }
84
85    return false;
86}
87
88bool KeyEvent::isSystemKey() const {
89    return isSystemKey(getKeyCode());
90}
91
92void KeyEvent::initialize(
93        int32_t deviceId,
94        int32_t source,
95        int32_t action,
96        int32_t flags,
97        int32_t keyCode,
98        int32_t scanCode,
99        int32_t metaState,
100        int32_t repeatCount,
101        nsecs_t downTime,
102        nsecs_t eventTime) {
103    InputEvent::initialize(deviceId, source);
104    mAction = action;
105    mFlags = flags;
106    mKeyCode = keyCode;
107    mScanCode = scanCode;
108    mMetaState = metaState;
109    mRepeatCount = repeatCount;
110    mDownTime = downTime;
111    mEventTime = eventTime;
112}
113
114void KeyEvent::initialize(const KeyEvent& from) {
115    InputEvent::initialize(from);
116    mAction = from.mAction;
117    mFlags = from.mFlags;
118    mKeyCode = from.mKeyCode;
119    mScanCode = from.mScanCode;
120    mMetaState = from.mMetaState;
121    mRepeatCount = from.mRepeatCount;
122    mDownTime = from.mDownTime;
123    mEventTime = from.mEventTime;
124}
125
126// class MotionEvent
127
128void MotionEvent::initialize(
129        int32_t deviceId,
130        int32_t source,
131        int32_t action,
132        int32_t flags,
133        int32_t edgeFlags,
134        int32_t metaState,
135        float xOffset,
136        float yOffset,
137        float xPrecision,
138        float yPrecision,
139        nsecs_t downTime,
140        nsecs_t eventTime,
141        size_t pointerCount,
142        const int32_t* pointerIds,
143        const PointerCoords* pointerCoords) {
144    InputEvent::initialize(deviceId, source);
145    mAction = action;
146    mFlags = flags;
147    mEdgeFlags = edgeFlags;
148    mMetaState = metaState;
149    mXOffset = xOffset;
150    mYOffset = yOffset;
151    mXPrecision = xPrecision;
152    mYPrecision = yPrecision;
153    mDownTime = downTime;
154    mPointerIds.clear();
155    mPointerIds.appendArray(pointerIds, pointerCount);
156    mSampleEventTimes.clear();
157    mSamplePointerCoords.clear();
158    addSample(eventTime, pointerCoords);
159}
160
161void MotionEvent::addSample(
162        int64_t eventTime,
163        const PointerCoords* pointerCoords) {
164    mSampleEventTimes.push(eventTime);
165    mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
166}
167
168void MotionEvent::offsetLocation(float xOffset, float yOffset) {
169    mXOffset += xOffset;
170    mYOffset += yOffset;
171}
172
173// class InputDeviceInfo
174
175InputDeviceInfo::InputDeviceInfo() {
176    initialize(-1, String8("uninitialized device info"));
177}
178
179InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
180        mId(other.mId), mName(other.mName), mSources(other.mSources),
181        mKeyboardType(other.mKeyboardType),
182        mMotionRanges(other.mMotionRanges) {
183}
184
185InputDeviceInfo::~InputDeviceInfo() {
186}
187
188void InputDeviceInfo::initialize(int32_t id, const String8& name) {
189    mId = id;
190    mName = name;
191    mSources = 0;
192    mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
193    mMotionRanges.clear();
194}
195
196const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(int32_t rangeType) const {
197    ssize_t index = mMotionRanges.indexOfKey(rangeType);
198    return index >= 0 ? & mMotionRanges.valueAt(index) : NULL;
199}
200
201void InputDeviceInfo::addSource(uint32_t source) {
202    mSources |= source;
203}
204
205void InputDeviceInfo::addMotionRange(int32_t rangeType, float min, float max,
206        float flat, float fuzz) {
207    MotionRange range = { min, max, flat, fuzz };
208    addMotionRange(rangeType, range);
209}
210
211void InputDeviceInfo::addMotionRange(int32_t rangeType, const MotionRange& range) {
212    mMotionRanges.add(rangeType, range);
213}
214
215} // namespace android
216