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