diff options
author | Javier <dev.git@javispedro.com> | 2015-06-07 21:22:45 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2015-06-07 21:22:45 +0200 |
commit | a69e97943539a8abc4d2762638c169dc19c88516 (patch) | |
tree | f3516ea29745db65971247cee4c260b49f1067b2 /smartpenmanager.cc | |
download | scribiu-a69e97943539a8abc4d2762638c169dc19c88516.tar.gz scribiu-a69e97943539a8abc4d2762638c169dc19c88516.zip |
initial import
Diffstat (limited to 'smartpenmanager.cc')
-rw-r--r-- | smartpenmanager.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/smartpenmanager.cc b/smartpenmanager.cc new file mode 100644 index 0000000..d560b5b --- /dev/null +++ b/smartpenmanager.cc @@ -0,0 +1,69 @@ +#include <QtCore/QDebug> +#include <libudev.h> + +#include "smartpenmanager.h" + +SmartpenManager::SmartpenManager(QObject *parent) + : QObject(parent), _udev(udev_new()), _monitor(udev_monitor_new_from_netlink(_udev, "udev")), + _notifier(new QSocketNotifier(udev_monitor_get_fd(_monitor), QSocketNotifier::Read)) +{ + udev_monitor_filter_add_match_tag(_monitor, "livescribe-pen"); + + connect(_notifier, SIGNAL(activated(int)), SLOT(handleMonitorActivity())); + + udev_monitor_enable_receiving(_monitor); + + udev_enumerate *scan = udev_enumerate_new(_udev); + udev_enumerate_add_match_tag(scan, "livescribe-pen"); + + if (udev_enumerate_scan_devices(scan) == 0) { + udev_list_entry *l = udev_enumerate_get_list_entry(scan), *i; + udev_list_entry_foreach(i, l) { + const char *path = udev_list_entry_get_name(i); + udev_device *dev = udev_device_new_from_syspath(_udev, path); + processDevice(dev); + udev_device_unref(dev); + } + } else { + qWarning() << "Failed to scan for devices"; + } + + udev_enumerate_unref(scan); +} + +SmartpenManager::~SmartpenManager() +{ + delete _notifier; + udev_monitor_unref(_monitor); + udev_unref(_udev); +} + +void SmartpenManager::handleMonitorActivity() +{ + qDebug() << "udev activity"; + udev_device *dev = udev_monitor_receive_device(_monitor); + udev_device_unref(dev); +} + +void SmartpenManager::handleSyncerFinished() +{ + SmartpenSyncer *syncer = static_cast<SmartpenSyncer*>(sender()); + Smartpen::Address addr = syncer->penAddress(); + qDebug() << "Finished synchronization with pen with address:" << addr; + _syncers.remove(addr); + syncer->deleteLater(); +} + +void SmartpenManager::processDevice(udev_device *dev) +{ + uint busnum = atol(udev_device_get_sysattr_value(dev, "busnum")); + uint devnum = atol(udev_device_get_sysattr_value(dev, "devnum")); + + Smartpen::Address addr(busnum, devnum); + if (!_syncers.contains(addr)) { + SmartpenSyncer *syncer = new SmartpenSyncer(addr, this); + _syncers.insert(addr, syncer); + connect(syncer, SIGNAL(finished()), SLOT(handleSyncerFinished())); + syncer->start(); + } +} |