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

Commit 04278bb3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Use std::filesystem for input event devices scan." am: b70a4325

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

Change-Id: Id24304e48122fb5f2fbfbf6ce04fe5057d053c4c
parents 0f0af3a1 b70a4325
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -90,4 +90,7 @@ cc_library_shared {
    export_header_lib_headers: [
    export_header_lib_headers: [
        "libinputreader_headers",
        "libinputreader_headers",
    ],
    ],
    static_libs: [
        "libc++fs"
    ],
}
}
+33 −57
Original line number Original line Diff line number Diff line
@@ -34,20 +34,20 @@
#define LOG_TAG "EventHub"
#define LOG_TAG "EventHub"


// #define LOG_NDEBUG 0
// #define LOG_NDEBUG 0

#include "EventHub.h"

#include <android-base/stringprintf.h>
#include <android-base/stringprintf.h>
#include <cutils/properties.h>
#include <cutils/properties.h>
#include <input/KeyCharacterMap.h>
#include <input/KeyLayoutMap.h>
#include <input/VirtualKeyMap.h>
#include <openssl/sha.h>
#include <openssl/sha.h>
#include <utils/Errors.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Log.h>
#include <utils/Timers.h>
#include <utils/Timers.h>
#include <utils/threads.h>
#include <utils/threads.h>


#include <input/KeyCharacterMap.h>
#include <filesystem>
#include <input/KeyLayoutMap.h>

#include <input/VirtualKeyMap.h>
#include "EventHub.h"


/* this macro is used to tell if "bit" is set in "array"
/* this macro is used to tell if "bit" is set in "array"
 * it selects a byte from the array, and does a boolean AND
 * it selects a byte from the array, and does a boolean AND
@@ -94,8 +94,8 @@ static std::string sha1(const std::string& in) {
/**
/**
 * Return true if name matches "v4l-touch*"
 * Return true if name matches "v4l-touch*"
 */
 */
static bool isV4lTouchNode(const char* name) {
static bool isV4lTouchNode(std::string name) {
    return strstr(name, "v4l-touch") == name;
    return name.find("v4l-touch") != std::string::npos;
}
}


/**
/**
@@ -810,7 +810,7 @@ EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
    return index >= 0 ? mDevices.valueAt(index) : NULL;
    return index >= 0 ? mDevices.valueAt(index) : NULL;
}
}


EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const {
    for (size_t i = 0; i < mDevices.size(); i++) {
    for (size_t i = 0; i < mDevices.size(); i++) {
        Device* device = mDevices.valueAt(i);
        Device* device = mDevices.valueAt(i);
        if (device->path == devicePath) {
        if (device->path == devicePath) {
@@ -1215,14 +1215,14 @@ void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& vide
    }
    }
}
}


status_t EventHub::openDeviceLocked(const char* devicePath) {
status_t EventHub::openDeviceLocked(const std::string& devicePath) {
    char buffer[80];
    char buffer[80];


    ALOGV("Opening device: %s", devicePath);
    ALOGV("Opening device: %s", devicePath.c_str());


    int fd = open(devicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK);
    int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
    if (fd < 0) {
    if (fd < 0) {
        ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
        ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno));
        return -1;
        return -1;
    }
    }


@@ -1230,7 +1230,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {


    // Get device name.
    // Get device name.
    if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
    if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
        ALOGE("Could not get device name for %s: %s", devicePath, strerror(errno));
        ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno));
    } else {
    } else {
        buffer[sizeof(buffer) - 1] = '\0';
        buffer[sizeof(buffer) - 1] = '\0';
        identifier.name = buffer;
        identifier.name = buffer;
@@ -1240,7 +1240,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {
    for (size_t i = 0; i < mExcludedDevices.size(); i++) {
    for (size_t i = 0; i < mExcludedDevices.size(); i++) {
        const std::string& item = mExcludedDevices[i];
        const std::string& item = mExcludedDevices[i];
        if (identifier.name == item) {
        if (identifier.name == item) {
            ALOGI("ignoring event id %s driver %s\n", devicePath, item.c_str());
            ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str());
            close(fd);
            close(fd);
            return -1;
            return -1;
        }
        }
@@ -1249,7 +1249,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {
    // Get device driver version.
    // Get device driver version.
    int driverVersion;
    int driverVersion;
    if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
    if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
        ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
        ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno));
        close(fd);
        close(fd);
        return -1;
        return -1;
    }
    }
@@ -1257,7 +1257,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {
    // Get device identifier.
    // Get device identifier.
    struct input_id inputId;
    struct input_id inputId;
    if (ioctl(fd, EVIOCGID, &inputId)) {
    if (ioctl(fd, EVIOCGID, &inputId)) {
        ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
        ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno));
        close(fd);
        close(fd);
        return -1;
        return -1;
    }
    }
@@ -1289,7 +1289,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {
    int32_t deviceId = mNextDeviceId++;
    int32_t deviceId = mNextDeviceId++;
    Device* device = new Device(fd, deviceId, devicePath, identifier);
    Device* device = new Device(fd, deviceId, devicePath, identifier);


    ALOGV("add device %d: %s\n", deviceId, devicePath);
    ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
    ALOGV("  bus:        %04x\n"
    ALOGV("  bus:        %04x\n"
          "  vendor      %04x\n"
          "  vendor      %04x\n"
          "  product     %04x\n"
          "  product     %04x\n"
@@ -1446,7 +1446,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {


    // If the device isn't recognized as something we handle, don't monitor it.
    // If the device isn't recognized as something we handle, don't monitor it.
    if (device->classes == 0) {
    if (device->classes == 0) {
        ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath,
        ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(),
              device->identifier.name.c_str());
              device->identifier.name.c_str());
        delete device;
        delete device;
        return -1;
        return -1;
@@ -1492,7 +1492,7 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {


    ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
    ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
          "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
          "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
          deviceId, fd, devicePath, device->identifier.name.c_str(), device->classes,
          deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(), device->classes,
          device->configurationFile.c_str(), device->keyMap.keyLayoutFile.c_str(),
          device->configurationFile.c_str(), device->keyMap.keyLayoutFile.c_str(),
          device->keyMap.keyCharacterMapFile.c_str(), toString(mBuiltInKeyboardId == deviceId));
          device->keyMap.keyCharacterMapFile.c_str(), toString(mBuiltInKeyboardId == deviceId));


@@ -1724,13 +1724,13 @@ status_t EventHub::mapLed(Device* device, int32_t led, int32_t* outScanCode) con
    return NAME_NOT_FOUND;
    return NAME_NOT_FOUND;
}
}


void EventHub::closeDeviceByPathLocked(const char* devicePath) {
void EventHub::closeDeviceByPathLocked(const std::string& devicePath) {
    Device* device = getDeviceByPathLocked(devicePath);
    Device* device = getDeviceByPathLocked(devicePath);
    if (device) {
    if (device) {
        closeDeviceLocked(device);
        closeDeviceLocked(device);
        return;
        return;
    }
    }
    ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
    ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str());
}
}


/**
/**
@@ -1835,16 +1835,16 @@ status_t EventHub::readNotifyLocked() {
        event = (struct inotify_event*)(event_buf + event_pos);
        event = (struct inotify_event*)(event_buf + event_pos);
        if (event->len) {
        if (event->len) {
            if (event->wd == mInputWd) {
            if (event->wd == mInputWd) {
                std::string filename = StringPrintf("%s/%s", DEVICE_PATH, event->name);
                std::string filename = std::string(DEVICE_PATH) + "/" + event->name;
                if (event->mask & IN_CREATE) {
                if (event->mask & IN_CREATE) {
                    openDeviceLocked(filename.c_str());
                    openDeviceLocked(filename);
                } else {
                } else {
                    ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
                    ALOGI("Removing device '%s' due to inotify event\n", filename.c_str());
                    closeDeviceByPathLocked(filename.c_str());
                    closeDeviceByPathLocked(filename);
                }
                }
            } else if (event->wd == mVideoWd) {
            } else if (event->wd == mVideoWd) {
                if (isV4lTouchNode(event->name)) {
                if (isV4lTouchNode(event->name)) {
                    std::string filename = StringPrintf("%s/%s", VIDEO_DEVICE_PATH, event->name);
                    std::string filename = std::string(VIDEO_DEVICE_PATH) + "/" + event->name;
                    if (event->mask & IN_CREATE) {
                    if (event->mask & IN_CREATE) {
                        openVideoDeviceLocked(filename);
                        openVideoDeviceLocked(filename);
                    } else {
                    } else {
@@ -1863,24 +1863,10 @@ status_t EventHub::readNotifyLocked() {
    return 0;
    return 0;
}
}


status_t EventHub::scanDirLocked(const char* dirname) {
status_t EventHub::scanDirLocked(const std::string& dirname) {
    char devname[PATH_MAX];
    for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
    char* filename;
        openDeviceLocked(entry.path());
    DIR* dir;
    struct dirent* de;
    dir = opendir(dirname);
    if (dir == nullptr) return -1;
    strcpy(devname, dirname);
    filename = devname + strlen(devname);
    *filename++ = '/';
    while ((de = readdir(dir))) {
        if (de->d_name[0] == '.' &&
            (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
            continue;
        strcpy(filename, de->d_name);
        openDeviceLocked(devname);
    }
    }
    closedir(dir);
    return 0;
    return 0;
}
}


@@ -1888,22 +1874,12 @@ status_t EventHub::scanDirLocked(const char* dirname) {
 * Look for all dirname/v4l-touch* devices, and open them.
 * Look for all dirname/v4l-touch* devices, and open them.
 */
 */
status_t EventHub::scanVideoDirLocked(const std::string& dirname) {
status_t EventHub::scanVideoDirLocked(const std::string& dirname) {
    DIR* dir;
    for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
    struct dirent* de;
        if (isV4lTouchNode(entry.path())) {
    dir = opendir(dirname.c_str());
            ALOGI("Found touch video device %s", entry.path().c_str());
    if (!dir) {
            openVideoDeviceLocked(entry.path());
        ALOGE("Could not open video directory %s", dirname.c_str());
        return BAD_VALUE;
    }

    while ((de = readdir(dir))) {
        const char* name = de->d_name;
        if (isV4lTouchNode(name)) {
            ALOGI("Found touch video device %s", name);
            openVideoDeviceLocked(dirname + "/" + name);
        }
        }
    }
    }
    closedir(dir);
    return OK;
    return OK;
}
}


+4 −4
Original line number Original line Diff line number Diff line
@@ -373,13 +373,13 @@ private:
        }
        }
    };
    };


    status_t openDeviceLocked(const char* devicePath);
    status_t openDeviceLocked(const std::string& devicePath);
    void openVideoDeviceLocked(const std::string& devicePath);
    void openVideoDeviceLocked(const std::string& devicePath);
    void createVirtualKeyboardLocked();
    void createVirtualKeyboardLocked();
    void addDeviceLocked(Device* device);
    void addDeviceLocked(Device* device);
    void assignDescriptorLocked(InputDeviceIdentifier& identifier);
    void assignDescriptorLocked(InputDeviceIdentifier& identifier);


    void closeDeviceByPathLocked(const char* devicePath);
    void closeDeviceByPathLocked(const std::string& devicePath);
    void closeVideoDeviceByPathLocked(const std::string& devicePath);
    void closeVideoDeviceByPathLocked(const std::string& devicePath);
    void closeDeviceLocked(Device* device);
    void closeDeviceLocked(Device* device);
    void closeAllDevicesLocked();
    void closeAllDevicesLocked();
@@ -396,14 +396,14 @@ private:
    status_t unregisterDeviceFromEpollLocked(Device* device);
    status_t unregisterDeviceFromEpollLocked(Device* device);
    void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);
    void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);


    status_t scanDirLocked(const char* dirname);
    status_t scanDirLocked(const std::string& dirname);
    status_t scanVideoDirLocked(const std::string& dirname);
    status_t scanVideoDirLocked(const std::string& dirname);
    void scanDevicesLocked();
    void scanDevicesLocked();
    status_t readNotifyLocked();
    status_t readNotifyLocked();


    Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
    Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
    Device* getDeviceLocked(int32_t deviceId) const;
    Device* getDeviceLocked(int32_t deviceId) const;
    Device* getDeviceByPathLocked(const char* devicePath) const;
    Device* getDeviceByPathLocked(const std::string& devicePath) const;
    /**
    /**
     * Look through all available fd's (both for input devices and for video devices),
     * Look through all available fd's (both for input devices and for video devices),
     * and return the device pointer.
     * and return the device pointer.
+3 −0
Original line number Original line Diff line number Diff line
@@ -46,5 +46,8 @@ cc_test {
        "InputReader_test.cpp",
        "InputReader_test.cpp",
        "UinputDevice.cpp",
        "UinputDevice.cpp",
    ],
    ],
    static_libs: [
        "libc++fs"
    ],
    require_root: true,
    require_root: true,
}
}