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
3 // found in the LICENSE file.
5 #include "ash/display/event_transformation_handler.h"
10 #include "ash/wm/coordinate_conversion.h"
11 #include "ash/wm/window_util.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/compositor/dip_util.h"
15 #include "ui/events/event.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/screen.h"
19 #if defined(OS_CHROMEOS)
20 #include "ui/display/chromeos/display_configurator.h"
21 #endif // defined(OS_CHROMEOS)
26 // Boost factor for non-integrated displays.
27 const float kBoostForNonIntegrated = 1.20f;
30 EventTransformationHandler::EventTransformationHandler()
31 : transformation_mode_(TRANSFORM_AUTO) {
34 EventTransformationHandler::~EventTransformationHandler() {
37 void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) {
38 if (transformation_mode_ == TRANSFORM_NONE)
41 // It is unnecessary to scale the event for the device scale factor since
42 // the event locations etc. are already in DIP.
43 gfx::Point point_in_screen(event->location());
44 aura::Window* target = static_cast<aura::Window*>(event->target());
45 wm::ConvertPointToScreen(target, &point_in_screen);
46 const gfx::Display& display =
47 Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen);
49 // Apply some additional scaling if the display is non-integrated.
50 if (!display.IsInternal())
51 event->Scale(kBoostForNonIntegrated);
54 #if defined(OS_CHROMEOS)
55 // This is to scale the TouchEvent's radius when the touch display is in
56 // mirror mode. TouchEvent's radius is often reported in the touchscreen's
57 // native resolution. In mirror mode, the touch display could be configured
58 // at a lower resolution. We scale down the radius using the ratio defined as
60 // (mirror_width * mirror_height) / (native_width * native_height)
61 void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) {
62 using ui::DisplayConfigurator;
63 DisplayConfigurator* display_configurator =
64 ash::Shell::GetInstance()->display_configurator();
66 // Check display_configurator's output_state instead of checking
67 // DisplayManager::IsMirrored() because the compositor based mirroring
68 // won't cause the scaling issue.
69 if (display_configurator->display_state() !=
70 ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR)
73 const std::map<int, float>& area_ratio_map =
74 display_configurator->GetMirroredDisplayAreaRatioMap();
76 // TODO(miletus): When there are more than 1 touchscreen (e.g. Link connected
77 // to an external touchscreen), the correct way to do is to have a way
78 // to find out which touchscreen is the event originating from and use the
79 // area ratio of that touchscreen to scale the event's radius.
80 // Tracked here crbug.com/233245
81 if (area_ratio_map.size() != 1) {
82 LOG(ERROR) << "Mirroring mode with " << area_ratio_map.size()
83 << " touch display found";
87 float area_ratio_sqrt = std::sqrt(area_ratio_map.begin()->second);
88 event->set_radius_x(event->radius_x() * area_ratio_sqrt);
89 event->set_radius_y(event->radius_y() * area_ratio_sqrt);
91 #endif // defined(OS_CHROMEOS)