Revert 178383
[chromium-blink-merge.git] / base / message_pump_gtk.cc
blob780b4d8a60bf2731c5ae566a32c9c635e54169b4
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/message_pump_gtk.h"
7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h>
10 #include "base/profiler/scoped_profile.h"
11 #include "base/debug/trace_event.h"
13 namespace {
15 const char* EventToTypeString(const GdkEvent* event) {
16 switch (event->type) {
17 case GDK_NOTHING: return "GDK_NOTHING";
18 case GDK_DELETE: return "GDK_DELETE";
19 case GDK_DESTROY: return "GDK_DESTROY";
20 case GDK_EXPOSE: return "GDK_EXPOSE";
21 case GDK_MOTION_NOTIFY: return "GDK_MOTION_NOTIFY";
22 case GDK_BUTTON_PRESS: return "GDK_BUTTON_PRESS";
23 case GDK_2BUTTON_PRESS: return "GDK_2BUTTON_PRESS";
24 case GDK_3BUTTON_PRESS: return "GDK_3BUTTON_PRESS";
25 case GDK_BUTTON_RELEASE: return "GDK_BUTTON_RELEASE";
26 case GDK_KEY_PRESS: return "GDK_KEY_PRESS";
27 case GDK_KEY_RELEASE: return "GDK_KEY_RELEASE";
28 case GDK_ENTER_NOTIFY: return "GDK_ENTER_NOTIFY";
29 case GDK_LEAVE_NOTIFY: return "GDK_LEAVE_NOTIFY";
30 case GDK_FOCUS_CHANGE: return "GDK_FOCUS_CHANGE";
31 case GDK_CONFIGURE: return "GDK_CONFIGURE";
32 case GDK_MAP: return "GDK_MAP";
33 case GDK_UNMAP: return "GDK_UNMAP";
34 case GDK_PROPERTY_NOTIFY: return "GDK_PROPERTY_NOTIFY";
35 case GDK_SELECTION_CLEAR: return "GDK_SELECTION_CLEAR";
36 case GDK_SELECTION_REQUEST: return "GDK_SELECTION_REQUEST";
37 case GDK_SELECTION_NOTIFY: return "GDK_SELECTION_NOTIFY";
38 case GDK_PROXIMITY_IN: return "GDK_PROXIMITY_IN";
39 case GDK_PROXIMITY_OUT: return "GDK_PROXIMITY_OUT";
40 case GDK_DRAG_ENTER: return "GDK_DRAG_ENTER";
41 case GDK_DRAG_LEAVE: return "GDK_DRAG_LEAVE";
42 case GDK_DRAG_MOTION: return "GDK_DRAG_MOTION";
43 case GDK_DRAG_STATUS: return "GDK_DRAG_STATUS";
44 case GDK_DROP_START: return "GDK_DROP_START";
45 case GDK_DROP_FINISHED: return "GDK_DROP_FINISHED";
46 case GDK_CLIENT_EVENT: return "GDK_CLIENT_EVENT";
47 case GDK_VISIBILITY_NOTIFY: return "GDK_VISIBILITY_NOTIFY";
48 case GDK_NO_EXPOSE: return "GDK_NO_EXPOSE";
49 case GDK_SCROLL: return "GDK_SCROLL";
50 case GDK_WINDOW_STATE: return "GDK_WINDOW_STATE";
51 case GDK_SETTING: return "GDK_SETTING";
52 case GDK_OWNER_CHANGE: return "GDK_OWNER_CHANGE";
53 case GDK_GRAB_BROKEN: return "GDK_GRAB_BROKEN";
54 case GDK_DAMAGE: return "GDK_DAMAGE";
55 default:
56 return "Unknown Gdk Event";
62 namespace base {
64 MessagePumpGtk::MessagePumpGtk() : MessagePumpGlib() {
65 gdk_event_handler_set(&EventDispatcher, this, NULL);
68 void MessagePumpGtk::DispatchEvents(GdkEvent* event) {
69 UNSHIPPED_TRACE_EVENT1("task", "MessagePumpGtk::DispatchEvents",
70 "type", EventToTypeString(event));
72 WillProcessEvent(event);
74 MessagePumpDispatcher* dispatcher = GetDispatcher();
75 if (!dispatcher)
76 gtk_main_do_event(event);
77 else if (!dispatcher->Dispatch(event))
78 Quit();
80 DidProcessEvent(event);
83 // static
84 Display* MessagePumpGtk::GetDefaultXDisplay() {
85 static GdkDisplay* display = gdk_display_get_default();
86 if (!display) {
87 // GTK / GDK has not been initialized, which is a decision we wish to
88 // support, for example for the GPU process.
89 static Display* xdisplay = XOpenDisplay(NULL);
90 return xdisplay;
92 return GDK_DISPLAY_XDISPLAY(display);
95 MessagePumpGtk::~MessagePumpGtk() {
96 gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
97 this, NULL);
100 void MessagePumpGtk::WillProcessEvent(GdkEvent* event) {
101 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), WillProcessEvent(event));
104 void MessagePumpGtk::DidProcessEvent(GdkEvent* event) {
105 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(event));
108 // static
109 void MessagePumpGtk::EventDispatcher(GdkEvent* event, gpointer data) {
110 MessagePumpGtk* message_pump = reinterpret_cast<MessagePumpGtk*>(data);
111 message_pump->DispatchEvents(event);
114 } // namespace base