Mandoline: Disable slimming paint
[chromium-blink-merge.git] / remoting / protocol / input_event_tracker.cc
blob8e6274d61b1070f27fd1c842ecae828c04266b1a
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"
9 #include "remoting/protocol/usb_key_codes.h"
11 namespace remoting {
12 namespace protocol {
14 InputEventTracker::InputEventTracker(InputStub* input_stub)
15 : input_stub_(input_stub),
16 mouse_button_state_(0) {
19 InputEventTracker::~InputEventTracker() {}
21 bool InputEventTracker::IsKeyPressed(uint32 usb_keycode) const {
22 return pressed_keys_.find(usb_keycode) != pressed_keys_.end();
25 int InputEventTracker::PressedKeyCount() const {
26 return pressed_keys_.size();
29 void InputEventTracker::ReleaseAll() {
30 // Release all pressed keys.
31 for (uint32 keycode : pressed_keys_) {
32 KeyEvent event;
33 event.set_pressed(false);
34 event.set_usb_keycode(keycode);
35 input_stub_->InjectKeyEvent(event);
37 pressed_keys_.clear();
39 // Release all mouse buttons.
40 for (int i = MouseEvent::BUTTON_UNDEFINED + 1;
41 i < MouseEvent::BUTTON_MAX; ++i) {
42 if (mouse_button_state_ & (1 << (i - 1))) {
43 MouseEvent mouse;
45 // TODO(wez): EventInjectors should cope with positionless events by
46 // using the current cursor position, and we wouldn't set position here.
47 mouse.set_x(mouse_pos_.x());
48 mouse.set_y(mouse_pos_.y());
50 mouse.set_button((MouseEvent::MouseButton)i);
51 mouse.set_button_down(false);
52 input_stub_->InjectMouseEvent(mouse);
55 mouse_button_state_ = 0;
57 // Cancel all active touch points.
58 if (!touch_point_ids_.empty()) {
59 TouchEvent cancel_all_touch_event;
60 cancel_all_touch_event.set_event_type(TouchEvent::TOUCH_POINT_CANCEL);
61 for (uint32 touch_point_id : touch_point_ids_) {
62 TouchEventPoint* point = cancel_all_touch_event.add_touch_points();
63 point->set_id(touch_point_id);
65 input_stub_->InjectTouchEvent(cancel_all_touch_event);
66 touch_point_ids_.clear();
68 DCHECK(touch_point_ids_.empty());
71 void InputEventTracker::ReleaseAllIfModifiersStuck(bool alt_expected,
72 bool ctrl_expected,
73 bool os_expected,
74 bool shift_expected) {
75 bool alt_down =
76 pressed_keys_.find(kUsbLeftAlt) != pressed_keys_.end() ||
77 pressed_keys_.find(kUsbRightAlt) != pressed_keys_.end();
78 bool ctrl_down =
79 pressed_keys_.find(kUsbLeftControl) != pressed_keys_.end() ||
80 pressed_keys_.find(kUsbRightControl) != pressed_keys_.end();
81 bool os_down =
82 pressed_keys_.find(kUsbLeftOs) != pressed_keys_.end() ||
83 pressed_keys_.find(kUsbRightOs) != pressed_keys_.end();
84 bool shift_down =
85 pressed_keys_.find(kUsbLeftShift) != pressed_keys_.end() ||
86 pressed_keys_.find(kUsbRightShift) != pressed_keys_.end();
88 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) ||
89 (os_down && !os_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