blob: 811edaf8a26fc6f0f03836ef2fa8385c0b3e0512 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001//
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
Jeff Brownc5ed5912010-07-14 18:48:53 -070016void InputEvent::initialize(int32_t deviceId, int32_t source) {
Jeff Brown46b9ac02010-04-22 18:58:52 -070017 mDeviceId = deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -070018 mSource = source;
Jeff Brown46b9ac02010-04-22 18:58:52 -070019}
20
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070021void InputEvent::initialize(const InputEvent& from) {
22 mDeviceId = from.mDeviceId;
23 mSource = from.mSource;
24}
25
Jeff Brown46b9ac02010-04-22 18:58:52 -070026// class KeyEvent
27
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070028bool KeyEvent::hasDefaultAction(int32_t keyCode) {
29 switch (keyCode) {
Jeff Brownfd035822010-06-30 16:10:35 -070030 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:
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070050 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) {
Jeff Brownfd035822010-06-30 16:10:35 -070062 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:
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070082 return true;
83 }
84
85 return false;
86}
87
88bool KeyEvent::isSystemKey() const {
89 return isSystemKey(getKeyCode());
90}
91
Jeff Brown46b9ac02010-04-22 18:58:52 -070092void KeyEvent::initialize(
93 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -070094 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -070095 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) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700103 InputEvent::initialize(deviceId, source);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700104 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
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700114void 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
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126// class MotionEvent
127
128void MotionEvent::initialize(
129 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700130 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700131 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700132 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700133 int32_t edgeFlags,
134 int32_t metaState,
Jeff Brown5c225b12010-06-16 01:53:36 -0700135 float xOffset,
136 float yOffset,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700137 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) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700144 InputEvent::initialize(deviceId, source);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700145 mAction = action;
Jeff Brown85a31762010-09-01 17:01:00 -0700146 mFlags = flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700147 mEdgeFlags = edgeFlags;
148 mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700149 mXOffset = xOffset;
150 mYOffset = yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700151 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) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700169 mXOffset += xOffset;
170 mYOffset += yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171}
172
Jeff Brown6d0fec22010-07-23 21:28:06 -0700173// 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
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215} // namespace android