Disable ChannelMojo.
[chromium-blink-merge.git] / remoting / protocol / input_event_tracker.cc
blobaca346b0aee76674cd90967bf4101bb1e85219a8
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.
5 #include "remoting/protocol/input_event_tracker.h"
7 #include "base/logging.h"
8 #include "remoting/proto/event.pb.h"
10 namespace remoting {
11 namespace protocol {
13 InputEventTracker::InputEventTracker(InputStub* input_stub)
14 : input_stub_(input_stub),
15 mouse_button_state_(0) {
18 InputEventTracker::~InputEventTracker() {}
20 bool InputEventTracker::IsKeyPressed(uint32 usb_keycode) const {
21 return pressed_keys_.find(usb_keycode) != pressed_keys_.end();
24 int InputEventTracker::PressedKeyCount() const {
25 return pressed_keys_.size();
28 void InputEventTracker::ReleaseAll() {
29 // Release all pressed keys.
30 for (uint32 keycode : pressed_keys_) {
31 KeyEvent event;
32 event.set_pressed(false);
33 event.set_usb_keycode(keycode);
34 input_stub_->InjectKeyEvent(event);
36 pressed_keys_.clear();
38 // Release all mouse buttons.
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;
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());
49 mouse.set_button((MouseEvent::MouseButton)i);
50 mouse.set_button_down(false);
51 input_stub_->InjectMouseEvent(mouse);
54 mouse_button_state_ = 0;
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);
64 input_stub_->InjectTouchEvent(cancel_all_touch_event);
65 touch_point_ids_.clear();
67 DCHECK(touch_point_ids_.empty());
70 void InputEventTracker::ReleaseAllIfModifiersStuck(bool alt_expected,
71 bool ctrl_expected,
72 bool meta_expected,
73 bool shift_expected) {
74 // See src/ui/events/keycodes/dom4/keycode_converter_data.h for these values.
75 bool alt_down =
76 pressed_keys_.find(0x0700e2) != pressed_keys_.end() || // Left
77 pressed_keys_.find(0x0700e6) != pressed_keys_.end(); // Right
78 bool ctrl_down =
79 pressed_keys_.find(0x0700e0) != pressed_keys_.end() || // Left
80 pressed_keys_.find(0x0700e4) != pressed_keys_.end(); // Right
81 bool meta_down =
82 pressed_keys_.find(0x0700e3) != pressed_keys_.end() || // Left
83 pressed_keys_.find(0x0700e7) != pressed_keys_.end(); // Right
84 bool shift_down =
85 pressed_keys_.find(0x0700e1) != pressed_keys_.end() || // Left
86 pressed_keys_.find(0x0700e5) != pressed_keys_.end(); // Right
88 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) ||
89 (meta_down && !meta_expected) || (shift_down && !shift_expected)) {
90 ReleaseAll();
94 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) {
95 // We don't need to track the keyboard lock states of key down events.
96 // Pressed keys will be released with |lock_states| set to 0.
97 // The lock states of auto generated key up events don't matter as long as
98 // we release all the pressed keys at blurring/disconnection time.
99 if (event.has_pressed()) {
100 if (event.has_usb_keycode()) {
101 if (event.pressed()) {
102 pressed_keys_.insert(event.usb_keycode());
103 } else {
104 pressed_keys_.erase(event.usb_keycode());
108 input_stub_->InjectKeyEvent(event);
111 void InputEventTracker::InjectTextEvent(const TextEvent& event) {
112 input_stub_->InjectTextEvent(event);
115 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) {
116 if (event.has_x() && event.has_y()) {
117 mouse_pos_ = webrtc::DesktopVector(event.x(), event.y());
119 if (event.has_button() && event.has_button_down()) {
120 // Button values are defined in remoting/proto/event.proto.
121 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) {
122 uint32 button_change = 1 << (event.button() - 1);
123 if (event.button_down()) {
124 mouse_button_state_ |= button_change;
125 } else {
126 mouse_button_state_ &= ~button_change;
130 input_stub_->InjectMouseEvent(event);
133 void InputEventTracker::InjectTouchEvent(const TouchEvent& event) {
134 // We only need the IDs to cancel all touch points in ReleaseAll(). Other
135 // fields do not have to be tracked here as long as the host keeps track of
136 // them.
137 switch (event.event_type()) {
138 case TouchEvent::TOUCH_POINT_START:
139 for (const TouchEventPoint& touch_point : event.touch_points()) {
140 DCHECK(touch_point_ids_.find(touch_point.id()) ==
141 touch_point_ids_.end());
142 touch_point_ids_.insert(touch_point.id());
144 break;
145 case TouchEvent::TOUCH_POINT_END:
146 case TouchEvent::TOUCH_POINT_CANCEL:
147 for (const TouchEventPoint& touch_point : event.touch_points()) {
148 DCHECK(touch_point_ids_.find(touch_point.id()) !=
149 touch_point_ids_.end());
150 touch_point_ids_.erase(touch_point.id());
152 break;
153 default:
154 break;
156 input_stub_->InjectTouchEvent(event);
159 } // namespace protocol
160 } // namespace remoting