Refactor the glib message-pump, and use it as the base for a gtk message pump and an X message pump.
The changes:
* Rename MessagePumpGlibX to MessagePumpX.
* Rename MessagePumpForUI to MessagePumpGlib.
* Move some stuff out of MessagePumpGlib, and into MessagePumpGtk and MessagePumpX.
* Rename MessagePumpForUI::Observer to MessageObserver, moved the platform-specific implementations into MessagePumpGtk and MessagePumpX. Ditto for MessagePumpForUI::Dispatcher.
MessagePumpX is independent of MessagePumpGtk. At the moment, MessagePumpX does process some GDK event, but once we have a complete native_widget_x, we can take out the GDK processing and things should continue to work.
BUG=none
TEST=existing message-pump tests.
Review URL: http://codereview.chromium.org/7250001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90418 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: 2047ef4d68229d47cba8bf7f2e0465ab31f8b3ce
diff --git a/base/base.gypi b/base/base.gypi
index dac1bd2..41ea923 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -368,9 +368,15 @@
'sources!': [
'atomicops_internals_x86_gcc.cc',
'message_pump_glib.cc',
- 'message_pump_glib_x.cc',
+ 'message_pump_gtk.cc',
+ 'message_pump_x.cc',
],
}],
+ [ 'touchui==0', {
+ 'sources!' : [ 'message_pump_x.cc', ],
+ }, {
+ 'sources!' : [ 'message_pump_gtk.cc', ],
+ }],
[ 'OS != "linux"', {
'sources!': [
# Not automatically excluded by the *linux.cc rules.
@@ -571,9 +577,10 @@
'md5.h',
'message_pump_glib.cc',
'message_pump_glib.h',
- 'message_pump_glib_x.cc',
- 'message_pump_glib_x.h',
- 'message_pump_glib_x_dispatch.h',
+ 'message_pump_gtk.cc',
+ 'message_pump_gtk.h',
+ 'message_pump_x.cc',
+ 'message_pump_x.h',
'message_pump_libevent.cc',
'message_pump_libevent.h',
'message_pump_mac.h',
diff --git a/base/message_loop.cc b/base/message_loop.cc
index b0526f4..e45c0d7 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -25,14 +25,16 @@
#if defined(OS_POSIX)
#include "base/message_pump_libevent.h"
#endif
+
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
-#include "base/message_pump_glib.h"
-#endif
#if defined(TOUCH_UI)
-#include "base/message_pump_glib_x.h"
-#endif
+#include "base/message_pump_x.h"
+#else
+#include "base/message_pump_gtk.h"
+#endif // defined(TOUCH_UI)
+#endif // defined(OS_POSIX) && !defined(OS_MACOSX)
using base::TimeDelta;
using base::TimeTicks;
@@ -176,7 +178,7 @@
#define MESSAGE_PUMP_UI base::MessagePumpMac::Create()
#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
#elif defined(TOUCH_UI)
-#define MESSAGE_PUMP_UI new base::MessagePumpGlibX()
+#define MESSAGE_PUMP_UI new base::MessagePumpX()
#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
#elif defined(OS_NACL)
// Currently NaCl doesn't have a UI or an IO MessageLoop.
@@ -184,7 +186,7 @@
#define MESSAGE_PUMP_UI NULL
#define MESSAGE_PUMP_IO NULL
#elif defined(OS_POSIX) // POSIX but not MACOSX.
-#define MESSAGE_PUMP_UI new base::MessagePumpForUI()
+#define MESSAGE_PUMP_UI new base::MessagePumpGtk()
#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
#else
#error Not implemented
diff --git a/base/message_loop.h b/base/message_loop.h
index 9a03d6b..8fd9cbc 100644
--- a/base/message_loop.h
+++ b/base/message_loop.h
@@ -27,13 +27,14 @@
#elif defined(OS_POSIX)
#include "base/message_pump_libevent.h"
#if !defined(OS_MACOSX)
-#include "base/message_pump_glib.h"
+#if defined(TOUCH_UI)
+#include "base/message_pump_x.h"
+#else
+#include "base/message_pump_gtk.h"
+#endif
typedef struct _XDisplay Display;
#endif
#endif
-#if defined(TOUCH_UI)
-#include "base/message_pump_glib_x_dispatch.h"
-#endif
namespace base {
class Histogram;
@@ -80,12 +81,9 @@
#if defined(OS_WIN)
typedef base::MessagePumpWin::Dispatcher Dispatcher;
typedef base::MessagePumpForUI::Observer Observer;
-#elif defined(TOUCH_UI)
- typedef base::MessagePumpGlibXDispatcher Dispatcher;
- typedef base::MessagePumpXObserver Observer;
#elif !defined(OS_MACOSX)
- typedef base::MessagePumpForUI::Dispatcher Dispatcher;
- typedef base::MessagePumpForUI::Observer Observer;
+ typedef base::MessagePumpDispatcher Dispatcher;
+ typedef base::MessagePumpObserver Observer;
#endif
// A MessageLoop has a particular type, which indicates the set of
diff --git a/base/message_pump_glib.cc b/base/message_pump_glib.cc
index 721fedb..20fc8ea 100644
--- a/base/message_pump_glib.cc
+++ b/base/message_pump_glib.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -7,7 +7,6 @@
#include <fcntl.h>
#include <math.h>
-#include <gtk/gtk.h>
#include <glib.h>
#include "base/eintr_wrapper.h"
@@ -85,7 +84,7 @@
// loop, around event handling.
struct WorkSource : public GSource {
- base::MessagePumpForUI* pump;
+ base::MessagePumpGlib* pump;
};
gboolean WorkSourcePrepare(GSource* source,
@@ -124,9 +123,9 @@
namespace base {
-struct MessagePumpForUI::RunState {
+struct MessagePumpGlib::RunState {
Delegate* delegate;
- Dispatcher* dispatcher;
+ MessagePumpDispatcher* dispatcher;
// Used to flag that the current Run() invocation should return ASAP.
bool should_quit;
@@ -140,7 +139,7 @@
bool has_work;
};
-MessagePumpForUI::MessagePumpForUI()
+MessagePumpGlib::MessagePumpGlib()
: state_(NULL),
context_(g_main_context_default()),
wakeup_gpollfd_(new GPollFD) {
@@ -160,26 +159,23 @@
// This is needed to allow Run calls inside Dispatch.
g_source_set_can_recurse(work_source_, TRUE);
g_source_attach(work_source_, context_);
- gdk_event_handler_set(&EventDispatcher, this, NULL);
}
-MessagePumpForUI::~MessagePumpForUI() {
- gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
- this, NULL);
+MessagePumpGlib::~MessagePumpGlib() {
g_source_destroy(work_source_);
g_source_unref(work_source_);
close(wakeup_pipe_read_);
close(wakeup_pipe_write_);
}
-void MessagePumpForUI::RunWithDispatcher(Delegate* delegate,
- Dispatcher* dispatcher) {
+void MessagePumpGlib::RunWithDispatcher(Delegate* delegate,
+ MessagePumpDispatcher* dispatcher) {
#ifndef NDEBUG
- // Make sure we only run this on one thread. GTK only has one message pump
+ // Make sure we only run this on one thread. X/GTK only has one message pump
// so we can only have one UI loop per process.
static base::PlatformThreadId thread_id = base::PlatformThread::CurrentId();
DCHECK(thread_id == base::PlatformThread::CurrentId()) <<
- "Running MessagePumpForUI on two different threads; "
+ "Running MessagePumpGlib on two different threads; "
"this is unsupported by GLib!";
#endif
@@ -231,13 +227,8 @@
state_ = previous_state;
}
-bool MessagePumpForUI::RunOnce(GMainContext* context, bool block) {
- // g_main_context_iteration returns true if events have been dispatched.
- return g_main_context_iteration(context, block);
-}
-
// Return the timeout we want passed to poll.
-int MessagePumpForUI::HandlePrepare() {
+int MessagePumpGlib::HandlePrepare() {
// We know we have work, but we haven't called HandleDispatch yet. Don't let
// the pump block so that we can do some processing.
if (state_ && // state_ may be null during tests.
@@ -249,7 +240,7 @@
return GetTimeIntervalMilliseconds(delayed_work_time_);
}
-bool MessagePumpForUI::HandleCheck() {
+bool MessagePumpGlib::HandleCheck() {
if (!state_) // state_ may be null during tests.
return false;
@@ -279,11 +270,11 @@
return false;
}
-void MessagePumpForUI::HandleDispatch() {
+void MessagePumpGlib::HandleDispatch() {
state_->has_work = false;
if (state_->delegate->DoWork()) {
// NOTE: on Windows at this point we would call ScheduleWork (see
- // MessagePumpForUI::HandleWorkMessage in message_pump_win.cc). But here,
+ // MessagePumpGlib::HandleWorkMessage in message_pump_win.cc). But here,
// instead of posting a message on the wakeup pipe, we can avoid the
// syscalls and just signal that we have more work.
state_->has_work = true;
@@ -295,30 +286,19 @@
state_->delegate->DoDelayedWork(&delayed_work_time_);
}
-void MessagePumpForUI::AddObserver(Observer* observer) {
+void MessagePumpGlib::AddObserver(MessagePumpObserver* observer) {
observers_.AddObserver(observer);
}
-void MessagePumpForUI::RemoveObserver(Observer* observer) {
+void MessagePumpGlib::RemoveObserver(MessagePumpObserver* observer) {
observers_.RemoveObserver(observer);
}
-void MessagePumpForUI::DispatchEvents(GdkEvent* event) {
- WillProcessEvent(event);
- if (state_ && state_->dispatcher) { // state_ may be null during tests.
- if (!state_->dispatcher->Dispatch(event))
- state_->should_quit = true;
- } else {
- gtk_main_do_event(event);
- }
- DidProcessEvent(event);
-}
-
-void MessagePumpForUI::Run(Delegate* delegate) {
+void MessagePumpGlib::Run(Delegate* delegate) {
RunWithDispatcher(delegate, NULL);
}
-void MessagePumpForUI::Quit() {
+void MessagePumpGlib::Quit() {
if (state_) {
state_->should_quit = true;
} else {
@@ -326,7 +306,7 @@
}
}
-void MessagePumpForUI::ScheduleWork() {
+void MessagePumpGlib::ScheduleWork() {
// This can be called on any thread, so we don't want to touch any state
// variables as we would then need locks all over. This ensures that if
// we are sleeping in a poll that we will wake up.
@@ -336,29 +316,15 @@
}
}
-void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
+void MessagePumpGlib::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
// We need to wake up the loop in case the poll timeout needs to be
// adjusted. This will cause us to try to do work, but that's ok.
delayed_work_time_ = delayed_work_time;
ScheduleWork();
}
-MessagePumpForUI::Dispatcher* MessagePumpForUI::GetDispatcher() {
+MessagePumpDispatcher* MessagePumpGlib::GetDispatcher() {
return state_ ? state_->dispatcher : NULL;
}
-void MessagePumpForUI::WillProcessEvent(GdkEvent* event) {
- FOR_EACH_OBSERVER(Observer, observers_, WillProcessEvent(event));
-}
-
-void MessagePumpForUI::DidProcessEvent(GdkEvent* event) {
- FOR_EACH_OBSERVER(Observer, observers_, DidProcessEvent(event));
-}
-
-// static
-void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) {
- MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data);
- message_pump->DispatchEvents(event);
-}
-
} // namespace base
diff --git a/base/message_pump_glib.h b/base/message_pump_glib.h
index 32d0d8f..36d75cd 100644
--- a/base/message_pump_glib.h
+++ b/base/message_pump_glib.h
@@ -11,56 +11,40 @@
#include "base/observer_list.h"
#include "base/time.h"
-typedef union _GdkEvent GdkEvent;
typedef struct _GMainContext GMainContext;
typedef struct _GPollFD GPollFD;
typedef struct _GSource GSource;
namespace base {
-// This class implements a MessagePump needed for TYPE_UI MessageLoops on
-// OS_LINUX platforms using GLib.
-class MessagePumpForUI : public MessagePump {
+// MessagePumpObserver is notified prior to an event being dispatched. As
+// Observers are notified of every change, they have to be FAST! The platform
+// specific implementation of the class is in message_pump_gtk/message_pump_x.
+class MessagePumpObserver;
+
+// MessagePumpDispatcher is used during a nested invocation of Run to dispatch
+// events. If Run is invoked with a non-NULL MessagePumpDispatcher, MessageLoop
+// does not dispatch events (or invoke gtk_main_do_event), rather every event is
+// passed to Dispatcher's Dispatch method for dispatch. It is up to the
+// Dispatcher to dispatch, or not, the event. The platform specific
+// implementation of the class is in message_pump_gtk/message_pump_x.
+class MessagePumpDispatcher;
+
+// This class implements a base MessagePump needed for TYPE_UI MessageLoops on
+// platforms using GLib.
+class MessagePumpGlib : public MessagePump {
public:
- // Observer is notified prior to a GdkEvent event being dispatched. As
- // Observers are notified of every change, they have to be FAST!
- class Observer {
- public:
- virtual ~Observer() {}
+ MessagePumpGlib();
+ virtual ~MessagePumpGlib();
- // This method is called before processing a message.
- virtual void WillProcessEvent(GdkEvent* event) = 0;
-
- // This method is called after processing a message.
- virtual void DidProcessEvent(GdkEvent* event) = 0;
- };
-
- // Dispatcher is used during a nested invocation of Run to dispatch events.
- // If Run is invoked with a non-NULL Dispatcher, MessageLoop does not
- // dispatch events (or invoke gtk_main_do_event), rather every event is
- // passed to Dispatcher's Dispatch method for dispatch. It is up to the
- // Dispatcher to dispatch, or not, the event.
- //
- // The nested loop is exited by either posting a quit, or returning false
- // from Dispatch.
- class Dispatcher {
- public:
- virtual ~Dispatcher() {}
- // Dispatches the event. If true is returned processing continues as
- // normal. If false is returned, the nested loop exits immediately.
- virtual bool Dispatch(GdkEvent* event) = 0;
- };
-
- MessagePumpForUI();
- virtual ~MessagePumpForUI();
-
- // Like MessagePump::Run, but GdkEvent objects are routed through dispatcher.
- virtual void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher);
+ // Like MessagePump::Run, but events are routed through dispatcher.
+ virtual void RunWithDispatcher(Delegate* delegate,
+ MessagePumpDispatcher* dispatcher);
// Run a single iteration of the mainloop. A return value of true indicates
// that an event was handled. |block| indicates if it should wait if no event
// is ready for processing.
- virtual bool RunOnce(GMainContext* context, bool block);
+ virtual bool RunOnce(GMainContext* context, bool block) = 0;
// Internal methods used for processing the pump callbacks. They are
// public for simplicity but should not be used directly. HandlePrepare
@@ -73,15 +57,11 @@
void HandleDispatch();
// Adds an Observer, which will start receiving notifications immediately.
- void AddObserver(Observer* observer);
+ void AddObserver(MessagePumpObserver* observer);
// Removes an Observer. It is safe to call this method while an Observer is
// receiving a notification callback.
- void RemoveObserver(Observer* observer);
-
- // Dispatch an available GdkEvent. Essentially this allows a subclass to do
- // some task before/after calling the default handler (EventDispatcher).
- virtual void DispatchEvents(GdkEvent* event);
+ void RemoveObserver(MessagePumpObserver* observer);
// Overridden from MessagePump:
virtual void Run(Delegate* delegate);
@@ -91,26 +71,15 @@
protected:
// Returns the dispatcher for the current run state (|state_->dispatcher|).
- Dispatcher* GetDispatcher();
+ MessagePumpDispatcher* GetDispatcher();
- ObserverList<Observer>& observers() { return observers_; }
+ ObserverList<MessagePumpObserver>& observers() { return observers_; }
private:
// We may make recursive calls to Run, so we save state that needs to be
// separate between them in this structure type.
struct RunState;
- // Invoked from EventDispatcher. Notifies all observers we're about to
- // process an event.
- void WillProcessEvent(GdkEvent* event);
-
- // Invoked from EventDispatcher. Notifies all observers we processed an
- // event.
- void DidProcessEvent(GdkEvent* event);
-
- // Callback prior to gdk dispatching an event.
- static void EventDispatcher(GdkEvent* event, void* data);
-
RunState* state_;
// This is a GLib structure that we can add event sources to. We use the
@@ -135,9 +104,9 @@
scoped_ptr<GPollFD> wakeup_gpollfd_;
// List of observers.
- ObserverList<Observer> observers_;
+ ObserverList<MessagePumpObserver> observers_;
- DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI);
+ DISALLOW_COPY_AND_ASSIGN(MessagePumpGlib);
};
} // namespace base
diff --git a/base/message_pump_glib_x.h b/base/message_pump_glib_x.h
deleted file mode 100644
index ed29e6a..0000000
--- a/base/message_pump_glib_x.h
+++ /dev/null
@@ -1,97 +0,0 @@
-// 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.
-
-#ifndef BASE_MESSAGE_PUMP_GLIB_X_H
-#define BASE_MESSAGE_PUMP_GLIB_X_H
-
-#include "base/message_pump.h"
-#include "base/message_pump_glib.h"
-
-#include <bitset>
-
-#include <glib.h>
-#include <gtk/gtk.h>
-#include <X11/X.h>
-
-typedef union _XEvent XEvent;
-
-namespace base {
-
-class MessagePumpGlibX : public MessagePumpForUI {
- public:
- MessagePumpGlibX();
- virtual ~MessagePumpGlibX();
-
- // Indicates whether a GDK event was injected by chrome (when |true|) or if it
- // was captured and being processed by GDK (when |false|).
- bool IsDispatchingEvent(void) { return dispatching_event_; }
-
- // Overridden from MessagePumpForUI:
- virtual bool RunOnce(GMainContext* context, bool block);
-
- private:
- // Some XEvent's can't be directly read from X event queue and will go
- // through GDK's dispatching process and may get discarded. This function
- // sets up a filter to intercept those XEvent's we are interested in
- // and dispatches them so that they won't get lost.
- static GdkFilterReturn GdkEventFilter(GdkXEvent* gxevent,
- GdkEvent* gevent,
- gpointer data);
-
- static void EventDispatcherX(GdkEvent* event, gpointer data);
-
- // Decides whether we are interested in processing this XEvent.
- bool ShouldCaptureXEvent(XEvent* event);
-
- // Dispatches the XEvent and returns true if we should exit the current loop
- // of message processing.
- bool ProcessXEvent(XEvent* event);
-
- // Sends the event to the observers. If an observer returns true, then it does
- // not send the event to any other observers and returns true. Returns false
- // if no observer returns true.
- bool WillProcessXEvent(XEvent* xevent);
-
- // Update the lookup table and flag the events that should be captured and
- // processed so that GDK doesn't get to them.
- void InitializeEventsToCapture(void);
-
-#if defined(HAVE_XINPUT2)
- // Initialize X2 input.
- void InitializeXInput2(void);
-
- // The opcode used for checking events.
- int xiopcode_;
-#endif
-
- // The event source for GDK events.
- GSource* gdksource_;
-
- // The default GDK event dispatcher. This is stored so that it can be restored
- // when necessary during nested event dispatching.
- gboolean (*gdkdispatcher_)(GSource*, GSourceFunc, void*);
-
- // Indicates whether a GDK event was injected by chrome (when |true|) or if it
- // was captured and being processed by GDK (when |false|).
- bool dispatching_event_;
-
-#if ! GTK_CHECK_VERSION(2,18,0)
-// GDK_EVENT_LAST was introduced in GTK+ 2.18.0. For earlier versions, we pick a
-// large enough value (the value of GDK_EVENT_LAST in 2.18.0) so that it works
-// for all versions.
-#define GDK_EVENT_LAST 37
-#endif
-
- // We do not want to process all the events ourselves. So we use a lookup
- // table to quickly check if a particular event should be handled by us or if
- // it should be passed on to the default GDK handler.
- std::bitset<LASTEvent> capture_x_events_;
- std::bitset<GDK_EVENT_LAST> capture_gdk_events_;
-
- DISALLOW_COPY_AND_ASSIGN(MessagePumpGlibX);
-};
-
-} // namespace base
-
-#endif // BASE_MESSAGE_PUMP_GLIB_X_H
diff --git a/base/message_pump_glib_x_dispatch.h b/base/message_pump_glib_x_dispatch.h
deleted file mode 100644
index 9a2358a..0000000
--- a/base/message_pump_glib_x_dispatch.h
+++ /dev/null
@@ -1,46 +0,0 @@
-// 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.
-
-#ifndef BASE_MESSAGE_PUMP_GLIB_X_DISPATCH_H
-#define BASE_MESSAGE_PUMP_GLIB_X_DISPATCH_H
-
-#include "base/base_api.h"
-#include "base/message_pump.h"
-#include "base/message_pump_glib.h"
-
-typedef union _XEvent XEvent;
-
-namespace base {
-
-// The message pump used for TOUCH_UI on linux is MessagePumpGlibX, which can
-// dispatch both GdkEvents* and XEvents* captured directly from X.
-// MessagePumpForUI::Dispatcher provides the mechanism for dispatching
-// GdkEvents. This class provides additional mechanism for dispatching XEvents.
-class MessagePumpGlibXDispatcher : public MessagePumpForUI::Dispatcher {
- public:
- enum DispatchStatus {
- EVENT_IGNORED, // The event was not processed.
- EVENT_PROCESSED, // The event has been processed.
- EVENT_QUIT // The event was processed and the message-loop should
- // terminate.
- };
-
- // Dispatches the event. EVENT_IGNORED is returned if the event was ignored
- // (i.e. not processed). EVENT_PROCESSED is returned if the event was
- // processed. The nested loop exits immediately if EVENT_QUIT is returned.
- virtual DispatchStatus DispatchX(XEvent* xevent) = 0;
-};
-
-class BASE_API MessagePumpXObserver : public MessagePumpForUI::Observer {
- public:
- // This method is called before processing an XEvent. If the method returns
- // true, it indicates the event has already been handled, so the event is not
- // processed any farther. If the method returns false, the event dispatching
- // proceeds as normal.
- virtual bool WillProcessXEvent(XEvent* xevent);
-};
-
-} // namespace base
-
-#endif // BASE_MESSAGE_PUMP_GLIB_X_DISPATCH_H
diff --git a/base/message_pump_gtk.cc b/base/message_pump_gtk.cc
new file mode 100644
index 0000000..f5ed042
--- /dev/null
+++ b/base/message_pump_gtk.cc
@@ -0,0 +1,52 @@
+// 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_gtk.h"
+
+#include <gtk/gtk.h>
+
+namespace base {
+
+MessagePumpGtk::MessagePumpGtk() : MessagePumpGlib() {
+ gdk_event_handler_set(&EventDispatcher, this, NULL);
+}
+
+MessagePumpGtk::~MessagePumpGtk() {
+ gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
+ this, NULL);
+}
+
+void MessagePumpGtk::DispatchEvents(GdkEvent* event) {
+ WillProcessEvent(event);
+
+ MessagePumpDispatcher* dispatcher = GetDispatcher();
+ if (!dispatcher)
+ gtk_main_do_event(event);
+ else if (!dispatcher->Dispatch(event))
+ Quit();
+
+ DidProcessEvent(event);
+}
+
+bool MessagePumpGtk::RunOnce(GMainContext* context, bool block) {
+ // g_main_context_iteration returns true if events have been dispatched.
+ return g_main_context_iteration(context, block);
+}
+
+void MessagePumpGtk::WillProcessEvent(GdkEvent* event) {
+ FOR_EACH_OBSERVER(MessagePumpObserver, observers(), WillProcessEvent(event));
+}
+
+void MessagePumpGtk::DidProcessEvent(GdkEvent* event) {
+ FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(event));
+}
+
+// static
+void MessagePumpGtk::EventDispatcher(GdkEvent* event, gpointer data) {
+ MessagePumpGtk* message_pump = reinterpret_cast<MessagePumpGtk*>(data);
+ message_pump->DispatchEvents(event);
+}
+
+} // namespace base
+
diff --git a/base/message_pump_gtk.h b/base/message_pump_gtk.h
new file mode 100644
index 0000000..72eaafa
--- /dev/null
+++ b/base/message_pump_gtk.h
@@ -0,0 +1,74 @@
+// 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.
+
+#ifndef BASE_MESSAGE_PUMP_GTK_H_
+#define BASE_MESSAGE_PUMP_GTK_H_
+#pragma once
+
+#include "base/message_pump_glib.h"
+
+typedef union _GdkEvent GdkEvent;
+
+namespace base {
+
+// The documentation for this class is in message_pump_glib.h
+class MessagePumpObserver {
+ public:
+ // This method is called before processing a message.
+ virtual void WillProcessEvent(GdkEvent* event) = 0;
+
+ // This method is called after processing a message.
+ virtual void DidProcessEvent(GdkEvent* event) = 0;
+
+ protected:
+ virtual ~MessagePumpObserver() {}
+};
+
+// The documentation for this class is in message_pump_glib.h
+//
+// The nested loop is exited by either posting a quit, or returning false
+// from Dispatch.
+class MessagePumpDispatcher {
+ public:
+ // Dispatches the event. If true is returned processing continues as
+ // normal. If false is returned, the nested loop exits immediately.
+ virtual bool Dispatch(GdkEvent* event) = 0;
+
+ protected:
+ virtual ~MessagePumpDispatcher() {}
+};
+
+// This class implements a message-pump for dispatching GTK events.
+class MessagePumpGtk : public MessagePumpGlib {
+ public:
+ MessagePumpGtk();
+ virtual ~MessagePumpGtk();
+
+ // Dispatch an available GdkEvent. Essentially this allows a subclass to do
+ // some task before/after calling the default handler (EventDispatcher).
+ void DispatchEvents(GdkEvent* event);
+
+ private:
+ // Overridden from MessagePumpGlib
+ virtual bool RunOnce(GMainContext* context, bool block) OVERRIDE;
+
+ // Invoked from EventDispatcher. Notifies all observers we're about to
+ // process an event.
+ void WillProcessEvent(GdkEvent* event);
+
+ // Invoked from EventDispatcher. Notifies all observers we processed an
+ // event.
+ void DidProcessEvent(GdkEvent* event);
+
+ // Callback prior to gdk dispatching an event.
+ static void EventDispatcher(GdkEvent* event, void* data);
+
+ DISALLOW_COPY_AND_ASSIGN(MessagePumpGtk);
+};
+
+typedef MessagePumpGtk MessagePumpForUI;
+
+} // namespace base
+
+#endif // BASE_MESSAGE_PUMP_GTK_H_
diff --git a/base/message_pump_glib_x.cc b/base/message_pump_x.cc
similarity index 72%
rename from base/message_pump_glib_x.cc
rename to base/message_pump_x.cc
index 430d937..a50e182 100644
--- a/base/message_pump_glib_x.cc
+++ b/base/message_pump_x.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/message_pump_glib_x.h"
+#include "base/message_pump_x.h"
#include <gdk/gdkx.h>
#if defined(HAVE_XINPUT2)
@@ -11,7 +11,7 @@
#include <X11/Xlib.h>
#endif
-#include "base/message_pump_glib_x_dispatch.h"
+#include "base/basictypes.h"
namespace {
@@ -25,7 +25,7 @@
namespace base {
-MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(),
+MessagePumpX::MessagePumpX() : MessagePumpGlib(),
#if defined(HAVE_XINPUT2)
xiopcode_(-1),
#endif
@@ -42,15 +42,13 @@
InitializeEventsToCapture();
}
-MessagePumpGlibX::~MessagePumpGlibX() {
+MessagePumpX::~MessagePumpX() {
gdk_window_remove_filter(NULL, &GdkEventFilter, this);
-
- // It is not necessary to reset the GDK event handler using
- // gdk_event_handler_set since it's done in the destructor for
- // MessagePumpForUI.
+ gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
+ this, NULL);
}
-bool MessagePumpGlibX::ShouldCaptureXEvent(XEvent* xev) {
+bool MessagePumpX::ShouldCaptureXEvent(XEvent* xev) {
return capture_x_events_[xev->type]
#if defined(HAVE_XINPUT2)
&& (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_)
@@ -59,7 +57,7 @@
}
-bool MessagePumpGlibX::ProcessXEvent(XEvent* xev) {
+bool MessagePumpX::ProcessXEvent(XEvent* xev) {
bool should_quit = false;
#if defined(HAVE_XINPUT2)
@@ -70,15 +68,14 @@
}
#endif
- if (!WillProcessXEvent(xev)) {
- MessagePumpGlibXDispatcher::DispatchStatus status =
- static_cast<MessagePumpGlibXDispatcher*>
- (GetDispatcher())->DispatchX(xev);
+ if (WillProcessXEvent(xev) == MessagePumpObserver::EVENT_CONTINUE) {
+ MessagePumpDispatcher::DispatchStatus status =
+ GetDispatcher()->Dispatch(xev);
- if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) {
+ if (status == MessagePumpDispatcher::EVENT_QUIT) {
should_quit = true;
Quit();
- } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) {
+ } else if (status == MessagePumpDispatcher::EVENT_IGNORED) {
VLOG(1) << "Event (" << xev->type << ") not handled.";
}
}
@@ -92,10 +89,10 @@
return should_quit;
}
-bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) {
+bool MessagePumpX::RunOnce(GMainContext* context, bool block) {
GdkDisplay* gdisp = gdk_display_get_default();
if (!gdisp || !GetDispatcher())
- return MessagePumpForUI::RunOnce(context, block);
+ return g_main_context_iteration(context, block);
Display* display = GDK_DISPLAY_XDISPLAY(gdisp);
@@ -137,10 +134,10 @@
return retvalue;
}
-GdkFilterReturn MessagePumpGlibX::GdkEventFilter(GdkXEvent* gxevent,
- GdkEvent* gevent,
- gpointer data) {
- MessagePumpGlibX* pump = static_cast<MessagePumpGlibX*>(data);
+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()) {
@@ -151,20 +148,18 @@
return GDK_FILTER_CONTINUE;
}
-bool MessagePumpGlibX::WillProcessXEvent(XEvent* xevent) {
- ObserverListBase<Observer>::Iterator it(observers());
- Observer* obs;
+bool MessagePumpX::WillProcessXEvent(XEvent* xevent) {
+ ObserverListBase<MessagePumpObserver>::Iterator it(observers());
+ MessagePumpObserver* obs;
while ((obs = it.GetNext()) != NULL) {
- MessagePumpXObserver* xobs =
- static_cast<MessagePumpXObserver*>(obs);
- if (xobs->WillProcessXEvent(xevent))
+ if (obs->WillProcessXEvent(xevent))
return true;
}
return false;
}
-void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) {
- MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data);
+void MessagePumpX::EventDispatcherX(GdkEvent* event, gpointer data) {
+ MessagePumpX* pump_x = reinterpret_cast<MessagePumpX*>(data);
if (!pump_x->gdksource_) {
pump_x->gdksource_ = g_main_current_source();
@@ -177,10 +172,10 @@
}
}
- pump_x->DispatchEvents(event);
+ gtk_main_do_event(event);
}
-void MessagePumpGlibX::InitializeEventsToCapture(void) {
+void MessagePumpX::InitializeEventsToCapture(void) {
// TODO(sad): Decide which events we want to capture and update the tables
// accordingly.
capture_x_events_[KeyPress] = true;
@@ -204,7 +199,7 @@
}
#if defined(HAVE_XINPUT2)
-void MessagePumpGlibX::InitializeXInput2(void) {
+void MessagePumpX::InitializeXInput2(void) {
GdkDisplay* display = gdk_display_get_default();
if (!display)
return;
@@ -227,8 +222,11 @@
}
#endif // HAVE_XINPUT2
-bool MessagePumpXObserver::WillProcessXEvent(XEvent* xev) {
- return false;
+MessagePumpObserver::EventStatus
+ MessagePumpObserver::WillProcessXEvent(XEvent* xev) {
+ return EVENT_CONTINUE;
}
+COMPILE_ASSERT(XLASTEvent >= LASTEvent, XLASTEvent_too_small);
+
} // namespace base
diff --git a/base/message_pump_x.h b/base/message_pump_x.h
new file mode 100644
index 0000000..f7f271c
--- /dev/null
+++ b/base/message_pump_x.h
@@ -0,0 +1,144 @@
+// 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.
+
+#ifndef BASE_MESSAGE_PUMP_X_H
+#define BASE_MESSAGE_PUMP_X_H
+
+#include "base/message_pump.h"
+#include "base/message_pump_glib.h"
+
+#include <bitset>
+
+#include <glib.h>
+#include <gtk/gtk.h>
+
+typedef union _XEvent XEvent;
+
+namespace base {
+
+// The documentation for this class is in message_pump_glib.h
+class BASE_API MessagePumpObserver {
+ public:
+ enum EventStatus {
+ EVENT_CONTINUE, // The event should be dispatched as normal.
+ EVENT_HANDLED // The event should not be processed any farther.
+ };
+
+ // This method is called before processing an XEvent. If the method returns
+ // EVENT_HANDLED, it indicates the event has already been handled, so the
+ // event is not processed any farther. If the method returns EVENT_CONTINUE,
+ // the event dispatching proceeds as normal.
+ virtual EventStatus WillProcessXEvent(XEvent* xevent);
+
+ protected:
+ virtual ~MessagePumpObserver() {}
+};
+
+// The documentation for this class is in message_pump_glib.h
+//
+// The nested loop is exited by either posting a quit, or returning EVENT_QUIT
+// from Dispatch.
+class MessagePumpDispatcher {
+ public:
+ enum DispatchStatus {
+ EVENT_IGNORED, // The event was not processed.
+ EVENT_PROCESSED, // The event has been processed.
+ EVENT_QUIT // The event was processed and the message-loop should
+ // terminate.
+ };
+
+ // Dispatches the event. EVENT_IGNORED is returned if the event was ignored
+ // (i.e. not processed). EVENT_PROCESSED is returned if the event was
+ // processed. The nested loop exits immediately if EVENT_QUIT is returned.
+ virtual DispatchStatus Dispatch(XEvent* xevent) = 0;
+
+ protected:
+ virtual ~MessagePumpDispatcher() {}
+};
+
+// This class implements a message-pump for dispatching X events.
+class MessagePumpX : public MessagePumpGlib {
+ public:
+ MessagePumpX();
+ virtual ~MessagePumpX();
+
+ // Indicates whether a GDK event was injected by chrome (when |true|) or if it
+ // was captured and being processed by GDK (when |false|).
+ bool IsDispatchingEvent(void) { return dispatching_event_; }
+
+ // Overridden from MessagePumpGlib:
+ virtual bool RunOnce(GMainContext* context, bool block) OVERRIDE;
+
+ private:
+ // Some XEvent's can't be directly read from X event queue and will go
+ // through GDK's dispatching process and may get discarded. This function
+ // sets up a filter to intercept those XEvent's we are interested in
+ // and dispatches them so that they won't get lost.
+ static GdkFilterReturn GdkEventFilter(GdkXEvent* gxevent,
+ GdkEvent* gevent,
+ gpointer data);
+
+ static void EventDispatcherX(GdkEvent* event, gpointer data);
+
+ // Decides whether we are interested in processing this XEvent.
+ bool ShouldCaptureXEvent(XEvent* event);
+
+ // Dispatches the XEvent and returns true if we should exit the current loop
+ // of message processing.
+ bool ProcessXEvent(XEvent* event);
+
+ // Sends the event to the observers. If an observer returns true, then it does
+ // not send the event to any other observers and returns true. Returns false
+ // if no observer returns true.
+ bool WillProcessXEvent(XEvent* xevent);
+
+ // Update the lookup table and flag the events that should be captured and
+ // processed so that GDK doesn't get to them.
+ void InitializeEventsToCapture(void);
+
+#if defined(HAVE_XINPUT2)
+ // Initialize X2 input.
+ void InitializeXInput2(void);
+
+ // The opcode used for checking events.
+ int xiopcode_;
+#endif
+
+ // The event source for GDK events.
+ GSource* gdksource_;
+
+ // The default GDK event dispatcher. This is stored so that it can be restored
+ // when necessary during nested event dispatching.
+ gboolean (*gdkdispatcher_)(GSource*, GSourceFunc, void*);
+
+ // Indicates whether a GDK event was injected by chrome (when |true|) or if it
+ // was captured and being processed by GDK (when |false|).
+ bool dispatching_event_;
+
+#if ! GTK_CHECK_VERSION(2,18,0)
+// GDK_EVENT_LAST was introduced in GTK+ 2.18.0. For earlier versions, we pick a
+// large enough value (the value of GDK_EVENT_LAST in 2.18.0) so that it works
+// for all versions.
+#define GDK_EVENT_LAST 37
+#endif
+
+// Ideally we would #include X.h for LASTEvent, but it brings in a lot of stupid
+// stuff (like Time, CurrentTime etc.) that messes up a lot of things. So define
+// XLASTEvent here to a large enough value so that it works.
+#define XLASTEvent 40
+
+ // We do not want to process all the events ourselves. So we use a lookup
+ // table to quickly check if a particular event should be handled by us or if
+ // it should be passed on to the default GDK handler.
+ std::bitset<XLASTEvent> capture_x_events_;
+ std::bitset<GDK_EVENT_LAST> capture_gdk_events_;
+
+ DISALLOW_COPY_AND_ASSIGN(MessagePumpX);
+};
+
+typedef MessagePumpX MessagePumpForUI;
+
+} // namespace base
+
+#endif // BASE_MESSAGE_PUMP_X_H