mouse_input_filter.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2012 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 "remoting/protocol/mouse_input_filter.h"
  5. #include <algorithm>
  6. #include "base/cxx17_backports.h"
  7. #include "base/logging.h"
  8. #include "remoting/base/logging.h"
  9. #include "remoting/proto/event.pb.h"
  10. namespace remoting {
  11. namespace protocol {
  12. MouseInputFilter::MouseInputFilter() = default;
  13. MouseInputFilter::MouseInputFilter(InputStub* input_stub)
  14. : InputFilter(input_stub) {}
  15. MouseInputFilter::~MouseInputFilter() = default;
  16. void MouseInputFilter::InjectMouseEvent(const MouseEvent& event) {
  17. if (input_bounds_.is_zero() || output_bounds_.is_zero()) {
  18. return;
  19. }
  20. // We scale based on the max input and output coordinates (which are equal
  21. // to size-1), rather than the input and output sizes, so that it's possible
  22. // to reach the edge of the output when up-scaling. We also take care to
  23. // round up or down correctly, which is important when down-scaling.
  24. // After scaling, we offset by the output rect origin. This is normally
  25. // (0,0), but may be non-zero when there are multiple displays and we are
  26. // showing a single display.
  27. MouseEvent out_event(event);
  28. if (out_event.has_x()) {
  29. out_event.set_x(output_offset_.x() + GetScaledX(out_event.x()));
  30. }
  31. if (out_event.has_y()) {
  32. out_event.set_y(output_offset_.y() + GetScaledY(out_event.y()));
  33. }
  34. InputFilter::InjectMouseEvent(out_event);
  35. }
  36. void MouseInputFilter::set_input_size(const int32_t x, const int32_t y) {
  37. input_bounds_ = webrtc::DesktopVector(std::max(x - 1, 0), std::max(y - 1, 0));
  38. HOST_LOG << "Setting MouseInputFilter input boundary to " << input_bounds_.x()
  39. << "," << input_bounds_.y();
  40. }
  41. void MouseInputFilter::set_output_size(const int32_t x, const int32_t y) {
  42. output_bounds_ =
  43. webrtc::DesktopVector(std::max(x - 1, 0), std::max(y - 1, 0));
  44. HOST_LOG << "Setting MouseInputFilter output boundary to "
  45. << output_bounds_.x() << "," << output_bounds_.y();
  46. }
  47. void MouseInputFilter::set_output_offset(const webrtc::DesktopVector& v) {
  48. output_offset_ = webrtc::DesktopVector(v.x(), v.y());
  49. HOST_LOG << "Setting MouseInputFilter output_offset to " << output_offset_.x()
  50. << "," << output_offset_.y();
  51. }
  52. int32_t MouseInputFilter::GetScaledX(int32_t x) {
  53. if (output_bounds_.x() != input_bounds_.x()) {
  54. x = ((x * output_bounds_.x()) + (input_bounds_.x() / 2)) /
  55. input_bounds_.x();
  56. }
  57. return base::clamp(x, 0, output_bounds_.x());
  58. }
  59. int32_t MouseInputFilter::GetScaledY(int32_t y) {
  60. if (output_bounds_.y() != input_bounds_.y()) {
  61. y = ((y * output_bounds_.y()) + (input_bounds_.y() / 2)) /
  62. input_bounds_.y();
  63. }
  64. return base::clamp(y, 0, output_bounds_.y());
  65. }
  66. } // namespace protocol
  67. } // namespace remoting