blob: b49e02ac6a1695159b211985f7783ffa2ed4529a [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 Hackborn289b9b62010-07-09 11:44:11 -070029extern void android_NativeActivity_setWindowFormat(
30 ANativeActivity* activity, int32_t format);
31
32extern void android_NativeActivity_setWindowFlags(
33 ANativeActivity* activity, int32_t values, int32_t mask);
34
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070035extern void android_NativeActivity_showSoftInput(
36 ANativeActivity* activity, int32_t flags);
37
38extern void android_NativeActivity_hideSoftInput(
39 ANativeActivity* activity, int32_t flags);
Dianne Hackborn54a181b2010-06-30 18:35:14 -070040
41} // namespace android
42
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070043
44/*
45 * NDK input queue API.
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070046 *
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 Hackbornd76b67c2010-07-13 17:48:30 -070064 */
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070065struct AInputQueue : public android::InputEventFactoryInterface {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070066public:
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 Brown4fe6c3e2010-09-13 23:17:30 -070073 void attachLooper(ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070074
75 void detachLooper();
76
77 int32_t hasEvents();
78
79 int32_t getEvent(AInputEvent** outEvent);
80
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070081 bool preDispatchEvent(AInputEvent* event);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070082
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070083 void finishEvent(AInputEvent* event, bool handled);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070084
85 // ----------------------------------------------------------
86
87 inline android::InputConsumer& getConsumer() { return mConsumer; }
88
89 void dispatchEvent(android::KeyEvent* event);
90
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070091 void finishPreDispatch(int seq, bool handled);
92
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070093 android::KeyEvent* consumeUnhandledEvent();
Dianne Hackborn2c6081c2010-07-15 17:44:53 -070094 android::KeyEvent* consumePreDispatchingEvent(int* outSeq);
95
96 virtual android::KeyEvent* createKeyEvent();
97 virtual android::MotionEvent* createMotionEvent();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070098
99 int mWorkWrite;
100
101private:
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700102 void doUnhandledKey(android::KeyEvent* keyEvent);
103 bool preDispatchKey(android::KeyEvent* keyEvent);
104 void wakeupDispatch();
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700105
106 android::InputConsumer mConsumer;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700107 android::sp<android::Looper> mLooper;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700108
109 int mDispatchKeyRead;
110 int mDispatchKeyWrite;
111
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700112 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 Hackbornd76b67c2010-07-13 17:48:30 -0700122
123 android::Mutex mLock;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700124
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 Hackbornd76b67c2010-07-13 17:48:30 -0700141 android::Vector<android::KeyEvent*> mDispatchingKeys;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700142
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 Hackbornd76b67c2010-07-13 17:48:30 -0700148};
149
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700150#endif // _ANDROID_APP_NATIVEACTIVITY_H