| 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 "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
| 15 #include "ui/events/ozone/device/device_event.h" | 15 #include "ui/events/ozone/device/device_event.h" |
| 16 #include "ui/events/ozone/device/device_manager.h" | 16 #include "ui/events/ozone/device/device_manager.h" |
| 17 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" | 17 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" |
| 18 #include "ui/events/ozone/evdev/event_device_info.h" | 18 #include "ui/events/ozone/evdev/event_device_info.h" |
| 19 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" | 19 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" |
| 20 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" | 20 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
| 21 | 21 |
| 22 #if defined(USE_EVDEV_GESTURES) | 22 #if defined(USE_EVDEV_GESTURES) |
| 23 #include "ui/events/ozone/evdev/libgestures_glue/event_reader_libevdev_cros.h" | 23 #include "ui/events/ozone/evdev/libgestures_glue/event_reader_libevdev_cros.h" |
| 24 #include "ui/events/ozone/evdev/libgestures_glue/gesture_interpreter_libevdev_cr
os.h" | 24 #include "ui/events/ozone/evdev/libgestures_glue/gesture_interpreter_libevdev_cr
os.h" |
| 25 #include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h" |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 #ifndef EVIOCSCLOCKID | 28 #ifndef EVIOCSCLOCKID |
| 28 #define EVIOCSCLOCKID _IOW('E', 0xa0, int) | 29 #define EVIOCSCLOCKID _IOW('E', 0xa0, int) |
| 29 #endif | 30 #endif |
| 30 | 31 |
| 31 namespace ui { | 32 namespace ui { |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 #if defined(USE_EVDEV_GESTURES) | 36 #if defined(USE_EVDEV_GESTURES) |
| 36 bool UseGesturesLibraryForDevice(const EventDeviceInfo& devinfo) { | 37 bool UseGesturesLibraryForDevice(const EventDeviceInfo& devinfo) { |
| 37 if (devinfo.HasAbsXY() && !devinfo.IsMappedToScreen()) | 38 if (devinfo.HasAbsXY() && !devinfo.IsMappedToScreen()) |
| 38 return true; // touchpad | 39 return true; // touchpad |
| 39 | 40 |
| 40 if (devinfo.HasRelXY()) | 41 if (devinfo.HasRelXY()) |
| 41 return true; // mouse | 42 return true; // mouse |
| 42 | 43 |
| 43 return false; | 44 return false; |
| 44 } | 45 } |
| 45 #endif | 46 #endif |
| 46 | 47 |
| 47 scoped_ptr<EventConverterEvdev> CreateConverter( | 48 scoped_ptr<EventConverterEvdev> CreateConverter( |
| 48 int fd, | 49 int fd, |
| 49 const base::FilePath& path, | 50 const base::FilePath& path, |
| 50 const EventDeviceInfo& devinfo, | 51 const EventDeviceInfo& devinfo, |
| 51 const EventDispatchCallback& dispatch, | 52 const EventDispatchCallback& dispatch, |
| 52 EventModifiersEvdev* modifiers, | 53 EventModifiersEvdev* modifiers, |
| 53 CursorDelegateEvdev* cursor) { | 54 CursorDelegateEvdev* cursor |
| 55 #if defined(USE_EVDEV_GESTURES) |
| 56 , |
| 57 GesturePropertyProvider* prop_provider |
| 58 #endif |
| 59 ) { |
| 54 #if defined(USE_EVDEV_GESTURES) | 60 #if defined(USE_EVDEV_GESTURES) |
| 55 // Touchpad or mouse: use gestures library. | 61 // Touchpad or mouse: use gestures library. |
| 56 // EventReaderLibevdevCros -> GestureInterpreterLibevdevCros -> DispatchEvent | 62 // EventReaderLibevdevCros -> GestureInterpreterLibevdevCros -> DispatchEvent |
| 57 if (UseGesturesLibraryForDevice(devinfo)) { | 63 if (UseGesturesLibraryForDevice(devinfo)) { |
| 58 scoped_ptr<GestureInterpreterLibevdevCros> gesture_interp = make_scoped_ptr( | 64 scoped_ptr<GestureInterpreterLibevdevCros> gesture_interp = |
| 59 new GestureInterpreterLibevdevCros(modifiers, cursor, dispatch)); | 65 make_scoped_ptr(new GestureInterpreterLibevdevCros( |
| 66 modifiers, cursor, prop_provider, dispatch)); |
| 60 scoped_ptr<EventReaderLibevdevCros> libevdev_reader = | 67 scoped_ptr<EventReaderLibevdevCros> libevdev_reader = |
| 61 make_scoped_ptr(new EventReaderLibevdevCros( | 68 make_scoped_ptr(new EventReaderLibevdevCros( |
| 62 fd, | 69 fd, |
| 63 path, | 70 path, |
| 64 gesture_interp.PassAs<EventReaderLibevdevCros::Delegate>())); | 71 gesture_interp.PassAs<EventReaderLibevdevCros::Delegate>())); |
| 65 return libevdev_reader.PassAs<EventConverterEvdev>(); | 72 return libevdev_reader.PassAs<EventConverterEvdev>(); |
| 66 } | 73 } |
| 67 #endif | 74 #endif |
| 68 | 75 |
| 69 // Touchscreen: use TouchEventConverterEvdev. | 76 // Touchscreen: use TouchEventConverterEvdev. |
| 70 scoped_ptr<EventConverterEvdev> converter; | 77 scoped_ptr<EventConverterEvdev> converter; |
| 71 if (devinfo.HasAbsXY()) | 78 if (devinfo.HasAbsXY()) |
| 72 return make_scoped_ptr<EventConverterEvdev>( | 79 return make_scoped_ptr<EventConverterEvdev>( |
| 73 new TouchEventConverterEvdev(fd, path, devinfo, dispatch)); | 80 new TouchEventConverterEvdev(fd, path, devinfo, dispatch)); |
| 74 | 81 |
| 75 // Everything else: use KeyEventConverterEvdev. | 82 // Everything else: use KeyEventConverterEvdev. |
| 76 return make_scoped_ptr<EventConverterEvdev>( | 83 return make_scoped_ptr<EventConverterEvdev>( |
| 77 new KeyEventConverterEvdev(fd, path, modifiers, dispatch)); | 84 new KeyEventConverterEvdev(fd, path, modifiers, dispatch)); |
| 78 } | 85 } |
| 79 | 86 |
| 80 // Open an input device. Opening may put the calling thread to sleep, and | 87 // Open an input device. Opening may put the calling thread to sleep, and |
| 81 // therefore should be run on a thread where latency is not critical. We | 88 // therefore should be run on a thread where latency is not critical. We |
| 82 // run it on a thread from the worker pool. | 89 // run it on a thread from the worker pool. |
| 83 // | 90 // |
| 84 // This takes a TaskRunner and runs the reply on that thread, so that we | 91 // This takes a TaskRunner and runs the reply on that thread, so that we |
| 85 // can hop threads if necessary (back to the UI thread). | 92 // can hop threads if necessary (back to the UI thread). |
| 86 void OpenInputDevice( | 93 void OpenInputDevice( |
| 87 const base::FilePath& path, | 94 const base::FilePath& path, |
| 88 EventModifiersEvdev* modifiers, | 95 EventModifiersEvdev* modifiers, |
| 89 CursorDelegateEvdev* cursor, | 96 CursorDelegateEvdev* cursor |
| 97 #if defined(USE_EVDEV_GESTURES) |
| 98 , |
| 99 GesturePropertyProvider* prop_provider, |
| 100 #endif |
| 90 scoped_refptr<base::TaskRunner> reply_runner, | 101 scoped_refptr<base::TaskRunner> reply_runner, |
| 91 const EventDispatchCallback& dispatch, | 102 const EventDispatchCallback& dispatch, |
| 92 base::Callback<void(scoped_ptr<EventConverterEvdev>)> reply_callback) { | 103 base::Callback<void(scoped_ptr<EventConverterEvdev>)> reply_callback) { |
| 93 TRACE_EVENT1("ozone", "OpenInputDevice", "path", path.value()); | 104 TRACE_EVENT1("ozone", "OpenInputDevice", "path", path.value()); |
| 94 | 105 |
| 95 int fd = open(path.value().c_str(), O_RDONLY | O_NONBLOCK); | 106 int fd = open(path.value().c_str(), O_RDONLY | O_NONBLOCK); |
| 96 if (fd < 0) { | 107 if (fd < 0) { |
| 97 PLOG(ERROR) << "Cannot open '" << path.value(); | 108 PLOG(ERROR) << "Cannot open '" << path.value(); |
| 98 return; | 109 return; |
| 99 } | 110 } |
| 100 | 111 |
| 101 // Use monotonic timestamps for events. The touch code in particular | 112 // Use monotonic timestamps for events. The touch code in particular |
| 102 // expects event timestamps to correlate to the monotonic clock | 113 // expects event timestamps to correlate to the monotonic clock |
| 103 // (base::TimeTicks). | 114 // (base::TimeTicks). |
| 104 unsigned int clk = CLOCK_MONOTONIC; | 115 unsigned int clk = CLOCK_MONOTONIC; |
| 105 if (ioctl(fd, EVIOCSCLOCKID, &clk)) | 116 if (ioctl(fd, EVIOCSCLOCKID, &clk)) |
| 106 PLOG(ERROR) << "failed to set CLOCK_MONOTONIC"; | 117 PLOG(ERROR) << "failed to set CLOCK_MONOTONIC"; |
| 107 | 118 |
| 108 EventDeviceInfo devinfo; | 119 EventDeviceInfo devinfo; |
| 109 if (!devinfo.Initialize(fd)) { | 120 if (!devinfo.Initialize(fd)) { |
| 110 LOG(ERROR) << "failed to get device information for " << path.value(); | 121 LOG(ERROR) << "failed to get device information for " << path.value(); |
| 111 close(fd); | 122 close(fd); |
| 112 return; | 123 return; |
| 113 } | 124 } |
| 114 | 125 |
| 115 scoped_ptr<EventConverterEvdev> converter = | 126 scoped_ptr<EventConverterEvdev> converter = CreateConverter(fd, |
| 116 CreateConverter(fd, path, devinfo, dispatch, modifiers, cursor); | 127 path, |
| 128 devinfo, |
| 129 dispatch, |
| 130 modifiers, |
| 131 cursor |
| 132 #if defined(USE_EVDEV_GESTURES) |
| 133 , |
| 134 prop_provider |
| 135 #endif |
| 136 ); |
| 117 | 137 |
| 118 // Reply with the constructed converter. | 138 // Reply with the constructed converter. |
| 119 reply_runner->PostTask(FROM_HERE, | 139 reply_runner->PostTask(FROM_HERE, |
| 120 base::Bind(reply_callback, base::Passed(&converter))); | 140 base::Bind(reply_callback, base::Passed(&converter))); |
| 121 } | 141 } |
| 122 | 142 |
| 123 // Close an input device. Closing may put the calling thread to sleep, and | 143 // Close an input device. Closing may put the calling thread to sleep, and |
| 124 // therefore should be run on a thread where latency is not critical. We | 144 // therefore should be run on a thread where latency is not critical. We |
| 125 // run it on the FILE thread. | 145 // run it on the FILE thread. |
| 126 void CloseInputDevice(const base::FilePath& path, | 146 void CloseInputDevice(const base::FilePath& path, |
| 127 scoped_ptr<EventConverterEvdev> converter) { | 147 scoped_ptr<EventConverterEvdev> converter) { |
| 128 TRACE_EVENT1("ozone", "CloseInputDevice", "path", path.value()); | 148 TRACE_EVENT1("ozone", "CloseInputDevice", "path", path.value()); |
| 129 converter.reset(); | 149 converter.reset(); |
| 130 } | 150 } |
| 131 | 151 |
| 132 } // namespace | 152 } // namespace |
| 133 | 153 |
| 134 EventFactoryEvdev::EventFactoryEvdev( | 154 EventFactoryEvdev::EventFactoryEvdev(CursorDelegateEvdev* cursor, |
| 135 CursorDelegateEvdev* cursor, | 155 DeviceManager* device_manager) |
| 136 DeviceManager* device_manager) | |
| 137 : device_manager_(device_manager), | 156 : device_manager_(device_manager), |
| 138 cursor_(cursor), | 157 cursor_(cursor) |
| 158 #if defined(USE_EVDEV_GESTURES) |
| 159 , |
| 160 gesture_prop_provider_(new GesturePropertyProvider), |
| 161 #endif |
| 139 dispatch_callback_( | 162 dispatch_callback_( |
| 140 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), | 163 base::Bind(base::IgnoreResult(&EventFactoryEvdev::DispatchUiEvent), |
| 141 base::Unretained(this))), | 164 base::Unretained(this))), |
| 142 weak_ptr_factory_(this) { | 165 weak_ptr_factory_(this) { |
| 143 DCHECK(device_manager_); | 166 DCHECK(device_manager_); |
| 144 } | 167 } |
| 145 | 168 |
| 146 EventFactoryEvdev::~EventFactoryEvdev() { STLDeleteValues(&converters_); } | 169 EventFactoryEvdev::~EventFactoryEvdev() { STLDeleteValues(&converters_); } |
| 147 | 170 |
| 148 void EventFactoryEvdev::DispatchUiEvent(Event* event) { | 171 void EventFactoryEvdev::DispatchUiEvent(Event* event) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 173 case DeviceEvent::ADD: | 196 case DeviceEvent::ADD: |
| 174 case DeviceEvent::CHANGE: { | 197 case DeviceEvent::CHANGE: { |
| 175 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event.path().value()); | 198 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event.path().value()); |
| 176 | 199 |
| 177 // Dispatch task to open from the worker pool, since open may block. | 200 // Dispatch task to open from the worker pool, since open may block. |
| 178 base::WorkerPool::PostTask( | 201 base::WorkerPool::PostTask( |
| 179 FROM_HERE, | 202 FROM_HERE, |
| 180 base::Bind(&OpenInputDevice, | 203 base::Bind(&OpenInputDevice, |
| 181 event.path(), | 204 event.path(), |
| 182 &modifiers_, | 205 &modifiers_, |
| 183 cursor_, | 206 cursor_ |
| 207 #if defined(USE_EVDEV_GESTURES) |
| 208 , |
| 209 gesture_prop_provider_.get(), |
| 210 #endif |
| 184 ui_task_runner_, | 211 ui_task_runner_, |
| 185 dispatch_callback_, | 212 dispatch_callback_, |
| 186 base::Bind(&EventFactoryEvdev::AttachInputDevice, | 213 base::Bind(&EventFactoryEvdev::AttachInputDevice, |
| 187 weak_ptr_factory_.GetWeakPtr(), | 214 weak_ptr_factory_.GetWeakPtr(), |
| 188 event.path())), | 215 event.path())), |
| 189 true); | 216 true); |
| 190 } | 217 } |
| 191 break; | 218 break; |
| 192 case DeviceEvent::REMOVE: { | 219 case DeviceEvent::REMOVE: { |
| 193 TRACE_EVENT1("ozone", "OnDeviceRemoved", "path", event.path().value()); | 220 TRACE_EVENT1("ozone", "OnDeviceRemoved", "path", event.path().value()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 MouseEvent mouse_event(ET_MOUSE_MOVED, | 261 MouseEvent mouse_event(ET_MOUSE_MOVED, |
| 235 cursor_->location(), | 262 cursor_->location(), |
| 236 cursor_->location(), | 263 cursor_->location(), |
| 237 modifiers_.GetModifierFlags(), | 264 modifiers_.GetModifierFlags(), |
| 238 /* changed_button_flags */ 0); | 265 /* changed_button_flags */ 0); |
| 239 DispatchEvent(&mouse_event); | 266 DispatchEvent(&mouse_event); |
| 240 } | 267 } |
| 241 } | 268 } |
| 242 | 269 |
| 243 } // namespace ui | 270 } // namespace ui |
| OLD | NEW |