Chromium Code Reviews| 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 = 1; i < MouseEvent::BUTTON_MAX; i++) { | |
|
Sergey Ulanov
2012/03/30 20:33:54
nit: might be cleaner to replace 1 with "MosueButt
Wez
2012/04/01 00:47:15
Done.
| |
| 62 if (mouse_button_state_ & (1 << (i - 1))) { | |
| 63 MouseEvent mouse; | |
| 64 | |
| 65 // TODO(wez): EventInjectors should cope with positionless events by | |
| 66 // using the current cursor position, and we wouldn't set position here. | |
| 67 mouse.set_x(mouse_pos_.x()); | |
| 68 mouse.set_y(mouse_pos_.y()); | |
| 69 | |
| 70 mouse.set_button((MouseEvent::MouseButton)i); | |
| 71 mouse.set_button_down(false); | |
| 72 input_stub_->InjectMouseEvent(mouse); | |
| 73 } | |
| 74 } | |
| 75 mouse_button_state_ = 0; | |
| 76 } | |
| 77 | |
| 78 } // namespace protocol | |
| 79 } // namespace remoting | |
| OLD | NEW |