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" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/message_loop_proxy.h" |
12 #include "remoting/host/capturer.h" | 13 #include "remoting/host/capturer.h" |
13 #include "remoting/host/clipboard.h" | 14 #include "remoting/host/clipboard.h" |
14 #include "remoting/proto/event.pb.h" | 15 #include "remoting/proto/event.pb.h" |
15 #include "ui/base/keycodes/keyboard_codes.h" | 16 #include "ui/base/keycodes/keyboard_codes.h" |
16 | 17 |
17 namespace remoting { | 18 namespace remoting { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 using protocol::ClipboardEvent; | 22 using protocol::ClipboardEvent; |
22 using protocol::KeyEvent; | 23 using protocol::KeyEvent; |
23 using protocol::MouseEvent; | 24 using protocol::MouseEvent; |
24 | 25 |
25 // USB to XKB keycode map table. | 26 // USB to XKB keycode map table. |
26 #define USB_KEYMAP(usb, xkb, win, mac) {usb, win} | 27 #define USB_KEYMAP(usb, xkb, win, mac) {usb, win} |
27 #include "remoting/host/usb_keycode_map.h" | 28 #include "remoting/host/usb_keycode_map.h" |
28 #undef USB_KEYMAP | 29 #undef USB_KEYMAP |
29 | 30 |
30 // A class to generate events on Windows. | 31 // A class to generate events on Windows. |
31 class EventExecutorWin : public EventExecutor { | 32 class EventExecutorWin : public EventExecutor { |
32 public: | 33 public: |
33 EventExecutorWin(MessageLoop* message_loop, Capturer* capturer); | 34 EventExecutorWin(MessageLoop* message_loop, |
| 35 base::MessageLoopProxy* ui_loop, |
| 36 Capturer* capturer); |
34 virtual ~EventExecutorWin() {} | 37 virtual ~EventExecutorWin() {} |
35 | 38 |
36 // ClipboardStub interface. | 39 // ClipboardStub interface. |
37 virtual void InjectClipboardEvent(const ClipboardEvent& event) OVERRIDE; | 40 virtual void InjectClipboardEvent(const ClipboardEvent& event) OVERRIDE; |
38 | 41 |
39 // InputStub interface. | 42 // InputStub interface. |
40 virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; | 43 virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; |
41 virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; | 44 virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; |
42 | 45 |
43 // EventExecutor interface. | 46 // EventExecutor interface. |
44 virtual void OnSessionStarted() OVERRIDE; | 47 virtual void OnSessionStarted() OVERRIDE; |
45 virtual void OnSessionFinished() OVERRIDE; | 48 virtual void OnSessionFinished() OVERRIDE; |
46 | 49 |
47 private: | 50 private: |
48 HKL GetForegroundKeyboardLayout(); | 51 HKL GetForegroundKeyboardLayout(); |
49 void HandleKey(const KeyEvent& event); | 52 void HandleKey(const KeyEvent& event); |
50 void HandleMouse(const MouseEvent& event); | 53 void HandleMouse(const MouseEvent& event); |
51 void HandleSessionStarted(); | |
52 void HandleSessionFinished(); | |
53 | 54 |
54 MessageLoop* message_loop_; | 55 MessageLoop* message_loop_; |
| 56 base::MessageLoopProxy* ui_loop_; |
55 Capturer* capturer_; | 57 Capturer* capturer_; |
56 scoped_ptr<Clipboard> clipboard_; | 58 scoped_ptr<Clipboard> clipboard_; |
57 | 59 |
58 DISALLOW_COPY_AND_ASSIGN(EventExecutorWin); | 60 DISALLOW_COPY_AND_ASSIGN(EventExecutorWin); |
59 }; | 61 }; |
60 | 62 |
61 EventExecutorWin::EventExecutorWin(MessageLoop* message_loop, | 63 EventExecutorWin::EventExecutorWin(MessageLoop* message_loop, |
| 64 base::MessageLoopProxy* ui_loop, |
62 Capturer* capturer) | 65 Capturer* capturer) |
63 : message_loop_(message_loop), | 66 : message_loop_(message_loop), |
| 67 ui_loop_(ui_loop), |
64 capturer_(capturer), | 68 capturer_(capturer), |
65 clipboard_(Clipboard::Create()) { | 69 clipboard_(Clipboard::Create()) { |
66 } | 70 } |
67 | 71 |
68 void EventExecutorWin::InjectClipboardEvent(const ClipboardEvent& event) { | 72 void EventExecutorWin::InjectClipboardEvent(const ClipboardEvent& event) { |
69 if (MessageLoop::current() != message_loop_) { | 73 if (!ui_loop_->BelongsToCurrentThread()) { |
70 message_loop_->PostTask( | 74 ui_loop_->PostTask( |
71 FROM_HERE, | 75 FROM_HERE, |
72 base::Bind(&EventExecutorWin::InjectClipboardEvent, | 76 base::Bind(&EventExecutorWin::InjectClipboardEvent, |
73 base::Unretained(this), | 77 base::Unretained(this), |
74 event)); | 78 event)); |
75 return; | 79 return; |
76 } | 80 } |
77 | 81 |
78 clipboard_->InjectClipboardEvent(event); | 82 clipboard_->InjectClipboardEvent(event); |
79 } | 83 } |
80 | 84 |
(...skipping 15 matching lines...) Expand all Loading... |
96 FROM_HERE, | 100 FROM_HERE, |
97 base::Bind(&EventExecutorWin::InjectMouseEvent, base::Unretained(this), | 101 base::Bind(&EventExecutorWin::InjectMouseEvent, base::Unretained(this), |
98 event)); | 102 event)); |
99 return; | 103 return; |
100 } | 104 } |
101 | 105 |
102 HandleMouse(event); | 106 HandleMouse(event); |
103 } | 107 } |
104 | 108 |
105 void EventExecutorWin::OnSessionStarted() { | 109 void EventExecutorWin::OnSessionStarted() { |
106 if (MessageLoop::current() != message_loop_) { | 110 if (!ui_loop_->BelongsToCurrentThread()) { |
107 message_loop_->PostTask( | 111 ui_loop_->PostTask( |
108 FROM_HERE, | 112 FROM_HERE, |
109 base::Bind(&EventExecutorWin::OnSessionStarted, | 113 base::Bind(&EventExecutorWin::OnSessionStarted, |
110 base::Unretained(this))); | 114 base::Unretained(this))); |
111 return; | 115 return; |
112 } | 116 } |
113 | 117 |
114 HandleSessionStarted(); | 118 clipboard_->Start(); |
115 } | 119 } |
116 | 120 |
117 void EventExecutorWin::OnSessionFinished() { | 121 void EventExecutorWin::OnSessionFinished() { |
118 if (MessageLoop::current() != message_loop_) { | 122 if (!ui_loop_->BelongsToCurrentThread()) { |
119 message_loop_->PostTask( | 123 ui_loop_->PostTask( |
120 FROM_HERE, | 124 FROM_HERE, |
121 base::Bind(&EventExecutorWin::OnSessionFinished, | 125 base::Bind(&EventExecutorWin::OnSessionFinished, |
122 base::Unretained(this))); | 126 base::Unretained(this))); |
123 return; | 127 return; |
124 } | 128 } |
125 | 129 |
126 HandleSessionFinished(); | 130 clipboard_->Stop(); |
127 } | 131 } |
128 | 132 |
129 HKL EventExecutorWin::GetForegroundKeyboardLayout() { | 133 HKL EventExecutorWin::GetForegroundKeyboardLayout() { |
130 HKL layout = 0; | 134 HKL layout = 0; |
131 | 135 |
132 // Can return NULL if a window is losing focus. | 136 // Can return NULL if a window is losing focus. |
133 HWND foreground = GetForegroundWindow(); | 137 HWND foreground = GetForegroundWindow(); |
134 if (foreground) { | 138 if (foreground) { |
135 // Can return 0 if the window no longer exists. | 139 // Can return 0 if the window no longer exists. |
136 DWORD thread_id = GetWindowThreadProcessId(foreground, 0); | 140 DWORD thread_id = GetWindowThreadProcessId(foreground, 0); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 button_event.mi.dwFlags = | 275 button_event.mi.dwFlags = |
272 down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; | 276 down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; |
273 } | 277 } |
274 | 278 |
275 if (SendInput(1, &button_event, sizeof(INPUT)) == 0) { | 279 if (SendInput(1, &button_event, sizeof(INPUT)) == 0) { |
276 LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse button event"; | 280 LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse button event"; |
277 } | 281 } |
278 } | 282 } |
279 } | 283 } |
280 | 284 |
281 void EventExecutorWin::HandleSessionStarted() { | |
282 clipboard_->Start(); | |
283 } | |
284 | |
285 void EventExecutorWin::HandleSessionFinished() { | |
286 clipboard_->Stop(); | |
287 } | |
288 | |
289 } // namespace | 285 } // namespace |
290 | 286 |
291 scoped_ptr<EventExecutor> EventExecutor::Create( | 287 scoped_ptr<EventExecutor> EventExecutor::Create(MessageLoop* message_loop, |
292 MessageLoop* message_loop, Capturer* capturer) { | 288 base::MessageLoopProxy* ui_loop, |
| 289 Capturer* capturer) { |
293 return scoped_ptr<EventExecutor>( | 290 return scoped_ptr<EventExecutor>( |
294 new EventExecutorWin(message_loop, capturer)); | 291 new EventExecutorWin(message_loop, ui_loop, capturer)); |
295 } | 292 } |
296 | 293 |
297 } // namespace remoting | 294 } // namespace remoting |
OLD | NEW |