[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 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), |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 15 | mouse_button_state_(0) { |
| 16 | } |
| 17 | |
[email protected] | 529bbd1 | 2014-03-27 20:25:39 | [diff] [blame] | 18 | InputEventTracker::~InputEventTracker() {} |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 19 | |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 20 | bool InputEventTracker::IsKeyPressed(uint32 usb_keycode) const { |
| 21 | return pressed_keys_.find(usb_keycode) != pressed_keys_.end(); |
| 22 | } |
| 23 | |
| 24 | int InputEventTracker::PressedKeyCount() const { |
[email protected] | f5e7c88 | 2012-09-11 01:52:14 | [diff] [blame] | 25 | return pressed_keys_.size(); |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void InputEventTracker::ReleaseAll() { |
rkuroiwa | 0e68803f | 2015-02-26 19:41:42 | [diff] [blame^] | 29 | // Release all pressed keys. |
| 30 | for (uint32 keycode : pressed_keys_) { |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 31 | KeyEvent event; |
| 32 | event.set_pressed(false); |
rkuroiwa | 0e68803f | 2015-02-26 19:41:42 | [diff] [blame^] | 33 | event.set_usb_keycode(keycode); |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 34 | input_stub_->InjectKeyEvent(event); |
| 35 | } |
| 36 | pressed_keys_.clear(); |
| 37 | |
rkuroiwa | 0e68803f | 2015-02-26 19:41:42 | [diff] [blame^] | 38 | // Release all mouse buttons. |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 39 | for (int i = MouseEvent::BUTTON_UNDEFINED + 1; |
| 40 | i < MouseEvent::BUTTON_MAX; ++i) { |
| 41 | if (mouse_button_state_ & (1 << (i - 1))) { |
| 42 | MouseEvent mouse; |
| 43 | |
| 44 | // TODO(wez): EventInjectors should cope with positionless events by |
| 45 | // using the current cursor position, and we wouldn't set position here. |
| 46 | mouse.set_x(mouse_pos_.x()); |
| 47 | mouse.set_y(mouse_pos_.y()); |
| 48 | |
| 49 | mouse.set_button((MouseEvent::MouseButton)i); |
| 50 | mouse.set_button_down(false); |
| 51 | input_stub_->InjectMouseEvent(mouse); |
| 52 | } |
| 53 | } |
| 54 | mouse_button_state_ = 0; |
rkuroiwa | 0e68803f | 2015-02-26 19:41:42 | [diff] [blame^] | 55 | |
| 56 | // Cancel all active touch points. |
| 57 | if (!touch_point_ids_.empty()) { |
| 58 | TouchEvent cancel_all_touch_event; |
| 59 | cancel_all_touch_event.set_event_type(TouchEvent::TOUCH_POINT_CANCEL); |
| 60 | for (uint32 touch_point_id : touch_point_ids_) { |
| 61 | TouchEventPoint* point = cancel_all_touch_event.add_touch_points(); |
| 62 | point->set_id(touch_point_id); |
| 63 | } |
| 64 | input_stub_->InjectTouchEvent(cancel_all_touch_event); |
| 65 | touch_point_ids_.clear(); |
| 66 | } |
| 67 | DCHECK(touch_point_ids_.empty()); |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 70 | void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { |
[email protected] | 5e3afcc | 2013-06-26 05:42:27 | [diff] [blame] | 71 | // We don't need to track the keyboard lock states of key down events. |
| 72 | // Pressed keys will be released with |lock_states| set to 0. |
| 73 | // The lock states of auto generated key up events don't matter as long as |
| 74 | // we release all the pressed keys at blurring/disconnection time. |
[email protected] | 83e6f58 | 2012-04-09 19:41:46 | [diff] [blame] | 75 | if (event.has_pressed()) { |
| 76 | if (event.has_usb_keycode()) { |
| 77 | if (event.pressed()) { |
| 78 | pressed_keys_.insert(event.usb_keycode()); |
| 79 | } else { |
| 80 | pressed_keys_.erase(event.usb_keycode()); |
| 81 | } |
[email protected] | fa8c729 | 2012-04-04 17:26:37 | [diff] [blame] | 82 | } |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 83 | } |
| 84 | input_stub_->InjectKeyEvent(event); |
| 85 | } |
| 86 | |
[email protected] | 529bbd1 | 2014-03-27 20:25:39 | [diff] [blame] | 87 | void InputEventTracker::InjectTextEvent(const TextEvent& event) { |
| 88 | input_stub_->InjectTextEvent(event); |
| 89 | } |
| 90 | |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 91 | void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { |
| 92 | if (event.has_x() && event.has_y()) { |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 93 | mouse_pos_ = webrtc::DesktopVector(event.x(), event.y()); |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 94 | } |
| 95 | if (event.has_button() && event.has_button_down()) { |
| 96 | // Button values are defined in remoting/proto/event.proto. |
| 97 | if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { |
| 98 | uint32 button_change = 1 << (event.button() - 1); |
| 99 | if (event.button_down()) { |
| 100 | mouse_button_state_ |= button_change; |
| 101 | } else { |
| 102 | mouse_button_state_ &= ~button_change; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | input_stub_->InjectMouseEvent(event); |
| 107 | } |
| 108 | |
rkuroiwa | 0e68803f | 2015-02-26 19:41:42 | [diff] [blame^] | 109 | void InputEventTracker::InjectTouchEvent(const TouchEvent& event) { |
| 110 | // We only need the IDs to cancel all touch points in ReleaseAll(). Other |
| 111 | // fields do not have to be tracked here as long as the host keeps track of |
| 112 | // them. |
| 113 | switch (event.event_type()) { |
| 114 | case TouchEvent::TOUCH_POINT_START: |
| 115 | for (const TouchEventPoint& touch_point : event.touch_points()) { |
| 116 | DCHECK(touch_point_ids_.find(touch_point.id()) == |
| 117 | touch_point_ids_.end()); |
| 118 | touch_point_ids_.insert(touch_point.id()); |
| 119 | } |
| 120 | break; |
| 121 | case TouchEvent::TOUCH_POINT_END: |
| 122 | case TouchEvent::TOUCH_POINT_CANCEL: |
| 123 | for (const TouchEventPoint& touch_point : event.touch_points()) { |
| 124 | DCHECK(touch_point_ids_.find(touch_point.id()) != |
| 125 | touch_point_ids_.end()); |
| 126 | touch_point_ids_.erase(touch_point.id()); |
| 127 | } |
| 128 | break; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | input_stub_->InjectTouchEvent(event); |
| 133 | } |
| 134 | |
[email protected] | 86cbe6b | 2012-04-03 00:56:18 | [diff] [blame] | 135 | } // namespace protocol |
| 136 | } // namespace remoting |