Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: base/message_pump_x.cc

Issue 7250001: Refactor the glib message-pump, and use it as the base for a gtk message pump and an X message pump. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: media.gyp update Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/message_pump_x.h ('k') | chrome/browser/chromeos/system_key_event_listener.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_pump_glib_x.h" 5 #include "base/message_pump_x.h"
6 6
7 #include <gdk/gdkx.h> 7 #include <gdk/gdkx.h>
8 #if defined(HAVE_XINPUT2) 8 #if defined(HAVE_XINPUT2)
9 #include <X11/extensions/XInput2.h> 9 #include <X11/extensions/XInput2.h>
10 #else 10 #else
11 #include <X11/Xlib.h> 11 #include <X11/Xlib.h>
12 #endif 12 #endif
13 13
14 #include "base/message_pump_glib_x_dispatch.h" 14 #include "base/basictypes.h"
15 15
16 namespace { 16 namespace {
17 17
18 gboolean PlaceholderDispatch(GSource* source, 18 gboolean PlaceholderDispatch(GSource* source,
19 GSourceFunc cb, 19 GSourceFunc cb,
20 gpointer data) { 20 gpointer data) {
21 return TRUE; 21 return TRUE;
22 } 22 }
23 23
24 } // namespace 24 } // namespace
25 25
26 namespace base { 26 namespace base {
27 27
28 MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(), 28 MessagePumpX::MessagePumpX() : MessagePumpGlib(),
29 #if defined(HAVE_XINPUT2) 29 #if defined(HAVE_XINPUT2)
30 xiopcode_(-1), 30 xiopcode_(-1),
31 #endif 31 #endif
32 gdksource_(NULL), 32 gdksource_(NULL),
33 dispatching_event_(false), 33 dispatching_event_(false),
34 capture_x_events_(0), 34 capture_x_events_(0),
35 capture_gdk_events_(0) { 35 capture_gdk_events_(0) {
36 gdk_window_add_filter(NULL, &GdkEventFilter, this); 36 gdk_window_add_filter(NULL, &GdkEventFilter, this);
37 gdk_event_handler_set(&EventDispatcherX, this, NULL); 37 gdk_event_handler_set(&EventDispatcherX, this, NULL);
38 38
39 #if defined(HAVE_XINPUT2) 39 #if defined(HAVE_XINPUT2)
40 InitializeXInput2(); 40 InitializeXInput2();
41 #endif 41 #endif
42 InitializeEventsToCapture(); 42 InitializeEventsToCapture();
43 } 43 }
44 44
45 MessagePumpGlibX::~MessagePumpGlibX() { 45 MessagePumpX::~MessagePumpX() {
46 gdk_window_remove_filter(NULL, &GdkEventFilter, this); 46 gdk_window_remove_filter(NULL, &GdkEventFilter, this);
47 47 gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event),
48 // It is not necessary to reset the GDK event handler using 48 this, NULL);
49 // gdk_event_handler_set since it's done in the destructor for
50 // MessagePumpForUI.
51 } 49 }
52 50
53 bool MessagePumpGlibX::ShouldCaptureXEvent(XEvent* xev) { 51 bool MessagePumpX::ShouldCaptureXEvent(XEvent* xev) {
54 return capture_x_events_[xev->type] 52 return capture_x_events_[xev->type]
55 #if defined(HAVE_XINPUT2) 53 #if defined(HAVE_XINPUT2)
56 && (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_) 54 && (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_)
57 #endif 55 #endif
58 ; 56 ;
59 } 57 }
60 58
61 59
62 bool MessagePumpGlibX::ProcessXEvent(XEvent* xev) { 60 bool MessagePumpX::ProcessXEvent(XEvent* xev) {
63 bool should_quit = false; 61 bool should_quit = false;
64 62
65 #if defined(HAVE_XINPUT2) 63 #if defined(HAVE_XINPUT2)
66 bool have_cookie = false; 64 bool have_cookie = false;
67 if (xev->type == GenericEvent && 65 if (xev->type == GenericEvent &&
68 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { 66 XGetEventData(xev->xgeneric.display, &xev->xcookie)) {
69 have_cookie = true; 67 have_cookie = true;
70 } 68 }
71 #endif 69 #endif
72 70
73 if (!WillProcessXEvent(xev)) { 71 if (WillProcessXEvent(xev) == MessagePumpObserver::EVENT_CONTINUE) {
74 MessagePumpGlibXDispatcher::DispatchStatus status = 72 MessagePumpDispatcher::DispatchStatus status =
75 static_cast<MessagePumpGlibXDispatcher*> 73 GetDispatcher()->Dispatch(xev);
76 (GetDispatcher())->DispatchX(xev);
77 74
78 if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) { 75 if (status == MessagePumpDispatcher::EVENT_QUIT) {
79 should_quit = true; 76 should_quit = true;
80 Quit(); 77 Quit();
81 } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) { 78 } else if (status == MessagePumpDispatcher::EVENT_IGNORED) {
82 VLOG(1) << "Event (" << xev->type << ") not handled."; 79 VLOG(1) << "Event (" << xev->type << ") not handled.";
83 } 80 }
84 } 81 }
85 82
86 #if defined(HAVE_XINPUT2) 83 #if defined(HAVE_XINPUT2)
87 if (have_cookie) { 84 if (have_cookie) {
88 XFreeEventData(xev->xgeneric.display, &xev->xcookie); 85 XFreeEventData(xev->xgeneric.display, &xev->xcookie);
89 } 86 }
90 #endif 87 #endif
91 88
92 return should_quit; 89 return should_quit;
93 } 90 }
94 91
95 bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) { 92 bool MessagePumpX::RunOnce(GMainContext* context, bool block) {
96 GdkDisplay* gdisp = gdk_display_get_default(); 93 GdkDisplay* gdisp = gdk_display_get_default();
97 if (!gdisp || !GetDispatcher()) 94 if (!gdisp || !GetDispatcher())
98 return MessagePumpForUI::RunOnce(context, block); 95 return g_main_context_iteration(context, block);
99 96
100 Display* display = GDK_DISPLAY_XDISPLAY(gdisp); 97 Display* display = GDK_DISPLAY_XDISPLAY(gdisp);
101 98
102 if (XPending(display)) { 99 if (XPending(display)) {
103 XEvent xev; 100 XEvent xev;
104 XPeekEvent(display, &xev); 101 XPeekEvent(display, &xev);
105 102
106 if (ShouldCaptureXEvent(&xev)) { 103 if (ShouldCaptureXEvent(&xev)) {
107 XNextEvent(display, &xev); 104 XNextEvent(display, &xev);
108 if (ProcessXEvent(&xev)) 105 if (ProcessXEvent(&xev))
(...skipping 21 matching lines...) Expand all
130 dispatching_event_ = false; 127 dispatching_event_ = false;
131 128
132 gdksource_->source_funcs->dispatch = cb; 129 gdksource_->source_funcs->dispatch = cb;
133 } else { 130 } else {
134 retvalue = g_main_context_iteration(context, block); 131 retvalue = g_main_context_iteration(context, block);
135 } 132 }
136 133
137 return retvalue; 134 return retvalue;
138 } 135 }
139 136
140 GdkFilterReturn MessagePumpGlibX::GdkEventFilter(GdkXEvent* gxevent, 137 GdkFilterReturn MessagePumpX::GdkEventFilter(GdkXEvent* gxevent,
141 GdkEvent* gevent, 138 GdkEvent* gevent,
142 gpointer data) { 139 gpointer data) {
143 MessagePumpGlibX* pump = static_cast<MessagePumpGlibX*>(data); 140 MessagePumpX* pump = static_cast<MessagePumpX*>(data);
144 XEvent* xev = static_cast<XEvent*>(gxevent); 141 XEvent* xev = static_cast<XEvent*>(gxevent);
145 142
146 if (pump->ShouldCaptureXEvent(xev) && pump->GetDispatcher()) { 143 if (pump->ShouldCaptureXEvent(xev) && pump->GetDispatcher()) {
147 pump->ProcessXEvent(xev); 144 pump->ProcessXEvent(xev);
148 return GDK_FILTER_REMOVE; 145 return GDK_FILTER_REMOVE;
149 } 146 }
150 147
151 return GDK_FILTER_CONTINUE; 148 return GDK_FILTER_CONTINUE;
152 } 149 }
153 150
154 bool MessagePumpGlibX::WillProcessXEvent(XEvent* xevent) { 151 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) {
155 ObserverListBase<Observer>::Iterator it(observers()); 152 ObserverListBase<MessagePumpObserver>::Iterator it(observers());
156 Observer* obs; 153 MessagePumpObserver* obs;
157 while ((obs = it.GetNext()) != NULL) { 154 while ((obs = it.GetNext()) != NULL) {
158 MessagePumpXObserver* xobs = 155 if (obs->WillProcessXEvent(xevent))
159 static_cast<MessagePumpXObserver*>(obs);
160 if (xobs->WillProcessXEvent(xevent))
161 return true; 156 return true;
162 } 157 }
163 return false; 158 return false;
164 } 159 }
165 160
166 void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) { 161 void MessagePumpX::EventDispatcherX(GdkEvent* event, gpointer data) {
167 MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data); 162 MessagePumpX* pump_x = reinterpret_cast<MessagePumpX*>(data);
168 163
169 if (!pump_x->gdksource_) { 164 if (!pump_x->gdksource_) {
170 pump_x->gdksource_ = g_main_current_source(); 165 pump_x->gdksource_ = g_main_current_source();
171 if (pump_x->gdksource_) 166 if (pump_x->gdksource_)
172 pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch; 167 pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch;
173 } else if (!pump_x->IsDispatchingEvent()) { 168 } else if (!pump_x->IsDispatchingEvent()) {
174 if (event->type != GDK_NOTHING && 169 if (event->type != GDK_NOTHING &&
175 pump_x->capture_gdk_events_[event->type]) { 170 pump_x->capture_gdk_events_[event->type]) {
176 NOTREACHED() << "GDK received an event it shouldn't have"; 171 NOTREACHED() << "GDK received an event it shouldn't have";
177 } 172 }
178 } 173 }
179 174
180 pump_x->DispatchEvents(event); 175 gtk_main_do_event(event);
181 } 176 }
182 177
183 void MessagePumpGlibX::InitializeEventsToCapture(void) { 178 void MessagePumpX::InitializeEventsToCapture(void) {
184 // TODO(sad): Decide which events we want to capture and update the tables 179 // TODO(sad): Decide which events we want to capture and update the tables
185 // accordingly. 180 // accordingly.
186 capture_x_events_[KeyPress] = true; 181 capture_x_events_[KeyPress] = true;
187 capture_gdk_events_[GDK_KEY_PRESS] = true; 182 capture_gdk_events_[GDK_KEY_PRESS] = true;
188 183
189 capture_x_events_[KeyRelease] = true; 184 capture_x_events_[KeyRelease] = true;
190 capture_gdk_events_[GDK_KEY_RELEASE] = true; 185 capture_gdk_events_[GDK_KEY_RELEASE] = true;
191 186
192 capture_x_events_[ButtonPress] = true; 187 capture_x_events_[ButtonPress] = true;
193 capture_gdk_events_[GDK_BUTTON_PRESS] = true; 188 capture_gdk_events_[GDK_BUTTON_PRESS] = true;
194 189
195 capture_x_events_[ButtonRelease] = true; 190 capture_x_events_[ButtonRelease] = true;
196 capture_gdk_events_[GDK_BUTTON_RELEASE] = true; 191 capture_gdk_events_[GDK_BUTTON_RELEASE] = true;
197 192
198 capture_x_events_[MotionNotify] = true; 193 capture_x_events_[MotionNotify] = true;
199 capture_gdk_events_[GDK_MOTION_NOTIFY] = true; 194 capture_gdk_events_[GDK_MOTION_NOTIFY] = true;
200 195
201 #if defined(HAVE_XINPUT2) 196 #if defined(HAVE_XINPUT2)
202 capture_x_events_[GenericEvent] = true; 197 capture_x_events_[GenericEvent] = true;
203 #endif 198 #endif
204 } 199 }
205 200
206 #if defined(HAVE_XINPUT2) 201 #if defined(HAVE_XINPUT2)
207 void MessagePumpGlibX::InitializeXInput2(void) { 202 void MessagePumpX::InitializeXInput2(void) {
208 GdkDisplay* display = gdk_display_get_default(); 203 GdkDisplay* display = gdk_display_get_default();
209 if (!display) 204 if (!display)
210 return; 205 return;
211 206
212 Display* xdisplay = GDK_DISPLAY_XDISPLAY(display); 207 Display* xdisplay = GDK_DISPLAY_XDISPLAY(display);
213 int event, err; 208 int event, err;
214 209
215 if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) { 210 if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) {
216 VLOG(1) << "X Input extension not available."; 211 VLOG(1) << "X Input extension not available.";
217 xiopcode_ = -1; 212 xiopcode_ = -1;
218 return; 213 return;
219 } 214 }
220 215
221 int major = 2, minor = 0; 216 int major = 2, minor = 0;
222 if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) { 217 if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) {
223 VLOG(1) << "XInput2 not supported in the server."; 218 VLOG(1) << "XInput2 not supported in the server.";
224 xiopcode_ = -1; 219 xiopcode_ = -1;
225 return; 220 return;
226 } 221 }
227 } 222 }
228 #endif // HAVE_XINPUT2 223 #endif // HAVE_XINPUT2
229 224
230 bool MessagePumpXObserver::WillProcessXEvent(XEvent* xev) { 225 MessagePumpObserver::EventStatus
231 return false; 226 MessagePumpObserver::WillProcessXEvent(XEvent* xev) {
227 return EVENT_CONTINUE;
232 } 228 }
233 229
230 COMPILE_ASSERT(XLASTEvent >= LASTEvent, XLASTEvent_too_small);
231
234 } // namespace base 232 } // namespace base
OLDNEW
« no previous file with comments | « base/message_pump_x.h ('k') | chrome/browser/chromeos/system_key_event_listener.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698