blob: 5fbaf0961bfbacee5c908372bd43aea98ef0dd6d [file] [log] [blame]
Jeff Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -070016void InputEvent::initialize(int32_t deviceId, int32_t source) {
Jeff Browne839a582010-04-22 18:58:52 -070017 mDeviceId = deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -070018 mSource = source;
Jeff Browne839a582010-04-22 18:58:52 -070019}
20
Dianne Hackborn0e885272010-07-15 17:44:53 -070021void InputEvent::initialize(const InputEvent& from) {
22 mDeviceId = from.mDeviceId;
23 mSource = from.mSource;
24}
25
Jeff Browne839a582010-04-22 18:58:52 -070026// class KeyEvent
27
Dianne Hackborn189ed232010-06-29 19:20:40 -070028bool KeyEvent::hasDefaultAction(int32_t keyCode) {
29 switch (keyCode) {
Jeff Brown8575a872010-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 Hackborn189ed232010-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 Brown8575a872010-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 Hackborn189ed232010-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 Browne839a582010-04-22 18:58:52 -070092void KeyEvent::initialize(
93 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -070094 int32_t source,
Jeff Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -0700103 InputEvent::initialize(deviceId, source);
Jeff Browne839a582010-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 Hackborn0e885272010-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 Browne839a582010-04-22 18:58:52 -0700126// class MotionEvent
127
128void MotionEvent::initialize(
129 int32_t deviceId,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700130 int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700131 int32_t action,
132 int32_t edgeFlags,
133 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700134 float xOffset,
135 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700136 float xPrecision,
137 float yPrecision,
138 nsecs_t downTime,
139 nsecs_t eventTime,
140 size_t pointerCount,
141 const int32_t* pointerIds,
142 const PointerCoords* pointerCoords) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700143 InputEvent::initialize(deviceId, source);
Jeff Browne839a582010-04-22 18:58:52 -0700144 mAction = action;
145 mEdgeFlags = edgeFlags;
146 mMetaState = metaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700147 mXOffset = xOffset;
148 mYOffset = yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700149 mXPrecision = xPrecision;
150 mYPrecision = yPrecision;
151 mDownTime = downTime;
152 mPointerIds.clear();
153 mPointerIds.appendArray(pointerIds, pointerCount);
154 mSampleEventTimes.clear();
155 mSamplePointerCoords.clear();
156 addSample(eventTime, pointerCoords);
157}
158
159void MotionEvent::addSample(
160 int64_t eventTime,
161 const PointerCoords* pointerCoords) {
162 mSampleEventTimes.push(eventTime);
163 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
164}
165
166void MotionEvent::offsetLocation(float xOffset, float yOffset) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700167 mXOffset += xOffset;
168 mYOffset += yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700169}
170
Jeff Browne57e8952010-07-23 21:28:06 -0700171// class InputDeviceInfo
172
173InputDeviceInfo::InputDeviceInfo() {
174 initialize(-1, String8("uninitialized device info"));
175}
176
177InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
178 mId(other.mId), mName(other.mName), mSources(other.mSources),
179 mKeyboardType(other.mKeyboardType),
180 mMotionRanges(other.mMotionRanges) {
181}
182
183InputDeviceInfo::~InputDeviceInfo() {
184}
185
186void InputDeviceInfo::initialize(int32_t id, const String8& name) {
187 mId = id;
188 mName = name;
189 mSources = 0;
190 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
191 mMotionRanges.clear();
192}
193
194const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(int32_t rangeType) const {
195 ssize_t index = mMotionRanges.indexOfKey(rangeType);
196 return index >= 0 ? & mMotionRanges.valueAt(index) : NULL;
197}
198
199void InputDeviceInfo::addSource(uint32_t source) {
200 mSources |= source;
201}
202
203void InputDeviceInfo::addMotionRange(int32_t rangeType, float min, float max,
204 float flat, float fuzz) {
205 MotionRange range = { min, max, flat, fuzz };
206 addMotionRange(rangeType, range);
207}
208
209void InputDeviceInfo::addMotionRange(int32_t rangeType, const MotionRange& range) {
210 mMotionRanges.add(rangeType, range);
211}
212
213// class InputDeviceProxy
214
215InputDeviceProxy::InputDeviceProxy() {
216}
217
218InputDeviceProxy::~InputDeviceProxy() {
219}
220
221void InputDeviceProxy::getDeviceIds(Vector<int32_t>& outIds) {
222 // TODO use Binder
223}
224
225sp<InputDeviceProxy> InputDeviceProxy::getDevice(int32_t id) {
226 // TODO use Binder
227 return NULL;
228}
229
Jeff Browne839a582010-04-22 18:58:52 -0700230} // namespace android