OLD | NEW |
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 data) { | 28 gpointer unused_data) { |
29 base::MessagePumpX* pump = static_cast<base::MessagePumpX*>(data); | 29 // TODO(sad): When GTK event proecssing is completely removed, the event |
30 return pump->DispatchXEvents(); | 30 // processing and dispatching should be done here (i.e. XNextEvent, |
| 31 // ProcessXEvent etc.) |
| 32 return TRUE; |
31 } | 33 } |
32 | 34 |
33 GSourceFuncs XSourceFuncs = { | 35 GSourceFuncs XSourceFuncs = { |
34 XSourcePrepare, | 36 XSourcePrepare, |
35 XSourceCheck, | 37 XSourceCheck, |
36 XSourceDispatch, | 38 XSourceDispatch, |
37 NULL | 39 NULL |
38 }; | 40 }; |
39 | 41 |
40 // The opcode used for checking events. | 42 // The opcode used for checking events. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 DCHECK(!x_source_); | 122 DCHECK(!x_source_); |
121 GPollFD* x_poll = new GPollFD(); | 123 GPollFD* x_poll = new GPollFD(); |
122 Display* display = GetDefaultXDisplay(); | 124 Display* display = GetDefaultXDisplay(); |
123 DCHECK(display) << "Unable to get connection to X server"; | 125 DCHECK(display) << "Unable to get connection to X server"; |
124 x_poll->fd = ConnectionNumber(display); | 126 x_poll->fd = ConnectionNumber(display); |
125 x_poll->events = G_IO_IN; | 127 x_poll->events = G_IO_IN; |
126 | 128 |
127 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource)); | 129 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource)); |
128 g_source_add_poll(x_source_, x_poll); | 130 g_source_add_poll(x_source_, x_poll); |
129 g_source_set_can_recurse(x_source_, FALSE); | 131 g_source_set_can_recurse(x_source_, FALSE); |
130 g_source_set_callback(x_source_, NULL, this, NULL); | |
131 g_source_attach(x_source_, g_main_context_default()); | 132 g_source_attach(x_source_, g_main_context_default()); |
132 } | 133 } |
133 | 134 |
134 bool MessagePumpX::ProcessXEvent(MessagePumpDispatcher* dispatcher, | 135 bool MessagePumpX::ProcessXEvent(MessagePumpDispatcher* dispatcher, |
135 XEvent* xev) { | 136 XEvent* xev) { |
136 bool should_quit = false; | 137 bool should_quit = false; |
137 | 138 |
138 bool have_cookie = false; | 139 bool have_cookie = false; |
139 if (xev->type == GenericEvent && | 140 if (xev->type == GenericEvent && |
140 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { | 141 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { |
(...skipping 13 matching lines...) Expand all Loading... |
154 DidProcessXEvent(xev); | 155 DidProcessXEvent(xev); |
155 } | 156 } |
156 | 157 |
157 if (have_cookie) { | 158 if (have_cookie) { |
158 XFreeEventData(xev->xgeneric.display, &xev->xcookie); | 159 XFreeEventData(xev->xgeneric.display, &xev->xcookie); |
159 } | 160 } |
160 | 161 |
161 return should_quit; | 162 return should_quit; |
162 } | 163 } |
163 | 164 |
164 gboolean MessagePumpX::DispatchXEvents() { | 165 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { |
165 Display* display = GetDefaultXDisplay(); | 166 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 |
170 // In the general case, we want to handle all pending events before running | 173 // In the general case, we want to handle all pending events before running |
171 // the tasks. This is what happens in the message_pump_glib case. | 174 // the tasks. This is what happens in the message_pump_glib case. |
172 while (XPending(display)) { | 175 while (XPending(display)) { |
173 XEvent xev; | 176 XEvent xev; |
174 XNextEvent(display, &xev); | 177 XNextEvent(display, &xev); |
175 if (dispatcher && ProcessXEvent(dispatcher, &xev)) | 178 if (dispatcher && ProcessXEvent(dispatcher, &xev)) |
176 return TRUE; | 179 return true; |
177 } | 180 } |
178 return TRUE; | 181 |
| 182 return g_main_context_iteration(context, block); |
179 } | 183 } |
180 | 184 |
181 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) { | 185 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) { |
182 if (!observers().might_have_observers()) | 186 if (!observers().might_have_observers()) |
183 return false; | 187 return false; |
184 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); | 188 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); |
185 MessagePumpObserver* obs; | 189 MessagePumpObserver* obs; |
186 while ((obs = it.GetNext()) != NULL) { | 190 while ((obs = it.GetNext()) != NULL) { |
187 if (obs->WillProcessEvent(xevent)) | 191 if (obs->WillProcessEvent(xevent)) |
188 return true; | 192 return true; |
189 } | 193 } |
190 return false; | 194 return false; |
191 } | 195 } |
192 | 196 |
193 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { | 197 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { |
194 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); | 198 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); |
195 } | 199 } |
196 | 200 |
197 } // namespace base | 201 } // namespace base |
OLD | NEW |