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" |
(...skipping 27 matching lines...) Expand all Loading... |
38 XSourceDispatch, | 38 XSourceDispatch, |
39 NULL | 39 NULL |
40 }; | 40 }; |
41 | 41 |
42 // The opcode used for checking events. | 42 // The opcode used for checking events. |
43 int xiopcode = -1; | 43 int xiopcode = -1; |
44 | 44 |
45 // The message-pump opens a connection to the display and owns it. | 45 // The message-pump opens a connection to the display and owns it. |
46 Display* g_xdisplay = NULL; | 46 Display* g_xdisplay = NULL; |
47 | 47 |
| 48 // The default dispatcher to process native events when no dispatcher |
| 49 // is specified. |
| 50 base::MessagePumpDispatcher* g_default_dispatcher = NULL; |
| 51 |
48 void InitializeXInput2(void) { | 52 void InitializeXInput2(void) { |
49 Display* display = base::MessagePumpX::GetDefaultXDisplay(); | 53 Display* display = base::MessagePumpX::GetDefaultXDisplay(); |
50 if (!display) | 54 if (!display) |
51 return; | 55 return; |
52 | 56 |
53 int event, err; | 57 int event, err; |
54 | 58 |
55 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { | 59 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { |
56 DVLOG(1) << "X Input extension not available."; | 60 DVLOG(1) << "X Input extension not available."; |
57 xiopcode = -1; | 61 xiopcode = -1; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 if (!g_xdisplay) | 105 if (!g_xdisplay) |
102 g_xdisplay = XOpenDisplay(NULL); | 106 g_xdisplay = XOpenDisplay(NULL); |
103 return g_xdisplay; | 107 return g_xdisplay; |
104 } | 108 } |
105 | 109 |
106 // static | 110 // static |
107 bool MessagePumpX::HasXInput2() { | 111 bool MessagePumpX::HasXInput2() { |
108 return xiopcode != -1; | 112 return xiopcode != -1; |
109 } | 113 } |
110 | 114 |
| 115 // static |
| 116 void MessagePumpX::SetDefaultDispatcher(MessagePumpDispatcher* dispatcher) { |
| 117 DCHECK(!g_default_dispatcher || !dispatcher); |
| 118 g_default_dispatcher = dispatcher; |
| 119 } |
| 120 |
111 void MessagePumpX::InitXSource() { | 121 void MessagePumpX::InitXSource() { |
112 DCHECK(!x_source_); | 122 DCHECK(!x_source_); |
113 GPollFD* x_poll = new GPollFD(); | 123 GPollFD* x_poll = new GPollFD(); |
114 Display* display = GetDefaultXDisplay(); | 124 Display* display = GetDefaultXDisplay(); |
115 DCHECK(display) << "Unable to get connection to X server"; | 125 DCHECK(display) << "Unable to get connection to X server"; |
116 x_poll->fd = ConnectionNumber(display); | 126 x_poll->fd = ConnectionNumber(display); |
117 x_poll->events = G_IO_IN; | 127 x_poll->events = G_IO_IN; |
118 | 128 |
119 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource)); | 129 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource)); |
120 g_source_add_poll(x_source_, x_poll); | 130 g_source_add_poll(x_source_, x_poll); |
121 g_source_set_can_recurse(x_source_, FALSE); | 131 g_source_set_can_recurse(x_source_, FALSE); |
122 g_source_attach(x_source_, g_main_context_default()); | 132 g_source_attach(x_source_, g_main_context_default()); |
123 } | 133 } |
124 | 134 |
125 bool MessagePumpX::ProcessXEvent(XEvent* xev) { | 135 bool MessagePumpX::ProcessXEvent(MessagePumpDispatcher* dispatcher, |
| 136 XEvent* xev) { |
126 bool should_quit = false; | 137 bool should_quit = false; |
127 | 138 |
128 bool have_cookie = false; | 139 bool have_cookie = false; |
129 if (xev->type == GenericEvent && | 140 if (xev->type == GenericEvent && |
130 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { | 141 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { |
131 have_cookie = true; | 142 have_cookie = true; |
132 } | 143 } |
133 | 144 |
134 if (WillProcessXEvent(xev) == EVENT_CONTINUE) { | 145 if (WillProcessXEvent(xev) == EVENT_CONTINUE) { |
135 MessagePumpDispatcher::DispatchStatus status = | 146 MessagePumpDispatcher::DispatchStatus status = |
136 GetDispatcher()->Dispatch(xev); | 147 dispatcher->Dispatch(xev); |
137 | 148 |
138 if (status == MessagePumpDispatcher::EVENT_QUIT) { | 149 if (status == MessagePumpDispatcher::EVENT_QUIT) { |
139 should_quit = true; | 150 should_quit = true; |
140 Quit(); | 151 Quit(); |
141 } else if (status == MessagePumpDispatcher::EVENT_IGNORED) { | 152 } else if (status == MessagePumpDispatcher::EVENT_IGNORED) { |
142 DVLOG(1) << "Event (" << xev->type << ") not handled."; | 153 DVLOG(1) << "Event (" << xev->type << ") not handled."; |
143 } | 154 } |
144 DidProcessXEvent(xev); | 155 DidProcessXEvent(xev); |
145 } | 156 } |
146 | 157 |
147 if (have_cookie) { | 158 if (have_cookie) { |
148 XFreeEventData(xev->xgeneric.display, &xev->xcookie); | 159 XFreeEventData(xev->xgeneric.display, &xev->xcookie); |
149 } | 160 } |
150 | 161 |
151 return should_quit; | 162 return should_quit; |
152 } | 163 } |
153 | 164 |
154 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { | 165 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { |
155 Display* display = GetDefaultXDisplay(); | 166 Display* display = GetDefaultXDisplay(); |
156 if (!display || !GetDispatcher()) | 167 MessagePumpDispatcher* dispatcher = |
| 168 GetDispatcher() ? GetDispatcher() : g_default_dispatcher; |
| 169 |
| 170 if (!display || !dispatcher) |
157 return g_main_context_iteration(context, block); | 171 return g_main_context_iteration(context, block); |
158 | 172 |
159 // 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 |
160 // 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. |
161 while (XPending(display)) { | 175 while (XPending(display)) { |
162 XEvent xev; | 176 XEvent xev; |
163 XNextEvent(display, &xev); | 177 XNextEvent(display, &xev); |
164 if (ProcessXEvent(&xev)) | 178 if (ProcessXEvent(dispatcher, &xev)) |
165 return true; | 179 return true; |
166 } | 180 } |
167 | 181 |
168 return g_main_context_iteration(context, block); | 182 return g_main_context_iteration(context, block); |
169 } | 183 } |
170 | 184 |
171 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) { | 185 bool MessagePumpX::WillProcessXEvent(XEvent* xevent) { |
172 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); | 186 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); |
173 MessagePumpObserver* obs; | 187 MessagePumpObserver* obs; |
174 while ((obs = it.GetNext()) != NULL) { | 188 while ((obs = it.GetNext()) != NULL) { |
175 if (obs->WillProcessEvent(xevent)) | 189 if (obs->WillProcessEvent(xevent)) |
176 return true; | 190 return true; |
177 } | 191 } |
178 return false; | 192 return false; |
179 } | 193 } |
180 | 194 |
181 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { | 195 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { |
182 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); | 196 ObserverListBase<MessagePumpObserver>::Iterator it(observers()); |
183 MessagePumpObserver* obs; | 197 MessagePumpObserver* obs; |
184 while ((obs = it.GetNext()) != NULL) { | 198 while ((obs = it.GetNext()) != NULL) { |
185 obs->DidProcessEvent(xevent); | 199 obs->DidProcessEvent(xevent); |
186 } | 200 } |
187 } | 201 } |
188 | 202 |
189 } // namespace base | 203 } // namespace base |
OLD | NEW |