BlackBerry Platform Support Events: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:BlackBerry]] | |||
= Introduction = | |||
The BlackBerry platform support library, BPS, provides an API for system events. In Qt, BPS events are integrated into Qt's event loop, i.e. Qt receives and consumes BPS events. One example in which Qt consumes a BPS event is the NAVIGATOR_EXIT event - when receiving that event, Qt will trigger quitting the application. See "Handling BPS events":https://developer.blackberry.com/native/documentation/cascades/device_platform/bps/ article for more details. | |||
= Receiving BPS events = | |||
Since BPS events are integrated into Qt's event loop, how is it possible as a user to still receive BPS events? An application should '''never''' call "bps_get_event()":https://developer.blackberry.com/native/reference/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_ref/topic/bps_get_event.html itself, as that would 'steal' the events from Qt, and Qt would miss out on important events like the above mentioned NAVIGATOR_EXIT. | |||
The following example shows how to install native event filters to process a | Instead, Qt offers to install '''native event filters'''. Once a native event filter is installed, Qt will forward all BPS events to that native event filter. So the solution for receiving BPS events is to install a native event filter and process the BPS events in the event filter function. | ||
The following example shows how to install native event filters to process a NAVIGATOR_SWIPE_DOWN event. The Qt4 and the Qt5 versions are a bit different, therefore the two code examples. | |||
Native event filters are installed on a per-thread basis, as each thread in Qt has its own event dispatcher. Each thread only receives the native events requested in that thread. If your code that installs the native event filter is running in multiple threads, this needs to be considered, especially in Qt4 version, in which the previous event filter pointer then needs to be stored on a per-thread basis, and not as a global variable. | Native event filters are installed on a per-thread basis, as each thread in Qt has its own event dispatcher. Each thread only receives the native events requested in that thread. If your code that installs the native event filter is running in multiple threads, this needs to be considered, especially in Qt4 version, in which the previous event filter pointer then needs to be stored on a per-thread basis, and not as a global variable. | ||
Note that this wiki page is about '''native events''' only, e.g. | Note that this wiki page is about '''native events''' only, e.g. BPS events on the BlackBerry platform. Receiving normal '''QEvents''' is an entirely unrelated matter, see "the documentation of QCoreApplication::notify()":http://doc.qt.io/qt-4.8/qcoreapplication.html#notify for details about that. | ||
= Qt4 Example = | |||
First, install the native event filter. Note that the previous event filter needs to be remembered, as the native event filters form a chain - one event filter always needs to pass on the native event to the previous filter in the chain, otherwise the event gets lost! | |||
Other event types, such as mmrenderer events, are not automatically requested by Qt, there you have to call the appropriate ''foo_request_events()'' function yourself.<br /> Note that it is not necessary to call ''bps_initialize()'' yourself, Qt already does that for you. But again, it | <code><br />static QAbstractEventDispatcher::EventFilter s_previousEventFilter = 0;<br />s_previousEventFilter = QAbstractEventDispatcher::instance()<s>>setEventFilter(s_eventFilter);<br /></code> | ||
<br />Afterwards, make sure BPS delivers navigator events to the current thread. For certain types of events, like navigator and virtual keyboard events, Qt will already have requested the events, but I can't hurt to request them twice: | |||
<br /><code><br />navigator_request_events();<br /></code> | |||
<br />Other event types, such as mmrenderer events, are not automatically requested by Qt, there you have to call the appropriate ''foo_request_events()'' function yourself.<br />Note that it is not necessary to call ''bps_initialize()'' yourself, Qt already does that for you. But again, it doesn't hurt either.<br />Lastly, let's have a look at the event filter function: | |||
<br /><code><br />static bool s_eventFilter(void '''message)<br />{<br /> bps_event_t''' const event = static_cast&lt;bps_event_t *>(message); | |||
<br /> if (event && bps_event_get_domain(event) navigator_get_domain()) &#123; | |||
const int id = bps_event_get_code(event); | |||
if (id NAVIGATOR_SWIPE_DOWN)<br /> qDebug() << "The user swiped down!";<br /> } | |||
<br /> if (s_previousEventFilter)<br /> return s_previousEventFilter(message);<br /> else<br /> return false;<br />}<br /></code> | |||
<br />Note that in Qt4, native event filters should never be removed once installed, as that will easily break the chain. Instead of removing a native event filter, simply don't do any event processing in it anymore, apart from passing the event on to the previous element in the chain. | |||
<br />h1. Qt5 Example | |||
<br />The API for native event filters in Qt5 has been improved, the brittle event filter chaining is no longer exposed. | |||
<br />Instead of an event filter function like in Qt4, there is an event filter object: | |||
<br /><code><br />class MyEventFilter : public QAbstractNativeEventFilter<br />{<br /> bool nativeEventFilter(const QByteArray &eventType, void *message, long '''result) Q_DECL_OVERRIDE<br /> {<br /> Q_UNUSED(eventType);<br /> Q_UNUSED(result); | |||
<br /> bps_event_t''' const event = static_cast&lt;bps_event_t *>(message);<br /> if (event && bps_event_get_domain(event) navigator_get_domain()) &#123; | |||
const int id = bps_event_get_code(event); | |||
if (id NAVIGATOR_SWIPE_DOWN)<br /> qDebug() << "The user swiped down!";<br /> } | |||
<br /> return false; // Do not filter out the event<br /> }<br />};<br /></code> | |||
<br />Installing the event filter is easy:<br /><code><br />MyEventFilter *filter = …;<br />QCoreApplication::eventDispatcher()</s>>installNativeEventFilter(filter);<br /></code> | |||
Afterwards, make sure BPS delivers navigator events to the current thread. For certain types of events, like navigator and virtual keyboard events, Qt will already have requested the events, but I can't hurt to request them twice: | |||
<code><br />navigator_request_events();<br /></code> | |||
Other event types, such as mmrenderer events, are not automatically requested by Qt, there you have to call the appropriate ''foo_request_events()'' function yourself.<br />Note that it is not necessary to call ''bps_initialize()'' yourself, Qt already does that for you. But again, it doesn't hurt either. |
Revision as of 09:52, 24 February 2015
Introduction
The BlackBerry platform support library, BPS, provides an API for system events. In Qt, BPS events are integrated into Qt's event loop, i.e. Qt receives and consumes BPS events. One example in which Qt consumes a BPS event is the NAVIGATOR_EXIT event - when receiving that event, Qt will trigger quitting the application. See "Handling BPS events":https://developer.blackberry.com/native/documentation/cascades/device_platform/bps/ article for more details.
Receiving BPS events
Since BPS events are integrated into Qt's event loop, how is it possible as a user to still receive BPS events? An application should never call "bps_get_event()":https://developer.blackberry.com/native/reference/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_ref/topic/bps_get_event.html itself, as that would 'steal' the events from Qt, and Qt would miss out on important events like the above mentioned NAVIGATOR_EXIT.
Instead, Qt offers to install native event filters. Once a native event filter is installed, Qt will forward all BPS events to that native event filter. So the solution for receiving BPS events is to install a native event filter and process the BPS events in the event filter function.
The following example shows how to install native event filters to process a NAVIGATOR_SWIPE_DOWN event. The Qt4 and the Qt5 versions are a bit different, therefore the two code examples.
Native event filters are installed on a per-thread basis, as each thread in Qt has its own event dispatcher. Each thread only receives the native events requested in that thread. If your code that installs the native event filter is running in multiple threads, this needs to be considered, especially in Qt4 version, in which the previous event filter pointer then needs to be stored on a per-thread basis, and not as a global variable.
Note that this wiki page is about native events only, e.g. BPS events on the BlackBerry platform. Receiving normal QEvents is an entirely unrelated matter, see "the documentation of QCoreApplication::notify()":http://doc.qt.io/qt-4.8/qcoreapplication.html#notify for details about that.
Qt4 Example
First, install the native event filter. Note that the previous event filter needs to be remembered, as the native event filters form a chain - one event filter always needs to pass on the native event to the previous filter in the chain, otherwise the event gets lost!
<br />static QAbstractEventDispatcher::EventFilter s_previousEventFilter = 0;<br />s_previousEventFilter = QAbstractEventDispatcher::instance()<s>>setEventFilter(s_eventFilter);<br />
Afterwards, make sure BPS delivers navigator events to the current thread. For certain types of events, like navigator and virtual keyboard events, Qt will already have requested the events, but I can't hurt to request them twice:
<br />navigator_request_events();<br />
Other event types, such as mmrenderer events, are not automatically requested by Qt, there you have to call the appropriate foo_request_events() function yourself.
Note that it is not necessary to call bps_initialize() yourself, Qt already does that for you. But again, it doesn't hurt either.
Lastly, let's have a look at the event filter function:
<br />static bool s_eventFilter(void '''message)<br />{<br /> bps_event_t''' const event = static_cast&lt;bps_event_t *>(message);
<br /> if (event && bps_event_get_domain(event) navigator_get_domain()) &#123;
const int id = bps_event_get_code(event);
if (id NAVIGATOR_SWIPE_DOWN)<br /> qDebug() << "The user swiped down!";<br /> }
<br /> if (s_previousEventFilter)<br /> return s_previousEventFilter(message);<br /> else<br /> return false;<br />}<br />
Note that in Qt4, native event filters should never be removed once installed, as that will easily break the chain. Instead of removing a native event filter, simply don't do any event processing in it anymore, apart from passing the event on to the previous element in the chain.
h1. Qt5 Example
The API for native event filters in Qt5 has been improved, the brittle event filter chaining is no longer exposed.
Instead of an event filter function like in Qt4, there is an event filter object:
<br />class MyEventFilter : public QAbstractNativeEventFilter<br />{<br /> bool nativeEventFilter(const QByteArray &eventType, void *message, long '''result) Q_DECL_OVERRIDE<br /> {<br /> Q_UNUSED(eventType);<br /> Q_UNUSED(result);
<br /> bps_event_t''' const event = static_cast&lt;bps_event_t *>(message);<br /> if (event && bps_event_get_domain(event) navigator_get_domain()) &#123;
const int id = bps_event_get_code(event);
if (id NAVIGATOR_SWIPE_DOWN)<br /> qDebug() << "The user swiped down!";<br /> }
<br /> return false; // Do not filter out the event<br /> }<br />};<br />
Installing the event filter is easy:
<br />MyEventFilter *filter = …;<br />QCoreApplication::eventDispatcher()</s>>installNativeEventFilter(filter);<br />
Afterwards, make sure BPS delivers navigator events to the current thread. For certain types of events, like navigator and virtual keyboard events, Qt will already have requested the events, but I can't hurt to request them twice:
<br />navigator_request_events();<br />
Other event types, such as mmrenderer events, are not automatically requested by Qt, there you have to call the appropriate foo_request_events() function yourself.
Note that it is not necessary to call bps_initialize() yourself, Qt already does that for you. But again, it doesn't hurt either.