Chromium Code Reviews| 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(); | 156 base::TimeTicks start = base::TimeTicks::Now(); |
| 158 | 157 |
| 159 DCHECK(display_); | 158 DCHECK(display_); |
| 160 | 159 |
| 161 if (!dummy_initialized_) { | 160 if (!dummy_initialized_) { |
| 162 // Create a new Window and Atom that will be used for the property change. | 161 // Create a new Window and Atom that will be used for the property change. |
| 163 dummy_window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), | 162 dummy_window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), |
| 164 0, 0, 1, 1, 0, 0, 0); | 163 0, 0, 1, 1, 0, 0, 0); |
| 165 dummy_atom_ = XInternAtom(display_, "CHROMIUM_TIMESTAMP", False); | 164 dummy_atom_ = XInternAtom(display_, "CHROMIUM_TIMESTAMP", False); |
| 166 XSelectInput(display_, dummy_window_, PropertyChangeMask); | 165 XSelectInput(display_, dummy_window_, PropertyChangeMask); |
| 167 dummy_initialized_ = true; | 166 dummy_initialized_ = true; |
| 168 } | 167 } |
| 169 | 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 "Event.Latency.X11EventSource.UpdateServerTime", |
|
Daniel Erat
2016/08/27 01:47:07
nit: time to consider fixing this histogram name?
Tom (Use chromium acct)
2016/08/30 19:14:58
Done.
| |
| 183 (base::TimeTicks::Now() - start).InMicroseconds(), 1, | 180 (base::TimeTicks::Now() - start).InMicroseconds(), 1, |
| 184 base::TimeDelta::FromMilliseconds(1).InMicroseconds(), 50); | 181 base::TimeDelta::FromMilliseconds(1).InMicroseconds(), 50); |
| 185 return last_seen_server_time_; | 182 return event.xproperty.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 } | 183 } |
| 199 | 184 |
| 200 Time X11EventSource::GetTimestamp() { | 185 Time X11EventSource::GetTimestamp() { |
| 201 if (event_timestamp_ != CurrentTime) { | 186 if (event_timestamp_ != CurrentTime) { |
| 202 return event_timestamp_; | 187 return event_timestamp_; |
| 203 } | 188 } |
| 204 DVLOG(1) << "Making a round trip to get a recent server timestamp."; | 189 DVLOG(1) << "Making a round trip to get a recent server timestamp."; |
| 205 return UpdateLastSeenServerTime(); | 190 return GetCurrentServerTime(); |
| 206 } | 191 } |
| 207 | 192 |
| 208 //////////////////////////////////////////////////////////////////////////////// | 193 //////////////////////////////////////////////////////////////////////////////// |
| 209 // X11EventSource, protected | 194 // X11EventSource, protected |
| 210 | 195 |
| 211 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { | 196 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { |
| 212 bool have_cookie = false; | 197 bool have_cookie = false; |
| 213 if (xevent->type == GenericEvent && | 198 if (xevent->type == GenericEvent && |
| 214 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { | 199 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
| 215 have_cookie = true; | 200 have_cookie = true; |
| 216 } | 201 } |
| 217 | 202 |
| 218 event_timestamp_ = ExtractTimeFromXEvent(*xevent); | 203 event_timestamp_ = ExtractTimeFromXEvent(*xevent); |
| 219 SetLastSeenServerTime(event_timestamp_); | |
| 220 | 204 |
| 221 delegate_->ProcessXEvent(xevent); | 205 delegate_->ProcessXEvent(xevent); |
| 222 PostDispatchEvent(xevent); | 206 PostDispatchEvent(xevent); |
| 223 | 207 |
| 224 event_timestamp_ = CurrentTime; | 208 event_timestamp_ = CurrentTime; |
| 225 | 209 |
| 226 if (have_cookie) | 210 if (have_cookie) |
| 227 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); | 211 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); |
| 228 } | 212 } |
| 229 | 213 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 | 259 |
| 276 void X11EventSource::OnDispatcherListChanged() { | 260 void X11EventSource::OnDispatcherListChanged() { |
| 277 if (!hotplug_event_handler_) { | 261 if (!hotplug_event_handler_) { |
| 278 hotplug_event_handler_.reset(new X11HotplugEventHandler()); | 262 hotplug_event_handler_.reset(new X11HotplugEventHandler()); |
| 279 // Force the initial device query to have an update list of active devices. | 263 // Force the initial device query to have an update list of active devices. |
| 280 hotplug_event_handler_->OnHotplugEvent(); | 264 hotplug_event_handler_->OnHotplugEvent(); |
| 281 } | 265 } |
| 282 } | 266 } |
| 283 | 267 |
| 284 } // namespace ui | 268 } // namespace ui |
| OLD | NEW |