Input.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "Input"
  17. //#define LOG_NDEBUG 0
  18. #include <math.h>
  19. #include <limits.h>
  20. #include <input/Input.h>
  21. #include <input/InputEventLabels.h>
  22. #ifdef HAVE_ANDROID_OS
  23. #include <binder/Parcel.h>
  24. #endif
  25. namespace android {
  26. // --- InputEvent ---
  27. void InputEvent::initialize(int32_t deviceId, int32_t source) {
  28. mDeviceId = deviceId;
  29. mSource = source;
  30. }
  31. void InputEvent::initialize(const InputEvent& from) {
  32. mDeviceId = from.mDeviceId;
  33. mSource = from.mSource;
  34. }
  35. // --- KeyEvent ---
  36. const char* KeyEvent::getLabel(int32_t keyCode) {
  37. return getLabelByKeyCode(keyCode);
  38. }
  39. int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
  40. return getKeyCodeByLabel(label);
  41. }
  42. void KeyEvent::initialize(
  43. int32_t deviceId,
  44. int32_t source,
  45. int32_t action,
  46. int32_t flags,
  47. int32_t keyCode,
  48. int32_t scanCode,
  49. int32_t metaState,
  50. int32_t repeatCount,
  51. nsecs_t downTime,
  52. nsecs_t eventTime) {
  53. InputEvent::initialize(deviceId, source);
  54. mAction = action;
  55. mFlags = flags;
  56. mKeyCode = keyCode;
  57. mScanCode = scanCode;
  58. mMetaState = metaState;
  59. mRepeatCount = repeatCount;
  60. mDownTime = downTime;
  61. mEventTime = eventTime;
  62. }
  63. void KeyEvent::initialize(const KeyEvent& from) {
  64. InputEvent::initialize(from);
  65. mAction = from.mAction;
  66. mFlags = from.mFlags;
  67. mKeyCode = from.mKeyCode;
  68. mScanCode = from.mScanCode;
  69. mMetaState = from.mMetaState;
  70. mRepeatCount = from.mRepeatCount;
  71. mDownTime = from.mDownTime;
  72. mEventTime = from.mEventTime;
  73. }
  74. // --- PointerCoords ---
  75. float PointerCoords::getAxisValue(int32_t axis) const {
  76. if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
  77. return 0;
  78. }
  79. return values[BitSet64::getIndexOfBit(bits, axis)];
  80. }
  81. status_t PointerCoords::setAxisValue(int32_t axis, float value) {
  82. if (axis < 0 || axis > 63) {
  83. return NAME_NOT_FOUND;
  84. }
  85. uint32_t index = BitSet64::getIndexOfBit(bits, axis);
  86. if (!BitSet64::hasBit(bits, axis)) {
  87. if (value == 0) {
  88. return OK; // axes with value 0 do not need to be stored
  89. }
  90. uint32_t count = BitSet64::count(bits);
  91. if (count >= MAX_AXES) {
  92. tooManyAxes(axis);
  93. return NO_MEMORY;
  94. }
  95. BitSet64::markBit(bits, axis);
  96. for (uint32_t i = count; i > index; i--) {
  97. values[i] = values[i - 1];
  98. }
  99. }
  100. values[index] = value;
  101. return OK;
  102. }
  103. static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
  104. float value = c.getAxisValue(axis);
  105. if (value != 0) {
  106. c.setAxisValue(axis, value * scaleFactor);
  107. }
  108. }
  109. void PointerCoords::scale(float scaleFactor) {
  110. // No need to scale pressure or size since they are normalized.
  111. // No need to scale orientation since it is meaningless to do so.
  112. scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, scaleFactor);
  113. scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, scaleFactor);
  114. scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, scaleFactor);
  115. scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, scaleFactor);
  116. scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, scaleFactor);
  117. scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, scaleFactor);
  118. }
  119. void PointerCoords::applyOffset(float xOffset, float yOffset) {
  120. setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
  121. setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
  122. }
  123. #ifdef HAVE_ANDROID_OS
  124. status_t PointerCoords::readFromParcel(Parcel* parcel) {
  125. bits = parcel->readInt64();
  126. uint32_t count = BitSet64::count(bits);
  127. if (count > MAX_AXES) {
  128. return BAD_VALUE;
  129. }
  130. for (uint32_t i = 0; i < count; i++) {
  131. values[i] = parcel->readFloat();
  132. }
  133. return OK;
  134. }
  135. status_t PointerCoords::writeToParcel(Parcel* parcel) const {
  136. parcel->writeInt64(bits);
  137. uint32_t count = BitSet64::count(bits);
  138. for (uint32_t i = 0; i < count; i++) {
  139. parcel->writeFloat(values[i]);
  140. }
  141. return OK;
  142. }
  143. #endif
  144. void PointerCoords::tooManyAxes(int axis) {
  145. ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
  146. "cannot contain more than %d axis values.", axis, int(MAX_AXES));
  147. }
  148. bool PointerCoords::operator==(const PointerCoords& other) const {
  149. if (bits != other.bits) {
  150. return false;
  151. }
  152. uint32_t count = BitSet64::count(bits);
  153. for (uint32_t i = 0; i < count; i++) {
  154. if (values[i] != other.values[i]) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. void PointerCoords::copyFrom(const PointerCoords& other) {
  161. bits = other.bits;
  162. uint32_t count = BitSet64::count(bits);
  163. for (uint32_t i = 0; i < count; i++) {
  164. values[i] = other.values[i];
  165. }
  166. }
  167. // --- PointerProperties ---
  168. bool PointerProperties::operator==(const PointerProperties& other) const {
  169. return id == other.id
  170. && toolType == other.toolType;
  171. }
  172. void PointerProperties::copyFrom(const PointerProperties& other) {
  173. id = other.id;
  174. toolType = other.toolType;
  175. }
  176. // --- MotionEvent ---
  177. void MotionEvent::initialize(
  178. int32_t deviceId,
  179. int32_t source,
  180. int32_t action,
  181. int32_t actionButton,
  182. int32_t flags,
  183. int32_t edgeFlags,
  184. int32_t metaState,
  185. int32_t buttonState,
  186. float xOffset,
  187. float yOffset,
  188. float xPrecision,
  189. float yPrecision,
  190. nsecs_t downTime,
  191. nsecs_t eventTime,
  192. size_t pointerCount,
  193. const PointerProperties* pointerProperties,
  194. const PointerCoords* pointerCoords) {
  195. InputEvent::initialize(deviceId, source);
  196. mAction = action;
  197. mActionButton = actionButton;
  198. mFlags = flags;
  199. mEdgeFlags = edgeFlags;
  200. mMetaState = metaState;
  201. mButtonState = buttonState;
  202. mXOffset = xOffset;
  203. mYOffset = yOffset;
  204. mXPrecision = xPrecision;
  205. mYPrecision = yPrecision;
  206. mDownTime = downTime;
  207. mPointerProperties.clear();
  208. mPointerProperties.appendArray(pointerProperties, pointerCount);
  209. mSampleEventTimes.clear();
  210. mSamplePointerCoords.clear();
  211. addSample(eventTime, pointerCoords);
  212. }
  213. void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
  214. InputEvent::initialize(other->mDeviceId, other->mSource);
  215. mAction = other->mAction;
  216. mActionButton = other->mActionButton;
  217. mFlags = other->mFlags;
  218. mEdgeFlags = other->mEdgeFlags;
  219. mMetaState = other->mMetaState;
  220. mButtonState = other->mButtonState;
  221. mXOffset = other->mXOffset;
  222. mYOffset = other->mYOffset;
  223. mXPrecision = other->mXPrecision;
  224. mYPrecision = other->mYPrecision;
  225. mDownTime = other->mDownTime;
  226. mPointerProperties = other->mPointerProperties;
  227. if (keepHistory) {
  228. mSampleEventTimes = other->mSampleEventTimes;
  229. mSamplePointerCoords = other->mSamplePointerCoords;
  230. } else {
  231. mSampleEventTimes.clear();
  232. mSampleEventTimes.push(other->getEventTime());
  233. mSamplePointerCoords.clear();
  234. size_t pointerCount = other->getPointerCount();
  235. size_t historySize = other->getHistorySize();
  236. mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
  237. + (historySize * pointerCount), pointerCount);
  238. }
  239. }
  240. void MotionEvent::addSample(
  241. int64_t eventTime,
  242. const PointerCoords* pointerCoords) {
  243. mSampleEventTimes.push(eventTime);
  244. mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
  245. }
  246. const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
  247. return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
  248. }
  249. float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
  250. return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
  251. }
  252. float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
  253. float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
  254. switch (axis) {
  255. case AMOTION_EVENT_AXIS_X:
  256. return value + mXOffset;
  257. case AMOTION_EVENT_AXIS_Y:
  258. return value + mYOffset;
  259. }
  260. return value;
  261. }
  262. const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
  263. size_t pointerIndex, size_t historicalIndex) const {
  264. return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
  265. }
  266. float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
  267. size_t historicalIndex) const {
  268. return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
  269. }
  270. float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
  271. size_t historicalIndex) const {
  272. float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
  273. switch (axis) {
  274. case AMOTION_EVENT_AXIS_X:
  275. return value + mXOffset;
  276. case AMOTION_EVENT_AXIS_Y:
  277. return value + mYOffset;
  278. }
  279. return value;
  280. }
  281. ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
  282. size_t pointerCount = mPointerProperties.size();
  283. for (size_t i = 0; i < pointerCount; i++) {
  284. if (mPointerProperties.itemAt(i).id == pointerId) {
  285. return i;
  286. }
  287. }
  288. return -1;
  289. }
  290. void MotionEvent::offsetLocation(float xOffset, float yOffset) {
  291. mXOffset += xOffset;
  292. mYOffset += yOffset;
  293. }
  294. void MotionEvent::scale(float scaleFactor) {
  295. mXOffset *= scaleFactor;
  296. mYOffset *= scaleFactor;
  297. mXPrecision *= scaleFactor;
  298. mYPrecision *= scaleFactor;
  299. size_t numSamples = mSamplePointerCoords.size();
  300. for (size_t i = 0; i < numSamples; i++) {
  301. mSamplePointerCoords.editItemAt(i).scale(scaleFactor);
  302. }
  303. }
  304. static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
  305. // Apply perspective transform like Skia.
  306. float newX = matrix[0] * x + matrix[1] * y + matrix[2];
  307. float newY = matrix[3] * x + matrix[4] * y + matrix[5];
  308. float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
  309. if (newZ) {
  310. newZ = 1.0f / newZ;
  311. }
  312. *outX = newX * newZ;
  313. *outY = newY * newZ;
  314. }
  315. static float transformAngle(const float matrix[9], float angleRadians,
  316. float originX, float originY) {
  317. // Construct and transform a vector oriented at the specified clockwise angle from vertical.
  318. // Coordinate system: down is increasing Y, right is increasing X.
  319. float x = sinf(angleRadians);
  320. float y = -cosf(angleRadians);
  321. transformPoint(matrix, x, y, &x, &y);
  322. x -= originX;
  323. y -= originY;
  324. // Derive the transformed vector's clockwise angle from vertical.
  325. float result = atan2f(x, -y);
  326. if (result < - M_PI_2) {
  327. result += M_PI;
  328. } else if (result > M_PI_2) {
  329. result -= M_PI;
  330. }
  331. return result;
  332. }
  333. void MotionEvent::transform(const float matrix[9]) {
  334. // The tricky part of this implementation is to preserve the value of
  335. // rawX and rawY. So we apply the transformation to the first point
  336. // then derive an appropriate new X/Y offset that will preserve rawX
  337. // and rawY for that point.
  338. float oldXOffset = mXOffset;
  339. float oldYOffset = mYOffset;
  340. float newX, newY;
  341. float rawX = getRawX(0);
  342. float rawY = getRawY(0);
  343. transformPoint(matrix, rawX + oldXOffset, rawY + oldYOffset, &newX, &newY);
  344. mXOffset = newX - rawX;
  345. mYOffset = newY - rawY;
  346. // Determine how the origin is transformed by the matrix so that we
  347. // can transform orientation vectors.
  348. float originX, originY;
  349. transformPoint(matrix, 0, 0, &originX, &originY);
  350. // Apply the transformation to all samples.
  351. size_t numSamples = mSamplePointerCoords.size();
  352. for (size_t i = 0; i < numSamples; i++) {
  353. PointerCoords& c = mSamplePointerCoords.editItemAt(i);
  354. float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) + oldXOffset;
  355. float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) + oldYOffset;
  356. transformPoint(matrix, x, y, &x, &y);
  357. c.setAxisValue(AMOTION_EVENT_AXIS_X, x - mXOffset);
  358. c.setAxisValue(AMOTION_EVENT_AXIS_Y, y - mYOffset);
  359. float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
  360. c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
  361. transformAngle(matrix, orientation, originX, originY));
  362. }
  363. }
  364. #ifdef HAVE_ANDROID_OS
  365. status_t MotionEvent::readFromParcel(Parcel* parcel) {
  366. size_t pointerCount = parcel->readInt32();
  367. size_t sampleCount = parcel->readInt32();
  368. if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
  369. sampleCount == 0 || sampleCount > MAX_SAMPLES) {
  370. return BAD_VALUE;
  371. }
  372. mDeviceId = parcel->readInt32();
  373. mSource = parcel->readInt32();
  374. mAction = parcel->readInt32();
  375. mActionButton = parcel->readInt32();
  376. mFlags = parcel->readInt32();
  377. mEdgeFlags = parcel->readInt32();
  378. mMetaState = parcel->readInt32();
  379. mButtonState = parcel->readInt32();
  380. mXOffset = parcel->readFloat();
  381. mYOffset = parcel->readFloat();
  382. mXPrecision = parcel->readFloat();
  383. mYPrecision = parcel->readFloat();
  384. mDownTime = parcel->readInt64();
  385. mPointerProperties.clear();
  386. mPointerProperties.setCapacity(pointerCount);
  387. mSampleEventTimes.clear();
  388. mSampleEventTimes.setCapacity(sampleCount);
  389. mSamplePointerCoords.clear();
  390. mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
  391. for (size_t i = 0; i < pointerCount; i++) {
  392. mPointerProperties.push();
  393. PointerProperties& properties = mPointerProperties.editTop();
  394. properties.id = parcel->readInt32();
  395. properties.toolType = parcel->readInt32();
  396. }
  397. while (sampleCount-- > 0) {
  398. mSampleEventTimes.push(parcel->readInt64());
  399. for (size_t i = 0; i < pointerCount; i++) {
  400. mSamplePointerCoords.push();
  401. status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
  402. if (status) {
  403. return status;
  404. }
  405. }
  406. }
  407. return OK;
  408. }
  409. status_t MotionEvent::writeToParcel(Parcel* parcel) const {
  410. size_t pointerCount = mPointerProperties.size();
  411. size_t sampleCount = mSampleEventTimes.size();
  412. parcel->writeInt32(pointerCount);
  413. parcel->writeInt32(sampleCount);
  414. parcel->writeInt32(mDeviceId);
  415. parcel->writeInt32(mSource);
  416. parcel->writeInt32(mAction);
  417. parcel->writeInt32(mActionButton);
  418. parcel->writeInt32(mFlags);
  419. parcel->writeInt32(mEdgeFlags);
  420. parcel->writeInt32(mMetaState);
  421. parcel->writeInt32(mButtonState);
  422. parcel->writeFloat(mXOffset);
  423. parcel->writeFloat(mYOffset);
  424. parcel->writeFloat(mXPrecision);
  425. parcel->writeFloat(mYPrecision);
  426. parcel->writeInt64(mDownTime);
  427. for (size_t i = 0; i < pointerCount; i++) {
  428. const PointerProperties& properties = mPointerProperties.itemAt(i);
  429. parcel->writeInt32(properties.id);
  430. parcel->writeInt32(properties.toolType);
  431. }
  432. const PointerCoords* pc = mSamplePointerCoords.array();
  433. for (size_t h = 0; h < sampleCount; h++) {
  434. parcel->writeInt64(mSampleEventTimes.itemAt(h));
  435. for (size_t i = 0; i < pointerCount; i++) {
  436. status_t status = (pc++)->writeToParcel(parcel);
  437. if (status) {
  438. return status;
  439. }
  440. }
  441. }
  442. return OK;
  443. }
  444. #endif
  445. bool MotionEvent::isTouchEvent(int32_t source, int32_t action) {
  446. if (source & AINPUT_SOURCE_CLASS_POINTER) {
  447. // Specifically excludes HOVER_MOVE and SCROLL.
  448. switch (action & AMOTION_EVENT_ACTION_MASK) {
  449. case AMOTION_EVENT_ACTION_DOWN:
  450. case AMOTION_EVENT_ACTION_MOVE:
  451. case AMOTION_EVENT_ACTION_UP:
  452. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  453. case AMOTION_EVENT_ACTION_POINTER_UP:
  454. case AMOTION_EVENT_ACTION_CANCEL:
  455. case AMOTION_EVENT_ACTION_OUTSIDE:
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. const char* MotionEvent::getLabel(int32_t axis) {
  462. return getAxisLabel(axis);
  463. }
  464. int32_t MotionEvent::getAxisFromLabel(const char* label) {
  465. return getAxisByLabel(label);
  466. }
  467. // --- PooledInputEventFactory ---
  468. PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
  469. mMaxPoolSize(maxPoolSize) {
  470. }
  471. PooledInputEventFactory::~PooledInputEventFactory() {
  472. for (size_t i = 0; i < mKeyEventPool.size(); i++) {
  473. delete mKeyEventPool.itemAt(i);
  474. }
  475. for (size_t i = 0; i < mMotionEventPool.size(); i++) {
  476. delete mMotionEventPool.itemAt(i);
  477. }
  478. }
  479. KeyEvent* PooledInputEventFactory::createKeyEvent() {
  480. if (!mKeyEventPool.isEmpty()) {
  481. KeyEvent* event = mKeyEventPool.top();
  482. mKeyEventPool.pop();
  483. return event;
  484. }
  485. return new KeyEvent();
  486. }
  487. MotionEvent* PooledInputEventFactory::createMotionEvent() {
  488. if (!mMotionEventPool.isEmpty()) {
  489. MotionEvent* event = mMotionEventPool.top();
  490. mMotionEventPool.pop();
  491. return event;
  492. }
  493. return new MotionEvent();
  494. }
  495. void PooledInputEventFactory::recycle(InputEvent* event) {
  496. switch (event->getType()) {
  497. case AINPUT_EVENT_TYPE_KEY:
  498. if (mKeyEventPool.size() < mMaxPoolSize) {
  499. mKeyEventPool.push(static_cast<KeyEvent*>(event));
  500. return;
  501. }
  502. break;
  503. case AINPUT_EVENT_TYPE_MOTION:
  504. if (mMotionEventPool.size() < mMaxPoolSize) {
  505. mMotionEventPool.push(static_cast<MotionEvent*>(event));
  506. return;
  507. }
  508. break;
  509. }
  510. delete event;
  511. }
  512. } // namespace android