diff options
author | Javier <dev.git@javispedro.com> | 2021-09-20 11:59:14 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2021-09-20 11:59:14 +0200 |
commit | 263c1c8846b1f7aed8c3a563f76a5b90191611d9 (patch) | |
tree | ea8c7db23528e464825312f84417a53b1bd5675f | |
parent | d805dfb9f9488db5fe19483021ca2dded8727e20 (diff) | |
download | scribiu-263c1c8846b1f7aed8c3a563f76a5b90191611d9.tar.gz scribiu-263c1c8846b1f7aed8c3a563f76a5b90191611d9.zip |
fix crash when hot-unplugging pen
-rw-r--r-- | smartpenmanager.cc | 23 | ||||
-rw-r--r-- | smartpenmanager.h | 1 |
2 files changed, 18 insertions, 6 deletions
diff --git a/smartpenmanager.cc b/smartpenmanager.cc index 1eeb685..d375fea 100644 --- a/smartpenmanager.cc +++ b/smartpenmanager.cc @@ -197,9 +197,8 @@ void SmartpenManager::handleTimerNextTry() void SmartpenManager::processDeviceAdded(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); + Smartpen::Address addr = getDeviceAddress(dev); + if (!addr.first && !addr.second) return; QString name = parseUdevEscapedString(udev_device_get_property_value(dev, "ID_MODEL_ENC")); @@ -223,9 +222,8 @@ void SmartpenManager::processDeviceAdded(udev_device *dev) void SmartpenManager::processDeviceRemoved(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); + Smartpen::Address addr = getDeviceAddress(dev); + if (!addr.first && !addr.second) return; qDebug() << "Device removed with address:" << addr; @@ -325,3 +323,16 @@ QString SmartpenManager::parseUdevEscapedString(const char *s) } return r; } + +Smartpen::Address SmartpenManager::getDeviceAddress(udev_device *dev) +{ + const char *busnumS = udev_device_get_property_value(dev, "BUSNUM"); + const char *devnumS = udev_device_get_property_value(dev, "DEVNUM"); + if (!busnumS || !devnumS) { + qWarning() << "Cannot find busnum/devnum env var for udev device"; + return Smartpen::Address(); + } + uint busnum = atol(busnumS); + uint devnum = atol(devnumS); + return Smartpen::Address(busnum, devnum); +} diff --git a/smartpenmanager.h b/smartpenmanager.h index a823709..6a5e39f 100644 --- a/smartpenmanager.h +++ b/smartpenmanager.h @@ -61,6 +61,7 @@ private: void scheduleNextTry(); static QString parseUdevEscapedString(const char *s); + static Smartpen::Address getDeviceAddress(udev_device *dev); private: udev *_udev; |