Qt 4.8
qguieventdispatcher_glib.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 
43 
44 #include "qapplication.h"
45 #include "qx11info_x11.h"
46 
47 #include "qt_x11_p.h"
48 
49 #include <glib.h>
50 
52 
54 {
55  GSource source;
56  GPollFD pollfd;
57  QEventLoop::ProcessEventsFlags flags;
60 };
61 
63 {
65 
66 public:
70 };
71 
72 static gboolean x11EventSourcePrepare(GSource *s, gint *timeout)
73 {
74  if (timeout)
75  *timeout = -1;
76  GX11EventSource *source = reinterpret_cast<GX11EventSource *>(s);
77  return (XEventsQueued(X11->display, QueuedAfterFlush)
79  && !source->d->queuedUserInputEvents.isEmpty()));
80 }
81 
82 static gboolean x11EventSourceCheck(GSource *s)
83 {
84  GX11EventSource *source = reinterpret_cast<GX11EventSource *>(s);
85  return (XEventsQueued(X11->display, QueuedAfterFlush)
87  && !source->d->queuedUserInputEvents.isEmpty()));
88 }
89 
90 static gboolean x11EventSourceDispatch(GSource *s, GSourceFunc callback, gpointer user_data)
91 {
92  GX11EventSource *source = reinterpret_cast<GX11EventSource *>(s);
93 
94  ulong marker = XNextRequest(X11->display);
95  do {
96  XEvent event;
98  && !source->d->queuedUserInputEvents.isEmpty()) {
99  // process a pending user input event
100  event = source->d->queuedUserInputEvents.takeFirst();
101  } else if (XEventsQueued(X11->display, QueuedAlready)) {
102  // process events from the X server
103  XNextEvent(X11->display, &event);
104 
106  // queue user input events
107  switch (event.type) {
108  case ButtonPress:
109  case ButtonRelease:
110  case MotionNotify:
111  case XKeyPress:
112  case XKeyRelease:
113  case EnterNotify:
114  case LeaveNotify:
115  source->d->queuedUserInputEvents.append(event);
116  continue;
117 
118  case ClientMessage:
119  // only keep the wm_take_focus and
120  // _qt_scrolldone protocols, queue all other
121  // client messages
122  if (event.xclient.format == 32) {
123  if (event.xclient.message_type == ATOM(WM_PROTOCOLS) &&
124  (Atom) event.xclient.data.l[0] == ATOM(WM_TAKE_FOCUS)) {
125  break;
126  } else if (event.xclient.message_type == ATOM(_QT_SCROLL_DONE)) {
127  break;
128  }
129  }
130  source->d->queuedUserInputEvents.append(event);
131  continue;
132 
133  default:
134  break;
135  }
136  }
137  } else {
138  // no event to process
139  break;
140  }
141 
142  // send through event filter
143  if (source->q->filterEvent(&event))
144  continue;
145 
146  if (qApp->x11ProcessEvent(&event) == 1)
147  return true;
148 
149  if (event.xany.serial >= marker)
150  goto out;
151  } while (XEventsQueued(X11->display, QueuedAfterFlush));
152 
153  out:
154 
156 
157  if (callback)
158  callback(user_data);
159  return true;
160 }
161 
162 static GSourceFuncs x11EventSourceFuncs = {
166  NULL,
167  NULL,
168  NULL
169 };
170 
172 {
173  x11EventSource = reinterpret_cast<GX11EventSource *>(g_source_new(&x11EventSourceFuncs,
174  sizeof(GX11EventSource)));
175  g_source_set_can_recurse(&x11EventSource->source, true);
176 
177  memset(&x11EventSource->pollfd, 0, sizeof(GPollFD));
178  x11EventSource->flags = QEventLoop::AllEvents;
179  x11EventSource->q = 0;
180  x11EventSource->d = 0;
181 
182  g_source_attach(&x11EventSource->source, mainContext);
183 }
184 
187 {
188 }
189 
191 {
193 
194  g_source_remove_poll(&d->x11EventSource->source, &d->x11EventSource->pollfd);
195  g_source_destroy(&d->x11EventSource->source);
196  d->x11EventSource = 0;
197 }
198 
199 bool QGuiEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
200 {
202  QEventLoop::ProcessEventsFlags saved_flags = d->x11EventSource->flags;
203  d->x11EventSource->flags = flags;
204  bool returnValue = QEventDispatcherGlib::processEvents(flags);
205  d->x11EventSource->flags = saved_flags;
206  return returnValue;
207 }
208 
210 {
212  d->x11EventSource->pollfd.fd = XConnectionNumber(X11->display);
213  d->x11EventSource->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
214  d->x11EventSource->q = this;
215  d->x11EventSource->d = d;
216  g_source_add_poll(&d->x11EventSource->source, &d->x11EventSource->pollfd);
217 }
218 
220 {
221  XFlush(X11->display);
222 }
223 
double d
Definition: qnumeric_p.h:62
static void callback(AuServer *, AuEventHandlerRec *, AuEvent *e, AuPointer p)
Definition: qsound_x11.cpp:170
bool filterEvent(void *message)
Sends message through the event filter that was set by setEventFilter().
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QGuiEventDispatcherGlibPrivate * d
QGuiEventDispatcherGlib * q
QEventLoop::ProcessEventsFlags flags
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define X11
Definition: qt_x11_p.h:724
#define Q_D(Class)
Definition: qglobal.h:2482
static GSourceFuncs x11EventSourceFuncs
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
union _XEvent XEvent
Definition: qwindowdefs.h:116
#define ATOM(x)
Definition: qt_x11_p.h:723
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
#define qApp
bool processEvents(QEventLoop::ProcessEventsFlags flags)
Processes pending events that match flags until there are no more events to process.
unsigned long ulong
Definition: qglobal.h:997
void flush()
Flushes the event queue.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
static gboolean x11EventSourceDispatch(GSource *s, GSourceFunc callback, gpointer user_data)
QGuiEventDispatcherGlib(QObject *parent=0)
static gboolean x11EventSourcePrepare(GSource *s, gint *timeout)
static gboolean x11EventSourceCheck(GSource *s)
bool processEvents(QEventLoop::ProcessEventsFlags flags)
Processes pending events that match flags until there are no more events to process.