Qt 4.8
qbbbpseventfilter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Research In Motion <blackberry-qt@qnx.com>
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QBBBPSEVENTFILTER_DEBUG
43 
44 #include "qbbbpseventfilter.h"
46 #include "qbbscreen.h"
47 #include "qbbscreeneventhandler.h"
48 #include "qbbvirtualkeyboardbps.h"
49 
50 #include <QCoreApplication>
51 #include <QAbstractEventDispatcher>
52 #include <QDebug>
53 
54 #include <bps/event.h>
55 #include <bps/navigator.h>
56 #include <bps/screen.h>
57 
59 
61 
63  QBBScreenEventHandler *screenEventHandler,
64  QBBVirtualKeyboardBps *virtualKeyboard, QObject *parent)
65  : QObject(parent)
66  , mNavigatorEventHandler(navigatorEventHandler)
67  , mScreenEventHandler(screenEventHandler)
68  , mVirtualKeyboard(virtualKeyboard)
69 {
70  Q_ASSERT(sInstance == 0);
71 
72  sInstance = this;
73 }
74 
76 {
77  Q_ASSERT(sInstance == this);
78 
79  sInstance = 0;
80 }
81 
83 {
84 #if defined(QBBBPSEVENTFILTER_DEBUG)
85  qDebug() << Q_FUNC_INFO << "dispatcher=" << dispatcher;
86 #endif
87  if (navigator_request_events(0) != BPS_SUCCESS)
88  qWarning("QBB: failed to register for navigator events");
89 
90  QAbstractEventDispatcher::EventFilter previousEventFilter = dispatcher->setEventFilter(dispatcherEventFilter);
91 
92  // the QPA plugin in created in the QApplication constructor which indirectly also creates
93  // the event dispatcher so we are the first event filter.
94  // assert on that just in case somebody adds another event filter
95  // in the QBBIntegration constructor instead of adding a new section in here
96  Q_ASSERT(previousEventFilter == 0);
97  Q_UNUSED(previousEventFilter);
98 }
99 
101 {
102  if (!mScreenEventHandler) {
103  qWarning("QBB: trying to register for screen events, but no handler provided");
104  return;
105  }
106 
107  int attached;
108  if (screen_get_display_property_iv(screen->nativeDisplay(), SCREEN_PROPERTY_ATTACHED, &attached) != BPS_SUCCESS) {
109  qWarning() << "QBB: unable to query display attachment";
110  return;
111  }
112 
113  if (!attached) {
114 #if defined(QBBBPSEVENTFILTER_DEBUG)
115  qDebug() << Q_FUNC_INFO << "skipping event registration for non-attached screen";
116 #endif
117  return;
118  }
119 
120  if (screen_request_events(screen->nativeContext()) != BPS_SUCCESS)
121  qWarning("QBB: failed to register for screen events on screen %p", screen->nativeContext());
122 }
123 
125 {
126  if (!mScreenEventHandler) {
127  qWarning("QBB: trying to unregister for screen events, but no handler provided");
128  return;
129  }
130 
131  if (screen_stop_events(screen->nativeContext()) != BPS_SUCCESS)
132  qWarning("QBB: failed to unregister for screen events on screen %p", screen->nativeContext());
133 }
134 
136 {
137 #if defined(QBBBPSEVENTFILTER_DEBUG)
138  qDebug() << Q_FUNC_INFO;
139 #endif
140  if (sInstance == 0)
141  return false;
142 
143  bps_event_t *event = static_cast<bps_event_t *>(message);
144  return sInstance->bpsEventFilter(event);
145 }
146 
148 {
149  const int eventDomain = bps_event_get_domain(event);
150 
151 #if defined(QBBBPSEVENTFILTER_DEBUG)
152  qDebug() << Q_FUNC_INFO << "event=" << event << "domain=" << eventDomain;
153 #endif
154 
155  if (eventDomain == screen_get_domain()) {
156  if (!mScreenEventHandler) {
157  qWarning("QBB: registered for screen events, but no handler provided");
158  return false;
159  }
160 
161  screen_event_t screenEvent = screen_event_get_event(event);
162  return mScreenEventHandler->handleEvent(screenEvent);
163  }
164 
165  if (eventDomain == navigator_get_domain())
166  return handleNavigatorEvent(event);
167 
168  if (mVirtualKeyboard->handleEvent(event))
169  return true;
170 
171  return false;
172 }
173 
175 {
176  switch (bps_event_get_code(event)) {
177  case NAVIGATOR_ORIENTATION_CHECK: {
178  const int angle = navigator_event_get_orientation_angle(event);
179 
180  #if defined(QBBBPSEVENTFILTER_DEBUG)
181  qDebug() << "QBB: Navigator ORIENTATION CHECK event. angle=" << angle;
182  #endif
183 
184  const bool result = mNavigatorEventHandler->handleOrientationCheck(angle);
185 
186  #if defined(QBBBPSEVENTFILTER_DEBUG)
187  qDebug() << "QBB: Navigator ORIENTATION CHECK event. result=" << result;
188  #endif
189 
190  // reply to navigator whether orientation is acceptable
191  navigator_orientation_check_response(event, result);
192  break;
193  }
194 
195  case NAVIGATOR_ORIENTATION: {
196  const int angle = navigator_event_get_orientation_angle(event);
197 
198  #if defined(QBBBPSEVENTFILTER_DEBUG)
199  qDebug() << "QBB: Navigator ORIENTATION event. angle=" << angle;
200  #endif
201 
203 
204  navigator_done_orientation(event);
205  break;
206  }
207 
208  case NAVIGATOR_SWIPE_DOWN:
209  #if defined(QBBBPSEVENTFILTER_DEBUG)
210  qDebug() << "QBB: Navigator SWIPE DOWN event";
211  #endif
212 
214  break;
215 
216  case NAVIGATOR_EXIT:
217  #if defined(QBBBPSEVENTFILTER_DEBUG)
218  qDebug() << "QBB: Navigator EXIT event";
219  #endif
220 
222  break;
223 
224  case NAVIGATOR_WINDOW_STATE: {
225  #if defined(QBBBPSEVENTFILTER_DEBUG)
226  qDebug() << Q_FUNC_INFO << "WINDOW STATE event";
227  #endif
228 
229  const navigator_window_state_t state = navigator_event_get_window_state(event);
230  const QByteArray id(navigator_event_get_groupid(event));
231 
232  switch (state) {
233  case NAVIGATOR_WINDOW_FULLSCREEN:
235  break;
236  case NAVIGATOR_WINDOW_THUMBNAIL:
238  break;
239  case NAVIGATOR_WINDOW_INVISIBLE:
241  break;
242  }
243 
244  break;
245  }
246 
247  case NAVIGATOR_WINDOW_ACTIVE: {
248  #if defined(QBBBPSEVENTFILTER_DEBUG)
249  qDebug() << "QBB: Navigator WINDOW ACTIVE event";
250  #endif
251 
252  const QByteArray id(navigator_event_get_groupid(event));
254  break;
255  }
256 
257  case NAVIGATOR_WINDOW_INACTIVE: {
258  #if defined(QBBBPSEVENTFILTER_DEBUG)
259  qDebug() << "QBB: Navigator WINDOW INACTIVE event";
260  #endif
261 
262  const QByteArray id(navigator_event_get_groupid(event));
264  break;
265  }
266 
267  case NAVIGATOR_LOW_MEMORY:
268  qWarning() << "QApplication based process" << QCoreApplication::applicationPid()
269  << "received \"NAVIGATOR_LOW_MEMORY\" event";
270  return false;
271 
272  default:
273  #if defined(QBBBPSEVENTFILTER_DEBUG)
274  qDebug() << "QBB: Unhandled navigator event. code=" << bps_event_get_code(event);
275  #endif
276  return false;
277  }
278 
279  return true;
280 }
281 
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void unregisterForScreenEvents(QBBScreen *screen)
QBBScreenEventHandler * mScreenEventHandler
void handleWindowGroupActivated(const QByteArray &id)
screen_context_t nativeContext() const
Definition: qbbscreen.h:73
QBBNavigatorEventHandler * mNavigatorEventHandler
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QBBVirtualKeyboardBps * mVirtualKeyboard
static QBBBpsEventFilter * sInstance
void handleWindowGroupDeactivated(const QByteArray &id)
bool bpsEventFilter(bps_event_t *event)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void handleWindowGroupStateChanged(const QByteArray &id, Qt::WindowState state)
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
Q_CORE_EXPORT void qDebug(const char *,...)
bool handleEvent(screen_event_t event)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
static qint64 applicationPid()
Returns the current process ID for the application.
static bool dispatcherEventFilter(void *message)
bool(* EventFilter)(void *message)
Typedef for a function with the signature.
bool handleEvent(bps_event_t *event)
screen_display_t nativeDisplay() const
Definition: qbbscreen.h:72
qreal angle(const QPointF &p1, const QPointF &p2)
void installOnEventDispatcher(QAbstractEventDispatcher *dispatcher)
void registerForScreenEvents(QBBScreen *screen)
bool handleNavigatorEvent(bps_event_t *event)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QAbstractEventDispatcher class provides an interface to manage Qt&#39;s event queue.
QBBBpsEventFilter(QBBNavigatorEventHandler *navigatorEventHandler, QBBScreenEventHandler *screenEventHandler, QBBVirtualKeyboardBps *virtualKeyboard, QObject *parent=0)
#define Q_FUNC_INFO
Definition: qglobal.h:1871