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));
102 OnAcceleratedWidgetAvailable();
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.
128 OnAcceleratedWidgetAvailable();
132 void RemoteWindowTreeHostWin::Disconnected() {
133 // Don't CHECK here, Disconnected is called on a channel error which can
134 // happen before we're successfully Connected.
137 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
138 GetRemoteInputMethodPrivate();
139 if (remote_input_method_private
)
140 remote_input_method_private
->SetRemoteDelegate(NULL
);
142 remote_window_
= NULL
;
145 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message
& message
) {
147 IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin
, message
)
148 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved
, OnMouseMoved
)
149 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton
, OnMouseButton
)
150 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown
, OnKeyDown
)
151 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp
, OnKeyUp
)
152 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character
, OnChar
)
153 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated
,
155 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture
, OnEdgeGesture
)
156 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown
,
158 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp
,
160 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved
,
162 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck
,
164 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged
,
165 OnImeCandidatePopupChanged
)
166 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged
,
167 OnImeCompositionChanged
)
168 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted
,
170 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged
,
171 OnImeInputSourceChanged
)
172 IPC_MESSAGE_UNHANDLED(handled
= false)
173 IPC_END_MESSAGE_MAP()
177 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
178 const base::FilePath
& shortcut
,
179 const base::string16
& url
) {
182 host_
->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut
, url
));
185 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width
,
187 SetBounds(gfx::Rect(0, 0, width
, height
));
190 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
191 return ::GetForegroundWindow() == remote_window_
;
194 Window
* RemoteWindowTreeHostWin::GetAshWindow() {
198 ui::EventSource
* RemoteWindowTreeHostWin::GetEventSource() {
202 gfx::AcceleratedWidget
RemoteWindowTreeHostWin::GetAcceleratedWidget() {
204 return remote_window_
;
205 // Getting here should only happen for ash_unittests.exe and related code.
206 return ::GetDesktopWindow();
209 void RemoteWindowTreeHostWin::ShowImpl() {
210 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
211 GetRemoteInputMethodPrivate();
212 if (remote_input_method_private
)
213 remote_input_method_private
->SetRemoteDelegate(this);
216 void RemoteWindowTreeHostWin::HideImpl() {
220 gfx::Rect
RemoteWindowTreeHostWin::GetBounds() const {
221 return gfx::Rect(window_size_
);
224 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect
& bounds
) {
225 window_size_
= bounds
.size();
226 OnHostResized(bounds
.size());
229 gfx::Point
RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
230 return gfx::Point(0, 0);
233 void RemoteWindowTreeHostWin::SetCapture() {
236 void RemoteWindowTreeHostWin::ReleaseCapture() {
239 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor
) {
243 new MetroViewerHostMsg_SetCursor(uint64(native_cursor
.platform())));
246 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point
& location
) {
247 VLOG(1) << "In MoveCursorTo: " << location
.x() << ", " << location
.y();
251 // This function can be called in cases like when the mouse cursor is
252 // restricted within a viewport (For e.g. LockCursor) which assumes that
253 // subsequent mouse moves would be received starting with the new cursor
254 // coordinates. This is a challenge for Windows ASH for the reasons
256 // Other cases which don't expect this behavior should continue to work
259 // The mouse events are received by the viewer process and sent to the
260 // browser. If we invoke the SetCursor API here we continue to receive
261 // mouse messages from the viewer which were posted before the SetCursor
262 // API executes which messes up the state in the browser. To workaround
263 // this we invoke the SetCursor API in the viewer process and ignore
264 // mouse messages until we received an ACK from the viewer indicating that
265 // the SetCursor operation completed.
266 ignore_mouse_moves_until_set_cursor_ack_
++;
267 VLOG(1) << "In MoveCursorTo. Sending IPC";
268 host_
->Send(new MetroViewerHostMsg_SetCursorPos(location
.x(), location
.y()));
271 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show
) {
275 void RemoteWindowTreeHostWin::CancelComposition() {
278 host_
->Send(new MetroViewerHostMsg_ImeCancelComposition
);
281 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
282 const std::vector
<int32
>& input_scopes
,
283 const std::vector
<gfx::Rect
>& composition_character_bounds
) {
286 std::vector
<metro_viewer::CharacterBounds
> character_bounds
;
287 for (size_t i
= 0; i
< composition_character_bounds
.size(); ++i
) {
288 const gfx::Rect
& rect
= composition_character_bounds
[i
];
289 metro_viewer::CharacterBounds bounds
;
290 bounds
.left
= rect
.x();
291 bounds
.top
= rect
.y();
292 bounds
.right
= rect
.right();
293 bounds
.bottom
= rect
.bottom();
294 character_bounds
.push_back(bounds
);
296 host_
->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
297 input_scopes
, character_bounds
));
300 gfx::Point
PointFromNativeEvent(int32 x
, int32 y
) {
301 static float scale_factor
= gfx::GetDPIScale();
302 gfx::Point
result( x
* scale_factor
, y
* scale_factor
);
306 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x
, int32 y
, int32 flags
) {
307 if (ignore_mouse_moves_until_set_cursor_ack_
)
310 gfx::Point location
= PointFromNativeEvent(x
, y
);
311 ui::MouseEvent
event(ui::ET_MOUSE_MOVED
, location
, location
,
312 ui::EventTimeForNow(), flags
, 0);
313 SendEventToProcessor(&event
);
316 void RemoteWindowTreeHostWin::OnMouseButton(
317 const MetroViewerHostMsg_MouseButtonParams
& params
) {
318 gfx::Point location
= PointFromNativeEvent(params
.x
, params
.y
);
319 ui::MouseEvent
mouse_event(
320 params
.event_type
, location
, location
, ui::EventTimeForNow(),
321 static_cast<int>(params
.flags
), static_cast<int>(params
.changed_button
));
323 SetEventFlags(params
.flags
| key_event_flags());
324 if (params
.event_type
== ui::ET_MOUSEWHEEL
) {
325 int x_offset
= params
.is_horizontal_wheel
? params
.extra
: 0;
326 int y_offset
= !params
.is_horizontal_wheel
? params
.extra
: 0;
327 ui::MouseWheelEvent
wheel_event(mouse_event
, x_offset
, y_offset
);
328 SendEventToProcessor(&wheel_event
);
329 } else if (params
.event_type
== ui::ET_MOUSE_PRESSED
) {
330 // TODO(shrikant): Ideally modify code in event.cc by adding automatic
331 // tracking of double clicks in synthetic MouseEvent constructor code.
332 // Non-synthetic MouseEvent constructor code does automatically track
333 // this. Need to use some caution while modifying synthetic constructor
334 // as many tests and other code paths depend on it and apparently
335 // specifically depend on non implicit tracking of previous mouse event.
336 if (last_mouse_click_event_
&&
337 ui::MouseEvent::IsRepeatedClickEvent(mouse_event
,
338 *last_mouse_click_event_
)) {
339 mouse_event
.SetClickCount(2);
341 mouse_event
.SetClickCount(1);
343 last_mouse_click_event_
.reset(new ui::MouseEvent(mouse_event
));
344 SendEventToProcessor(&mouse_event
);
346 SendEventToProcessor(&mouse_event
);
350 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey
,
354 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, vkey
, repeat_count
, scan_code
,
358 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey
,
362 DispatchKeyboardMessage(ui::ET_KEY_RELEASED
, vkey
, repeat_count
, scan_code
,
366 void RemoteWindowTreeHostWin::OnChar(uint32 key_code
,
370 DispatchKeyboardMessage(ui::ET_KEY_PRESSED
, key_code
, repeat_count
,
371 scan_code
, flags
, true);
374 void RemoteWindowTreeHostWin::OnWindowActivated(bool repaint
) {
376 if (repaint
&& compositor())
377 compositor()->ScheduleFullRedraw();
380 void RemoteWindowTreeHostWin::OnEdgeGesture() {
381 ui::GestureEvent
event(
385 ui::EventTimeForNow(),
386 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE
));
387 SendEventToProcessor(&event
);
390 void RemoteWindowTreeHostWin::OnTouchDown(int32 x
,
394 gfx::Point location
= PointFromNativeEvent(x
, y
);
395 ui::TouchEvent
event(ui::ET_TOUCH_PRESSED
,
398 base::TimeDelta::FromMicroseconds(timestamp
));
399 SendEventToProcessor(&event
);
402 void RemoteWindowTreeHostWin::OnTouchUp(int32 x
,
406 gfx::Point location
= PointFromNativeEvent(x
, y
);
407 ui::TouchEvent
event(ui::ET_TOUCH_RELEASED
,
410 base::TimeDelta::FromMicroseconds(timestamp
));
411 SendEventToProcessor(&event
);
414 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x
,
418 gfx::Point location
= PointFromNativeEvent(x
, y
);
419 ui::TouchEvent
event(ui::ET_TOUCH_MOVED
,
422 base::TimeDelta::FromMicroseconds(timestamp
));
423 SendEventToProcessor(&event
);
426 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
427 DCHECK_GT(ignore_mouse_moves_until_set_cursor_ack_
, 0);
428 ignore_mouse_moves_until_set_cursor_ack_
--;
431 ui::RemoteInputMethodPrivateWin
*
432 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
433 return ui::RemoteInputMethodPrivateWin::Get(GetInputMethod());
436 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible
) {
437 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
438 GetRemoteInputMethodPrivate();
439 if (!remote_input_method_private
)
441 remote_input_method_private
->OnCandidatePopupChanged(visible
);
444 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
445 const base::string16
& text
,
446 int32 selection_start
,
448 const std::vector
<metro_viewer::UnderlineInfo
>& underlines
) {
449 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
450 GetRemoteInputMethodPrivate();
451 if (!remote_input_method_private
)
453 ui::CompositionText composition_text
;
455 text
, selection_start
, selection_end
, underlines
, &composition_text
);
456 remote_input_method_private
->OnCompositionChanged(composition_text
);
459 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16
& text
) {
460 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
461 GetRemoteInputMethodPrivate();
462 if (!remote_input_method_private
)
464 remote_input_method_private
->OnTextCommitted(text
);
467 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id
,
469 ui::RemoteInputMethodPrivateWin
* remote_input_method_private
=
470 GetRemoteInputMethodPrivate();
471 if (!remote_input_method_private
)
473 remote_input_method_private
->OnInputSourceChanged(language_id
, is_ime
);
476 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type
,
482 SetEventFlags(flags
| mouse_event_flags());
483 if (base::MessageLoop::current()->IsNested()) {
484 int index
= (flags
& ui::EF_ALT_DOWN
) ? 1 : 0;
485 const int char_message
[] = {WM_CHAR
, WM_SYSCHAR
};
486 const int keydown_message
[] = {WM_KEYDOWN
, WM_SYSKEYDOWN
};
487 const int keyup_message
[] = {WM_KEYUP
, WM_SYSKEYUP
};
488 uint32 message
= is_character
489 ? char_message
[index
]
490 : (type
== ui::ET_KEY_PRESSED
? keydown_message
[index
]
491 : keyup_message
[index
]);
492 ::PostThreadMessage(::GetCurrentThreadId(),
495 repeat_count
| scan_code
>> 15);
496 } else if (is_character
) {
497 ui::KeyEvent
event(static_cast<base::char16
>(vkey
),
498 ui::KeyboardCodeForWindowsKeyCode(vkey
),
500 SendEventToProcessor(&event
);
502 ui::KeyEvent
event(type
,
503 ui::KeyboardCodeForWindowsKeyCode(vkey
),
505 SendEventToProcessor(&event
);
509 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags
) {
510 if (flags
== event_flags_
)
512 event_flags_
= flags
;
513 SetVirtualKeyStates(event_flags_
);