diff options
author | Kevin Krammer <kevin.krammer.qnx@kdab.com> | 2012-03-08 11:15:41 (GMT) |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-09 16:15:35 (GMT) |
commit | bede41701dbd6bb98408f3e1ebbc72b32fa60088 (patch) | |
tree | 8472e17b2f44ba38b8b20cbcbee149fea78a7cd1 /src/plugins | |
parent | 3557368340e0ce9d448fd3df81b30ded86b0e158 (diff) | |
download | Qt-bede41701dbd6bb98408f3e1ebbc72b32fa60088.zip Qt-bede41701dbd6bb98408f3e1ebbc72b32fa60088.tar.gz Qt-bede41701dbd6bb98408f3e1ebbc72b32fa60088.tar.bz2 |
Use QSocketNotifier instead of sleeping in an endless loop
Event driven listening for navigator events, basically a backport of
the respective QPA plugin code in Qt5/qtbase
Change-Id: I120a755af614d35a9e5a0e755c521bbf8bf0d13d
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/platforms/blackberry/qbbnavigatorthread.cpp | 79 | ||||
-rw-r--r-- | src/plugins/platforms/blackberry/qbbnavigatorthread.h | 7 |
2 files changed, 51 insertions, 35 deletions
diff --git a/src/plugins/platforms/blackberry/qbbnavigatorthread.cpp b/src/plugins/platforms/blackberry/qbbnavigatorthread.cpp index 97d50a9..675e812 100644 --- a/src/plugins/platforms/blackberry/qbbnavigatorthread.cpp +++ b/src/plugins/platforms/blackberry/qbbnavigatorthread.cpp @@ -43,12 +43,14 @@ #include "qbbnavigatorthread.h" #include "qbbscreen.h" +#include <QtCore/private/qcore_unix_p.h> #include <QtGui/QApplication> #include <QtGui/QWidget> #include <QtGui/QWindowSystemInterface> #include <QByteArray> #include <QList> #include <QDebug> +#include <QSocketNotifier> #include <errno.h> #include <fcntl.h> @@ -62,7 +64,7 @@ QBBNavigatorThread::QBBNavigatorThread(QBBScreen& primaryScreen) : mPrimaryScreen(primaryScreen), mFd(-1), - mQuit(false) + mReadNotifier(0) { } @@ -70,6 +72,8 @@ QBBNavigatorThread::~QBBNavigatorThread() { // block until thread terminates shutdown(); + + delete mReadNotifier; } void QBBNavigatorThread::run() @@ -86,37 +90,11 @@ void QBBNavigatorThread::run() return; } - // allocate buffer for pps data - char buffer[PPS_BUFFER_SIZE]; - - // loop indefinitely - while (!mQuit) { - - // attempt to read pps data - errno = 0; - int bytes = read(mFd, buffer, PPS_BUFFER_SIZE - 1); - if (bytes == -1) { - qFatal("QBB: failed to read navigator pps, errno=%d", errno); - } - - // check if pps data was received - if (bytes > 0) { - - // ensure data is null terminated - buffer[bytes] = '\0'; + mReadNotifier = new QSocketNotifier(mFd, QSocketNotifier::Read); + // using direct connection to get the slot called in this thread's context + connect(mReadNotifier, SIGNAL(activated(int)), this, SLOT(readData()), Qt::DirectConnection); - // process received message - QByteArray ppsData(buffer); - QByteArray msg; - QByteArray dat; - QByteArray id; - parsePPS(ppsData, msg, dat, id); - handleMessage(msg, dat, id); - } - - // yield - msleep(5); - } + exec(); // close connection to navigator close(mFd); @@ -128,13 +106,13 @@ void QBBNavigatorThread::run() void QBBNavigatorThread::shutdown() { - // signal thread to terminate - mQuit = true; - #if defined(QBBNAVIGATORTHREAD_DEBUG) qDebug() << "QBB: navigator thread shutdown begin"; #endif + // signal thread to terminate + quit(); + // block until thread terminates wait(); @@ -269,3 +247,36 @@ void QBBNavigatorThread::handleMessage(const QByteArray &msg, const QByteArray & QApplication::quit(); } } + +void QBBNavigatorThread::readData() +{ +#if defined(QBBNAVIGATORTHREAD_DEBUG) + qDebug() << "QBB: reading navigator data"; +#endif + + // allocate buffer for pps data + char buffer[PPS_BUFFER_SIZE]; + + // attempt to read pps data + errno = 0; + int bytes = qt_safe_read(mFd, buffer, PPS_BUFFER_SIZE - 1); + if (bytes == -1) { + qFatal("QBB: failed to read navigator pps, errno=%d", errno); + } + + // check if pps data was received + if (bytes > 0) { + + // ensure data is null terminated + buffer[bytes] = '\0'; + + // process received message + QByteArray ppsData(buffer); + QByteArray msg; + QByteArray dat; + QByteArray id; + parsePPS(ppsData, msg, dat, id); + handleMessage(msg, dat, id); + } + +} diff --git a/src/plugins/platforms/blackberry/qbbnavigatorthread.h b/src/plugins/platforms/blackberry/qbbnavigatorthread.h index fa7e8ae..a4dd23a 100644 --- a/src/plugins/platforms/blackberry/qbbnavigatorthread.h +++ b/src/plugins/platforms/blackberry/qbbnavigatorthread.h @@ -45,9 +45,11 @@ QT_BEGIN_NAMESPACE class QBBScreen; +class QSocketNotifier; class QBBNavigatorThread : public QThread { + Q_OBJECT public: QBBNavigatorThread(QBBScreen& primaryScreen); virtual ~QBBNavigatorThread(); @@ -55,10 +57,13 @@ public: protected: virtual void run(); +private Q_SLOTS: + void readData(); + private: QBBScreen& mPrimaryScreen; int mFd; - bool mQuit; + QSocketNotifier *mReadNotifier; void shutdown(); void parsePPS(const QByteArray &ppsData, QByteArray &msg, QByteArray &dat, QByteArray &id); |