1a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright/*
2a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * Copyright (C) 2013 The Android Open Source Project
3a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright *
4a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * Licensed under the Apache License, Version 2.0 (the "License");
5a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * you may not use this file except in compliance with the License.
6a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * You may obtain a copy of the License at
7a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright *
8a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright *      http://www.apache.org/licenses/LICENSE-2.0
9a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright *
10a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * Unless required by applicable law or agreed to in writing, software
11a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * distributed under the License is distributed on an "AS IS" BASIS,
12a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * See the License for the specific language governing permissions and
14a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * limitations under the License.
15a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright */
16a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
17a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#ifndef _ANDROID_VIEW_INPUTQUEUE_H
18a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#define _ANDROID_VIEW_INPUTQUEUE_H
19a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
209d3b1a424c5c61e24e9659d15fb353026a00d925Jeff Brown#include <input/Input.h>
21a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#include <utils/Looper.h>
22a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#include <utils/TypeHelpers.h>
23a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#include <utils/Vector.h>
24a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
25a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#include "JNIHelp.h"
26a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
27a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright/*
28a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright * Declare a concrete type for the NDK's AInputQueue forward declaration
29a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright */
30a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightstruct AInputQueue{
31a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright};
32a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
33a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightnamespace android {
34a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
35a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightclass InputQueue : public AInputQueue, public MessageHandler {
36a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightpublic:
37a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    virtual ~InputQueue();
38a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
39a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void attachLooper(Looper* looper, int ident, ALooper_callbackFunc callback, void* data);
40a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
41a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void detachLooper();
42a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
43a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    bool hasEvents();
44a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
45a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    status_t getEvent(InputEvent** outEvent);
46a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
47a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    bool preDispatchEvent(InputEvent* event);
48a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
49a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void finishEvent(InputEvent* event, bool handled);
50a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
51a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    KeyEvent* createKeyEvent();
52a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
53a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    MotionEvent* createMotionEvent();
54a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
55a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void recycleInputEvent(InputEvent* event);
56a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
57a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void enqueueEvent(InputEvent* event);
58a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
59a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    static InputQueue* createQueue(jobject inputQueueObj, const sp<Looper>& looper);
60a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
61a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightprotected:
62a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    virtual void handleMessage(const Message& message);
63a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
64a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wrightprivate:
65a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    InputQueue(jobject inputQueueObj, const sp<Looper>& looper,
66a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright            int readDispatchFd, int writeDispatchFd);
67a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
68a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    void detachLooperLocked();
69a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
70a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    jobject mInputQueueWeakGlobal;
71a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    int mDispatchReadFd;
72a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    int mDispatchWriteFd;
73a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    Vector<Looper*> mAppLoopers;
74a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    sp<Looper> mDispatchLooper;
75a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    sp<WeakMessageHandler> mHandler;
76a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    PooledInputEventFactory mPooledInputEventFactory;
77a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    // Guards the pending and finished event vectors
78a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    mutable Mutex mLock;
79a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    Vector<InputEvent*> mPendingEvents;
80a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright    Vector<key_value_pair_t<InputEvent*, bool> > mFinishedEvents;
81a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright};
82a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
83a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright} // namespace android
84a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright
85a44dd26a75e24cc021802288fb81f4761e47be6bMichael Wright#endif
86