Chromium Code Reviews| 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 #ifndef BASE_MESSAGE_PUMP_X_H | |
| 6 #define BASE_MESSAGE_PUMP_X_H | |
| 7 | |
| 8 #include "base/message_pump.h" | |
| 9 #include "base/message_pump_glib.h" | |
| 10 | |
| 11 #include <bitset> | |
| 12 | |
| 13 #include <glib.h> | |
| 14 #include <gtk/gtk.h> | |
| 15 | |
| 16 typedef union _XEvent XEvent; | |
| 17 | |
| 18 namespace base { | |
| 19 | |
| 20 // The documentation for this class is in message_pump_glib.h | |
| 21 class BASE_API MessagePumpObserver { | |
| 22 public: | |
| 23 enum EventStatus { | |
| 24 EVENT_CONTINUE, // The event should be dispatched as normal. | |
| 25 EVENT_HANDLED // The event should not be processed any farther. | |
| 26 }; | |
| 27 | |
| 28 // This method is called before processing an XEvent. If the method returns | |
| 29 // EVENT_HANDLED, it indicates the event has already been handled, so the | |
| 30 // event is not processed any farther. If the method returns EVENT_CONTINUE, | |
| 31 // the event dispatching proceeds as normal. | |
| 32 virtual EventStatus WillProcessXEvent(XEvent* xevent); | |
| 33 | |
| 34 protected: | |
| 35 virtual ~MessagePumpObserver() {} | |
| 36 }; | |
| 37 | |
| 38 // The documentation for this class is in message_pump_glib.h | |
| 39 // | |
| 40 // The nested loop is exited by either posting a quit, or returning EVENT_QUIT | |
| 41 // from Dispatch. | |
| 42 class MessagePumpDispatcher { | |
| 43 public: | |
| 44 enum DispatchStatus { | |
| 45 EVENT_IGNORED, // The event was not processed. | |
| 46 EVENT_PROCESSED, // The event has been processed. | |
| 47 EVENT_QUIT // The event was processed and the message-loop should | |
| 48 // terminate. | |
| 49 }; | |
| 50 | |
| 51 // Dispatches the event. EVENT_IGNORED is returned if the event was ignored | |
| 52 // (i.e. not processed). EVENT_PROCESSED is returned if the event was | |
| 53 // processed. The nested loop exits immediately if EVENT_QUIT is returned. | |
| 54 virtual DispatchStatus Dispatch(XEvent* xevent) = 0; | |
| 55 | |
| 56 protected: | |
| 57 virtual ~MessagePumpDispatcher() {} | |
| 58 }; | |
| 59 | |
| 60 // This class implements a message-pump for dispatching X events. | |
| 61 class MessagePumpX : public MessagePumpGlib { | |
| 62 public: | |
| 63 MessagePumpX(); | |
| 64 virtual ~MessagePumpX(); | |
| 65 | |
| 66 // Indicates whether a GDK event was injected by chrome (when |true|) or if it | |
| 67 // was captured and being processed by GDK (when |false|). | |
| 68 bool IsDispatchingEvent(void) { return dispatching_event_; } | |
| 69 | |
| 70 // Overridden from MessagePumpGlib: | |
| 71 virtual bool RunOnce(GMainContext* context, bool block) OVERRIDE; | |
| 72 | |
| 73 private: | |
| 74 // Some XEvent's can't be directly read from X event queue and will go | |
| 75 // through GDK's dispatching process and may get discarded. This function | |
| 76 // sets up a filter to intercept those XEvent's we are interested in | |
| 77 // and dispatches them so that they won't get lost. | |
| 78 static GdkFilterReturn GdkEventFilter(GdkXEvent* gxevent, | |
| 79 GdkEvent* gevent, | |
| 80 gpointer data); | |
| 81 | |
| 82 static void EventDispatcherX(GdkEvent* event, gpointer data); | |
| 83 | |
| 84 // Decides whether we are interested in processing this XEvent. | |
| 85 bool ShouldCaptureXEvent(XEvent* event); | |
| 86 | |
| 87 // Dispatches the XEvent and returns true if we should exit the current loop | |
| 88 // of message processing. | |
| 89 bool ProcessXEvent(XEvent* event); | |
| 90 | |
| 91 // Sends the event to the observers. If an observer returns true, then it does | |
| 92 // not send the event to any other observers and returns true. Returns false | |
| 93 // if no observer returns true. | |
| 94 bool WillProcessXEvent(XEvent* xevent); | |
| 95 | |
| 96 // Update the lookup table and flag the events that should be captured and | |
| 97 // processed so that GDK doesn't get to them. | |
| 98 void InitializeEventsToCapture(void); | |
| 99 | |
| 100 #if defined(HAVE_XINPUT2) | |
| 101 // Initialize X2 input. | |
| 102 void InitializeXInput2(void); | |
| 103 | |
| 104 // The opcode used for checking events. | |
| 105 int xiopcode_; | |
| 106 #endif | |
| 107 | |
| 108 // The event source for GDK events. | |
| 109 GSource* gdksource_; | |
| 110 | |
| 111 // The default GDK event dispatcher. This is stored so that it can be restored | |
| 112 // when necessary during nested event dispatching. | |
| 113 gboolean (*gdkdispatcher_)(GSource*, GSourceFunc, void*); | |
| 114 | |
| 115 // Indicates whether a GDK event was injected by chrome (when |true|) or if it | |
| 116 // was captured and being processed by GDK (when |false|). | |
| 117 bool dispatching_event_; | |
| 118 | |
| 119 #if ! GTK_CHECK_VERSION(2,18,0) | |
| 120 // GDK_EVENT_LAST was introduced in GTK+ 2.18.0. For earlier versions, we pick a | |
| 121 // large enough value (the value of GDK_EVENT_LAST in 2.18.0) so that it works | |
| 122 // for all versions. | |
| 123 #define GDK_EVENT_LAST 37 | |
| 124 #endif | |
|
Evan Martin
2011/06/24 17:33:15
We now require GTK 2.18, so I think this code is o
| |
| 125 | |
| 126 // Ideally we would #include X.h for LASTEvent, but it brings in a lot of stupid | |
| 127 // stuff (like Time, CurrentTime etc.) that messes up a lot of things. So define | |
| 128 // XLASTEvent here to a large enough value so that it works. | |
| 129 #define XLASTEvent 40 | |
| 130 | |
| 131 // We do not want to process all the events ourselves. So we use a lookup | |
| 132 // table to quickly check if a particular event should be handled by us or if | |
| 133 // it should be passed on to the default GDK handler. | |
| 134 std::bitset<XLASTEvent> capture_x_events_; | |
| 135 std::bitset<GDK_EVENT_LAST> capture_gdk_events_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(MessagePumpX); | |
| 138 }; | |
| 139 | |
| 140 typedef MessagePumpX MessagePumpForUI; | |
| 141 | |
| 142 } // namespace base | |
| 143 | |
| 144 #endif // BASE_MESSAGE_PUMP_X_H | |
| OLD | NEW |