• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utils/Trace.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... {};
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 
traceEvent(const char * functionName,int32_t id)64 static inline void traceEvent(const char* functionName, int32_t id) {
65     if (ATRACE_ENABLED()) {
66         std::string message = StringPrintf("%s(id=0x%" PRIx32 ")", functionName, id);
67         ATRACE_NAME(message.c_str());
68     }
69 }
70 
QueuedInputListener(InputListenerInterface & innerListener)71 QueuedInputListener::QueuedInputListener(InputListenerInterface& innerListener)
72       : mInnerListener(innerListener) {}
73 
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)74 void QueuedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
75     traceEvent(__func__, args.id);
76     mArgsQueue.emplace_back(args);
77 }
78 
notifyConfigurationChanged(const NotifyConfigurationChangedArgs & args)79 void QueuedInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
80     traceEvent(__func__, args.id);
81     mArgsQueue.emplace_back(args);
82 }
83 
notifyKey(const NotifyKeyArgs & args)84 void QueuedInputListener::notifyKey(const NotifyKeyArgs& args) {
85     traceEvent(__func__, args.id);
86     mArgsQueue.emplace_back(args);
87 }
88 
notifyMotion(const NotifyMotionArgs & args)89 void QueuedInputListener::notifyMotion(const NotifyMotionArgs& args) {
90     traceEvent(__func__, args.id);
91     mArgsQueue.emplace_back(args);
92 }
93 
notifySwitch(const NotifySwitchArgs & args)94 void QueuedInputListener::notifySwitch(const NotifySwitchArgs& args) {
95     traceEvent(__func__, args.id);
96     mArgsQueue.emplace_back(args);
97 }
98 
notifySensor(const NotifySensorArgs & args)99 void QueuedInputListener::notifySensor(const NotifySensorArgs& args) {
100     traceEvent(__func__, args.id);
101     mArgsQueue.emplace_back(args);
102 }
103 
notifyVibratorState(const NotifyVibratorStateArgs & args)104 void QueuedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
105     traceEvent(__func__, args.id);
106     mArgsQueue.emplace_back(args);
107 }
108 
notifyDeviceReset(const NotifyDeviceResetArgs & args)109 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
110     traceEvent(__func__, args.id);
111     mArgsQueue.emplace_back(args);
112 }
113 
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)114 void QueuedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
115     traceEvent(__func__, args.id);
116     mArgsQueue.emplace_back(args);
117 }
118 
flush()119 void QueuedInputListener::flush() {
120     for (const NotifyArgs& args : mArgsQueue) {
121         mInnerListener.notify(args);
122     }
123     mArgsQueue.clear();
124 }
125 
126 } // namespace android
127