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