Add support for indeterminate checkbox on Windows classic theme.
[chromium-blink-merge.git] / base / message_pump_aurax11.cc
blob8c764d5ffcd04990236ee250bab7f6de905d3446
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_aurax11.h"
7 #include <glib.h>
8 #include <X11/X.h>
9 #include <X11/extensions/XInput2.h>
10 #include <X11/XKBlib.h>
12 #include "base/basictypes.h"
13 #include "base/message_loop.h"
15 namespace {
17 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) {
18 if (XPending(base::MessagePumpAuraX11::GetDefaultXDisplay()))
19 *timeout_ms = 0;
20 else
21 *timeout_ms = -1;
22 return FALSE;
25 gboolean XSourceCheck(GSource* source) {
26 return XPending(base::MessagePumpAuraX11::GetDefaultXDisplay());
29 gboolean XSourceDispatch(GSource* source,
30 GSourceFunc unused_func,
31 gpointer data) {
32 base::MessagePumpAuraX11* pump = static_cast<base::MessagePumpAuraX11*>(data);
33 return pump->DispatchXEvents();
36 GSourceFuncs XSourceFuncs = {
37 XSourcePrepare,
38 XSourceCheck,
39 XSourceDispatch,
40 NULL
43 // The connection is essentially a global that's accessed through a static
44 // method and destroyed whenever ~MessagePumpAuraX11() is called. We do this
45 // for historical reasons so user code can call
46 // MessagePumpForUI::GetDefaultXDisplay() where MessagePumpForUI is a typedef
47 // to whatever type in the current build.
49 // TODO(erg): This can be changed to something more sane like
50 // MessagePumpAuraX11::Current()->display() once MessagePumpGtk goes away.
51 Display* g_xdisplay = NULL;
52 int g_xinput_opcode = -1;
54 bool InitializeXInput2Internal() {
55 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay();
56 if (!display)
57 return false;
59 int event, err;
61 int xiopcode;
62 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
63 DVLOG(1) << "X Input extension not available.";
64 return false;
66 g_xinput_opcode = xiopcode;
68 #if defined(USE_XI2_MT)
69 // USE_XI2_MT also defines the required XI2 minor minimum version.
70 int major = 2, minor = USE_XI2_MT;
71 #else
72 int major = 2, minor = 0;
73 #endif
74 if (XIQueryVersion(display, &major, &minor) == BadRequest) {
75 DVLOG(1) << "XInput2 not supported in the server.";
76 return false;
78 #if defined(USE_XI2_MT)
79 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
80 DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
81 << "But 2." << USE_XI2_MT << " is required.";
82 return false;
84 #endif
86 return true;
89 Window FindEventTarget(const base::NativeEvent& xev) {
90 Window target = xev->xany.window;
91 if (xev->type == GenericEvent &&
92 static_cast<XIEvent*>(xev->xcookie.data)->extension == g_xinput_opcode) {
93 target = static_cast<XIDeviceEvent*>(xev->xcookie.data)->event;
94 } else if (xev->type == MapNotify) {
95 target = xev->xmap.window;
96 } else if (xev->type == UnmapNotify) {
97 target = xev->xunmap.window;
99 // TODO(erg): Are there other events that we aren't reacting to properly
100 // because xev->xany.window != xev->eventname.window?
102 return target;
105 bool InitializeXInput2() {
106 static bool xinput2_supported = InitializeXInput2Internal();
107 return xinput2_supported;
110 bool InitializeXkb() {
111 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay();
112 if (!display)
113 return false;
115 int opcode, event, error;
116 int major = XkbMajorVersion;
117 int minor = XkbMinorVersion;
118 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
119 DVLOG(1) << "Xkb extension not available.";
120 return false;
123 // Ask the server not to send KeyRelease event when the user holds down a key.
124 // crbug.com/138092
125 Bool supported_return;
126 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
127 DVLOG(1) << "XKB not supported in the server.";
128 return false;
131 return true;
134 } // namespace
136 namespace base {
138 MessagePumpAuraX11::MessagePumpAuraX11() : MessagePumpGlib(),
139 x_source_(NULL) {
140 InitializeXInput2();
141 InitializeXkb();
142 InitXSource();
144 // Can't put this in the initializer list because g_xdisplay may not exist
145 // until after InitXSource().
146 x_root_window_ = DefaultRootWindow(g_xdisplay);
149 // static
150 Display* MessagePumpAuraX11::GetDefaultXDisplay() {
151 if (!g_xdisplay)
152 g_xdisplay = XOpenDisplay(NULL);
153 return g_xdisplay;
156 // static
157 bool MessagePumpAuraX11::HasXInput2() {
158 return InitializeXInput2();
161 // static
162 MessagePumpAuraX11* MessagePumpAuraX11::Current() {
163 MessageLoopForUI* loop = MessageLoopForUI::current();
164 return static_cast<MessagePumpAuraX11*>(loop->pump_ui());
167 void MessagePumpAuraX11::AddDispatcherForWindow(
168 MessagePumpDispatcher* dispatcher,
169 unsigned long xid) {
170 dispatchers_.insert(std::make_pair(xid, dispatcher));
173 void MessagePumpAuraX11::RemoveDispatcherForWindow(unsigned long xid) {
174 dispatchers_.erase(xid);
177 void MessagePumpAuraX11::AddDispatcherForRootWindow(
178 MessagePumpDispatcher* dispatcher) {
179 root_window_dispatchers_.AddObserver(dispatcher);
182 void MessagePumpAuraX11::RemoveDispatcherForRootWindow(
183 MessagePumpDispatcher* dispatcher) {
184 root_window_dispatchers_.RemoveObserver(dispatcher);
187 bool MessagePumpAuraX11::DispatchXEvents() {
188 Display* display = GetDefaultXDisplay();
189 DCHECK(display);
190 MessagePumpDispatcher* dispatcher =
191 GetDispatcher() ? GetDispatcher() : this;
193 // In the general case, we want to handle all pending events before running
194 // the tasks. This is what happens in the message_pump_glib case.
195 while (XPending(display)) {
196 XEvent xev;
197 XNextEvent(display, &xev);
198 if (dispatcher && ProcessXEvent(dispatcher, &xev))
199 return TRUE;
201 return TRUE;
204 void MessagePumpAuraX11::BlockUntilWindowMapped(unsigned long xid) {
205 XEvent event;
207 Display* display = GetDefaultXDisplay();
208 DCHECK(display);
210 MessagePumpDispatcher* dispatcher =
211 GetDispatcher() ? GetDispatcher() : this;
213 do {
214 // Block until there's a message of |event_mask| type on |w|. Then remove
215 // it from the queue and stuff it in |event|.
216 XWindowEvent(display, xid, StructureNotifyMask, &event);
217 ProcessXEvent(dispatcher, &event);
218 } while (event.type != MapNotify);
221 MessagePumpAuraX11::~MessagePumpAuraX11() {
222 g_source_destroy(x_source_);
223 g_source_unref(x_source_);
224 XCloseDisplay(g_xdisplay);
225 g_xdisplay = NULL;
228 void MessagePumpAuraX11::InitXSource() {
229 // CHECKs are to help track down crbug.com/113106.
230 CHECK(!x_source_);
231 Display* display = GetDefaultXDisplay();
232 CHECK(display) << "Unable to get connection to X server";
233 x_poll_.reset(new GPollFD());
234 CHECK(x_poll_.get());
235 x_poll_->fd = ConnectionNumber(display);
236 x_poll_->events = G_IO_IN;
238 x_source_ = g_source_new(&XSourceFuncs, sizeof(GSource));
239 g_source_add_poll(x_source_, x_poll_.get());
240 g_source_set_can_recurse(x_source_, TRUE);
241 g_source_set_callback(x_source_, NULL, this, NULL);
242 g_source_attach(x_source_, g_main_context_default());
245 bool MessagePumpAuraX11::ProcessXEvent(MessagePumpDispatcher* dispatcher,
246 XEvent* xev) {
247 bool should_quit = false;
249 bool have_cookie = false;
250 if (xev->type == GenericEvent &&
251 XGetEventData(xev->xgeneric.display, &xev->xcookie)) {
252 have_cookie = true;
255 if (!WillProcessXEvent(xev)) {
256 if (!dispatcher->Dispatch(xev)) {
257 should_quit = true;
258 Quit();
260 DidProcessXEvent(xev);
263 if (have_cookie) {
264 XFreeEventData(xev->xgeneric.display, &xev->xcookie);
267 return should_quit;
270 bool MessagePumpAuraX11::WillProcessXEvent(XEvent* xevent) {
271 if (!observers().might_have_observers())
272 return false;
273 ObserverListBase<MessagePumpObserver>::Iterator it(observers());
274 MessagePumpObserver* obs;
275 while ((obs = it.GetNext()) != NULL) {
276 if (obs->WillProcessEvent(xevent))
277 return true;
279 return false;
282 void MessagePumpAuraX11::DidProcessXEvent(XEvent* xevent) {
283 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent));
286 MessagePumpDispatcher* MessagePumpAuraX11::GetDispatcherForXEvent(
287 const base::NativeEvent& xev) const {
288 ::Window x_window = FindEventTarget(xev);
289 DispatchersMap::const_iterator it = dispatchers_.find(x_window);
290 return it != dispatchers_.end() ? it->second : NULL;
293 bool MessagePumpAuraX11::Dispatch(const base::NativeEvent& xev) {
294 // MappingNotify events (meaning that the keyboard or pointer buttons have
295 // been remapped) aren't associated with a window; send them to all
296 // dispatchers.
297 if (xev->type == MappingNotify) {
298 for (DispatchersMap::const_iterator it = dispatchers_.begin();
299 it != dispatchers_.end(); ++it) {
300 it->second->Dispatch(xev);
302 return true;
305 if (FindEventTarget(xev) == x_root_window_) {
306 FOR_EACH_OBSERVER(MessagePumpDispatcher, root_window_dispatchers_,
307 Dispatch(xev));
308 return true;
310 MessagePumpDispatcher* dispatcher = GetDispatcherForXEvent(xev);
311 return dispatcher ? dispatcher->Dispatch(xev) : true;
314 } // namespace base