blob: a64251f15adcb23dfba866d4f0ec593f307f5cb8 [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
16void InputEvent::initialize(int32_t deviceId, int32_t nature) {
17 mDeviceId = deviceId;
18 mNature = nature;
19}
20
21// class KeyEvent
22
Dianne Hackborn189ed232010-06-29 19:20:40 -070023bool KeyEvent::hasDefaultAction(int32_t keyCode) {
24 switch (keyCode) {
25 case KEYCODE_HOME:
26 case KEYCODE_BACK:
27 case KEYCODE_CALL:
28 case KEYCODE_ENDCALL:
29 case KEYCODE_VOLUME_UP:
30 case KEYCODE_VOLUME_DOWN:
31 case KEYCODE_POWER:
32 case KEYCODE_CAMERA:
33 case KEYCODE_HEADSETHOOK:
34 case KEYCODE_MENU:
35 case KEYCODE_NOTIFICATION:
36 case KEYCODE_FOCUS:
37 case KEYCODE_SEARCH:
38 case KEYCODE_MEDIA_PLAY_PAUSE:
39 case KEYCODE_MEDIA_STOP:
40 case KEYCODE_MEDIA_NEXT:
41 case KEYCODE_MEDIA_PREVIOUS:
42 case KEYCODE_MEDIA_REWIND:
43 case KEYCODE_MEDIA_FAST_FORWARD:
44 case KEYCODE_MUTE:
45 return true;
46 }
47
48 return false;
49}
50
51bool KeyEvent::hasDefaultAction() const {
52 return hasDefaultAction(getKeyCode());
53}
54
55bool KeyEvent::isSystemKey(int32_t keyCode) {
56 switch (keyCode) {
57 case KEYCODE_MENU:
58 case KEYCODE_SOFT_RIGHT:
59 case KEYCODE_HOME:
60 case KEYCODE_BACK:
61 case KEYCODE_CALL:
62 case KEYCODE_ENDCALL:
63 case KEYCODE_VOLUME_UP:
64 case KEYCODE_VOLUME_DOWN:
65 case KEYCODE_MUTE:
66 case KEYCODE_POWER:
67 case KEYCODE_HEADSETHOOK:
68 case KEYCODE_MEDIA_PLAY_PAUSE:
69 case KEYCODE_MEDIA_STOP:
70 case KEYCODE_MEDIA_NEXT:
71 case KEYCODE_MEDIA_PREVIOUS:
72 case KEYCODE_MEDIA_REWIND:
73 case KEYCODE_MEDIA_FAST_FORWARD:
74 case KEYCODE_CAMERA:
75 case KEYCODE_FOCUS:
76 case KEYCODE_SEARCH:
77 return true;
78 }
79
80 return false;
81}
82
83bool KeyEvent::isSystemKey() const {
84 return isSystemKey(getKeyCode());
85}
86
Jeff Browne839a582010-04-22 18:58:52 -070087void KeyEvent::initialize(
88 int32_t deviceId,
89 int32_t nature,
90 int32_t action,
91 int32_t flags,
92 int32_t keyCode,
93 int32_t scanCode,
94 int32_t metaState,
95 int32_t repeatCount,
96 nsecs_t downTime,
97 nsecs_t eventTime) {
98 InputEvent::initialize(deviceId, nature);
99 mAction = action;
100 mFlags = flags;
101 mKeyCode = keyCode;
102 mScanCode = scanCode;
103 mMetaState = metaState;
104 mRepeatCount = repeatCount;
105 mDownTime = downTime;
106 mEventTime = eventTime;
107}
108
109// class MotionEvent
110
111void MotionEvent::initialize(
112 int32_t deviceId,
113 int32_t nature,
114 int32_t action,
115 int32_t edgeFlags,
116 int32_t metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700117 float xOffset,
118 float yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700119 float xPrecision,
120 float yPrecision,
121 nsecs_t downTime,
122 nsecs_t eventTime,
123 size_t pointerCount,
124 const int32_t* pointerIds,
125 const PointerCoords* pointerCoords) {
126 InputEvent::initialize(deviceId, nature);
127 mAction = action;
128 mEdgeFlags = edgeFlags;
129 mMetaState = metaState;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700130 mXOffset = xOffset;
131 mYOffset = yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700132 mXPrecision = xPrecision;
133 mYPrecision = yPrecision;
134 mDownTime = downTime;
135 mPointerIds.clear();
136 mPointerIds.appendArray(pointerIds, pointerCount);
137 mSampleEventTimes.clear();
138 mSamplePointerCoords.clear();
139 addSample(eventTime, pointerCoords);
140}
141
142void MotionEvent::addSample(
143 int64_t eventTime,
144 const PointerCoords* pointerCoords) {
145 mSampleEventTimes.push(eventTime);
146 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
147}
148
149void MotionEvent::offsetLocation(float xOffset, float yOffset) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700150 mXOffset += xOffset;
151 mYOffset += yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700152}
153
154} // namespace android