event_transformation_handler.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/shell.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" 18 19#if defined(OS_CHROMEOS) 20#include "ui/display/chromeos/output_configurator.h" 21#endif // defined(OS_CHROMEOS) 22 23namespace ash { 24namespace internal { 25namespace { 26 27// Boost factor for non-integrated displays. 28const float kBoostForNonIntegrated = 1.20f; 29} 30 31EventTransformationHandler::EventTransformationHandler() 32 : transformation_mode_(TRANSFORM_AUTO) { 33} 34 35EventTransformationHandler::~EventTransformationHandler() { 36} 37 38void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) { 39 if (transformation_mode_ == TRANSFORM_NONE) 40 return; 41 42 // It is unnecessary to scale the event for the device scale factor since 43 // the event locations etc. are already in DIP. 44 gfx::Point point_in_screen(event->location()); 45 aura::Window* target = static_cast<aura::Window*>(event->target()); 46 wm::ConvertPointToScreen(target, &point_in_screen); 47 const gfx::Display& display = 48 Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen); 49 50 // Apply some additional scaling if the display is non-integrated. 51 if (!display.IsInternal()) 52 event->Scale(kBoostForNonIntegrated); 53} 54 55#if defined(OS_CHROMEOS) 56// This is to scale the TouchEvent's radius when the touch display is in 57// mirror mode. TouchEvent's radius is often reported in the touchscreen's 58// native resolution. In mirror mode, the touch display could be configured 59// at a lower resolution. We scale down the radius using the ratio defined as 60// the sqrt of 61// (mirror_width * mirror_height) / (native_width * native_height) 62void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) { 63 using ui::OutputConfigurator; 64 OutputConfigurator* output_configurator = 65 ash::Shell::GetInstance()->output_configurator(); 66 67 // Check output_configurator's output_state instead of checking 68 // DisplayManager::IsMirrored() because the compositor based mirroring 69 // won't cause the scaling issue. 70 if (output_configurator->output_state() != ui::OUTPUT_STATE_DUAL_MIRROR) 71 return; 72 73 const std::map<int, float>& area_ratio_map = 74 output_configurator->GetMirroredDisplayAreaRatioMap(); 75 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"; 84 return; 85 } 86 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); 90} 91#endif // defined(OS_CHROMEOS) 92 93} // namespace internal 94} // namespace ash 95