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