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

Side by Side Diff: base/message_pump_x.cc

Issue 8872055: Refactor MessagePumpX to dispatch events inside of the source Dispatch function. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: allow recurse Created 9 years 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') | no next file » | 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_x.h" 5 #include "base/message_pump_x.h"
6 6
7 #include <X11/extensions/XInput2.h> 7 #include <X11/extensions/XInput2.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 11
12 namespace { 12 namespace {
13 13
14 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { 14 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) {
15 if (XPending(base::MessagePumpX::GetDefaultXDisplay())) 15 if (XPending(base::MessagePumpX::GetDefaultXDisplay()))
16 *timeout_ms = 0; 16 *timeout_ms = 0;
17 else 17 else
18 *timeout_ms = -1; 18 *timeout_ms = -1;
19 return FALSE; 19 return FALSE;
20 } 20 }
21 21
22 gboolean XSourceCheck(GSource* source) { 22 gboolean XSourceCheck(GSource* source) {
23 return XPending(base::MessagePumpX::GetDefaultXDisplay()); 23 return XPending(base::MessagePumpX::GetDefaultXDisplay());
24 } 24 }
25 25
26 gboolean XSourceDispatch(GSource* source, 26 gboolean XSourceDispatch(GSource* source,
27 GSourceFunc unused_func, 27 GSourceFunc unused_func,
28 gpointer unused_data) { 28 gpointer data) {
29 // TODO(sad): When GTK event proecssing is completely removed, the event 29 base::MessagePumpX* pump = static_cast<base::MessagePumpX*>(data);
30 // processing and dispatching should be done here (i.e. XNextEvent, 30 return pump->DispatchXEvents();
31 // ProcessXEvent etc.)
32 return TRUE;
33 } 31 }
34 32
35 GSourceFuncs XSourceFuncs = { 33 GSourceFuncs XSourceFuncs = {
36 XSourcePrepare, 34 XSourcePrepare,
37 XSourceCheck, 35 XSourceCheck,
38 XSourceDispatch, 36 XSourceDispatch,
39 NULL 37 NULL
40 }; 38 };
41 39
42 // The opcode used for checking events. 40 // The opcode used for checking events.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 void MessagePumpX::InitXSource() { 119 void MessagePumpX::InitXSource() {
122 DCHECK(!x_source_); 120 DCHECK(!x_source_);
123 GPollFD* x_poll = new GPollFD(); 121 GPollFD* x_poll = new GPollFD();
124 Display* display = GetDefaultXDisplay(); 122 Display* display = GetDefaultXDisplay();
125 DCHECK(display) << "Unable to get connection to X server"; 123 DCHECK(display) << "Unable to get connection to X server";
126 x_poll->fd = ConnectionNumber(display); 124 x_poll->fd = ConnectionNumber(display);
127 x_poll->events = G_IO_IN; 125 x_poll->events = G_IO_IN;
128 126
129 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource)); 127 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource));
130 g_source_add_poll(x_source_, x_poll); 128 g_source_add_poll(x_source_, x_poll);
131 g_source_set_can_recurse(x_source_, FALSE); 129 g_source_set_can_recurse(x_source_, TRUE);
130 g_source_set_callback(x_source_, NULL, this, NULL);
132 g_source_attach(x_source_, g_main_context_default()); 131 g_source_attach(x_source_, g_main_context_default());
133 } 132 }
134 133
135 bool MessagePumpX::ProcessXEvent(MessagePumpDispatcher* dispatcher, 134 bool MessagePumpX::ProcessXEvent(MessagePumpDispatcher* dispatcher,
136 XEvent* xev) { 135 XEvent* xev) {
137 bool should_quit = false; 136 bool should_quit = false;
138 137
139 bool have_cookie = false; 138 bool have_cookie = false;
140 if (xev->type == GenericEvent && 139 if (xev->type == GenericEvent &&
141 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { 140 XGetEventData(xev->xgeneric.display, &xev->xcookie)) {
(...skipping 13 matching lines...) Expand all
155 DidProcessXEvent(xev); 154 DidProcessXEvent(xev);
156 } 155 }
157 156
158 if (have_cookie) { 157 if (have_cookie) {
159 XFreeEventData(xev->xgeneric.display, &xev->xcookie); 158 XFreeEventData(xev->xgeneric.display, &xev->xcookie);
160 } 159 }
161 160
162 return should_quit; 161 return should_quit;
163 } 162 }
164 163
165 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { 164 gboolean MessagePumpX::DispatchXEvents() {
166 Display* display = GetDefaultXDisplay(); 165 Display* display = GetDefaultXDisplay();
166 DCHECK(display);
167 MessagePumpDispatcher* dispatcher = 167 MessagePumpDispatcher* dispatcher =
168 GetDispatcher() ? GetDispatcher() : g_default_dispatcher; 168 GetDispatcher() ? GetDispatcher() : g_default_dispatcher;
169 169
170 if (!display)
171 return g_main_context_iteration(context, block);
172
173 // In the general case, we want to handle all pending events before running 170 // In the general case, we want to handle all pending events before running
174 // the tasks. This is what happens in the message_pump_glib case. 171 // the tasks. This is what happens in the message_pump_glib case.
175 while (XPending(display)) { 172 while (XPending(display)) {
176 XEvent xev; 173 XEvent xev;
177 XNextEvent(display, &xev); 174 XNextEvent(display, &xev);
178 if (dispatcher && ProcessXEvent(dispatcher, &xev)) 175 if (dispatcher && ProcessXEvent(dispatcher, &xev))
179 return true; 176 return TRUE;
180 } 177 }
181 178 return TRUE;
182 return g_main_context_iteration(context, block);
183 } 179 }
184 180
185 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) { 181 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) {
186 if (!observers().might_have_observers()) 182 if (!observers().might_have_observers())
187 return false; 183 return false;
188 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); 184 ObserverListBase<MessagePumpObserver>::Iterator it(observers());
189 MessagePumpObserver* obs; 185 MessagePumpObserver* obs;
190 while ((obs = it.GetNext()) != NULL) { 186 while ((obs = it.GetNext()) != NULL) {
191 if (obs->WillProcessEvent(xevent)) 187 if (obs->WillProcessEvent(xevent))
192 return true; 188 return true;
193 } 189 }
194 return false; 190 return false;
195 } 191 }
196 192
197 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { 193 void MessagePumpX::DidProcessXEvent(XEvent* xevent) {
198 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); 194 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent));
199 } 195 }
200 196
201 } // namespace base 197 } // namespace base
OLDNEW
« no previous file with comments | « base/message_pump_x.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698