event_transformation_handler.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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/compositor/dip_util.h"
16#include "ui/events/event.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  // It is unnecessary to scale the event for the device scale factor since
44  // the event locations etc. are already in DIP.
45  gfx::Point point_in_screen(event->location());
46  aura::Window* target = static_cast<aura::Window*>(event->target());
47  wm::ConvertPointToScreen(target, &point_in_screen);
48  const gfx::Display& display =
49      Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen);
50
51  // Apply some additional scaling if the display is non-integrated.
52  if (!display.IsInternal())
53    event->Scale(kBoostForNonIntegrated);
54}
55
56#if defined(OS_CHROMEOS)
57// This is to scale the TouchEvent's radius when the touch display is in
58// mirror mode. TouchEvent's radius is often reported in the touchscreen's
59// native resolution. In mirror mode, the touch display could be configured
60// at a lower resolution. We scale down the radius using the ratio defined as
61// the sqrt of
62// (mirror_width * mirror_height) / (native_width * native_height)
63void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) {
64  using chromeos::OutputConfigurator;
65  OutputConfigurator* output_configurator =
66      ash::Shell::GetInstance()->output_configurator();
67
68  // Check output_configurator's output_state instead of checking
69  // DisplayManager::IsMirrored() because the compositor based mirroring
70  // won't cause the scaling issue.
71  if (output_configurator->output_state() != chromeos::STATE_DUAL_MIRROR)
72    return;
73
74  const std::map<int, float>& area_ratio_map =
75      output_configurator->GetMirroredDisplayAreaRatioMap();
76
77  // TODO(miletus): When there are more than 1 touchscreen (e.g. Link connected
78  // to an external touchscreen), the correct way to do is to have a way
79  // to find out which touchscreen is the event originating from and use the
80  // area ratio of that touchscreen to scale the event's radius.
81  // Tracked here crbug.com/233245
82  if (area_ratio_map.size() != 1) {
83    LOG(ERROR) << "Mirroring mode with " << area_ratio_map.size()
84               << " touch display found";
85    return;
86  }
87
88  float area_ratio_sqrt = std::sqrt(area_ratio_map.begin()->second);
89  event->set_radius_x(event->radius_x() * area_ratio_sqrt);
90  event->set_radius_y(event->radius_y() * area_ratio_sqrt);
91}
92#endif  // defined(OS_CHROMEOS)
93
94}  // namespace internal
95}  // namespace ash
96