| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/ui/ws/touch_controller.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "services/ui/ws/display.h" | |
| 12 #include "services/ui/ws/display_manager.h" | |
| 13 #include "ui/events/devices/device_data_manager.h" | |
| 14 #include "ui/events/devices/touchscreen_device.h" | |
| 15 #include "ui/gfx/geometry/size.h" | |
| 16 #include "ui/gfx/transform.h" | |
| 17 | |
| 18 namespace ui { | |
| 19 namespace ws { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Computes the scale ratio for the TouchEvent's radius. | |
| 24 double ComputeTouchResolutionScale(const Display* touch_display, | |
| 25 const ui::TouchscreenDevice& touch_device) { | |
| 26 gfx::Size touch_display_size = touch_display->GetSize(); | |
| 27 | |
| 28 if (touch_device.size.IsEmpty() || touch_display_size.IsEmpty()) | |
| 29 return 1.0; | |
| 30 | |
| 31 double display_area = touch_display_size.GetArea(); | |
| 32 double touch_area = touch_device.size.GetArea(); | |
| 33 double ratio = std::sqrt(display_area / touch_area); | |
| 34 | |
| 35 return ratio; | |
| 36 } | |
| 37 | |
| 38 // Computes a touch transform that maps from window bounds to touchscreen size. | |
| 39 // Assumes scale factor of 1.0. | |
| 40 gfx::Transform ComputeTouchTransform( | |
| 41 const Display* touch_display, | |
| 42 const ui::TouchscreenDevice& touch_device) { | |
| 43 gfx::Size touch_display_size = touch_display->GetSize(); | |
| 44 | |
| 45 gfx::SizeF current_size(touch_display_size); | |
| 46 gfx::SizeF touch_area(touch_device.size); | |
| 47 gfx::Transform transform; | |
| 48 | |
| 49 if (current_size.IsEmpty() || touch_area.IsEmpty()) | |
| 50 return transform; | |
| 51 | |
| 52 // Take care of scaling between touchscreen area and display resolution. | |
| 53 transform.Scale(current_size.width() / touch_area.width(), | |
| 54 current_size.height() / touch_area.height()); | |
| 55 return transform; | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 TouchController::TouchController(DisplayManager* display_manager) | |
| 61 : display_manager_(display_manager) { | |
| 62 DCHECK(display_manager_); | |
| 63 ui::DeviceDataManager::GetInstance()->AddObserver(this); | |
| 64 } | |
| 65 | |
| 66 TouchController::~TouchController() { | |
| 67 ui::DeviceDataManager::GetInstance()->RemoveObserver(this); | |
| 68 } | |
| 69 | |
| 70 void TouchController::UpdateTouchTransforms() const { | |
| 71 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance(); | |
| 72 device_manager->ClearTouchDeviceAssociations(); | |
| 73 | |
| 74 const std::set<Display*>& touch_displays = display_manager_->displays(); | |
| 75 const std::vector<ui::TouchscreenDevice>& touch_devices = | |
| 76 device_manager->GetTouchscreenDevices(); | |
| 77 | |
| 78 // Mash can only handle a single display so this doesn't implement support for | |
| 79 // matching touchscreens with multiple displays. | |
| 80 // TODO(kylechar): Implement support for multiple displays when needed. | |
| 81 if (touch_displays.size() == 1 && touch_devices.size() == 1) { | |
| 82 const Display* touch_display = *touch_displays.begin(); | |
| 83 const ui::TouchscreenDevice& touch_device = touch_devices[0]; | |
| 84 | |
| 85 int64_t touch_display_id = touch_display->GetId(); | |
| 86 int touch_device_id = touch_device.id; | |
| 87 | |
| 88 if (touch_device_id != ui::InputDevice::kInvalidId) { | |
| 89 device_manager->UpdateTouchRadiusScale( | |
| 90 touch_device_id, | |
| 91 ComputeTouchResolutionScale(touch_display, touch_device)); | |
| 92 | |
| 93 device_manager->UpdateTouchInfoForDisplay( | |
| 94 touch_display_id, touch_device_id, | |
| 95 ComputeTouchTransform(touch_display, touch_device)); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void TouchController::OnTouchscreenDeviceConfigurationChanged() { | |
| 101 UpdateTouchTransforms(); | |
| 102 } | |
| 103 | |
| 104 } // namespace ws | |
| 105 } // namespace ui | |
| OLD | NEW |