Add linux USB hotplug

qcpfix
M0VSE 2023-04-11 20:25:29 +01:00
rodzic 4874d70215
commit c9300f4d86
4 zmienionych plików z 79 dodań i 10 usunięć

Wyświetl plik

@ -140,7 +140,7 @@ void usbController::run()
return; return;
} }
#ifdef Q_OS_WIN #ifdef USB_HOTPLUG
qDebug(logUsbControl()) << "Re-enumerating USB devices due to program startup or hotplug event"; qDebug(logUsbControl()) << "Re-enumerating USB devices due to program startup or hotplug event";
#endif #endif
@ -452,7 +452,7 @@ void usbController::run()
dataTimer->start(25); dataTimer->start(25);
} }
#ifndef Q_OS_WIN #ifndef USB_HOTPLUG
// Run the periodic timer to check for new devices // Run the periodic timer to check for new devices
QTimer::singleShot(2000, this, SLOT(run())); QTimer::singleShot(2000, this, SLOT(run()));
#endif #endif

Wyświetl plik

@ -84,6 +84,7 @@ wfmain::wfmain(const QString settingsFile, const QString logFile, bool debugMode
qRegisterMetaType<codecType>(); qRegisterMetaType<codecType>();
qRegisterMetaType<errorType>(); qRegisterMetaType<errorType>();
qRegisterMetaType<usbFeatureType>(); qRegisterMetaType<usbFeatureType>();
qRegisterMetaType<cmds>();
haveRigCaps = false; haveRigCaps = false;
@ -169,17 +170,38 @@ wfmain::wfmain(const QString settingsFile, const QString logFile, bool debugMode
amTransmitting = false; amTransmitting = false;
connect(ui->txPowerSlider, &QSlider::sliderMoved,
[&](int value) {
QToolTip::showText(QCursor::pos(), QString("%1").arg(value*100/255), nullptr);
});
#if !defined(USB_CONTROLLER) #if !defined(USB_CONTROLLER)
ui->enableUsbChk->setVisible(false); ui->enableUsbChk->setVisible(false);
ui->usbControllerBtn->setVisible(false); ui->usbControllerBtn->setVisible(false);
ui->usbControllersResetBtn->setVisible(false); ui->usbControllersResetBtn->setVisible(false);
ui->usbResetLbl->setVisible(false); ui->usbResetLbl->setVisible(false);
#else
#if defined(USB_HOTPLUG) && defined(Q_OS_LINUX)
uDev = udev_new();
if (!uDev)
{
qInfo(logUsbControl()) << "Cannot register udev, hotplug of USB devices is not available";
return;
}
uDevMonitor = udev_monitor_new_from_netlink(uDev, "udev");
if (!uDevMonitor)
{
qInfo(logUsbControl()) << "Cannot register udev_monitor, hotplug of USB devices is not available";
return;
}
int fd = udev_monitor_get_fd(uDevMonitor);
uDevNotifier = new QSocketNotifier(fd, QSocketNotifier::Read,this);
connect(uDevNotifier, SIGNAL(activated(int)), this, SLOT(uDevEvent()));
udev_monitor_enable_receiving(uDevMonitor);
#endif
#endif #endif
connect(ui->txPowerSlider, &QSlider::sliderMoved,
[&](int value) {
QToolTip::showText(QCursor::pos(), QString("%1").arg(value*100/255), nullptr);
});
} }
@ -214,6 +236,13 @@ wfmain::~wfmain()
usbControllerThread->quit(); usbControllerThread->quit();
usbControllerThread->wait(); usbControllerThread->wait();
} }
if (uDevMonitor)
{
udev_monitor_unref(uDevMonitor);
udev_unref(uDev);
delete uDevNotifier;
}
#endif #endif
} }
@ -9569,12 +9598,15 @@ void wfmain::on_cwButton_clicked()
} }
#ifdef USB_HOTPLUG #ifdef USB_HOTPLUG
#ifdef Q_OS_WINDOWS
bool wfmain::nativeEvent(const QByteArray& eventType, void* message, qintptr* result) bool wfmain::nativeEvent(const QByteArray& eventType, void* message, qintptr* result)
{ {
Q_UNUSED(eventType); Q_UNUSED(eventType);
Q_UNUSED(result); Q_UNUSED(result);
if (prefs.enableUSBControllers) if (QDateTime::currentMSecsSinceEpoch() > lastUsbNotify + 10)
{ {
static bool created = false; static bool created = false;
@ -9602,6 +9634,7 @@ bool wfmain::nativeEvent(const QByteArray& eventType, void* message, qintptr* re
case DBT_DEVICEARRIVAL: case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE: case DBT_DEVICEREMOVECOMPLETE:
emit usbHotplug(); emit usbHotplug();
lastUsbNotify = QDateTime::currentMSecsSinceEpoch();
break; break;
case DBT_DEVNODES_CHANGED: case DBT_DEVNODES_CHANGED:
break; break;
@ -9618,4 +9651,23 @@ bool wfmain::nativeEvent(const QByteArray& eventType, void* message, qintptr* re
} }
return false; // Process native events as normal return false; // Process native events as normal
} }
#elif defined(Q_OS_LINUX)
void wfmain::uDevEvent()
{
udev_device *dev = udev_monitor_receive_device(uDevMonitor);
if (dev)
{
const char* action = udev_device_get_action(dev);
if (action && strcmp(action, "add") == 0 && QDateTime::currentMSecsSinceEpoch() > lastUsbNotify + 10)
{
emit usbHotplug();
lastUsbNotify = QDateTime::currentMSecsSinceEpoch();
}
udev_device_unref(dev);
}
}
#endif #endif
#endif

Wyświetl plik

@ -68,6 +68,10 @@
#include <windows.h> #include <windows.h>
#include <dbt.h> #include <dbt.h>
#define USB_HOTPLUG #define USB_HOTPLUG
#elif defined(Q_OS_LINUX)
#include <QSocketNotifier>
#include <libudev.h>
#define USB_HOTPLUG
#endif #endif
#endif #endif
@ -88,8 +92,13 @@ public:
void handleLogText(QString text); void handleLogText(QString text);
#ifdef USB_HOTPLUG #ifdef USB_HOTPLUG
protected: #if defined(Q_OS_WIN)
virtual bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result); protected:
virtual bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result);
#elif defined(Q_OS_LINUX)
private slots:
void uDevEvent();
#endif
#endif #endif
signals: signals:
@ -1195,6 +1204,13 @@ private:
QVector<KNOB> usbKnobs; QVector<KNOB> usbKnobs;
usbDevMap usbDevices; usbDevMap usbDevices;
QMutex usbMutex; QMutex usbMutex;
qint64 lastUsbNotify;
#if defined (Q_OS_LINUX)
struct udev* uDev = nullptr;
struct udev_monitor* uDevMonitor = nullptr;
QSocketNotifier* uDevNotifier = nullptr;
#endif
#endif #endif
dxClusterClient* cluster = Q_NULLPTR; dxClusterClient* cluster = Q_NULLPTR;
@ -1241,6 +1257,7 @@ Q_DECLARE_METATYPE(enum rptAccessTxRx)
Q_DECLARE_METATYPE(struct rptrTone_t) Q_DECLARE_METATYPE(struct rptrTone_t)
Q_DECLARE_METATYPE(struct rptrAccessData_t) Q_DECLARE_METATYPE(struct rptrAccessData_t)
Q_DECLARE_METATYPE(enum usbFeatureType) Q_DECLARE_METATYPE(enum usbFeatureType)
Q_DECLARE_METATYPE(enum cmds)
//void (*wfmain::logthistext)(QString text) = NULL; //void (*wfmain::logthistext)(QString text) = NULL;

Wyświetl plik

@ -51,7 +51,7 @@ macx:DEFINES += __MACOSX_CORE__
!linux:HEADERS += ../rtaudio/RTAUdio.h !linux:HEADERS += ../rtaudio/RTAUdio.h
!linux:INCLUDEPATH += ../rtaudio !linux:INCLUDEPATH += ../rtaudio
linux:LIBS += -lpulse -lpulse-simple -lrtaudio -lpthread linux:LIBS += -lpulse -lpulse-simple -lrtaudio -lpthread -ludev
win32:INCLUDEPATH += ../portaudio/include win32:INCLUDEPATH += ../portaudio/include
!win32:LIBS += -lportaudio !win32:LIBS += -lportaudio