Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 | |
| 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | #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 | |
| 32 | namespace android { |
| 33 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 34 | class InputChannel; |
| 35 | |
| 36 | class InputReaderInterface; |
| 37 | class InputReaderPolicyInterface; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | class InputReaderThread; |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 39 | |
| 40 | class InputDispatcherInterface; |
| 41 | class InputDispatcherPolicyInterface; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | class 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 | */ |
| 63 | class InputManagerInterface : public virtual RefBase { |
| 64 | protected: |
| 65 | InputManagerInterface() { } |
| 66 | virtual ~InputManagerInterface() { } |
| 67 | |
| 68 | public: |
| 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 Brown | 51d45a7 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 81 | /* 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 Brown | 50de30a | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 90 | /* 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 Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 98 | /* Gets input device configuration. */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame^] | 99 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 100 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame^] | 101 | /* 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 104 | */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame^] | 105 | 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 117 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 118 | /* Determines whether physical keys exist for the given framework-domain key codes. */ |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame^] | 119 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 120 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | class InputManager : public InputManagerInterface { |
| 124 | protected: |
| 125 | virtual ~InputManager(); |
| 126 | |
| 127 | public: |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 128 | 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 137 | |
| 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 Brown | 51d45a7 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 144 | virtual int32_t injectInputEvent(const InputEvent* event, |
| 145 | int32_t injectorPid, int32_t injectorUid, bool sync, int32_t timeoutMillis); |
| 146 | |
Jeff Brown | 50de30a | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 147 | virtual void preemptInputDispatch(); |
| 148 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame^] | 149 | 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | |
| 161 | private: |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 162 | sp<InputReaderInterface> mReader; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | sp<InputReaderThread> mReaderThread; |
| 164 | |
Jeff Brown | 54bc281 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 165 | sp<InputDispatcherInterface> mDispatcher; |
| 166 | sp<InputDispatcherThread> mDispatcherThread; |
| 167 | |
| 168 | void initialize(); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | } // namespace android |
| 172 | |
| 173 | #endif // _UI_INPUT_MANAGER_H |