OLD | NEW |
(Empty) | |
| 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/input_event_tracker.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "remoting/proto/event.pb.h" |
| 9 |
| 10 namespace remoting { |
| 11 namespace protocol { |
| 12 |
| 13 InputEventTracker::InputEventTracker(InputStub* input_stub) |
| 14 : input_stub_(input_stub), |
| 15 mouse_pos_(SkIPoint::Make(0, 0)), |
| 16 mouse_button_state_(0) { |
| 17 } |
| 18 |
| 19 InputEventTracker::~InputEventTracker() { |
| 20 } |
| 21 |
| 22 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { |
| 23 DCHECK(event.has_pressed()); |
| 24 DCHECK(event.has_keycode()); |
| 25 if (event.pressed()) { |
| 26 pressed_keys_.insert(event.keycode()); |
| 27 } else { |
| 28 pressed_keys_.erase(event.keycode()); |
| 29 } |
| 30 input_stub_->InjectKeyEvent(event); |
| 31 } |
| 32 |
| 33 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { |
| 34 if (event.has_x() && event.has_y()) { |
| 35 mouse_pos_ = SkIPoint::Make(event.x(), event.y()); |
| 36 } |
| 37 if (event.has_button() && event.has_button_down()) { |
| 38 // Button values are defined in remoting/proto/event.proto. |
| 39 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { |
| 40 uint32 button_change = 1 << (event.button() - 1); |
| 41 if (event.button_down()) { |
| 42 mouse_button_state_ |= button_change; |
| 43 } else { |
| 44 mouse_button_state_ &= ~button_change; |
| 45 } |
| 46 } |
| 47 } |
| 48 input_stub_->InjectMouseEvent(event); |
| 49 } |
| 50 |
| 51 void InputEventTracker::ReleaseAll() { |
| 52 std::set<int>::iterator i; |
| 53 for (i = pressed_keys_.begin(); i != pressed_keys_.end(); ++i) { |
| 54 KeyEvent event; |
| 55 event.set_keycode(*i); |
| 56 event.set_pressed(false); |
| 57 input_stub_->InjectKeyEvent(event); |
| 58 } |
| 59 pressed_keys_.clear(); |
| 60 |
| 61 for (int i = MouseEvent::BUTTON_UNDEFINED + 1; |
| 62 i < MouseEvent::BUTTON_MAX; ++i) { |
| 63 if (mouse_button_state_ & (1 << (i - 1))) { |
| 64 MouseEvent mouse; |
| 65 |
| 66 // TODO(wez): EventInjectors should cope with positionless events by |
| 67 // using the current cursor position, and we wouldn't set position here. |
| 68 mouse.set_x(mouse_pos_.x()); |
| 69 mouse.set_y(mouse_pos_.y()); |
| 70 |
| 71 mouse.set_button((MouseEvent::MouseButton)i); |
| 72 mouse.set_button_down(false); |
| 73 input_stub_->InjectMouseEvent(mouse); |
| 74 } |
| 75 } |
| 76 mouse_button_state_ = 0; |
| 77 } |
| 78 |
| 79 } // namespace protocol |
| 80 } // namespace remoting |
OLD | NEW |