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 "ui/aura/remote_window_tree_host_win.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_sender.h"
14 #include "ui/aura/client/cursor_client.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/aura/window_property.h"
17 #include "ui/base/cursor/cursor_loader_win.h"
18 #include "ui/base/ime/composition_text.h"
19 #include "ui/base/ime/input_method.h"
20 #include "ui/base/ime/remote_input_method_win.h"
21 #include "ui/base/ime/text_input_client.h"
22 #include "ui/base/view_prop.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
25 #include "ui/gfx/geometry/insets.h"
26 #include "ui/gfx/win/dpi.h"
27 #include "ui/metro_viewer/metro_viewer_messages.h"
33 const char* kWindowTreeHostWinKey
= "__AURA_REMOTE_WINDOW_TREE_HOST_WIN__";
35 // Sets the keystate for the virtual key passed in to down or up.
36 void SetKeyState(uint8
* key_states
, bool key_down
, uint32 virtual_key_code
) {
40 key_states
[virtual_key_code
] |= 0x80;
42 key_states
[virtual_key_code
] &= 0x7F;
45 // Sets the keyboard states for the Shift/Control/Alt/Caps lock keys.
46 void SetVirtualKeyStates(uint32 flags
) {
47 uint8 keyboard_state
[256] = {0};
48 ::GetKeyboardState(keyboard_state
);
50 SetKeyState(keyboard_state
, !!(flags
& ui::EF_SHIFT_DOWN
), VK_SHIFT
);
51 SetKeyState(keyboard_state
, !!(flags
& ui::EF_CONTROL_DOWN
), VK_CONTROL
);
52 SetKeyState(keyboard_state
, !!(flags
& ui::EF_ALT_DOWN
), VK_MENU
);
53 SetKeyState(keyboard_state
, !!(flags
& ui::EF_CAPS_LOCK_DOWN
), VK_CAPITAL
);
54 SetKeyState(keyboard_state
, !!(flags
& ui::EF_LEFT_MOUSE_BUTTON
), VK_LBUTTON
);
55 SetKeyState(keyboard_state
, !!(flags
& ui::EF_RIGHT_MOUSE_BUTTON
),
57 SetKeyState(keyboard_state
, !!(flags
& ui::EF_MIDDLE_MOUSE_BUTTON
),
60 ::SetKeyboardState(keyboard_state
);
63 void FillCompositionText(
64 const base::string16
& text
,
65 int32 selection_start
,
67 const std::vector
<metro_viewer::UnderlineInfo
>& underlines
,
68 ui::CompositionText
* composition_text
) {
69 composition_text
->Clear();
70 composition_text
->text
= text
;
71 composition_text
->selection
.set_start(selection_start
);
72 composition_text
->selection
.set_end(selection_end
);
73 composition_text
->underlines
.resize(underlines
.size());
74 for (size_t i
= 0; i
< underlines
.size(); ++i
) {
75 composition_text
->underlines
[i
].start_offset
= underlines
[i
].start_offset
;
76 composition_text
->underlines
[i
].end_offset
= underlines
[i
].end_offset
;
77 composition_text
->underlines
[i
].color
= SK_ColorBLACK
;
78 composition_text
->underlines
[i
].thick
= underlines
[i
].thick
;
79 composition_text
->underlines
[i
].background_color
= SK_ColorTRANSPARENT
;
85 RemoteWindowTreeHostWin
* g_instance
= NULL
;
88 RemoteWindowTreeHostWin
* RemoteWindowTreeHostWin::Instance() {
92 RemoteWindowTreeHostWin::RemoteWindowTreeHostWin()
93 : remote_window_(NULL
),
95 ignore_mouse_moves_until_set_cursor_ack_(0),
97 window_size_(aura::WindowTreeHost::GetNativeScreenSize()) {
100 prop_
.reset(new ui::ViewProp(NULL
, kWindowTreeHostWinKey
, this));
101 CreateCompositor(GetAcceleratedWidget());
104 RemoteWindowTreeHostWin::~RemoteWindowTreeHostWin() {
107 DCHECK_EQ(g_instance
, this);
112 bool RemoteWindowTreeHostWin::IsValid() {
113 return Instance()->remote_window_
!= NULL
;
116 void RemoteWindowTreeHostWin::SetRemoteWindowHandle(HWND remote_window
) {
117 remote_window_
= remote_window
;
120 void RemoteWindowTreeHostWin::Connected(IPC::Sender
* host
) {
121 CHECK(host_
== NULL
);
122 DCHECK(remote_window_
);
124 // Recreate the compositor for the target surface represented by the
125 // remote_window HWND.
126 CreateCompositor(remote_window_
);
130 void RemoteWindowTreeHostWin::Disconnected() {
131 // Don't CHECK here, Disconnected is called on a channel error which can
132 // happen before we're successfully Connected.
135 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
136 GetRemoteInputMethodPrivate();
137 if (remote_input_method_private
)
138 remote_input_method_private
->SetRemoteDelegate(NULL
);
140 remote_window_
= NULL
;
143 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message
& message
) {
145 IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin
, message
)
146 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved
, OnMouseMoved
)
147 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton
, OnMouseButton
)
148 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown
, OnKeyDown
)
149 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp
, OnKeyUp
)
150 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character
, OnChar
)
151 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated
,
153 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture
, OnEdgeGesture
)
154 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown
,
156 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp
,
158 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved
,
160 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck
,
162 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged
,
163 OnImeCandidatePopupChanged
)
164 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged
,
165 OnImeCompositionChanged
)
166 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted
,
168 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged
,
169 OnImeInputSourceChanged
)
170 IPC_MESSAGE_UNHANDLED(handled
= false)
171 IPC_END_MESSAGE_MAP()
175 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
176 const base::FilePath
& shortcut
,
177 const base::string16
& url
) {
180 host_
->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut
, url
));
183 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width
,
185 SetBounds(gfx::Rect(0, 0, width
, height
));
188 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
189 return ::GetForegroundWindow() == remote_window_
;
192 Window
* RemoteWindowTreeHostWin::GetAshWindow() {
196 ui::EventSource
* RemoteWindowTreeHostWin::GetEventSource() {
200 gfx::AcceleratedWidget
RemoteWindowTreeHostWin::GetAcceleratedWidget() {
202 return remote_window_
;
203 // Getting here should only happen for ash_unittests.exe and related code.
204 return ::GetDesktopWindow();
207 void RemoteWindowTreeHostWin::ShowImpl() {
208 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
209 GetRemoteInputMethodPrivate();
210 if (remote_input_method_private
)
211 remote_input_method_private
->SetRemoteDelegate(this);
214 void RemoteWindowTreeHostWin::HideImpl() {
218 gfx::Rect
RemoteWindowTreeHostWin::GetBounds() const {
219 return gfx::Rect(window_size_
);
222 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect
& bounds
) {
223 window_size_
= bounds
.size();
224 OnHostResized(bounds
.size());
227 gfx::Point
RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
228 return gfx::Point(0, 0);
231 void RemoteWindowTreeHostWin::SetCapture() {
234 void RemoteWindowTreeHostWin::ReleaseCapture() {
237 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor
) {
241 new MetroViewerHostMsg_SetCursor(uint64(native_cursor
.platform())));
244 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point
& location
) {
245 VLOG(1) << "In MoveCursorTo: " << location
.x() << ", " << location
.y();
249 // This function can be called in cases like when the mouse cursor is
250 // restricted within a viewport (For e.g. LockCursor) which assumes that
251 // subsequent mouse moves would be received starting with the new cursor
252 // coordinates. This is a challenge for Windows ASH for the reasons
254 // Other cases which don't expect this behavior should continue to work
257 // The mouse events are received by the viewer process and sent to the
258 // browser. If we invoke the SetCursor API here we continue to receive
259 // mouse messages from the viewer which were posted before the SetCursor
260 // API executes which messes up the state in the browser. To workaround
261 // this we invoke the SetCursor API in the viewer process and ignore
262 // mouse messages until we received an ACK from the viewer indicating that
263 // the SetCursor operation completed.
264 ignore_mouse_moves_until_set_cursor_ack_
++;
265 VLOG(1) << "In MoveCursorTo. Sending IPC";
266 host_
->Send(new MetroViewerHostMsg_SetCursorPos(location
.x(), location
.y()));
269 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show
) {
273 void RemoteWindowTreeHostWin::CancelComposition() {
276 host_
->Send(new MetroViewerHostMsg_ImeCancelComposition
);
279 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
280 const std::vector
<int32
>& input_scopes
,
281 const std::vector
<gfx::Rect
>& composition_character_bounds
) {
284 std::vector
<metro_viewer::CharacterBounds
> character_bounds
;
285 for (size_t i
= 0; i
< composition_character_bounds
.size(); ++i
) {
286 const gfx::Rect
& rect
= composition_character_bounds
[i
];
287 metro_viewer::CharacterBounds bounds
;
288 bounds
.left
= rect
.x();
289 bounds
.top
= rect
.y();
290 bounds
.right
= rect
.right();
291 bounds
.bottom
= rect
.bottom();
292 character_bounds
.push_back(bounds
);
294 host_
->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
295 input_scopes
, character_bounds
));
298 gfx::Point
PointFromNativeEvent(int32 x
, int32 y
) {
299 static float scale_factor
= gfx::GetDPIScale();
300 gfx::Point
result( x
* scale_factor
, y
* scale_factor
);
304 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x
, int32 y
, int32 flags
) {
305 if (ignore_mouse_moves_until_set_cursor_ack_
)
308 gfx::Point location
= PointFromNativeEvent(x
, y
);
309 ui::MouseEvent
event(ui::ET_MOUSE_MOVED
, location
, location
,
310 ui::EventTimeForNow(), flags
, 0);
311 SendEventToProcessor(&event
);
314 void RemoteWindowTreeHostWin::OnMouseButton(
315 const MetroViewerHostMsg_MouseButtonParams
& params
) {
316 gfx::Point location
= PointFromNativeEvent(params
.x
, params
.y
);
317 ui::MouseEvent
mouse_event(
318 params
.event_type
, location
, location
, ui::EventTimeForNow(),
319 static_cast<int>(params
.flags
), static_cast<int>(params
.changed_button
));
321 SetEventFlags(params
.flags
| key_event_flags());
322 if (params
.event_type
== ui::ET_MOUSEWHEEL
) {
323 int x_offset
= params
.is_horizontal_wheel
? params
.extra
: 0;
324 int y_offset
= !params
.is_horizontal_wheel
? params
.extra
: 0;
325 ui::MouseWheelEvent
wheel_event(mouse_event
, x_offset
, y_offset
);
326 SendEventToProcessor(&wheel_event
);
327 } else if (params
.event_type
== ui::ET_MOUSE_PRESSED
) {
328 // TODO(shrikant): Ideally modify code in event.cc by adding automatic
329 // tracking of double clicks in synthetic MouseEvent constructor code.
330 // Non-synthetic MouseEvent constructor code does automatically track
331 // this. Need to use some caution while modifying synthetic constructor
332 // as many tests and other code paths depend on it and apparently
333 // specifically depend on non implicit tracking of previous mouse event.
334 if (last_mouse_click_event_
&&
335 ui::MouseEvent::IsRepeatedClickEvent(mouse_event
,
336 *last_mouse_click_event_
)) {
337 mouse_event
.SetClickCount(2);
339 mouse_event
.SetClickCount(1);
341 last_mouse_click_event_
.reset(new ui::MouseEvent(mouse_event
));
342 SendEventToProcessor(&mouse_event
);
344 SendEventToProcessor(&mouse_event
);
348 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey
,
352 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, vkey
, repeat_count
, scan_code
,
356 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey
,
360 DispatchKeyboardMessage(ui::ET_KEY_RELEASED
, vkey
, repeat_count
, scan_code
,
364 void RemoteWindowTreeHostWin::OnChar(uint32 key_code
,
368 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, key_code
, repeat_count
,
369 scan_code
, flags
, true);
372 void RemoteWindowTreeHostWin::OnWindowActivated(bool repaint
) {
374 if (repaint
&& compositor())
375 compositor()->ScheduleFullRedraw();
378 void RemoteWindowTreeHostWin::OnEdgeGesture() {
379 ui::GestureEvent
event(
383 ui::EventTimeForNow(),
384 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE
));
385 SendEventToProcessor(&event
);
388 void RemoteWindowTreeHostWin::OnTouchDown(int32 x
,
392 gfx::Point location
= PointFromNativeEvent(x
, y
);
393 ui::TouchEvent
event(ui::ET_TOUCH_PRESSED
,
396 base::TimeDelta::FromMicroseconds(timestamp
));
397 SendEventToProcessor(&event
);
400 void RemoteWindowTreeHostWin::OnTouchUp(int32 x
,
404 gfx::Point location
= PointFromNativeEvent(x
, y
);
405 ui::TouchEvent
event(ui::ET_TOUCH_RELEASED
,
408 base::TimeDelta::FromMicroseconds(timestamp
));
409 SendEventToProcessor(&event
);
412 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x
,
416 gfx::Point location
= PointFromNativeEvent(x
, y
);
417 ui::TouchEvent
event(ui::ET_TOUCH_MOVED
,
420 base::TimeDelta::FromMicroseconds(timestamp
));
421 SendEventToProcessor(&event
);
424 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
425 DCHECK_GT(ignore_mouse_moves_until_set_cursor_ack_
, 0);
426 ignore_mouse_moves_until_set_cursor_ack_
--;
429 ui::RemoteInputMethodPrivateWin
*
430 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
431 return ui::RemoteInputMethodPrivateWin::Get(GetInputMethod());
434 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible
) {
435 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
436 GetRemoteInputMethodPrivate();
437 if (!remote_input_method_private
)
439 remote_input_method_private
->OnCandidatePopupChanged(visible
);
442 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
443 const base::string16
& text
,
444 int32 selection_start
,
446 const std::vector
<metro_viewer::UnderlineInfo
>& underlines
) {
447 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
448 GetRemoteInputMethodPrivate();
449 if (!remote_input_method_private
)
451 ui::CompositionText composition_text
;
453 text
, selection_start
, selection_end
, underlines
, &composition_text
);
454 remote_input_method_private
->OnCompositionChanged(composition_text
);
457 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16
& text
) {
458 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
459 GetRemoteInputMethodPrivate();
460 if (!remote_input_method_private
)
462 remote_input_method_private
->OnTextCommitted(text
);
465 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id
,
467 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
468 GetRemoteInputMethodPrivate();
469 if (!remote_input_method_private
)
471 remote_input_method_private
->OnInputSourceChanged(language_id
, is_ime
);
474 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type
,
480 SetEventFlags(flags
| mouse_event_flags());
481 if (base::MessageLoop::current()->IsNested()) {
482 int index
= (flags
& ui::EF_ALT_DOWN
) ? 1 : 0;
483 const int char_message
[] = {WM_CHAR
, WM_SYSCHAR
};
484 const int keydown_message
[] = {WM_KEYDOWN
, WM_SYSKEYDOWN
};
485 const int keyup_message
[] = {WM_KEYUP
, WM_SYSKEYUP
};
486 uint32 message
= is_character
487 ? char_message
[index
]
488 : (type
== ui::ET_KEY_PRESSED
? keydown_message
[index
]
489 : keyup_message
[index
]);
490 ::PostThreadMessage(::GetCurrentThreadId(),
493 repeat_count
| scan_code
>> 15);
494 } else if (is_character
) {
495 ui::KeyEvent
event(static_cast<base::char16
>(vkey
),
496 ui::KeyboardCodeForWindowsKeyCode(vkey
),
498 SendEventToProcessor(&event
);
500 ui::KeyEvent
event(type
,
501 ui::KeyboardCodeForWindowsKeyCode(vkey
),
503 SendEventToProcessor(&event
);
507 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags
) {
508 if (flags
== event_flags_
)
510 event_flags_
= flags
;
511 SetVirtualKeyStates(event_flags_
);