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/aura_constants.h"
15 #include "ui/aura/client/cursor_client.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/aura/window_property.h"
18 #include "ui/base/cursor/cursor_loader_win.h"
19 #include "ui/base/ime/composition_text.h"
20 #include "ui/base/ime/input_method.h"
21 #include "ui/base/ime/remote_input_method_win.h"
22 #include "ui/base/ime/text_input_client.h"
23 #include "ui/base/view_prop.h"
24 #include "ui/events/event_utils.h"
25 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
26 #include "ui/gfx/geometry/insets.h"
27 #include "ui/gfx/win/dpi.h"
28 #include "ui/metro_viewer/metro_viewer_messages.h"
34 const char* kWindowTreeHostWinKey
= "__AURA_REMOTE_WINDOW_TREE_HOST_WIN__";
36 // Sets the keystate for the virtual key passed in to down or up.
37 void SetKeyState(uint8
* key_states
, bool key_down
, uint32 virtual_key_code
) {
41 key_states
[virtual_key_code
] |= 0x80;
43 key_states
[virtual_key_code
] &= 0x7F;
46 // Sets the keyboard states for the Shift/Control/Alt/Caps lock keys.
47 void SetVirtualKeyStates(uint32 flags
) {
48 uint8 keyboard_state
[256] = {0};
49 ::GetKeyboardState(keyboard_state
);
51 SetKeyState(keyboard_state
, !!(flags
& ui::EF_SHIFT_DOWN
), VK_SHIFT
);
52 SetKeyState(keyboard_state
, !!(flags
& ui::EF_CONTROL_DOWN
), VK_CONTROL
);
53 SetKeyState(keyboard_state
, !!(flags
& ui::EF_ALT_DOWN
), VK_MENU
);
54 SetKeyState(keyboard_state
, !!(flags
& ui::EF_CAPS_LOCK_DOWN
), VK_CAPITAL
);
55 SetKeyState(keyboard_state
, !!(flags
& ui::EF_LEFT_MOUSE_BUTTON
), VK_LBUTTON
);
56 SetKeyState(keyboard_state
, !!(flags
& ui::EF_RIGHT_MOUSE_BUTTON
),
58 SetKeyState(keyboard_state
, !!(flags
& ui::EF_MIDDLE_MOUSE_BUTTON
),
61 ::SetKeyboardState(keyboard_state
);
64 void FillCompositionText(
65 const base::string16
& text
,
66 int32 selection_start
,
68 const std::vector
<metro_viewer::UnderlineInfo
>& underlines
,
69 ui::CompositionText
* composition_text
) {
70 composition_text
->Clear();
71 composition_text
->text
= text
;
72 composition_text
->selection
.set_start(selection_start
);
73 composition_text
->selection
.set_end(selection_end
);
74 composition_text
->underlines
.resize(underlines
.size());
75 for (size_t i
= 0; i
< underlines
.size(); ++i
) {
76 composition_text
->underlines
[i
].start_offset
= underlines
[i
].start_offset
;
77 composition_text
->underlines
[i
].end_offset
= underlines
[i
].end_offset
;
78 composition_text
->underlines
[i
].color
= SK_ColorBLACK
;
79 composition_text
->underlines
[i
].thick
= underlines
[i
].thick
;
80 composition_text
->underlines
[i
].background_color
= SK_ColorTRANSPARENT
;
86 RemoteWindowTreeHostWin
* g_instance
= NULL
;
89 RemoteWindowTreeHostWin
* RemoteWindowTreeHostWin::Instance() {
93 RemoteWindowTreeHostWin::RemoteWindowTreeHostWin()
94 : remote_window_(NULL
),
96 ignore_mouse_moves_until_set_cursor_ack_(0),
98 window_size_(aura::WindowTreeHost::GetNativeScreenSize()) {
101 prop_
.reset(new ui::ViewProp(NULL
, kWindowTreeHostWinKey
, this));
102 CreateCompositor(GetAcceleratedWidget());
105 RemoteWindowTreeHostWin::~RemoteWindowTreeHostWin() {
108 DCHECK_EQ(g_instance
, this);
113 bool RemoteWindowTreeHostWin::IsValid() {
114 return Instance()->remote_window_
!= NULL
;
117 void RemoteWindowTreeHostWin::SetRemoteWindowHandle(HWND remote_window
) {
118 remote_window_
= remote_window
;
121 void RemoteWindowTreeHostWin::Connected(IPC::Sender
* host
) {
122 CHECK(host_
== NULL
);
123 DCHECK(remote_window_
);
125 // Recreate the compositor for the target surface represented by the
126 // remote_window HWND.
127 CreateCompositor(remote_window_
);
131 void RemoteWindowTreeHostWin::Disconnected() {
132 // Don't CHECK here, Disconnected is called on a channel error which can
133 // happen before we're successfully Connected.
136 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
137 GetRemoteInputMethodPrivate();
138 if (remote_input_method_private
)
139 remote_input_method_private
->SetRemoteDelegate(NULL
);
141 remote_window_
= NULL
;
144 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message
& message
) {
146 IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin
, message
)
147 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved
, OnMouseMoved
)
148 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton
, OnMouseButton
)
149 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown
, OnKeyDown
)
150 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp
, OnKeyUp
)
151 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character
, OnChar
)
152 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated
,
154 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture
, OnEdgeGesture
)
155 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown
,
157 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp
,
159 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved
,
161 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck
,
163 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged
,
164 OnImeCandidatePopupChanged
)
165 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged
,
166 OnImeCompositionChanged
)
167 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted
,
169 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged
,
170 OnImeInputSourceChanged
)
171 IPC_MESSAGE_UNHANDLED(handled
= false)
172 IPC_END_MESSAGE_MAP()
176 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
177 const base::FilePath
& shortcut
,
178 const base::string16
& url
) {
181 host_
->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut
, url
));
184 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width
,
186 SetBounds(gfx::Rect(0, 0, width
, height
));
189 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
190 return ::GetForegroundWindow() == remote_window_
;
193 Window
* RemoteWindowTreeHostWin::GetAshWindow() {
197 ui::EventSource
* RemoteWindowTreeHostWin::GetEventSource() {
201 gfx::AcceleratedWidget
RemoteWindowTreeHostWin::GetAcceleratedWidget() {
203 return remote_window_
;
204 // Getting here should only happen for ash_unittests.exe and related code.
205 return ::GetDesktopWindow();
208 void RemoteWindowTreeHostWin::Show() {
209 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
210 GetRemoteInputMethodPrivate();
211 if (remote_input_method_private
)
212 remote_input_method_private
->SetRemoteDelegate(this);
215 void RemoteWindowTreeHostWin::Hide() {
219 gfx::Rect
RemoteWindowTreeHostWin::GetBounds() const {
220 return gfx::Rect(window_size_
);
223 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect
& bounds
) {
224 window_size_
= bounds
.size();
225 OnHostResized(bounds
.size());
228 gfx::Point
RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
229 return gfx::Point(0, 0);
232 void RemoteWindowTreeHostWin::SetCapture() {
235 void RemoteWindowTreeHostWin::ReleaseCapture() {
238 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor
) {
242 new MetroViewerHostMsg_SetCursor(uint64(native_cursor
.platform())));
245 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point
& location
) {
246 VLOG(1) << "In MoveCursorTo: " << location
.x() << ", " << location
.y();
250 // This function can be called in cases like when the mouse cursor is
251 // restricted within a viewport (For e.g. LockCursor) which assumes that
252 // subsequent mouse moves would be received starting with the new cursor
253 // coordinates. This is a challenge for Windows ASH for the reasons
255 // Other cases which don't expect this behavior should continue to work
258 // The mouse events are received by the viewer process and sent to the
259 // browser. If we invoke the SetCursor API here we continue to receive
260 // mouse messages from the viewer which were posted before the SetCursor
261 // API executes which messes up the state in the browser. To workaround
262 // this we invoke the SetCursor API in the viewer process and ignore
263 // mouse messages until we received an ACK from the viewer indicating that
264 // the SetCursor operation completed.
265 ignore_mouse_moves_until_set_cursor_ack_
++;
266 VLOG(1) << "In MoveCursorTo. Sending IPC";
267 host_
->Send(new MetroViewerHostMsg_SetCursorPos(location
.x(), location
.y()));
270 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show
) {
274 ui::EventProcessor
* RemoteWindowTreeHostWin::GetEventProcessor() {
278 void RemoteWindowTreeHostWin::CancelComposition() {
281 host_
->Send(new MetroViewerHostMsg_ImeCancelComposition
);
284 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
285 const std::vector
<int32
>& input_scopes
,
286 const std::vector
<gfx::Rect
>& composition_character_bounds
) {
289 std::vector
<metro_viewer::CharacterBounds
> character_bounds
;
290 for (size_t i
= 0; i
< composition_character_bounds
.size(); ++i
) {
291 const gfx::Rect
& rect
= composition_character_bounds
[i
];
292 metro_viewer::CharacterBounds bounds
;
293 bounds
.left
= rect
.x();
294 bounds
.top
= rect
.y();
295 bounds
.right
= rect
.right();
296 bounds
.bottom
= rect
.bottom();
297 character_bounds
.push_back(bounds
);
299 host_
->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
300 input_scopes
, character_bounds
));
303 gfx::Point
PointFromNativeEvent(int32 x
, int32 y
) {
304 static float scale_factor
= gfx::GetDPIScale();
305 gfx::Point
result( x
* scale_factor
, y
* scale_factor
);
309 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x
, int32 y
, int32 flags
) {
310 if (ignore_mouse_moves_until_set_cursor_ack_
)
313 gfx::Point location
= PointFromNativeEvent(x
, y
);
314 ui::MouseEvent
event(ui::ET_MOUSE_MOVED
, location
, location
,
315 ui::EventTimeForNow(), flags
, 0);
316 SendEventToProcessor(&event
);
319 void RemoteWindowTreeHostWin::OnMouseButton(
320 const MetroViewerHostMsg_MouseButtonParams
& params
) {
321 gfx::Point location
= PointFromNativeEvent(params
.x
, params
.y
);
322 ui::MouseEvent
mouse_event(
323 params
.event_type
, location
, location
, ui::EventTimeForNow(),
324 static_cast<int>(params
.flags
), static_cast<int>(params
.changed_button
));
326 SetEventFlags(params
.flags
| key_event_flags());
327 if (params
.event_type
== ui::ET_MOUSEWHEEL
) {
328 int x_offset
= params
.is_horizontal_wheel
? params
.extra
: 0;
329 int y_offset
= !params
.is_horizontal_wheel
? params
.extra
: 0;
330 ui::MouseWheelEvent
wheel_event(mouse_event
, x_offset
, y_offset
);
331 SendEventToProcessor(&wheel_event
);
332 } else if (params
.event_type
== ui::ET_MOUSE_PRESSED
) {
333 // TODO(shrikant): Ideally modify code in event.cc by adding automatic
334 // tracking of double clicks in synthetic MouseEvent constructor code.
335 // Non-synthetic MouseEvent constructor code does automatically track
336 // this. Need to use some caution while modifying synthetic constructor
337 // as many tests and other code paths depend on it and apparently
338 // specifically depend on non implicit tracking of previous mouse event.
339 if (last_mouse_click_event_
&&
340 ui::MouseEvent::IsRepeatedClickEvent(mouse_event
,
341 *last_mouse_click_event_
)) {
342 mouse_event
.SetClickCount(2);
344 mouse_event
.SetClickCount(1);
346 last_mouse_click_event_
.reset(new ui::MouseEvent(mouse_event
));
347 SendEventToProcessor(&mouse_event
);
349 SendEventToProcessor(&mouse_event
);
353 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey
,
357 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, vkey
, repeat_count
, scan_code
,
361 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey
,
365 DispatchKeyboardMessage(ui::ET_KEY_RELEASED
, vkey
, repeat_count
, scan_code
,
369 void RemoteWindowTreeHostWin::OnChar(uint32 key_code
,
373 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, key_code
, repeat_count
,
374 scan_code
, flags
, true);
377 void RemoteWindowTreeHostWin::OnWindowActivated(bool repaint
) {
379 if (repaint
&& compositor())
380 compositor()->ScheduleFullRedraw();
383 void RemoteWindowTreeHostWin::OnEdgeGesture() {
384 ui::GestureEvent
event(
388 ui::EventTimeForNow(),
389 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE
));
390 SendEventToProcessor(&event
);
393 void RemoteWindowTreeHostWin::OnTouchDown(int32 x
,
397 gfx::Point location
= PointFromNativeEvent(x
, y
);
398 ui::TouchEvent
event(ui::ET_TOUCH_PRESSED
,
401 base::TimeDelta::FromMicroseconds(timestamp
));
402 SendEventToProcessor(&event
);
405 void RemoteWindowTreeHostWin::OnTouchUp(int32 x
,
409 gfx::Point location
= PointFromNativeEvent(x
, y
);
410 ui::TouchEvent
event(ui::ET_TOUCH_RELEASED
,
413 base::TimeDelta::FromMicroseconds(timestamp
));
414 SendEventToProcessor(&event
);
417 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x
,
421 gfx::Point location
= PointFromNativeEvent(x
, y
);
422 ui::TouchEvent
event(ui::ET_TOUCH_MOVED
,
425 base::TimeDelta::FromMicroseconds(timestamp
));
426 SendEventToProcessor(&event
);
429 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
430 DCHECK_GT(ignore_mouse_moves_until_set_cursor_ack_
, 0);
431 ignore_mouse_moves_until_set_cursor_ack_
--;
434 ui::RemoteInputMethodPrivateWin
*
435 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
436 ui::InputMethod
* input_method
= GetAshWindow()->GetProperty(
437 aura::client::kRootWindowInputMethodKey
);
438 return ui::RemoteInputMethodPrivateWin::Get(input_method
);
441 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible
) {
442 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
443 GetRemoteInputMethodPrivate();
444 if (!remote_input_method_private
)
446 remote_input_method_private
->OnCandidatePopupChanged(visible
);
449 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
450 const base::string16
& text
,
451 int32 selection_start
,
453 const std::vector
<metro_viewer::UnderlineInfo
>& underlines
) {
454 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
455 GetRemoteInputMethodPrivate();
456 if (!remote_input_method_private
)
458 ui::CompositionText composition_text
;
460 text
, selection_start
, selection_end
, underlines
, &composition_text
);
461 remote_input_method_private
->OnCompositionChanged(composition_text
);
464 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16
& text
) {
465 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
466 GetRemoteInputMethodPrivate();
467 if (!remote_input_method_private
)
469 remote_input_method_private
->OnTextCommitted(text
);
472 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id
,
474 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
475 GetRemoteInputMethodPrivate();
476 if (!remote_input_method_private
)
478 remote_input_method_private
->OnInputSourceChanged(language_id
, is_ime
);
481 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type
,
487 SetEventFlags(flags
| mouse_event_flags());
488 if (base::MessageLoop::current()->IsNested()) {
489 int index
= (flags
& ui::EF_ALT_DOWN
) ? 1 : 0;
490 const int char_message
[] = {WM_CHAR
, WM_SYSCHAR
};
491 const int keydown_message
[] = {WM_KEYDOWN
, WM_SYSKEYDOWN
};
492 const int keyup_message
[] = {WM_KEYUP
, WM_SYSKEYUP
};
493 uint32 message
= is_character
494 ? char_message
[index
]
495 : (type
== ui::ET_KEY_PRESSED
? keydown_message
[index
]
496 : keyup_message
[index
]);
497 ::PostThreadMessage(::GetCurrentThreadId(),
500 repeat_count
| scan_code
>> 15);
501 } else if (is_character
) {
502 ui::KeyEvent
event(static_cast<base::char16
>(vkey
),
503 ui::KeyboardCodeForWindowsKeyCode(vkey
),
505 SendEventToProcessor(&event
);
507 ui::KeyEvent
event(type
,
508 ui::KeyboardCodeForWindowsKeyCode(vkey
),
510 SendEventToProcessor(&event
);
514 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags
) {
515 if (flags
== event_flags_
)
517 event_flags_
= flags
;
518 SetVirtualKeyStates(event_flags_
);