| 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 <gdk/gdkx.h> | 7 #include <gdk/gdkx.h> |
| 8 #if defined(HAVE_XINPUT2) | |
| 9 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
| 10 #else | |
| 11 #include <X11/Xlib.h> | |
| 12 #endif | |
| 13 | 9 |
| 14 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 15 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 16 | 12 |
| 17 namespace { | 13 namespace { |
| 18 | 14 |
| 19 gboolean PlaceholderDispatch(GSource* source, | 15 gboolean PlaceholderDispatch(GSource* source, |
| 20 GSourceFunc cb, | 16 GSourceFunc cb, |
| 21 gpointer data) { | 17 gpointer data) { |
| 22 return TRUE; | 18 return TRUE; |
| 23 } | 19 } |
| 24 | 20 |
| 25 // A flag to disable GTK's message pump. This is intermediate step | 21 // A flag to disable GTK's message pump. This is intermediate step |
| 26 // to remove gtk and will be removed once migration is complete. | 22 // to remove gtk and will be removed once migration is complete. |
| 27 bool use_gtk_message_pump = true; | 23 bool use_gtk_message_pump = true; |
| 28 | 24 |
| 29 } // namespace | 25 } // namespace |
| 30 | 26 |
| 31 namespace base { | 27 namespace base { |
| 32 | 28 |
| 33 MessagePumpX::MessagePumpX() : MessagePumpGlib(), | 29 MessagePumpX::MessagePumpX() : MessagePumpGlib(), |
| 34 #if defined(HAVE_XINPUT2) | |
| 35 xiopcode_(-1), | 30 xiopcode_(-1), |
| 36 #endif | |
| 37 gdksource_(NULL), | 31 gdksource_(NULL), |
| 38 dispatching_event_(false), | 32 dispatching_event_(false), |
| 39 capture_x_events_(0), | 33 capture_x_events_(0), |
| 40 capture_gdk_events_(0) { | 34 capture_gdk_events_(0) { |
| 41 gdk_window_add_filter(NULL, &GdkEventFilter, this); | 35 gdk_window_add_filter(NULL, &GdkEventFilter, this); |
| 42 gdk_event_handler_set(&EventDispatcherX, this, NULL); | 36 gdk_event_handler_set(&EventDispatcherX, this, NULL); |
| 43 | 37 |
| 44 #if defined(HAVE_XINPUT2) | |
| 45 InitializeXInput2(); | 38 InitializeXInput2(); |
| 46 #endif | |
| 47 if (use_gtk_message_pump) | 39 if (use_gtk_message_pump) |
| 48 InitializeEventsToCapture(); | 40 InitializeEventsToCapture(); |
| 49 } | 41 } |
| 50 | 42 |
| 51 MessagePumpX::~MessagePumpX() { | 43 MessagePumpX::~MessagePumpX() { |
| 52 gdk_window_remove_filter(NULL, &GdkEventFilter, this); | 44 gdk_window_remove_filter(NULL, &GdkEventFilter, this); |
| 53 gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event), | 45 gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event), |
| 54 this, NULL); | 46 this, NULL); |
| 55 } | 47 } |
| 56 | 48 |
| 57 // static | 49 // static |
| 58 void MessagePumpX::DisableGtkMessagePump() { | 50 void MessagePumpX::DisableGtkMessagePump() { |
| 59 use_gtk_message_pump = false; | 51 use_gtk_message_pump = false; |
| 60 } | 52 } |
| 61 | 53 |
| 62 bool MessagePumpX::ShouldCaptureXEvent(XEvent* xev) { | 54 bool MessagePumpX::ShouldCaptureXEvent(XEvent* xev) { |
| 63 return (!use_gtk_message_pump || capture_x_events_[xev->type]) | 55 return (!use_gtk_message_pump || capture_x_events_[xev->type]) |
| 64 #if defined(HAVE_XINPUT2) | |
| 65 && (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_) | 56 && (xev->type != GenericEvent || xev->xcookie.extension == xiopcode_) |
| 66 #endif | |
| 67 ; | 57 ; |
| 68 } | 58 } |
| 69 | 59 |
| 70 bool MessagePumpX::ProcessXEvent(XEvent* xev) { | 60 bool MessagePumpX::ProcessXEvent(XEvent* xev) { |
| 71 bool should_quit = false; | 61 bool should_quit = false; |
| 72 | 62 |
| 73 #if defined(HAVE_XINPUT2) | |
| 74 bool have_cookie = false; | 63 bool have_cookie = false; |
| 75 if (xev->type == GenericEvent && | 64 if (xev->type == GenericEvent && |
| 76 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { | 65 XGetEventData(xev->xgeneric.display, &xev->xcookie)) { |
| 77 have_cookie = true; | 66 have_cookie = true; |
| 78 } | 67 } |
| 79 #endif | |
| 80 | 68 |
| 81 if (WillProcessXEvent(xev) == MessagePumpObserver::EVENT_CONTINUE) { | 69 if (WillProcessXEvent(xev) == MessagePumpObserver::EVENT_CONTINUE) { |
| 82 MessagePumpDispatcher::DispatchStatus status = | 70 MessagePumpDispatcher::DispatchStatus status = |
| 83 GetDispatcher()->Dispatch(xev); | 71 GetDispatcher()->Dispatch(xev); |
| 84 | 72 |
| 85 if (status == MessagePumpDispatcher::EVENT_QUIT) { | 73 if (status == MessagePumpDispatcher::EVENT_QUIT) { |
| 86 should_quit = true; | 74 should_quit = true; |
| 87 Quit(); | 75 Quit(); |
| 88 } else if (status == MessagePumpDispatcher::EVENT_IGNORED) { | 76 } else if (status == MessagePumpDispatcher::EVENT_IGNORED) { |
| 89 VLOG(1) << "Event (" << xev->type << ") not handled."; | 77 VLOG(1) << "Event (" << xev->type << ") not handled."; |
| 90 } | 78 } |
| 91 } | 79 } |
| 92 | 80 |
| 93 #if defined(HAVE_XINPUT2) | |
| 94 if (have_cookie) { | 81 if (have_cookie) { |
| 95 XFreeEventData(xev->xgeneric.display, &xev->xcookie); | 82 XFreeEventData(xev->xgeneric.display, &xev->xcookie); |
| 96 } | 83 } |
| 97 #endif | |
| 98 | 84 |
| 99 return should_quit; | 85 return should_quit; |
| 100 } | 86 } |
| 101 | 87 |
| 102 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { | 88 bool MessagePumpX::RunOnce(GMainContext* context, bool block) { |
| 103 Display* display = MessageLoopForUI::current()->GetDisplay(); | 89 Display* display = MessageLoopForUI::current()->GetDisplay(); |
| 104 if (!display || !GetDispatcher()) | 90 if (!display || !GetDispatcher()) |
| 105 return g_main_context_iteration(context, block); | 91 return g_main_context_iteration(context, block); |
| 106 | 92 |
| 107 if (XPending(display)) { | 93 if (XPending(display)) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 181 |
| 196 capture_x_events_[ButtonPress] = true; | 182 capture_x_events_[ButtonPress] = true; |
| 197 capture_gdk_events_[GDK_BUTTON_PRESS] = true; | 183 capture_gdk_events_[GDK_BUTTON_PRESS] = true; |
| 198 | 184 |
| 199 capture_x_events_[ButtonRelease] = true; | 185 capture_x_events_[ButtonRelease] = true; |
| 200 capture_gdk_events_[GDK_BUTTON_RELEASE] = true; | 186 capture_gdk_events_[GDK_BUTTON_RELEASE] = true; |
| 201 | 187 |
| 202 capture_x_events_[MotionNotify] = true; | 188 capture_x_events_[MotionNotify] = true; |
| 203 capture_gdk_events_[GDK_MOTION_NOTIFY] = true; | 189 capture_gdk_events_[GDK_MOTION_NOTIFY] = true; |
| 204 | 190 |
| 205 #if defined(HAVE_XINPUT2) | |
| 206 capture_x_events_[GenericEvent] = true; | 191 capture_x_events_[GenericEvent] = true; |
| 207 #endif | |
| 208 } | 192 } |
| 209 | 193 |
| 210 #if defined(HAVE_XINPUT2) | |
| 211 void MessagePumpX::InitializeXInput2(void) { | 194 void MessagePumpX::InitializeXInput2(void) { |
| 212 Display* display = MessageLoopForUI::current()->GetDisplay(); | 195 Display* display = MessageLoopForUI::current()->GetDisplay(); |
| 213 if (!display) | 196 if (!display) |
| 214 return; | 197 return; |
| 215 | 198 |
| 216 int event, err; | 199 int event, err; |
| 217 | 200 |
| 218 if (!XQueryExtension(display, "XInputExtension", &xiopcode_, &event, &err)) { | 201 if (!XQueryExtension(display, "XInputExtension", &xiopcode_, &event, &err)) { |
| 219 VLOG(1) << "X Input extension not available."; | 202 VLOG(1) << "X Input extension not available."; |
| 220 xiopcode_ = -1; | 203 xiopcode_ = -1; |
| 221 return; | 204 return; |
| 222 } | 205 } |
| 223 | 206 |
| 224 int major = 2, minor = 0; | 207 int major = 2, minor = 0; |
| 225 if (XIQueryVersion(display, &major, &minor) == BadRequest) { | 208 if (XIQueryVersion(display, &major, &minor) == BadRequest) { |
| 226 VLOG(1) << "XInput2 not supported in the server."; | 209 VLOG(1) << "XInput2 not supported in the server."; |
| 227 xiopcode_ = -1; | 210 xiopcode_ = -1; |
| 228 return; | 211 return; |
| 229 } | 212 } |
| 230 } | 213 } |
| 231 #endif // HAVE_XINPUT2 | |
| 232 | 214 |
| 233 MessagePumpObserver::EventStatus | 215 MessagePumpObserver::EventStatus |
| 234 MessagePumpObserver::WillProcessXEvent(XEvent* xev) { | 216 MessagePumpObserver::WillProcessXEvent(XEvent* xev) { |
| 235 return EVENT_CONTINUE; | 217 return EVENT_CONTINUE; |
| 236 } | 218 } |
| 237 | 219 |
| 238 COMPILE_ASSERT(XLASTEvent >= LASTEvent, XLASTEvent_too_small); | 220 COMPILE_ASSERT(XLASTEvent >= LASTEvent, XLASTEvent_too_small); |
| 239 | 221 |
| 240 } // namespace base | 222 } // namespace base |
| OLD | NEW |