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
5#include "remoting/protocol/mouse_input_filter.h"
6
7#include "remoting/proto/event.pb.h"
8
9namespace remoting {
10namespace protocol {
11
12MouseInputFilter::MouseInputFilter() {
13}
14
15MouseInputFilter::MouseInputFilter(InputStub* input_stub)
16    : InputFilter(input_stub) {
17}
18
19MouseInputFilter::~MouseInputFilter() {
20}
21
22void MouseInputFilter::InjectMouseEvent(const MouseEvent& event) {
23  if (input_max_.is_empty() || output_max_.is_empty())
24    return;
25
26  // We scale based on the maximum input & output coordinates, rather than the
27  // input and output sizes, so that it's possible to reach the edge of the
28  // output when up-scaling.  We also take care to round up or down correctly,
29  // which is important when down-scaling.
30  MouseEvent out_event(event);
31  if (out_event.has_x()) {
32    int x = out_event.x() * output_max_.width();
33    x = (x + input_max_.width() / 2) / input_max_.width();
34    out_event.set_x(std::max(0, std::min(output_max_.width(), x)));
35  }
36  if (out_event.has_y()) {
37    int y = out_event.y() * output_max_.height();
38    y = (y + input_max_.height() / 2) / input_max_.height();
39    out_event.set_y(std::max(0, std::min(output_max_.height(), y)));
40  }
41
42  InputFilter::InjectMouseEvent(out_event);
43}
44
45void MouseInputFilter::set_input_size(const webrtc::DesktopSize& size) {
46  input_max_.set(size.width() - 1, size.height() - 1);
47}
48
49void MouseInputFilter::set_output_size(const webrtc::DesktopSize& size) {
50  output_max_.set(size.width() - 1, size.height() - 1);
51}
52
53}  // namespace protocol
54}  // namespace remoting
55