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 #define LOG_TAG "InputListener"
18
19 #define ATRACE_TAG ATRACE_TAG_INPUT
20
21 //#define LOG_NDEBUG 0
22
23 #include "InputListener.h"
24
25 #include <android-base/stringprintf.h>
26 #include <android/log.h>
27 #include <input/TraceTools.h>
28
29 using android::base::StringPrintf;
30
31 namespace android {
32
operator +=(std::list<NotifyArgs> & keep,std::list<NotifyArgs> && consume)33 std::list<NotifyArgs>& operator+=(std::list<NotifyArgs>& keep, std::list<NotifyArgs>&& consume) {
34 keep.splice(keep.end(), consume);
35 return keep;
36 }
37
38 // --- InputListenerInterface ---
39
40 // Helper to std::visit with lambdas.
41 template <typename... V>
42 struct Visitor : V... { using V::operator()...; };
43 // explicit deduction guide (not needed as of C++20)
44 template <typename... V>
45 Visitor(V...) -> Visitor<V...>;
46
notify(const NotifyArgs & generalArgs)47 void InputListenerInterface::notify(const NotifyArgs& generalArgs) {
48 Visitor v{
49 [&](const NotifyInputDevicesChangedArgs& args) { notifyInputDevicesChanged(args); },
50 [&](const NotifyConfigurationChangedArgs& args) { notifyConfigurationChanged(args); },
51 [&](const NotifyKeyArgs& args) { notifyKey(args); },
52 [&](const NotifyMotionArgs& args) { notifyMotion(args); },
53 [&](const NotifySwitchArgs& args) { notifySwitch(args); },
54 [&](const NotifySensorArgs& args) { notifySensor(args); },
55 [&](const NotifyVibratorStateArgs& args) { notifyVibratorState(args); },
56 [&](const NotifyDeviceResetArgs& args) { notifyDeviceReset(args); },
57 [&](const NotifyPointerCaptureChangedArgs& args) { notifyPointerCaptureChanged(args); },
58 };
59 std::visit(v, generalArgs);
60 }
61
62 // --- QueuedInputListener ---
63
QueuedInputListener(InputListenerInterface & innerListener)64 QueuedInputListener::QueuedInputListener(InputListenerInterface& innerListener)
65 : mInnerListener(innerListener) {}
66
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)67 void QueuedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
68 mArgsQueue.emplace_back(args);
69 }
70
notifyConfigurationChanged(const NotifyConfigurationChangedArgs & args)71 void QueuedInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
72 mArgsQueue.emplace_back(args);
73 }
74
notifyKey(const NotifyKeyArgs & args)75 void QueuedInputListener::notifyKey(const NotifyKeyArgs& args) {
76 mArgsQueue.emplace_back(args);
77 }
78
notifyMotion(const NotifyMotionArgs & args)79 void QueuedInputListener::notifyMotion(const NotifyMotionArgs& args) {
80 mArgsQueue.emplace_back(args);
81 }
82
notifySwitch(const NotifySwitchArgs & args)83 void QueuedInputListener::notifySwitch(const NotifySwitchArgs& args) {
84 mArgsQueue.emplace_back(args);
85 }
86
notifySensor(const NotifySensorArgs & args)87 void QueuedInputListener::notifySensor(const NotifySensorArgs& args) {
88 mArgsQueue.emplace_back(args);
89 }
90
notifyVibratorState(const NotifyVibratorStateArgs & args)91 void QueuedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
92 mArgsQueue.emplace_back(args);
93 }
94
notifyDeviceReset(const NotifyDeviceResetArgs & args)95 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
96 mArgsQueue.emplace_back(args);
97 }
98
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)99 void QueuedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
100 mArgsQueue.emplace_back(args);
101 }
102
flush()103 void QueuedInputListener::flush() {
104 for (const NotifyArgs& args : mArgsQueue) {
105 mInnerListener.notify(args);
106 }
107 mArgsQueue.clear();
108 }
109
110 // --- TracedInputListener ---
111
TracedInputListener(const char * name,InputListenerInterface & innerListener)112 TracedInputListener::TracedInputListener(const char* name, InputListenerInterface& innerListener)
113 : mInnerListener(innerListener), mName(name) {}
114
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)115 void TracedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
116 constexpr static auto& fnName = __func__;
117 ATRACE_NAME_IF(ATRACE_ENABLED(),
118 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
119 mInnerListener.notify(args);
120 }
121
notifyConfigurationChanged(const NotifyConfigurationChangedArgs & args)122 void TracedInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
123 constexpr static auto& fnName = __func__;
124 ATRACE_NAME_IF(ATRACE_ENABLED(),
125 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
126 mInnerListener.notify(args);
127 }
128
notifyKey(const NotifyKeyArgs & args)129 void TracedInputListener::notifyKey(const NotifyKeyArgs& args) {
130 constexpr static auto& fnName = __func__;
131 ATRACE_NAME_IF(ATRACE_ENABLED(),
132 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
133 mInnerListener.notify(args);
134 }
135
notifyMotion(const NotifyMotionArgs & args)136 void TracedInputListener::notifyMotion(const NotifyMotionArgs& args) {
137 constexpr static auto& fnName = __func__;
138 ATRACE_NAME_IF(ATRACE_ENABLED(),
139 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
140 mInnerListener.notify(args);
141 }
142
notifySwitch(const NotifySwitchArgs & args)143 void TracedInputListener::notifySwitch(const NotifySwitchArgs& args) {
144 constexpr static auto& fnName = __func__;
145 ATRACE_NAME_IF(ATRACE_ENABLED(),
146 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
147 mInnerListener.notify(args);
148 }
149
notifySensor(const NotifySensorArgs & args)150 void TracedInputListener::notifySensor(const NotifySensorArgs& args) {
151 constexpr static auto& fnName = __func__;
152 ATRACE_NAME_IF(ATRACE_ENABLED(),
153 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
154 mInnerListener.notify(args);
155 }
156
notifyVibratorState(const NotifyVibratorStateArgs & args)157 void TracedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
158 constexpr static auto& fnName = __func__;
159 ATRACE_NAME_IF(ATRACE_ENABLED(),
160 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
161 mInnerListener.notify(args);
162 }
163
notifyDeviceReset(const NotifyDeviceResetArgs & args)164 void TracedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
165 constexpr static auto& fnName = __func__;
166 ATRACE_NAME_IF(ATRACE_ENABLED(),
167 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
168 mInnerListener.notify(args);
169 }
170
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)171 void TracedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
172 constexpr static auto& fnName = __func__;
173 ATRACE_NAME_IF(ATRACE_ENABLED(),
174 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
175 mInnerListener.notify(args);
176 }
177
178 } // namespace android
179