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.
4
5#include "ash/display/event_transformation_handler.h"
6
7#include <cmath>
8
9#include "ash/screen_ash.h"
10#include "ash/shell.h"
11#include "ash/wm/coordinate_conversion.h"
12#include "ash/wm/window_util.h"
13#include "ui/aura/root_window.h"
14#include "ui/aura/window.h"
15#include "ui/base/events/event.h"
16#include "ui/compositor/dip_util.h"
17#include "ui/gfx/display.h"
18#include "ui/gfx/screen.h"
19
20#if defined(OS_CHROMEOS)
21#include "chromeos/display/output_configurator.h"
22#endif  // defined(OS_CHROMEOS)
23
24namespace ash {
25namespace internal {
26namespace {
27
28// Boost factor for non-integrated displays.
29const float kBoostForNonIntegrated = 1.20f;
30}
31
32EventTransformationHandler::EventTransformationHandler()
33    : transformation_mode_(TRANSFORM_AUTO) {
34}
35
36EventTransformationHandler::~EventTransformationHandler() {
37}
38
39void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) {
40  if (transformation_mode_ == TRANSFORM_NONE)
41    return;
42
43  // Get the device scale factor and stack it on the final scale factor.
44  gfx::Point point_in_screen(event->location());
45  aura::Window* target = static_cast<aura::Window*>(event->target());
46  const float scale_at_target = ui::GetDeviceScaleFactor(target->layer());
47  float scale = scale_at_target;
48
49  // Apply some additional scaling if the display is non-integrated.
50  wm::ConvertPointToScreen(target, &point_in_screen);
51  const gfx::Display& display =
52      Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen);
53  if (!display.IsInternal())
54    scale *= kBoostForNonIntegrated;
55
56  event->Scale(scale);
57}
58
59#if defined(OS_CHROMEOS)
60// This is to scale the TouchEvent's radius when the touch display is in
61// mirror mode. TouchEvent's radius is often reported in the touchscreen's
62// native resolution. In mirror mode, the touch display could be configured
63// at a lower resolution. We scale down the radius using the ratio defined as
64// the sqrt of
65// (mirror_width * mirror_height) / (native_width * native_height)
66void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) {
67  using chromeos::OutputConfigurator;
68  OutputConfigurator* output_configurator =
69      ash::Shell::GetInstance()->output_configurator();
70
71  // Check output_configurator's output_state instead of checking
72  // DisplayManager::IsMirrored() because the compositor based mirroring
73  // won't cause the scaling issue.
74  if (output_configurator->output_state() != chromeos::STATE_DUAL_MIRROR)
75    return;
76
77  const std::map<int, float>& area_ratio_map =
78      output_configurator->GetMirroredDisplayAreaRatioMap();
79
80  // TODO(miletus): When there are more than 1 touchscreen (e.g. Link connected
81  // to an external touchscreen), the correct way to do is to have a way
82  // to find out which touchscreen is the event originating from and use the
83  // area ratio of that touchscreen to scale the event's radius.
84  // Tracked here crbug.com/233245
85  if (area_ratio_map.size() != 1) {
86    LOG(ERROR) << "Mirroring mode with " << area_ratio_map.size()
87               << " touch display found";
88    return;
89  }
90
91  float area_ratio_sqrt = std::sqrt(area_ratio_map.begin()->second);
92  event->set_radius_x(event->radius_x() * area_ratio_sqrt);
93  event->set_radius_y(event->radius_y() * area_ratio_sqrt);
94}
95#endif  // defined(OS_CHROMEOS)
96
97}  // namespace internal
98}  // namespace ash
99