| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ | 5 #ifndef UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ |
| 6 #define UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ | 6 #define UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <xcb/xcb.h> |
| 9 | 10 |
| 11 #include <list> |
| 10 #include <memory> | 12 #include <memory> |
| 13 #include <queue> |
| 11 #include <stack> | 14 #include <stack> |
| 12 | 15 |
| 13 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 14 #include "base/optional.h" | 18 #include "base/optional.h" |
| 15 #include "ui/events/events_export.h" | 19 #include "ui/events/events_export.h" |
| 16 #include "ui/gfx/x/x11_types.h" | 20 #include "ui/gfx/x/x11_types.h" |
| 17 | 21 |
| 18 using Time = unsigned long; | 22 using Time = unsigned long; |
| 19 using XEvent = union _XEvent; | 23 using XEvent = union _XEvent; |
| 20 using XID = unsigned long; | 24 using XID = unsigned long; |
| 21 using XWindow = unsigned long; | 25 using XWindow = unsigned long; |
| 22 | 26 |
| 23 namespace gfx { | 27 namespace gfx { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 virtual void ProcessXEvent(XEvent* xevent) = 0; | 44 virtual void ProcessXEvent(XEvent* xevent) = 0; |
| 41 | 45 |
| 42 private: | 46 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(X11EventSourceDelegate); | 47 DISALLOW_COPY_AND_ASSIGN(X11EventSourceDelegate); |
| 44 }; | 48 }; |
| 45 | 49 |
| 46 // Receives X11 events and sends them to X11EventSourceDelegate. Handles | 50 // Receives X11 events and sends them to X11EventSourceDelegate. Handles |
| 47 // receiving, pre-process and post-processing XEvents. | 51 // receiving, pre-process and post-processing XEvents. |
| 48 class EVENTS_EXPORT X11EventSource { | 52 class EVENTS_EXPORT X11EventSource { |
| 49 public: | 53 public: |
| 54 class EVENTS_EXPORT Request { |
| 55 public: |
| 56 ~Request(); |
| 57 |
| 58 virtual void OnReply(xcb_generic_reply_t* reply, |
| 59 xcb_generic_error_t* error) = 0; |
| 60 |
| 61 uint32_t sequence() { return sequence_; } |
| 62 |
| 63 base::WeakPtr<Request> GetWeakPtr(); |
| 64 |
| 65 protected: |
| 66 Request(uint32_t sequence); |
| 67 |
| 68 uint32_t sequence_; |
| 69 |
| 70 // State necessary for discarding. Set during |
| 71 // X11EventSource::EnqueueRequest. |
| 72 std::list<std::unique_ptr<Request>>::iterator it_; |
| 73 |
| 74 base::WeakPtrFactory<Request> weak_factory_; |
| 75 |
| 76 private: |
| 77 friend class X11EventSource; |
| 78 }; |
| 79 |
| 50 X11EventSource(X11EventSourceDelegate* delegate, XDisplay* display); | 80 X11EventSource(X11EventSourceDelegate* delegate, XDisplay* display); |
| 51 ~X11EventSource(); | 81 ~X11EventSource(); |
| 52 | 82 |
| 53 static bool HasInstance(); | 83 static bool HasInstance(); |
| 54 | 84 |
| 55 static X11EventSource* GetInstance(); | 85 static X11EventSource* GetInstance(); |
| 56 | 86 |
| 57 // Called when there is a new XEvent available. Processes all (if any) | 87 // Called when there is a new XEvent available. Processes all (if any) |
| 58 // available X events. | 88 // available X events. |
| 59 void DispatchXEvents(); | 89 void DispatchXEvents(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 80 | 110 |
| 81 // Returns the timestamp of the event currently being dispatched. Falls back | 111 // Returns the timestamp of the event currently being dispatched. Falls back |
| 82 // on GetCurrentServerTime() if there's no event being dispatched, or if the | 112 // on GetCurrentServerTime() if there's no event being dispatched, or if the |
| 83 // current event does not have a timestamp. | 113 // current event does not have a timestamp. |
| 84 Time GetTimestamp(); | 114 Time GetTimestamp(); |
| 85 | 115 |
| 86 // Returns the root pointer location only if there is an event being | 116 // Returns the root pointer location only if there is an event being |
| 87 // dispatched that contains that information. | 117 // dispatched that contains that information. |
| 88 base::Optional<gfx::Point> GetRootCursorLocationFromCurrentEvent() const; | 118 base::Optional<gfx::Point> GetRootCursorLocationFromCurrentEvent() const; |
| 89 | 119 |
| 120 // Takes ownership of |request|. |
| 121 void EnqueueRequest(Request* request); |
| 122 |
| 123 void DiscardRequest(Request* request); |
| 124 |
| 125 // Blocks until the reply corresponding to |request| is available. Dispatches |
| 126 // the request in a nested-message-loop fashion. Returns true iff the request |
| 127 // resulted in an error. |
| 128 bool DispatchRequestNow(Request* request); |
| 129 |
| 90 void StopCurrentEventStream(); | 130 void StopCurrentEventStream(); |
| 91 void OnDispatcherListChanged(); | 131 void OnDispatcherListChanged(); |
| 92 | 132 |
| 93 protected: | 133 protected: |
| 94 // Extracts cookie data from |xevent| if it's of GenericType, and dispatches | 134 // Extracts cookie data from |xevent| if it's of GenericType, and dispatches |
| 95 // the event. This function also frees up the cookie data after dispatch is | 135 // the event. This function also frees up the cookie data after dispatch is |
| 96 // complete. | 136 // complete. |
| 97 void ExtractCookieDataDispatchEvent(XEvent* xevent); | 137 void ExtractCookieDataDispatchEvent(XEvent* xevent); |
| 98 | 138 |
| 99 // Handles updates after event has been dispatched. | 139 // Handles updates after event has been dispatched. |
| 100 void PostDispatchEvent(XEvent* xevent); | 140 void PostDispatchEvent(XEvent* xevent); |
| 101 | 141 |
| 102 // Block until receiving a structure notify event of |type| on |window|. | 142 // Block until receiving a structure notify event of |type| on |window|. |
| 103 // Dispatch all encountered events prior to the one we're blocking on. | 143 // Dispatch all encountered events prior to the one we're blocking on. |
| 104 void BlockOnWindowStructureEvent(XID window, int type); | 144 void BlockOnWindowStructureEvent(XID window, int type); |
| 105 | 145 |
| 106 // Explicitly asks the X11 server for the current timestamp, and updates | 146 // Explicitly asks the X11 server for the current timestamp, and updates |
| 107 // |last_seen_server_time_| with this value. | 147 // |last_seen_server_time_| with this value. |
| 108 Time GetCurrentServerTime(); | 148 Time GetCurrentServerTime(); |
| 109 | 149 |
| 150 bool HasNextReply(); |
| 151 uint32_t NextReplySequence(); |
| 152 void ProcessRequest(Request* request, |
| 153 xcb_generic_reply_t* reply, |
| 154 xcb_generic_error_t* error); |
| 155 void ProcessNextReply(); |
| 156 |
| 157 bool HasNextEvent(); |
| 158 uint32_t NextEventSequence(); |
| 159 void ProcessNextEvent(); |
| 160 |
| 110 private: | 161 private: |
| 162 friend class TestX11EventSource; |
| 111 static X11EventSource* instance_; | 163 static X11EventSource* instance_; |
| 112 | 164 |
| 113 X11EventSourceDelegate* delegate_; | 165 X11EventSourceDelegate* delegate_; |
| 114 | 166 |
| 115 // The connection to the X11 server used to receive the events. | 167 // The connection to the X11 server used to receive the events. |
| 116 XDisplay* display_; | 168 XDisplay* display_; |
| 169 xcb_connection_t* connection_; |
| 117 | 170 |
| 118 // Events currently being dispatched. The topmost event in this stack | 171 // Events currently being dispatched. The topmost event in this stack |
| 119 // corresponds to the deepest-nested message loop. | 172 // corresponds to the deepest-nested message loop. |
| 120 std::stack<XEvent*> dispatching_events_; | 173 std::stack<XEvent*> dispatching_events_; |
| 121 | 174 |
| 122 // State necessary for UpdateLastSeenServerTime | 175 // State necessary for UpdateLastSeenServerTime |
| 123 bool dummy_initialized_; | 176 bool dummy_initialized_; |
| 124 XWindow dummy_window_; | 177 XWindow dummy_window_; |
| 125 XAtom dummy_atom_; | 178 XAtom dummy_atom_; |
| 126 std::unique_ptr<XScopedEventSelector> dummy_window_events_; | 179 std::unique_ptr<XScopedEventSelector> dummy_window_events_; |
| 127 | 180 |
| 181 // Outstanding requests that should be serviced. |
| 182 std::list<std::unique_ptr<Request>> request_queue_; |
| 183 |
| 184 // For HasNextReply(); |
| 185 void* next_reply_; |
| 186 xcb_generic_error_t* next_error_; |
| 187 |
| 128 // Keeps track of whether this source should continue to dispatch all the | 188 // Keeps track of whether this source should continue to dispatch all the |
| 129 // available events. | 189 // available events. |
| 130 bool continue_stream_ = true; | 190 bool continue_stream_ = true; |
| 131 | 191 |
| 132 std::unique_ptr<X11HotplugEventHandler> hotplug_event_handler_; | 192 std::unique_ptr<X11HotplugEventHandler> hotplug_event_handler_; |
| 133 | 193 |
| 134 DISALLOW_COPY_AND_ASSIGN(X11EventSource); | 194 DISALLOW_COPY_AND_ASSIGN(X11EventSource); |
| 135 }; | 195 }; |
| 136 | 196 |
| 137 } // namespace ui | 197 } // namespace ui |
| 138 | 198 |
| 139 #endif // UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ | 199 #endif // UI_EVENTS_PLATFORM_X11_X11_EVENT_SOURCE_H_ |
| OLD | NEW |