| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ash/display/event_transformation_handler.h" | 5 #include "ash/display/event_transformation_handler.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "ash/display/display_info.h" | 9 #include "ash/display/display_info.h" |
| 10 #include "ash/display/display_manager.h" | 10 #include "ash/display/display_manager.h" |
| 11 #include "ash/shell.h" | 11 #include "ash/shell.h" |
| 12 #include "ash/wm/coordinate_conversion.h" | 12 #include "ash/wm/coordinate_conversion.h" |
| 13 #include "ash/wm/window_util.h" | 13 #include "ash/wm/window_util.h" |
| 14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
| 15 #include "ui/aura/window_event_dispatcher.h" | 15 #include "ui/aura/window_event_dispatcher.h" |
| 16 #include "ui/compositor/dip_util.h" | 16 #include "ui/compositor/dip_util.h" |
| 17 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
| 18 #include "ui/gfx/display.h" | 18 #include "ui/gfx/display.h" |
| 19 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
| 20 | 20 |
| 21 #if defined(OS_CHROMEOS) | |
| 22 #include "ui/display/chromeos/display_configurator.h" | |
| 23 #endif // defined(OS_CHROMEOS) | |
| 24 | |
| 25 namespace ash { | 21 namespace ash { |
| 26 namespace { | 22 namespace { |
| 27 | 23 |
| 28 // Boost factor for non-integrated displays. | 24 // Boost factor for non-integrated displays. |
| 29 const float kBoostForNonIntegrated = 1.20f; | 25 const float kBoostForNonIntegrated = 1.20f; |
| 30 | 26 |
| 31 #if defined(OS_CHROMEOS) | |
| 32 gfx::Size GetNativeModeSize(const DisplayInfo& info) { | |
| 33 const std::vector<DisplayMode>& modes = info.display_modes(); | |
| 34 for (size_t i = 0; i < modes.size(); ++i) { | |
| 35 if (modes[i].native) | |
| 36 return modes[i].size; | |
| 37 } | |
| 38 | |
| 39 return gfx::Size(); | |
| 40 } | |
| 41 | |
| 42 float GetMirroredDisplayAreaRatio(const gfx::Size& current_size, | |
| 43 const gfx::Size& native_size) { | |
| 44 float area_ratio = 1.0f; | |
| 45 | |
| 46 if (current_size.IsEmpty() || native_size.IsEmpty()) | |
| 47 return area_ratio; | |
| 48 | |
| 49 float width_ratio = static_cast<float>(current_size.width()) / | |
| 50 static_cast<float>(native_size.width()); | |
| 51 float height_ratio = static_cast<float>(current_size.height()) / | |
| 52 static_cast<float>(native_size.height()); | |
| 53 | |
| 54 area_ratio = width_ratio * height_ratio; | |
| 55 return area_ratio; | |
| 56 } | |
| 57 | |
| 58 // Key of the map is the touch display's id, and the value of the map is the | |
| 59 // touch display's area ratio in mirror mode defined as: | |
| 60 // mirror_mode_area / native_mode_area. | |
| 61 // This is used for scaling touch event's radius when the touch display is in | |
| 62 // mirror mode: new_touch_radius = sqrt(area_ratio) * old_touch_radius | |
| 63 std::map<int, float> GetMirroredDisplayAreaRatioMap() { | |
| 64 std::map<int, float> area_ratios; | |
| 65 DisplayManager* display_manager = | |
| 66 ash::Shell::GetInstance()->display_manager(); | |
| 67 const std::vector<gfx::Display>& displays = display_manager->displays(); | |
| 68 for (size_t i = 0; i < displays.size(); ++i) { | |
| 69 const DisplayInfo& info = display_manager->GetDisplayInfo( | |
| 70 displays[i].id()); | |
| 71 const gfx::Size current_size = info.size_in_pixel(); | |
| 72 const gfx::Size native_size = GetNativeModeSize(info); | |
| 73 | |
| 74 if (info.touch_support() == gfx::Display::TOUCH_SUPPORT_AVAILABLE && | |
| 75 current_size != native_size && | |
| 76 info.is_aspect_preserving_scaling()) { | |
| 77 area_ratios[info.touch_device_id()] = GetMirroredDisplayAreaRatio( | |
| 78 current_size, native_size); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 return area_ratios; | |
| 83 } | |
| 84 #endif // defined(OS_CHROMEOS) | |
| 85 | |
| 86 } // namespace | 27 } // namespace |
| 87 | 28 |
| 88 EventTransformationHandler::EventTransformationHandler() | 29 EventTransformationHandler::EventTransformationHandler() |
| 89 : transformation_mode_(TRANSFORM_AUTO) { | 30 : transformation_mode_(TRANSFORM_AUTO) { |
| 90 } | 31 } |
| 91 | 32 |
| 92 EventTransformationHandler::~EventTransformationHandler() { | 33 EventTransformationHandler::~EventTransformationHandler() { |
| 93 } | 34 } |
| 94 | 35 |
| 95 void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) { | 36 void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) { |
| 96 if (transformation_mode_ == TRANSFORM_NONE) | 37 if (transformation_mode_ == TRANSFORM_NONE) |
| 97 return; | 38 return; |
| 98 | 39 |
| 99 // It is unnecessary to scale the event for the device scale factor since | 40 // It is unnecessary to scale the event for the device scale factor since |
| 100 // the event locations etc. are already in DIP. | 41 // the event locations etc. are already in DIP. |
| 101 gfx::Point point_in_screen(event->location()); | 42 gfx::Point point_in_screen(event->location()); |
| 102 aura::Window* target = static_cast<aura::Window*>(event->target()); | 43 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 103 wm::ConvertPointToScreen(target, &point_in_screen); | 44 wm::ConvertPointToScreen(target, &point_in_screen); |
| 104 const gfx::Display& display = | 45 const gfx::Display& display = |
| 105 Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen); | 46 Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen); |
| 106 | 47 |
| 107 // Apply some additional scaling if the display is non-integrated. | 48 // Apply some additional scaling if the display is non-integrated. |
| 108 if (!display.IsInternal()) | 49 if (!display.IsInternal()) |
| 109 event->Scale(kBoostForNonIntegrated); | 50 event->Scale(kBoostForNonIntegrated); |
| 110 } | 51 } |
| 111 | 52 |
| 112 #if defined(OS_CHROMEOS) | |
| 113 // This is to scale the TouchEvent's radius when the touch display is in | |
| 114 // mirror mode. TouchEvent's radius is often reported in the touchscreen's | |
| 115 // native resolution. In mirror mode, the touch display could be configured | |
| 116 // at a lower resolution. We scale down the radius using the ratio defined as | |
| 117 // the sqrt of | |
| 118 // (mirror_width * mirror_height) / (native_width * native_height) | |
| 119 void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) { | |
| 120 // Check display_configurator's output_state instead of checking | |
| 121 // DisplayManager::IsMirrored() because the compositor based mirroring | |
| 122 // won't cause the scaling issue. | |
| 123 if (ash::Shell::GetInstance()->display_configurator()->display_state() != | |
| 124 ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR) | |
| 125 return; | |
| 126 | |
| 127 const std::map<int, float> area_ratio_map = GetMirroredDisplayAreaRatioMap(); | |
| 128 | |
| 129 // TODO(miletus): When there are more than 1 touchscreen (e.g. Link connected | |
| 130 // to an external touchscreen), the correct way to do is to have a way | |
| 131 // to find out which touchscreen is the event originating from and use the | |
| 132 // area ratio of that touchscreen to scale the event's radius. | |
| 133 // Tracked here crbug.com/233245 | |
| 134 if (area_ratio_map.size() != 1) { | |
| 135 LOG(ERROR) << "Mirroring mode with " << area_ratio_map.size() | |
| 136 << " touch display found"; | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 float area_ratio_sqrt = std::sqrt(area_ratio_map.begin()->second); | |
| 141 event->set_radius_x(event->radius_x() * area_ratio_sqrt); | |
| 142 event->set_radius_y(event->radius_y() * area_ratio_sqrt); | |
| 143 } | |
| 144 #endif // defined(OS_CHROMEOS) | |
| 145 | |
| 146 } // namespace ash | 53 } // namespace ash |
| OLD | NEW |