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.
7 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
10 LPTHREAD_START_ROUTINE host_main;
19 void ODS(const char* str, LONG_PTR val = 0) {
21 size_t len = strlen(str);
23 ::OutputDebugStringA("ODS: buffer too long");
29 DWORD gle = ::GetLastError();
30 if (::IsDebuggerPresent())
32 wsprintfA(buf, "ODS:fatal %s (%p) gle=0x%x", str, val, gle);
33 ::MessageBoxA(NULL, buf, "!!!", MB_OK);
37 wsprintfA(buf, "ODS:%s (%p)\n", str, val);
38 ::OutputDebugStringA(buf);
42 LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
43 WPARAM wparam, LPARAM lparam) {
48 hdc = BeginPaint(hwnd, &ps);
53 ODS("Metro WM_DESTROY received");
56 return DefWindowProc(hwnd, message, wparam, lparam);
61 HWND CreateMetroTopLevelWindow() {
62 HINSTANCE hInst = reinterpret_cast<HINSTANCE>(&__ImageBase);
64 wcex.cbSize = sizeof(wcex);
65 wcex.style = CS_HREDRAW | CS_VREDRAW;
66 wcex.lpfnWndProc = WndProc;
69 wcex.hInstance = hInst;
71 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
72 wcex.hbrBackground = (HBRUSH)(COLOR_INACTIVECAPTION+1);
73 wcex.lpszMenuName = 0;
74 wcex.lpszClassName = L"Windows.UI.Core.CoreWindow";
77 HWND hwnd = ::CreateWindowExW(0,
78 MAKEINTATOM(::RegisterClassExW(&wcex)),
82 NULL, NULL, hInst, NULL);
86 DWORD WINAPI HostThread(void*) {
87 // The sleeps simulates the delay we have in the actual metro code
88 // which takes in account the corewindow being created and some other
89 // unknown machinations of metro.
90 ODS("Chrome main thread", ::GetCurrentThreadId());
92 return globals.host_main(globals.host_context);
95 extern "C" __declspec(dllexport)
96 int InitMetro(LPTHREAD_START_ROUTINE thread_proc, void* context) {
97 ODS("InitMetro [Win7 emulation]");
98 HWND window = CreateMetroTopLevelWindow();
101 // This magic incatation tells windows that the window is going fullscreen
102 // so the taskbar gets out of the wait automatically.
103 ::SetWindowPos(window,
106 GetSystemMetrics(SM_CXSCREEN),
107 GetSystemMetrics(SM_CYSCREEN),
110 // Ready to start our caller.
111 globals.core_window = window;
112 globals.host_main = thread_proc;
113 globals.host_context = context;
114 HANDLE thread = ::CreateThread(NULL, 0, &HostThread, NULL, 0, NULL);
116 // Main message loop.
118 while (GetMessage(&msg, NULL, 0, 0)) {
119 TranslateMessage(&msg);
120 DispatchMessage(&msg);
123 return (int) msg.wParam;
126 extern "C" _declspec(dllexport) HWND GetRootWindow() {
127 ODS("GetRootWindow", ULONG_PTR(globals.core_window));
128 return globals.core_window;
131 extern "C" _declspec(dllexport) void SetFrameWindow(HWND window) {
132 ODS("SetFrameWindow", ULONG_PTR(window));
133 globals.host_window = window;
136 extern "C" __declspec(dllexport) const wchar_t* GetInitialUrl() {