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/XKBlib.h> | 8 #include <X11/XKBlib.h> |
8 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" |
11 #include "ui/events/devices/x11/device_data_manager_x11.h" | 13 #include "ui/events/devices/x11/device_data_manager_x11.h" |
12 #include "ui/events/devices/x11/touch_factory_x11.h" | 14 #include "ui/events/devices/x11/touch_factory_x11.h" |
13 #include "ui/events/event_utils.h" | 15 #include "ui/events/event_utils.h" |
14 #include "ui/events/platform/platform_event_dispatcher.h" | 16 #include "ui/events/platform/platform_event_dispatcher.h" |
15 #include "ui/events/platform/x11/x11_hotplug_event_handler.h" | 17 #include "ui/events/platform/x11/x11_hotplug_event_handler.h" |
16 | 18 |
17 namespace ui { | 19 namespace ui { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 return CurrentTime; | 73 return CurrentTime; |
72 } | 74 } |
73 | 75 |
74 void UpdateDeviceList() { | 76 void UpdateDeviceList() { |
75 XDisplay* display = gfx::GetXDisplay(); | 77 XDisplay* display = gfx::GetXDisplay(); |
76 DeviceListCacheX11::GetInstance()->UpdateDeviceList(display); | 78 DeviceListCacheX11::GetInstance()->UpdateDeviceList(display); |
77 TouchFactory::GetInstance()->UpdateDeviceList(display); | 79 TouchFactory::GetInstance()->UpdateDeviceList(display); |
78 DeviceDataManagerX11::GetInstance()->UpdateDeviceList(display); | 80 DeviceDataManagerX11::GetInstance()->UpdateDeviceList(display); |
79 } | 81 } |
80 | 82 |
| 83 Bool IsPropertyNotifyForTimestamp(Display* display, |
| 84 XEvent* event, |
| 85 XPointer arg) { |
| 86 return event->type == PropertyNotify && |
| 87 event->xproperty.window == *reinterpret_cast<Window*>(arg); |
| 88 } |
| 89 |
81 } // namespace | 90 } // namespace |
82 | 91 |
83 X11EventSource* X11EventSource::instance_ = nullptr; | 92 X11EventSource* X11EventSource::instance_ = nullptr; |
84 | 93 |
85 X11EventSource::X11EventSource(X11EventSourceDelegate* delegate, | 94 X11EventSource::X11EventSource(X11EventSourceDelegate* delegate, |
86 XDisplay* display) | 95 XDisplay* display) |
87 : delegate_(delegate), | 96 : delegate_(delegate), |
88 display_(display), | 97 display_(display), |
89 last_seen_server_time_(CurrentTime), | 98 last_seen_server_time_(CurrentTime), |
| 99 dummy_initialized_(false), |
90 continue_stream_(true) { | 100 continue_stream_(true) { |
91 DCHECK(!instance_); | 101 DCHECK(!instance_); |
92 instance_ = this; | 102 instance_ = this; |
93 | 103 |
94 DCHECK(delegate_); | 104 DCHECK(delegate_); |
95 DCHECK(display_); | 105 DCHECK(display_); |
96 DeviceDataManagerX11::CreateInstance(); | 106 DeviceDataManagerX11::CreateInstance(); |
97 InitializeXkb(display_); | 107 InitializeXkb(display_); |
98 } | 108 } |
99 | 109 |
100 X11EventSource::~X11EventSource() { | 110 X11EventSource::~X11EventSource() { |
101 DCHECK_EQ(this, instance_); | 111 DCHECK_EQ(this, instance_); |
102 instance_ = nullptr; | 112 instance_ = nullptr; |
| 113 if (dummy_initialized_) |
| 114 XDestroyWindow(display_, dummy_window_); |
103 } | 115 } |
104 | 116 |
105 // static | 117 // static |
106 X11EventSource* X11EventSource::GetInstance() { | 118 X11EventSource* X11EventSource::GetInstance() { |
107 DCHECK(instance_); | 119 DCHECK(instance_); |
108 return instance_; | 120 return instance_; |
109 } | 121 } |
110 | 122 |
111 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
112 // X11EventSource, public | 124 // X11EventSource, public |
(...skipping 14 matching lines...) Expand all Loading... |
127 void X11EventSource::BlockUntilWindowMapped(XID window) { | 139 void X11EventSource::BlockUntilWindowMapped(XID window) { |
128 XEvent event; | 140 XEvent event; |
129 do { | 141 do { |
130 // Block until there's a message of |event_mask| type on |w|. Then remove | 142 // Block until there's a message of |event_mask| type on |w|. Then remove |
131 // it from the queue and stuff it in |event|. | 143 // it from the queue and stuff it in |event|. |
132 XWindowEvent(display_, window, StructureNotifyMask, &event); | 144 XWindowEvent(display_, window, StructureNotifyMask, &event); |
133 ExtractCookieDataDispatchEvent(&event); | 145 ExtractCookieDataDispatchEvent(&event); |
134 } while (event.type != MapNotify); | 146 } while (event.type != MapNotify); |
135 } | 147 } |
136 | 148 |
| 149 Time X11EventSource::UpdateLastSeenServerTime() { |
| 150 base::TimeTicks start = base::TimeTicks::Now(); |
| 151 |
| 152 DCHECK(display_); |
| 153 |
| 154 if (!dummy_initialized_) { |
| 155 // Create a new Window and Atom that will be used for the property change. |
| 156 dummy_window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), |
| 157 0, 0, 1, 1, 0, 0, 0); |
| 158 dummy_atom_ = XInternAtom(display_, "CHROMIUM_TIMESTAMP", False); |
| 159 XSelectInput(display_, dummy_window_, PropertyChangeMask); |
| 160 dummy_initialized_ = true; |
| 161 } |
| 162 |
| 163 // Make a no-op property change on |dummy_window_|. |
| 164 XChangeProperty(display_, dummy_window_, dummy_atom_, XA_STRING, 8, |
| 165 PropModeAppend, nullptr, 0); |
| 166 |
| 167 // Observe the resulting PropertyNotify event to obtain the timestamp. |
| 168 XEvent event; |
| 169 XIfEvent(display_, &event, IsPropertyNotifyForTimestamp, |
| 170 reinterpret_cast<XPointer>(&dummy_window_)); |
| 171 |
| 172 last_seen_server_time_ = event.xproperty.time; |
| 173 |
| 174 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 175 "Event.Latency.X11EventSource.UpdateServerTime", |
| 176 (base::TimeTicks::Now() - start).InMicroseconds(), 0, |
| 177 base::TimeDelta::FromMilliseconds(1).InMicroseconds(), 50); |
| 178 return last_seen_server_time_; |
| 179 } |
| 180 |
137 //////////////////////////////////////////////////////////////////////////////// | 181 //////////////////////////////////////////////////////////////////////////////// |
138 // X11EventSource, protected | 182 // X11EventSource, protected |
139 | 183 |
140 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { | 184 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { |
141 bool have_cookie = false; | 185 bool have_cookie = false; |
142 if (xevent->type == GenericEvent && | 186 if (xevent->type == GenericEvent && |
143 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { | 187 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
144 have_cookie = true; | 188 have_cookie = true; |
145 } | 189 } |
146 Time event_time = ExtractTimeFromXEvent(*xevent); | 190 Time event_time = ExtractTimeFromXEvent(*xevent); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 227 |
184 void X11EventSource::OnDispatcherListChanged() { | 228 void X11EventSource::OnDispatcherListChanged() { |
185 if (!hotplug_event_handler_) { | 229 if (!hotplug_event_handler_) { |
186 hotplug_event_handler_.reset(new X11HotplugEventHandler()); | 230 hotplug_event_handler_.reset(new X11HotplugEventHandler()); |
187 // Force the initial device query to have an update list of active devices. | 231 // Force the initial device query to have an update list of active devices. |
188 hotplug_event_handler_->OnHotplugEvent(); | 232 hotplug_event_handler_->OnHotplugEvent(); |
189 } | 233 } |
190 } | 234 } |
191 | 235 |
192 } // namespace ui | 236 } // namespace ui |
OLD | NEW |