| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/message_pump_glib_x.h" | |
| 6 | |
| 7 #include <gdk/gdkx.h> | |
| 8 #if defined(HAVE_XINPUT2) | |
| 9 #include <X11/extensions/XInput2.h> | |
| 10 #else | |
| 11 #include <X11/Xlib.h> | |
| 12 #endif | |
| 13 | |
| 14 #include "base/message_pump_glib_x_dispatch.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 gboolean PlaceholderDispatch(GSource* source, | |
| 19 GSourceFunc cb, | |
| 20 gpointer data) { | |
| 21 return TRUE; | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 namespace base { | |
| 27 | |
| 28 MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(), | |
| 29 #if defined(HAVE_XINPUT2) | |
| 30 xiopcode_(-1), | |
| 31 #endif | |
| 32 gdksource_(NULL), | |
| 33 dispatching_event_(false), | |
| 34 capture_x_events_(0), | |
| 35 capture_gdk_events_(0) { | |
| 36 gdk_window_add_filter(NULL, &GdkEventFilter, this); | |
| 37 gdk_event_handler_set(&EventDispatcherX, this, NULL); | |
| 38 | |
| 39 #if defined(HAVE_XINPUT2) | |
| 40 InitializeXInput2(); | |
| 41 #endif | |
| 42 InitializeEventsToCapture(); | |
| 43 } | |
| 44 | |
| 45 MessagePumpGlibX::~MessagePumpGlibX() { | |
| 46 gdk_window_remove_filter(NULL, &GdkEventFilter, this); | |
| 47 | |
| 48 // It is not necessary to reset the GDK event handler using | |
| 49 // gdk_event_handler_set since it's done in the destructor for | |
| 50 // MessagePumpForUI. | |
| 51 } | |
| 52 | |
| 53 bool MessagePumpGlibX::ShouldCaptureXEvent(XEvent* xev) { | |
| 54 return capture_x_events_[xev->type] | |
| 55 #if defined(HAVE_XINPUT2) | |
| 56 && (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_) | |
| 57 #endif | |
| 58 ; | |
| 59 } | |
| 60 | |
| 61 | |
| 62 bool MessagePumpGlibX::ProcessXEvent(XEvent* xev) { | |
| 63 bool should_quit = false; | |
| 64 | |
| 65 #if defined(HAVE_XINPUT2) | |
| 66 bool have_cookie = false; | |
| 67 if (xev->type == GenericEvent && | |
| 68 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { | |
| 69 have_cookie = true; | |
| 70 } | |
| 71 #endif | |
| 72 | |
| 73 if (!WillProcessXEvent(xev)) { | |
| 74 MessagePumpGlibXDispatcher::DispatchStatus status = | |
| 75 static_cast<MessagePumpGlibXDispatcher*> | |
| 76 (GetDispatcher())->DispatchX(xev); | |
| 77 | |
| 78 if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) { | |
| 79 should_quit = true; | |
| 80 Quit(); | |
| 81 } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) { | |
| 82 VLOG(1) << "Event (" << xev->type << ") not handled."; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 #if defined(HAVE_XINPUT2) | |
| 87 if (have_cookie) { | |
| 88 XFreeEventData(xev->xgeneric.display, &xev->xcookie); | |
| 89 } | |
| 90 #endif | |
| 91 | |
| 92 return should_quit; | |
| 93 } | |
| 94 | |
| 95 bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) { | |
| 96 GdkDisplay* gdisp = gdk_display_get_default(); | |
| 97 if (!gdisp || !GetDispatcher()) | |
| 98 return MessagePumpForUI::RunOnce(context, block); | |
| 99 | |
| 100 Display* display = GDK_DISPLAY_XDISPLAY(gdisp); | |
| 101 | |
| 102 if (XPending(display)) { | |
| 103 XEvent xev; | |
| 104 XPeekEvent(display, &xev); | |
| 105 | |
| 106 if (ShouldCaptureXEvent(&xev)) { | |
| 107 XNextEvent(display, &xev); | |
| 108 if (ProcessXEvent(&xev)) | |
| 109 return true; | |
| 110 } else { | |
| 111 // TODO(sad): A couple of extra events can still sneak in during this. | |
| 112 // Those should be sent back to the X queue from the dispatcher | |
| 113 // EventDispatcherX. | |
| 114 if (gdksource_) | |
| 115 gdksource_->source_funcs->dispatch = gdkdispatcher_; | |
| 116 g_main_context_iteration(context, FALSE); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 bool retvalue; | |
| 121 if (gdksource_) { | |
| 122 // Replace the dispatch callback of the GDK event source temporarily so that | |
| 123 // it doesn't read events from X. | |
| 124 gboolean (*cb)(GSource*, GSourceFunc, void*) = | |
| 125 gdksource_->source_funcs->dispatch; | |
| 126 gdksource_->source_funcs->dispatch = PlaceholderDispatch; | |
| 127 | |
| 128 dispatching_event_ = true; | |
| 129 retvalue = g_main_context_iteration(context, block); | |
| 130 dispatching_event_ = false; | |
| 131 | |
| 132 gdksource_->source_funcs->dispatch = cb; | |
| 133 } else { | |
| 134 retvalue = g_main_context_iteration(context, block); | |
| 135 } | |
| 136 | |
| 137 return retvalue; | |
| 138 } | |
| 139 | |
| 140 GdkFilterReturn MessagePumpGlibX::GdkEventFilter(GdkXEvent* gxevent, | |
| 141 GdkEvent* gevent, | |
| 142 gpointer data) { | |
| 143 MessagePumpGlibX* pump = static_cast<MessagePumpGlibX*>(data); | |
| 144 XEvent* xev = static_cast<XEvent*>(gxevent); | |
| 145 | |
| 146 if (pump->ShouldCaptureXEvent(xev) && pump->GetDispatcher()) { | |
| 147 pump->ProcessXEvent(xev); | |
| 148 return GDK_FILTER_REMOVE; | |
| 149 } | |
| 150 | |
| 151 return GDK_FILTER_CONTINUE; | |
| 152 } | |
| 153 | |
| 154 bool MessagePumpGlibX::WillProcessXEvent(XEvent* xevent) { | |
| 155 ObserverListBase<Observer>::Iterator it(observers()); | |
| 156 Observer* obs; | |
| 157 while ((obs = it.GetNext()) != NULL) { | |
| 158 MessagePumpXObserver* xobs = | |
| 159 static_cast<MessagePumpXObserver*>(obs); | |
| 160 if (xobs->WillProcessXEvent(xevent)) | |
| 161 return true; | |
| 162 } | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) { | |
| 167 MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data); | |
| 168 | |
| 169 if (!pump_x->gdksource_) { | |
| 170 pump_x->gdksource_ = g_main_current_source(); | |
| 171 if (pump_x->gdksource_) | |
| 172 pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch; | |
| 173 } else if (!pump_x->IsDispatchingEvent()) { | |
| 174 if (event->type != GDK_NOTHING && | |
| 175 pump_x->capture_gdk_events_[event->type]) { | |
| 176 NOTREACHED() << "GDK received an event it shouldn't have"; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 pump_x->DispatchEvents(event); | |
| 181 } | |
| 182 | |
| 183 void MessagePumpGlibX::InitializeEventsToCapture(void) { | |
| 184 // TODO(sad): Decide which events we want to capture and update the tables | |
| 185 // accordingly. | |
| 186 capture_x_events_[KeyPress] = true; | |
| 187 capture_gdk_events_[GDK_KEY_PRESS] = true; | |
| 188 | |
| 189 capture_x_events_[KeyRelease] = true; | |
| 190 capture_gdk_events_[GDK_KEY_RELEASE] = true; | |
| 191 | |
| 192 capture_x_events_[ButtonPress] = true; | |
| 193 capture_gdk_events_[GDK_BUTTON_PRESS] = true; | |
| 194 | |
| 195 capture_x_events_[ButtonRelease] = true; | |
| 196 capture_gdk_events_[GDK_BUTTON_RELEASE] = true; | |
| 197 | |
| 198 capture_x_events_[MotionNotify] = true; | |
| 199 capture_gdk_events_[GDK_MOTION_NOTIFY] = true; | |
| 200 | |
| 201 #if defined(HAVE_XINPUT2) | |
| 202 capture_x_events_[GenericEvent] = true; | |
| 203 #endif | |
| 204 } | |
| 205 | |
| 206 #if defined(HAVE_XINPUT2) | |
| 207 void MessagePumpGlibX::InitializeXInput2(void) { | |
| 208 GdkDisplay* display = gdk_display_get_default(); | |
| 209 if (!display) | |
| 210 return; | |
| 211 | |
| 212 Display* xdisplay = GDK_DISPLAY_XDISPLAY(display); | |
| 213 int event, err; | |
| 214 | |
| 215 if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) { | |
| 216 VLOG(1) << "X Input extension not available."; | |
| 217 xiopcode_ = -1; | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 int major = 2, minor = 0; | |
| 222 if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) { | |
| 223 VLOG(1) << "XInput2 not supported in the server."; | |
| 224 xiopcode_ = -1; | |
| 225 return; | |
| 226 } | |
| 227 } | |
| 228 #endif // HAVE_XINPUT2 | |
| 229 | |
| 230 bool MessagePumpXObserver::WillProcessXEvent(XEvent* xev) { | |
| 231 return false; | |
| 232 } | |
| 233 | |
| 234 } // namespace base | |
| OLD | NEW |