InputReader.h 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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. #ifndef _UI_INPUT_READER_H
  17. #define _UI_INPUT_READER_H
  18. #include "EventHub.h"
  19. #include "PointerControllerInterface.h"
  20. #include "InputListener.h"
  21. #include <input/Input.h>
  22. #include <input/VelocityControl.h>
  23. #include <input/VelocityTracker.h>
  24. #include <ui/DisplayInfo.h>
  25. #include <utils/KeyedVector.h>
  26. #include <utils/threads.h>
  27. #include <utils/Timers.h>
  28. #include <utils/RefBase.h>
  29. #include <utils/String8.h>
  30. #include <utils/BitSet.h>
  31. #include <stddef.h>
  32. #include <unistd.h>
  33. // Maximum supported size of a vibration pattern.
  34. // Must be at least 2.
  35. #define MAX_VIBRATE_PATTERN_SIZE 100
  36. // Maximum allowable delay value in a vibration pattern before
  37. // which the delay will be truncated.
  38. #define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
  39. namespace android {
  40. class InputDevice;
  41. class InputMapper;
  42. /*
  43. * Describes how coordinates are mapped on a physical display.
  44. * See com.android.server.display.DisplayViewport.
  45. */
  46. struct DisplayViewport {
  47. int32_t displayId; // -1 if invalid
  48. int32_t orientation;
  49. int32_t logicalLeft;
  50. int32_t logicalTop;
  51. int32_t logicalRight;
  52. int32_t logicalBottom;
  53. int32_t physicalLeft;
  54. int32_t physicalTop;
  55. int32_t physicalRight;
  56. int32_t physicalBottom;
  57. int32_t deviceWidth;
  58. int32_t deviceHeight;
  59. DisplayViewport() :
  60. displayId(ADISPLAY_ID_NONE), orientation(DISPLAY_ORIENTATION_0),
  61. logicalLeft(0), logicalTop(0), logicalRight(0), logicalBottom(0),
  62. physicalLeft(0), physicalTop(0), physicalRight(0), physicalBottom(0),
  63. deviceWidth(0), deviceHeight(0) {
  64. }
  65. bool operator==(const DisplayViewport& other) const {
  66. return displayId == other.displayId
  67. && orientation == other.orientation
  68. && logicalLeft == other.logicalLeft
  69. && logicalTop == other.logicalTop
  70. && logicalRight == other.logicalRight
  71. && logicalBottom == other.logicalBottom
  72. && physicalLeft == other.physicalLeft
  73. && physicalTop == other.physicalTop
  74. && physicalRight == other.physicalRight
  75. && physicalBottom == other.physicalBottom
  76. && deviceWidth == other.deviceWidth
  77. && deviceHeight == other.deviceHeight;
  78. }
  79. bool operator!=(const DisplayViewport& other) const {
  80. return !(*this == other);
  81. }
  82. inline bool isValid() const {
  83. return displayId >= 0;
  84. }
  85. void setNonDisplayViewport(int32_t width, int32_t height) {
  86. displayId = ADISPLAY_ID_NONE;
  87. orientation = DISPLAY_ORIENTATION_0;
  88. logicalLeft = 0;
  89. logicalTop = 0;
  90. logicalRight = width;
  91. logicalBottom = height;
  92. physicalLeft = 0;
  93. physicalTop = 0;
  94. physicalRight = width;
  95. physicalBottom = height;
  96. deviceWidth = width;
  97. deviceHeight = height;
  98. }
  99. };
  100. /*
  101. * Input reader configuration.
  102. *
  103. * Specifies various options that modify the behavior of the input reader.
  104. */
  105. struct InputReaderConfiguration {
  106. // Describes changes that have occurred.
  107. enum {
  108. // The pointer speed changed.
  109. CHANGE_POINTER_SPEED = 1 << 0,
  110. // The pointer gesture control changed.
  111. CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
  112. // The display size or orientation changed.
  113. CHANGE_DISPLAY_INFO = 1 << 2,
  114. // The visible touches option changed.
  115. CHANGE_SHOW_TOUCHES = 1 << 3,
  116. // The keyboard layouts must be reloaded.
  117. CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
  118. // The device name alias supplied by the may have changed for some devices.
  119. CHANGE_DEVICE_ALIAS = 1 << 5,
  120. // The location calibration matrix changed.
  121. CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
  122. // The presence of an external stylus has changed.
  123. CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
  124. // Volume keys rotation option changed.
  125. CHANGE_VOLUME_KEYS_ROTATION = 1 << 8,
  126. // Stylus icon option changed.
  127. CHANGE_STYLUS_ICON_ENABLED = 1 << 9,
  128. // All devices must be reopened.
  129. CHANGE_MUST_REOPEN = 1 << 31,
  130. };
  131. // Gets the amount of time to disable virtual keys after the screen is touched
  132. // in order to filter out accidental virtual key presses due to swiping gestures
  133. // or taps near the edge of the display. May be 0 to disable the feature.
  134. nsecs_t virtualKeyQuietTime;
  135. // The excluded device names for the platform.
  136. // Devices with these names will be ignored.
  137. Vector<String8> excludedDeviceNames;
  138. // Velocity control parameters for mouse pointer movements.
  139. VelocityControlParameters pointerVelocityControlParameters;
  140. // Velocity control parameters for mouse wheel movements.
  141. VelocityControlParameters wheelVelocityControlParameters;
  142. // True if pointer gestures are enabled.
  143. bool pointerGesturesEnabled;
  144. // Quiet time between certain pointer gesture transitions.
  145. // Time to allow for all fingers or buttons to settle into a stable state before
  146. // starting a new gesture.
  147. nsecs_t pointerGestureQuietInterval;
  148. // The minimum speed that a pointer must travel for us to consider switching the active
  149. // touch pointer to it during a drag. This threshold is set to avoid switching due
  150. // to noise from a finger resting on the touch pad (perhaps just pressing it down).
  151. float pointerGestureDragMinSwitchSpeed; // in pixels per second
  152. // Tap gesture delay time.
  153. // The time between down and up must be less than this to be considered a tap.
  154. nsecs_t pointerGestureTapInterval;
  155. // Tap drag gesture delay time.
  156. // The time between the previous tap's up and the next down must be less than
  157. // this to be considered a drag. Otherwise, the previous tap is finished and a
  158. // new tap begins.
  159. //
  160. // Note that the previous tap will be held down for this entire duration so this
  161. // interval must be shorter than the long press timeout.
  162. nsecs_t pointerGestureTapDragInterval;
  163. // The distance in pixels that the pointer is allowed to move from initial down
  164. // to up and still be called a tap.
  165. float pointerGestureTapSlop; // in pixels
  166. // Time after the first touch points go down to settle on an initial centroid.
  167. // This is intended to be enough time to handle cases where the user puts down two
  168. // fingers at almost but not quite exactly the same time.
  169. nsecs_t pointerGestureMultitouchSettleInterval;
  170. // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
  171. // at least two pointers have moved at least this far from their starting place.
  172. float pointerGestureMultitouchMinDistance; // in pixels
  173. // The transition from PRESS to SWIPE gesture mode can only occur when the
  174. // cosine of the angle between the two vectors is greater than or equal to than this value
  175. // which indicates that the vectors are oriented in the same direction.
  176. // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
  177. // (In exactly opposite directions, the cosine is -1.0.)
  178. float pointerGestureSwipeTransitionAngleCosine;
  179. // The transition from PRESS to SWIPE gesture mode can only occur when the
  180. // fingers are no more than this far apart relative to the diagonal size of
  181. // the touch pad. For example, a ratio of 0.5 means that the fingers must be
  182. // no more than half the diagonal size of the touch pad apart.
  183. float pointerGestureSwipeMaxWidthRatio;
  184. // The gesture movement speed factor relative to the size of the display.
  185. // Movement speed applies when the fingers are moving in the same direction.
  186. // Without acceleration, a full swipe of the touch pad diagonal in movement mode
  187. // will cover this portion of the display diagonal.
  188. float pointerGestureMovementSpeedRatio;
  189. // The gesture zoom speed factor relative to the size of the display.
  190. // Zoom speed applies when the fingers are mostly moving relative to each other
  191. // to execute a scale gesture or similar.
  192. // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
  193. // will cover this portion of the display diagonal.
  194. float pointerGestureZoomSpeedRatio;
  195. // True to show the location of touches on the touch screen as spots.
  196. bool showTouches;
  197. // True to show the pointer icon when a stylus is used.
  198. bool stylusIconEnabled;
  199. // Ignore finger touches this long after the stylus has been used (including hover)
  200. nsecs_t stylusPalmRejectionTime;
  201. // Remap volume keys according to display rotation
  202. // 0 - disabled, 1 - phone or hybrid rotation mode, 2 - tablet rotation mode
  203. int volumeKeysRotationMode;
  204. InputReaderConfiguration() :
  205. virtualKeyQuietTime(0),
  206. pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
  207. wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
  208. pointerGesturesEnabled(true),
  209. pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
  210. pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
  211. pointerGestureTapInterval(150 * 1000000LL), // 150 ms
  212. pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
  213. pointerGestureTapSlop(10.0f), // 10 pixels
  214. pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
  215. pointerGestureMultitouchMinDistance(15), // 15 pixels
  216. pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
  217. pointerGestureSwipeMaxWidthRatio(0.25f),
  218. pointerGestureMovementSpeedRatio(0.8f),
  219. pointerGestureZoomSpeedRatio(0.3f),
  220. showTouches(false),
  221. stylusIconEnabled(false),
  222. stylusPalmRejectionTime(50 * 10000000LL), // 50 ms
  223. volumeKeysRotationMode(0)
  224. { }
  225. bool getDisplayInfo(bool external, DisplayViewport* outViewport) const;
  226. void setDisplayInfo(bool external, const DisplayViewport& viewport);
  227. private:
  228. DisplayViewport mInternalDisplay;
  229. DisplayViewport mExternalDisplay;
  230. };
  231. struct TouchAffineTransformation {
  232. float x_scale;
  233. float x_ymix;
  234. float x_offset;
  235. float y_xmix;
  236. float y_scale;
  237. float y_offset;
  238. TouchAffineTransformation() :
  239. x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
  240. y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
  241. }
  242. TouchAffineTransformation(float xscale, float xymix, float xoffset,
  243. float yxmix, float yscale, float yoffset) :
  244. x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
  245. y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
  246. }
  247. void applyTo(float& x, float& y) const;
  248. };
  249. /*
  250. * Input reader policy interface.
  251. *
  252. * The input reader policy is used by the input reader to interact with the Window Manager
  253. * and other system components.
  254. *
  255. * The actual implementation is partially supported by callbacks into the DVM
  256. * via JNI. This interface is also mocked in the unit tests.
  257. *
  258. * These methods must NOT re-enter the input reader since they may be called while
  259. * holding the input reader lock.
  260. */
  261. class InputReaderPolicyInterface : public virtual RefBase {
  262. protected:
  263. InputReaderPolicyInterface() { }
  264. virtual ~InputReaderPolicyInterface() { }
  265. public:
  266. /* Gets the input reader configuration. */
  267. virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
  268. /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
  269. virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
  270. /* Notifies the input reader policy that some input devices have changed
  271. * and provides information about all current input devices.
  272. */
  273. virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
  274. /* Gets the keyboard layout for a particular input device. */
  275. virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
  276. const InputDeviceIdentifier& identifier) = 0;
  277. /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
  278. virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
  279. /* Gets the affine calibration associated with the specified device. */
  280. virtual TouchAffineTransformation getTouchAffineTransformation(
  281. const String8& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
  282. };
  283. /* Processes raw input events and sends cooked event data to an input listener. */
  284. class InputReaderInterface : public virtual RefBase {
  285. protected:
  286. InputReaderInterface() { }
  287. virtual ~InputReaderInterface() { }
  288. public:
  289. /* Dumps the state of the input reader.
  290. *
  291. * This method may be called on any thread (usually by the input manager). */
  292. virtual void dump(String8& dump) = 0;
  293. /* Called by the heatbeat to ensures that the reader has not deadlocked. */
  294. virtual void monitor() = 0;
  295. /* Runs a single iteration of the processing loop.
  296. * Nominally reads and processes one incoming message from the EventHub.
  297. *
  298. * This method should be called on the input reader thread.
  299. */
  300. virtual void loopOnce() = 0;
  301. /* Gets information about all input devices.
  302. *
  303. * This method may be called on any thread (usually by the input manager).
  304. */
  305. virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
  306. /* Query current input state. */
  307. virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
  308. int32_t scanCode) = 0;
  309. virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
  310. int32_t keyCode) = 0;
  311. virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
  312. int32_t sw) = 0;
  313. /* Determine whether physical keys exist for the given framework-domain key codes. */
  314. virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
  315. size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
  316. /* Requests that a reconfiguration of all input devices.
  317. * The changes flag is a bitfield that indicates what has changed and whether
  318. * the input devices must all be reopened. */
  319. virtual void requestRefreshConfiguration(uint32_t changes) = 0;
  320. /* Controls the vibrator of a particular input device. */
  321. virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
  322. ssize_t repeat, int32_t token) = 0;
  323. virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
  324. };
  325. struct StylusState {
  326. /* Time the stylus event was received. */
  327. nsecs_t when;
  328. /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */
  329. float pressure;
  330. /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */
  331. uint32_t buttons;
  332. /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */
  333. int32_t toolType;
  334. void copyFrom(const StylusState& other) {
  335. when = other.when;
  336. pressure = other.pressure;
  337. buttons = other.buttons;
  338. toolType = other.toolType;
  339. }
  340. void clear() {
  341. when = LLONG_MAX;
  342. pressure = 0.f;
  343. buttons = 0;
  344. toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
  345. }
  346. };
  347. /* Internal interface used by individual input devices to access global input device state
  348. * and parameters maintained by the input reader.
  349. */
  350. class InputReaderContext {
  351. public:
  352. InputReaderContext() { }
  353. virtual ~InputReaderContext() { }
  354. virtual void updateGlobalMetaState() = 0;
  355. virtual int32_t getGlobalMetaState() = 0;
  356. virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
  357. virtual bool shouldDropVirtualKey(nsecs_t now,
  358. InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
  359. virtual void fadePointer() = 0;
  360. virtual void requestTimeoutAtTime(nsecs_t when) = 0;
  361. virtual int32_t bumpGeneration() = 0;
  362. virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) = 0;
  363. virtual void dispatchExternalStylusState(const StylusState& outState) = 0;
  364. virtual InputReaderPolicyInterface* getPolicy() = 0;
  365. virtual InputListenerInterface* getListener() = 0;
  366. virtual EventHubInterface* getEventHub() = 0;
  367. };
  368. /* The input reader reads raw event data from the event hub and processes it into input events
  369. * that it sends to the input listener. Some functions of the input reader, such as early
  370. * event filtering in low power states, are controlled by a separate policy object.
  371. *
  372. * The InputReader owns a collection of InputMappers. Most of the work it does happens
  373. * on the input reader thread but the InputReader can receive queries from other system
  374. * components running on arbitrary threads. To keep things manageable, the InputReader
  375. * uses a single Mutex to guard its state. The Mutex may be held while calling into the
  376. * EventHub or the InputReaderPolicy but it is never held while calling into the
  377. * InputListener.
  378. */
  379. class InputReader : public InputReaderInterface {
  380. public:
  381. InputReader(const sp<EventHubInterface>& eventHub,
  382. const sp<InputReaderPolicyInterface>& policy,
  383. const sp<InputListenerInterface>& listener);
  384. virtual ~InputReader();
  385. virtual void dump(String8& dump);
  386. virtual void monitor();
  387. virtual void loopOnce();
  388. virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
  389. virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
  390. int32_t scanCode);
  391. virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
  392. int32_t keyCode);
  393. virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
  394. int32_t sw);
  395. virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
  396. size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
  397. virtual void requestRefreshConfiguration(uint32_t changes);
  398. virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
  399. ssize_t repeat, int32_t token);
  400. virtual void cancelVibrate(int32_t deviceId, int32_t token);
  401. protected:
  402. // These members are protected so they can be instrumented by test cases.
  403. virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
  404. const InputDeviceIdentifier& identifier, uint32_t classes);
  405. class ContextImpl : public InputReaderContext {
  406. InputReader* mReader;
  407. public:
  408. ContextImpl(InputReader* reader);
  409. virtual void updateGlobalMetaState();
  410. virtual int32_t getGlobalMetaState();
  411. virtual void disableVirtualKeysUntil(nsecs_t time);
  412. virtual bool shouldDropVirtualKey(nsecs_t now,
  413. InputDevice* device, int32_t keyCode, int32_t scanCode);
  414. virtual void fadePointer();
  415. virtual void requestTimeoutAtTime(nsecs_t when);
  416. virtual int32_t bumpGeneration();
  417. virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices);
  418. virtual void dispatchExternalStylusState(const StylusState& outState);
  419. virtual InputReaderPolicyInterface* getPolicy();
  420. virtual InputListenerInterface* getListener();
  421. virtual EventHubInterface* getEventHub();
  422. } mContext;
  423. friend class ContextImpl;
  424. private:
  425. Mutex mLock;
  426. Condition mReaderIsAliveCondition;
  427. sp<EventHubInterface> mEventHub;
  428. sp<InputReaderPolicyInterface> mPolicy;
  429. sp<QueuedInputListener> mQueuedListener;
  430. InputReaderConfiguration mConfig;
  431. // The event queue.
  432. static const int EVENT_BUFFER_SIZE = 256;
  433. RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
  434. KeyedVector<int32_t, InputDevice*> mDevices;
  435. // low-level input event decoding and device management
  436. void processEventsLocked(const RawEvent* rawEvents, size_t count);
  437. void addDeviceLocked(nsecs_t when, int32_t deviceId);
  438. void removeDeviceLocked(nsecs_t when, int32_t deviceId);
  439. void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
  440. void timeoutExpiredLocked(nsecs_t when);
  441. void handleConfigurationChangedLocked(nsecs_t when);
  442. int32_t mGlobalMetaState;
  443. void updateGlobalMetaStateLocked();
  444. int32_t getGlobalMetaStateLocked();
  445. void notifyExternalStylusPresenceChanged();
  446. void getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices);
  447. void dispatchExternalStylusState(const StylusState& state);
  448. void fadePointerLocked();
  449. int32_t mGeneration;
  450. int32_t bumpGenerationLocked();
  451. void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
  452. nsecs_t mDisableVirtualKeysTimeout;
  453. void disableVirtualKeysUntilLocked(nsecs_t time);
  454. bool shouldDropVirtualKeyLocked(nsecs_t now,
  455. InputDevice* device, int32_t keyCode, int32_t scanCode);
  456. nsecs_t mNextTimeout;
  457. void requestTimeoutAtTimeLocked(nsecs_t when);
  458. uint32_t mConfigurationChangesToRefresh;
  459. void refreshConfigurationLocked(uint32_t changes);
  460. // state queries
  461. typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
  462. int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
  463. GetStateFunc getStateFunc);
  464. bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
  465. const int32_t* keyCodes, uint8_t* outFlags);
  466. };
  467. /* Reads raw events from the event hub and processes them, endlessly. */
  468. class InputReaderThread : public Thread {
  469. public:
  470. InputReaderThread(const sp<InputReaderInterface>& reader);
  471. virtual ~InputReaderThread();
  472. private:
  473. sp<InputReaderInterface> mReader;
  474. virtual bool threadLoop();
  475. };
  476. /* Represents the state of a single input device. */
  477. class InputDevice {
  478. public:
  479. InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
  480. controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
  481. ~InputDevice();
  482. inline InputReaderContext* getContext() { return mContext; }
  483. inline int32_t getId() const { return mId; }
  484. inline int32_t getControllerNumber() const { return mControllerNumber; }
  485. inline int32_t getGeneration() const { return mGeneration; }
  486. inline const String8& getName() const { return mIdentifier.name; }
  487. inline const String8& getDescriptor() { return mIdentifier.descriptor; }
  488. inline uint32_t getClasses() const { return mClasses; }
  489. inline uint32_t getSources() const { return mSources; }
  490. inline bool isExternal() { return mIsExternal; }
  491. inline void setExternal(bool external) { mIsExternal = external; }
  492. inline void setMic(bool hasMic) { mHasMic = hasMic; }
  493. inline bool hasMic() const { return mHasMic; }
  494. inline bool isIgnored() { return mMappers.isEmpty(); }
  495. void dump(String8& dump);
  496. void addMapper(InputMapper* mapper);
  497. void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  498. void reset(nsecs_t when);
  499. void process(const RawEvent* rawEvents, size_t count);
  500. void timeoutExpired(nsecs_t when);
  501. void updateExternalStylusState(const StylusState& state);
  502. void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
  503. int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
  504. int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
  505. int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
  506. bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  507. const int32_t* keyCodes, uint8_t* outFlags);
  508. void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
  509. void cancelVibrate(int32_t token);
  510. void cancelTouch(nsecs_t when);
  511. int32_t getMetaState();
  512. void fadePointer();
  513. void bumpGeneration();
  514. void notifyReset(nsecs_t when);
  515. inline const PropertyMap& getConfiguration() { return mConfiguration; }
  516. inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
  517. bool hasKey(int32_t code) {
  518. return getEventHub()->hasScanCode(mId, code);
  519. }
  520. bool hasAbsoluteAxis(int32_t code) {
  521. RawAbsoluteAxisInfo info;
  522. getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
  523. return info.valid;
  524. }
  525. bool isKeyPressed(int32_t code) {
  526. return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
  527. }
  528. int32_t getAbsoluteAxisValue(int32_t code) {
  529. int32_t value;
  530. getEventHub()->getAbsoluteAxisValue(mId, code, &value);
  531. return value;
  532. }
  533. private:
  534. InputReaderContext* mContext;
  535. int32_t mId;
  536. int32_t mGeneration;
  537. int32_t mControllerNumber;
  538. InputDeviceIdentifier mIdentifier;
  539. String8 mAlias;
  540. uint32_t mClasses;
  541. Vector<InputMapper*> mMappers;
  542. uint32_t mSources;
  543. bool mIsExternal;
  544. bool mHasMic;
  545. bool mDropUntilNextSync;
  546. typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
  547. int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
  548. PropertyMap mConfiguration;
  549. };
  550. /* Keeps track of the state of mouse or touch pad buttons. */
  551. class CursorButtonAccumulator {
  552. public:
  553. CursorButtonAccumulator();
  554. void reset(InputDevice* device);
  555. void process(const RawEvent* rawEvent);
  556. uint32_t getButtonState() const;
  557. private:
  558. bool mBtnLeft;
  559. bool mBtnRight;
  560. bool mBtnMiddle;
  561. bool mBtnBack;
  562. bool mBtnSide;
  563. bool mBtnForward;
  564. bool mBtnExtra;
  565. bool mBtnTask;
  566. void clearButtons();
  567. };
  568. /* Keeps track of cursor movements. */
  569. class CursorMotionAccumulator {
  570. public:
  571. CursorMotionAccumulator();
  572. void reset(InputDevice* device);
  573. void process(const RawEvent* rawEvent);
  574. void finishSync();
  575. inline int32_t getRelativeX() const { return mRelX; }
  576. inline int32_t getRelativeY() const { return mRelY; }
  577. private:
  578. int32_t mRelX;
  579. int32_t mRelY;
  580. void clearRelativeAxes();
  581. };
  582. /* Keeps track of cursor scrolling motions. */
  583. class CursorScrollAccumulator {
  584. public:
  585. CursorScrollAccumulator();
  586. void configure(InputDevice* device);
  587. void reset(InputDevice* device);
  588. void process(const RawEvent* rawEvent);
  589. void finishSync();
  590. inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
  591. inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
  592. inline int32_t getRelativeX() const { return mRelX; }
  593. inline int32_t getRelativeY() const { return mRelY; }
  594. inline int32_t getRelativeVWheel() const { return mRelWheel; }
  595. inline int32_t getRelativeHWheel() const { return mRelHWheel; }
  596. private:
  597. bool mHaveRelWheel;
  598. bool mHaveRelHWheel;
  599. int32_t mRelX;
  600. int32_t mRelY;
  601. int32_t mRelWheel;
  602. int32_t mRelHWheel;
  603. void clearRelativeAxes();
  604. };
  605. /* Keeps track of the state of touch, stylus and tool buttons. */
  606. class TouchButtonAccumulator {
  607. public:
  608. TouchButtonAccumulator();
  609. void configure(InputDevice* device);
  610. void reset(InputDevice* device);
  611. void process(const RawEvent* rawEvent);
  612. uint32_t getButtonState() const;
  613. int32_t getToolType() const;
  614. bool isToolActive() const;
  615. bool isHovering() const;
  616. bool hasStylus() const;
  617. private:
  618. bool mHaveBtnTouch;
  619. bool mHaveStylus;
  620. bool mBtnTouch;
  621. bool mBtnStylus;
  622. bool mBtnStylus2;
  623. bool mBtnToolFinger;
  624. bool mBtnToolPen;
  625. bool mBtnToolRubber;
  626. bool mBtnToolBrush;
  627. bool mBtnToolPencil;
  628. bool mBtnToolAirbrush;
  629. bool mBtnToolMouse;
  630. bool mBtnToolLens;
  631. bool mBtnToolDoubleTap;
  632. bool mBtnToolTripleTap;
  633. bool mBtnToolQuadTap;
  634. void clearButtons();
  635. };
  636. /* Raw axis information from the driver. */
  637. struct RawPointerAxes {
  638. RawAbsoluteAxisInfo x;
  639. RawAbsoluteAxisInfo y;
  640. RawAbsoluteAxisInfo pressure;
  641. RawAbsoluteAxisInfo touchMajor;
  642. RawAbsoluteAxisInfo touchMinor;
  643. RawAbsoluteAxisInfo toolMajor;
  644. RawAbsoluteAxisInfo toolMinor;
  645. RawAbsoluteAxisInfo orientation;
  646. RawAbsoluteAxisInfo distance;
  647. RawAbsoluteAxisInfo tiltX;
  648. RawAbsoluteAxisInfo tiltY;
  649. RawAbsoluteAxisInfo trackingId;
  650. RawAbsoluteAxisInfo slot;
  651. RawPointerAxes();
  652. void clear();
  653. };
  654. /* Raw data for a collection of pointers including a pointer id mapping table. */
  655. struct RawPointerData {
  656. struct Pointer {
  657. uint32_t id;
  658. int32_t x;
  659. int32_t y;
  660. int32_t pressure;
  661. int32_t touchMajor;
  662. int32_t touchMinor;
  663. int32_t toolMajor;
  664. int32_t toolMinor;
  665. int32_t orientation;
  666. int32_t distance;
  667. int32_t tiltX;
  668. int32_t tiltY;
  669. int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
  670. bool isHovering;
  671. };
  672. uint32_t pointerCount;
  673. Pointer pointers[MAX_POINTERS];
  674. BitSet32 hoveringIdBits, touchingIdBits;
  675. uint32_t idToIndex[MAX_POINTER_ID + 1];
  676. RawPointerData();
  677. void clear();
  678. void copyFrom(const RawPointerData& other);
  679. void getCentroidOfTouchingPointers(float* outX, float* outY) const;
  680. inline void markIdBit(uint32_t id, bool isHovering) {
  681. if (isHovering) {
  682. hoveringIdBits.markBit(id);
  683. } else {
  684. touchingIdBits.markBit(id);
  685. }
  686. }
  687. inline void clearIdBits() {
  688. hoveringIdBits.clear();
  689. touchingIdBits.clear();
  690. }
  691. inline const Pointer& pointerForId(uint32_t id) const {
  692. return pointers[idToIndex[id]];
  693. }
  694. inline bool isHovering(uint32_t pointerIndex) {
  695. return pointers[pointerIndex].isHovering;
  696. }
  697. };
  698. /* Cooked data for a collection of pointers including a pointer id mapping table. */
  699. struct CookedPointerData {
  700. uint32_t pointerCount;
  701. PointerProperties pointerProperties[MAX_POINTERS];
  702. PointerCoords pointerCoords[MAX_POINTERS];
  703. BitSet32 hoveringIdBits, touchingIdBits;
  704. uint32_t idToIndex[MAX_POINTER_ID + 1];
  705. CookedPointerData();
  706. void clear();
  707. void copyFrom(const CookedPointerData& other);
  708. inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
  709. return pointerCoords[idToIndex[id]];
  710. }
  711. inline PointerCoords& editPointerCoordsWithId(uint32_t id) {
  712. return pointerCoords[idToIndex[id]];
  713. }
  714. inline PointerProperties& editPointerPropertiesWithId(uint32_t id) {
  715. return pointerProperties[idToIndex[id]];
  716. }
  717. inline bool isHovering(uint32_t pointerIndex) const {
  718. return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
  719. }
  720. inline bool isTouching(uint32_t pointerIndex) const {
  721. return touchingIdBits.hasBit(pointerProperties[pointerIndex].id);
  722. }
  723. };
  724. /* Keeps track of the state of single-touch protocol. */
  725. class SingleTouchMotionAccumulator {
  726. public:
  727. SingleTouchMotionAccumulator();
  728. void process(const RawEvent* rawEvent);
  729. void reset(InputDevice* device);
  730. inline int32_t getAbsoluteX() const { return mAbsX; }
  731. inline int32_t getAbsoluteY() const { return mAbsY; }
  732. inline int32_t getAbsolutePressure() const { return mAbsPressure; }
  733. inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
  734. inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
  735. inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
  736. inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
  737. private:
  738. int32_t mAbsX;
  739. int32_t mAbsY;
  740. int32_t mAbsPressure;
  741. int32_t mAbsToolWidth;
  742. int32_t mAbsDistance;
  743. int32_t mAbsTiltX;
  744. int32_t mAbsTiltY;
  745. void clearAbsoluteAxes();
  746. };
  747. /* Keeps track of the state of multi-touch protocol. */
  748. class MultiTouchMotionAccumulator {
  749. public:
  750. class Slot {
  751. public:
  752. inline bool isInUse() const { return mInUse; }
  753. inline int32_t getX() const { return mAbsMTPositionX; }
  754. inline int32_t getY() const { return mAbsMTPositionY; }
  755. inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
  756. inline int32_t getTouchMinor() const {
  757. return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
  758. inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
  759. inline int32_t getToolMinor() const {
  760. return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
  761. inline int32_t getOrientation() const { return mAbsMTOrientation; }
  762. inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
  763. inline int32_t getPressure() const { return mAbsMTPressure; }
  764. inline int32_t getDistance() const { return mAbsMTDistance; }
  765. inline int32_t getToolType() const;
  766. private:
  767. friend class MultiTouchMotionAccumulator;
  768. bool mInUse;
  769. bool mHaveAbsMTTouchMinor;
  770. bool mHaveAbsMTWidthMinor;
  771. bool mHaveAbsMTToolType;
  772. int32_t mAbsMTPositionX;
  773. int32_t mAbsMTPositionY;
  774. int32_t mAbsMTTouchMajor;
  775. int32_t mAbsMTTouchMinor;
  776. int32_t mAbsMTWidthMajor;
  777. int32_t mAbsMTWidthMinor;
  778. int32_t mAbsMTOrientation;
  779. int32_t mAbsMTTrackingId;
  780. int32_t mAbsMTPressure;
  781. int32_t mAbsMTDistance;
  782. int32_t mAbsMTToolType;
  783. Slot();
  784. void clear();
  785. };
  786. MultiTouchMotionAccumulator();
  787. ~MultiTouchMotionAccumulator();
  788. void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
  789. void reset(InputDevice* device);
  790. void process(const RawEvent* rawEvent);
  791. void finishSync();
  792. bool hasStylus() const;
  793. inline size_t getSlotCount() const { return mSlotCount; }
  794. inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
  795. private:
  796. int32_t mCurrentSlot;
  797. Slot* mSlots;
  798. size_t mSlotCount;
  799. bool mUsingSlotsProtocol;
  800. bool mHaveStylus;
  801. void clearSlots(int32_t initialSlot);
  802. };
  803. /* An input mapper transforms raw input events into cooked event data.
  804. * A single input device can have multiple associated input mappers in order to interpret
  805. * different classes of events.
  806. *
  807. * InputMapper lifecycle:
  808. * - create
  809. * - configure with 0 changes
  810. * - reset
  811. * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
  812. * - reset
  813. * - destroy
  814. */
  815. class InputMapper {
  816. public:
  817. InputMapper(InputDevice* device);
  818. virtual ~InputMapper();
  819. inline InputDevice* getDevice() { return mDevice; }
  820. inline int32_t getDeviceId() { return mDevice->getId(); }
  821. inline const String8 getDeviceName() { return mDevice->getName(); }
  822. inline InputReaderContext* getContext() { return mContext; }
  823. inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
  824. inline InputListenerInterface* getListener() { return mContext->getListener(); }
  825. inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
  826. virtual uint32_t getSources() = 0;
  827. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  828. virtual void dump(String8& dump);
  829. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  830. virtual void reset(nsecs_t when);
  831. virtual void process(const RawEvent* rawEvent) = 0;
  832. virtual void timeoutExpired(nsecs_t when);
  833. virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
  834. virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
  835. virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
  836. virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  837. const int32_t* keyCodes, uint8_t* outFlags);
  838. virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
  839. int32_t token);
  840. virtual void cancelVibrate(int32_t token);
  841. virtual void cancelTouch(nsecs_t when);
  842. virtual int32_t getMetaState();
  843. virtual void updateExternalStylusState(const StylusState& state);
  844. virtual void fadePointer();
  845. protected:
  846. InputDevice* mDevice;
  847. InputReaderContext* mContext;
  848. status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
  849. void bumpGeneration();
  850. static void dumpRawAbsoluteAxisInfo(String8& dump,
  851. const RawAbsoluteAxisInfo& axis, const char* name);
  852. static void dumpStylusState(String8& dump, const StylusState& state);
  853. };
  854. class SwitchInputMapper : public InputMapper {
  855. public:
  856. SwitchInputMapper(InputDevice* device);
  857. virtual ~SwitchInputMapper();
  858. virtual uint32_t getSources();
  859. virtual void process(const RawEvent* rawEvent);
  860. virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
  861. virtual void dump(String8& dump);
  862. private:
  863. uint32_t mSwitchValues;
  864. uint32_t mUpdatedSwitchMask;
  865. void processSwitch(int32_t switchCode, int32_t switchValue);
  866. void sync(nsecs_t when);
  867. };
  868. class VibratorInputMapper : public InputMapper {
  869. public:
  870. VibratorInputMapper(InputDevice* device);
  871. virtual ~VibratorInputMapper();
  872. virtual uint32_t getSources();
  873. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  874. virtual void process(const RawEvent* rawEvent);
  875. virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
  876. int32_t token);
  877. virtual void cancelVibrate(int32_t token);
  878. virtual void timeoutExpired(nsecs_t when);
  879. virtual void dump(String8& dump);
  880. private:
  881. bool mVibrating;
  882. nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
  883. size_t mPatternSize;
  884. ssize_t mRepeat;
  885. int32_t mToken;
  886. ssize_t mIndex;
  887. nsecs_t mNextStepTime;
  888. void nextStep();
  889. void stopVibrating();
  890. };
  891. class KeyboardInputMapper : public InputMapper {
  892. public:
  893. KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
  894. virtual ~KeyboardInputMapper();
  895. virtual uint32_t getSources();
  896. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  897. virtual void dump(String8& dump);
  898. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  899. virtual void reset(nsecs_t when);
  900. virtual void process(const RawEvent* rawEvent);
  901. virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
  902. virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
  903. virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  904. const int32_t* keyCodes, uint8_t* outFlags);
  905. virtual int32_t getMetaState();
  906. private:
  907. struct KeyDown {
  908. int32_t keyCode;
  909. int32_t scanCode;
  910. };
  911. uint32_t mSource;
  912. int32_t mKeyboardType;
  913. int32_t mRotationMapOffset; // determines if and how volume keys rotate
  914. int32_t mOrientation; // orientation for dpad and volume keys
  915. Vector<KeyDown> mKeyDowns; // keys that are down
  916. int32_t mMetaState;
  917. nsecs_t mDownTime; // time of most recent key down
  918. int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
  919. struct LedState {
  920. bool avail; // led is available
  921. bool on; // we think the led is currently on
  922. };
  923. LedState mCapsLockLedState;
  924. LedState mNumLockLedState;
  925. LedState mScrollLockLedState;
  926. // Immutable configuration parameters.
  927. struct Parameters {
  928. bool hasAssociatedDisplay;
  929. bool orientationAware;
  930. bool handlesKeyRepeat;
  931. } mParameters;
  932. void configureParameters();
  933. void dumpParameters(String8& dump);
  934. bool isKeyboardOrGamepadKey(int32_t scanCode);
  935. void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode);
  936. ssize_t findKeyDown(int32_t scanCode);
  937. void resetLedState();
  938. void initializeLedState(LedState& ledState, int32_t led);
  939. void updateLedState(bool reset);
  940. void updateLedStateForModifier(LedState& ledState, int32_t led,
  941. int32_t modifier, bool reset);
  942. };
  943. class CursorInputMapper : public InputMapper {
  944. public:
  945. CursorInputMapper(InputDevice* device);
  946. virtual ~CursorInputMapper();
  947. virtual uint32_t getSources();
  948. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  949. virtual void dump(String8& dump);
  950. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  951. virtual void reset(nsecs_t when);
  952. virtual void process(const RawEvent* rawEvent);
  953. virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
  954. virtual void fadePointer();
  955. private:
  956. // Amount that trackball needs to move in order to generate a key event.
  957. static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
  958. // Immutable configuration parameters.
  959. struct Parameters {
  960. enum Mode {
  961. MODE_POINTER,
  962. MODE_NAVIGATION,
  963. };
  964. Mode mode;
  965. bool hasAssociatedDisplay;
  966. bool orientationAware;
  967. } mParameters;
  968. CursorButtonAccumulator mCursorButtonAccumulator;
  969. CursorMotionAccumulator mCursorMotionAccumulator;
  970. CursorScrollAccumulator mCursorScrollAccumulator;
  971. int32_t mSource;
  972. float mXScale;
  973. float mYScale;
  974. float mXPrecision;
  975. float mYPrecision;
  976. float mVWheelScale;
  977. float mHWheelScale;
  978. // Velocity controls for mouse pointer and wheel movements.
  979. // The controls for X and Y wheel movements are separate to keep them decoupled.
  980. VelocityControl mPointerVelocityControl;
  981. VelocityControl mWheelXVelocityControl;
  982. VelocityControl mWheelYVelocityControl;
  983. int32_t mOrientation;
  984. sp<PointerControllerInterface> mPointerController;
  985. int32_t mButtonState;
  986. nsecs_t mDownTime;
  987. void configureParameters();
  988. void dumpParameters(String8& dump);
  989. void sync(nsecs_t when);
  990. };
  991. class TouchInputMapper : public InputMapper {
  992. public:
  993. TouchInputMapper(InputDevice* device);
  994. virtual ~TouchInputMapper();
  995. virtual uint32_t getSources();
  996. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  997. virtual void dump(String8& dump);
  998. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  999. virtual void reset(nsecs_t when);
  1000. virtual void process(const RawEvent* rawEvent);
  1001. virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
  1002. virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
  1003. virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  1004. const int32_t* keyCodes, uint8_t* outFlags);
  1005. virtual void fadePointer();
  1006. virtual void cancelTouch(nsecs_t when);
  1007. virtual void timeoutExpired(nsecs_t when);
  1008. virtual void updateExternalStylusState(const StylusState& state);
  1009. protected:
  1010. CursorButtonAccumulator mCursorButtonAccumulator;
  1011. CursorScrollAccumulator mCursorScrollAccumulator;
  1012. TouchButtonAccumulator mTouchButtonAccumulator;
  1013. struct VirtualKey {
  1014. int32_t keyCode;
  1015. int32_t scanCode;
  1016. uint32_t flags;
  1017. // computed hit box, specified in touch screen coords based on known display size
  1018. int32_t hitLeft;
  1019. int32_t hitTop;
  1020. int32_t hitRight;
  1021. int32_t hitBottom;
  1022. inline bool isHit(int32_t x, int32_t y) const {
  1023. return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
  1024. }
  1025. };
  1026. // Input sources and device mode.
  1027. uint32_t mSource;
  1028. enum DeviceMode {
  1029. DEVICE_MODE_DISABLED, // input is disabled
  1030. DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
  1031. DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
  1032. DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
  1033. DEVICE_MODE_POINTER, // pointer mapping (pointer)
  1034. };
  1035. DeviceMode mDeviceMode;
  1036. // The reader's configuration.
  1037. InputReaderConfiguration mConfig;
  1038. // Immutable configuration parameters.
  1039. struct Parameters {
  1040. enum DeviceType {
  1041. DEVICE_TYPE_TOUCH_SCREEN,
  1042. DEVICE_TYPE_TOUCH_PAD,
  1043. DEVICE_TYPE_TOUCH_NAVIGATION,
  1044. DEVICE_TYPE_GESTURE_SENSOR,
  1045. DEVICE_TYPE_POINTER,
  1046. };
  1047. DeviceType deviceType;
  1048. bool hasAssociatedDisplay;
  1049. bool associatedDisplayIsExternal;
  1050. bool orientationAware;
  1051. bool hasButtonUnderPad;
  1052. enum GestureMode {
  1053. GESTURE_MODE_POINTER,
  1054. GESTURE_MODE_SPOTS,
  1055. };
  1056. GestureMode gestureMode;
  1057. bool wake;
  1058. } mParameters;
  1059. // Immutable calibration parameters in parsed form.
  1060. struct Calibration {
  1061. // Size
  1062. enum SizeCalibration {
  1063. SIZE_CALIBRATION_DEFAULT,
  1064. SIZE_CALIBRATION_NONE,
  1065. SIZE_CALIBRATION_GEOMETRIC,
  1066. SIZE_CALIBRATION_DIAMETER,
  1067. SIZE_CALIBRATION_BOX,
  1068. SIZE_CALIBRATION_AREA,
  1069. };
  1070. SizeCalibration sizeCalibration;
  1071. bool haveSizeScale;
  1072. float sizeScale;
  1073. bool haveSizeBias;
  1074. float sizeBias;
  1075. bool haveSizeIsSummed;
  1076. bool sizeIsSummed;
  1077. // Pressure
  1078. enum PressureCalibration {
  1079. PRESSURE_CALIBRATION_DEFAULT,
  1080. PRESSURE_CALIBRATION_NONE,
  1081. PRESSURE_CALIBRATION_PHYSICAL,
  1082. PRESSURE_CALIBRATION_AMPLITUDE,
  1083. };
  1084. PressureCalibration pressureCalibration;
  1085. bool havePressureScale;
  1086. float pressureScale;
  1087. // Orientation
  1088. enum OrientationCalibration {
  1089. ORIENTATION_CALIBRATION_DEFAULT,
  1090. ORIENTATION_CALIBRATION_NONE,
  1091. ORIENTATION_CALIBRATION_INTERPOLATED,
  1092. ORIENTATION_CALIBRATION_VECTOR,
  1093. };
  1094. OrientationCalibration orientationCalibration;
  1095. // Distance
  1096. enum DistanceCalibration {
  1097. DISTANCE_CALIBRATION_DEFAULT,
  1098. DISTANCE_CALIBRATION_NONE,
  1099. DISTANCE_CALIBRATION_SCALED,
  1100. };
  1101. DistanceCalibration distanceCalibration;
  1102. bool haveDistanceScale;
  1103. float distanceScale;
  1104. enum CoverageCalibration {
  1105. COVERAGE_CALIBRATION_DEFAULT,
  1106. COVERAGE_CALIBRATION_NONE,
  1107. COVERAGE_CALIBRATION_BOX,
  1108. };
  1109. CoverageCalibration coverageCalibration;
  1110. inline void applySizeScaleAndBias(float* outSize) const {
  1111. if (haveSizeScale) {
  1112. *outSize *= sizeScale;
  1113. }
  1114. if (haveSizeBias) {
  1115. *outSize += sizeBias;
  1116. }
  1117. if (*outSize < 0) {
  1118. *outSize = 0;
  1119. }
  1120. }
  1121. } mCalibration;
  1122. // Affine location transformation/calibration
  1123. struct TouchAffineTransformation mAffineTransform;
  1124. RawPointerAxes mRawPointerAxes;
  1125. struct RawState {
  1126. nsecs_t when;
  1127. // Raw pointer sample data.
  1128. RawPointerData rawPointerData;
  1129. int32_t buttonState;
  1130. // Scroll state.
  1131. int32_t rawVScroll;
  1132. int32_t rawHScroll;
  1133. void copyFrom(const RawState& other) {
  1134. when = other.when;
  1135. rawPointerData.copyFrom(other.rawPointerData);
  1136. buttonState = other.buttonState;
  1137. rawVScroll = other.rawVScroll;
  1138. rawHScroll = other.rawHScroll;
  1139. }
  1140. void clear() {
  1141. when = 0;
  1142. rawPointerData.clear();
  1143. buttonState = 0;
  1144. rawVScroll = 0;
  1145. rawHScroll = 0;
  1146. }
  1147. };
  1148. struct CookedState {
  1149. // Cooked pointer sample data.
  1150. CookedPointerData cookedPointerData;
  1151. // Id bits used to differentiate fingers, stylus and mouse tools.
  1152. BitSet32 fingerIdBits;
  1153. BitSet32 stylusIdBits;
  1154. BitSet32 mouseIdBits;
  1155. int32_t buttonState;
  1156. void copyFrom(const CookedState& other) {
  1157. cookedPointerData.copyFrom(other.cookedPointerData);
  1158. fingerIdBits = other.fingerIdBits;
  1159. stylusIdBits = other.stylusIdBits;
  1160. mouseIdBits = other.mouseIdBits;
  1161. buttonState = other.buttonState;
  1162. }
  1163. void clear() {
  1164. cookedPointerData.clear();
  1165. fingerIdBits.clear();
  1166. stylusIdBits.clear();
  1167. mouseIdBits.clear();
  1168. buttonState = 0;
  1169. }
  1170. };
  1171. Vector<RawState> mRawStatesPending;
  1172. RawState mCurrentRawState;
  1173. CookedState mCurrentCookedState;
  1174. RawState mLastRawState;
  1175. CookedState mLastCookedState;
  1176. // State provided by an external stylus
  1177. StylusState mExternalStylusState;
  1178. int64_t mExternalStylusId;
  1179. nsecs_t mExternalStylusFusionTimeout;
  1180. bool mExternalStylusDataPending;
  1181. // True if we sent a HOVER_ENTER event.
  1182. bool mSentHoverEnter;
  1183. // Have we assigned pointer IDs for this stream
  1184. bool mHavePointerIds;
  1185. // Is the current stream of direct touch events aborted
  1186. bool mCurrentMotionAborted;
  1187. // The time the primary pointer last went down.
  1188. nsecs_t mDownTime;
  1189. // The pointer controller, or null if the device is not a pointer.
  1190. sp<PointerControllerInterface> mPointerController;
  1191. Vector<VirtualKey> mVirtualKeys;
  1192. virtual void configureParameters();
  1193. virtual void dumpParameters(String8& dump);
  1194. virtual void configureRawPointerAxes();
  1195. virtual void dumpRawPointerAxes(String8& dump);
  1196. virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
  1197. virtual void dumpSurface(String8& dump);
  1198. virtual void configureVirtualKeys();
  1199. virtual void dumpVirtualKeys(String8& dump);
  1200. virtual void parseCalibration();
  1201. virtual void resolveCalibration();
  1202. virtual void dumpCalibration(String8& dump);
  1203. virtual void updateAffineTransformation();
  1204. virtual void dumpAffineTransformation(String8& dump);
  1205. virtual void resolveExternalStylusPresence();
  1206. virtual bool hasStylus() const = 0;
  1207. virtual bool hasExternalStylus() const;
  1208. virtual void syncTouch(nsecs_t when, RawState* outState) = 0;
  1209. private:
  1210. // The current viewport.
  1211. // The components of the viewport are specified in the display's rotated orientation.
  1212. DisplayViewport mViewport;
  1213. // The surface orientation, width and height set by configureSurface().
  1214. // The width and height are derived from the viewport but are specified
  1215. // in the natural orientation.
  1216. // The surface origin specifies how the surface coordinates should be translated
  1217. // to align with the logical display coordinate space.
  1218. // The orientation may be different from the viewport orientation as it specifies
  1219. // the rotation of the surface coordinates required to produce the viewport's
  1220. // requested orientation, so it will depend on whether the device is orientation aware.
  1221. int32_t mSurfaceWidth;
  1222. int32_t mSurfaceHeight;
  1223. int32_t mSurfaceLeft;
  1224. int32_t mSurfaceTop;
  1225. int32_t mSurfaceOrientation;
  1226. // Translation and scaling factors, orientation-independent.
  1227. float mXTranslate;
  1228. float mXScale;
  1229. float mXPrecision;
  1230. float mYTranslate;
  1231. float mYScale;
  1232. float mYPrecision;
  1233. float mGeometricScale;
  1234. float mPressureScale;
  1235. float mSizeScale;
  1236. float mOrientationScale;
  1237. float mDistanceScale;
  1238. bool mHaveTilt;
  1239. float mTiltXCenter;
  1240. float mTiltXScale;
  1241. float mTiltYCenter;
  1242. float mTiltYScale;
  1243. bool mExternalStylusConnected;
  1244. // Oriented motion ranges for input device info.
  1245. struct OrientedRanges {
  1246. InputDeviceInfo::MotionRange x;
  1247. InputDeviceInfo::MotionRange y;
  1248. InputDeviceInfo::MotionRange pressure;
  1249. bool haveSize;
  1250. InputDeviceInfo::MotionRange size;
  1251. bool haveTouchSize;
  1252. InputDeviceInfo::MotionRange touchMajor;
  1253. InputDeviceInfo::MotionRange touchMinor;
  1254. bool haveToolSize;
  1255. InputDeviceInfo::MotionRange toolMajor;
  1256. InputDeviceInfo::MotionRange toolMinor;
  1257. bool haveOrientation;
  1258. InputDeviceInfo::MotionRange orientation;
  1259. bool haveDistance;
  1260. InputDeviceInfo::MotionRange distance;
  1261. bool haveTilt;
  1262. InputDeviceInfo::MotionRange tilt;
  1263. OrientedRanges() {
  1264. clear();
  1265. }
  1266. void clear() {
  1267. haveSize = false;
  1268. haveTouchSize = false;
  1269. haveToolSize = false;
  1270. haveOrientation = false;
  1271. haveDistance = false;
  1272. haveTilt = false;
  1273. }
  1274. } mOrientedRanges;
  1275. // Oriented dimensions and precision.
  1276. float mOrientedXPrecision;
  1277. float mOrientedYPrecision;
  1278. struct CurrentVirtualKeyState {
  1279. bool down;
  1280. bool ignored;
  1281. nsecs_t downTime;
  1282. int32_t keyCode;
  1283. int32_t scanCode;
  1284. } mCurrentVirtualKey;
  1285. // Scale factor for gesture or mouse based pointer movements.
  1286. float mPointerXMovementScale;
  1287. float mPointerYMovementScale;
  1288. // Scale factor for gesture based zooming and other freeform motions.
  1289. float mPointerXZoomScale;
  1290. float mPointerYZoomScale;
  1291. // The maximum swipe width.
  1292. float mPointerGestureMaxSwipeWidth;
  1293. struct PointerDistanceHeapElement {
  1294. uint32_t currentPointerIndex : 8;
  1295. uint32_t lastPointerIndex : 8;
  1296. uint64_t distance : 48; // squared distance
  1297. };
  1298. enum PointerUsage {
  1299. POINTER_USAGE_NONE,
  1300. POINTER_USAGE_GESTURES,
  1301. POINTER_USAGE_STYLUS,
  1302. POINTER_USAGE_MOUSE,
  1303. };
  1304. PointerUsage mPointerUsage;
  1305. struct PointerGesture {
  1306. enum Mode {
  1307. // No fingers, button is not pressed.
  1308. // Nothing happening.
  1309. NEUTRAL,
  1310. // No fingers, button is not pressed.
  1311. // Tap detected.
  1312. // Emits DOWN and UP events at the pointer location.
  1313. TAP,
  1314. // Exactly one finger dragging following a tap.
  1315. // Pointer follows the active finger.
  1316. // Emits DOWN, MOVE and UP events at the pointer location.
  1317. //
  1318. // Detect double-taps when the finger goes up while in TAP_DRAG mode.
  1319. TAP_DRAG,
  1320. // Button is pressed.
  1321. // Pointer follows the active finger if there is one. Other fingers are ignored.
  1322. // Emits DOWN, MOVE and UP events at the pointer location.
  1323. BUTTON_CLICK_OR_DRAG,
  1324. // Exactly one finger, button is not pressed.
  1325. // Pointer follows the active finger.
  1326. // Emits HOVER_MOVE events at the pointer location.
  1327. //
  1328. // Detect taps when the finger goes up while in HOVER mode.
  1329. HOVER,
  1330. // Exactly two fingers but neither have moved enough to clearly indicate
  1331. // whether a swipe or freeform gesture was intended. We consider the
  1332. // pointer to be pressed so this enables clicking or long-pressing on buttons.
  1333. // Pointer does not move.
  1334. // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
  1335. PRESS,
  1336. // Exactly two fingers moving in the same direction, button is not pressed.
  1337. // Pointer does not move.
  1338. // Emits DOWN, MOVE and UP events with a single pointer coordinate that
  1339. // follows the midpoint between both fingers.
  1340. SWIPE,
  1341. // Two or more fingers moving in arbitrary directions, button is not pressed.
  1342. // Pointer does not move.
  1343. // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
  1344. // each finger individually relative to the initial centroid of the finger.
  1345. FREEFORM,
  1346. // Waiting for quiet time to end before starting the next gesture.
  1347. QUIET,
  1348. };
  1349. // Time the first finger went down.
  1350. nsecs_t firstTouchTime;
  1351. // The active pointer id from the raw touch data.
  1352. int32_t activeTouchId; // -1 if none
  1353. // The active pointer id from the gesture last delivered to the application.
  1354. int32_t activeGestureId; // -1 if none
  1355. // Pointer coords and ids for the current and previous pointer gesture.
  1356. Mode currentGestureMode;
  1357. BitSet32 currentGestureIdBits;
  1358. uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
  1359. PointerProperties currentGestureProperties[MAX_POINTERS];
  1360. PointerCoords currentGestureCoords[MAX_POINTERS];
  1361. Mode lastGestureMode;
  1362. BitSet32 lastGestureIdBits;
  1363. uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
  1364. PointerProperties lastGestureProperties[MAX_POINTERS];
  1365. PointerCoords lastGestureCoords[MAX_POINTERS];
  1366. // Time the pointer gesture last went down.
  1367. nsecs_t downTime;
  1368. // Time when the pointer went down for a TAP.
  1369. nsecs_t tapDownTime;
  1370. // Time when the pointer went up for a TAP.
  1371. nsecs_t tapUpTime;
  1372. // Location of initial tap.
  1373. float tapX, tapY;
  1374. // Time we started waiting for quiescence.
  1375. nsecs_t quietTime;
  1376. // Reference points for multitouch gestures.
  1377. float referenceTouchX; // reference touch X/Y coordinates in surface units
  1378. float referenceTouchY;
  1379. float referenceGestureX; // reference gesture X/Y coordinates in pixels
  1380. float referenceGestureY;
  1381. // Distance that each pointer has traveled which has not yet been
  1382. // subsumed into the reference gesture position.
  1383. BitSet32 referenceIdBits;
  1384. struct Delta {
  1385. float dx, dy;
  1386. };
  1387. Delta referenceDeltas[MAX_POINTER_ID + 1];
  1388. // Describes how touch ids are mapped to gesture ids for freeform gestures.
  1389. uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
  1390. // A velocity tracker for determining whether to switch active pointers during drags.
  1391. VelocityTracker velocityTracker;
  1392. void reset() {
  1393. firstTouchTime = LLONG_MIN;
  1394. activeTouchId = -1;
  1395. activeGestureId = -1;
  1396. currentGestureMode = NEUTRAL;
  1397. currentGestureIdBits.clear();
  1398. lastGestureMode = NEUTRAL;
  1399. lastGestureIdBits.clear();
  1400. downTime = 0;
  1401. velocityTracker.clear();
  1402. resetTap();
  1403. resetQuietTime();
  1404. }
  1405. void resetTap() {
  1406. tapDownTime = LLONG_MIN;
  1407. tapUpTime = LLONG_MIN;
  1408. }
  1409. void resetQuietTime() {
  1410. quietTime = LLONG_MIN;
  1411. }
  1412. } mPointerGesture;
  1413. struct PointerSimple {
  1414. PointerCoords currentCoords;
  1415. PointerProperties currentProperties;
  1416. PointerCoords lastCoords;
  1417. PointerProperties lastProperties;
  1418. // True if the pointer is down.
  1419. bool down;
  1420. // True if the pointer is hovering.
  1421. bool hovering;
  1422. // Time the pointer last went down.
  1423. nsecs_t downTime;
  1424. void reset() {
  1425. currentCoords.clear();
  1426. currentProperties.clear();
  1427. lastCoords.clear();
  1428. lastProperties.clear();
  1429. down = false;
  1430. hovering = false;
  1431. downTime = 0;
  1432. }
  1433. } mPointerSimple;
  1434. // The pointer and scroll velocity controls.
  1435. VelocityControl mPointerVelocityControl;
  1436. VelocityControl mWheelXVelocityControl;
  1437. VelocityControl mWheelYVelocityControl;
  1438. // The time the stylus event was processed by any TouchInputMapper
  1439. static nsecs_t mLastStylusTime;
  1440. void resetExternalStylus();
  1441. void clearStylusDataPendingFlags();
  1442. void sync(nsecs_t when);
  1443. bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
  1444. void processRawTouches(bool timeout);
  1445. void cookAndDispatch(nsecs_t when);
  1446. void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
  1447. int32_t keyEventAction, int32_t keyEventFlags);
  1448. void dispatchTouches(nsecs_t when, uint32_t policyFlags);
  1449. void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
  1450. void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
  1451. void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags);
  1452. void dispatchButtonPress(nsecs_t when, uint32_t policyFlags);
  1453. const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData);
  1454. void cookPointerData();
  1455. void abortTouches(nsecs_t when, uint32_t policyFlags);
  1456. void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
  1457. void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
  1458. void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
  1459. void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
  1460. bool preparePointerGestures(nsecs_t when,
  1461. bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
  1462. bool isTimeout);
  1463. void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
  1464. void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
  1465. void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
  1466. void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
  1467. void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
  1468. bool down, bool hovering);
  1469. void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
  1470. bool assignExternalStylusId(const RawState& state, bool timeout);
  1471. void applyExternalStylusButtonState(nsecs_t when);
  1472. void applyExternalStylusTouchState(nsecs_t when);
  1473. // Dispatches a motion event.
  1474. // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
  1475. // method will take care of setting the index and transmuting the action to DOWN or UP
  1476. // it is the first / last pointer to go down / up.
  1477. void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
  1478. int32_t action, int32_t actionButton,
  1479. int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
  1480. const PointerProperties* properties, const PointerCoords* coords,
  1481. const uint32_t* idToIndex, BitSet32 idBits,
  1482. int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
  1483. // Updates pointer coords and properties for pointers with specified ids that have moved.
  1484. // Returns true if any of them changed.
  1485. bool updateMovedPointers(const PointerProperties* inProperties,
  1486. const PointerCoords* inCoords, const uint32_t* inIdToIndex,
  1487. PointerProperties* outProperties, PointerCoords* outCoords,
  1488. const uint32_t* outIdToIndex, BitSet32 idBits) const;
  1489. bool isPointInsideSurface(int32_t x, int32_t y);
  1490. const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
  1491. static void assignPointerIds(const RawState* last, RawState* current);
  1492. void unfadePointer(PointerControllerInterface::Transition transition);
  1493. bool rejectPalm(nsecs_t when);
  1494. };
  1495. class SingleTouchInputMapper : public TouchInputMapper {
  1496. public:
  1497. SingleTouchInputMapper(InputDevice* device);
  1498. virtual ~SingleTouchInputMapper();
  1499. virtual void reset(nsecs_t when);
  1500. virtual void process(const RawEvent* rawEvent);
  1501. protected:
  1502. virtual void syncTouch(nsecs_t when, RawState* outState);
  1503. virtual void configureRawPointerAxes();
  1504. virtual bool hasStylus() const;
  1505. private:
  1506. SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
  1507. };
  1508. class MultiTouchInputMapper : public TouchInputMapper {
  1509. public:
  1510. MultiTouchInputMapper(InputDevice* device);
  1511. virtual ~MultiTouchInputMapper();
  1512. virtual void reset(nsecs_t when);
  1513. virtual void process(const RawEvent* rawEvent);
  1514. protected:
  1515. virtual void syncTouch(nsecs_t when, RawState* outState);
  1516. virtual void configureRawPointerAxes();
  1517. virtual bool hasStylus() const;
  1518. private:
  1519. MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
  1520. // Specifies the pointer id bits that are in use, and their associated tracking id.
  1521. BitSet32 mPointerIdBits;
  1522. int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
  1523. };
  1524. class ExternalStylusInputMapper : public InputMapper {
  1525. public:
  1526. ExternalStylusInputMapper(InputDevice* device);
  1527. virtual ~ExternalStylusInputMapper() = default;
  1528. virtual uint32_t getSources();
  1529. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  1530. virtual void dump(String8& dump);
  1531. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  1532. virtual void reset(nsecs_t when);
  1533. virtual void process(const RawEvent* rawEvent);
  1534. virtual void sync(nsecs_t when);
  1535. private:
  1536. SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
  1537. RawAbsoluteAxisInfo mRawPressureAxis;
  1538. TouchButtonAccumulator mTouchButtonAccumulator;
  1539. StylusState mStylusState;
  1540. };
  1541. class JoystickInputMapper : public InputMapper {
  1542. public:
  1543. JoystickInputMapper(InputDevice* device);
  1544. virtual ~JoystickInputMapper();
  1545. virtual uint32_t getSources();
  1546. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
  1547. virtual void dump(String8& dump);
  1548. virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
  1549. virtual void reset(nsecs_t when);
  1550. virtual void process(const RawEvent* rawEvent);
  1551. private:
  1552. struct Axis {
  1553. RawAbsoluteAxisInfo rawAxisInfo;
  1554. AxisInfo axisInfo;
  1555. bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
  1556. float scale; // scale factor from raw to normalized values
  1557. float offset; // offset to add after scaling for normalization
  1558. float highScale; // scale factor from raw to normalized values of high split
  1559. float highOffset; // offset to add after scaling for normalization of high split
  1560. float min; // normalized inclusive minimum
  1561. float max; // normalized inclusive maximum
  1562. float flat; // normalized flat region size
  1563. float fuzz; // normalized error tolerance
  1564. float resolution; // normalized resolution in units/mm
  1565. float filter; // filter out small variations of this size
  1566. float currentValue; // current value
  1567. float newValue; // most recent value
  1568. float highCurrentValue; // current value of high split
  1569. float highNewValue; // most recent value of high split
  1570. void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
  1571. bool explicitlyMapped, float scale, float offset,
  1572. float highScale, float highOffset,
  1573. float min, float max, float flat, float fuzz, float resolution) {
  1574. this->rawAxisInfo = rawAxisInfo;
  1575. this->axisInfo = axisInfo;
  1576. this->explicitlyMapped = explicitlyMapped;
  1577. this->scale = scale;
  1578. this->offset = offset;
  1579. this->highScale = highScale;
  1580. this->highOffset = highOffset;
  1581. this->min = min;
  1582. this->max = max;
  1583. this->flat = flat;
  1584. this->fuzz = fuzz;
  1585. this->resolution = resolution;
  1586. this->filter = 0;
  1587. resetValue();
  1588. }
  1589. void resetValue() {
  1590. this->currentValue = 0;
  1591. this->newValue = 0;
  1592. this->highCurrentValue = 0;
  1593. this->highNewValue = 0;
  1594. }
  1595. };
  1596. // Axes indexed by raw ABS_* axis index.
  1597. KeyedVector<int32_t, Axis> mAxes;
  1598. void sync(nsecs_t when, bool force);
  1599. bool haveAxis(int32_t axisId);
  1600. void pruneAxes(bool ignoreExplicitlyMappedAxes);
  1601. bool filterAxes(bool force);
  1602. static bool hasValueChangedSignificantly(float filter,
  1603. float newValue, float currentValue, float min, float max);
  1604. static bool hasMovedNearerToValueWithinFilteredRange(float filter,
  1605. float newValue, float currentValue, float thresholdValue);
  1606. static bool isCenteredAxis(int32_t axis);
  1607. static int32_t getCompatAxis(int32_t axis);
  1608. static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
  1609. static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
  1610. float value);
  1611. };
  1612. } // namespace android
  1613. #endif // _UI_INPUT_READER_H