Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / win8 / metro_driver / metro_driver_win7.cc
blobace0d940e6baff899ceeb50e8d63a043e5d93f10
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 "stdafx.h"
6 #include <corewindow.h>
8 #include "base/logging.h"
9 #include "ui/gfx/geometry/safe_integer_conversions.h"
10 #include "ui/gfx/win/msg_util.h"
12 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
13 int g_window_count = 0;
15 extern float GetModernUIScale();
17 LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
18 WPARAM wparam, LPARAM lparam) {
19 PAINTSTRUCT ps;
20 HDC hdc;
21 switch (message) {
22 case WM_CREATE:
23 ++g_window_count;
24 break;
25 case WM_PAINT:
26 hdc = ::BeginPaint(hwnd, &ps);
27 ::EndPaint(hwnd, &ps);
28 break;
29 case WM_CLOSE:
30 ::DestroyWindow(hwnd);
31 break;
32 case WM_DESTROY:
33 --g_window_count;
34 if (!g_window_count)
35 ::PostQuitMessage(0);
36 break;
37 // Always allow Chrome to set the cursor.
38 case WM_SETCURSOR:
39 return 1;
40 default:
41 return ::DefWindowProc(hwnd, message, wparam, lparam);
43 return 0;
46 HWND CreateMetroTopLevelWindow() {
47 HINSTANCE hInst = reinterpret_cast<HINSTANCE>(&__ImageBase);
48 WNDCLASSEXW wcex;
49 wcex.cbSize = sizeof(wcex);
50 wcex.style = CS_HREDRAW | CS_VREDRAW;
51 wcex.lpfnWndProc = WndProc;
52 wcex.cbClsExtra = 0;
53 wcex.cbWndExtra = 0;
54 wcex.hInstance = hInst;
55 wcex.hIcon = 0;
56 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
57 wcex.hbrBackground = (HBRUSH)(COLOR_INACTIVECAPTION+1);
58 wcex.lpszMenuName = 0;
59 wcex.lpszClassName = L"Windows.UI.Core.CoreWindow";
60 wcex.hIconSm = 0;
62 HWND hwnd = ::CreateWindowExW(0,
63 MAKEINTATOM(::RegisterClassExW(&wcex)),
64 L"metro_win7",
65 WS_POPUP | WS_VISIBLE,
66 0, 0, 1600, 900,
67 NULL, NULL, hInst, NULL);
68 return hwnd;
71 typedef winfoundtn::ITypedEventHandler<
72 winapp::Core::CoreApplicationView*,
73 winapp::Activation::IActivatedEventArgs*> ActivatedHandler;
75 typedef winfoundtn::ITypedEventHandler<
76 winui::Core::CoreWindow*,
77 winui::Core::WindowActivatedEventArgs*> WindowActivatedHandler;
79 typedef winfoundtn::ITypedEventHandler<
80 winui::Core::CoreWindow*,
81 winui::Core::AutomationProviderRequestedEventArgs*>
82 AutomationProviderHandler;
84 typedef winfoundtn::ITypedEventHandler<
85 winui::Core::CoreWindow*,
86 winui::Core::CharacterReceivedEventArgs*> CharEventHandler;
88 typedef winfoundtn::ITypedEventHandler<
89 winui::Core::CoreWindow*,
90 winui::Core::CoreWindowEventArgs*> CoreWindowEventHandler;
92 typedef winfoundtn::ITypedEventHandler<
93 winui::Core::CoreWindow*,
94 winui::Core::InputEnabledEventArgs*> InputEnabledEventHandler;
96 typedef winfoundtn::ITypedEventHandler<
97 winui::Core::CoreWindow*,
98 winui::Core::KeyEventArgs*> KeyEventHandler;
100 typedef winfoundtn::ITypedEventHandler<
101 winui::Core::CoreWindow*,
102 winui::Core::PointerEventArgs*> PointerEventHandler;
104 typedef winfoundtn::ITypedEventHandler<
105 winui::Core::CoreWindow*,
106 winui::Core::WindowSizeChangedEventArgs*> SizeChangedHandler;
108 typedef winfoundtn::ITypedEventHandler<
109 winui::Core::CoreWindow*,
110 winui::Core::TouchHitTestingEventArgs*> TouchHitTestHandler;
112 typedef winfoundtn::ITypedEventHandler<
113 winui::Core::CoreWindow*,
114 winui::Core::VisibilityChangedEventArgs*> VisibilityChangedHandler;
116 typedef winfoundtn::ITypedEventHandler<
117 winui::Core::CoreDispatcher*,
118 winui::Core::AcceleratorKeyEventArgs*> AcceleratorKeyEventHandler;
120 // This interface is implemented by classes which handle mouse and keyboard
121 // input.
122 class InputHandler {
123 public:
124 InputHandler() {}
125 virtual ~InputHandler() {}
127 virtual bool HandleKeyboardMessage(const MSG& msg) = 0;
128 virtual bool HandleMouseMessage(const MSG& msg) = 0;
130 private:
131 DISALLOW_COPY_AND_ASSIGN(InputHandler);
134 // This class implements the winrt interfaces corresponding to mouse input.
135 class MouseEvent : public mswr::RuntimeClass<
136 winui::Core::IPointerEventArgs,
137 winui::Input::IPointerPoint,
138 winui::Input::IPointerPointProperties,
139 windevs::Input::IPointerDevice> {
140 public:
141 MouseEvent(const MSG& msg)
142 : msg_(msg) {
145 // IPointerEventArgs implementation.
146 virtual HRESULT STDMETHODCALLTYPE get_CurrentPoint(
147 winui::Input::IPointerPoint** point) {
148 return QueryInterface(winui::Input::IID_IPointerPoint,
149 reinterpret_cast<void**>(point));
152 virtual HRESULT STDMETHODCALLTYPE get_KeyModifiers(
153 winsys::VirtualKeyModifiers* modifiers) {
154 return E_NOTIMPL;
157 virtual HRESULT STDMETHODCALLTYPE GetIntermediatePoints(
158 winfoundtn::Collections::IVector<winui::Input::PointerPoint*>** points) {
159 return E_NOTIMPL;
162 // IPointerPoint implementation.
163 virtual HRESULT STDMETHODCALLTYPE get_PointerDevice(
164 windevs::Input::IPointerDevice** pointer_device) {
165 return QueryInterface(windevs::Input::IID_IPointerDevice,
166 reinterpret_cast<void**>(pointer_device));
169 virtual HRESULT STDMETHODCALLTYPE get_Position(winfoundtn::Point* position) {
170 static float scale = GetModernUIScale();
171 // Scale down the points here as they are scaled up on the other side.
172 position->X = gfx::ToRoundedInt(CR_GET_X_LPARAM(msg_.lParam) / scale);
173 position->Y = gfx::ToRoundedInt(CR_GET_Y_LPARAM(msg_.lParam) / scale);
174 return S_OK;
177 virtual HRESULT STDMETHODCALLTYPE get_PointerId(uint32* pointer_id) {
178 // TODO(ananta)
179 // Implement this properly.
180 *pointer_id = 1;
181 return S_OK;
184 virtual HRESULT STDMETHODCALLTYPE get_Timestamp(uint64* timestamp) {
185 *timestamp = msg_.time;
186 return S_OK;
189 virtual HRESULT STDMETHODCALLTYPE get_Properties(
190 winui::Input::IPointerPointProperties** properties) {
191 return QueryInterface(winui::Input::IID_IPointerPointProperties,
192 reinterpret_cast<void**>(properties));
195 virtual HRESULT STDMETHODCALLTYPE get_RawPosition(
196 winfoundtn::Point* position) {
197 return E_NOTIMPL;
200 virtual HRESULT STDMETHODCALLTYPE get_FrameId(uint32* frame_id) {
201 return E_NOTIMPL;
204 virtual HRESULT STDMETHODCALLTYPE get_IsInContact(boolean* in_contact) {
205 return E_NOTIMPL;
208 // IPointerPointProperties implementation.
209 virtual HRESULT STDMETHODCALLTYPE get_PointerUpdateKind(
210 winui::Input::PointerUpdateKind* update_kind) {
211 // TODO(ananta)
212 // There is no WM_POINTERUPDATE equivalent on Windows 7. Look into
213 // equivalents.
214 if (msg_.message == WM_LBUTTONDOWN) {
215 *update_kind = winui::Input::PointerUpdateKind_LeftButtonPressed;
216 } else if (msg_.message == WM_RBUTTONDOWN) {
217 *update_kind = winui::Input::PointerUpdateKind_RightButtonPressed;
218 } else if (msg_.message == WM_MBUTTONDOWN) {
219 *update_kind = winui::Input::PointerUpdateKind_MiddleButtonPressed;
220 } else if (msg_.message == WM_LBUTTONUP) {
221 *update_kind = winui::Input::PointerUpdateKind_LeftButtonReleased;
222 } else if (msg_.message == WM_RBUTTONUP) {
223 *update_kind = winui::Input::PointerUpdateKind_RightButtonReleased;
224 } else if (msg_.message == WM_MBUTTONUP) {
225 *update_kind = winui::Input::PointerUpdateKind_MiddleButtonReleased;
227 return S_OK;
230 virtual HRESULT STDMETHODCALLTYPE get_IsLeftButtonPressed(
231 boolean* left_button_pressed) {
232 *left_button_pressed = msg_.wParam & MK_LBUTTON ? true : false;
233 return S_OK;
236 virtual HRESULT STDMETHODCALLTYPE get_IsRightButtonPressed(
237 boolean* right_button_pressed) {
238 *right_button_pressed = msg_.wParam & MK_RBUTTON ? true : false;
239 return S_OK;
242 virtual HRESULT STDMETHODCALLTYPE get_IsMiddleButtonPressed(
243 boolean* middle_button_pressed) {
244 *middle_button_pressed = msg_.wParam & MK_MBUTTON ? true : false;
245 return S_OK;
248 virtual HRESULT STDMETHODCALLTYPE get_IsHorizontalMouseWheel(
249 boolean* is_horizontal_mouse_wheel) {
250 *is_horizontal_mouse_wheel =
251 (msg_.message == WM_MOUSEHWHEEL) ? true : false;
252 return S_OK;
255 virtual HRESULT STDMETHODCALLTYPE get_MouseWheelDelta(int* delta) {
256 if (msg_.message == WM_MOUSEWHEEL || msg_.message == WM_MOUSEHWHEEL) {
257 *delta = GET_WHEEL_DELTA_WPARAM(msg_.wParam);
258 return S_OK;
259 } else {
260 return S_FALSE;
264 virtual HRESULT STDMETHODCALLTYPE get_Pressure(float* pressure) {
265 return E_NOTIMPL;
268 virtual HRESULT STDMETHODCALLTYPE get_IsInverted(boolean* inverted) {
269 return E_NOTIMPL;
272 virtual HRESULT STDMETHODCALLTYPE get_IsEraser(boolean* is_eraser) {
273 return E_NOTIMPL;
276 virtual HRESULT STDMETHODCALLTYPE get_Orientation(float* orientation) {
277 return E_NOTIMPL;
280 virtual HRESULT STDMETHODCALLTYPE get_XTilt(float* x_tilt) {
281 return E_NOTIMPL;
284 virtual HRESULT STDMETHODCALLTYPE get_YTilt(float* y_tilt) {
285 return E_NOTIMPL;
288 virtual HRESULT STDMETHODCALLTYPE get_Twist(float* twist) {
289 return E_NOTIMPL;
292 virtual HRESULT STDMETHODCALLTYPE get_ContactRect(winfoundtn::Rect* rect) {
293 return E_NOTIMPL;
296 virtual HRESULT STDMETHODCALLTYPE get_ContactRectRaw(winfoundtn::Rect* rect) {
297 return E_NOTIMPL;
300 virtual HRESULT STDMETHODCALLTYPE get_TouchConfidence(boolean* confidence) {
301 return E_NOTIMPL;
304 virtual HRESULT STDMETHODCALLTYPE get_IsPrimary(boolean* is_primary) {
305 return E_NOTIMPL;
308 virtual HRESULT STDMETHODCALLTYPE get_IsInRange(boolean* is_in_range) {
309 return E_NOTIMPL;
312 virtual HRESULT STDMETHODCALLTYPE get_IsCanceled(boolean* is_canceled) {
313 return E_NOTIMPL;
316 virtual HRESULT STDMETHODCALLTYPE get_IsBarrelButtonPressed(
317 boolean* is_barrel_button_pressed) {
318 return E_NOTIMPL;
321 virtual HRESULT STDMETHODCALLTYPE get_IsXButton1Pressed(
322 boolean* is_xbutton1_pressed) {
323 return E_NOTIMPL;
326 virtual HRESULT STDMETHODCALLTYPE get_IsXButton2Pressed(
327 boolean* is_xbutton2_pressed) {
328 return E_NOTIMPL;
331 virtual HRESULT STDMETHODCALLTYPE HasUsage(uint32 usage_page,
332 uint32 usage_id,
333 boolean* has_usage) {
334 return E_NOTIMPL;
337 virtual HRESULT STDMETHODCALLTYPE GetUsageValue(uint32 usage_page,
338 uint32 usage_id,
339 int32* usage_value) {
340 return E_NOTIMPL;
343 // IPointerDevice implementation.
344 virtual HRESULT STDMETHODCALLTYPE get_PointerDeviceType(
345 windevs::Input::PointerDeviceType* device_type) {
346 if (msg_.message == WM_TOUCH) {
347 *device_type = windevs::Input::PointerDeviceType_Touch;
348 } else {
349 *device_type = windevs::Input::PointerDeviceType_Mouse;
351 return S_OK;
354 virtual HRESULT STDMETHODCALLTYPE get_IsIntegrated(boolean* is_integrated) {
355 return E_NOTIMPL;
358 virtual HRESULT STDMETHODCALLTYPE get_MaxContacts(uint32* contacts) {
359 return E_NOTIMPL;
362 virtual HRESULT STDMETHODCALLTYPE get_PhysicalDeviceRect(
363 winfoundtn::Rect* rect) {
364 return E_NOTIMPL;
367 virtual HRESULT STDMETHODCALLTYPE get_ScreenRect(winfoundtn::Rect* rect) {
368 return E_NOTIMPL;
371 virtual HRESULT STDMETHODCALLTYPE get_SupportedUsages(
372 winfoundtn::Collections::IVectorView<
373 windevs::Input::PointerDeviceUsage>** usages) {
374 return E_NOTIMPL;
377 private:
378 MSG msg_;
380 DISALLOW_COPY_AND_ASSIGN(MouseEvent);
383 // This class implements the winrt interfaces needed to support keyboard
384 // character and system character messages.
385 class KeyEvent : public mswr::RuntimeClass<
386 winui::Core::IKeyEventArgs,
387 winui::Core::ICharacterReceivedEventArgs,
388 winui::Core::IAcceleratorKeyEventArgs> {
389 public:
390 KeyEvent(const MSG& msg)
391 : msg_(msg) {}
393 // IKeyEventArgs implementation.
394 virtual HRESULT STDMETHODCALLTYPE get_VirtualKey(
395 winsys::VirtualKey* virtual_key) {
396 *virtual_key = static_cast<winsys::VirtualKey>(msg_.wParam);
397 return S_OK;
400 virtual HRESULT STDMETHODCALLTYPE get_KeyStatus(
401 winui::Core::CorePhysicalKeyStatus* key_status) {
402 // As per msdn documentation for the keyboard messages.
403 key_status->RepeatCount = msg_.lParam & 0x0000FFFF;
404 key_status->ScanCode = (msg_.lParam >> 16) & 0x00FF;
405 key_status->IsExtendedKey = (msg_.lParam & (1 << 24));
406 key_status->IsMenuKeyDown = (msg_.lParam & (1 << 29));
407 key_status->WasKeyDown = (msg_.lParam & (1 << 30));
408 key_status->IsKeyReleased = (msg_.lParam & (1 << 31));
409 return S_OK;
412 // ICharacterReceivedEventArgs implementation.
413 virtual HRESULT STDMETHODCALLTYPE get_KeyCode(uint32* key_code) {
414 *key_code = msg_.wParam;
415 return S_OK;
418 // IAcceleratorKeyEventArgs implementation.
419 virtual HRESULT STDMETHODCALLTYPE get_EventType(
420 winui::Core::CoreAcceleratorKeyEventType* event_type) {
421 if (msg_.message == WM_SYSKEYDOWN) {
422 *event_type = winui::Core::CoreAcceleratorKeyEventType_SystemKeyDown;
423 } else if (msg_.message == WM_SYSKEYUP) {
424 *event_type = winui::Core::CoreAcceleratorKeyEventType_SystemKeyUp;
425 } else if (msg_.message == WM_SYSCHAR) {
426 *event_type = winui::Core::CoreAcceleratorKeyEventType_SystemCharacter;
428 return S_OK;
431 private:
432 MSG msg_;
435 // The following classes are the emulation of the WinRT system as exposed
436 // to metro applications. There is one application (ICoreApplication) which
437 // contains a series of Views (ICoreApplicationView) each one of them
438 // containing a CoreWindow which represents a surface that can drawn to
439 // and that receives events.
441 // Here is the general dependency hierachy in terms of interfaces:
443 // IFrameworkViewSource --> IFrameworkView
444 // ^ |
445 // | | metro app
446 // ---------------------------------------------------------------------
447 // | | winRT system
448 // | v
449 // ICoreApplication ICoreApplicationView
450 // |
451 // v
452 // ICoreWindow -----> ICoreWindowInterop
453 // | |
454 // | |
455 // v V
456 // ICoreDispatcher <==> real HWND
458 class CoreDispatcherEmulation :
459 public mswr::RuntimeClass<
460 winui::Core::ICoreDispatcher,
461 winui::Core::ICoreAcceleratorKeys> {
462 public:
463 CoreDispatcherEmulation(InputHandler* input_handler)
464 : input_handler_(input_handler),
465 accelerator_key_event_handler_(NULL) {}
467 // ICoreDispatcher implementation:
468 virtual HRESULT STDMETHODCALLTYPE get_HasThreadAccess(boolean* value) {
469 return S_OK;
472 virtual HRESULT STDMETHODCALLTYPE ProcessEvents(
473 winui::Core::CoreProcessEventsOption options) {
474 // We don't support the other message pump modes. So we basically enter a
475 // traditional message loop that we only exit a teardown.
476 if (options != winui::Core::CoreProcessEventsOption_ProcessUntilQuit)
477 return E_FAIL;
479 MSG msg = {0};
480 while((::GetMessage(&msg, NULL, 0, 0) != 0) && g_window_count > 0) {
481 ProcessInputMessage(msg);
482 ::TranslateMessage(&msg);
483 ::DispatchMessage(&msg);
485 // TODO(cpu): figure what to do with msg.WParam which we would normally
486 // return here.
487 return S_OK;
490 virtual HRESULT STDMETHODCALLTYPE RunAsync(
491 winui::Core::CoreDispatcherPriority priority,
492 winui::Core::IDispatchedHandler *agileCallback,
493 ABI::Windows::Foundation::IAsyncAction** asyncAction) {
494 return S_OK;
497 virtual HRESULT STDMETHODCALLTYPE RunIdleAsync(
498 winui::Core::IIdleDispatchedHandler *agileCallback,
499 winfoundtn::IAsyncAction** asyncAction) {
500 return S_OK;
503 // ICoreAcceleratorKeys implementation:
504 virtual HRESULT STDMETHODCALLTYPE add_AcceleratorKeyActivated(
505 AcceleratorKeyEventHandler* handler,
506 EventRegistrationToken *pCookie) {
507 accelerator_key_event_handler_ = handler;
508 accelerator_key_event_handler_->AddRef();
509 return S_OK;
512 virtual HRESULT STDMETHODCALLTYPE remove_AcceleratorKeyActivated(
513 EventRegistrationToken cookie) {
514 accelerator_key_event_handler_->Release();
515 accelerator_key_event_handler_ = NULL;
516 return S_OK;
519 private:
520 bool ProcessInputMessage(const MSG& msg) {
521 // Poor man's way of dispatching input events.
522 bool ret = false;
523 if (input_handler_) {
524 if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST)) {
525 if ((msg.message == WM_SYSKEYDOWN) || (msg.message == WM_SYSKEYUP) ||
526 msg.message == WM_SYSCHAR) {
527 ret = HandleSystemKeys(msg);
528 } else {
529 ret = input_handler_->HandleKeyboardMessage(msg);
531 } else if ((msg.message >= WM_MOUSEFIRST) &&
532 (msg.message <= WM_MOUSELAST)) {
533 ret = input_handler_->HandleMouseMessage(msg);
536 return ret;
539 bool HandleSystemKeys(const MSG& msg) {
540 mswr::ComPtr<winui::Core::IAcceleratorKeyEventArgs> event_args;
541 event_args = mswr::Make<KeyEvent>(msg);
542 accelerator_key_event_handler_->Invoke(this, event_args.Get());
543 return true;
546 InputHandler* input_handler_;
547 AcceleratorKeyEventHandler* accelerator_key_event_handler_;
550 class CoreWindowEmulation
551 : public mswr::RuntimeClass<
552 mswr::RuntimeClassFlags<mswr::WinRtClassicComMix>,
553 winui::Core::ICoreWindow, ICoreWindowInterop>,
554 public InputHandler {
555 public:
556 CoreWindowEmulation()
557 : core_hwnd_(NULL),
558 mouse_moved_handler_(NULL),
559 mouse_capture_lost_handler_(NULL),
560 mouse_pressed_handler_(NULL),
561 mouse_released_handler_(NULL),
562 mouse_entered_handler_(NULL),
563 mouse_exited_handler_(NULL),
564 mouse_wheel_changed_handler_(NULL),
565 key_down_handler_(NULL),
566 key_up_handler_(NULL),
567 character_received_handler_(NULL) {
568 dispatcher_ = mswr::Make<CoreDispatcherEmulation>(this);
569 core_hwnd_ = CreateMetroTopLevelWindow();
572 ~CoreWindowEmulation() {
573 if (core_hwnd_)
574 ::DestroyWindow(core_hwnd_);
577 // ICoreWindow implementation:
578 virtual HRESULT STDMETHODCALLTYPE get_AutomationHostProvider(
579 IInspectable** value) {
580 return S_OK;
583 virtual HRESULT STDMETHODCALLTYPE get_Bounds(
584 winfoundtn::Rect* value) {
585 RECT rect;
586 if (!::GetClientRect(core_hwnd_, &rect))
587 return E_FAIL;
588 value->Width = rect.right;
589 value->Height = rect.bottom;
590 return S_OK;
593 virtual HRESULT STDMETHODCALLTYPE get_CustomProperties(
594 winfoundtn::Collections::IPropertySet** value) {
595 return S_OK;
598 virtual HRESULT STDMETHODCALLTYPE get_Dispatcher(
599 winui::Core::ICoreDispatcher** value) {
600 return dispatcher_.CopyTo(value);
603 virtual HRESULT STDMETHODCALLTYPE get_FlowDirection(
604 winui::Core::CoreWindowFlowDirection* value) {
605 return S_OK;
608 virtual HRESULT STDMETHODCALLTYPE put_FlowDirection(
609 winui::Core::CoreWindowFlowDirection value) {
610 return S_OK;
613 virtual HRESULT STDMETHODCALLTYPE get_IsInputEnabled(
614 boolean* value) {
615 return S_OK;
618 virtual HRESULT STDMETHODCALLTYPE put_IsInputEnabled(
619 boolean value) {
620 return S_OK;
623 virtual HRESULT STDMETHODCALLTYPE get_PointerCursor(
624 winui::Core::ICoreCursor** value) {
625 return S_OK;
628 virtual HRESULT STDMETHODCALLTYPE put_PointerCursor(
629 winui::Core::ICoreCursor* value) {
630 return S_OK;
633 virtual HRESULT STDMETHODCALLTYPE get_PointerPosition(
634 winfoundtn::Point* value) {
635 return S_OK;
638 virtual HRESULT STDMETHODCALLTYPE get_Visible(
639 boolean* value) {
640 return S_OK;
643 virtual HRESULT STDMETHODCALLTYPE Activate(void) {
644 // After we fire OnActivate on the View, Chrome calls us back here.
645 return S_OK;
648 virtual HRESULT STDMETHODCALLTYPE Close(void) {
649 ::PostMessage(core_hwnd_, WM_CLOSE, 0, 0);
650 core_hwnd_ = NULL;
651 return S_OK;
654 virtual HRESULT STDMETHODCALLTYPE GetAsyncKeyState(
655 ABI::Windows::System::VirtualKey virtualKey,
656 winui::Core::CoreVirtualKeyStates* KeyState) {
657 return S_OK;
660 virtual HRESULT STDMETHODCALLTYPE GetKeyState(
661 ABI::Windows::System::VirtualKey virtualKey,
662 winui::Core::CoreVirtualKeyStates* KeyState) {
663 return S_OK;
666 virtual HRESULT STDMETHODCALLTYPE ReleasePointerCapture(void) {
667 return S_OK;
670 virtual HRESULT STDMETHODCALLTYPE SetPointerCapture(void) {
671 return S_OK;
674 virtual HRESULT STDMETHODCALLTYPE add_Activated(
675 WindowActivatedHandler* handler,
676 EventRegistrationToken* pCookie) {
677 // TODO(cpu) implement this.
678 return S_OK;
681 virtual HRESULT STDMETHODCALLTYPE remove_Activated(
682 EventRegistrationToken cookie) {
683 return S_OK;
686 virtual HRESULT STDMETHODCALLTYPE add_AutomationProviderRequested(
687 AutomationProviderHandler* handler,
688 EventRegistrationToken* cookie) {
689 return S_OK;
692 virtual HRESULT STDMETHODCALLTYPE remove_AutomationProviderRequested(
693 EventRegistrationToken cookie) {
694 return S_OK;
697 virtual HRESULT STDMETHODCALLTYPE add_CharacterReceived(
698 CharEventHandler* handler,
699 EventRegistrationToken* pCookie) {
700 character_received_handler_ = handler;
701 character_received_handler_->AddRef();
702 return S_OK;
705 virtual HRESULT STDMETHODCALLTYPE remove_CharacterReceived(
706 EventRegistrationToken cookie) {
707 character_received_handler_->Release();
708 character_received_handler_ = NULL;
709 return S_OK;
712 virtual HRESULT STDMETHODCALLTYPE add_Closed(
713 CoreWindowEventHandler* handler,
714 EventRegistrationToken* pCookie) {
715 return S_OK;
718 virtual HRESULT STDMETHODCALLTYPE remove_Closed(
719 EventRegistrationToken cookie) {
720 return S_OK;
723 virtual HRESULT STDMETHODCALLTYPE add_InputEnabled(
724 InputEnabledEventHandler* handler,
725 EventRegistrationToken* pCookie) {
726 return S_OK;
729 virtual HRESULT STDMETHODCALLTYPE remove_InputEnabled(
730 EventRegistrationToken cookie) {
731 return S_OK;
734 virtual HRESULT STDMETHODCALLTYPE add_KeyDown(
735 KeyEventHandler* handler,
736 EventRegistrationToken* pCookie) {
737 key_down_handler_ = handler;
738 key_down_handler_->AddRef();
739 return S_OK;
742 virtual HRESULT STDMETHODCALLTYPE remove_KeyDown(
743 EventRegistrationToken cookie) {
744 key_down_handler_->Release();
745 key_down_handler_ = NULL;
746 return S_OK;
749 virtual HRESULT STDMETHODCALLTYPE add_KeyUp(
750 KeyEventHandler* handler,
751 EventRegistrationToken* pCookie) {
752 key_up_handler_ = handler;
753 key_up_handler_->AddRef();
754 return S_OK;
757 virtual HRESULT STDMETHODCALLTYPE remove_KeyUp(
758 EventRegistrationToken cookie) {
759 key_up_handler_->Release();
760 key_up_handler_ = NULL;
761 return S_OK;
764 virtual HRESULT STDMETHODCALLTYPE add_PointerCaptureLost(
765 PointerEventHandler* handler,
766 EventRegistrationToken* cookie) {
767 mouse_capture_lost_handler_ = handler;
768 return S_OK;
771 virtual HRESULT STDMETHODCALLTYPE remove_PointerCaptureLost(
772 EventRegistrationToken cookie) {
773 mouse_capture_lost_handler_ = NULL;
774 return S_OK;
777 virtual HRESULT STDMETHODCALLTYPE add_PointerEntered(
778 PointerEventHandler* handler,
779 EventRegistrationToken* cookie) {
780 mouse_entered_handler_ = handler;
781 return S_OK;
784 virtual HRESULT STDMETHODCALLTYPE remove_PointerEntered(
785 EventRegistrationToken cookie) {
786 mouse_entered_handler_ = NULL;
787 return S_OK;
790 virtual HRESULT STDMETHODCALLTYPE add_PointerExited(
791 PointerEventHandler* handler,
792 EventRegistrationToken* cookie) {
793 mouse_exited_handler_ = handler;
794 return S_OK;
797 virtual HRESULT STDMETHODCALLTYPE remove_PointerExited(
798 EventRegistrationToken cookie) {
799 mouse_exited_handler_ = NULL;
800 return S_OK;
803 virtual HRESULT STDMETHODCALLTYPE add_PointerMoved(
804 PointerEventHandler* handler,
805 EventRegistrationToken* cookie) {
806 mouse_moved_handler_ = handler;
807 mouse_moved_handler_->AddRef();
808 return S_OK;
811 virtual HRESULT STDMETHODCALLTYPE remove_PointerMoved(
812 EventRegistrationToken cookie) {
813 mouse_moved_handler_->Release();
814 mouse_moved_handler_ = NULL;
815 return S_OK;
818 virtual HRESULT STDMETHODCALLTYPE add_PointerPressed(
819 PointerEventHandler* handler,
820 EventRegistrationToken* cookie) {
821 mouse_pressed_handler_ = handler;
822 mouse_pressed_handler_->AddRef();
823 return S_OK;
826 virtual HRESULT STDMETHODCALLTYPE remove_PointerPressed(
827 EventRegistrationToken cookie) {
828 mouse_pressed_handler_->Release();
829 mouse_pressed_handler_ = NULL;
830 return S_OK;
833 virtual HRESULT STDMETHODCALLTYPE add_PointerReleased(
834 PointerEventHandler* handler,
835 EventRegistrationToken* cookie) {
836 mouse_released_handler_ = handler;
837 mouse_released_handler_->AddRef();
838 return S_OK;
841 virtual HRESULT STDMETHODCALLTYPE remove_PointerReleased(
842 EventRegistrationToken cookie) {
843 mouse_released_handler_->Release();
844 mouse_released_handler_ = NULL;
845 return S_OK;
848 virtual HRESULT STDMETHODCALLTYPE add_TouchHitTesting(
849 TouchHitTestHandler* handler,
850 EventRegistrationToken* pCookie) {
851 return S_OK;
854 virtual HRESULT STDMETHODCALLTYPE remove_TouchHitTesting(
855 EventRegistrationToken cookie) {
856 return S_OK;
859 virtual HRESULT STDMETHODCALLTYPE add_PointerWheelChanged(
860 PointerEventHandler* handler,
861 EventRegistrationToken* cookie) {
862 mouse_wheel_changed_handler_ = handler;
863 mouse_wheel_changed_handler_->AddRef();
864 return S_OK;
867 virtual HRESULT STDMETHODCALLTYPE remove_PointerWheelChanged(
868 EventRegistrationToken cookie) {
869 mouse_wheel_changed_handler_->Release();
870 mouse_wheel_changed_handler_ = NULL;
871 return S_OK;
874 virtual HRESULT STDMETHODCALLTYPE add_SizeChanged(
875 SizeChangedHandler* handler,
876 EventRegistrationToken* pCookie) {
877 // TODO(cpu): implement this.
878 return S_OK;
881 virtual HRESULT STDMETHODCALLTYPE remove_SizeChanged(
882 EventRegistrationToken cookie) {
883 return S_OK;
886 virtual HRESULT STDMETHODCALLTYPE add_VisibilityChanged(
887 VisibilityChangedHandler* handler,
888 EventRegistrationToken* pCookie) {
889 return S_OK;
892 virtual HRESULT STDMETHODCALLTYPE remove_VisibilityChanged(
893 EventRegistrationToken cookie) {
894 return S_OK;
897 // ICoreWindowInterop implementation:
898 virtual HRESULT STDMETHODCALLTYPE get_WindowHandle(HWND* hwnd) {
899 if (!core_hwnd_)
900 return E_FAIL;
901 *hwnd = core_hwnd_;
902 return S_OK;
905 virtual HRESULT STDMETHODCALLTYPE put_MessageHandled(
906 boolean value) {
907 return S_OK;
910 // InputHandler
911 virtual bool HandleKeyboardMessage(const MSG& msg) OVERRIDE {
912 switch (msg.message) {
913 case WM_KEYDOWN:
914 case WM_KEYUP: {
915 mswr::ComPtr<winui::Core::IKeyEventArgs> event_args;
916 event_args = mswr::Make<KeyEvent>(msg);
917 KeyEventHandler* handler = NULL;
918 if (msg.message == WM_KEYDOWN) {
919 handler = key_down_handler_;
920 } else {
921 handler = key_up_handler_;
923 handler->Invoke(this, event_args.Get());
924 break;
927 case WM_CHAR:
928 case WM_DEADCHAR:
929 case WM_UNICHAR: {
930 mswr::ComPtr<winui::Core::ICharacterReceivedEventArgs> event_args;
931 event_args = mswr::Make<KeyEvent>(msg);
932 character_received_handler_->Invoke(this, event_args.Get());
933 break;
936 default:
937 return false;
939 return true;
942 virtual bool HandleMouseMessage(const MSG& msg) OVERRIDE {
943 PointerEventHandler* handler = NULL;
944 mswr::ComPtr<winui::Core::IPointerEventArgs> event_args;
945 event_args = mswr::Make<MouseEvent>(msg);
946 switch (msg.message) {
947 case WM_MOUSEMOVE: {
948 handler = mouse_moved_handler_;
949 break;
951 case WM_LBUTTONDOWN: {
952 case WM_RBUTTONDOWN:
953 case WM_MBUTTONDOWN:
954 handler = mouse_pressed_handler_;
955 break;
958 case WM_LBUTTONUP: {
959 case WM_RBUTTONUP:
960 case WM_MBUTTONUP:
961 handler = mouse_released_handler_;
962 break;
965 case WM_MOUSEWHEEL: {
966 case WM_MOUSEHWHEEL:
967 handler = mouse_wheel_changed_handler_;
968 break;
971 default:
972 return false;
974 DCHECK(handler);
975 handler->Invoke(this, event_args.Get());
976 return true;
979 private:
980 PointerEventHandler* mouse_moved_handler_;
981 PointerEventHandler* mouse_capture_lost_handler_;
982 PointerEventHandler* mouse_pressed_handler_;
983 PointerEventHandler* mouse_released_handler_;
984 PointerEventHandler* mouse_entered_handler_;
985 PointerEventHandler* mouse_exited_handler_;
986 PointerEventHandler* mouse_wheel_changed_handler_;
987 KeyEventHandler* key_down_handler_;
988 KeyEventHandler* key_up_handler_;
989 CharEventHandler* character_received_handler_;
990 HWND core_hwnd_;
991 mswr::ComPtr<winui::Core::ICoreDispatcher> dispatcher_;
994 class ActivatedEvent
995 : public mswr::RuntimeClass<winapp::Activation::IActivatedEventArgs> {
996 public:
997 ActivatedEvent(winapp::Activation::ActivationKind activation_kind)
998 : activation_kind_(activation_kind) {
1001 virtual HRESULT STDMETHODCALLTYPE get_Kind(
1002 winapp::Activation::ActivationKind *value) {
1003 *value = activation_kind_;
1004 return S_OK;
1007 virtual HRESULT STDMETHODCALLTYPE get_PreviousExecutionState(
1008 winapp::Activation::ApplicationExecutionState *value) {
1009 *value = winapp::Activation::ApplicationExecutionState_ClosedByUser;
1010 return S_OK;
1013 virtual HRESULT STDMETHODCALLTYPE get_SplashScreen(
1014 winapp::Activation::ISplashScreen **value) {
1015 return E_FAIL;
1018 private:
1019 winapp::Activation::ActivationKind activation_kind_;
1022 class CoreApplicationViewEmulation
1023 : public mswr::RuntimeClass<winapp::Core::ICoreApplicationView> {
1024 public:
1025 CoreApplicationViewEmulation() {
1026 core_window_ = mswr::Make<CoreWindowEmulation>();
1029 HRESULT Activate() {
1030 if (activated_handler_) {
1031 auto ae = mswr::Make<ActivatedEvent>(
1032 winapp::Activation::ActivationKind_File);
1033 return activated_handler_->Invoke(this, ae.Get());
1034 } else {
1035 return S_OK;
1039 HRESULT Close() {
1040 return core_window_->Close();
1043 // ICoreApplicationView implementation:
1044 virtual HRESULT STDMETHODCALLTYPE get_CoreWindow(
1045 winui::Core::ICoreWindow** value) {
1046 if (!core_window_)
1047 return E_FAIL;
1048 return core_window_.CopyTo(value);
1051 virtual HRESULT STDMETHODCALLTYPE add_Activated(
1052 ActivatedHandler* handler,
1053 EventRegistrationToken* token) {
1054 // The real component supports multiple handles but we don't yet.
1055 if (activated_handler_)
1056 return E_FAIL;
1057 activated_handler_ = handler;
1058 return S_OK;
1061 virtual HRESULT STDMETHODCALLTYPE remove_Activated(
1062 EventRegistrationToken token) {
1063 // Chrome never unregisters handlers, so we don't care about it.
1064 return S_OK;
1067 virtual HRESULT STDMETHODCALLTYPE get_IsMain(
1068 boolean* value) {
1069 return S_OK;
1072 virtual HRESULT STDMETHODCALLTYPE get_IsHosted(
1073 boolean* value) {
1074 return S_OK;
1077 private:
1078 mswr::ComPtr<CoreWindowEmulation> core_window_;
1079 mswr::ComPtr<ActivatedHandler> activated_handler_;
1082 class CoreApplicationWin7Emulation
1083 : public mswr::RuntimeClass<winapp::Core::ICoreApplication,
1084 winapp::Core::ICoreApplicationExit> {
1085 public:
1086 // ICoreApplication implementation:
1088 virtual HRESULT STDMETHODCALLTYPE get_Id(
1089 HSTRING* value) {
1090 return S_OK;
1093 virtual HRESULT STDMETHODCALLTYPE add_Suspending(
1094 winfoundtn::IEventHandler<winapp::SuspendingEventArgs*>* handler,
1095 EventRegistrationToken* token) {
1096 return S_OK;
1099 virtual HRESULT STDMETHODCALLTYPE remove_Suspending(
1100 EventRegistrationToken token) {
1101 return S_OK;
1104 virtual HRESULT STDMETHODCALLTYPE add_Resuming(
1105 winfoundtn::IEventHandler<IInspectable*>* handler,
1106 EventRegistrationToken* token) {
1107 return S_OK;
1110 virtual HRESULT STDMETHODCALLTYPE remove_Resuming(
1111 EventRegistrationToken token) {
1112 return S_OK;
1115 virtual HRESULT STDMETHODCALLTYPE get_Properties(
1116 winfoundtn::Collections::IPropertySet** value) {
1117 return S_OK;
1120 virtual HRESULT STDMETHODCALLTYPE GetCurrentView(
1121 winapp::Core::ICoreApplicationView** value) {
1122 return S_OK;
1125 virtual HRESULT STDMETHODCALLTYPE Run(
1126 winapp::Core::IFrameworkViewSource* viewSource) {
1127 HRESULT hr = viewSource->CreateView(app_view_.GetAddressOf());
1128 if (FAILED(hr))
1129 return hr;
1130 view_emulation_ = mswr::Make<CoreApplicationViewEmulation>();
1131 hr = app_view_->Initialize(view_emulation_.Get());
1132 if (FAILED(hr))
1133 return hr;
1134 mswr::ComPtr<winui::Core::ICoreWindow> core_window;
1135 hr = view_emulation_->get_CoreWindow(core_window.GetAddressOf());
1136 if (FAILED(hr))
1137 return hr;
1138 hr = app_view_->SetWindow(core_window.Get());
1139 if (FAILED(hr))
1140 return hr;
1141 hr = app_view_->Load(NULL);
1142 if (FAILED(hr))
1143 return hr;
1144 hr = view_emulation_->Activate();
1145 if (FAILED(hr))
1146 return hr;
1147 return app_view_->Run();
1150 virtual HRESULT STDMETHODCALLTYPE RunWithActivationFactories(
1151 winfoundtn::IGetActivationFactory* activationFactoryCallback) {
1152 return S_OK;
1155 // ICoreApplicationExit implementation:
1157 virtual HRESULT STDMETHODCALLTYPE Exit(void) {
1158 return view_emulation_->Close();
1161 virtual HRESULT STDMETHODCALLTYPE add_Exiting(
1162 winfoundtn::IEventHandler<IInspectable*>* handler,
1163 EventRegistrationToken* token) {
1164 return S_OK;
1167 virtual HRESULT STDMETHODCALLTYPE remove_Exiting(
1168 EventRegistrationToken token) {
1169 return S_OK;
1172 private:
1173 mswr::ComPtr<winapp::Core::IFrameworkView> app_view_;
1174 mswr::ComPtr<CoreApplicationViewEmulation> view_emulation_;
1178 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7() {
1179 HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
1180 if (FAILED(hr))
1181 CHECK(false);
1182 return mswr::Make<CoreApplicationWin7Emulation>();