| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/remote_input_filter.h" | 5 #include "remoting/host/remote_input_filter.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "remoting/proto/event.pb.h" | 8 #include "remoting/proto/event.pb.h" |
| 11 | 9 |
| 12 namespace { | 10 namespace { |
| 13 | 11 |
| 14 // The number of remote mouse events to record for the purpose of eliminating | 12 // The number of remote mouse events to record for the purpose of eliminating |
| 15 // "echoes" detected by the local input detector. The value should be large | 13 // "echoes" detected by the local input detector. The value should be large |
| 16 // enough to cope with the fact that multiple events might be injected before | 14 // enough to cope with the fact that multiple events might be injected before |
| 17 // any echoes are detected. | 15 // any echoes are detected. |
| 18 const unsigned int kNumRemoteMousePositions = 50; | 16 const unsigned int kNumRemoteMousePositions = 50; |
| 19 | 17 |
| 20 // The number of milliseconds for which to block remote input when local input | 18 // The number of milliseconds for which to block remote input when local input |
| 21 // is received. | 19 // is received. |
| 22 const int64 kRemoteBlockTimeoutMillis = 2000; | 20 const int64 kRemoteBlockTimeoutMillis = 2000; |
| 23 | 21 |
| 24 } // namespace | 22 } // namespace |
| 25 | 23 |
| 26 namespace remoting { | 24 namespace remoting { |
| 27 | 25 |
| 28 RemoteInputFilter::RemoteInputFilter(protocol::InputEventTracker* event_tracker) | 26 RemoteInputFilter::RemoteInputFilter(protocol::InputEventTracker* event_tracker) |
| 29 : event_tracker_(event_tracker), | 27 : event_tracker_(event_tracker), |
| 30 expect_local_echo_(true) { | 28 expect_local_echo_(true) { |
| 31 } | 29 } |
| 32 | 30 |
| 33 RemoteInputFilter::~RemoteInputFilter() { | 31 RemoteInputFilter::~RemoteInputFilter() { |
| 34 } | 32 } |
| 35 | 33 |
| 36 void RemoteInputFilter::LocalMouseMoved(const SkIPoint& mouse_pos) { | 34 void RemoteInputFilter::LocalMouseMoved( |
| 35 const webrtc::DesktopVector& mouse_pos) { |
| 37 // If this is a genuine local input event (rather than an echo of a remote | 36 // If this is a genuine local input event (rather than an echo of a remote |
| 38 // input event that we've just injected), then ignore remote inputs for a | 37 // input event that we've just injected), then ignore remote inputs for a |
| 39 // short time. | 38 // short time. |
| 40 if (expect_local_echo_) { | 39 if (expect_local_echo_) { |
| 41 std::list<SkIPoint>::iterator found_position = | 40 std::list<webrtc::DesktopVector>::iterator found_position = |
| 42 std::find(injected_mouse_positions_.begin(), | 41 injected_mouse_positions_.begin(); |
| 43 injected_mouse_positions_.end(), mouse_pos); | 42 while (found_position != injected_mouse_positions_.end() && |
| 43 !mouse_pos.equals(*found_position)) { |
| 44 ++found_position; |
| 45 } |
| 44 if (found_position != injected_mouse_positions_.end()) { | 46 if (found_position != injected_mouse_positions_.end()) { |
| 45 // Remove it from the list, and any positions that were added before it, | 47 // Remove it from the list, and any positions that were added before it, |
| 46 // if any. This is because the local input monitor is assumed to receive | 48 // if any. This is because the local input monitor is assumed to receive |
| 47 // injected mouse position events in the order in which they were injected | 49 // injected mouse position events in the order in which they were injected |
| 48 // (if at all). If the position is found somewhere other than the front | 50 // (if at all). If the position is found somewhere other than the front |
| 49 // of the queue, this would be because the earlier positions weren't | 51 // of the queue, this would be because the earlier positions weren't |
| 50 // successfully injected (or the local input monitor might have skipped | 52 // successfully injected (or the local input monitor might have skipped |
| 51 // over some positions), and not because the events were out-of-sequence. | 53 // over some positions), and not because the events were out-of-sequence. |
| 52 // These spurious positions should therefore be discarded. | 54 // These spurious positions should therefore be discarded. |
| 53 injected_mouse_positions_.erase(injected_mouse_positions_.begin(), | 55 injected_mouse_positions_.erase(injected_mouse_positions_.begin(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 70 void RemoteInputFilter::InjectKeyEvent(const protocol::KeyEvent& event) { | 72 void RemoteInputFilter::InjectKeyEvent(const protocol::KeyEvent& event) { |
| 71 if (ShouldIgnoreInput()) | 73 if (ShouldIgnoreInput()) |
| 72 return; | 74 return; |
| 73 event_tracker_->InjectKeyEvent(event); | 75 event_tracker_->InjectKeyEvent(event); |
| 74 } | 76 } |
| 75 | 77 |
| 76 void RemoteInputFilter::InjectMouseEvent(const protocol::MouseEvent& event) { | 78 void RemoteInputFilter::InjectMouseEvent(const protocol::MouseEvent& event) { |
| 77 if (ShouldIgnoreInput()) | 79 if (ShouldIgnoreInput()) |
| 78 return; | 80 return; |
| 79 if (expect_local_echo_ && event.has_x() && event.has_y()) { | 81 if (expect_local_echo_ && event.has_x() && event.has_y()) { |
| 80 injected_mouse_positions_.push_back(SkIPoint::Make(event.x(), event.y())); | 82 injected_mouse_positions_.push_back( |
| 83 webrtc::DesktopVector(event.x(), event.y())); |
| 81 if (injected_mouse_positions_.size() > kNumRemoteMousePositions) { | 84 if (injected_mouse_positions_.size() > kNumRemoteMousePositions) { |
| 82 VLOG(1) << "Injected mouse positions queue full."; | 85 VLOG(1) << "Injected mouse positions queue full."; |
| 83 injected_mouse_positions_.pop_front(); | 86 injected_mouse_positions_.pop_front(); |
| 84 } | 87 } |
| 85 } | 88 } |
| 86 event_tracker_->InjectMouseEvent(event); | 89 event_tracker_->InjectMouseEvent(event); |
| 87 } | 90 } |
| 88 | 91 |
| 89 bool RemoteInputFilter::ShouldIgnoreInput() const { | 92 bool RemoteInputFilter::ShouldIgnoreInput() const { |
| 90 // Ignore remote events if the local mouse moved recently. | 93 // Ignore remote events if the local mouse moved recently. |
| 91 int64 millis = | 94 int64 millis = |
| 92 (base::TimeTicks::Now() - latest_local_input_time_).InMilliseconds(); | 95 (base::TimeTicks::Now() - latest_local_input_time_).InMilliseconds(); |
| 93 if (millis < kRemoteBlockTimeoutMillis) | 96 if (millis < kRemoteBlockTimeoutMillis) |
| 94 return true; | 97 return true; |
| 95 return false; | 98 return false; |
| 96 } | 99 } |
| 97 | 100 |
| 98 } // namespace remoting | 101 } // namespace remoting |
| OLD | NEW |