From 1eaaa955de899e8c6076be3a7bd1d8855e7ce682 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Wed, 26 Dec 2012 17:19:54 +0100 Subject: implemented coldplugging --- inputdev.c | 52 +++++++++++++++++++++++++++++++++++++++++++++------- inputdev.h | 3 ++- plugin.c | 6 ++++-- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/inputdev.c b/inputdev.c index ee1e947..f800425 100644 --- a/inputdev.c +++ b/inputdev.c @@ -570,16 +570,17 @@ void cInputDeviceController::remove_device(class cInputDevice *dev) dev_mutex_.Unlock(); } -void cInputDeviceController::add_device(char const *dev_name) +bool cInputDeviceController::add_device(char const *dev_name) { class cInputDevice *dev = new cInputDevice(*this, cString::sprintf("/dev/input/%s", dev_name)); char const *desc; + bool res; if (!dev->open()) { delete dev; - return; + return false; } desc = dev->get_description(); @@ -606,8 +607,14 @@ void cInputDeviceController::add_device(char const *dev_name) } } - if (dev != NULL && !dev->start(fd_epoll_)) + if (dev != NULL && !dev->start(fd_epoll_)) { + res = false; remove_device(dev); + } else { + res = dev != NULL; + } + + return res; } void cInputDeviceController::handle_uevent(void) @@ -653,15 +660,46 @@ void cInputDeviceController::handle_uevent(void) } } -bool cInputDeviceController::start(void) +bool cInputDeviceController::coldplug_devices(char const *path) { - cThread::Start(); - return true; + cReadDir cdir(path); + bool res = true; + + for (;;) { + struct dirent const *ent = cdir.Next(); + + if (!ent) + break; + + if (strcmp(ent->d_name, ".") == 0 || + strcmp(ent->d_name, "..") == 0) + continue; + + if (!add_device(ent->d_name)) { + esyslog("%s: failed to coldplug '%s'\n", + plugin_name(), ent->d_name); + res = false; + } else { + isyslog("%s: coldplugged '%s'\n", + plugin_name(), ent->d_name); + } + } + + return res; } -bool cInputDeviceController::initialize(void) +bool cInputDeviceController::initialize(char const *coldplug_dir) { cInputDevice::install_keymap(Name()); + + coldplug_devices(coldplug_dir); + + return true; +} + +bool cInputDeviceController::start(void) +{ + cThread::Start(); return true; } diff --git a/inputdev.h b/inputdev.h index a483601..847ff5a 100644 --- a/inputdev.h +++ b/inputdev.h @@ -40,6 +40,7 @@ private: void cleanup_devices(void); void handle_uevent(void); + bool coldplug_devices(char const *); protected: virtual void Action(void); @@ -48,7 +49,7 @@ public: explicit cInputDeviceController(cPlugin &p); virtual ~cInputDeviceController(); - bool initialize(void); + bool initialize(char const *coldplug_dir); bool start(void); void stop(void); diff --git a/plugin.c b/plugin.c index 346d8a9..8e769b0 100644 --- a/plugin.c +++ b/plugin.c @@ -39,6 +39,8 @@ private: int idx; } socket_; + cString coldplug_dir; + public: cInputDevicePlugin(void); virtual ~cInputDevicePlugin(); @@ -53,7 +55,7 @@ public: }; cInputDevicePlugin::cInputDevicePlugin() : - controller_(NULL) + controller_(NULL), coldplug_dir("/dev/vdr/input") { } @@ -134,7 +136,7 @@ bool cInputDevicePlugin::Initialize(void) } if (is_ok) - is_ok = controller_->initialize(); + is_ok = controller_->initialize(coldplug_dir); if (!is_ok) { delete controller_; -- cgit v1.2.3