Skip to content

Commit df31be1

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "EventHub: Search IDC for additional lights" into main
2 parents d6e8320 + 36b1159 commit df31be1

2 files changed

Lines changed: 70 additions & 42 deletions

File tree

services/inputflinger/reader/EventHub.cpp

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ static std::optional<std::array<LightColor, COLOR_NUM>> getColorIndexArray(
351351
return colors;
352352
}
353353

354+
static base::Result<std::shared_ptr<PropertyMap>> loadConfiguration(
355+
const InputDeviceIdentifier& ident) {
356+
std::string configurationFile =
357+
getInputDeviceConfigurationFilePathByDeviceIdentifier(ident,
358+
InputDeviceConfigurationFileType::
359+
CONFIGURATION);
360+
if (configurationFile.empty()) {
361+
ALOGD("No input device configuration file found for device '%s'.", ident.name.c_str());
362+
return base::Result<std::shared_ptr<PropertyMap>>(nullptr);
363+
}
364+
base::Result<std::shared_ptr<PropertyMap>> propertyMap =
365+
PropertyMap::load(configurationFile.c_str());
366+
367+
return propertyMap;
368+
}
369+
354370
/**
355371
* Read country code information exposed through the sysfs path and convert it to Layout info.
356372
*/
@@ -409,11 +425,22 @@ static std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> readBatteryConf
409425
* Read information about lights exposed through the sysfs path.
410426
*/
411427
static std::unordered_map<int32_t /*lightId*/, RawLightInfo> readLightsConfiguration(
412-
const std::filesystem::path& sysfsRootPath) {
428+
const std::filesystem::path& sysfsRootPath, const std::shared_ptr<PropertyMap>& config) {
413429
std::unordered_map<int32_t, RawLightInfo> lightInfos;
414430
int32_t nextLightId = 0;
415-
// Check if device has any lights.
416-
const auto& paths = findSysfsNodes(sysfsRootPath, SysfsClass::LEDS);
431+
// Check if device has any lights. If the Input Device Configuration file specifies any lights,
432+
// use those in addition to searching the device node itself for lights.
433+
std::vector<std::filesystem::path> paths = findSysfsNodes(sysfsRootPath, SysfsClass::LEDS);
434+
435+
if (config) {
436+
auto additionalLights = config->getString("device.additionalSysfsLedsNode");
437+
if (additionalLights) {
438+
ALOGI("IDC specifies additional path for lights at '%s'",
439+
additionalLights.value().c_str());
440+
paths.push_back(std::filesystem::path(additionalLights.value()));
441+
}
442+
}
443+
417444
for (const auto& nodePath : paths) {
418445
RawLightInfo info;
419446
info.id = ++nextLightId;
@@ -532,17 +559,16 @@ std::ostream& operator<<(std::ostream& out, const std::optional<RawAbsoluteAxisI
532559
// --- EventHub::Device ---
533560

534561
EventHub::Device::Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
535-
std::shared_ptr<const AssociatedDevice> assocDev)
562+
std::shared_ptr<PropertyMap> config)
536563
: fd(fd),
537564
id(id),
538565
path(std::move(path)),
539566
identifier(std::move(identifier)),
540567
classes(0),
541-
configuration(nullptr),
568+
configuration(std::move(config)),
542569
virtualKeyMap(nullptr),
543570
ffEffectPlaying(false),
544571
ffEffectId(-1),
545-
associatedDevice(std::move(assocDev)),
546572
controllerNumber(0),
547573
enabled(true),
548574
isVirtual(fd < 0),
@@ -696,26 +722,6 @@ bool EventHub::Device::hasKeycodeInternalLocked(int keycode) const {
696722
return false;
697723
}
698724

699-
void EventHub::Device::loadConfigurationLocked() {
700-
configurationFile =
701-
getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier,
702-
InputDeviceConfigurationFileType::
703-
CONFIGURATION);
704-
if (configurationFile.empty()) {
705-
ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str());
706-
} else {
707-
android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
708-
PropertyMap::load(configurationFile.c_str());
709-
if (!propertyMap.ok()) {
710-
ALOGE("Error loading input device configuration file for device '%s'. "
711-
"Using default configuration.",
712-
identifier.name.c_str());
713-
} else {
714-
configuration = std::move(*propertyMap);
715-
}
716-
}
717-
}
718-
719725
bool EventHub::Device::loadVirtualKeyMapLocked() {
720726
// The virtual key map is supplied by the kernel as a system board property file.
721727
std::string propPath = "/sys/board_properties/virtualkeys.";
@@ -1611,7 +1617,7 @@ void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
16111617
}
16121618

16131619
std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDeviceLocked(
1614-
const std::filesystem::path& devicePath) const {
1620+
const std::filesystem::path& devicePath, const std::shared_ptr<PropertyMap>& config) const {
16151621
const std::optional<std::filesystem::path> sysfsRootPathOpt =
16161622
getSysfsRootPath(devicePath.c_str());
16171623
if (!sysfsRootPathOpt) {
@@ -1628,8 +1634,13 @@ std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDevi
16281634
if (!associatedDevice) {
16291635
// Found matching associated device for the first time.
16301636
associatedDevice = dev->associatedDevice;
1631-
// Reload this associated device if needed.
1632-
const auto reloadedDevice = AssociatedDevice(path);
1637+
// Reload this associated device if needed. Use the base device
1638+
// config. Note that this will essentially arbitrarily pick one
1639+
// Device as the base for the AssociatedDevice configuration. If
1640+
// there are multiple Device's that have a configuration for the
1641+
// AssociatedDevice, only one configuration will be chosen and will
1642+
// be used for all other AssociatedDevices for the same sysfs path.
1643+
const auto reloadedDevice = AssociatedDevice(path, associatedDevice->baseDevConfig);
16331644
if (reloadedDevice != *dev->associatedDevice) {
16341645
ALOGI("The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s",
16351646
path.c_str(), associatedDevice->dump().c_str());
@@ -1642,16 +1653,18 @@ std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDevi
16421653

16431654
if (!associatedDevice) {
16441655
// No existing associated device found for this path, so create a new one.
1645-
associatedDevice = std::make_shared<AssociatedDevice>(path);
1656+
associatedDevice = std::make_shared<AssociatedDevice>(path, config);
16461657
}
16471658

16481659
return associatedDevice;
16491660
}
16501661

1651-
EventHub::AssociatedDevice::AssociatedDevice(const std::filesystem::path& sysfsRootPath)
1662+
EventHub::AssociatedDevice::AssociatedDevice(const std::filesystem::path& sysfsRootPath,
1663+
std::shared_ptr<PropertyMap> config)
16521664
: sysfsRootPath(sysfsRootPath),
1665+
baseDevConfig(std::move(config)),
16531666
batteryInfos(readBatteryConfiguration(sysfsRootPath)),
1654-
lightInfos(readLightsConfiguration(sysfsRootPath)),
1667+
lightInfos(readLightsConfiguration(sysfsRootPath, baseDevConfig)),
16551668
layoutInfo(readLayoutConfiguration(sysfsRootPath)) {}
16561669

16571670
std::string EventHub::AssociatedDevice::dump() const {
@@ -2337,11 +2350,21 @@ void EventHub::openDeviceLocked(const std::string& devicePath) {
23372350
// Fill in the descriptor.
23382351
assignDescriptorLocked(identifier);
23392352

2353+
// Load the configuration file for the device.
2354+
std::shared_ptr<PropertyMap> configuration = nullptr;
2355+
base::Result<std::shared_ptr<PropertyMap>> propertyMapResult = loadConfiguration(identifier);
2356+
if (!propertyMapResult.ok()) {
2357+
ALOGE("Error loading input device configuration file for device '%s'. "
2358+
"Using default configuration. Error: %s",
2359+
identifier.name.c_str(), propertyMapResult.error().message().c_str());
2360+
} else {
2361+
configuration = propertyMapResult.value();
2362+
}
2363+
23402364
// Allocate device. (The device object takes ownership of the fd at this point.)
23412365
int32_t deviceId = mNextDeviceId++;
23422366
std::unique_ptr<Device> device =
2343-
std::make_unique<Device>(fd, deviceId, devicePath, identifier,
2344-
obtainAssociatedDeviceLocked(devicePath));
2367+
std::make_unique<Device>(fd, deviceId, devicePath, identifier, configuration);
23452368

23462369
ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
23472370
ALOGV(" bus: %04x\n"
@@ -2356,8 +2379,8 @@ void EventHub::openDeviceLocked(const std::string& devicePath) {
23562379
ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff,
23572380
driverVersion & 0xff);
23582381

2359-
// Load the configuration file for the device.
2360-
device->loadConfigurationLocked();
2382+
// Obtain the associated device, if any.
2383+
device->associatedDevice = obtainAssociatedDeviceLocked(devicePath, device->configuration);
23612384

23622385
// Figure out the kinds of events the device reports.
23632386
device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask);
@@ -2664,7 +2687,8 @@ void EventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
26642687
testedDevices.emplace(dev.associatedDevice, false);
26652688
return false;
26662689
}
2667-
auto reloadedDevice = AssociatedDevice(dev.associatedDevice->sysfsRootPath);
2690+
auto reloadedDevice = AssociatedDevice(dev.associatedDevice->sysfsRootPath,
2691+
dev.associatedDevice->baseDevConfig);
26682692
const bool changed = *dev.associatedDevice != reloadedDevice;
26692693
testedDevices.emplace(dev.associatedDevice, changed);
26702694
return changed;

services/inputflinger/reader/include/EventHub.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <filesystem>
2222
#include <functional>
2323
#include <map>
24+
#include <memory>
2425
#include <optional>
2526
#include <ostream>
2627
#include <string>
@@ -619,9 +620,12 @@ class EventHub : public EventHubInterface {
619620
private:
620621
// Holds information about the sysfs device associated with the Device.
621622
struct AssociatedDevice {
622-
AssociatedDevice(const std::filesystem::path& sysfsRootPath);
623+
AssociatedDevice(const std::filesystem::path& sysfsRootPath,
624+
std::shared_ptr<PropertyMap> baseDevConfig);
623625
// The sysfs root path of the misc device.
624626
std::filesystem::path sysfsRootPath;
627+
// The configuration of the base device.
628+
std::shared_ptr<PropertyMap> baseDevConfig;
625629
std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;
626630
std::unordered_map<int32_t /*lightId*/, RawLightInfo> lightInfos;
627631
std::optional<RawLayoutInfo> layoutInfo;
@@ -658,7 +662,7 @@ class EventHub : public EventHubInterface {
658662
std::map<int /*axis*/, AxisState> absState;
659663

660664
std::string configurationFile;
661-
std::unique_ptr<PropertyMap> configuration;
665+
std::shared_ptr<PropertyMap> configuration;
662666
std::unique_ptr<VirtualKeyMap> virtualKeyMap;
663667
KeyMap keyMap;
664668

@@ -672,7 +676,7 @@ class EventHub : public EventHubInterface {
672676
int32_t controllerNumber;
673677

674678
Device(int fd, int32_t id, std::string path, InputDeviceIdentifier identifier,
675-
std::shared_ptr<const AssociatedDevice> assocDev);
679+
std::shared_ptr<PropertyMap> config);
676680
~Device();
677681

678682
void close();
@@ -692,7 +696,6 @@ class EventHub : public EventHubInterface {
692696
void populateAbsoluteAxisStates();
693697
bool hasKeycodeLocked(int keycode) const;
694698
bool hasKeycodeInternalLocked(int keycode) const;
695-
void loadConfigurationLocked();
696699
bool loadVirtualKeyMapLocked();
697700
status_t loadKeyMapLocked();
698701
bool isExternalDeviceLocked();
@@ -724,7 +727,8 @@ class EventHub : public EventHubInterface {
724727
void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
725728
void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);
726729
std::shared_ptr<const AssociatedDevice> obtainAssociatedDeviceLocked(
727-
const std::filesystem::path& devicePath) const REQUIRES(mLock);
730+
const std::filesystem::path& devicePath,
731+
const std::shared_ptr<PropertyMap>& config) const REQUIRES(mLock);
728732

729733
void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
730734
void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);

0 commit comments

Comments
 (0)