1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_pump_x.h"
#include <X11/extensions/XInput2.h>
#include "base/basictypes.h"
#include "base/message_loop.h"
#if defined(TOOLKIT_USES_GTK)
#include <gdk/gdkx.h>
#endif
namespace {
gboolean XSourcePrepare(GSource* source, gint* timeout_ms) {
if (XPending(base::MessagePumpX::GetDefaultXDisplay()))
*timeout_ms = 0;
else
*timeout_ms = -1;
return FALSE;
}
gboolean XSourceCheck(GSource* source) {
return XPending(base::MessagePumpX::GetDefaultXDisplay());
}
gboolean XSourceDispatch(GSource* source,
GSourceFunc unused_func,
gpointer unused_data) {
// TODO(sad): When GTK event proecssing is completely removed, the event
// processing and dispatching should be done here (i.e. XNextEvent,
// ProcessXEvent etc.)
return TRUE;
}
GSourceFuncs XSourceFuncs = {
XSourcePrepare,
XSourceCheck,
XSourceDispatch,
NULL
};
// The opcode used for checking events.
int xiopcode = -1;
// When the GTK/GDK event processing is disabled, the message-pump opens a
// connection to the display and owns it.
Display* g_xdisplay = NULL;
#if defined(TOOLKIT_USES_GTK)
// A flag to disable GTK's message pump. This is intermediate step
// to remove gtk and will be removed once migration is complete.
bool use_gtk_message_pump = true;
gboolean PlaceholderDispatch(GSource* source,
GSourceFunc cb,
gpointer data) {
return TRUE;
}
#endif // defined(TOOLKIT_USES_GTK)
void InitializeXInput2(void) {
Display* display = base::MessagePumpX::GetDefaultXDisplay();
if (!display)
return;
int event, err;
if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
VLOG(1) << "X Input extension not available.";
xiopcode = -1;
return;
}
#if defined(USE_XI2_MT)
// USE_XI2_MT also defines the required XI2 minor minimum version.
int major = 2, minor = USE_XI2_MT;
#else
int major = 2, minor = 0;
#endif
if (XIQueryVersion(display, &major, &minor) == BadRequest) {
VLOG(1) << "XInput2 not supported in the server.";
xiopcode = -1;
return;
}
#if defined(USE_XI2_MT)
if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
VLOG(1) << "XI version on server is " << major << "." << minor << ". "
<< "But 2." << USE_XI2_MT << " is required.";
xiopcode = -1;
return;
}
#endif
}
} // namespace
namespace base {
MessagePumpX::MessagePumpX() : MessagePumpGlib(),
#if defined(TOOLKIT_USES_GTK)
gdksource_(NULL),
dispatching_event_(false),
capture_x_events_(0),
capture_gdk_events_(0),
#endif
x_source_(NULL) {
InitializeXInput2();
#if defined(TOOLKIT_USES_GTK)
if (use_gtk_message_pump) {
gdk_window_add_filter(NULL, &GdkEventFilter, this);
gdk_event_handler_set(&EventDispatcherX, this, NULL);
} else {
InitXSource();
}
if (use_gtk_message_pump)
InitializeEventsToCapture();
#else
InitXSource();
#endif
}
MessagePumpX::~MessagePumpX() {
#if defined(TOOLKIT_USES_GTK)
if (use_gtk_message_pump) {
gdk_window_remove_filter(NULL, &GdkEventFilter, this);
gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
this, NULL);
return;
}
#endif
g_source_destroy(x_source_);
g_source_unref(x_source_);
XCloseDisplay(g_xdisplay);
g_xdisplay = NULL;
}
#if defined(TOOLKIT_USES_GTK)
// static
void MessagePumpX::DisableGtkMessagePump() {
use_gtk_message_pump = false;
}
#endif
// static
Display* MessagePumpX::GetDefaultXDisplay() {
#if defined(TOOLKIT_USES_GTK)
if (use_gtk_message_pump) {
static GdkDisplay* display = gdk_display_get_default();
return display ? GDK_DISPLAY_XDISPLAY(display) : NULL;
}
#endif
if (!g_xdisplay)
g_xdisplay = XOpenDisplay(NULL);
return g_xdisplay;
}
// static
bool MessagePumpX::HasXInput2() {
return xiopcode != -1;
}
void MessagePumpX::InitXSource() {
DCHECK(!x_source_);
GPollFD* x_poll = new GPollFD();
x_poll->fd = ConnectionNumber(g_xdisplay);
x_poll->events = G_IO_IN;
x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource));
g_source_add_poll(x_source_, x_poll);
g_source_set_can_recurse(x_source_, FALSE);
g_source_attach(x_source_, g_main_context_default());
}
bool MessagePumpX::ShouldCaptureXEvent(XEvent* xev) {
return
#if defined(TOOLKIT_USES_GTK)
(!use_gtk_message_pump || capture_x_events_[xev->type]) &&
#endif
(xev->type != GenericEvent || xev->xcookie.extension == xiopcode);
}
bool MessagePumpX::ProcessXEvent(XEvent* xev) {
bool should_quit = false;
bool have_cookie = false;
if (xev->type == GenericEvent &&
XGetEventData(xev->xgeneric.display, &xev->xcookie)) {
have_cookie = true;
}
if (WillProcessXEvent(xev) == EVENT_CONTINUE) {
MessagePumpDispatcher::DispatchStatus status =
GetDispatcher()->Dispatch(xev);
if (status == MessagePumpDispatcher::EVENT_QUIT) {
should_quit = true;
Quit();
} else if (status == MessagePumpDispatcher::EVENT_IGNORED) {
VLOG(1) << "Event (" << xev->type << ") not handled.";
}
DidProcessXEvent(xev);
}
if (have_cookie) {
XFreeEventData(xev->xgeneric.display, &xev->xcookie);
}
return should_quit;
}
bool MessagePumpX::RunOnce(GMainContext* context, bool block) {
Display* display = GetDefaultXDisplay();
if (!display || !GetDispatcher())
return g_main_context_iteration(context, block);
if (XPending(display)) {
XEvent xev;
XPeekEvent(display, &xev);
if (ShouldCaptureXEvent(&xev)) {
XNextEvent(display, &xev);
if (ProcessXEvent(&xev))
return true;
#if defined(TOOLKIT_USES_GTK)
} else if (use_gtk_message_pump && gdksource_) {
// TODO(sad): A couple of extra events can still sneak in during this.
// Those should be sent back to the X queue from the dispatcher
// EventDispatcherX.
gdksource_->source_funcs->dispatch = gdkdispatcher_;
g_main_context_iteration(context, FALSE);
#endif
}
}
bool retvalue;
#if defined(TOOLKIT_USES_GTK)
if (gdksource_ && use_gtk_message_pump) {
// Replace the dispatch callback of the GDK event source temporarily so that
// it doesn't read events from X.
gboolean (*cb)(GSource*, GSourceFunc, void*) =
gdksource_->source_funcs->dispatch;
gdksource_->source_funcs->dispatch = PlaceholderDispatch;
dispatching_event_ = true;
retvalue = g_main_context_iteration(context, block);
dispatching_event_ = false;
gdksource_->source_funcs->dispatch = cb;
} else {
retvalue = g_main_context_iteration(context, block);
}
#else
retvalue = g_main_context_iteration(context, block);
#endif
return retvalue;
}
bool MessagePumpX::WillProcessXEvent(XEvent* xevent) {
ObserverListBase<MessagePumpObserver>::Iterator it(observers());
MessagePumpObserver* obs;
while ((obs = it.GetNext()) != NULL) {
if (obs->WillProcessEvent(xevent))
return true;
}
return false;
}
void MessagePumpX::DidProcessXEvent(XEvent* xevent) {
ObserverListBase<MessagePumpObserver>::Iterator it(observers());
MessagePumpObserver* obs;
while ((obs = it.GetNext()) != NULL) {
obs->DidProcessEvent(xevent);
}
}
#if defined(TOOLKIT_USES_GTK)
GdkFilterReturn MessagePumpX::GdkEventFilter(GdkXEvent* gxevent,
GdkEvent* gevent,
gpointer data) {
MessagePumpX* pump = static_cast<MessagePumpX*>(data);
XEvent* xev = static_cast<XEvent*>(gxevent);
if (pump->ShouldCaptureXEvent(xev) && pump->GetDispatcher()) {
pump->ProcessXEvent(xev);
return GDK_FILTER_REMOVE;
}
CHECK(use_gtk_message_pump) << "GdkEvent:" << gevent->type;
return GDK_FILTER_CONTINUE;
}
void MessagePumpX::EventDispatcherX(GdkEvent* event, gpointer data) {
MessagePumpX* pump_x = reinterpret_cast<MessagePumpX*>(data);
CHECK(use_gtk_message_pump) << "GdkEvent:" << event->type;
if (!pump_x->gdksource_) {
pump_x->gdksource_ = g_main_current_source();
if (pump_x->gdksource_)
pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch;
} else if (!pump_x->IsDispatchingEvent()) {
if (event->type != GDK_NOTHING &&
pump_x->capture_gdk_events_[event->type]) {
NOTREACHED() << "GDK received an event it shouldn't have:" << event->type;
}
}
gtk_main_do_event(event);
}
void MessagePumpX::InitializeEventsToCapture(void) {
// TODO(sad): Decide which events we want to capture and update the tables
// accordingly.
capture_x_events_[KeyPress] = true;
capture_gdk_events_[GDK_KEY_PRESS] = true;
capture_x_events_[KeyRelease] = true;
capture_gdk_events_[GDK_KEY_RELEASE] = true;
capture_x_events_[ButtonPress] = true;
capture_gdk_events_[GDK_BUTTON_PRESS] = true;
capture_x_events_[ButtonRelease] = true;
capture_gdk_events_[GDK_BUTTON_RELEASE] = true;
capture_x_events_[MotionNotify] = true;
capture_gdk_events_[GDK_MOTION_NOTIFY] = true;
capture_x_events_[GenericEvent] = true;
}
COMPILE_ASSERT(XLASTEvent >= LASTEvent, XLASTEvent_too_small);
#endif // defined(TOOLKIT_USES_GTK)
} // namespace base
|