blob: 617175bb3517fbc2a525faf2ac6fca2a3cfb9fac [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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
17#ifndef _LIBINPUT_INPUT_H
18#define _LIBINPUT_INPUT_H
19
20/**
21 * Native input event structures.
22 */
23
24#include <android/input.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070025#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/RefBase.h>
28#include <utils/String8.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070029#include <utils/Timers.h>
30#include <utils/Vector.h>
Nick Kralevich834ac202015-10-22 07:09:23 -070031#include <stdint.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
Jeff Brown5912f952013-07-01 19:10:31 -070033/*
34 * Additional private constants not defined in ndk/ui/input.h.
35 */
36enum {
37 /* Signifies that the key is being predispatched */
38 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
39
40 /* Private control to determine when an app is tracking a key sequence. */
41 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
42
43 /* Key event is inconsistent with previously sent key events. */
44 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
45};
46
47enum {
48 /* Motion event is inconsistent with previously sent motion events. */
49 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
50};
51
52enum {
53 /* Used when a motion event is not associated with any display.
54 * Typically used for non-pointer events. */
55 ADISPLAY_ID_NONE = -1,
56
57 /* The default display id. */
58 ADISPLAY_ID_DEFAULT = 0,
59};
60
61enum {
62 /*
63 * Indicates that an input device has switches.
64 * This input source flag is hidden from the API because switches are only used by the system
65 * and applications have no way to interact with them.
66 */
67 AINPUT_SOURCE_SWITCH = 0x80000000,
68};
69
Michael Wright962a1082013-10-17 17:35:53 -070070enum {
71 /**
72 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
73 * with LEDs to developers
74 *
Michael Wright872db4f2014-04-22 15:03:51 -070075 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -070076 */
77
78 ALED_NUM_LOCK = 0x00,
79 ALED_CAPS_LOCK = 0x01,
80 ALED_SCROLL_LOCK = 0x02,
81 ALED_COMPOSE = 0x03,
82 ALED_KANA = 0x04,
83 ALED_SLEEP = 0x05,
84 ALED_SUSPEND = 0x06,
85 ALED_MUTE = 0x07,
86 ALED_MISC = 0x08,
87 ALED_MAIL = 0x09,
88 ALED_CHARGING = 0x0a,
89 ALED_CONTROLLER_1 = 0x10,
90 ALED_CONTROLLER_2 = 0x11,
91 ALED_CONTROLLER_3 = 0x12,
92 ALED_CONTROLLER_4 = 0x13,
93};
94
Michael Wright9b04f862013-10-18 17:53:50 -070095/* Maximum number of controller LEDs we support */
96#define MAX_CONTROLLER_LEDS 4
97
Jeff Brown5912f952013-07-01 19:10:31 -070098/*
99 * SystemUiVisibility constants from View.
100 */
101enum {
102 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
103 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
104};
105
106/*
107 * Maximum number of pointers supported per motion event.
108 * Smallest number of pointers is 1.
109 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
110 * will occasionally emit 11. There is not much harm making this constant bigger.)
111 */
112#define MAX_POINTERS 16
113
114/*
Flanker5d178382015-09-07 15:28:58 +0800115 * Maximum number of samples supported per motion event.
116 */
117#define MAX_SAMPLES UINT16_MAX
118
119/*
Jeff Brown5912f952013-07-01 19:10:31 -0700120 * Maximum pointer id value supported in a motion event.
121 * Smallest pointer id is 0.
122 * (This is limited by our use of BitSet32 to track pointer assignments.)
123 */
124#define MAX_POINTER_ID 31
125
126/*
127 * Declare a concrete type for the NDK's input event forward declaration.
128 */
129struct AInputEvent {
130 virtual ~AInputEvent() { }
131};
132
133/*
134 * Declare a concrete type for the NDK's input device forward declaration.
135 */
136struct AInputDevice {
137 virtual ~AInputDevice() { }
138};
139
140
141namespace android {
142
143#ifdef HAVE_ANDROID_OS
144class Parcel;
145#endif
146
147/*
148 * Flags that flow alongside events in the input dispatch system to help with certain
149 * policy decisions such as waking from device sleep.
150 *
151 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
152 */
153enum {
154 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700155 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
156 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700157
Jeff Brownc9aa6282015-02-11 19:03:28 -0800158 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700159 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800160
161 // Indicates that the key is virtual, such as a capacitive button, and should
162 // generate haptic feedback. Virtual keys may be suppressed for some time
163 // after a recent touch to prevent accidental activation of virtual keys adjacent
164 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700165 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800166
167 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700168 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700169
Jeff Brownc9aa6282015-02-11 19:03:28 -0800170 // Indicates that the key represents a special gesture that has been detected by
171 // the touch firmware or driver. Causes touch events from the same device to be canceled.
172 POLICY_FLAG_GESTURE = 0x00000008,
173
Jeff Brown5912f952013-07-01 19:10:31 -0700174 POLICY_FLAG_RAW_MASK = 0x0000ffff,
175
176 /* These flags are set by the input dispatcher. */
177
178 // Indicates that the input event was injected.
179 POLICY_FLAG_INJECTED = 0x01000000,
180
181 // Indicates that the input event is from a trusted source such as a directly attached
182 // input device or an application with system-wide event injection permission.
183 POLICY_FLAG_TRUSTED = 0x02000000,
184
185 // Indicates that the input event has passed through an input filter.
186 POLICY_FLAG_FILTERED = 0x04000000,
187
188 // Disables automatic key repeating behavior.
189 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
190
191 /* These flags are set by the input reader policy as it intercepts each event. */
192
Jeff Browndb19e462014-04-08 19:55:38 -0700193 // Indicates that the device was in an interactive state when the
194 // event was intercepted.
195 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700196
197 // Indicates that the event should be dispatched to applications.
198 // The input event should still be sent to the InputDispatcher so that it can see all
199 // input events received include those that it will not deliver.
200 POLICY_FLAG_PASS_TO_USER = 0x40000000,
201};
202
203/*
204 * Pointer coordinate data.
205 */
206struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700207 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700208
209 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800210 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700211
212 // Values of axes that are stored in this structure packed in order by axis id
213 // for each axis that is present in the structure according to 'bits'.
214 float values[MAX_AXES];
215
216 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700217 BitSet64::clear(bits);
218 }
219
220 bool isEmpty() const {
221 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700222 }
223
224 float getAxisValue(int32_t axis) const;
225 status_t setAxisValue(int32_t axis, float value);
226
227 void scale(float scale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800228 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700229
230 inline float getX() const {
231 return getAxisValue(AMOTION_EVENT_AXIS_X);
232 }
233
234 inline float getY() const {
235 return getAxisValue(AMOTION_EVENT_AXIS_Y);
236 }
237
238#ifdef HAVE_ANDROID_OS
239 status_t readFromParcel(Parcel* parcel);
240 status_t writeToParcel(Parcel* parcel) const;
241#endif
242
243 bool operator==(const PointerCoords& other) const;
244 inline bool operator!=(const PointerCoords& other) const {
245 return !(*this == other);
246 }
247
248 void copyFrom(const PointerCoords& other);
249
250private:
251 void tooManyAxes(int axis);
252};
253
254/*
255 * Pointer property data.
256 */
257struct PointerProperties {
258 // The id of the pointer.
259 int32_t id;
260
261 // The pointer tool type.
262 int32_t toolType;
263
264 inline void clear() {
265 id = -1;
266 toolType = 0;
267 }
268
269 bool operator==(const PointerProperties& other) const;
270 inline bool operator!=(const PointerProperties& other) const {
271 return !(*this == other);
272 }
273
274 void copyFrom(const PointerProperties& other);
275};
276
277/*
278 * Input events.
279 */
280class InputEvent : public AInputEvent {
281public:
282 virtual ~InputEvent() { }
283
284 virtual int32_t getType() const = 0;
285
286 inline int32_t getDeviceId() const { return mDeviceId; }
287
288 inline int32_t getSource() const { return mSource; }
289
290 inline void setSource(int32_t source) { mSource = source; }
291
292protected:
293 void initialize(int32_t deviceId, int32_t source);
294 void initialize(const InputEvent& from);
295
296 int32_t mDeviceId;
297 int32_t mSource;
298};
299
300/*
301 * Key events.
302 */
303class KeyEvent : public InputEvent {
304public:
305 virtual ~KeyEvent() { }
306
307 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
308
309 inline int32_t getAction() const { return mAction; }
310
311 inline int32_t getFlags() const { return mFlags; }
312
313 inline void setFlags(int32_t flags) { mFlags = flags; }
314
315 inline int32_t getKeyCode() const { return mKeyCode; }
316
317 inline int32_t getScanCode() const { return mScanCode; }
318
319 inline int32_t getMetaState() const { return mMetaState; }
320
321 inline int32_t getRepeatCount() const { return mRepeatCount; }
322
323 inline nsecs_t getDownTime() const { return mDownTime; }
324
325 inline nsecs_t getEventTime() const { return mEventTime; }
326
Michael Wright872db4f2014-04-22 15:03:51 -0700327 static const char* getLabel(int32_t keyCode);
328 static int32_t getKeyCodeFromLabel(const char* label);
Jeff Brown5912f952013-07-01 19:10:31 -0700329
330 void initialize(
331 int32_t deviceId,
332 int32_t source,
333 int32_t action,
334 int32_t flags,
335 int32_t keyCode,
336 int32_t scanCode,
337 int32_t metaState,
338 int32_t repeatCount,
339 nsecs_t downTime,
340 nsecs_t eventTime);
341 void initialize(const KeyEvent& from);
342
343protected:
344 int32_t mAction;
345 int32_t mFlags;
346 int32_t mKeyCode;
347 int32_t mScanCode;
348 int32_t mMetaState;
349 int32_t mRepeatCount;
350 nsecs_t mDownTime;
351 nsecs_t mEventTime;
352};
353
354/*
355 * Motion events.
356 */
357class MotionEvent : public InputEvent {
358public:
359 virtual ~MotionEvent() { }
360
361 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
362
363 inline int32_t getAction() const { return mAction; }
364
365 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
366
367 inline int32_t getActionIndex() const {
368 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
369 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
370 }
371
372 inline void setAction(int32_t action) { mAction = action; }
373
374 inline int32_t getFlags() const { return mFlags; }
375
376 inline void setFlags(int32_t flags) { mFlags = flags; }
377
378 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
379
380 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
381
382 inline int32_t getMetaState() const { return mMetaState; }
383
384 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
385
386 inline int32_t getButtonState() const { return mButtonState; }
387
Michael Wright7b159c92015-05-14 14:48:03 +0100388 inline int32_t setButtonState(int32_t buttonState) { mButtonState = buttonState; }
389
390 inline int32_t getActionButton() const { return mActionButton; }
391
Michael Wright21957b92015-06-17 21:06:54 +0100392 inline void setActionButton(int32_t button) { mActionButton = button; }
393
Jeff Brown5912f952013-07-01 19:10:31 -0700394 inline float getXOffset() const { return mXOffset; }
395
396 inline float getYOffset() const { return mYOffset; }
397
398 inline float getXPrecision() const { return mXPrecision; }
399
400 inline float getYPrecision() const { return mYPrecision; }
401
402 inline nsecs_t getDownTime() const { return mDownTime; }
403
404 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
405
406 inline size_t getPointerCount() const { return mPointerProperties.size(); }
407
408 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
409 return &mPointerProperties[pointerIndex];
410 }
411
412 inline int32_t getPointerId(size_t pointerIndex) const {
413 return mPointerProperties[pointerIndex].id;
414 }
415
416 inline int32_t getToolType(size_t pointerIndex) const {
417 return mPointerProperties[pointerIndex].toolType;
418 }
419
420 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
421
422 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
423
424 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
425
426 inline float getRawX(size_t pointerIndex) const {
427 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
428 }
429
430 inline float getRawY(size_t pointerIndex) const {
431 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
432 }
433
434 float getAxisValue(int32_t axis, size_t pointerIndex) const;
435
436 inline float getX(size_t pointerIndex) const {
437 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
438 }
439
440 inline float getY(size_t pointerIndex) const {
441 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
442 }
443
444 inline float getPressure(size_t pointerIndex) const {
445 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
446 }
447
448 inline float getSize(size_t pointerIndex) const {
449 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
450 }
451
452 inline float getTouchMajor(size_t pointerIndex) const {
453 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
454 }
455
456 inline float getTouchMinor(size_t pointerIndex) const {
457 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
458 }
459
460 inline float getToolMajor(size_t pointerIndex) const {
461 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
462 }
463
464 inline float getToolMinor(size_t pointerIndex) const {
465 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
466 }
467
468 inline float getOrientation(size_t pointerIndex) const {
469 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
470 }
471
472 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
473
474 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
475 return mSampleEventTimes[historicalIndex];
476 }
477
478 const PointerCoords* getHistoricalRawPointerCoords(
479 size_t pointerIndex, size_t historicalIndex) const;
480
481 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
482 size_t historicalIndex) const;
483
484 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
485 return getHistoricalRawAxisValue(
486 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
487 }
488
489 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
490 return getHistoricalRawAxisValue(
491 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
492 }
493
494 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
495
496 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
497 return getHistoricalAxisValue(
498 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
499 }
500
501 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
502 return getHistoricalAxisValue(
503 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
504 }
505
506 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
507 return getHistoricalAxisValue(
508 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
509 }
510
511 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
512 return getHistoricalAxisValue(
513 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
514 }
515
516 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
517 return getHistoricalAxisValue(
518 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
519 }
520
521 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
522 return getHistoricalAxisValue(
523 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
524 }
525
526 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
527 return getHistoricalAxisValue(
528 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
529 }
530
531 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
532 return getHistoricalAxisValue(
533 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
534 }
535
536 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
537 return getHistoricalAxisValue(
538 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
539 }
540
541 ssize_t findPointerIndex(int32_t pointerId) const;
542
543 void initialize(
544 int32_t deviceId,
545 int32_t source,
546 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100547 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700548 int32_t flags,
549 int32_t edgeFlags,
550 int32_t metaState,
551 int32_t buttonState,
552 float xOffset,
553 float yOffset,
554 float xPrecision,
555 float yPrecision,
556 nsecs_t downTime,
557 nsecs_t eventTime,
558 size_t pointerCount,
559 const PointerProperties* pointerProperties,
560 const PointerCoords* pointerCoords);
561
562 void copyFrom(const MotionEvent* other, bool keepHistory);
563
564 void addSample(
565 nsecs_t eventTime,
566 const PointerCoords* pointerCoords);
567
568 void offsetLocation(float xOffset, float yOffset);
569
570 void scale(float scaleFactor);
571
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700572 // Apply 3x3 perspective matrix transformation.
573 // Matrix is in row-major form and compatible with SkMatrix.
574 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700575
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700576#ifdef HAVE_ANDROID_OS
Jeff Brown5912f952013-07-01 19:10:31 -0700577 status_t readFromParcel(Parcel* parcel);
578 status_t writeToParcel(Parcel* parcel) const;
579#endif
580
581 static bool isTouchEvent(int32_t source, int32_t action);
582 inline bool isTouchEvent() const {
583 return isTouchEvent(mSource, mAction);
584 }
585
586 // Low-level accessors.
587 inline const PointerProperties* getPointerProperties() const {
588 return mPointerProperties.array();
589 }
590 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
591 inline const PointerCoords* getSamplePointerCoords() const {
592 return mSamplePointerCoords.array();
593 }
594
Michael Wright872db4f2014-04-22 15:03:51 -0700595 static const char* getLabel(int32_t axis);
596 static int32_t getAxisFromLabel(const char* label);
597
Jeff Brown5912f952013-07-01 19:10:31 -0700598protected:
599 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100600 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700601 int32_t mFlags;
602 int32_t mEdgeFlags;
603 int32_t mMetaState;
604 int32_t mButtonState;
605 float mXOffset;
606 float mYOffset;
607 float mXPrecision;
608 float mYPrecision;
609 nsecs_t mDownTime;
610 Vector<PointerProperties> mPointerProperties;
611 Vector<nsecs_t> mSampleEventTimes;
612 Vector<PointerCoords> mSamplePointerCoords;
613};
614
615/*
616 * Input event factory.
617 */
618class InputEventFactoryInterface {
619protected:
620 virtual ~InputEventFactoryInterface() { }
621
622public:
623 InputEventFactoryInterface() { }
624
625 virtual KeyEvent* createKeyEvent() = 0;
626 virtual MotionEvent* createMotionEvent() = 0;
627};
628
629/*
630 * A simple input event factory implementation that uses a single preallocated instance
631 * of each type of input event that are reused for each request.
632 */
633class PreallocatedInputEventFactory : public InputEventFactoryInterface {
634public:
635 PreallocatedInputEventFactory() { }
636 virtual ~PreallocatedInputEventFactory() { }
637
638 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
639 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
640
641private:
642 KeyEvent mKeyEvent;
643 MotionEvent mMotionEvent;
644};
645
646/*
647 * An input event factory implementation that maintains a pool of input events.
648 */
649class PooledInputEventFactory : public InputEventFactoryInterface {
650public:
651 PooledInputEventFactory(size_t maxPoolSize = 20);
652 virtual ~PooledInputEventFactory();
653
654 virtual KeyEvent* createKeyEvent();
655 virtual MotionEvent* createMotionEvent();
656
657 void recycle(InputEvent* event);
658
659private:
660 const size_t mMaxPoolSize;
661
662 Vector<KeyEvent*> mKeyEventPool;
663 Vector<MotionEvent*> mMotionEventPool;
664};
665
666} // namespace android
667
668#endif // _LIBINPUT_INPUT_H