| 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() {} |
| 14 |
| 13 InputEventTracker::InputEventTracker(InputStub* input_stub) | 15 InputEventTracker::InputEventTracker(InputStub* input_stub) |
| 14 : input_stub_(input_stub), | 16 : input_stub_(input_stub) { |
| 15 mouse_button_state_(0) { | |
| 16 } | 17 } |
| 17 | 18 |
| 18 InputEventTracker::~InputEventTracker() {} | 19 InputEventTracker::~InputEventTracker() {} |
| 19 | 20 |
| 20 bool InputEventTracker::IsKeyPressed(ui::DomCode usb_keycode) const { | 21 bool InputEventTracker::IsKeyPressed(ui::DomCode usb_keycode) const { |
| 21 return pressed_keys_.find(usb_keycode) != pressed_keys_.end(); | 22 return pressed_keys_.find(usb_keycode) != pressed_keys_.end(); |
| 22 } | 23 } |
| 23 | 24 |
| 24 int InputEventTracker::PressedKeyCount() const { | 25 int InputEventTracker::PressedKeyCount() const { |
| 25 return pressed_keys_.size(); | 26 return pressed_keys_.size(); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void InputEventTracker::ReleaseAll() { | 29 void InputEventTracker::ReleaseAll() { |
| 30 DCHECK(input_stub_); |
| 31 |
| 29 // Release all pressed keys. | 32 // Release all pressed keys. |
| 30 for (auto keycode : pressed_keys_) { | 33 for (auto keycode : pressed_keys_) { |
| 31 KeyEvent event; | 34 KeyEvent event; |
| 32 event.set_pressed(false); | 35 event.set_pressed(false); |
| 33 event.set_usb_keycode(static_cast<uint32_t>(keycode)); | 36 event.set_usb_keycode(static_cast<uint32_t>(keycode)); |
| 34 input_stub_->InjectKeyEvent(event); | 37 input_stub_->InjectKeyEvent(event); |
| 35 } | 38 } |
| 36 pressed_keys_.clear(); | 39 pressed_keys_.clear(); |
| 37 | 40 |
| 38 // Release all mouse buttons. | 41 // Release all mouse buttons. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 pressed_keys_.find(ui::DomCode::SHIFT_LEFT) != pressed_keys_.end() || | 87 pressed_keys_.find(ui::DomCode::SHIFT_LEFT) != pressed_keys_.end() || |
| 85 pressed_keys_.find(ui::DomCode::SHIFT_RIGHT) != pressed_keys_.end(); | 88 pressed_keys_.find(ui::DomCode::SHIFT_RIGHT) != pressed_keys_.end(); |
| 86 | 89 |
| 87 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) || | 90 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) || |
| 88 (os_down && !os_expected) || (shift_down && !shift_expected)) { | 91 (os_down && !os_expected) || (shift_down && !shift_expected)) { |
| 89 ReleaseAll(); | 92 ReleaseAll(); |
| 90 } | 93 } |
| 91 } | 94 } |
| 92 | 95 |
| 93 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { | 96 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { |
| 97 DCHECK(input_stub_); |
| 98 |
| 94 // We don't need to track the keyboard lock states of key down events. | 99 // We don't need to track the keyboard lock states of key down events. |
| 95 // Pressed keys will be released with |lock_states| set to 0. | 100 // Pressed keys will be released with |lock_states| set to 0. |
| 96 // The lock states of auto generated key up events don't matter as long as | 101 // The lock states of auto generated key up events don't matter as long as |
| 97 // we release all the pressed keys at blurring/disconnection time. | 102 // we release all the pressed keys at blurring/disconnection time. |
| 98 if (event.has_pressed()) { | 103 if (event.has_pressed()) { |
| 99 if (event.has_usb_keycode()) { | 104 if (event.has_usb_keycode()) { |
| 100 if (event.pressed()) { | 105 if (event.pressed()) { |
| 101 pressed_keys_.insert(static_cast<ui::DomCode>(event.usb_keycode())); | 106 pressed_keys_.insert(static_cast<ui::DomCode>(event.usb_keycode())); |
| 102 } else { | 107 } else { |
| 103 pressed_keys_.erase(static_cast<ui::DomCode>(event.usb_keycode())); | 108 pressed_keys_.erase(static_cast<ui::DomCode>(event.usb_keycode())); |
| 104 } | 109 } |
| 105 } | 110 } |
| 106 } | 111 } |
| 107 input_stub_->InjectKeyEvent(event); | 112 input_stub_->InjectKeyEvent(event); |
| 108 } | 113 } |
| 109 | 114 |
| 110 void InputEventTracker::InjectTextEvent(const TextEvent& event) { | 115 void InputEventTracker::InjectTextEvent(const TextEvent& event) { |
| 116 DCHECK(input_stub_); |
| 111 input_stub_->InjectTextEvent(event); | 117 input_stub_->InjectTextEvent(event); |
| 112 } | 118 } |
| 113 | 119 |
| 114 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { | 120 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { |
| 121 DCHECK(input_stub_); |
| 122 |
| 115 if (event.has_x() && event.has_y()) { | 123 if (event.has_x() && event.has_y()) { |
| 116 mouse_pos_ = webrtc::DesktopVector(event.x(), event.y()); | 124 mouse_pos_ = webrtc::DesktopVector(event.x(), event.y()); |
| 117 } | 125 } |
| 118 if (event.has_button() && event.has_button_down()) { | 126 if (event.has_button() && event.has_button_down()) { |
| 119 // Button values are defined in remoting/proto/event.proto. | 127 // Button values are defined in remoting/proto/event.proto. |
| 120 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { | 128 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { |
| 121 uint32_t button_change = 1 << (event.button() - 1); | 129 uint32_t button_change = 1 << (event.button() - 1); |
| 122 if (event.button_down()) { | 130 if (event.button_down()) { |
| 123 mouse_button_state_ |= button_change; | 131 mouse_button_state_ |= button_change; |
| 124 } else { | 132 } else { |
| 125 mouse_button_state_ &= ~button_change; | 133 mouse_button_state_ &= ~button_change; |
| 126 } | 134 } |
| 127 } | 135 } |
| 128 } | 136 } |
| 129 input_stub_->InjectMouseEvent(event); | 137 input_stub_->InjectMouseEvent(event); |
| 130 } | 138 } |
| 131 | 139 |
| 132 void InputEventTracker::InjectTouchEvent(const TouchEvent& event) { | 140 void InputEventTracker::InjectTouchEvent(const TouchEvent& event) { |
| 141 DCHECK(input_stub_); |
| 133 // We only need the IDs to cancel all touch points in ReleaseAll(). Other | 142 // We only need the IDs to cancel all touch points in ReleaseAll(). Other |
| 134 // fields do not have to be tracked here as long as the host keeps track of | 143 // fields do not have to be tracked here as long as the host keeps track of |
| 135 // them. | 144 // them. |
| 136 switch (event.event_type()) { | 145 switch (event.event_type()) { |
| 137 case TouchEvent::TOUCH_POINT_START: | 146 case TouchEvent::TOUCH_POINT_START: |
| 138 for (const TouchEventPoint& touch_point : event.touch_points()) { | 147 for (const TouchEventPoint& touch_point : event.touch_points()) { |
| 139 DCHECK(touch_point_ids_.find(touch_point.id()) == | 148 DCHECK(touch_point_ids_.find(touch_point.id()) == |
| 140 touch_point_ids_.end()); | 149 touch_point_ids_.end()); |
| 141 touch_point_ids_.insert(touch_point.id()); | 150 touch_point_ids_.insert(touch_point.id()); |
| 142 } | 151 } |
| 143 break; | 152 break; |
| 144 case TouchEvent::TOUCH_POINT_END: | 153 case TouchEvent::TOUCH_POINT_END: |
| 145 case TouchEvent::TOUCH_POINT_CANCEL: | 154 case TouchEvent::TOUCH_POINT_CANCEL: |
| 146 for (const TouchEventPoint& touch_point : event.touch_points()) { | 155 for (const TouchEventPoint& touch_point : event.touch_points()) { |
| 147 DCHECK(touch_point_ids_.find(touch_point.id()) != | 156 DCHECK(touch_point_ids_.find(touch_point.id()) != |
| 148 touch_point_ids_.end()); | 157 touch_point_ids_.end()); |
| 149 touch_point_ids_.erase(touch_point.id()); | 158 touch_point_ids_.erase(touch_point.id()); |
| 150 } | 159 } |
| 151 break; | 160 break; |
| 152 default: | 161 default: |
| 153 break; | 162 break; |
| 154 } | 163 } |
| 155 input_stub_->InjectTouchEvent(event); | 164 input_stub_->InjectTouchEvent(event); |
| 156 } | 165 } |
| 157 | 166 |
| 158 } // namespace protocol | 167 } // namespace protocol |
| 159 } // namespace remoting | 168 } // namespace remoting |
| OLD | NEW |