Jeff Brown | 46b9ac0a | 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 | 46b9ac0a | 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 | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 34 | class InputChannel; |
| 35 | |
| 36 | class InputReaderInterface; |
| 37 | class InputReaderPolicyInterface; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | class InputReaderThread; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 39 | |
| 40 | class InputDispatcherInterface; |
| 41 | class InputDispatcherPolicyInterface; |
Jeff Brown | 46b9ac0a | 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 | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 81 | /* Injects an input event and optionally waits for sync. |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 82 | * The synchronization mode determines whether the method blocks while waiting for |
| 83 | * input injection to proceed. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 84 | * Returns one of the INPUT_EVENT_INJECTION_XXX constants. |
| 85 | */ |
| 86 | virtual int32_t injectInputEvent(const InputEvent* event, |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 87 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 88 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 89 | /* 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 Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 97 | /* Gets input device configuration. */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 98 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 99 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 100 | /* 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 103 | */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 104 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 116 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 117 | /* Determines whether physical keys exist for the given framework-domain key codes. */ |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 118 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 119 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | class InputManager : public InputManagerInterface { |
| 123 | protected: |
| 124 | virtual ~InputManager(); |
| 125 | |
| 126 | public: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 127 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 136 | |
| 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 Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 143 | virtual int32_t injectInputEvent(const InputEvent* event, |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 144 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 145 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 146 | virtual void preemptInputDispatch(); |
| 147 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 148 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 159 | |
| 160 | private: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 161 | sp<InputReaderInterface> mReader; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 162 | sp<InputReaderThread> mReaderThread; |
| 163 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 164 | sp<InputDispatcherInterface> mDispatcher; |
| 165 | sp<InputDispatcherThread> mDispatcherThread; |
| 166 | |
| 167 | void initialize(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace android |
| 171 | |
| 172 | #endif // _UI_INPUT_MANAGER_H |