blob: 990143b5ca89f12c5ece2cecdcee74b1bbbe1433 [file] [log] [blame]
Dianne Hackborn54a181b2010-06-30 18:35:14 -07001/*
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 Hackborn289b9b62010-07-09 11:44:11 -070017#ifndef _ANDROID_APP_NATIVEACTIVITY_H
18#define _ANDROID_APP_NATIVEACTIVITY_H
Dianne Hackborn54a181b2010-06-30 18:35:14 -070019
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070020#include <ui/InputTransport.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070021#include <utils/Looper.h>
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070022
Dianne Hackborn289b9b62010-07-09 11:44:11 -070023#include <android/native_activity.h>
Dianne Hackborn54a181b2010-06-30 18:35:14 -070024
25#include "jni.h"
26
27namespace android {
28
Dianne Hackborndb28a942010-10-21 17:22:30 -070029extern void android_NativeActivity_finish(
30 ANativeActivity* activity);
31
Dianne Hackborn289b9b62010-07-09 11:44:11 -070032extern void android_NativeActivity_setWindowFormat(
33 ANativeActivity* activity, int32_t format);
34
35extern void android_NativeActivity_setWindowFlags(
36 ANativeActivity* activity, int32_t values, int32_t mask);
37
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070038extern void android_NativeActivity_showSoftInput(
39 ANativeActivity* activity, int32_t flags);
40
41extern void android_NativeActivity_hideSoftInput(
42 ANativeActivity* activity, int32_t flags);
Dianne Hackborn54a181b2010-06-30 18:35:14 -070043
44} // namespace android
45
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070046
47/*
48 * NDK input queue API.
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070049 *
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 Hackbornd76b67c2010-07-13 17:48:30 -070067 */
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070068struct AInputQueue : public android::InputEventFactoryInterface {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070069public:
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 Brown4fe6c3e2010-09-13 23:17:30 -070076 void attachLooper(ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070077
78 void detachLooper();
79
80 int32_t hasEvents();
81
82 int32_t getEvent(AInputEvent** outEvent);
83
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070084 bool preDispatchEvent(AInputEvent* event);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070085
Jeff Brown3915bb82010-11-05 15:02:16 -070086 void finishEvent(AInputEvent* event, bool handled, bool didDefaultHandling);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070087
88 // ----------------------------------------------------------
89
90 inline android::InputConsumer& getConsumer() { return mConsumer; }
91
92 void dispatchEvent(android::KeyEvent* event);
93
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070094 void finishPreDispatch(int seq, bool handled);
95
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070096 android::KeyEvent* consumeUnhandledEvent();
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070097 android::KeyEvent* consumePreDispatchingEvent(int* outSeq);
98
99 virtual android::KeyEvent* createKeyEvent();
100 virtual android::MotionEvent* createMotionEvent();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700101
102 int mWorkWrite;
103
104private:
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700105 void doUnhandledKey(android::KeyEvent* keyEvent);
106 bool preDispatchKey(android::KeyEvent* keyEvent);
107 void wakeupDispatch();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700108
109 android::InputConsumer mConsumer;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700110 android::sp<android::Looper> mLooper;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700111
112 int mDispatchKeyRead;
113 int mDispatchKeyWrite;
114
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700115 struct in_flight_event {
116 android::InputEvent* event;
117 int seq;
118 bool doFinish;
119 };
120
121 struct finish_pre_dispatch {
122 int seq;
123 bool handled;
124 };
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700125
126 android::Mutex mLock;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700127
128 int mSeq;
129
130 // Cache of previously allocated key events.
131 android::Vector<android::KeyEvent*> mAvailKeyEvents;
132 // Cache of previously allocated motion events.
133 android::Vector<android::MotionEvent*> mAvailMotionEvents;
134
135 // All input events that are actively being processed.
136 android::Vector<in_flight_event> mInFlightEvents;
137
138 // Key events that the app didn't handle, and are pending for
139 // delivery to the activity's default key handling.
140 android::Vector<android::KeyEvent*> mUnhandledKeys;
141
142 // Keys that arrived in the Java framework and need to be
143 // dispatched to the app.
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700144 android::Vector<android::KeyEvent*> mDispatchingKeys;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700145
146 // Key events that are pending to be pre-dispatched to the IME.
147 android::Vector<in_flight_event> mPreDispatchingKeys;
148
149 // Event sequence numbers that we have finished pre-dispatching.
150 android::Vector<finish_pre_dispatch> mFinishPreDispatches;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700151};
152
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700153#endif // _ANDROID_APP_NATIVEACTIVITY_H