blob: e49a04226c41cf85fe80f11fa7d163b1de54299f [file]
/*
* 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.
*/
#define LOG_TAG "MotionButtonInterceptor"
#include "MotionButtonInterceptor.h"
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android/input.h>
#include <InputListener.h>
#include <input/Input.h>
#include <log/log.h>
#include <MotionButtonInterceptorPolicyInterface.h>
#include <NotifyArgs.h>
#include <string>
namespace android {
namespace {
/**
* Log detailed debug messages about each motion event notification and its processing.
* Enable this via "adb shell setprop log.tag.MotionButtonInterceptorEvents DEBUG"
* (requires restart)
*/
const bool DEBUG_BUTTON_INTERCEPTOR =
__android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Events", ANDROID_LOG_INFO);
constexpr int32_t FORBIDDEN_BUTTONS =
AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY;
} // namespace
// --- MotionButtonInterceptor ---
MotionButtonInterceptor::MotionButtonInterceptor(InputListenerInterface& nextListener,
MotionButtonInterceptorPolicyInterface& policy)
: mNextListener(nextListener), mPolicy(policy) {}
void MotionButtonInterceptor::notifyMotion(const NotifyMotionArgs& args) {
LOG_IF(INFO, DEBUG_BUTTON_INTERCEPTOR) << __func__ << " [Inbound event]: " << args.dump();
if (!isFromMouse(args.source, args.pointerProperties[0].toolType)) {
mNextListener.notify(args);
return;
}
std::optional<NotifyMotionArgs> processedArgs = processMotionArgs(args);
if (processedArgs.has_value()) {
LOG_IF(INFO, DEBUG_BUTTON_INTERCEPTOR)
<< __func__ << " [Outbound event]: " << processedArgs->dump();
mNextListener.notify(*processedArgs);
}
}
void MotionButtonInterceptor::dump(std::string& dump) const {
std::scoped_lock lock(mLock);
dump += "MotionButtonInterceptor:\n";
if (mInterceptedButtons.empty()) {
dump += " Intercepted Buttons: <none>\n";
return;
}
dump += " Intercepted Buttons:\n";
for (const auto& [deviceId, buttonMask] : mInterceptedButtons) {
dump += android::base::StringPrintf(" Device %d: %s\n", deviceId,
buttonMask.string().c_str());
}
}
void MotionButtonInterceptor::requestPolicyInterceptionForButtons(DeviceId deviceId,
int32_t buttonMask) {
LOG_ALWAYS_FATAL_IF((buttonMask & FORBIDDEN_BUTTONS) != 0,
"Should not be requesting interception for PRIMARY or "
"SECONDARY mouse buttons");
{ // acquire lock
std::scoped_lock lock(mLock);
if (buttonMask == 0) {
mInterceptedButtons.erase(deviceId);
} else {
mInterceptedButtons[deviceId] = ftl::Flags<MotionEventButton>(buttonMask);
}
} // release lock
}
std::optional<NotifyMotionArgs> MotionButtonInterceptor::processMotionArgs(
const NotifyMotionArgs& args) {
// Process incoming mouse events by intercepting customized button clicks.
// The processing flow is divided into three stages:
// 1. Verify mappings and return early for standard mouse pointer movements.
// 2. Intercept physical button press/release events and notify the policy.
// 3. Scrub remapped keys from states and synthesize synthetic Down/Up transitions.
int32_t interceptedMask = 0;
int32_t activeInterceptions = 0;
{ // acquire lock
std::scoped_lock lock(mLock);
if (auto it = mInterceptedButtons.find(args.deviceId); it != mInterceptedButtons.end()) {
interceptedMask = it->second.get();
}
if (auto activeIt = mActiveInterceptions.find(args.deviceId);
activeIt != mActiveInterceptions.end()) {
activeInterceptions = activeIt->second.get();
}
if (interceptedMask == 0 && activeInterceptions == 0 &&
!mPendingUpSuppression.contains(args.deviceId)) {
return args;
}
} // release lock
// Update interceptedMask in case mInterceptedButtons was updated when a button is held down.
interceptedMask |= activeInterceptions;
const int32_t actionMasked = args.action & AMOTION_EVENT_ACTION_MASK;
// Return early for mouse movements with no active interceptions.
const bool isMoveAction = actionMasked == AMOTION_EVENT_ACTION_MOVE ||
actionMasked == AMOTION_EVENT_ACTION_HOVER_MOVE;
if (isMoveAction && activeInterceptions == 0 && args.buttonState == 0) {
return args;
}
const int32_t button = args.actionButton;
const int32_t uninterceptedButtonState = args.buttonState & ~interceptedMask;
if ((interceptedMask & button) != 0) {
if (actionMasked == AMOTION_EVENT_ACTION_BUTTON_PRESS) {
// Intercept button press and notify the policy.
{ // acquire lock
std::scoped_lock lock(mLock);
mActiveInterceptions[args.deviceId] |= ftl::Flags<MotionEventButton>(button);
} // release lock
activeInterceptions |= button;
mPolicy.notifyButtonIntercepted(args.deviceId, button,
AMOTION_EVENT_ACTION_BUTTON_PRESS, args.displayId);
return std::nullopt;
} else if (actionMasked == AMOTION_EVENT_ACTION_BUTTON_RELEASE) {
// Intercept button release and notify the policy.
{ // acquire lock
std::scoped_lock lock(mLock);
mActiveInterceptions[args.deviceId].clear(ftl::Flags<MotionEventButton>(button));
if (!mActiveInterceptions[args.deviceId].any()) {
mActiveInterceptions.erase(args.deviceId);
// Suppress subsequent UP action if this released the last intercepted button.
if (uninterceptedButtonState == 0 && isPointerDown(button)) {
mPendingUpSuppression.insert(args.deviceId);
}
}
} // release lock
activeInterceptions &= ~button;
mPolicy.notifyButtonIntercepted(args.deviceId, button,
AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.displayId);
return std::nullopt;
}
}
// Synthesize DOWN or UP events for simultaneous button presses.
if (isPointerDown(button) && isPointerDown(activeInterceptions)) {
if (actionMasked == AMOTION_EVENT_ACTION_BUTTON_PRESS &&
uninterceptedButtonState == button) {
NotifyMotionArgs syntheticDown = args;
syntheticDown.action = AMOTION_EVENT_ACTION_DOWN;
syntheticDown.actionButton = 0;
syntheticDown.buttonState = button;
LOG_IF(INFO, DEBUG_BUTTON_INTERCEPTOR)
<< __func__ << " [Synthesized DOWN event]: " << syntheticDown.dump();
mNextListener.notify(syntheticDown);
} else if (actionMasked == AMOTION_EVENT_ACTION_BUTTON_RELEASE &&
uninterceptedButtonState == 0) {
NotifyMotionArgs cleanRelease = args;
cleanRelease.buttonState = 0;
LOG_IF(INFO, DEBUG_BUTTON_INTERCEPTOR)
<< __func__ << " [ButtonStateScrubbed event]: " << cleanRelease.dump();
mNextListener.notify(cleanRelease);
NotifyMotionArgs syntheticUp = args;
syntheticUp.action = AMOTION_EVENT_ACTION_UP;
syntheticUp.actionButton = 0;
syntheticUp.buttonState = 0;
return syntheticUp;
}
}
// Update the buttonState.
NotifyMotionArgs mutatedArgs = args;
mutatedArgs.buttonState = uninterceptedButtonState;
if (actionMasked == AMOTION_EVENT_ACTION_DOWN) {
// Drop DOWN caused solely by intercepted buttons.
if (uninterceptedButtonState == 0) {
return std::nullopt;
}
} else if (actionMasked == AMOTION_EVENT_ACTION_UP) {
// Drop UP caused solely by intercepted buttons.
{ // acquire lock
std::scoped_lock lock(mLock);
if (mPendingUpSuppression.contains(args.deviceId)) {
mPendingUpSuppression.erase(args.deviceId);
return std::nullopt;
}
} // release lock
} else if (actionMasked == AMOTION_EVENT_ACTION_MOVE) {
// Convert MOVE to HOVER_MOVE if only intercepted buttons are held.
if (uninterceptedButtonState == 0 && activeInterceptions != 0) {
mutatedArgs.action = AMOTION_EVENT_ACTION_HOVER_MOVE;
}
}
return mutatedArgs;
}
} // namespace android