blob: 4012c69135f1feffb53d9ecbb782655027da5e8f [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_MANAGER_H
18#define _UI_INPUT_MANAGER_H
19
20/**
21 * Native input manager.
22 */
23
24#include <ui/EventHub.h>
25#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070026#include <utils/Errors.h>
27#include <utils/Vector.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31
32namespace android {
33
Jeff Brown54bc2812010-06-15 01:31:58 -070034class InputChannel;
35
36class InputReaderInterface;
37class InputReaderPolicyInterface;
Jeff Browne839a582010-04-22 18:58:52 -070038class InputReaderThread;
Jeff Brown54bc2812010-06-15 01:31:58 -070039
40class InputDispatcherInterface;
41class InputDispatcherPolicyInterface;
Jeff Browne839a582010-04-22 18:58:52 -070042class InputDispatcherThread;
43
44/*
45 * The input manager is the core of the system event processing.
46 *
47 * The input manager uses two threads.
48 *
49 * 1. The InputReaderThread (called "InputReader") reads and preprocesses raw input events,
50 * applies policy, and posts messages to a queue managed by the DispatcherThread.
51 * 2. The InputDispatcherThread (called "InputDispatcher") thread waits for new events on the
52 * queue and asynchronously dispatches them to applications.
53 *
54 * By design, the InputReaderThread class and InputDispatcherThread class do not share any
55 * internal state. Moreover, all communication is done one way from the InputReaderThread
56 * into the InputDispatcherThread and never the reverse. Both classes may interact with the
57 * InputDispatchPolicy, however.
58 *
59 * The InputManager class never makes any calls into Java itself. Instead, the
60 * InputDispatchPolicy is responsible for performing all external interactions with the
61 * system, including calling DVM services.
62 */
63class InputManagerInterface : public virtual RefBase {
64protected:
65 InputManagerInterface() { }
66 virtual ~InputManagerInterface() { }
67
68public:
69 /* Starts the input manager threads. */
70 virtual status_t start() = 0;
71
72 /* Stops the input manager threads and waits for them to exit. */
73 virtual status_t stop() = 0;
74
75 /* Registers an input channel prior to using it as the target of an event. */
76 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel) = 0;
77
78 /* Unregisters an input channel. */
79 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
80
Jeff Brown51d45a72010-06-17 20:52:56 -070081 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -070082 * The synchronization mode determines whether the method blocks while waiting for
83 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -070084 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
85 */
86 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -070087 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -070088
Jeff Brown50de30a2010-06-22 01:27:15 -070089 /* Preempts input dispatch in progress by making pending synchronous
90 * dispatches asynchronous instead. This method is generally called during a focus
91 * transition from one application to the next so as to enable the new application
92 * to start receiving input as soon as possible without having to wait for the
93 * old application to finish up.
94 */
95 virtual void preemptInputDispatch() = 0;
96
Jeff Brown54bc2812010-06-15 01:31:58 -070097 /* Gets input device configuration. */
Jeff Browne57e8952010-07-23 21:28:06 -070098 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -070099
Jeff Browne57e8952010-07-23 21:28:06 -0700100 /* Gets information about the specified input device.
101 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
102 * was no such device.
Jeff Browne839a582010-04-22 18:58:52 -0700103 */
Jeff Browne57e8952010-07-23 21:28:06 -0700104 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
105
106 /* Gets the list of all registered device ids. */
107 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
108
109 /* Queries current input state. */
110 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
111 int32_t scanCode) = 0;
112 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
113 int32_t keyCode) = 0;
114 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
115 int32_t sw) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700116
Jeff Brown54bc2812010-06-15 01:31:58 -0700117 /* Determines whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700118 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
119 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700120};
121
122class InputManager : public InputManagerInterface {
123protected:
124 virtual ~InputManager();
125
126public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700127 InputManager(
128 const sp<EventHubInterface>& eventHub,
129 const sp<InputReaderPolicyInterface>& readerPolicy,
130 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy);
131
132 // (used for testing purposes)
133 InputManager(
134 const sp<InputReaderInterface>& reader,
135 const sp<InputDispatcherInterface>& dispatcher);
Jeff Browne839a582010-04-22 18:58:52 -0700136
137 virtual status_t start();
138 virtual status_t stop();
139
140 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel);
141 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
142
Jeff Brown51d45a72010-06-17 20:52:56 -0700143 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700144 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700145
Jeff Brown50de30a2010-06-22 01:27:15 -0700146 virtual void preemptInputDispatch();
147
Jeff Browne57e8952010-07-23 21:28:06 -0700148 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
149 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
150 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
151 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
152 int32_t scanCode);
153 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
154 int32_t keyCode);
155 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
156 int32_t sw);
157 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
158 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700159
160private:
Jeff Brown54bc2812010-06-15 01:31:58 -0700161 sp<InputReaderInterface> mReader;
Jeff Browne839a582010-04-22 18:58:52 -0700162 sp<InputReaderThread> mReaderThread;
163
Jeff Brown54bc2812010-06-15 01:31:58 -0700164 sp<InputDispatcherInterface> mDispatcher;
165 sp<InputDispatcherThread> mDispatcherThread;
166
167 void initialize();
Jeff Browne839a582010-04-22 18:58:52 -0700168};
169
170} // namespace android
171
172#endif // _UI_INPUT_MANAGER_H