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 #include "ui/events/platform/x11/x11_event_source.h" | 5 #include "ui/events/platform/x11/x11_event_source.h" |
6 | 6 |
7 #include <X11/Xatom.h> | 7 #include <X11/Xatom.h> |
8 #include <X11/XKBlib.h> | 8 #include <X11/XKBlib.h> |
9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
10 | 10 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 | 89 |
90 } // namespace | 90 } // namespace |
91 | 91 |
92 X11EventSource* X11EventSource::instance_ = nullptr; | 92 X11EventSource* X11EventSource::instance_ = nullptr; |
93 | 93 |
94 X11EventSource::X11EventSource(X11EventSourceDelegate* delegate, | 94 X11EventSource::X11EventSource(X11EventSourceDelegate* delegate, |
95 XDisplay* display) | 95 XDisplay* display) |
96 : delegate_(delegate), | 96 : delegate_(delegate), |
97 display_(display), | 97 display_(display), |
98 last_seen_server_time_(CurrentTime), | |
99 event_timestamp_(CurrentTime), | 98 event_timestamp_(CurrentTime), |
100 dummy_initialized_(false), | 99 dummy_initialized_(false), |
101 continue_stream_(true) { | 100 continue_stream_(true) { |
102 DCHECK(!instance_); | 101 DCHECK(!instance_); |
103 instance_ = this; | 102 instance_ = this; |
104 | 103 |
105 DCHECK(delegate_); | 104 DCHECK(delegate_); |
106 DCHECK(display_); | 105 DCHECK(display_); |
107 DeviceDataManagerX11::CreateInstance(); | 106 DeviceDataManagerX11::CreateInstance(); |
108 InitializeXkb(display_); | 107 InitializeXkb(display_); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 145 } |
147 | 146 |
148 void X11EventSource::BlockUntilWindowMapped(XID window) { | 147 void X11EventSource::BlockUntilWindowMapped(XID window) { |
149 BlockOnWindowStructureEvent(window, MapNotify); | 148 BlockOnWindowStructureEvent(window, MapNotify); |
150 } | 149 } |
151 | 150 |
152 void X11EventSource::BlockUntilWindowUnmapped(XID window) { | 151 void X11EventSource::BlockUntilWindowUnmapped(XID window) { |
153 BlockOnWindowStructureEvent(window, UnmapNotify); | 152 BlockOnWindowStructureEvent(window, UnmapNotify); |
154 } | 153 } |
155 | 154 |
156 Time X11EventSource::UpdateLastSeenServerTime() { | 155 Time X11EventSource::GetCurrentServerTime() { |
157 base::TimeTicks start = base::TimeTicks::Now(); | |
158 | |
159 DCHECK(display_); | 156 DCHECK(display_); |
160 | 157 |
161 if (!dummy_initialized_) { | 158 if (!dummy_initialized_) { |
162 // Create a new Window and Atom that will be used for the property change. | 159 // Create a new Window and Atom that will be used for the property change. |
163 dummy_window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), | 160 dummy_window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), |
164 0, 0, 1, 1, 0, 0, 0); | 161 0, 0, 1, 1, 0, 0, 0); |
165 dummy_atom_ = XInternAtom(display_, "CHROMIUM_TIMESTAMP", False); | 162 dummy_atom_ = XInternAtom(display_, "CHROMIUM_TIMESTAMP", False); |
166 XSelectInput(display_, dummy_window_, PropertyChangeMask); | 163 XSelectInput(display_, dummy_window_, PropertyChangeMask); |
167 dummy_initialized_ = true; | 164 dummy_initialized_ = true; |
168 } | 165 } |
169 | 166 |
| 167 base::TimeTicks start = base::TimeTicks::Now(); |
| 168 |
170 // Make a no-op property change on |dummy_window_|. | 169 // Make a no-op property change on |dummy_window_|. |
171 XChangeProperty(display_, dummy_window_, dummy_atom_, XA_STRING, 8, | 170 XChangeProperty(display_, dummy_window_, dummy_atom_, XA_STRING, 8, |
172 PropModeAppend, nullptr, 0); | 171 PropModeAppend, nullptr, 0); |
173 | 172 |
174 // Observe the resulting PropertyNotify event to obtain the timestamp. | 173 // Observe the resulting PropertyNotify event to obtain the timestamp. |
175 XEvent event; | 174 XEvent event; |
176 XIfEvent(display_, &event, IsPropertyNotifyForTimestamp, | 175 XIfEvent(display_, &event, IsPropertyNotifyForTimestamp, |
177 reinterpret_cast<XPointer>(&dummy_window_)); | 176 reinterpret_cast<XPointer>(&dummy_window_)); |
178 | 177 |
179 last_seen_server_time_ = event.xproperty.time; | |
180 | |
181 UMA_HISTOGRAM_CUSTOM_COUNTS( | 178 UMA_HISTOGRAM_CUSTOM_COUNTS( |
182 "Event.Latency.X11EventSource.UpdateServerTime", | 179 "Linux.X11.ServerRTT", (base::TimeTicks::Now() - start).InMicroseconds(), |
183 (base::TimeTicks::Now() - start).InMicroseconds(), 1, | 180 1, base::TimeDelta::FromMilliseconds(50).InMicroseconds(), 50); |
184 base::TimeDelta::FromMilliseconds(1).InMicroseconds(), 50); | 181 return event.xproperty.time; |
185 return last_seen_server_time_; | |
186 } | |
187 | |
188 void X11EventSource::SetLastSeenServerTime(Time time) { | |
189 if (time != CurrentTime) { | |
190 int64_t event_time_64 = time; | |
191 int64_t time_difference = last_seen_server_time_ - event_time_64; | |
192 // Ignore timestamps that go backwards. However, X server time is a 32-bit | |
193 // millisecond counter, so if the time goes backwards by more than half the | |
194 // range of the 32-bit counter, treat it as a rollover. | |
195 if (time_difference < 0 || time_difference > (UINT32_MAX >> 1)) | |
196 last_seen_server_time_ = time; | |
197 } | |
198 } | 182 } |
199 | 183 |
200 Time X11EventSource::GetTimestamp() { | 184 Time X11EventSource::GetTimestamp() { |
201 if (event_timestamp_ != CurrentTime) { | 185 if (event_timestamp_ != CurrentTime) { |
202 return event_timestamp_; | 186 return event_timestamp_; |
203 } | 187 } |
204 DVLOG(1) << "Making a round trip to get a recent server timestamp."; | 188 DVLOG(1) << "Making a round trip to get a recent server timestamp."; |
205 return UpdateLastSeenServerTime(); | 189 return GetCurrentServerTime(); |
206 } | 190 } |
207 | 191 |
208 //////////////////////////////////////////////////////////////////////////////// | 192 //////////////////////////////////////////////////////////////////////////////// |
209 // X11EventSource, protected | 193 // X11EventSource, protected |
210 | 194 |
211 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { | 195 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { |
212 bool have_cookie = false; | 196 bool have_cookie = false; |
213 if (xevent->type == GenericEvent && | 197 if (xevent->type == GenericEvent && |
214 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { | 198 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
215 have_cookie = true; | 199 have_cookie = true; |
216 } | 200 } |
217 | 201 |
218 event_timestamp_ = ExtractTimeFromXEvent(*xevent); | 202 event_timestamp_ = ExtractTimeFromXEvent(*xevent); |
219 SetLastSeenServerTime(event_timestamp_); | |
220 | 203 |
221 delegate_->ProcessXEvent(xevent); | 204 delegate_->ProcessXEvent(xevent); |
222 PostDispatchEvent(xevent); | 205 PostDispatchEvent(xevent); |
223 | 206 |
224 event_timestamp_ = CurrentTime; | 207 event_timestamp_ = CurrentTime; |
225 | 208 |
226 if (have_cookie) | 209 if (have_cookie) |
227 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); | 210 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); |
228 } | 211 } |
229 | 212 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 258 |
276 void X11EventSource::OnDispatcherListChanged() { | 259 void X11EventSource::OnDispatcherListChanged() { |
277 if (!hotplug_event_handler_) { | 260 if (!hotplug_event_handler_) { |
278 hotplug_event_handler_.reset(new X11HotplugEventHandler()); | 261 hotplug_event_handler_.reset(new X11HotplugEventHandler()); |
279 // Force the initial device query to have an update list of active devices. | 262 // Force the initial device query to have an update list of active devices. |
280 hotplug_event_handler_->OnHotplugEvent(); | 263 hotplug_event_handler_->OnHotplugEvent(); |
281 } | 264 } |
282 } | 265 } |
283 | 266 |
284 } // namespace ui | 267 } // namespace ui |
OLD | NEW |