Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 | |
| 12 | namespace android { |
| 13 | |
| 14 | // class InputEvent |
| 15 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 16 | void InputEvent::initialize(int32_t deviceId, int32_t source) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | mDeviceId = deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 18 | mSource = source; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 21 | void InputEvent::initialize(const InputEvent& from) { |
| 22 | mDeviceId = from.mDeviceId; |
| 23 | mSource = from.mSource; |
| 24 | } |
| 25 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | // class KeyEvent |
| 27 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 28 | bool KeyEvent::hasDefaultAction(int32_t keyCode) { |
| 29 | switch (keyCode) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 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: |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | bool KeyEvent::hasDefaultAction() const { |
| 57 | return hasDefaultAction(getKeyCode()); |
| 58 | } |
| 59 | |
| 60 | bool KeyEvent::isSystemKey(int32_t keyCode) { |
| 61 | switch (keyCode) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 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: |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool KeyEvent::isSystemKey() const { |
| 89 | return isSystemKey(getKeyCode()); |
| 90 | } |
| 91 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 92 | void KeyEvent::initialize( |
| 93 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 94 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 103 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 114 | void 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 Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 126 | // class MotionEvent |
| 127 | |
| 128 | void MotionEvent::initialize( |
| 129 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 130 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 132 | int32_t flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 133 | int32_t edgeFlags, |
| 134 | int32_t metaState, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 135 | float xOffset, |
| 136 | float yOffset, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 144 | InputEvent::initialize(deviceId, source); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 145 | mAction = action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 146 | mFlags = flags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 147 | mEdgeFlags = edgeFlags; |
| 148 | mMetaState = metaState; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 149 | mXOffset = xOffset; |
| 150 | mYOffset = yOffset; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 | |
| 161 | void MotionEvent::addSample( |
| 162 | int64_t eventTime, |
| 163 | const PointerCoords* pointerCoords) { |
| 164 | mSampleEventTimes.push(eventTime); |
| 165 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 166 | } |
| 167 | |
| 168 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 169 | mXOffset += xOffset; |
| 170 | mYOffset += yOffset; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 173 | // class InputDeviceInfo |
| 174 | |
| 175 | InputDeviceInfo::InputDeviceInfo() { |
| 176 | initialize(-1, String8("uninitialized device info")); |
| 177 | } |
| 178 | |
| 179 | InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) : |
| 180 | mId(other.mId), mName(other.mName), mSources(other.mSources), |
| 181 | mKeyboardType(other.mKeyboardType), |
| 182 | mMotionRanges(other.mMotionRanges) { |
| 183 | } |
| 184 | |
| 185 | InputDeviceInfo::~InputDeviceInfo() { |
| 186 | } |
| 187 | |
| 188 | void 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 | |
| 196 | const 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 | |
| 201 | void InputDeviceInfo::addSource(uint32_t source) { |
| 202 | mSources |= source; |
| 203 | } |
| 204 | |
| 205 | void 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 | |
| 211 | void InputDeviceInfo::addMotionRange(int32_t rangeType, const MotionRange& range) { |
| 212 | mMotionRanges.add(rangeType, range); |
| 213 | } |
| 214 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 215 | } // namespace android |