| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #pragma once | |
| #include <InputListener.h> | |
| #include <MotionButtonInterceptorPolicyInterface.h> | |
| #include <NotifyArgs.h> | |
| #include <android-base/thread_annotations.h> | |
| #include <input/MotionEventButton.h> | |
| #include <flat_map> | |
| #include <flat_set> | |
| #include <mutex> | |
| #include <optional> | |
| #include <string> | |
| namespace android { | |
| /** | |
| * An InputListener stage that intercepts remapped mouse button events. | |
| * | |
| * Communicates remapped button actions to the policy, and suppresses active | |
| * button states from downstream motion event streams. Button interception is | |
| * currently supported only for mouse-source motion events. | |
| * | |
| * TODO(b/515117642): Remove mouse-only restriction and handle hover events. | |
| * Note that CursorInputMapper does not send HOVER_EXIT or HOVER_ENTER events, | |
| * which do not have an actionButton and buttonState (and are thus difficult to | |
| * handle). Since the current implementation is only for mouse, the handling of | |
| * HOVER_ENTER and HOVER_EXIT is skipped. | |
| */ | |
| class MotionButtonInterceptor : public InputListenerInterface { | |
| public: | |
| explicit MotionButtonInterceptor(InputListenerInterface& nextListener, | |
| MotionButtonInterceptorPolicyInterface& policy); | |
| virtual ~MotionButtonInterceptor() = default; | |
| void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyWindowInfos(const NotifyWindowInfosArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyDisplayTopology(NotifyDisplayTopologyArgs&& args) override { | |
| mNextListener.notifyDisplayTopology(std::move(args)); | |
| } | |
| void notifyKey(const NotifyKeyArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyMotion(const NotifyMotionArgs& args) override; | |
| void notifySwitch(const NotifySwitchArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifySensor(const NotifySensorArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyVibratorState(const NotifyVibratorStateArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyDeviceReset(const NotifyDeviceResetArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override { | |
| mNextListener.notify(args); | |
| } | |
| void dump(std::string& dump) const; | |
| /** | |
| * Sets the customized button interception bitmask for the specified device. | |
| * The buttonMask parameter is a bitmask of mouse buttons to be intercepted. | |
| * Calling this overrides any existing remapped buttons for the device. | |
| */ | |
| void requestPolicyInterceptionForButtons(DeviceId deviceId, int32_t buttonMask); | |
| private: | |
| InputListenerInterface& mNextListener; | |
| MotionButtonInterceptorPolicyInterface& mPolicy; | |
| mutable std::mutex mLock; | |
| // The bitmask of customized buttons configured by policy to be intercepted. | |
| std::flat_map<DeviceId, ftl::Flags<MotionEventButton>> mInterceptedButtons GUARDED_BY(mLock); | |
| // The bitmask of customized buttons currently held down. | |
| std::flat_map<DeviceId, ftl::Flags<MotionEventButton>> mActiveInterceptions GUARDED_BY(mLock); | |
| // Tracks if an UP action should be suppressed for a device after releasing the | |
| // last custom button. | |
| std::flat_set<DeviceId> mPendingUpSuppression GUARDED_BY(mLock); | |
| std::optional<NotifyMotionArgs> processMotionArgs(const NotifyMotionArgs& args); | |
| }; | |
| } // namespace android |