cast_touch_device_manager.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 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. #include "chromecast/browser/cast_touch_device_manager.h"
  5. #include "chromecast/graphics/cast_screen.h"
  6. #include "ui/events/devices/device_data_manager.h"
  7. #include "ui/events/devices/touchscreen_device.h"
  8. #include "ui/gfx/geometry/rect_f.h"
  9. #include "ui/gfx/geometry/size_f.h"
  10. namespace chromecast {
  11. namespace shell {
  12. namespace {
  13. ui::TouchDeviceTransform GetDeviceTransform(
  14. const ui::TouchscreenDevice& touchscreen,
  15. int64_t display_id,
  16. display::Display::Rotation rotation,
  17. const gfx::Rect& native_bounds_in_pixel) {
  18. gfx::SizeF touchscreen_size = gfx::SizeF(touchscreen.size);
  19. ui::TouchDeviceTransform touch_device_transform;
  20. touch_device_transform.display_id = display_id;
  21. touch_device_transform.device_id = touchscreen.id;
  22. touch_device_transform.transform.Translate(native_bounds_in_pixel.x(),
  23. native_bounds_in_pixel.y());
  24. touch_device_transform.transform.Scale(
  25. native_bounds_in_pixel.width() / touchscreen_size.width(),
  26. native_bounds_in_pixel.height() / touchscreen_size.height());
  27. return touch_device_transform;
  28. }
  29. } // namespace
  30. CastTouchDeviceManager::CastTouchDeviceManager() {
  31. ui::DeviceDataManager::GetInstance()->AddObserver(this);
  32. }
  33. CastTouchDeviceManager::~CastTouchDeviceManager() {
  34. ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
  35. }
  36. void CastTouchDeviceManager::OnInputDeviceConfigurationChanged(
  37. uint8_t input_device_types) {
  38. if (input_device_types & ui::InputDeviceEventObserver::kTouchscreen) {
  39. UpdateTouchscreenConfiguration();
  40. }
  41. }
  42. void CastTouchDeviceManager::OnDisplayConfigured(
  43. int64_t display_id,
  44. display::Display::Rotation rotation,
  45. const gfx::Rect& native_bounds_in_pixel) {
  46. display_id_ = display_id;
  47. display_rotation_ = rotation;
  48. native_display_bounds_in_pixel_ = native_bounds_in_pixel;
  49. UpdateTouchscreenConfiguration();
  50. }
  51. void CastTouchDeviceManager::UpdateTouchscreenConfiguration() {
  52. const std::vector<ui::TouchscreenDevice>& touchscreen_devices =
  53. ui::DeviceDataManager::GetInstance()->GetTouchscreenDevices();
  54. if (native_display_bounds_in_pixel_ == gfx::Rect())
  55. return; // Waiting for display configuration.
  56. std::vector<ui::TouchDeviceTransform> touch_device_transforms;
  57. // All touchscreens are mapped onto primary display.
  58. for (const auto& touchscreen : touchscreen_devices) {
  59. touch_device_transforms.push_back(
  60. GetDeviceTransform(touchscreen, display_id_, display_rotation_,
  61. native_display_bounds_in_pixel_));
  62. }
  63. ui::DeviceDataManager::GetInstance()->ConfigureTouchDevices(
  64. touch_device_transforms);
  65. }
  66. } // namespace shell
  67. } // namespace chromecast