| 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/bind.h" | 10 #include "base/bind.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 OpenInputDeviceReplyCallback; | 43 OpenInputDeviceReplyCallback; |
| 44 | 44 |
| 45 struct OpenInputDeviceParams { | 45 struct OpenInputDeviceParams { |
| 46 // Unique identifier for the new device. | 46 // Unique identifier for the new device. |
| 47 int id; | 47 int id; |
| 48 | 48 |
| 49 // Device path to open. | 49 // Device path to open. |
| 50 base::FilePath path; | 50 base::FilePath path; |
| 51 | 51 |
| 52 // Callback for dispatching events. Call on UI thread only. | 52 // Callback for dispatching events. Call on UI thread only. |
| 53 EventDispatchCallback dispatch_callback; | |
| 54 KeyEventDispatchCallback key_callback; | 53 KeyEventDispatchCallback key_callback; |
| 55 MouseMoveEventDispatchCallback mouse_move_callback; | 54 MouseMoveEventDispatchCallback mouse_move_callback; |
| 56 MouseButtonEventDispatchCallback mouse_button_callback; | 55 MouseButtonEventDispatchCallback mouse_button_callback; |
| 56 MouseWheelEventDispatchCallback mouse_wheel_callback; |
| 57 ScrollEventDispatchCallback scroll_callback; |
| 57 TouchEventDispatchCallback touch_callback; | 58 TouchEventDispatchCallback touch_callback; |
| 58 | 59 |
| 59 // State shared between devices. Must not be dereferenced on worker thread. | 60 // State shared between devices. Must not be dereferenced on worker thread. |
| 60 EventModifiersEvdev* modifiers; | |
| 61 CursorDelegateEvdev* cursor; | 61 CursorDelegateEvdev* cursor; |
| 62 #if defined(USE_EVDEV_GESTURES) | 62 #if defined(USE_EVDEV_GESTURES) |
| 63 GesturePropertyProvider* gesture_property_provider; | 63 GesturePropertyProvider* gesture_property_provider; |
| 64 #endif | 64 #endif |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 #if defined(USE_EVDEV_GESTURES) | 67 #if defined(USE_EVDEV_GESTURES) |
| 68 bool UseGesturesLibraryForDevice(const EventDeviceInfo& devinfo) { | 68 bool UseGesturesLibraryForDevice(const EventDeviceInfo& devinfo) { |
| 69 if (devinfo.HasTouchpad()) | 69 if (devinfo.HasTouchpad()) |
| 70 return true; | 70 return true; |
| 71 | 71 |
| 72 if (devinfo.HasRelXY()) | 72 if (devinfo.HasRelXY()) |
| 73 return true; // mouse | 73 return true; // mouse |
| 74 | 74 |
| 75 return false; | 75 return false; |
| 76 } | 76 } |
| 77 #endif | 77 #endif |
| 78 | 78 |
| 79 scoped_ptr<EventConverterEvdev> CreateConverter( | 79 scoped_ptr<EventConverterEvdev> CreateConverter( |
| 80 const OpenInputDeviceParams& params, | 80 const OpenInputDeviceParams& params, |
| 81 int fd, | 81 int fd, |
| 82 InputDeviceType type, | 82 InputDeviceType type, |
| 83 const EventDeviceInfo& devinfo) { | 83 const EventDeviceInfo& devinfo) { |
| 84 #if defined(USE_EVDEV_GESTURES) | 84 #if defined(USE_EVDEV_GESTURES) |
| 85 // Touchpad or mouse: use gestures library. | 85 // Touchpad or mouse: use gestures library. |
| 86 // EventReaderLibevdevCros -> GestureInterpreterLibevdevCros -> DispatchEvent | 86 // EventReaderLibevdevCros -> GestureInterpreterLibevdevCros -> DispatchEvent |
| 87 if (UseGesturesLibraryForDevice(devinfo)) { | 87 if (UseGesturesLibraryForDevice(devinfo)) { |
| 88 scoped_ptr<GestureInterpreterLibevdevCros> gesture_interp = | 88 scoped_ptr<GestureInterpreterLibevdevCros> gesture_interp = |
| 89 make_scoped_ptr(new GestureInterpreterLibevdevCros( | 89 make_scoped_ptr(new GestureInterpreterLibevdevCros( |
| 90 params.id, params.modifiers, params.cursor, | 90 params.id, params.cursor, params.gesture_property_provider, |
| 91 params.gesture_property_provider, params.key_callback, | 91 params.key_callback, params.mouse_move_callback, |
| 92 params.mouse_move_callback, params.mouse_button_callback, | 92 params.mouse_button_callback, params.mouse_wheel_callback, |
| 93 params.dispatch_callback)); | 93 params.scroll_callback)); |
| 94 return make_scoped_ptr(new EventReaderLibevdevCros( | 94 return make_scoped_ptr(new EventReaderLibevdevCros( |
| 95 fd, params.path, params.id, type, devinfo, gesture_interp.Pass())); | 95 fd, params.path, params.id, type, devinfo, gesture_interp.Pass())); |
| 96 } | 96 } |
| 97 #endif | 97 #endif |
| 98 | 98 |
| 99 // Touchscreen: use TouchEventConverterEvdev. | 99 // Touchscreen: use TouchEventConverterEvdev. |
| 100 if (devinfo.HasMTAbsXY()) { | 100 if (devinfo.HasMTAbsXY()) { |
| 101 scoped_ptr<TouchEventConverterEvdev> converter(new TouchEventConverterEvdev( | 101 scoped_ptr<TouchEventConverterEvdev> converter(new TouchEventConverterEvdev( |
| 102 fd, params.path, params.id, type, params.touch_callback)); | 102 fd, params.path, params.id, type, params.touch_callback)); |
| 103 converter->Initialize(devinfo); | 103 converter->Initialize(devinfo); |
| 104 return converter.Pass(); | 104 return converter.Pass(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Graphics tablet | 107 // Graphics tablet |
| 108 if (devinfo.HasAbsXY()) | 108 if (devinfo.HasAbsXY()) |
| 109 return make_scoped_ptr<EventConverterEvdev>(new TabletEventConverterEvdev( | 109 return make_scoped_ptr<EventConverterEvdev>(new TabletEventConverterEvdev( |
| 110 fd, params.path, params.id, type, params.modifiers, params.cursor, | 110 fd, params.path, params.id, type, params.cursor, devinfo, |
| 111 devinfo, params.dispatch_callback)); | 111 params.mouse_move_callback, params.mouse_button_callback)); |
| 112 | 112 |
| 113 // Everything else: use EventConverterEvdevImpl. | 113 // Everything else: use EventConverterEvdevImpl. |
| 114 return make_scoped_ptr<EventConverterEvdevImpl>(new EventConverterEvdevImpl( | 114 return make_scoped_ptr<EventConverterEvdevImpl>(new EventConverterEvdevImpl( |
| 115 fd, params.path, params.id, type, devinfo, params.modifiers, | 115 fd, params.path, params.id, type, devinfo, params.cursor, |
| 116 params.cursor, params.key_callback, params.mouse_move_callback, | 116 params.key_callback, params.mouse_move_callback, |
| 117 params.mouse_button_callback)); | 117 params.mouse_button_callback)); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Open an input device. Opening may put the calling thread to sleep, and | 120 // Open an input device. Opening may put the calling thread to sleep, and |
| 121 // therefore should be run on a thread where latency is not critical. We | 121 // therefore should be run on a thread where latency is not critical. We |
| 122 // run it on a thread from the worker pool. | 122 // run it on a thread from the worker pool. |
| 123 // | 123 // |
| 124 // This takes a TaskRunner and runs the reply on that thread, so that we | 124 // This takes a TaskRunner and runs the reply on that thread, so that we |
| 125 // can hop threads if necessary (back to the UI thread). | 125 // can hop threads if necessary (back to the UI thread). |
| 126 void OpenInputDevice(scoped_ptr<OpenInputDeviceParams> params, | 126 void OpenInputDevice(scoped_ptr<OpenInputDeviceParams> params, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 modifiers_.UpdateModifier(modifier, params.down); | 241 modifiers_.UpdateModifier(modifier, params.down); |
| 242 | 242 |
| 243 scoped_ptr<MouseEvent> event(new MouseEvent( | 243 scoped_ptr<MouseEvent> event(new MouseEvent( |
| 244 params.down ? ui::ET_MOUSE_PRESSED : ui::ET_MOUSE_RELEASED, | 244 params.down ? ui::ET_MOUSE_PRESSED : ui::ET_MOUSE_RELEASED, |
| 245 params.location, params.location, modifiers_.GetModifierFlags() | flag, | 245 params.location, params.location, modifiers_.GetModifierFlags() | flag, |
| 246 /* changed_button_flags */ flag)); | 246 /* changed_button_flags */ flag)); |
| 247 event->set_source_device_id(params.device_id); | 247 event->set_source_device_id(params.device_id); |
| 248 PostUiEvent(event.Pass()); | 248 PostUiEvent(event.Pass()); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void EventFactoryEvdev::PostMouseWheelEvent( |
| 252 const MouseWheelEventParams& params) { |
| 253 scoped_ptr<MouseWheelEvent> event(new MouseWheelEvent( |
| 254 params.delta, params.location, params.location, |
| 255 modifiers_.GetModifierFlags(), 0 /* changed_button_flags */)); |
| 256 event->set_source_device_id(params.device_id); |
| 257 PostUiEvent(event.Pass()); |
| 258 } |
| 259 |
| 260 void EventFactoryEvdev::PostScrollEvent(const ScrollEventParams& params) { |
| 261 scoped_ptr<ScrollEvent> event(new ScrollEvent( |
| 262 params.type, params.location, params.timestamp, |
| 263 modifiers_.GetModifierFlags(), params.delta.x(), params.delta.y(), |
| 264 params.ordinal_delta.x(), params.ordinal_delta.y(), params.finger_count)); |
| 265 event->set_source_device_id(params.device_id); |
| 266 PostUiEvent(event.Pass()); |
| 267 } |
| 268 |
| 251 void EventFactoryEvdev::PostTouchEvent(const TouchEventParams& params) { | 269 void EventFactoryEvdev::PostTouchEvent(const TouchEventParams& params) { |
| 252 float x = params.location.x(); | 270 float x = params.location.x(); |
| 253 float y = params.location.y(); | 271 float y = params.location.y(); |
| 254 double radius_x = params.radii.x(); | 272 double radius_x = params.radii.x(); |
| 255 double radius_y = params.radii.y(); | 273 double radius_y = params.radii.y(); |
| 256 | 274 |
| 257 // Transform the event to align touches to the image based on display mode. | 275 // Transform the event to align touches to the image based on display mode. |
| 258 DeviceDataManager::GetInstance()->ApplyTouchTransformer(params.device_id, &x, | 276 DeviceDataManager::GetInstance()->ApplyTouchTransformer(params.device_id, &x, |
| 259 &y); | 277 &y); |
| 260 DeviceDataManager::GetInstance()->ApplyTouchRadiusScale(params.device_id, | 278 DeviceDataManager::GetInstance()->ApplyTouchRadiusScale(params.device_id, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 return; | 324 return; |
| 307 | 325 |
| 308 switch (event.action_type()) { | 326 switch (event.action_type()) { |
| 309 case DeviceEvent::ADD: | 327 case DeviceEvent::ADD: |
| 310 case DeviceEvent::CHANGE: { | 328 case DeviceEvent::CHANGE: { |
| 311 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event.path().value()); | 329 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event.path().value()); |
| 312 | 330 |
| 313 scoped_ptr<OpenInputDeviceParams> params(new OpenInputDeviceParams); | 331 scoped_ptr<OpenInputDeviceParams> params(new OpenInputDeviceParams); |
| 314 params->id = NextDeviceId(); | 332 params->id = NextDeviceId(); |
| 315 params->path = event.path(); | 333 params->path = event.path(); |
| 316 params->dispatch_callback = dispatch_callback_; | |
| 317 params->modifiers = &modifiers_; | |
| 318 params->cursor = cursor_; | 334 params->cursor = cursor_; |
| 319 params->key_callback = base::Bind(&EventFactoryEvdev::PostKeyEvent, | 335 params->key_callback = base::Bind(&EventFactoryEvdev::PostKeyEvent, |
| 320 weak_ptr_factory_.GetWeakPtr()); | 336 weak_ptr_factory_.GetWeakPtr()); |
| 321 params->mouse_move_callback = | 337 params->mouse_move_callback = |
| 322 base::Bind(&EventFactoryEvdev::PostMouseMoveEvent, | 338 base::Bind(&EventFactoryEvdev::PostMouseMoveEvent, |
| 323 weak_ptr_factory_.GetWeakPtr()); | 339 weak_ptr_factory_.GetWeakPtr()); |
| 324 params->mouse_button_callback = | 340 params->mouse_button_callback = |
| 325 base::Bind(&EventFactoryEvdev::PostMouseButtonEvent, | 341 base::Bind(&EventFactoryEvdev::PostMouseButtonEvent, |
| 326 weak_ptr_factory_.GetWeakPtr()); | 342 weak_ptr_factory_.GetWeakPtr()); |
| 343 params->mouse_wheel_callback = |
| 344 base::Bind(&EventFactoryEvdev::PostMouseWheelEvent, |
| 345 weak_ptr_factory_.GetWeakPtr()); |
| 346 params->scroll_callback = base::Bind(&EventFactoryEvdev::PostScrollEvent, |
| 347 weak_ptr_factory_.GetWeakPtr()); |
| 327 params->touch_callback = base::Bind(&EventFactoryEvdev::PostTouchEvent, | 348 params->touch_callback = base::Bind(&EventFactoryEvdev::PostTouchEvent, |
| 328 weak_ptr_factory_.GetWeakPtr()); | 349 weak_ptr_factory_.GetWeakPtr()); |
| 329 | 350 |
| 330 #if defined(USE_EVDEV_GESTURES) | 351 #if defined(USE_EVDEV_GESTURES) |
| 331 params->gesture_property_provider = gesture_property_provider_.get(); | 352 params->gesture_property_provider = gesture_property_provider_.get(); |
| 332 #endif | 353 #endif |
| 333 | 354 |
| 334 OpenInputDeviceReplyCallback reply_callback = | 355 OpenInputDeviceReplyCallback reply_callback = |
| 335 base::Bind(&EventFactoryEvdev::AttachInputDevice, | 356 base::Bind(&EventFactoryEvdev::AttachInputDevice, |
| 336 weak_ptr_factory_.GetWeakPtr()); | 357 weak_ptr_factory_.GetWeakPtr()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 gesture_property_provider_->GetDeviceIdsByType(type, &ids); | 514 gesture_property_provider_->GetDeviceIdsByType(type, &ids); |
| 494 #endif | 515 #endif |
| 495 // In the future we can add other device matching logics here. | 516 // In the future we can add other device matching logics here. |
| 496 | 517 |
| 497 if (device_ids) | 518 if (device_ids) |
| 498 device_ids->assign(ids.begin(), ids.end()); | 519 device_ids->assign(ids.begin(), ids.end()); |
| 499 return !ids.empty(); | 520 return !ids.empty(); |
| 500 } | 521 } |
| 501 | 522 |
| 502 } // namespace ui | 523 } // namespace ui |
| OLD | NEW |