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 #include "remoting/protocol/usb_key_codes.h" | |
10 | 9 |
11 namespace remoting { | 10 namespace remoting { |
12 namespace protocol { | 11 namespace protocol { |
13 | 12 |
14 InputEventTracker::InputEventTracker(InputStub* input_stub) | 13 InputEventTracker::InputEventTracker(InputStub* input_stub) |
15 : input_stub_(input_stub), | 14 : input_stub_(input_stub), |
16 mouse_button_state_(0) { | 15 mouse_button_state_(0) { |
17 } | 16 } |
18 | 17 |
19 InputEventTracker::~InputEventTracker() {} | 18 InputEventTracker::~InputEventTracker() {} |
20 | 19 |
21 bool InputEventTracker::IsKeyPressed(uint32 usb_keycode) const { | 20 bool InputEventTracker::IsKeyPressed(ui::DomCode usb_keycode) const { |
22 return pressed_keys_.find(usb_keycode) != pressed_keys_.end(); | 21 return pressed_keys_.find(usb_keycode) != pressed_keys_.end(); |
23 } | 22 } |
24 | 23 |
25 int InputEventTracker::PressedKeyCount() const { | 24 int InputEventTracker::PressedKeyCount() const { |
26 return pressed_keys_.size(); | 25 return pressed_keys_.size(); |
27 } | 26 } |
28 | 27 |
29 void InputEventTracker::ReleaseAll() { | 28 void InputEventTracker::ReleaseAll() { |
30 // Release all pressed keys. | 29 // Release all pressed keys. |
31 for (uint32 keycode : pressed_keys_) { | 30 for (auto keycode : pressed_keys_) { |
32 KeyEvent event; | 31 KeyEvent event; |
33 event.set_pressed(false); | 32 event.set_pressed(false); |
34 event.set_usb_keycode(keycode); | 33 event.set_usb_keycode(static_cast<uint32_t>(keycode)); |
35 input_stub_->InjectKeyEvent(event); | 34 input_stub_->InjectKeyEvent(event); |
36 } | 35 } |
37 pressed_keys_.clear(); | 36 pressed_keys_.clear(); |
38 | 37 |
39 // Release all mouse buttons. | 38 // Release all mouse buttons. |
40 for (int i = MouseEvent::BUTTON_UNDEFINED + 1; | 39 for (int i = MouseEvent::BUTTON_UNDEFINED + 1; |
41 i < MouseEvent::BUTTON_MAX; ++i) { | 40 i < MouseEvent::BUTTON_MAX; ++i) { |
42 if (mouse_button_state_ & (1 << (i - 1))) { | 41 if (mouse_button_state_ & (1 << (i - 1))) { |
43 MouseEvent mouse; | 42 MouseEvent mouse; |
44 | 43 |
45 // TODO(wez): EventInjectors should cope with positionless events by | 44 // TODO(wez): EventInjectors should cope with positionless events by |
46 // using the current cursor position, and we wouldn't set position here. | 45 // using the current cursor position, and we wouldn't set position here. |
47 mouse.set_x(mouse_pos_.x()); | 46 mouse.set_x(mouse_pos_.x()); |
48 mouse.set_y(mouse_pos_.y()); | 47 mouse.set_y(mouse_pos_.y()); |
49 | 48 |
50 mouse.set_button((MouseEvent::MouseButton)i); | 49 mouse.set_button((MouseEvent::MouseButton)i); |
51 mouse.set_button_down(false); | 50 mouse.set_button_down(false); |
52 input_stub_->InjectMouseEvent(mouse); | 51 input_stub_->InjectMouseEvent(mouse); |
53 } | 52 } |
54 } | 53 } |
55 mouse_button_state_ = 0; | 54 mouse_button_state_ = 0; |
56 | 55 |
57 // Cancel all active touch points. | 56 // Cancel all active touch points. |
58 if (!touch_point_ids_.empty()) { | 57 if (!touch_point_ids_.empty()) { |
59 TouchEvent cancel_all_touch_event; | 58 TouchEvent cancel_all_touch_event; |
60 cancel_all_touch_event.set_event_type(TouchEvent::TOUCH_POINT_CANCEL); | 59 cancel_all_touch_event.set_event_type(TouchEvent::TOUCH_POINT_CANCEL); |
61 for (uint32 touch_point_id : touch_point_ids_) { | 60 for (uint32_t touch_point_id : touch_point_ids_) { |
62 TouchEventPoint* point = cancel_all_touch_event.add_touch_points(); | 61 TouchEventPoint* point = cancel_all_touch_event.add_touch_points(); |
63 point->set_id(touch_point_id); | 62 point->set_id(touch_point_id); |
64 } | 63 } |
65 input_stub_->InjectTouchEvent(cancel_all_touch_event); | 64 input_stub_->InjectTouchEvent(cancel_all_touch_event); |
66 touch_point_ids_.clear(); | 65 touch_point_ids_.clear(); |
67 } | 66 } |
68 DCHECK(touch_point_ids_.empty()); | 67 DCHECK(touch_point_ids_.empty()); |
69 } | 68 } |
70 | 69 |
71 void InputEventTracker::ReleaseAllIfModifiersStuck(bool alt_expected, | 70 void InputEventTracker::ReleaseAllIfModifiersStuck(bool alt_expected, |
72 bool ctrl_expected, | 71 bool ctrl_expected, |
73 bool os_expected, | 72 bool os_expected, |
74 bool shift_expected) { | 73 bool shift_expected) { |
75 bool alt_down = | 74 bool alt_down = |
76 pressed_keys_.find(kUsbLeftAlt) != pressed_keys_.end() || | 75 pressed_keys_.find(ui::DomCode::ALT_LEFT) != pressed_keys_.end() || |
77 pressed_keys_.find(kUsbRightAlt) != pressed_keys_.end(); | 76 pressed_keys_.find(ui::DomCode::ALT_RIGHT) != pressed_keys_.end(); |
78 bool ctrl_down = | 77 bool ctrl_down = |
79 pressed_keys_.find(kUsbLeftControl) != pressed_keys_.end() || | 78 pressed_keys_.find(ui::DomCode::CONTROL_LEFT) != pressed_keys_.end() || |
80 pressed_keys_.find(kUsbRightControl) != pressed_keys_.end(); | 79 pressed_keys_.find(ui::DomCode::CONTROL_RIGHT) != pressed_keys_.end(); |
81 bool os_down = | 80 bool os_down = |
82 pressed_keys_.find(kUsbLeftOs) != pressed_keys_.end() || | 81 pressed_keys_.find(ui::DomCode::OS_LEFT) != pressed_keys_.end() || |
83 pressed_keys_.find(kUsbRightOs) != pressed_keys_.end(); | 82 pressed_keys_.find(ui::DomCode::OS_RIGHT) != pressed_keys_.end(); |
84 bool shift_down = | 83 bool shift_down = |
85 pressed_keys_.find(kUsbLeftShift) != pressed_keys_.end() || | 84 pressed_keys_.find(ui::DomCode::SHIFT_LEFT) != pressed_keys_.end() || |
86 pressed_keys_.find(kUsbRightShift) != pressed_keys_.end(); | 85 pressed_keys_.find(ui::DomCode::SHIFT_RIGHT) != pressed_keys_.end(); |
87 | 86 |
88 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) || | 87 if ((alt_down && !alt_expected) || (ctrl_down && !ctrl_expected) || |
89 (os_down && !os_expected) || (shift_down && !shift_expected)) { | 88 (os_down && !os_expected) || (shift_down && !shift_expected)) { |
90 ReleaseAll(); | 89 ReleaseAll(); |
91 } | 90 } |
92 } | 91 } |
93 | 92 |
94 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { | 93 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) { |
95 // We don't need to track the keyboard lock states of key down events. | 94 // 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. | 95 // 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 | 96 // 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. | 97 // we release all the pressed keys at blurring/disconnection time. |
99 if (event.has_pressed()) { | 98 if (event.has_pressed()) { |
100 if (event.has_usb_keycode()) { | 99 if (event.has_usb_keycode()) { |
101 if (event.pressed()) { | 100 if (event.pressed()) { |
102 pressed_keys_.insert(event.usb_keycode()); | 101 pressed_keys_.insert(static_cast<ui::DomCode>(event.usb_keycode())); |
103 } else { | 102 } else { |
104 pressed_keys_.erase(event.usb_keycode()); | 103 pressed_keys_.erase(static_cast<ui::DomCode>(event.usb_keycode())); |
105 } | 104 } |
106 } | 105 } |
107 } | 106 } |
108 input_stub_->InjectKeyEvent(event); | 107 input_stub_->InjectKeyEvent(event); |
109 } | 108 } |
110 | 109 |
111 void InputEventTracker::InjectTextEvent(const TextEvent& event) { | 110 void InputEventTracker::InjectTextEvent(const TextEvent& event) { |
112 input_stub_->InjectTextEvent(event); | 111 input_stub_->InjectTextEvent(event); |
113 } | 112 } |
114 | 113 |
115 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { | 114 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) { |
116 if (event.has_x() && event.has_y()) { | 115 if (event.has_x() && event.has_y()) { |
117 mouse_pos_ = webrtc::DesktopVector(event.x(), event.y()); | 116 mouse_pos_ = webrtc::DesktopVector(event.x(), event.y()); |
118 } | 117 } |
119 if (event.has_button() && event.has_button_down()) { | 118 if (event.has_button() && event.has_button_down()) { |
120 // Button values are defined in remoting/proto/event.proto. | 119 // Button values are defined in remoting/proto/event.proto. |
121 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { | 120 if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) { |
122 uint32 button_change = 1 << (event.button() - 1); | 121 uint32_t button_change = 1 << (event.button() - 1); |
123 if (event.button_down()) { | 122 if (event.button_down()) { |
124 mouse_button_state_ |= button_change; | 123 mouse_button_state_ |= button_change; |
125 } else { | 124 } else { |
126 mouse_button_state_ &= ~button_change; | 125 mouse_button_state_ &= ~button_change; |
127 } | 126 } |
128 } | 127 } |
129 } | 128 } |
130 input_stub_->InjectMouseEvent(event); | 129 input_stub_->InjectMouseEvent(event); |
131 } | 130 } |
132 | 131 |
(...skipping 18 matching lines...) Expand all Loading... |
151 } | 150 } |
152 break; | 151 break; |
153 default: | 152 default: |
154 break; | 153 break; |
155 } | 154 } |
156 input_stub_->InjectTouchEvent(event); | 155 input_stub_->InjectTouchEvent(event); |
157 } | 156 } |
158 | 157 |
159 } // namespace protocol | 158 } // namespace protocol |
160 } // namespace remoting | 159 } // namespace remoting |
OLD | NEW |