android_app_NativeActivity.h revision 4fe6c3e51be77e35f40872cdbca6c80f8f8b7ecb
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
17#ifndef _ANDROID_APP_NATIVEACTIVITY_H
18#define _ANDROID_APP_NATIVEACTIVITY_H
19
20#include <ui/InputTransport.h>
21#include <utils/Looper.h>
22
23#include <android/native_activity.h>
24
25#include "jni.h"
26
27namespace android {
28
29extern 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
35extern void android_NativeActivity_showSoftInput(
36        ANativeActivity* activity, int32_t flags);
37
38extern void android_NativeActivity_hideSoftInput(
39        ANativeActivity* activity, int32_t flags);
40
41} // namespace android
42
43
44/*
45 * NDK input queue API.
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.
64 */
65struct AInputQueue : public android::InputEventFactoryInterface {
66public:
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
73    void attachLooper(ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
74
75    void detachLooper();
76
77    int32_t hasEvents();
78
79    int32_t getEvent(AInputEvent** outEvent);
80
81    bool preDispatchEvent(AInputEvent* event);
82
83    void finishEvent(AInputEvent* event, bool handled);
84
85    // ----------------------------------------------------------
86
87    inline android::InputConsumer& getConsumer() { return mConsumer; }
88
89    void dispatchEvent(android::KeyEvent* event);
90
91    void finishPreDispatch(int seq, bool handled);
92
93    android::KeyEvent* consumeUnhandledEvent();
94    android::KeyEvent* consumePreDispatchingEvent(int* outSeq);
95
96    virtual android::KeyEvent* createKeyEvent();
97    virtual android::MotionEvent* createMotionEvent();
98
99    int mWorkWrite;
100
101private:
102    void doUnhandledKey(android::KeyEvent* keyEvent);
103    bool preDispatchKey(android::KeyEvent* keyEvent);
104    void wakeupDispatch();
105
106    android::InputConsumer mConsumer;
107    android::sp<android::Looper> mLooper;
108
109    int mDispatchKeyRead;
110    int mDispatchKeyWrite;
111
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    };
122
123    android::Mutex mLock;
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.
141    android::Vector<android::KeyEvent*> mDispatchingKeys;
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;
148};
149
150#endif // _ANDROID_APP_NATIVEACTIVITY_H
151