Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e5d49df5 authored by Chris Ye's avatar Chris Ye Committed by Automerger Merge Worker
Browse files

Merge "Refactor InputController into PeripheralController." into sc-dev am: 7660b217

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/14092345

Change-Id: Ic7e8c0272b3900d047a75c6f6e5cc44d45805a2d
parents e8d87ee3 7660b217
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ filegroup {
    srcs: [
        "EventHub.cpp",
        "InputDevice.cpp",
        "controller/InputController.cpp",
        "controller/PeripheralController.cpp",
        "mapper/accumulator/CursorButtonAccumulator.cpp",
        "mapper/accumulator/CursorScrollAccumulator.cpp",
        "mapper/accumulator/SingleTouchMotionAccumulator.cpp",
+19 −31
Original line number Diff line number Diff line
@@ -363,7 +363,7 @@ EventHub::Device::Device(int fd, int32_t id, const std::string& path,
        virtualKeyMap(nullptr),
        ffEffectPlaying(false),
        ffEffectId(-1),
        miscDevice(nullptr),
        associatedDevice(nullptr),
        controllerNumber(0),
        enabled(true),
        isVirtual(fd < 0) {}
@@ -554,7 +554,7 @@ status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const {
}

// Check the sysfs path for any input device batteries, returns true if battery found.
bool EventHub::MiscDevice::configureBatteryLocked() {
bool EventHub::AssociatedDevice::configureBatteryLocked() {
    nextBatteryId = 0;
    // Check if device has any battery.
    const auto& paths = findSysfsNodes(sysfsRootPath, SysfsClass::POWER_SUPPLY);
@@ -580,7 +580,7 @@ bool EventHub::MiscDevice::configureBatteryLocked() {
}

// Check the sysfs path for any input device lights, returns true if lights found.
bool EventHub::MiscDevice::configureLightsLocked() {
bool EventHub::AssociatedDevice::configureLightsLocked() {
    nextLightId = 0;
    // Check if device has any lights.
    const auto& paths = findSysfsNodes(sysfsRootPath, SysfsClass::LEDS);
@@ -988,14 +988,10 @@ const std::unordered_map<int32_t, RawBatteryInfo>& EventHub::getBatteryInfoLocke
        int32_t deviceId) const {
    static const std::unordered_map<int32_t, RawBatteryInfo> EMPTY_BATTERY_INFO = {};
    Device* device = getDeviceLocked(deviceId);
    if (device == nullptr) {
        return EMPTY_BATTERY_INFO;
    }
    auto it = mMiscDevices.find(device->identifier.descriptor);
    if (it == mMiscDevices.end()) {
    if (device == nullptr || !device->associatedDevice) {
        return EMPTY_BATTERY_INFO;
    }
    return it->second->batteryInfos;
    return device->associatedDevice->batteryInfos;
}

const std::vector<int32_t> EventHub::getRawBatteryIds(int32_t deviceId) {
@@ -1028,14 +1024,10 @@ const std::unordered_map<int32_t, RawLightInfo>& EventHub::getLightInfoLocked(
        int32_t deviceId) const {
    static const std::unordered_map<int32_t, RawLightInfo> EMPTY_LIGHT_INFO = {};
    Device* device = getDeviceLocked(deviceId);
    if (device == nullptr) {
        return EMPTY_LIGHT_INFO;
    }
    auto it = mMiscDevices.find(device->identifier.descriptor);
    if (it == mMiscDevices.end()) {
    if (device == nullptr || !device->associatedDevice) {
        return EMPTY_LIGHT_INFO;
    }
    return it->second->lightInfos;
    return device->associatedDevice->lightInfos;
}

const std::vector<int32_t> EventHub::getRawLightIds(int32_t deviceId) {
@@ -1946,18 +1938,20 @@ void EventHub::openDeviceLocked(const std::string& devicePath) {
    // Check the sysfs root path
    std::optional<std::filesystem::path> sysfsRootPath = getSysfsRootPath(devicePath.c_str());
    if (sysfsRootPath.has_value()) {
        std::shared_ptr<MiscDevice> miscDevice;
        auto it = mMiscDevices.find(device->identifier.descriptor);
        if (it == mMiscDevices.end()) {
            miscDevice = std::make_shared<MiscDevice>(sysfsRootPath.value());
        } else {
            miscDevice = it->second;
        std::shared_ptr<AssociatedDevice> associatedDevice;
        for (const auto& [id, dev] : mDevices) {
            if (device->identifier.descriptor == dev->identifier.descriptor &&
                !dev->associatedDevice) {
                associatedDevice = dev->associatedDevice;
            }
        }
        hasBattery = miscDevice->configureBatteryLocked();
        hasLights = miscDevice->configureLightsLocked();
        if (!associatedDevice) {
            associatedDevice = std::make_shared<AssociatedDevice>(sysfsRootPath.value());
        }
        hasBattery = associatedDevice->configureBatteryLocked();
        hasLights = associatedDevice->configureLightsLocked();

        device->miscDevice = miscDevice;
        mMiscDevices.insert_or_assign(device->identifier.descriptor, std::move(miscDevice));
        device->associatedDevice = associatedDevice;
    }

    // Figure out the kinds of events the device reports.
@@ -2329,12 +2323,6 @@ void EventHub::closeDeviceLocked(Device& device) {
    mClosingDevices.push_back(std::move(mDevices[device.id]));

    mDevices.erase(device.id);
    // If all devices with the descriptor have been removed then the miscellaneous device should
    // be removed too.
    std::string descriptor = device.identifier.descriptor;
    if (getDeviceByDescriptorLocked(descriptor) == nullptr) {
        mMiscDevices.erase(descriptor);
    }
}

status_t EventHub::readNotifyLocked() {
+3 −5
Original line number Diff line number Diff line
@@ -23,11 +23,11 @@

#include "CursorInputMapper.h"
#include "ExternalStylusInputMapper.h"
#include "InputController.h"
#include "InputReaderContext.h"
#include "JoystickInputMapper.h"
#include "KeyboardInputMapper.h"
#include "MultiTouchInputMapper.h"
#include "PeripheralController.h"
#include "RotaryEncoderInputMapper.h"
#include "SensorInputMapper.h"
#include "SingleTouchInputMapper.h"
@@ -165,9 +165,9 @@ void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
    }

    // Battery-like devices or light-containing devices.
    // InputController will be created with associated EventHub device.
    // PeripheralController will be created with associated EventHub device.
    if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
        mController = std::make_unique<InputController>(*contextPtr);
        mController = std::make_unique<PeripheralController>(*contextPtr);
    }

    // Keyboard-like devices.
@@ -504,8 +504,6 @@ void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
    for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
}

// TODO b/180733860 support multiple battery in API and remove this.
constexpr int32_t DEFAULT_BATTERY_ID = 1;
std::optional<int32_t> InputDevice::getBatteryCapacity() {
    return mController ? mController->getBatteryCapacity(DEFAULT_BATTERY_ID) : std::nullopt;
}
+27 −27
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@

#include "../Macros.h"

#include "InputController.h"
#include "PeripheralController.h"
#include "input/NamedEnum.h"

// Log detailed debug messages about input device lights.
@@ -52,15 +52,15 @@ static inline int32_t toArgb(int32_t brightness, int32_t red, int32_t green, int
 * lights, getting and setting the lights brightness and color, by interacting with EventHub
 * devices.
 */
InputController::InputController(InputDeviceContext& deviceContext)
PeripheralController::PeripheralController(InputDeviceContext& deviceContext)
      : mDeviceContext(deviceContext) {
    configureBattries();
    configureLights();
}

InputController::~InputController() {}
PeripheralController::~PeripheralController() {}

std::optional<std::int32_t> InputController::Light::getRawLightBrightness(int32_t rawLightId) {
std::optional<std::int32_t> PeripheralController::Light::getRawLightBrightness(int32_t rawLightId) {
    std::optional<RawLightInfo> rawInfoOpt = context.getRawLightInfo(rawLightId);
    if (!rawInfoOpt.has_value()) {
        return std::nullopt;
@@ -85,7 +85,7 @@ std::optional<std::int32_t> InputController::Light::getRawLightBrightness(int32_
    return brightness;
}

void InputController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) {
void PeripheralController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) {
    std::optional<RawLightInfo> rawInfo = context.getRawLightInfo(rawLightId);
    if (!rawInfo.has_value()) {
        return;
@@ -104,14 +104,14 @@ void InputController::Light::setRawLightBrightness(int32_t rawLightId, int32_t b
    context.setLightBrightness(rawLightId, brightness);
}

bool InputController::SingleLight::setLightColor(int32_t color) {
bool PeripheralController::SingleLight::setLightColor(int32_t color) {
    int32_t brightness = getAlpha(color);
    setRawLightBrightness(rawId, brightness);

    return true;
}

bool InputController::RgbLight::setLightColor(int32_t color) {
bool PeripheralController::RgbLight::setLightColor(int32_t color) {
    // Compose color value as per:
    // https://developer.android.com/reference/android/graphics/Color?hl=en
    // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
@@ -137,7 +137,7 @@ bool InputController::RgbLight::setLightColor(int32_t color) {
    return true;
}

bool InputController::MultiColorLight::setLightColor(int32_t color) {
bool PeripheralController::MultiColorLight::setLightColor(int32_t color) {
    std::unordered_map<LightColor, int32_t> intensities;
    intensities.emplace(LightColor::RED, getRed(color));
    intensities.emplace(LightColor::GREEN, getGreen(color));
@@ -148,7 +148,7 @@ bool InputController::MultiColorLight::setLightColor(int32_t color) {
    return true;
}

std::optional<int32_t> InputController::SingleLight::getLightColor() {
std::optional<int32_t> PeripheralController::SingleLight::getLightColor() {
    std::optional<int32_t> brightness = getRawLightBrightness(rawId);
    if (!brightness.has_value()) {
        return std::nullopt;
@@ -157,7 +157,7 @@ std::optional<int32_t> InputController::SingleLight::getLightColor() {
    return toArgb(brightness.value(), 0 /* red */, 0 /* green */, 0 /* blue */);
}

std::optional<int32_t> InputController::RgbLight::getLightColor() {
std::optional<int32_t> PeripheralController::RgbLight::getLightColor() {
    // If the Alpha component is zero, then return color 0.
    if (brightness == 0) {
        return 0;
@@ -192,7 +192,7 @@ std::optional<int32_t> InputController::RgbLight::getLightColor() {
    return toArgb(brightness, red, green, blue);
}

std::optional<int32_t> InputController::MultiColorLight::getLightColor() {
std::optional<int32_t> PeripheralController::MultiColorLight::getLightColor() {
    auto ret = context.getLightIntensities(rawId);
    if (!ret.has_value()) {
        return std::nullopt;
@@ -210,7 +210,7 @@ std::optional<int32_t> InputController::MultiColorLight::getLightColor() {
    return std::nullopt;
}

bool InputController::PlayerIdLight::setLightPlayerId(int32_t playerId) {
bool PeripheralController::PlayerIdLight::setLightPlayerId(int32_t playerId) {
    if (rawLightIds.find(playerId) == rawLightIds.end()) {
        return false;
    }
@@ -224,7 +224,7 @@ bool InputController::PlayerIdLight::setLightPlayerId(int32_t playerId) {
    return true;
}

std::optional<int32_t> InputController::PlayerIdLight::getLightPlayerId() {
std::optional<int32_t> PeripheralController::PlayerIdLight::getLightPlayerId() {
    for (const auto& [id, rawId] : rawLightIds) {
        std::optional<int32_t> brightness = getRawLightBrightness(rawId);
        if (brightness.has_value() && brightness.value() > 0) {
@@ -234,11 +234,11 @@ std::optional<int32_t> InputController::PlayerIdLight::getLightPlayerId() {
    return std::nullopt;
}

void InputController::SingleLight::dump(std::string& dump) {
void PeripheralController::SingleLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
}

void InputController::PlayerIdLight::dump(std::string& dump) {
void PeripheralController::PlayerIdLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "PlayerId: %d\n", getLightPlayerId().value_or(-1));
    dump += StringPrintf(INDENT4 "Raw Player ID LEDs:");
    for (const auto& [id, rawId] : rawLightIds) {
@@ -247,7 +247,7 @@ void InputController::PlayerIdLight::dump(std::string& dump) {
    dump += "\n";
}

void InputController::RgbLight::dump(std::string& dump) {
void PeripheralController::RgbLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
    dump += StringPrintf(INDENT4 "Raw RGB LEDs: [%d, %d, %d] ", rawRgbIds.at(LightColor::RED),
                         rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE));
@@ -257,11 +257,11 @@ void InputController::RgbLight::dump(std::string& dump) {
    dump += "\n";
}

void InputController::MultiColorLight::dump(std::string& dump) {
void PeripheralController::MultiColorLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
}

void InputController::populateDeviceInfo(InputDeviceInfo* deviceInfo) {
void PeripheralController::populateDeviceInfo(InputDeviceInfo* deviceInfo) {
    // TODO: b/180733860 Remove this after enabling multi-battery
    if (!mBatteries.empty()) {
        deviceInfo->setHasBattery(true);
@@ -279,7 +279,7 @@ void InputController::populateDeviceInfo(InputDeviceInfo* deviceInfo) {
    }
}

void InputController::dump(std::string& dump) {
void PeripheralController::dump(std::string& dump) {
    dump += INDENT2 "Input Controller:\n";
    if (!mLights.empty()) {
        dump += INDENT3 "Lights:\n";
@@ -340,7 +340,7 @@ void InputController::dump(std::string& dump) {
    }
}

void InputController::configureBattries() {
void PeripheralController::configureBattries() {
    // Check raw batteries
    const std::vector<int32_t> rawBatteryIds = getDeviceContext().getRawBatteryIds();

@@ -355,7 +355,7 @@ void InputController::configureBattries() {
    }
}

void InputController::configureLights() {
void PeripheralController::configureLights() {
    bool hasRedLed = false;
    bool hasGreenLed = false;
    bool hasBlueLed = false;
@@ -472,15 +472,15 @@ void InputController::configureLights() {
    }
}

std::optional<int32_t> InputController::getBatteryCapacity(int batteryId) {
std::optional<int32_t> PeripheralController::getBatteryCapacity(int batteryId) {
    return getDeviceContext().getBatteryCapacity(batteryId);
}

std::optional<int32_t> InputController::getBatteryStatus(int batteryId) {
std::optional<int32_t> PeripheralController::getBatteryStatus(int batteryId) {
    return getDeviceContext().getBatteryStatus(batteryId);
}

bool InputController::setLightColor(int32_t lightId, int32_t color) {
bool PeripheralController::setLightColor(int32_t lightId, int32_t color) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return false;
@@ -493,7 +493,7 @@ bool InputController::setLightColor(int32_t lightId, int32_t color) {
    return light->setLightColor(color);
}

std::optional<int32_t> InputController::getLightColor(int32_t lightId) {
std::optional<int32_t> PeripheralController::getLightColor(int32_t lightId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return std::nullopt;
@@ -507,7 +507,7 @@ std::optional<int32_t> InputController::getLightColor(int32_t lightId) {
    return color;
}

bool InputController::setLightPlayerId(int32_t lightId, int32_t playerId) {
bool PeripheralController::setLightPlayerId(int32_t lightId, int32_t playerId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return false;
@@ -516,7 +516,7 @@ bool InputController::setLightPlayerId(int32_t lightId, int32_t playerId) {
    return light->setLightPlayerId(playerId);
}

std::optional<int32_t> InputController::getLightPlayerId(int32_t lightId) {
std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return std::nullopt;
+4 −4
Original line number Diff line number Diff line
@@ -17,19 +17,19 @@
#ifndef _UI_INPUTREADER_LIGHT_CONTROLLER_H
#define _UI_INPUTREADER_LIGHT_CONTROLLER_H

#include "InputControllerInterface.h"
#include "PeripheralControllerInterface.h"

namespace android {

class InputController : public InputControllerInterface {
class PeripheralController : public PeripheralControllerInterface {
    // Refer to https://developer.android.com/reference/kotlin/android/graphics/Color
    /* Number of colors : {red, green, blue} */
    static constexpr size_t COLOR_NUM = 3;
    static constexpr int32_t MAX_BRIGHTNESS = 0xff;

public:
    explicit InputController(InputDeviceContext& deviceContext);
    ~InputController() override;
    explicit PeripheralController(InputDeviceContext& deviceContext);
    ~PeripheralController() override;

    void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
    void dump(std::string& dump) override;
Loading