Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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. |
| 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 | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 90 | /* Gets input device configuration. */ |
| 91 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) const = 0; |
| 92 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 93 | /* |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 94 | * Queries current input state. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 95 | * deviceId may be -1 to search for the device automatically, filtered by class. |
| 96 | * deviceClasses may be -1 to ignore device class while searching. |
| 97 | */ |
| 98 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t deviceClasses, |
| 99 | int32_t scanCode) const = 0; |
| 100 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t deviceClasses, |
| 101 | int32_t keyCode) const = 0; |
| 102 | virtual int32_t getSwitchState(int32_t deviceId, int32_t deviceClasses, |
| 103 | int32_t sw) const = 0; |
| 104 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 105 | /* Determines whether physical keys exist for the given framework-domain key codes. */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const = 0; |
| 107 | }; |
| 108 | |
| 109 | class InputManager : public InputManagerInterface { |
| 110 | protected: |
| 111 | virtual ~InputManager(); |
| 112 | |
| 113 | public: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 114 | InputManager( |
| 115 | const sp<EventHubInterface>& eventHub, |
| 116 | const sp<InputReaderPolicyInterface>& readerPolicy, |
| 117 | const sp<InputDispatcherPolicyInterface>& dispatcherPolicy); |
| 118 | |
| 119 | // (used for testing purposes) |
| 120 | InputManager( |
| 121 | const sp<InputReaderInterface>& reader, |
| 122 | const sp<InputDispatcherInterface>& dispatcher); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 123 | |
| 124 | virtual status_t start(); |
| 125 | virtual status_t stop(); |
| 126 | |
| 127 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel); |
| 128 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel); |
| 129 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 130 | virtual int32_t injectInputEvent(const InputEvent* event, |
| 131 | int32_t injectorPid, int32_t injectorUid, bool sync, int32_t timeoutMillis); |
| 132 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 133 | virtual void getInputConfiguration(InputConfiguration* outConfiguration) const; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 134 | virtual int32_t getScanCodeState(int32_t deviceId, int32_t deviceClasses, |
| 135 | int32_t scanCode) const; |
| 136 | virtual int32_t getKeyCodeState(int32_t deviceId, int32_t deviceClasses, |
| 137 | int32_t keyCode) const; |
| 138 | virtual int32_t getSwitchState(int32_t deviceId, int32_t deviceClasses, |
| 139 | int32_t sw) const; |
| 140 | virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const; |
| 141 | |
| 142 | private: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 143 | sp<InputReaderInterface> mReader; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 144 | sp<InputReaderThread> mReaderThread; |
| 145 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 146 | sp<InputDispatcherInterface> mDispatcher; |
| 147 | sp<InputDispatcherThread> mDispatcherThread; |
| 148 | |
| 149 | void initialize(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace android |
| 153 | |
| 154 | #endif // _UI_INPUT_MANAGER_H |