blob: d76b8fed003bce9f305ee77325ec5f60fe9ea3a8 [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -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 _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include <ui/EventHub.h>
21#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070022#include <ui/InputDispatcher.h>
23#include <utils/KeyedVector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/BitSet.h>
29
30#include <stddef.h>
31#include <unistd.h>
32
33/* Maximum pointer id value supported.
34 * (This is limited by our use of BitSet32 to track pointer assignments.) */
35#define MAX_POINTER_ID 32
36
Jeff Browne839a582010-04-22 18:58:52 -070037/* Maximum number of historical samples to average. */
38#define AVERAGING_HISTORY_SIZE 5
39
40
41namespace android {
42
43extern int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState);
44extern int32_t rotateKeyCode(int32_t keyCode, int32_t orientation);
45
46/*
47 * An input device structure tracks the state of a single input device.
48 *
49 * This structure is only used by ReaderThread and is not intended to be shared with
50 * DispatcherThread (because that would require locking). This works out fine because
51 * DispatcherThread is only interested in cooked event data anyways and does not need
52 * any of the low-level data from InputDevice.
53 */
54struct InputDevice {
55 struct AbsoluteAxisInfo {
56 int32_t minValue; // minimum value
57 int32_t maxValue; // maximum value
58 int32_t range; // range of values, equal to maxValue - minValue
59 int32_t flat; // center flat position, eg. flat == 8 means center is between -8 and 8
60 int32_t fuzz; // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
61 };
62
63 struct VirtualKey {
64 int32_t keyCode;
65 int32_t scanCode;
66 uint32_t flags;
67
68 // computed hit box, specified in touch screen coords based on known display size
69 int32_t hitLeft;
70 int32_t hitTop;
71 int32_t hitRight;
72 int32_t hitBottom;
73
74 inline bool isHit(int32_t x, int32_t y) const {
75 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
76 }
77 };
78
79 struct KeyboardState {
80 struct Current {
81 int32_t metaState;
82 nsecs_t downTime; // time of most recent key down
83 } current;
84
85 void reset();
86 };
87
88 struct TrackballState {
89 struct Accumulator {
90 enum {
91 FIELD_BTN_MOUSE = 1,
92 FIELD_REL_X = 2,
93 FIELD_REL_Y = 4
94 };
95
96 uint32_t fields;
97
98 bool btnMouse;
99 int32_t relX;
100 int32_t relY;
101
102 inline void clear() {
103 fields = 0;
104 }
105
106 inline bool isDirty() {
107 return fields != 0;
108 }
109 } accumulator;
110
111 struct Current {
112 bool down;
113 nsecs_t downTime;
114 } current;
115
116 struct Precalculated {
117 float xScale;
118 float yScale;
119 float xPrecision;
120 float yPrecision;
121 } precalculated;
122
123 void reset();
124 };
125
126 struct SingleTouchScreenState {
127 struct Accumulator {
128 enum {
129 FIELD_BTN_TOUCH = 1,
130 FIELD_ABS_X = 2,
131 FIELD_ABS_Y = 4,
132 FIELD_ABS_PRESSURE = 8,
133 FIELD_ABS_TOOL_WIDTH = 16
134 };
135
136 uint32_t fields;
137
138 bool btnTouch;
139 int32_t absX;
140 int32_t absY;
141 int32_t absPressure;
142 int32_t absToolWidth;
143
144 inline void clear() {
145 fields = 0;
146 }
147
148 inline bool isDirty() {
149 return fields != 0;
150 }
151 } accumulator;
152
153 struct Current {
154 bool down;
155 int32_t x;
156 int32_t y;
157 int32_t pressure;
158 int32_t size;
159 } current;
160
161 void reset();
162 };
163
164 struct MultiTouchScreenState {
165 struct Accumulator {
166 enum {
167 FIELD_ABS_MT_POSITION_X = 1,
168 FIELD_ABS_MT_POSITION_Y = 2,
169 FIELD_ABS_MT_TOUCH_MAJOR = 4,
170 FIELD_ABS_MT_WIDTH_MAJOR = 8,
171 FIELD_ABS_MT_TRACKING_ID = 16
172 };
173
174 uint32_t pointerCount;
175 struct Pointer {
176 uint32_t fields;
177
178 int32_t absMTPositionX;
179 int32_t absMTPositionY;
180 int32_t absMTTouchMajor;
181 int32_t absMTWidthMajor;
182 int32_t absMTTrackingId;
183
184 inline void clear() {
185 fields = 0;
186 }
187 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
188
189 inline void clear() {
190 pointerCount = 0;
191 pointers[0].clear();
192 }
193
194 inline bool isDirty() {
195 return pointerCount != 0;
196 }
197 } accumulator;
198
199 void reset();
200 };
201
202 struct PointerData {
203 uint32_t id;
204 int32_t x;
205 int32_t y;
206 int32_t pressure;
207 int32_t size;
208 };
209
210 struct TouchData {
211 uint32_t pointerCount;
212 PointerData pointers[MAX_POINTERS];
213 BitSet32 idBits;
214 uint32_t idToIndex[MAX_POINTER_ID];
215
216 void copyFrom(const TouchData& other);
217
218 inline void clear() {
219 pointerCount = 0;
220 idBits.clear();
221 }
222 };
223
224 // common state used for both single-touch and multi-touch screens after the initial
225 // touch decoding has been performed
226 struct TouchScreenState {
227 Vector<VirtualKey> virtualKeys;
228
229 struct Parameters {
230 bool useBadTouchFilter;
231 bool useJumpyTouchFilter;
232 bool useAveragingTouchFilter;
233
234 AbsoluteAxisInfo xAxis;
235 AbsoluteAxisInfo yAxis;
236 AbsoluteAxisInfo pressureAxis;
237 AbsoluteAxisInfo sizeAxis;
238 } parameters;
239
240 // The touch data of the current sample being processed.
241 TouchData currentTouch;
242
243 // The touch data of the previous sample that was processed. This is updated
244 // incrementally while the current sample is being processed.
245 TouchData lastTouch;
246
247 // The time the primary pointer last went down.
248 nsecs_t downTime;
249
250 struct CurrentVirtualKeyState {
251 bool down;
252 nsecs_t downTime;
253 int32_t keyCode;
254 int32_t scanCode;
255 } currentVirtualKey;
256
257 struct AveragingTouchFilterState {
258 // Individual history tracks are stored by pointer id
259 uint32_t historyStart[MAX_POINTERS];
260 uint32_t historyEnd[MAX_POINTERS];
261 struct {
262 struct {
263 int32_t x;
264 int32_t y;
265 int32_t pressure;
266 } pointers[MAX_POINTERS];
267 } historyData[AVERAGING_HISTORY_SIZE];
268 } averagingTouchFilter;
269
270 struct JumpTouchFilterState {
271 int32_t jumpyPointsDropped;
272 } jumpyTouchFilter;
273
274 struct Precalculated {
275 float xScale;
276 float yScale;
277 float pressureScale;
278 float sizeScale;
279 } precalculated;
280
281 void reset();
282
283 bool applyBadTouchFilter();
284 bool applyJumpyTouchFilter();
285 void applyAveragingTouchFilter();
286 void calculatePointerIds();
287
288 bool isPointInsideDisplay(int32_t x, int32_t y) const;
289 };
290
291 InputDevice(int32_t id, uint32_t classes, String8 name);
292
293 int32_t id;
294 uint32_t classes;
295 String8 name;
296 bool ignored;
297
298 KeyboardState keyboard;
299 TrackballState trackball;
300 TouchScreenState touchScreen;
301 union {
302 SingleTouchScreenState singleTouchScreen;
303 MultiTouchScreenState multiTouchScreen;
304 };
305
306 void reset();
307
308 inline bool isKeyboard() const { return classes & INPUT_DEVICE_CLASS_KEYBOARD; }
309 inline bool isAlphaKey() const { return classes & INPUT_DEVICE_CLASS_ALPHAKEY; }
310 inline bool isTrackball() const { return classes & INPUT_DEVICE_CLASS_TRACKBALL; }
311 inline bool isDPad() const { return classes & INPUT_DEVICE_CLASS_DPAD; }
312 inline bool isSingleTouchScreen() const { return (classes
313 & (INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT))
314 == INPUT_DEVICE_CLASS_TOUCHSCREEN; }
315 inline bool isMultiTouchScreen() const { return classes
316 & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT; }
317 inline bool isTouchScreen() const { return classes
318 & (INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT); }
319};
320
321
Jeff Brown54bc2812010-06-15 01:31:58 -0700322/*
323 * Input reader policy interface.
324 *
325 * The input reader policy is used by the input reader to interact with the Window Manager
326 * and other system components.
327 *
328 * The actual implementation is partially supported by callbacks into the DVM
329 * via JNI. This interface is also mocked in the unit tests.
330 */
331class InputReaderPolicyInterface : public virtual RefBase {
332protected:
333 InputReaderPolicyInterface() { }
334 virtual ~InputReaderPolicyInterface() { }
335
336public:
337 /* Display orientations. */
338 enum {
339 ROTATION_0 = 0,
340 ROTATION_90 = 1,
341 ROTATION_180 = 2,
342 ROTATION_270 = 3
343 };
344
345 /* Actions returned by interceptXXX methods. */
346 enum {
347 // The input dispatcher should do nothing and discard the input unless other
348 // flags are set.
349 ACTION_NONE = 0,
350
351 // The input dispatcher should dispatch the input to the application.
352 ACTION_DISPATCH = 0x00000001,
353
354 // The input dispatcher should perform special filtering in preparation for
355 // a pending app switch.
356 ACTION_APP_SWITCH_COMING = 0x00000002,
357
358 // The input dispatcher should add POLICY_FLAG_WOKE_HERE to the policy flags it
359 // passes through the dispatch pipeline.
360 ACTION_WOKE_HERE = 0x00000004,
361
362 // The input dispatcher should add POLICY_FLAG_BRIGHT_HERE to the policy flags it
363 // passes through the dispatch pipeline.
364 ACTION_BRIGHT_HERE = 0x00000008
365 };
366
367 /* Describes a virtual key. */
368 struct VirtualKeyDefinition {
369 int32_t scanCode;
370
371 // configured position data, specified in display coords
372 int32_t centerX;
373 int32_t centerY;
374 int32_t width;
375 int32_t height;
376 };
377
378 /* Gets information about the display with the specified id.
379 * Returns true if the display info is available, false otherwise.
380 */
381 virtual bool getDisplayInfo(int32_t displayId,
382 int32_t* width, int32_t* height, int32_t* orientation) = 0;
383
384 /* Provides feedback for a virtual key.
385 */
386 virtual void virtualKeyFeedback(nsecs_t when, int32_t deviceId,
387 int32_t action, int32_t flags, int32_t keyCode,
388 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
389
390 /* Intercepts a key event.
391 * The policy can use this method as an opportunity to perform power management functions
392 * and early event preprocessing.
393 *
394 * Returns a policy action constant such as ACTION_DISPATCH.
395 */
396 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
397 bool down, int32_t keyCode, int32_t scanCode, uint32_t policyFlags) = 0;
398
399 /* Intercepts a trackball event.
400 * The policy can use this method as an opportunity to perform power management functions
401 * and early event preprocessing.
402 *
403 * Returns a policy action constant such as ACTION_DISPATCH.
404 */
405 virtual int32_t interceptTrackball(nsecs_t when, bool buttonChanged, bool buttonDown,
406 bool rolled) = 0;
407
408 /* Intercepts a touch event.
409 * The policy can use this method as an opportunity to perform power management functions
410 * and early event preprocessing.
411 *
412 * Returns a policy action constant such as ACTION_DISPATCH.
413 */
414 virtual int32_t interceptTouch(nsecs_t when) = 0;
415
416 /* Intercepts a switch event.
417 * The policy can use this method as an opportunity to perform power management functions
418 * and early event preprocessing.
419 *
420 * Switches are not dispatched to applications so this method should
421 * usually return ACTION_NONE.
422 */
423 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) = 0;
424
425 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
426 * certain device whose screen currently is not all that good.
427 */
428 virtual bool filterTouchEvents() = 0;
429
430 /* Determines whether to turn on some hacks to improve touch interaction with another device
431 * where touch coordinate data can get corrupted.
432 */
433 virtual bool filterJumpyTouchEvents() = 0;
434
435 /* Gets the configured virtual key definitions for an input device. */
436 virtual void getVirtualKeyDefinitions(const String8& deviceName,
437 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
438
439 /* Gets the excluded device names for the platform. */
440 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
441};
442
443
444/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700445class InputReaderInterface : public virtual RefBase {
446protected:
447 InputReaderInterface() { }
448 virtual ~InputReaderInterface() { }
449
450public:
451 /* Runs a single iteration of the processing loop.
452 * Nominally reads and processes one incoming message from the EventHub.
453 *
454 * This method should be called on the input reader thread.
455 */
456 virtual void loopOnce() = 0;
457
458 /* Gets the current virtual key. Returns false if not down.
459 *
460 * This method may be called on any thread (usually by the input manager).
461 */
462 virtual bool getCurrentVirtualKey(int32_t* outKeyCode, int32_t* outScanCode) const = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700463
464 /* Gets the current input device configuration.
465 *
466 * This method may be called on any thread (usually by the input manager).
467 */
468 virtual void getCurrentInputConfiguration(InputConfiguration* outConfiguration) const = 0;
469
470 /*
471 * Query current input state.
472 * deviceId may be -1 to search for the device automatically, filtered by class.
473 * deviceClasses may be -1 to ignore device class while searching.
474 */
475 virtual int32_t getCurrentScanCodeState(int32_t deviceId, int32_t deviceClasses,
476 int32_t scanCode) const = 0;
477 virtual int32_t getCurrentKeyCodeState(int32_t deviceId, int32_t deviceClasses,
478 int32_t keyCode) const = 0;
479 virtual int32_t getCurrentSwitchState(int32_t deviceId, int32_t deviceClasses,
480 int32_t sw) const = 0;
481
482 /* Determine whether physical keys exist for the given framework-domain key codes. */
483 virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700484};
485
Jeff Brown54bc2812010-06-15 01:31:58 -0700486
Jeff Browne839a582010-04-22 18:58:52 -0700487/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700488 * that it sends to the input dispatcher. Some functions of the input reader, such as early
489 * event filtering in low power states, are controlled by a separate policy object.
490 *
491 * IMPORTANT INVARIANT:
492 * Because the policy can potentially block or cause re-entrance into the input reader,
493 * the input reader never calls into the policy while holding its internal locks.
Jeff Browne839a582010-04-22 18:58:52 -0700494 */
495class InputReader : public InputReaderInterface {
496public:
497 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700498 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700499 const sp<InputDispatcherInterface>& dispatcher);
500 virtual ~InputReader();
501
502 virtual void loopOnce();
503
504 virtual bool getCurrentVirtualKey(int32_t* outKeyCode, int32_t* outScanCode) const;
505
Jeff Brown54bc2812010-06-15 01:31:58 -0700506 virtual void getCurrentInputConfiguration(InputConfiguration* outConfiguration) const;
507
508 virtual int32_t getCurrentScanCodeState(int32_t deviceId, int32_t deviceClasses,
509 int32_t scanCode) const;
510 virtual int32_t getCurrentKeyCodeState(int32_t deviceId, int32_t deviceClasses,
511 int32_t keyCode) const;
512 virtual int32_t getCurrentSwitchState(int32_t deviceId, int32_t deviceClasses,
513 int32_t sw) const;
514
515 virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const;
516
Jeff Browne839a582010-04-22 18:58:52 -0700517private:
518 // Lock that must be acquired while manipulating state that may be concurrently accessed
519 // from other threads by input state query methods. It should be held for as short a
520 // time as possible.
521 //
522 // Exported state:
523 // - global virtual key code and scan code
524 // - device list and immutable properties of devices such as id, name, and class
525 // (but not other internal device state)
526 mutable Mutex mExportedStateLock;
527
Jeff Brown54bc2812010-06-15 01:31:58 -0700528 // current virtual key information (lock mExportedStateLock)
529 int32_t mExportedVirtualKeyCode;
530 int32_t mExportedVirtualScanCode;
531
532 // current input configuration (lock mExportedStateLock)
533 InputConfiguration mExportedInputConfiguration;
Jeff Browne839a582010-04-22 18:58:52 -0700534
535 // combined key meta state
536 int32_t mGlobalMetaState;
537
538 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700539 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700540 sp<InputDispatcherInterface> mDispatcher;
541
542 KeyedVector<int32_t, InputDevice*> mDevices;
543
544 // display properties needed to translate touch screen coordinates into display coordinates
545 int32_t mDisplayOrientation;
546 int32_t mDisplayWidth;
547 int32_t mDisplayHeight;
548
549 // low-level input event decoding
550 void process(const RawEvent* rawEvent);
551 void handleDeviceAdded(const RawEvent* rawEvent);
552 void handleDeviceRemoved(const RawEvent* rawEvent);
553 void handleSync(const RawEvent* rawEvent);
554 void handleKey(const RawEvent* rawEvent);
555 void handleRelativeMotion(const RawEvent* rawEvent);
556 void handleAbsoluteMotion(const RawEvent* rawEvent);
557 void handleSwitch(const RawEvent* rawEvent);
558
559 // input policy processing and dispatch
560 void onKey(nsecs_t when, InputDevice* device, bool down,
561 int32_t keyCode, int32_t scanCode, uint32_t policyFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700562 void onSwitch(nsecs_t when, InputDevice* device, int32_t switchCode, int32_t switchValue);
Jeff Browne839a582010-04-22 18:58:52 -0700563 void onSingleTouchScreenStateChanged(nsecs_t when, InputDevice* device);
564 void onMultiTouchScreenStateChanged(nsecs_t when, InputDevice* device);
565 void onTouchScreenChanged(nsecs_t when, InputDevice* device, bool havePointerIds);
566 void onTrackballStateChanged(nsecs_t when, InputDevice* device);
567 void onConfigurationChanged(nsecs_t when);
568
569 bool applyStandardInputDispatchPolicyActions(nsecs_t when,
570 int32_t policyActions, uint32_t* policyFlags);
571
572 bool consumeVirtualKeyTouches(nsecs_t when, InputDevice* device, uint32_t policyFlags);
573 void dispatchVirtualKey(nsecs_t when, InputDevice* device, uint32_t policyFlags,
574 int32_t keyEventAction, int32_t keyEventFlags);
575 void dispatchTouches(nsecs_t when, InputDevice* device, uint32_t policyFlags);
576 void dispatchTouch(nsecs_t when, InputDevice* device, uint32_t policyFlags,
577 InputDevice::TouchData* touch, BitSet32 idBits, int32_t motionEventAction);
578
579 // display
580 void resetDisplayProperties();
581 bool refreshDisplayProperties();
582
583 // device management
584 InputDevice* getDevice(int32_t deviceId);
585 InputDevice* getNonIgnoredDevice(int32_t deviceId);
586 void addDevice(nsecs_t when, int32_t deviceId);
587 void removeDevice(nsecs_t when, InputDevice* device);
588 void configureDevice(InputDevice* device);
589 void configureDeviceForCurrentDisplaySize(InputDevice* device);
590 void configureVirtualKeys(InputDevice* device);
591 void configureAbsoluteAxisInfo(InputDevice* device, int axis, const char* name,
592 InputDevice::AbsoluteAxisInfo* out);
Jeff Brown54bc2812010-06-15 01:31:58 -0700593 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700594
595 // global meta state management for all devices
596 void resetGlobalMetaState();
597 int32_t globalMetaState();
598
599 // virtual key management
Jeff Brown54bc2812010-06-15 01:31:58 -0700600 void updateExportedVirtualKeyState();
601
602 // input configuration management
603 void updateExportedInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700604};
605
606
607/* Reads raw events from the event hub and processes them, endlessly. */
608class InputReaderThread : public Thread {
609public:
610 InputReaderThread(const sp<InputReaderInterface>& reader);
611 virtual ~InputReaderThread();
612
613private:
614 sp<InputReaderInterface> mReader;
615
616 virtual bool threadLoop();
617};
618
619} // namespace android
620
621#endif // _UI_INPUT_READER_H