InputHost mInputHost;
std::shared_ptr<InputDeviceManager> mDeviceManager;
- std::shared_ptr<InputHub> mInputHub;
+ std::unique_ptr<InputHub> mInputHub;
std::thread mPollThread;
};
-static std::shared_ptr<EvdevModule> gEvdevModule;
+static std::unique_ptr<EvdevModule> gEvdevModule;
EvdevModule::EvdevModule(InputHost inputHost) :
mInputHost(inputHost),
mDeviceManager(std::make_shared<InputDeviceManager>()),
- mInputHub(std::make_shared<InputHub>(mDeviceManager)) {}
+ mInputHub(std::make_unique<InputHub>(mDeviceManager)) {}
void EvdevModule::init() {
ALOGV("%s", __func__);
input_host_t* host, input_host_callbacks_t cb) {
LOG_ALWAYS_FATAL_IF(strcmp(module->common.id, INPUT_HARDWARE_MODULE_ID) != 0);
InputHost inputHost = {host, cb};
- gEvdevModule = std::make_shared<EvdevModule>(inputHost);
+ gEvdevModule = std::make_unique<EvdevModule>(inputHost);
gEvdevModule->init();
}
namespace android {
-EvdevDevice::EvdevDevice(std::shared_ptr<InputDeviceNode> node) :
+EvdevDevice::EvdevDevice(const std::shared_ptr<InputDeviceNode>& node) :
mDeviceNode(node) {}
void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
*/
class EvdevDevice : public InputDeviceInterface {
public:
- explicit EvdevDevice(std::shared_ptr<InputDeviceNode> node);
+ explicit EvdevDevice(const std::shared_ptr<InputDeviceNode>& node);
virtual ~EvdevDevice() override = default;
virtual void processInput(InputEvent& event, nsecs_t currentTime) override;
namespace android {
-void InputDeviceManager::onInputEvent(std::shared_ptr<InputDeviceNode> node, InputEvent& event,
+void InputDeviceManager::onInputEvent(const std::shared_ptr<InputDeviceNode>& node, InputEvent& event,
nsecs_t event_time) {
if (mDevices[node] == nullptr) {
ALOGE("got input event for unknown node %s", node->getPath().c_str());
mDevices[node]->processInput(event, event_time);
}
-void InputDeviceManager::onDeviceAdded(std::shared_ptr<InputDeviceNode> node) {
+void InputDeviceManager::onDeviceAdded(const std::shared_ptr<InputDeviceNode>& node) {
mDevices[node] = std::make_shared<EvdevDevice>(node);
}
-void InputDeviceManager::onDeviceRemoved(std::shared_ptr<InputDeviceNode> node) {
+void InputDeviceManager::onDeviceRemoved(const std::shared_ptr<InputDeviceNode>& node) {
if (mDevices[node] == nullptr) {
ALOGE("could not remove unknown node %s", node->getPath().c_str());
return;
public:
virtual ~InputDeviceManager() override = default;
- virtual void onInputEvent(std::shared_ptr<InputDeviceNode> node, InputEvent& event,
+ virtual void onInputEvent(const std::shared_ptr<InputDeviceNode>& node, InputEvent& event,
nsecs_t event_time) override;
- virtual void onDeviceAdded(std::shared_ptr<InputDeviceNode> node) override;
- virtual void onDeviceRemoved(std::shared_ptr<InputDeviceNode> node) override;
+ virtual void onDeviceAdded(const std::shared_ptr<InputDeviceNode>& node) override;
+ virtual void onDeviceRemoved(const std::shared_ptr<InputDeviceNode>& node) override;
private:
template<class T, class U>
}
}
-InputHub::InputHub(std::shared_ptr<InputCallbackInterface> cb) :
+InputHub::InputHub(const std::shared_ptr<InputCallbackInterface>& cb) :
mInputCallback(cb) {
// Determine the type of suspend blocking we can do on this device. There
// are 3 options, in decreasing order of preference:
ALOGV("inotify event for path %s", path.c_str());
if (event->mask & IN_CREATE) {
- std::shared_ptr<InputDeviceNode> deviceNode;
- status_t res = openNode(path, &deviceNode);
- if (res != OK) {
+ auto deviceNode = openNode(path);
+ if (deviceNode == nullptr) {
ALOGE("could not open device node %s. err=%d", path.c_str(), res);
} else {
mInputCallback->onDeviceAdded(deviceNode);
} else {
auto deviceNode = findNodeByPath(path);
if (deviceNode != nullptr) {
- status_t ret = closeNode(deviceNode);
+ status_t ret = closeNode(deviceNode.get());
if (ret != OK) {
ALOGW("Could not close device %s. errno=%d", path.c_str(), ret);
} else {
continue;
}
std::string filename = path + "/" + dirent->d_name;
- std::shared_ptr<InputDeviceNode> node;
- if (openNode(filename, &node) != OK) {
+ auto node = openNode(filename);
+ if (node == nullptr) {
ALOGE("could not open device node %s", filename.c_str());
} else {
mInputCallback->onDeviceAdded(node);
return OK;
}
-status_t InputHub::openNode(const std::string& path,
- std::shared_ptr<InputDeviceNode>* outNode) {
+std::shared_ptr<InputDeviceNode> InputHub::openNode(const std::string& path) {
ALOGV("opening %s...", path.c_str());
auto evdevNode = std::shared_ptr<EvdevDeviceNode>(EvdevDeviceNode::openDeviceNode(path));
if (evdevNode == nullptr) {
- return UNKNOWN_ERROR;
+ return nullptr;
}
auto fd = evdevNode->getFd();
ALOGV("opened %s with fd %d", path.c_str(), fd);
- *outNode = std::static_pointer_cast<InputDeviceNode>(evdevNode);
- mDeviceNodes[fd] = *outNode;
+ mDeviceNodes[fd] = evdevNode;
struct epoll_event eventItem{};
eventItem.events = EPOLLIN;
if (mWakeupMechanism == WakeMechanism::EPOLL_WAKEUP) {
eventItem.data.u32 = fd;
if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
- return -errno;
+ return nullptr;
}
if (mNeedToCheckSuspendBlockIoctl) {
mNeedToCheckSuspendBlockIoctl = false;
}
- return OK;
+ return evdevNode;
}
-status_t InputHub::closeNode(const std::shared_ptr<InputDeviceNode>& node) {
+status_t InputHub::closeNode(const InputDeviceNode* node) {
for (auto pair : mDeviceNodes) {
- if (pair.second.get() == node.get()) {
+ if (pair.second.get() == node) {
return closeNodeByFd(pair.first);
}
}
/** Callback interface for receiving input events, including device changes. */
class InputCallbackInterface {
public:
- virtual void onInputEvent(std::shared_ptr<InputDeviceNode> node, InputEvent& event,
+ virtual void onInputEvent(const std::shared_ptr<InputDeviceNode>& node, InputEvent& event,
nsecs_t event_time) = 0;
- virtual void onDeviceAdded(std::shared_ptr<InputDeviceNode> node) = 0;
- virtual void onDeviceRemoved(std::shared_ptr<InputDeviceNode> node) = 0;
+ virtual void onDeviceAdded(const std::shared_ptr<InputDeviceNode>& node) = 0;
+ virtual void onDeviceRemoved(const std::shared_ptr<InputDeviceNode>& node) = 0;
protected:
InputCallbackInterface() = default;
*/
class InputHub : public InputHubInterface {
public:
- explicit InputHub(std::shared_ptr<InputCallbackInterface> cb);
+ explicit InputHub(const std::shared_ptr<InputCallbackInterface>& cb);
virtual ~InputHub() override;
virtual status_t registerDevicePath(const std::string& path) override;
private:
status_t readNotify();
status_t scanDir(const std::string& path);
- status_t openNode(const std::string& path, std::shared_ptr<InputDeviceNode>* outNode);
- status_t closeNode(const std::shared_ptr<InputDeviceNode>& node);
+ std::shared_ptr<InputDeviceNode> openNode(const std::string& path);
+ status_t closeNode(const InputDeviceNode* node);
status_t closeNodeByFd(int fd);
std::shared_ptr<InputDeviceNode> findNodeByPath(const std::string& path);
using namespace std::literals::chrono_literals;
-using InputCbFunc = std::function<void(std::shared_ptr<InputDeviceNode>, InputEvent&, nsecs_t)>;
-using DeviceCbFunc = std::function<void(std::shared_ptr<InputDeviceNode>)>;
+using InputCbFunc = std::function<void(const std::shared_ptr<InputDeviceNode>&, InputEvent&, nsecs_t)>;
+using DeviceCbFunc = std::function<void(const std::shared_ptr<InputDeviceNode>&)>;
-static const InputCbFunc kNoopInputCb = [](std::shared_ptr<InputDeviceNode>, InputEvent&, nsecs_t){};
-static const DeviceCbFunc kNoopDeviceCb = [](std::shared_ptr<InputDeviceNode>){};
+static const InputCbFunc kNoopInputCb = [](const std::shared_ptr<InputDeviceNode>&, InputEvent&, nsecs_t){};
+static const DeviceCbFunc kNoopDeviceCb = [](const std::shared_ptr<InputDeviceNode>&){};
class TestInputCallback : public InputCallbackInterface {
public:
void setDeviceAddedCallback(DeviceCbFunc cb) { mDeviceAddedCb = cb; }
void setDeviceRemovedCallback(DeviceCbFunc cb) { mDeviceRemovedCb = cb; }
- virtual void onInputEvent(std::shared_ptr<InputDeviceNode> node, InputEvent& event,
+ virtual void onInputEvent(const std::shared_ptr<InputDeviceNode>& node, InputEvent& event,
nsecs_t event_time) override {
mInputCb(node, event, event_time);
}
- virtual void onDeviceAdded(std::shared_ptr<InputDeviceNode> node) override {
+ virtual void onDeviceAdded(const std::shared_ptr<InputDeviceNode>& node) override {
mDeviceAddedCb(node);
}
- virtual void onDeviceRemoved(std::shared_ptr<InputDeviceNode> node) override {
+ virtual void onDeviceRemoved(const std::shared_ptr<InputDeviceNode>& node) override {
mDeviceRemovedCb(node);
}
std::string pathname;
// Expect that this callback will run and set handle and pathname.
mCallback->setDeviceAddedCallback(
- [&](std::shared_ptr<InputDeviceNode> node) {
+ [&](const std::shared_ptr<InputDeviceNode>& node) {
pathname = node->getPath();
});
std::shared_ptr<InputDeviceNode> tempNode;
// Expect that these callbacks will run for the above device file.
mCallback->setDeviceAddedCallback(
- [&](std::shared_ptr<InputDeviceNode> node) {
+ [&](const std::shared_ptr<InputDeviceNode>& node) {
tempNode = node;
});
mCallback->setDeviceRemovedCallback(
- [&](std::shared_ptr<InputDeviceNode> node) {
+ [&](const std::shared_ptr<InputDeviceNode>& node) {
EXPECT_EQ(tempNode, node);
});
// Expect this callback to run when the input event is read.
nsecs_t expectedWhen = systemTime(CLOCK_MONOTONIC) + ms2ns(inputDelayMs.count());
mCallback->setInputCallback(
- [&](std::shared_ptr<InputDeviceNode> node, InputEvent& event, nsecs_t event_time) {
+ [&](const std::shared_ptr<InputDeviceNode>& node, InputEvent& event,
+ nsecs_t event_time) {
EXPECT_NEAR(expectedWhen, event_time, ms2ns(TIMING_TOLERANCE_MS));
EXPECT_EQ(s2ns(1), event.when);
EXPECT_EQ(tempFileName, node->getPath());
// Setup the callback for input events. Should run before the device
// callback.
mCallback->setInputCallback(
- [&](std::shared_ptr<InputDeviceNode>, InputEvent&, nsecs_t) {
+ [&](const std::shared_ptr<InputDeviceNode>&, InputEvent&, nsecs_t) {
ASSERT_FALSE(deviceCallbackFinished);
inputCallbackFinished = true;
});
// Setup the callback for device removal. Should run after the input
// callback.
mCallback->setDeviceRemovedCallback(
- [&](std::shared_ptr<InputDeviceNode> node) {
+ [&](const std::shared_ptr<InputDeviceNode>& node) {
ASSERT_TRUE(inputCallbackFinished)
<< "input callback did not run before device changed callback";
// Make sure the correct device was removed.