Qt 4.8
qbbscreeneventthread.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 - 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 QBBSCREENEVENTTHREAD_DEBUG
43 
44 #include "qbbscreeneventthread.h"
45 #include "qbbscreeneventhandler.h"
46 
47 #include <QDebug>
48 
49 #include <errno.h>
50 
52 
53 QBBScreenEventThread::QBBScreenEventThread(screen_context_t context, QBBScreenEventHandler *eventHandler)
54  : mContext(context),
55  mQuit(false),
56  mEventHandler(eventHandler)
57 {
58 }
59 
61 {
62  // block until thread terminates
63  shutdown();
64 }
65 
66 void QBBScreenEventThread::injectKeyboardEvent(int flags, int sym, int mod, int scan, int cap)
67 {
68  QBBScreenEventHandler::injectKeyboardEvent(flags, sym, mod, scan, cap);
69 }
70 
72 {
74 }
75 
77 {
78  screen_event_t event;
79 
80  // create screen event
81  errno = 0;
82  int result = screen_create_event(&event);
83  if (result)
84  qFatal("QBB: failed to create screen event, errno=%d", errno);
85 
86 #if defined(QBBSCREENEVENTTHREAD_DEBUG)
87  qDebug() << "QBB: screen event thread started";
88 #endif
89 
90  // loop indefinitely
91  while (!mQuit) {
92 
93  // block until screen event is available
94  errno = 0;
95  result = screen_get_event(mContext, event, -1);
96  if (result)
97  qFatal("QBB: failed to get screen event, errno=%d", errno);
98 
99  // process received event
100  errno = 0;
101  int qnxType;
102  int result = screen_get_event_property_iv(event, SCREEN_PROPERTY_TYPE, &qnxType);
103  if (result)
104  qFatal("QBB: failed to query screen event type, errno=%d", errno);
105 
106  if (qnxType == SCREEN_EVENT_USER) {
107  // treat all screen user events as shutdown requests
108  #if defined(QBBSCREENEVENTTHREAD_DEBUG)
109  qDebug() << "QBB: QNX screen user event";
110  #endif
111  mQuit = true;
112  } else {
113  mEventHandler->handleEvent(event, qnxType);
114  }
115  }
116 
117 #if defined(QBBSCREENEVENTTHREAD_DEBUG)
118  qDebug() << "QBB: screen event thread stopped";
119 #endif
120 
121  // cleanup
122  screen_destroy_event(event);
123 }
124 
126 {
127  screen_event_t event;
128 
129  // create screen event
130  errno = 0;
131  int result = screen_create_event(&event);
132  if (result)
133  qFatal("QBB: failed to create screen event, errno=%d", errno);
134 
135  // set the event type as user
136  errno = 0;
137  int type = SCREEN_EVENT_USER;
138  result = screen_set_event_property_iv(event, SCREEN_PROPERTY_TYPE, &type);
139  if (result)
140  qFatal("QBB: failed to set screen event type, errno=%d", errno);
141 
142  // NOTE: ignore SCREEN_PROPERTY_USER_DATA; treat all user events as shutdown events
143 
144  // post event to event loop so it will wake up and die
145  errno = 0;
146  result = screen_send_event(mContext, event, getpid());
147  if (result)
148  qFatal("QBB: failed to set screen event type, errno=%d", errno);
149 
150  // cleanup
151  screen_destroy_event(event);
152 
153 #if defined(QBBSCREENEVENTTHREAD_DEBUG)
154  qDebug() << "QBB: screen event thread shutdown begin";
155 #endif
156 
157  // block until thread terminates
158  wait();
159 
160 #if defined(QBBSCREENEVENTTHREAD_DEBUG)
161  qDebug() << "QBB: screen event thread shutdown end";
162 #endif
163 }
164 
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QBBScreenEventThread(screen_context_t context, QBBScreenEventHandler *eventHandler)
void injectPointerMoveEvent(int x, int y)
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
static void injectKeyboardEvent(int flags, int sym, int mod, int scan, int cap)
void injectPointerMoveEvent(int x, int y)
screen_context_t mContext
QBBScreenEventHandler * mEventHandler
Q_CORE_EXPORT void qFatal(const char *,...)
static void injectKeyboardEvent(int flags, int sym, int mod, int scan, int cap)
virtual void run()
The starting point for the thread.
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
int errno