| 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/host/event_executor.h" | 5 #include "remoting/host/event_executor.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // InputStub interface. | 42 // InputStub interface. |
| 43 virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; | 43 virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; |
| 44 virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; | 44 virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; |
| 45 | 45 |
| 46 // EventExecutor interface. | 46 // EventExecutor interface. |
| 47 virtual void OnSessionStarted( | 47 virtual void OnSessionStarted( |
| 48 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; | 48 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
| 49 virtual void OnSessionFinished() OVERRIDE; | 49 virtual void OnSessionFinished() OVERRIDE; |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 HKL GetForegroundKeyboardLayout(); | |
| 53 void HandleKey(const KeyEvent& event); | 52 void HandleKey(const KeyEvent& event); |
| 54 void HandleMouse(const MouseEvent& event); | 53 void HandleMouse(const MouseEvent& event); |
| 55 | 54 |
| 56 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 55 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 57 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 56 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 58 scoped_ptr<Clipboard> clipboard_; | 57 scoped_ptr<Clipboard> clipboard_; |
| 59 | 58 |
| 60 DISALLOW_COPY_AND_ASSIGN(EventExecutorWin); | 59 DISALLOW_COPY_AND_ASSIGN(EventExecutorWin); |
| 61 }; | 60 }; |
| 62 | 61 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 ui_task_runner_->PostTask( | 123 ui_task_runner_->PostTask( |
| 125 FROM_HERE, | 124 FROM_HERE, |
| 126 base::Bind(&EventExecutorWin::OnSessionFinished, | 125 base::Bind(&EventExecutorWin::OnSessionFinished, |
| 127 base::Unretained(this))); | 126 base::Unretained(this))); |
| 128 return; | 127 return; |
| 129 } | 128 } |
| 130 | 129 |
| 131 clipboard_->Stop(); | 130 clipboard_->Stop(); |
| 132 } | 131 } |
| 133 | 132 |
| 134 HKL EventExecutorWin::GetForegroundKeyboardLayout() { | |
| 135 HKL layout = 0; | |
| 136 | |
| 137 // Can return NULL if a window is losing focus. | |
| 138 HWND foreground = GetForegroundWindow(); | |
| 139 if (foreground) { | |
| 140 // Can return 0 if the window no longer exists. | |
| 141 DWORD thread_id = GetWindowThreadProcessId(foreground, 0); | |
| 142 if (thread_id) { | |
| 143 // Can return 0 if the thread no longer exists, or if we're | |
| 144 // running on Windows Vista and the window is a command-prompt. | |
| 145 layout = GetKeyboardLayout(thread_id); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 // If we couldn't determine a layout then use the system default. | |
| 150 if (!layout) { | |
| 151 SystemParametersInfo(SPI_GETDEFAULTINPUTLANG, 0, &layout, 0); | |
| 152 } | |
| 153 | |
| 154 return layout; | |
| 155 } | |
| 156 | |
| 157 void EventExecutorWin::HandleKey(const KeyEvent& event) { | 133 void EventExecutorWin::HandleKey(const KeyEvent& event) { |
| 158 // HostEventDispatcher should filter events missing the pressed field. | 134 // HostEventDispatcher should filter events missing the pressed field. |
| 159 DCHECK(event.has_pressed()); | 135 DCHECK(event.has_pressed()); |
| 136 DCHECK(event.has_usb_keycode()); |
| 160 | 137 |
| 161 // Reset the system idle suspend timeout. | 138 // Reset the system idle suspend timeout. |
| 162 SetThreadExecutionState(ES_SYSTEM_REQUIRED); | 139 SetThreadExecutionState(ES_SYSTEM_REQUIRED); |
| 163 | 140 |
| 164 // The mapping between scancodes and VKEY values depends on the foreground | |
| 165 // window's current keyboard layout. | |
| 166 HKL layout = GetForegroundKeyboardLayout(); | |
| 167 | |
| 168 // Populate the a Windows INPUT structure for the event. | 141 // Populate the a Windows INPUT structure for the event. |
| 169 INPUT input; | 142 INPUT input; |
| 170 memset(&input, 0, sizeof(input)); | 143 memset(&input, 0, sizeof(input)); |
| 171 input.type = INPUT_KEYBOARD; | 144 input.type = INPUT_KEYBOARD; |
| 172 input.ki.time = 0; | 145 input.ki.time = 0; |
| 173 input.ki.dwFlags = event.pressed() ? 0 : KEYEVENTF_KEYUP; | 146 input.ki.dwFlags = KEYEVENTF_SCANCODE; |
| 147 if (!event.pressed()) |
| 148 input.ki.dwFlags |= KEYEVENTF_KEYUP; |
| 174 | 149 |
| 175 int scancode = kInvalidKeycode; | 150 int scancode = UsbKeycodeToNativeKeycode(event.usb_keycode()); |
| 176 if (event.has_usb_keycode()) { | 151 VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode() |
| 177 // If the event contains a USB-style code, map to a Windows scancode, and | 152 << " to scancode: " << scancode << std::dec; |
| 178 // set a flag to have Windows look up the corresponding VK code. | |
| 179 input.ki.dwFlags |= KEYEVENTF_SCANCODE; | |
| 180 scancode = UsbKeycodeToNativeKeycode(event.usb_keycode()); | |
| 181 VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode() | |
| 182 << " to scancode: " << scancode << std::dec; | |
| 183 } else { | |
| 184 // If the event provides only a VKEY then use it, and map to the scancode. | |
| 185 input.ki.wVk = event.keycode(); | |
| 186 scancode = MapVirtualKeyEx(event.keycode(), MAPVK_VK_TO_VSC_EX, layout); | |
| 187 VLOG(3) << "Converting VKEY: " << std::hex << event.keycode() | |
| 188 << " to scancode: " << scancode << std::dec; | |
| 189 } | |
| 190 | 153 |
| 191 // Ignore events with no VK- or USB-keycode, or which can't be mapped. | 154 // Ignore events which can't be mapped. |
| 192 if (scancode == kInvalidKeycode) | 155 if (scancode == kInvalidKeycode) |
| 193 return; | 156 return; |
| 194 | 157 |
| 195 // Windows scancodes are only 8-bit, so store the low-order byte into the | 158 // Windows scancodes are only 8-bit, so store the low-order byte into the |
| 196 // event and set the extended flag if any high-order bits are set. The only | 159 // event and set the extended flag if any high-order bits are set. The only |
| 197 // high-order values we should see are 0xE0 or 0xE1. The extended bit usually | 160 // high-order values we should see are 0xE0 or 0xE1. The extended bit usually |
| 198 // distinguishes keys with the same meaning, e.g. left & right shift. | 161 // distinguishes keys with the same meaning, e.g. left & right shift. |
| 199 input.ki.wScan = scancode & 0xFF; | 162 input.ki.wScan = scancode & 0xFF; |
| 200 if ((scancode & 0xFF00) != 0x0000) { | 163 if ((scancode & 0xFF00) != 0x0000) { |
| 201 input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; | 164 input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } // namespace | 253 } // namespace |
| 291 | 254 |
| 292 scoped_ptr<EventExecutor> EventExecutor::Create( | 255 scoped_ptr<EventExecutor> EventExecutor::Create( |
| 293 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 256 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 294 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | 257 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
| 295 return scoped_ptr<EventExecutor>( | 258 return scoped_ptr<EventExecutor>( |
| 296 new EventExecutorWin(main_task_runner, ui_task_runner)); | 259 new EventExecutorWin(main_task_runner, ui_task_runner)); |
| 297 } | 260 } |
| 298 | 261 |
| 299 } // namespace remoting | 262 } // namespace remoting |
| OLD | NEW |