Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/host/event_executor_win.h" | 5 #include "remoting/host/event_executor_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | |
| 8 #include "app/keyboard_codes.h" | 9 #include "app/keyboard_codes.h" |
| 10 #include "base/message_loop.h" | |
| 9 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
| 10 #include "remoting/host/capturer.h" | 12 #include "remoting/host/capturer.h" |
| 11 // TODO(hclam): Should not include internal.pb.h here. | 13 #include "remoting/proto/event.pb.h" |
| 12 #include "remoting/proto/internal.pb.h" | |
| 13 | 14 |
| 14 namespace remoting { | 15 namespace remoting { |
| 15 | 16 |
| 16 EventExecutorWin::EventExecutorWin(Capturer* capturer) | 17 EventExecutorWin::EventExecutorWin( |
| 17 : EventExecutor(capturer) { | 18 MessageLoop* message_loop, Capturer* capturer) |
| 19 : message_loop_(message_loop), capturer_(capturer) { | |
| 18 } | 20 } |
| 19 | 21 |
| 20 EventExecutorWin::~EventExecutorWin() { | 22 EventExecutorWin::~EventExecutorWin() { |
| 21 } | 23 } |
| 22 | 24 |
| 23 void EventExecutorWin::HandleInputEvent(ChromotingClientMessage* msg) { | 25 void EventExecutorWin::InjectKeyEvent(const KeyEvent* event, Task* done) { |
| 24 if (msg->has_mouse_set_position_event()) { | 26 if (!message_loop_->BelongsToCurrentThread()) { |
|
awong
2010/11/12 23:29:04
Does this compile?
| |
| 25 HandleMouseSetPosition(msg); | 27 message_loop_->PostTask( |
| 26 } else if (msg->has_mouse_move_event()) { | 28 FROM_HERE, |
| 27 HandleMouseMove(msg); | 29 NewRunnableMethod(this, &EventExecutorWin::InjectKeyEvent, |
| 28 } else if (msg->has_mouse_wheel_event()) { | 30 event, done)); |
| 29 HandleMouseWheel(msg); | 31 return; |
| 30 } else if (msg->has_mouse_down_event()) { | |
| 31 HandleMouseButtonDown(msg); | |
| 32 } else if (msg->has_mouse_up_event()) { | |
| 33 HandleMouseButtonUp(msg); | |
| 34 } else if (msg->has_key_event()) { | |
| 35 HandleKey(msg); | |
| 36 } | 32 } |
| 37 delete msg; | 33 HandleKey(*event); |
| 34 done->Run(); | |
| 35 delete done; | |
| 38 } | 36 } |
| 39 | 37 |
| 40 void EventExecutorWin::HandleMouseSetPosition(ChromotingClientMessage* msg) { | 38 void EventExecutorWin::InjectMouseEvent(const MouseEvent* event, |
| 41 int x = msg->mouse_set_position_event().x(); | 39 Task* done) { |
| 42 int y = msg->mouse_set_position_event().y(); | 40 if (!message_loop_->BelongsToCurrentThread()) { |
| 43 int width = msg->mouse_set_position_event().width(); | 41 message_loop_->PostTask( |
| 44 int height = msg->mouse_set_position_event().height(); | 42 FROM_HERE, |
| 43 NewRunnableMethod(this, &EventExecutorWin::InjectMouseEvent, | |
| 44 event, done)); | |
| 45 return; | |
| 46 } | |
| 47 if (event->has_set_position()) { | |
| 48 HandleMouseSetPosition(event->set_position()); | |
| 49 } else if (event->has_wheel()) { | |
| 50 HandleMouseWheel(event->wheel()); | |
| 51 } else if (event->has_down()) { | |
| 52 HandleMouseButtonDown(event->down()); | |
| 53 } else if (event->has_up()) { | |
| 54 HandleMouseButtonUp(event->up()); | |
| 55 } | |
| 56 done->Run(); | |
| 57 delete done; | |
| 58 } | |
| 59 | |
| 60 void EventExecutorWin::HandleMouseSetPosition( | |
| 61 const MouseSetPositionEvent& event) { | |
| 62 int x = event.x(); | |
| 63 int y = event.y(); | |
| 64 int width = event.width(); | |
| 65 int height = event.height(); | |
| 45 | 66 |
| 46 // Get width and height from the capturer if they are missing from the | 67 // Get width and height from the capturer if they are missing from the |
| 47 // message. | 68 // message. |
| 48 if (width == 0 || height == 0) { | 69 if (width == 0 || height == 0) { |
| 49 width = capturer_->width(); | 70 width = capturer_->width(); |
| 50 height = capturer_->height(); | 71 height = capturer_->height(); |
| 51 } | 72 } |
| 52 if (width == 0 || height == 0) { | 73 if (width == 0 || height == 0) { |
| 53 return; | 74 return; |
| 54 } | 75 } |
| 55 | 76 |
| 56 INPUT input; | 77 INPUT input; |
| 57 input.type = INPUT_MOUSE; | 78 input.type = INPUT_MOUSE; |
| 58 input.mi.time = 0; | 79 input.mi.time = 0; |
| 59 input.mi.dx = static_cast<int>((x * 65535) / width); | 80 input.mi.dx = static_cast<int>((x * 65535) / width); |
| 60 input.mi.dy = static_cast<int>((y * 65535) / height); | 81 input.mi.dy = static_cast<int>((y * 65535) / height); |
| 61 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; | 82 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; |
| 62 SendInput(1, &input, sizeof(INPUT)); | 83 SendInput(1, &input, sizeof(INPUT)); |
| 63 } | 84 } |
| 64 | 85 |
| 65 void EventExecutorWin::HandleMouseMove(ChromotingClientMessage* msg) { | 86 void EventExecutorWin::HandleMouseWheel( |
| 66 INPUT input; | 87 const MouseWheelEvent& event) { |
| 67 input.type = INPUT_MOUSE; | |
| 68 input.mi.time = 0; | |
| 69 input.mi.dx = msg->mouse_move_event().offset_x(); | |
| 70 input.mi.dy = msg->mouse_move_event().offset_y(); | |
| 71 input.mi.dwFlags = MOUSEEVENTF_MOVE; | |
| 72 SendInput(1, &input, sizeof(INPUT)); | |
| 73 } | |
| 74 | |
| 75 void EventExecutorWin::HandleMouseWheel(ChromotingClientMessage* msg) { | |
| 76 INPUT input; | 88 INPUT input; |
| 77 input.type = INPUT_MOUSE; | 89 input.type = INPUT_MOUSE; |
| 78 input.mi.time = 0; | 90 input.mi.time = 0; |
| 79 | 91 |
| 80 int dx = msg->mouse_wheel_event().offset_x(); | 92 int dx = event.offset_x(); |
| 81 int dy = msg->mouse_wheel_event().offset_y(); | 93 int dy = event.offset_y(); |
| 82 | 94 |
| 83 if (dx != 0) { | 95 if (dx != 0) { |
| 84 input.mi.mouseData = dx; | 96 input.mi.mouseData = dx; |
| 85 input.mi.dwFlags = MOUSEEVENTF_HWHEEL; | 97 input.mi.dwFlags = MOUSEEVENTF_HWHEEL; |
| 86 SendInput(1, &input, sizeof(INPUT)); | 98 SendInput(1, &input, sizeof(INPUT)); |
| 87 } | 99 } |
| 88 if (dy != 0) { | 100 if (dy != 0) { |
| 89 input.mi.mouseData = dy; | 101 input.mi.mouseData = dy; |
| 90 input.mi.dwFlags = MOUSEEVENTF_WHEEL; | 102 input.mi.dwFlags = MOUSEEVENTF_WHEEL; |
| 91 SendInput(1, &input, sizeof(INPUT)); | 103 SendInput(1, &input, sizeof(INPUT)); |
| 92 } | 104 } |
| 93 } | 105 } |
| 94 | 106 |
| 95 void EventExecutorWin::HandleMouseButtonDown(ChromotingClientMessage* msg) { | 107 void EventExecutorWin::HandleMouseButtonDown(const MouseDownEvent& event) { |
| 96 INPUT input; | 108 INPUT input; |
| 97 input.type = INPUT_MOUSE; | 109 input.type = INPUT_MOUSE; |
| 98 input.mi.time = 0; | 110 input.mi.time = 0; |
| 99 input.mi.dx = 0; | 111 input.mi.dx = 0; |
| 100 input.mi.dy = 0; | 112 input.mi.dy = 0; |
| 101 | 113 |
| 102 MouseButton button = msg->mouse_down_event().button(); | 114 MouseButton button = event.button(); |
| 103 if (button == MouseButtonLeft) { | 115 if (button == MouseButtonLeft) { |
| 104 input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; | 116 input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; |
| 105 } else if (button == MouseButtonMiddle) { | 117 } else if (button == MouseButtonMiddle) { |
| 106 input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; | 118 input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; |
| 107 } else if (button == MouseButtonRight) { | 119 } else if (button == MouseButtonRight) { |
| 108 input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; | 120 input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; |
| 109 } else { | 121 } else { |
| 110 input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; | 122 input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; |
| 111 } | 123 } |
| 112 | 124 |
| 113 SendInput(1, &input, sizeof(INPUT)); | 125 SendInput(1, &input, sizeof(INPUT)); |
| 114 } | 126 } |
| 115 | 127 |
| 116 void EventExecutorWin::HandleMouseButtonUp(ChromotingClientMessage* msg) { | 128 void EventExecutorWin::HandleMouseButtonUp(const MouseUpEvent& event) { |
| 117 INPUT input; | 129 INPUT input; |
| 118 input.type = INPUT_MOUSE; | 130 input.type = INPUT_MOUSE; |
| 119 input.mi.time = 0; | 131 input.mi.time = 0; |
| 120 input.mi.dx = 0; | 132 input.mi.dx = 0; |
| 121 input.mi.dy = 0; | 133 input.mi.dy = 0; |
| 122 | 134 |
| 123 MouseButton button = msg->mouse_down_event().button(); | 135 MouseButton button = event.button(); |
| 124 if (button == MouseButtonLeft) { | 136 if (button == MouseButtonLeft) { |
| 125 input.mi.dwFlags = MOUSEEVENTF_LEFTUP; | 137 input.mi.dwFlags = MOUSEEVENTF_LEFTUP; |
| 126 } else if (button == MouseButtonMiddle) { | 138 } else if (button == MouseButtonMiddle) { |
| 127 input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; | 139 input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; |
| 128 } else if (button == MouseButtonRight) { | 140 } else if (button == MouseButtonRight) { |
| 129 input.mi.dwFlags = MOUSEEVENTF_RIGHTUP; | 141 input.mi.dwFlags = MOUSEEVENTF_RIGHTUP; |
| 130 } else { | 142 } else { |
| 131 input.mi.dwFlags = MOUSEEVENTF_LEFTUP; | 143 input.mi.dwFlags = MOUSEEVENTF_LEFTUP; |
| 132 } | 144 } |
| 133 | 145 |
| 134 SendInput(1, &input, sizeof(INPUT)); | 146 SendInput(1, &input, sizeof(INPUT)); |
| 135 } | 147 } |
| 136 | 148 |
| 137 void EventExecutorWin::HandleKey(ChromotingClientMessage* msg) { | 149 void EventExecutorWin::HandleKey(const KeyEvent& event) { |
| 138 int key = msg->key_event().key(); | 150 int key = event.key(); |
| 139 bool down = msg->key_event().pressed(); | 151 bool down = event.pressed(); |
| 140 | 152 |
| 141 // Calculate scan code from virtual key. | 153 // Calculate scan code from virtual key. |
| 142 HKL hkl = GetKeyboardLayout(0); | 154 HKL hkl = GetKeyboardLayout(0); |
| 143 int scan_code = MapVirtualKeyEx(key, MAPVK_VK_TO_VSC_EX, hkl); | 155 int scan_code = MapVirtualKeyEx(key, MAPVK_VK_TO_VSC_EX, hkl); |
| 144 | 156 |
| 145 INPUT input; | 157 INPUT input; |
| 146 input.type = INPUT_KEYBOARD; | 158 input.type = INPUT_KEYBOARD; |
| 147 input.ki.time = 0; | 159 input.ki.time = 0; |
| 148 input.ki.wVk = key; | 160 input.ki.wVk = key; |
| 149 input.ki.wScan = scan_code; | 161 input.ki.wScan = scan_code; |
| 150 | 162 |
| 151 // Flag to mark extended 'e0' key scancodes. Without this, the left and | 163 // Flag to mark extended 'e0' key scancodes. Without this, the left and |
| 152 // right windows keys will not be handled properly (on US keyboard). | 164 // right windows keys will not be handled properly (on US keyboard). |
| 153 if ((scan_code & 0xFF00) == 0xE000) { | 165 if ((scan_code & 0xFF00) == 0xE000) { |
| 154 input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; | 166 input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; |
| 155 } | 167 } |
| 156 | 168 |
| 157 // Flag to mark keyup events. Default is keydown. | 169 // Flag to mark keyup events. Default is keydown. |
| 158 if (!down) { | 170 if (!down) { |
| 159 input.ki.dwFlags |= KEYEVENTF_KEYUP; | 171 input.ki.dwFlags |= KEYEVENTF_KEYUP; |
| 160 } | 172 } |
| 161 | 173 |
| 162 SendInput(1, &input, sizeof(INPUT)); | 174 SendInput(1, &input, sizeof(INPUT)); |
| 163 } | 175 } |
| 164 | 176 |
| 165 } // namespace remoting | 177 } // namespace remoting |
| OLD | NEW |