blob: c388ba80a3bc0e7d3f106491a0c08f157454543f [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>
21
Dianne Hackborn289b9b62010-07-09 11:44:11 -070022#include <android/native_activity.h>
Dianne Hackborn54a181b2010-06-30 18:35:14 -070023
24#include "jni.h"
25
26namespace android {
27
Dianne Hackborn289b9b62010-07-09 11:44:11 -070028extern void android_NativeActivity_setWindowFormat(
29 ANativeActivity* activity, int32_t format);
30
31extern void android_NativeActivity_setWindowFlags(
32 ANativeActivity* activity, int32_t values, int32_t mask);
33
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070034extern void android_NativeActivity_showSoftInput(
35 ANativeActivity* activity, int32_t flags);
36
37extern void android_NativeActivity_hideSoftInput(
38 ANativeActivity* activity, int32_t flags);
Dianne Hackborn54a181b2010-06-30 18:35:14 -070039
40} // namespace android
41
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070042
43/*
44 * NDK input queue API.
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070045 *
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 Hackbornd76b67c2010-07-13 17:48:30 -070063 */
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070064struct AInputQueue : public android::InputEventFactoryInterface {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070065public:
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 Hackborn2c6081c2010-07-15 17:44:53 -070080 bool preDispatchEvent(AInputEvent* event);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070081
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070082 void finishEvent(AInputEvent* event, bool handled);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070083
84 // ----------------------------------------------------------
85
86 inline android::InputConsumer& getConsumer() { return mConsumer; }
87
88 void dispatchEvent(android::KeyEvent* event);
89
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070090 void finishPreDispatch(int seq, bool handled);
91
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070092 android::KeyEvent* consumeUnhandledEvent();
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070093 android::KeyEvent* consumePreDispatchingEvent(int* outSeq);
94
95 virtual android::KeyEvent* createKeyEvent();
96 virtual android::MotionEvent* createMotionEvent();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070097
98 int mWorkWrite;
99
100private:
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700101 void doUnhandledKey(android::KeyEvent* keyEvent);
102 bool preDispatchKey(android::KeyEvent* keyEvent);
103 void wakeupDispatch();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700104
105 android::InputConsumer mConsumer;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700106 android::sp<android::PollLoop> mPollLoop;
107
108 int mDispatchKeyRead;
109 int mDispatchKeyWrite;
110
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700111 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 Hackbornd76b67c2010-07-13 17:48:30 -0700121
122 android::Mutex mLock;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700123
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 Hackbornd76b67c2010-07-13 17:48:30 -0700140 android::Vector<android::KeyEvent*> mDispatchingKeys;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700141
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 Hackbornd76b67c2010-07-13 17:48:30 -0700147};
148
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700149#endif // _ANDROID_APP_NATIVEACTIVITY_H