InputListener.h revision be1aa8250cee7819c49741e819e81659d1d03823
1/*
2 * Copyright (C) 2011 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 _UI_INPUT_LISTENER_H
18#define _UI_INPUT_LISTENER_H
19
20#include <ui/Input.h>
21#include <utils/RefBase.h>
22#include <utils/Vector.h>
23
24namespace android {
25
26class InputListenerInterface;
27
28
29/* Superclass of all input event argument objects */
30struct NotifyArgs {
31    virtual ~NotifyArgs() { }
32
33    virtual void notify(const sp<InputListenerInterface>& listener) const = 0;
34};
35
36
37/* Describes a configuration change event. */
38struct NotifyConfigurationChangedArgs : public NotifyArgs {
39    nsecs_t eventTime;
40
41    inline NotifyConfigurationChangedArgs() { }
42
43    NotifyConfigurationChangedArgs(nsecs_t eventTime);
44
45    NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other);
46
47    virtual ~NotifyConfigurationChangedArgs() { }
48
49    virtual void notify(const sp<InputListenerInterface>& listener) const;
50};
51
52
53/* Describes a key event. */
54struct NotifyKeyArgs : public NotifyArgs {
55    nsecs_t eventTime;
56    int32_t deviceId;
57    uint32_t source;
58    uint32_t policyFlags;
59    int32_t action;
60    int32_t flags;
61    int32_t keyCode;
62    int32_t scanCode;
63    int32_t metaState;
64    nsecs_t downTime;
65
66    inline NotifyKeyArgs() { }
67
68    NotifyKeyArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source, uint32_t policyFlags,
69            int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
70            int32_t metaState, nsecs_t downTime);
71
72    NotifyKeyArgs(const NotifyKeyArgs& other);
73
74    virtual ~NotifyKeyArgs() { }
75
76    virtual void notify(const sp<InputListenerInterface>& listener) const;
77};
78
79
80/* Describes a motion event. */
81struct NotifyMotionArgs : public NotifyArgs {
82    nsecs_t eventTime;
83    int32_t deviceId;
84    uint32_t source;
85    uint32_t policyFlags;
86    int32_t action;
87    int32_t flags;
88    int32_t metaState;
89    int32_t buttonState;
90    int32_t edgeFlags;
91    uint32_t pointerCount;
92    PointerProperties pointerProperties[MAX_POINTERS];
93    PointerCoords pointerCoords[MAX_POINTERS];
94    float xPrecision;
95    float yPrecision;
96    nsecs_t downTime;
97
98    inline NotifyMotionArgs() { }
99
100    NotifyMotionArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source, uint32_t policyFlags,
101            int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
102            int32_t edgeFlags, uint32_t pointerCount,
103            const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
104            float xPrecision, float yPrecision, nsecs_t downTime);
105
106    NotifyMotionArgs(const NotifyMotionArgs& other);
107
108    virtual ~NotifyMotionArgs() { }
109
110    virtual void notify(const sp<InputListenerInterface>& listener) const;
111};
112
113
114/* Describes a switch event. */
115struct NotifySwitchArgs : public NotifyArgs {
116    nsecs_t eventTime;
117    uint32_t policyFlags;
118    int32_t switchCode;
119    int32_t switchValue;
120
121    inline NotifySwitchArgs() { }
122
123    NotifySwitchArgs(nsecs_t eventTime, uint32_t policyFlags,
124            int32_t switchCode, int32_t switchValue);
125
126    NotifySwitchArgs(const NotifySwitchArgs& other);
127
128    virtual ~NotifySwitchArgs() { }
129
130    virtual void notify(const sp<InputListenerInterface>& listener) const;
131};
132
133
134/*
135 * The interface used by the InputReader to notify the InputListener about input events.
136 */
137class InputListenerInterface : public virtual RefBase {
138protected:
139    InputListenerInterface() { }
140    virtual ~InputListenerInterface() { }
141
142public:
143    virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) = 0;
144    virtual void notifyKey(const NotifyKeyArgs* args) = 0;
145    virtual void notifyMotion(const NotifyMotionArgs* args) = 0;
146    virtual void notifySwitch(const NotifySwitchArgs* args) = 0;
147};
148
149
150/*
151 * An implementation of the listener interface that queues up and defers dispatch
152 * of decoded events until flushed.
153 */
154class QueuedInputListener : public InputListenerInterface {
155protected:
156    virtual ~QueuedInputListener();
157
158public:
159    QueuedInputListener(const sp<InputListenerInterface>& innerListener);
160
161    virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
162    virtual void notifyKey(const NotifyKeyArgs* args);
163    virtual void notifyMotion(const NotifyMotionArgs* args);
164    virtual void notifySwitch(const NotifySwitchArgs* args);
165
166    void flush();
167
168private:
169    sp<InputListenerInterface> mInnerListener;
170    Vector<NotifyArgs*> mArgsQueue;
171};
172
173} // namespace android
174
175#endif // _UI_INPUT_LISTENER_H
176