blob: 7ebec10dd9adaa1b4f5b8770b6533e0bd0ca1c81 [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.
82 * This method may block even if sync is false because it must wait for previous events
83 * to be dispatched before it can determine whether input event injection will be
84 * permitted based on the current input focus.
85 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
86 */
87 virtual int32_t injectInputEvent(const InputEvent* event,
88 int32_t injectorPid, int32_t injectorUid, bool sync, int32_t timeoutMillis) = 0;
89
Jeff Brown50de30a2010-06-22 01:27:15 -070090 /* Preempts input dispatch in progress by making pending synchronous
91 * dispatches asynchronous instead. This method is generally called during a focus
92 * transition from one application to the next so as to enable the new application
93 * to start receiving input as soon as possible without having to wait for the
94 * old application to finish up.
95 */
96 virtual void preemptInputDispatch() = 0;
97
Jeff Brown54bc2812010-06-15 01:31:58 -070098 /* Gets input device configuration. */
Jeff Browne57e8952010-07-23 21:28:06 -070099 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700100
Jeff Browne57e8952010-07-23 21:28:06 -0700101 /* Gets information about the specified input device.
102 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
103 * was no such device.
Jeff Browne839a582010-04-22 18:58:52 -0700104 */
Jeff Browne57e8952010-07-23 21:28:06 -0700105 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
106
107 /* Gets the list of all registered device ids. */
108 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
109
110 /* Queries current input state. */
111 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
112 int32_t scanCode) = 0;
113 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
114 int32_t keyCode) = 0;
115 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
116 int32_t sw) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700117
Jeff Brown54bc2812010-06-15 01:31:58 -0700118 /* Determines whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-07-23 21:28:06 -0700119 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
120 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700121};
122
123class InputManager : public InputManagerInterface {
124protected:
125 virtual ~InputManager();
126
127public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700128 InputManager(
129 const sp<EventHubInterface>& eventHub,
130 const sp<InputReaderPolicyInterface>& readerPolicy,
131 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy);
132
133 // (used for testing purposes)
134 InputManager(
135 const sp<InputReaderInterface>& reader,
136 const sp<InputDispatcherInterface>& dispatcher);
Jeff Browne839a582010-04-22 18:58:52 -0700137
138 virtual status_t start();
139 virtual status_t stop();
140
141 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel);
142 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
143
Jeff Brown51d45a72010-06-17 20:52:56 -0700144 virtual int32_t injectInputEvent(const InputEvent* event,
145 int32_t injectorPid, int32_t injectorUid, bool sync, int32_t timeoutMillis);
146
Jeff Brown50de30a2010-06-22 01:27:15 -0700147 virtual void preemptInputDispatch();
148
Jeff Browne57e8952010-07-23 21:28:06 -0700149 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
150 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
151 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
152 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
153 int32_t scanCode);
154 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
155 int32_t keyCode);
156 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
157 int32_t sw);
158 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
159 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Browne839a582010-04-22 18:58:52 -0700160
161private:
Jeff Brown54bc2812010-06-15 01:31:58 -0700162 sp<InputReaderInterface> mReader;
Jeff Browne839a582010-04-22 18:58:52 -0700163 sp<InputReaderThread> mReaderThread;
164
Jeff Brown54bc2812010-06-15 01:31:58 -0700165 sp<InputDispatcherInterface> mDispatcher;
166 sp<InputDispatcherThread> mDispatcherThread;
167
168 void initialize();
Jeff Browne839a582010-04-22 18:58:52 -0700169};
170
171} // namespace android
172
173#endif // _UI_INPUT_MANAGER_H