Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -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 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 17 | #ifndef _ANDROID_APP_NATIVEACTIVITY_H |
| 18 | #define _ANDROID_APP_NATIVEACTIVITY_H |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 19 | |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 20 | #include <androidfw/InputTransport.h> |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 21 | #include <utils/Looper.h> |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 22 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 23 | #include <android/native_activity.h> |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include "jni.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 29 | extern void android_NativeActivity_finish( |
| 30 | ANativeActivity* activity); |
| 31 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 32 | extern void android_NativeActivity_setWindowFormat( |
| 33 | ANativeActivity* activity, int32_t format); |
| 34 | |
| 35 | extern void android_NativeActivity_setWindowFlags( |
| 36 | ANativeActivity* activity, int32_t values, int32_t mask); |
| 37 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 38 | extern void android_NativeActivity_showSoftInput( |
| 39 | ANativeActivity* activity, int32_t flags); |
| 40 | |
| 41 | extern void android_NativeActivity_hideSoftInput( |
| 42 | ANativeActivity* activity, int32_t flags); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 43 | |
| 44 | } // namespace android |
| 45 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * NDK input queue API. |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 49 | * |
| 50 | * Here is the event flow: |
| 51 | * 1. Event arrives in input consumer, and is returned by getEvent(). |
| 52 | * 2. Application calls preDispatchEvent(): |
| 53 | * a. Event is assigned a sequence ID and enqueued in mPreDispatchingKeys. |
| 54 | * b. Main thread picks up event, hands to input method. |
| 55 | * c. Input method eventually returns sequence # and whether it was handled. |
| 56 | * d. finishPreDispatch() is called to enqueue the information. |
| 57 | * e. next getEvent() call will: |
| 58 | * - finish any pre-dispatch events that the input method handled |
| 59 | * - return the next pre-dispatched event that the input method didn't handle. |
| 60 | * f. (A preDispatchEvent() call on this event will now return false). |
| 61 | * 3. Application calls finishEvent() with whether it was handled. |
| 62 | * - If handled is true, the event is finished. |
| 63 | * - If handled is false, the event is put on mUnhandledKeys, and: |
| 64 | * a. Main thread receives event from consumeUnhandledEvent(). |
| 65 | * b. Java sends event through default key handler. |
| 66 | * c. event is finished. |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 67 | */ |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 68 | struct AInputQueue { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 69 | public: |
| 70 | /* Creates a consumer associated with an input channel. */ |
| 71 | explicit AInputQueue(const android::sp<android::InputChannel>& channel, int workWrite); |
| 72 | |
| 73 | /* Destroys the consumer and releases its input channel. */ |
| 74 | ~AInputQueue(); |
| 75 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 76 | void attachLooper(ALooper* looper, int ident, ALooper_callbackFunc callback, void* data); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 77 | |
| 78 | void detachLooper(); |
| 79 | |
| 80 | int32_t hasEvents(); |
| 81 | |
| 82 | int32_t getEvent(AInputEvent** outEvent); |
| 83 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 84 | bool preDispatchEvent(AInputEvent* event); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 85 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 86 | void finishEvent(AInputEvent* event, bool handled, bool didDefaultHandling); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 87 | |
| 88 | // ---------------------------------------------------------- |
| 89 | |
| 90 | inline android::InputConsumer& getConsumer() { return mConsumer; } |
| 91 | |
| 92 | void dispatchEvent(android::KeyEvent* event); |
| 93 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 94 | void finishPreDispatch(int seq, bool handled); |
| 95 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 96 | android::KeyEvent* consumeUnhandledEvent(); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 97 | android::KeyEvent* consumePreDispatchingEvent(int* outSeq); |
| 98 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 99 | android::KeyEvent* createKeyEvent(); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 100 | |
| 101 | int mWorkWrite; |
| 102 | |
| 103 | private: |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 104 | void doUnhandledKey(android::KeyEvent* keyEvent); |
| 105 | bool preDispatchKey(android::KeyEvent* keyEvent); |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 106 | void wakeupDispatchLocked(); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 107 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 108 | android::PooledInputEventFactory mPooledInputEventFactory; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 109 | android::InputConsumer mConsumer; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 110 | android::sp<android::Looper> mLooper; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 111 | |
| 112 | int mDispatchKeyRead; |
| 113 | int mDispatchKeyWrite; |
| 114 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 115 | struct in_flight_event { |
| 116 | android::InputEvent* event; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 117 | int seq; // internal sequence number for synthetic pre-dispatch events |
| 118 | uint32_t finishSeq; // sequence number for sendFinishedSignal, or 0 if finish not required |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | struct finish_pre_dispatch { |
| 122 | int seq; |
| 123 | bool handled; |
| 124 | }; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 125 | |
| 126 | android::Mutex mLock; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 127 | |
| 128 | int mSeq; |
| 129 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 130 | // All input events that are actively being processed. |
| 131 | android::Vector<in_flight_event> mInFlightEvents; |
| 132 | |
| 133 | // Key events that the app didn't handle, and are pending for |
| 134 | // delivery to the activity's default key handling. |
| 135 | android::Vector<android::KeyEvent*> mUnhandledKeys; |
| 136 | |
| 137 | // Keys that arrived in the Java framework and need to be |
| 138 | // dispatched to the app. |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 139 | android::Vector<android::KeyEvent*> mDispatchingKeys; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 140 | |
| 141 | // Key events that are pending to be pre-dispatched to the IME. |
| 142 | android::Vector<in_flight_event> mPreDispatchingKeys; |
| 143 | |
| 144 | // Event sequence numbers that we have finished pre-dispatching. |
| 145 | android::Vector<finish_pre_dispatch> mFinishPreDispatches; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 148 | #endif // _ANDROID_APP_NATIVEACTIVITY_H |