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 NotifyKeyArgs& args) { notifyKey(args); },
51 [&](const NotifyMotionArgs& args) { notifyMotion(args); },
52 [&](const NotifySwitchArgs& args) { notifySwitch(args); },
53 [&](const NotifySensorArgs& args) { notifySensor(args); },
54 [&](const NotifyVibratorStateArgs& args) { notifyVibratorState(args); },
55 [&](const NotifyDeviceResetArgs& args) { notifyDeviceReset(args); },
56 [&](const NotifyPointerCaptureChangedArgs& args) { notifyPointerCaptureChanged(args); },
57 };
58 std::visit(v, generalArgs);
59 }
60
61 // --- QueuedInputListener ---
62
QueuedInputListener(InputListenerInterface & innerListener)63 QueuedInputListener::QueuedInputListener(InputListenerInterface& innerListener)
64 : mInnerListener(innerListener) {}
65
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)66 void QueuedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
67 mArgsQueue.emplace_back(args);
68 }
69
notifyKey(const NotifyKeyArgs & args)70 void QueuedInputListener::notifyKey(const NotifyKeyArgs& args) {
71 mArgsQueue.emplace_back(args);
72 }
73
notifyMotion(const NotifyMotionArgs & args)74 void QueuedInputListener::notifyMotion(const NotifyMotionArgs& args) {
75 mArgsQueue.emplace_back(args);
76 }
77
notifySwitch(const NotifySwitchArgs & args)78 void QueuedInputListener::notifySwitch(const NotifySwitchArgs& args) {
79 mArgsQueue.emplace_back(args);
80 }
81
notifySensor(const NotifySensorArgs & args)82 void QueuedInputListener::notifySensor(const NotifySensorArgs& args) {
83 mArgsQueue.emplace_back(args);
84 }
85
notifyVibratorState(const NotifyVibratorStateArgs & args)86 void QueuedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
87 mArgsQueue.emplace_back(args);
88 }
89
notifyDeviceReset(const NotifyDeviceResetArgs & args)90 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
91 mArgsQueue.emplace_back(args);
92 }
93
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)94 void QueuedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
95 mArgsQueue.emplace_back(args);
96 }
97
flush()98 void QueuedInputListener::flush() {
99 for (const NotifyArgs& args : mArgsQueue) {
100 mInnerListener.notify(args);
101 }
102 mArgsQueue.clear();
103 }
104
105 // --- TracedInputListener ---
106
TracedInputListener(const char * name,InputListenerInterface & innerListener)107 TracedInputListener::TracedInputListener(const char* name, InputListenerInterface& innerListener)
108 : mInnerListener(innerListener), mName(name) {}
109
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)110 void TracedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
111 constexpr static auto& fnName = __func__;
112 ATRACE_NAME_IF(ATRACE_ENABLED(),
113 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
114 mInnerListener.notify(args);
115 }
116
notifyKey(const NotifyKeyArgs & args)117 void TracedInputListener::notifyKey(const NotifyKeyArgs& args) {
118 constexpr static auto& fnName = __func__;
119 ATRACE_NAME_IF(ATRACE_ENABLED(),
120 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
121 mInnerListener.notify(args);
122 }
123
notifyMotion(const NotifyMotionArgs & args)124 void TracedInputListener::notifyMotion(const NotifyMotionArgs& args) {
125 constexpr static auto& fnName = __func__;
126 ATRACE_NAME_IF(ATRACE_ENABLED(),
127 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
128 mInnerListener.notify(args);
129 }
130
notifySwitch(const NotifySwitchArgs & args)131 void TracedInputListener::notifySwitch(const NotifySwitchArgs& args) {
132 constexpr static auto& fnName = __func__;
133 ATRACE_NAME_IF(ATRACE_ENABLED(),
134 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
135 mInnerListener.notify(args);
136 }
137
notifySensor(const NotifySensorArgs & args)138 void TracedInputListener::notifySensor(const NotifySensorArgs& args) {
139 constexpr static auto& fnName = __func__;
140 ATRACE_NAME_IF(ATRACE_ENABLED(),
141 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
142 mInnerListener.notify(args);
143 }
144
notifyVibratorState(const NotifyVibratorStateArgs & args)145 void TracedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
146 constexpr static auto& fnName = __func__;
147 ATRACE_NAME_IF(ATRACE_ENABLED(),
148 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
149 mInnerListener.notify(args);
150 }
151
notifyDeviceReset(const NotifyDeviceResetArgs & args)152 void TracedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
153 constexpr static auto& fnName = __func__;
154 ATRACE_NAME_IF(ATRACE_ENABLED(),
155 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
156 mInnerListener.notify(args);
157 }
158
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)159 void TracedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
160 constexpr static auto& fnName = __func__;
161 ATRACE_NAME_IF(ATRACE_ENABLED(),
162 StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
163 mInnerListener.notify(args);
164 }
165
166 } // namespace android
167