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/ozone/evdev/event_factory_evdev.h" | 5 #include "ui/events/ozone/evdev/event_factory_evdev.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <linux/input.h> | 8 #include <linux/input.h> |
9 | 9 |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "ui/events/ozone/device/device_event.h" |
| 14 #include "ui/events/ozone/device/device_manager.h" |
13 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" | 15 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" |
14 #include "ui/events/ozone/evdev/device_manager_evdev.h" | |
15 #include "ui/events/ozone/evdev/event_device_info.h" | 16 #include "ui/events/ozone/evdev/event_device_info.h" |
16 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" | 17 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" |
17 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" | 18 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
18 | 19 |
19 #if defined(USE_UDEV) | |
20 #include "ui/events/ozone/evdev/device_manager_udev.h" | |
21 #endif | |
22 | |
23 #if defined(USE_EVDEV_GESTURES) | 20 #if defined(USE_EVDEV_GESTURES) |
24 #include "ui/events/ozone/evdev/libgestures_glue/event_reader_libevdev_cros.h" | 21 #include "ui/events/ozone/evdev/libgestures_glue/event_reader_libevdev_cros.h" |
25 #include "ui/events/ozone/evdev/libgestures_glue/gesture_interpreter_libevdev_cr
os.h" | 22 #include "ui/events/ozone/evdev/libgestures_glue/gesture_interpreter_libevdev_cr
os.h" |
26 #endif | 23 #endif |
27 | 24 |
28 #ifndef EVIOCSCLOCKID | 25 #ifndef EVIOCSCLOCKID |
29 #define EVIOCSCLOCKID _IOW('E', 0xa0, int) | 26 #define EVIOCSCLOCKID _IOW('E', 0xa0, int) |
30 #endif | 27 #endif |
31 | 28 |
32 namespace ui { | 29 namespace ui { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // run it on the FILE thread. | 123 // run it on the FILE thread. |
127 void CloseInputDevice(const base::FilePath& path, | 124 void CloseInputDevice(const base::FilePath& path, |
128 scoped_ptr<EventConverterEvdev> converter) { | 125 scoped_ptr<EventConverterEvdev> converter) { |
129 TRACE_EVENT1("ozone", "CloseInputDevice", "path", path.value()); | 126 TRACE_EVENT1("ozone", "CloseInputDevice", "path", path.value()); |
130 converter.reset(); | 127 converter.reset(); |
131 } | 128 } |
132 | 129 |
133 } // namespace | 130 } // namespace |
134 | 131 |
135 EventFactoryEvdev::EventFactoryEvdev() | 132 EventFactoryEvdev::EventFactoryEvdev() |
136 : ui_task_runner_(base::MessageLoopProxy::current()), | 133 : device_manager_(NULL), |
| 134 has_started_processing_events_(false), |
| 135 ui_task_runner_(base::MessageLoopProxy::current()), |
137 file_task_runner_(base::MessageLoopProxy::current()), | 136 file_task_runner_(base::MessageLoopProxy::current()), |
138 cursor_(NULL), | 137 cursor_(NULL), |
139 dispatch_callback_( | 138 dispatch_callback_( |
140 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), | 139 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), |
141 base::Unretained(this))), | 140 base::Unretained(this))), |
142 weak_ptr_factory_(this) {} | 141 weak_ptr_factory_(this) {} |
143 | 142 |
144 EventFactoryEvdev::EventFactoryEvdev(CursorDelegateEvdev* cursor) | 143 EventFactoryEvdev::EventFactoryEvdev( |
145 : ui_task_runner_(base::MessageLoopProxy::current()), | 144 CursorDelegateEvdev* cursor, |
| 145 DeviceManager* device_manager) |
| 146 : device_manager_(device_manager), |
| 147 has_started_processing_events_(false), |
| 148 ui_task_runner_(base::MessageLoopProxy::current()), |
146 file_task_runner_(base::MessageLoopProxy::current()), | 149 file_task_runner_(base::MessageLoopProxy::current()), |
147 cursor_(cursor), | 150 cursor_(cursor), |
148 dispatch_callback_( | 151 dispatch_callback_( |
149 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), | 152 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), |
150 base::Unretained(this))), | 153 base::Unretained(this))), |
151 weak_ptr_factory_(this) {} | 154 weak_ptr_factory_(this) {} |
152 | 155 |
153 EventFactoryEvdev::~EventFactoryEvdev() { STLDeleteValues(&converters_); } | 156 EventFactoryEvdev::~EventFactoryEvdev() { STLDeleteValues(&converters_); } |
154 | 157 |
155 void EventFactoryEvdev::DispatchUiEvent(Event* event) { | 158 void EventFactoryEvdev::DispatchUiEvent(Event* event) { |
156 EventFactoryOzone::DispatchEvent(event); | 159 EventFactoryOzone::DispatchEvent(event); |
157 } | 160 } |
158 | 161 |
159 void EventFactoryEvdev::AttachInputDevice( | 162 void EventFactoryEvdev::AttachInputDevice( |
160 const base::FilePath& path, | 163 const base::FilePath& path, |
161 scoped_ptr<EventConverterEvdev> converter) { | 164 scoped_ptr<EventConverterEvdev> converter) { |
162 TRACE_EVENT1("ozone", "AttachInputDevice", "path", path.value()); | 165 TRACE_EVENT1("ozone", "AttachInputDevice", "path", path.value()); |
163 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 166 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
164 | 167 |
165 // If we have an existing device, detach it. We don't want two | 168 // If we have an existing device, detach it. We don't want two |
166 // devices with the same name open at the same time. | 169 // devices with the same name open at the same time. |
167 if (converters_[path]) | 170 if (converters_[path]) |
168 DetachInputDevice(path); | 171 DetachInputDevice(path); |
169 | 172 |
170 // Add initialized device to map. | 173 // Add initialized device to map. |
171 converters_[path] = converter.release(); | 174 converters_[path] = converter.release(); |
172 converters_[path]->Start(); | 175 converters_[path]->Start(); |
173 } | 176 } |
174 | 177 |
175 void EventFactoryEvdev::OnDeviceAdded(const base::FilePath& path) { | 178 void EventFactoryEvdev::OnDeviceEvent(const DeviceEvent& event) { |
176 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", path.value()); | 179 if (event.device_type() != DeviceEvent::INPUT) |
| 180 return; |
177 | 181 |
178 // Dispatch task to open on FILE thread, since open may block. | 182 switch (event.action_type()) { |
179 file_task_runner_->PostTask( | 183 case DeviceEvent::ADD: |
180 FROM_HERE, | 184 case DeviceEvent::CHANGE: { |
181 base::Bind(&OpenInputDevice, | 185 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event.path().value()); |
182 path, | 186 |
183 &modifiers_, | 187 // Dispatch task to open on FILE thread, since open may block. |
184 cursor_, | 188 file_task_runner_->PostTask( |
185 ui_task_runner_, | 189 FROM_HERE, |
186 dispatch_callback_, | 190 base::Bind(&OpenInputDevice, |
187 base::Bind(&EventFactoryEvdev::AttachInputDevice, | 191 event.path(), |
188 weak_ptr_factory_.GetWeakPtr(), | 192 &modifiers_, |
189 path))); | 193 cursor_, |
| 194 ui_task_runner_, |
| 195 dispatch_callback_, |
| 196 base::Bind(&EventFactoryEvdev::AttachInputDevice, |
| 197 weak_ptr_factory_.GetWeakPtr(), |
| 198 event.path()))); |
| 199 } |
| 200 break; |
| 201 case DeviceEvent::REMOVE: { |
| 202 TRACE_EVENT1("ozone", "OnDeviceRemoved", "path", event.path().value()); |
| 203 DetachInputDevice(event.path()); |
| 204 } |
| 205 break; |
| 206 } |
190 } | 207 } |
191 | 208 |
192 void EventFactoryEvdev::DetachInputDevice(const base::FilePath& path) { | 209 void EventFactoryEvdev::DetachInputDevice(const base::FilePath& path) { |
193 TRACE_EVENT1("ozone", "DetachInputDevice", "path", path.value()); | 210 TRACE_EVENT1("ozone", "DetachInputDevice", "path", path.value()); |
194 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 211 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
195 | 212 |
196 // Remove device from map. | 213 // Remove device from map. |
197 scoped_ptr<EventConverterEvdev> converter(converters_[path]); | 214 scoped_ptr<EventConverterEvdev> converter(converters_[path]); |
198 converters_.erase(path); | 215 converters_.erase(path); |
199 | 216 |
200 if (converter) { | 217 if (converter) { |
201 // Cancel libevent notifications from this converter. This part must be | 218 // Cancel libevent notifications from this converter. This part must be |
202 // on UI since the polling happens on UI. | 219 // on UI since the polling happens on UI. |
203 converter->Stop(); | 220 converter->Stop(); |
204 | 221 |
205 // Dispatch task to close on FILE thread, since close may block. | 222 // Dispatch task to close on FILE thread, since close may block. |
206 file_task_runner_->PostTask( | 223 file_task_runner_->PostTask( |
207 FROM_HERE, | 224 FROM_HERE, |
208 base::Bind(&CloseInputDevice, path, base::Passed(&converter))); | 225 base::Bind(&CloseInputDevice, path, base::Passed(&converter))); |
209 } | 226 } |
210 } | 227 } |
211 | 228 |
212 void EventFactoryEvdev::OnDeviceRemoved(const base::FilePath& path) { | |
213 TRACE_EVENT1("ozone", "OnDeviceRemoved", "path", path.value()); | |
214 DetachInputDevice(path); | |
215 } | |
216 | |
217 void EventFactoryEvdev::StartProcessingEvents() { | 229 void EventFactoryEvdev::StartProcessingEvents() { |
218 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 230 CHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
219 | 231 |
220 #if defined(USE_UDEV) | 232 if (device_manager_ && !has_started_processing_events_) { |
221 // Scan for input devices using udev. | 233 has_started_processing_events_ = true; |
222 device_manager_ = CreateDeviceManagerUdev(); | 234 // Scan & monitor devices. |
223 #else | 235 device_manager_->AddObserver(this); |
224 // No udev support. Scan devices manually in /dev/input. | 236 device_manager_->ScanDevices(this); |
225 device_manager_ = CreateDeviceManagerManual(); | 237 } |
226 #endif | |
227 | |
228 // Scan & monitor devices. | |
229 device_manager_->ScanAndStartMonitoring( | |
230 base::Bind(&EventFactoryEvdev::OnDeviceAdded, base::Unretained(this)), | |
231 base::Bind(&EventFactoryEvdev::OnDeviceRemoved, base::Unretained(this))); | |
232 } | 238 } |
233 | 239 |
234 void EventFactoryEvdev::SetFileTaskRunner( | 240 void EventFactoryEvdev::SetFileTaskRunner( |
235 scoped_refptr<base::TaskRunner> task_runner) { | 241 scoped_refptr<base::TaskRunner> task_runner) { |
236 file_task_runner_ = task_runner; | 242 file_task_runner_ = task_runner; |
237 } | 243 } |
238 | 244 |
239 void EventFactoryEvdev::WarpCursorTo(gfx::AcceleratedWidget widget, | 245 void EventFactoryEvdev::WarpCursorTo(gfx::AcceleratedWidget widget, |
240 const gfx::PointF& location) { | 246 const gfx::PointF& location) { |
241 if (cursor_) { | 247 if (cursor_) { |
242 cursor_->MoveCursorTo(widget, location); | 248 cursor_->MoveCursorTo(widget, location); |
243 MouseEvent mouse_event(ET_MOUSE_MOVED, | 249 MouseEvent mouse_event(ET_MOUSE_MOVED, |
244 cursor_->location(), | 250 cursor_->location(), |
245 cursor_->location(), | 251 cursor_->location(), |
246 modifiers_.GetModifierFlags(), | 252 modifiers_.GetModifierFlags(), |
247 /* changed_button_flags */ 0); | 253 /* changed_button_flags */ 0); |
248 DispatchEvent(&mouse_event); | 254 DispatchEvent(&mouse_event); |
249 } | 255 } |
250 } | 256 } |
251 | 257 |
252 } // namespace ui | 258 } // namespace ui |
OLD | NEW |